void ConfigLoad(Config *config, const char *filename) { // Load default values first ConfigLoadDefault(config); int configVersion = -1; FILE *f = fopen(filename, "r"); if (f == NULL) { printf("Error loading config '%s'\n", filename); return; } configVersion = ConfigGetVersion(f); fclose(f); switch (configVersion) { case 0: ConfigLoadOld(config, filename); break; case 1: case 2: case 3: case 4: ConfigLoadJSON(config, filename); break; default: printf("Unknown config version\n"); break; } }
Config ConfigLoad(const char *filename) { // Load default values first Config c = ConfigDefault(); if (filename == NULL) { // This is equivalent to loading nothing; just exit return c; } FILE *f = fopen(filename, "r"); if (f == NULL) { printf("Error loading config '%s'\n", filename); return c; } const int configVersion = ConfigGetVersion(f); fclose(f); switch (configVersion) { case 0: printf("Classic config is no longer supported\n"); break; case 1: case 2: case 3: case 4: case 5: case 6: case 7: ConfigLoadJSON(&c, filename); break; default: LOG(LM_MAIN, LL_ERROR, "Unknown config version"); break; } ConfigSetChanged(&c); return c; }
FEATURE(3, "Detect config version") SCENARIO("Detect JSON config version") { Config config1, config2; int version; FILE *file; GIVEN("a config file with some values, and I save the config to file in the JSON format") ConfigLoadDefault(&config1); config1.Game.FriendlyFire = 1; config1.Graphics.Brightness = 5; ConfigSave(&config1, "tmp"); GIVEN_END WHEN("I detect the version, and load a second config from that file") file = fopen("tmp", "r"); version = ConfigGetVersion(file); fclose(file); ConfigLoad(&config2, "tmp"); WHEN_END THEN("the version should be 4, and the two configs should be equal") SHOULD_INT_EQUAL(version, 4); SHOULD_MEM_EQUAL(&config1, &config2, sizeof(Config)); THEN_END } SCENARIO_END FEATURE_END FEATURE(4, "Save config as latest format by default") SCENARIO("Save as JSON by default") {