allegro 5 writing files when using physfs

433 Views Asked by At

i am currently trying to figure out a way to write a file (an allegro configuration file to be exact) to a mounted zip-file using physfs and allegro 5.

reading the config file works fine, but when it comes to writing the changed config, nothing happens (e.g. the file is not re-written and thus remains in it's old state).

also, when not using physfs, everything works perfectly.

here's the code i use:

Game::Game(int height, int width, int newDifficulty)
{

PHYSFS_init(NULL);
if (!PHYSFS_addToSearchPath("Data.zip", 1)) {
    // error handling
}
al_set_physfs_file_interface();

cfg = al_load_config_file("cfg.cfg");
if (cfg != NULL) // file exists, read from it
{
    const char *score = al_get_config_value(cfg, "", "highScore");
    highScore = atoi(score); // copy value
}
else // file does not exist, create it and init highScore to 0
{
    cfg = al_create_config();
    al_set_config_value(cfg, "", "highScore", "0");
    highScore = 0;
    al_save_config_file("cfg.cfg", cfg);
}
...
}

and in another function:

void Game::resetGame()
{
// high score
if (player->getScore() > highScore)
{
    highScore = player->getScore();
    // convert new highScore to char* that can be saved
    stringstream strs;
    strs << highScore;
    string temp_str = strs.str();
    char const* pchar = temp_str.c_str();

    if (cfg != NULL) // file exists, read from it
    {
        al_set_config_value(cfg, "", "highScore", pchar);
        al_save_config_file("cfg.cfg", cfg);
    }
}
...
}

since the code works without physfs, i guess i handle the config file itself correctly. any help would be highly appreciated!

cheers, hannes

1

There are 1 best solutions below

0
On

in the meantime, i solved the issue myself. apparently, physfs has no ability to write to an archive.

therefore, i need to PHYSFS_setWriteDir("jhdsaf"), save the cfg-file in that folder and then replace the original zip-file by an updated version with the cfg-file, just before the game closes (after all resources are unloaded because the zip is otherwise still in use).

if anyone is interested in the code to do this, just reply to this post!

hannes