1 #include "config.h"
2
3 #include "core/file.h"
4 #include "core/log.h"
5
6 #include <stdio.h>
7 #include <string.h>
8
9 #define MAX_LINE 100
10
11 static const char *INI_FILENAME = "julius.ini";
12
13 // Keep this in the same order as the config_keys in config.h
14 static const char *ini_keys[] = {
15 "gameplay_fix_immigration",
16 "gameplay_fix_100y_ghosts",
17 "screen_display_scale",
18 "screen_cursor_scale",
19 "ui_sidebar_info",
20 "ui_show_intro_video",
21 "ui_smooth_scrolling",
22 "ui_disable_mouse_edge_scrolling",
23 "ui_disable_map_drag",
24 "ui_visual_feedback_on_delete",
25 "ui_allow_cycling_temples",
26 "ui_show_water_structure_range",
27 "ui_show_construction_size",
28 "ui_highlight_legions",
29 "ui_show_military_sidebar"
30 };
31
32 static const char *ini_string_keys[] = {
33 "ui_language_dir"
34 };
35
36 static int values[CONFIG_MAX_ENTRIES];
37 static char string_values[CONFIG_STRING_MAX_ENTRIES][CONFIG_STRING_VALUE_MAX];
38
39 static int default_values[CONFIG_MAX_ENTRIES] = {
40 [CONFIG_SCREEN_DISPLAY_SCALE] = 100,
41 [CONFIG_SCREEN_CURSOR_SCALE] = 100
42 };
43 static const char default_string_values[CONFIG_STRING_MAX_ENTRIES][CONFIG_STRING_VALUE_MAX];
44
config_get(config_key key)45 int config_get(config_key key)
46 {
47 return values[key];
48 }
49
config_set(config_key key,int value)50 void config_set(config_key key, int value)
51 {
52 values[key] = value;
53 }
54
config_get_string(config_string_key key)55 const char *config_get_string(config_string_key key)
56 {
57 return string_values[key];
58 }
59
config_set_string(config_string_key key,const char * value)60 void config_set_string(config_string_key key, const char *value)
61 {
62 if (!value) {
63 string_values[key][0] = 0;
64 } else {
65 strncpy(string_values[key], value, CONFIG_STRING_VALUE_MAX - 1);
66 }
67 }
68
config_get_default_value(config_key key)69 int config_get_default_value(config_key key)
70 {
71 return default_values[key];
72 }
73
config_get_default_string_value(config_string_key key)74 const char *config_get_default_string_value(config_string_key key)
75 {
76 return default_string_values[key];
77 }
78
set_defaults(void)79 static void set_defaults(void)
80 {
81 for (int i = 0; i < CONFIG_MAX_ENTRIES; ++i) {
82 values[i] = default_values[i];
83 }
84 strncpy(string_values[CONFIG_STRING_UI_LANGUAGE_DIR],
85 default_string_values[CONFIG_STRING_UI_LANGUAGE_DIR], CONFIG_STRING_VALUE_MAX);
86 }
87
config_load(void)88 void config_load(void)
89 {
90 set_defaults();
91 FILE *fp = file_open(INI_FILENAME, "rt");
92 if (!fp) {
93 return;
94 }
95 char line_buffer[MAX_LINE];
96 char *line;
97 while ((line = fgets(line_buffer, MAX_LINE, fp))) {
98 // Remove newline from string
99 size_t size = strlen(line);
100 while (size > 0 && (line[size-1] == '\n' || line[size-1] == '\r')) {
101 line[--size] = 0;
102 }
103 char *equals = strchr(line, '=');
104 if (equals) {
105 *equals = 0;
106 for (int i = 0; i < CONFIG_MAX_ENTRIES; i++) {
107 if (strcmp(ini_keys[i], line) == 0) {
108 int value = atoi(&equals[1]);
109 log_info("Config key", ini_keys[i], value);
110 values[i] = value;
111 break;
112 }
113 }
114 for (int i = 0; i < CONFIG_STRING_MAX_ENTRIES; i++) {
115 if (strcmp(ini_string_keys[i], line) == 0) {
116 const char *value = &equals[1];
117 log_info("Config key", ini_string_keys[i], 0);
118 log_info("Config value", value, 0);
119 strncpy(string_values[i], value, CONFIG_STRING_VALUE_MAX - 1);
120 break;
121 }
122 }
123 }
124 }
125 file_close(fp);
126 }
127
config_save(void)128 void config_save(void)
129 {
130 FILE *fp = file_open(INI_FILENAME, "wt");
131 if (!fp) {
132 log_error("Unable to write configuration file", INI_FILENAME, 0);
133 return;
134 }
135 for (int i = 0; i < CONFIG_MAX_ENTRIES; i++) {
136 fprintf(fp, "%s=%d\n", ini_keys[i], values[i]);
137 }
138 for (int i = 0; i < CONFIG_STRING_MAX_ENTRIES; i++) {
139 fprintf(fp, "%s=%s\n", ini_string_keys[i], string_values[i]);
140 }
141 file_close(fp);
142 }
143