|
|
|
@ -14,8 +14,6 @@ |
|
|
|
|
|
|
|
|
|
static struct Hashmap* cvars = NULL; |
|
|
|
|
|
|
|
|
|
static void config_on_parser_assign(const char* key, const char* value, const char* filename, int current_line); |
|
|
|
|
|
|
|
|
|
void config_vars_init(void) |
|
|
|
|
{ |
|
|
|
|
cvars = hashmap_new(); |
|
|
|
@ -46,39 +44,53 @@ struct Hashmap* config_vars_get(void) |
|
|
|
|
return cvars; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void config_on_parser_assign(const char* key, const char* value, const char* filename, int current_line) |
|
|
|
|
{ |
|
|
|
|
struct Variant* cvar = hashmap_value_get(cvars, key); |
|
|
|
|
if(!cvar) |
|
|
|
|
{ |
|
|
|
|
log_warning("Unknown config key '%s' in file %s, line %d", key, filename, current_line); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
variant_from_str(cvar, value, cvar->type); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool config_vars_load(const char* filename, int directory_type) |
|
|
|
|
{ |
|
|
|
|
bool success = false; |
|
|
|
|
FILE* config_file = io_file_open(directory_type, filename, "rb"); |
|
|
|
|
if(!config_file) |
|
|
|
|
{ |
|
|
|
|
log_error("config:vars_load", "Could not open %s", filename); |
|
|
|
|
return success; |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(!parser_load(config_file, filename, &config_on_parser_assign, false, 0)) |
|
|
|
|
{ |
|
|
|
|
log_error("config_vars:load", "Failed to parse config file %s", filename); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
log_message("Loaded config from %s", filename); |
|
|
|
|
success = true; |
|
|
|
|
} |
|
|
|
|
struct Parser* parser = parser_load_objects(config_file, filename); |
|
|
|
|
if(!parser) |
|
|
|
|
{ |
|
|
|
|
log_error("config_vars:load", "Failed to load config data from %s", filename); |
|
|
|
|
fclose(config_file); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool config_loaded = false; |
|
|
|
|
for(int i = 0; i < array_len(parser->objects); i++) |
|
|
|
|
{ |
|
|
|
|
struct Parser_Object* object = &parser->objects[i]; |
|
|
|
|
if(object->type != PO_CONFIG) |
|
|
|
|
{ |
|
|
|
|
log_warning("Unexpected config object type %s in %s", parser_object_type_to_str(object->type), filename); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
config_loaded = true; |
|
|
|
|
char* key = NULL; |
|
|
|
|
struct Variant* value = NULL; |
|
|
|
|
char variant_str[MAX_VARIANT_STR_LEN]; |
|
|
|
|
HASHMAP_FOREACH(object->data, key, value) |
|
|
|
|
{ |
|
|
|
|
struct Variant* existing_val = hashmap_value_get(cvars, key); |
|
|
|
|
if(!existing_val) |
|
|
|
|
{ |
|
|
|
|
log_warning("Unkown key '%s' in config file %s", key, filename); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
variant_copy(existing_val, value); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(config_loaded) log_message("Loaded config from %s", filename); |
|
|
|
|
fclose(config_file); |
|
|
|
|
return success; |
|
|
|
|
return config_loaded; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool config_vars_save(const char* filename, int directory_type) |
|
|
|
@ -91,17 +103,32 @@ bool config_vars_save(const char* filename, int directory_type) |
|
|
|
|
return success; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
char* key = NULL; |
|
|
|
|
struct Variant* value = NULL; |
|
|
|
|
char variant_str[MAX_VARIANT_STR_LEN]; |
|
|
|
|
HASHMAP_FOREACH(cvars, key, value) |
|
|
|
|
struct Parser* parser = parser_new(); |
|
|
|
|
if(!parser) |
|
|
|
|
{ |
|
|
|
|
log_error("config_vars:save", "Could not create Parser for %s", filename); |
|
|
|
|
fclose(config_file); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
struct Parser_Object* object = parser_object_new(parser, PO_CONFIG); |
|
|
|
|
if(!object) |
|
|
|
|
{ |
|
|
|
|
log_error("config_vars:save", "Could not create Parser_Object for %s", filename); |
|
|
|
|
parser_free(parser); |
|
|
|
|
fclose(config_file); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
hashmap_copy(cvars, object->data); |
|
|
|
|
|
|
|
|
|
if(!parser_write_objects(parser, config_file, filename)) |
|
|
|
|
{ |
|
|
|
|
memset(variant_str, '\0', MAX_VARIANT_STR_LEN); |
|
|
|
|
variant_to_str(value, variant_str, MAX_VARIANT_STR_LEN); |
|
|
|
|
fprintf(config_file, "%s: %s\n", key, variant_str); |
|
|
|
|
log_error("config_vars:save", "Failed to write config to '%s'", filename); |
|
|
|
|
success = false; |
|
|
|
|
} |
|
|
|
|
log_message("Config file %s written.", filename); |
|
|
|
|
success = true; |
|
|
|
|
|
|
|
|
|
parser_free(parser); |
|
|
|
|
fclose(config_file); |
|
|
|
|
return success; |
|
|
|
|
} |
|
|
|
|