|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #include "IZ_config.h"
-
- #ifdef _WIN64
- void realpath(const char* filename, char* resolved_path) {
- TCHAR** lppPart = { NULL };
- GetFullPathNameA(filename, 256, resolved_path, lppPart);
- }
- #endif
-
- /**
- * Writes the configuration to a file.
- * @param config The pointer to the configuration data.
- * @param filename The filename of the destination file.
- * @return Error message, or NULL if there are no errors.
- */
- unsigned int IZ_ConfigSave(IZ_Config* config, const char* filename) {
- char resolved_path[256];
- realpath(filename, resolved_path);
- IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC, "Persisting configuration to: %s", resolved_path);
- FILE* output = fopen(filename, "w");
- if (output == NULL) {
- IZ_LogError("Could not persist config.");
- return 1;
- }
- fprintf(output, "[Window]\n");
- fprintf(output, "Width = %d\n", config->window.width);
- fprintf(output, "Height = %d\n", config->window.height);
-
- fprintf(output, "\n[Pool]\n");
- fprintf(output, "Characters = %d\n", config->pool.characters);
-
- unsigned int p;
- unsigned int i;
- for (p = 0; p < IZ_MAX_PLAYERS; p += 1) {
- fprintf(output, "\n[Mapping(%d).Keyboard]\n", p);
- for (i = 0; i < 16; i += 1) {
- fprintf(output, "%s = %d\n", IZ_ACTION_NAMES[i], config->mapping[p].keyboard[i]);
- }
- fprintf(output, "\n[Mapping(%d).Joystick]\n", p);
- for (i = 0; i < 12; i += 1) {
- fprintf(output, "%s = %d\n", IZ_ACTION_NAMES[i + 4], config->mapping[p].joystick[i]);
- }
- }
- fclose(output);
- IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC, "Config saved successfully.");
- return 0;
- }
-
- /**
- * Reads the configuration from a file.
- * @param config The pointer to the configuration data.
- * @param filename The filename of the destination file.
- * @return Error message, or NULL if there are no errors.
- */
- unsigned int IZ_ConfigLoad(IZ_Config* config, const char* filename) {
- char resolved_path[256];
- realpath(filename, resolved_path);
- IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC, "Loading configuration file: %s", resolved_path);
- FILE* input = fopen(filename, "r");
- unsigned int p;
- unsigned int i;
- if (input == NULL) {
- IZ_LogWarn(false, "Config file not found! Loading defaults.");
- *config = (IZ_Config) {
- .window = { .width = 320, .height = 240 },
- .pool = {
- .characters = 64
- },
- .mapping = {
- // player 1
- {
- .keyboard = {
- IZ_SCANCODE_D,
- IZ_SCANCODE_S,
- IZ_SCANCODE_A,
- IZ_SCANCODE_W,
- IZ_SCANCODE_RETURN,
- IZ_SCANCODE_BACKSPACE,
- IZ_SCANCODE_M,
- IZ_SCANCODE_COMMA,
- IZ_SCANCODE_PERIOD,
- IZ_SCANCODE_SLASH,
- IZ_SCANCODE_J,
- IZ_SCANCODE_K,
- IZ_SCANCODE_L,
- IZ_SCANCODE_SEMICOLON,
- IZ_SCANCODE_I,
- IZ_SCANCODE_O,
- },
- .joystick = {
- IZ_JOYSTICK_CODE_0,
- IZ_JOYSTICK_CODE_1,
- IZ_JOYSTICK_CODE_2,
- IZ_JOYSTICK_CODE_3,
- IZ_JOYSTICK_CODE_4,
- IZ_JOYSTICK_CODE_5,
- IZ_JOYSTICK_CODE_6,
- IZ_JOYSTICK_CODE_7,
- IZ_JOYSTICK_CODE_8,
- IZ_JOYSTICK_CODE_9,
- IZ_JOYSTICK_CODE_10,
- IZ_JOYSTICK_CODE_11,
- }
- }
- }
- };
-
- unsigned int persist_status = IZ_ConfigSave(config, filename);
- if (persist_status != 0) {
- return persist_status;
- }
- IZ_Log("Window.Width=%d", config->window.width);
- IZ_Log("Window.Height=%d", config->window.height);
- for (p = 0; p < IZ_MAX_PLAYERS; p += 1) {
- for (i = 0; i < 16; i += 1) {
- IZ_Log("Mapping(%d).Keyboard.%s=%02x", p, IZ_ACTION_NAMES[i], config->mapping[p].keyboard[i]);
- }
- for (i = 0; i < 12; i += 1) {
- IZ_Log("Mapping(%d).Joystick.%s=%02x", p, IZ_ACTION_NAMES[i + 4], config->mapping[p].joystick[i]);
- }
- }
- } else {
- fscanf(input, "[Window]\n");
- fscanf(input, "Width = %d\n", &config->window.width);
- IZ_Log("Window.Width=%d", config->window.width);
-
- fscanf(input, "Height = %d\n", &config->window.height);
- IZ_Log("Window.Height=%d", config->window.height);
-
- fscanf(input, "\n[Pool]\n");
- fscanf(input, "Characters = %d\n", &config->pool.characters);
- IZ_Log("Pool.Characters=%d", config->pool.characters);
-
- static char action_name[7];
- int player_index;
- for (p = 0; p < IZ_MAX_PLAYERS; p += 1) {
- fscanf(input, "\n[Mapping(%d).Keyboard]\n", &player_index);
- for (i = 0; i < 16; i += 1) {
- fscanf(input, "%s = %d\n", action_name, &config->mapping[player_index].keyboard[i]);
- IZ_Log("Mapping(%d).Keyboard.%s=%02x", player_index, action_name, config->mapping[player_index].keyboard[i]);
- }
- fscanf(input, "\n[Mapping(%d).Joystick]\n", &player_index);
- for (i = 0; i < 12; i += 1) {
- fscanf(input, "%s = %d\n", action_name, &config->mapping[player_index].joystick[i]);
- IZ_Log("Mapping(%d).Joystick.%s=%02x", player_index, action_name, config->mapping[player_index].joystick[i]);
- }
- }
- }
- fclose(input);
- IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC,"%s", "Config loaded successfully.");
- return 0;
- }
|