Rev 44 | Rev 59 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | // config.cpp |
2 | |||
3 | #include "common.h" |
||
4 | |||
5 | |||
6 | // useful macros |
||
32 | pmbaty | 7 | #define READ_WIDESTRING(dest,inifile,section,key,default_value) wcscpy_s ((dest), sizeof (dest) / sizeof (wchar_t), INIFile_ReadEntryAsString ((inifile), (section), (key), (default_value))) |
1 | pmbaty | 8 | #define WRITE_WIDESTRING(section,key,value) INIFile_WriteEntryAsString (inifile, (section), (key), (value)) |
9 | |||
10 | |||
40 | pmbaty | 11 | // global variables used in this module only |
12 | static wchar_t filename[MAX_PATH]; |
||
13 | |||
14 | |||
1 | pmbaty | 15 | void Config_Load (void) |
16 | { |
||
17 | // this function opens and parses the INI configuration file for the program |
||
18 | // WARNING: it does also build the smilies list ! |
||
19 | |||
20 | void *inifile; |
||
21 | smiley_t *smiley; |
||
22 | char *file_buffer; |
||
23 | wchar_t smiley_pathname[MAX_PATH]; |
||
24 | wchar_t *smiley_name; |
||
25 | int smiley_index; |
||
26 | int file_length; |
||
27 | FILE *fp; |
||
28 | |||
29 | // open the INI file |
||
11 | pmbaty | 30 | swprintf_s (filename, WCHAR_SIZEOF (filename), L"%s/config.ini", app_path); |
1 | pmbaty | 31 | inifile = INIFile_LoadINIFile (filename); |
32 | |||
33 | // read the INI file (if it doesn't exist, default values will be fed) |
||
34 | |||
35 | // [gameplay] |
||
36 | options.want_lastmove = (INIFile_ReadEntryAsBool (inifile, L"options", L"highlight last move", true) > 0); |
||
37 | options.want_possiblemoves = (INIFile_ReadEntryAsBool (inifile, L"options", L"highlight possible moves", true) > 0); |
||
38 | options.want_threats = (INIFile_ReadEntryAsBool (inifile, L"options", L"highlight king's threats", true) > 0); |
||
39 | options.want_animations = (INIFile_ReadEntryAsBool (inifile, L"options", L"display part animations", true) > 0); |
||
40 | options.want_takenparts = (INIFile_ReadEntryAsBool (inifile, L"options", L"show taken parts", true) > 0); |
||
41 | options.want_turn = (INIFile_ReadEntryAsBool (inifile, L"options", L"show turn", true) > 0); |
||
42 | options.want_clock = (INIFile_ReadEntryAsBool (inifile, L"options", L"show game clock", true) > 0); |
||
43 | options.clock_color = INIFile_ReadEntryAsUnsignedLong (inifile, L"options", L"game clock color", 0x00007f00); // dark blue, in RGBA |
||
44 | options.want_history = (INIFile_ReadEntryAsBool (inifile, L"options", L"show game history", true) > 0); |
||
45 | options.history_color = INIFile_ReadEntryAsUnsignedLong (inifile, L"options", L"game history color", 0xe7e7e700); // light grey, in RGBA |
||
46 | options.want_sepiafilter = (INIFile_ReadEntryAsBool (inifile, L"options", L"use sepia filter for past moves", true) > 0); |
||
47 | options.want_autorotateon1vs1 = (INIFile_ReadEntryAsBool (inifile, L"options", L"auto-rotate board",true) > 0); |
||
48 | options.rotate_speed = INIFile_ReadEntryAsLong (inifile, L"options", L"rotation speed", 5); |
||
49 | |||
50 | // [display] |
||
51 | options.want_fullscreen = (INIFile_ReadEntryAsBool (inifile, L"display", L"fullscreen", false) > 0); |
||
52 | options.window_width = INIFile_ReadEntryAsLong (inifile, L"display", L"window width", 1024); |
||
53 | options.window_height = INIFile_ReadEntryAsLong (inifile, L"display", L"window height", 768); |
||
54 | options.want_filtering = (INIFile_ReadEntryAsBool (inifile, L"display", L"enable texture filtering", true) > 0); |
||
55 | options.want_hiquality = (INIFile_ReadEntryAsBool (inifile, L"display", L"enable high quality filtering", true) > 0); |
||
56 | options.want_specularlighting = (INIFile_ReadEntryAsBool (inifile, L"display", L"enable specular lighting", true) > 0); |
||
57 | options.want_reflections = (INIFile_ReadEntryAsBool (inifile, L"display", L"enable board reflections", true) > 0); |
||
58 | |||
59 | // [sound] |
||
60 | options.want_sounds = (INIFile_ReadEntryAsBool (inifile, L"sound", L"play sounds", true) > 0); |
||
61 | |||
62 | // [theme] |
||
32 | pmbaty | 63 | READ_WIDESTRING (wantedtheme_name, inifile, L"theme", L"theme name", L"Marble"); |
1 | pmbaty | 64 | want_grid = (INIFile_ReadEntryAsBool (inifile, L"theme", L"show grid", false) > 0); |
65 | want_flaticons = (INIFile_ReadEntryAsBool (inifile, L"theme", L"flat icons instead", false) > 0); |
||
66 | want_custombackground = (INIFile_ReadEntryAsBool (inifile, L"theme", L"use custom background", false) > 0); |
||
32 | pmbaty | 67 | READ_WIDESTRING (custombackground_pathname, inifile, L"theme", L"custom background picture", L""); |
1 | pmbaty | 68 | |
69 | // [engine] |
||
32 | pmbaty | 70 | READ_WIDESTRING (options.engine.program, inifile, L"engine", L"program", L"Crafty"); |
1 | pmbaty | 71 | options.engine.depth = INIFile_ReadEntryAsLong (inifile, L"engine", L"allowed depth", 4); |
72 | options.engine.max_depth = INIFile_ReadEntryAsLong (inifile, L"engine", L"maximum depth", 100); |
||
73 | options.engine.blunder_chances = INIFile_ReadEntryAsLong (inifile, L"engine", L"blunder chances", 0); |
||
74 | options.engine.obstinacy_level = INIFile_ReadEntryAsLong (inifile, L"engine", L"obstinacy", 0); |
||
29 | pmbaty | 75 | options.engine.is_expert_mode = (INIFile_ReadEntryAsBool (inifile, L"engine", L"expert mode", false) > 0); |
1 | pmbaty | 76 | |
77 | // [network] |
||
32 | pmbaty | 78 | READ_WIDESTRING (options.network.server_address, inifile, L"network", L"server address", L"freechess.org"); |
1 | pmbaty | 79 | options.network.server_port = INIFile_ReadEntryAsLong (inifile, L"network", L"server port", 5000); |
32 | pmbaty | 80 | READ_WIDESTRING (options.network.login, inifile, L"network", L"login", L"guest"); |
81 | READ_WIDESTRING (options.network.password, inifile, L"network", L"password", L""); |
||
1 | pmbaty | 82 | options.network.want_servermessages = (INIFile_ReadEntryAsBool (inifile, L"network", L"show server messages", true) > 0); |
83 | options.network.want_publicchat = (INIFile_ReadEntryAsBool (inifile, L"network", L"show public chat", true) > 0); |
||
84 | options.network.want_motdonconnect = (INIFile_ReadEntryAsBool (inifile, L"network", L"show MOTD on connect", true) > 0); |
||
85 | |||
14 | pmbaty | 86 | // [registration] |
32 | pmbaty | 87 | READ_WIDESTRING (options.registration.user_email, inifile, L"registration", L"user email", L""); |
14 | pmbaty | 88 | options.registration.activation_code = INIFile_ReadEntryAsLong (inifile, L"registration", L"activation code", 0); |
89 | |||
1 | pmbaty | 90 | // [smilies] |
91 | smilies = NULL; |
||
92 | smiley_count = 0; |
||
93 | while ((smiley_name = INIFile_GetEntryName (inifile, L"smilies", smiley_count)) != NULL) |
||
94 | { |
||
95 | // for each smiley we can read, reallocate smilies array to hold one smiley more |
||
96 | smilies = (smiley_t *) SAFE_realloc (smilies, smiley_count, smiley_count + 1, sizeof (smiley_t), false); |
||
97 | |||
98 | smiley = &smilies[smiley_count]; // quick access to smiley |
||
99 | |||
100 | // read the smiley name and convert quotes to spaces |
||
101 | wcscpy_s (smiley->name, WCHAR_SIZEOF (smiley->name), smiley_name); |
||
102 | smiley->name[0] = L' '; // convert leading quote to a space |
||
103 | smiley->name[wcslen (smiley->name) - 1] = L' '; // convert ending quote to a space |
||
104 | |||
105 | // now read the smiley's corresponding picture file |
||
32 | pmbaty | 106 | READ_WIDESTRING (smiley->filename, inifile, L"smilies", smiley_name, L"smile.wmf"); |
1 | pmbaty | 107 | |
108 | // is this filename the same as another smiley we know already ? |
||
109 | for (smiley_index = 0; smiley_index < smiley_count; smiley_index++) |
||
110 | if (wcscmp (smilies[smiley_index].filename, smiley->filename) == 0) |
||
111 | break; // break as soon as we find a match |
||
112 | |||
113 | // did we find a match ? |
||
114 | if (smiley_index < smiley_count) |
||
115 | { |
||
116 | // if so, just copy data and data length from the smiley we already know |
||
117 | smiley->rtf_data = smilies[smiley_index].rtf_data; |
||
118 | smiley->rtf_len = smilies[smiley_index].rtf_len; |
||
119 | } |
||
120 | |||
121 | // else we found no match, we have to read the file ourselves |
||
122 | else |
||
123 | { |
||
124 | // build the smiley picture's file pathname and read that smiley's data as a hex string |
||
11 | pmbaty | 125 | swprintf_s (smiley_pathname, WCHAR_SIZEOF (smiley_pathname), L"%s/data/smilies/%s", app_path, smiley->filename); |
1 | pmbaty | 126 | _wfopen_s (&fp, smiley_pathname, L"rb"); |
127 | if (fp == NULL) |
||
128 | continue; // if this smiley picture file can't be opened, ignore this smiley |
||
129 | |||
130 | // get the file length |
||
131 | fseek (fp, 0, SEEK_END); |
||
132 | file_length = ftell (fp); |
||
133 | fseek (fp, 0, SEEK_SET); |
||
134 | |||
135 | // mallocate space for file contents and read it |
||
136 | file_buffer = (char *) SAFE_malloc (1 + file_length + 1 + 1, sizeof (char), false); // include null terminator |
||
137 | file_buffer[0] = ' '; // first character is a whitespace |
||
138 | fread (&file_buffer[1], file_length, sizeof (char), fp); |
||
139 | file_buffer[1 + file_length] = ' '; // last character is a whitespace |
||
140 | file_buffer[1 + file_length + 1] = 0; // terminate the char string |
||
141 | fclose (fp); // finished with the smiley picture file |
||
142 | |||
143 | // convert it to wide char |
||
144 | smiley->rtf_len = 1 + file_length + 1; |
||
145 | smiley->rtf_data = (wchar_t *) SAFE_malloc (smiley->rtf_len + 1, sizeof (wchar_t), false); // include null terminator |
||
146 | ConvertToWideChar (smiley->rtf_data, smiley->rtf_len + 1, file_buffer); |
||
147 | SAFE_free ((void **) &file_buffer); |
||
148 | } |
||
149 | |||
150 | smiley_count++; // we know now one smiley more |
||
151 | } |
||
152 | |||
153 | // close the INI file |
||
154 | INIFile_FreeINIFile (inifile); |
||
155 | return; // finished loading config |
||
156 | } |
||
157 | |||
158 | |||
40 | pmbaty | 159 | void Config_LoadEngine (const wchar_t *engine_name) |
160 | { |
||
161 | // this function loads the engine-specific parameters. The best place to call it is at the beginning |
||
162 | // of a game versus the computer, just before the engine executable process is started. |
||
163 | |||
164 | void *engine_inifile; |
||
165 | wchar_t *ptr; |
||
166 | |||
167 | // reset values first |
||
168 | memset (&options.engine.program_options, 0, sizeof (options.engine.program_options)); |
||
169 | |||
170 | // given the selected engine program, load and interpret the right engine config file |
||
171 | swprintf_s (filename, WCHAR_SIZEOF (filename), L"%s/engines/%s/engine.ini", app_path, engine_name); |
||
172 | engine_inifile = INIFile_LoadINIFile (filename); |
||
173 | if (engine_inifile == NULL) |
||
174 | return; // engine file not found |
||
175 | |||
176 | // basic program information |
||
177 | READ_WIDESTRING (options.engine.program_options.name, engine_inifile, L"program", L"name", L"Crafty v24.0"); |
||
178 | READ_WIDESTRING (options.engine.program_options.cmdline, engine_inifile, L"program", L"executable", L"crafty.exe"); |
||
179 | |||
180 | // reply string patterns |
||
181 | READ_WIDESTRING (options.engine.program_options.replystring_move, engine_inifile, L"reply strings", L"move", L"): "); |
||
182 | |||
183 | // engine commands |
||
184 | READ_WIDESTRING (options.engine.program_options.command_new, engine_inifile, L"commands", L"new game", L"new"); |
||
185 | for (ptr = options.engine.program_options.command_new; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n'; |
||
186 | READ_WIDESTRING (options.engine.program_options.command_setboard, engine_inifile, L"commands", L"setup table from FEN", L"setboard %s"); |
||
187 | for (ptr = options.engine.program_options.command_setboard; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n'; |
||
188 | READ_WIDESTRING (options.engine.program_options.command_sd, engine_inifile, L"commands", L"search depth set", L"sd %d"); |
||
189 | for (ptr = options.engine.program_options.command_sd; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n'; |
||
190 | READ_WIDESTRING (options.engine.program_options.command_go, engine_inifile, L"commands", L"play", L"go"); |
||
191 | for (ptr = options.engine.program_options.command_go; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n'; |
||
44 | pmbaty | 192 | READ_WIDESTRING (options.engine.program_options.command_move, engine_inifile, L"commands", L"move", L"%s"); |
193 | for (ptr = options.engine.program_options.command_move; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n'; |
||
40 | pmbaty | 194 | READ_WIDESTRING (options.engine.program_options.command_force, engine_inifile, L"commands", L"force move", L"force %s"); |
195 | for (ptr = options.engine.program_options.command_force; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n'; |
||
196 | READ_WIDESTRING (options.engine.program_options.command_quit, engine_inifile, L"commands", L"quit", L"quit"); |
||
197 | for (ptr = options.engine.program_options.command_quit; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n'; |
||
198 | |||
199 | INIFile_FreeINIFile (engine_inifile); // close the engine config file |
||
200 | return; // engine parameters loaded successfully |
||
201 | } |
||
202 | |||
203 | |||
1 | pmbaty | 204 | void Config_Save (void) |
205 | { |
||
206 | // this function saves the software configuration into an INI file. |
||
207 | // WARNING: it does also free the smilies list ! |
||
208 | |||
209 | wchar_t smiley_name[32]; |
||
210 | smiley_t *smiley; |
||
211 | void *inifile; |
||
212 | int smiley_index; |
||
213 | int smiley_index2; |
||
214 | |||
215 | // create a config file with the default values |
||
216 | inifile = INIFile_NewINIFile (); |
||
217 | |||
218 | // [gameplay] |
||
219 | INIFile_WriteEntryAsBool (inifile, L"options", L"highlight last move", options.want_lastmove); |
||
220 | INIFile_WriteEntryAsBool (inifile, L"options", L"highlight possible moves", options.want_possiblemoves); |
||
221 | INIFile_WriteEntryAsBool (inifile, L"options", L"highlight king's threats", options.want_threats); |
||
222 | INIFile_WriteEntryAsBool (inifile, L"options", L"display part animations", options.want_animations); |
||
223 | INIFile_WriteEntryAsBool (inifile, L"options", L"show taken parts", options.want_takenparts); |
||
224 | INIFile_WriteEntryAsBool (inifile, L"options", L"show turn", options.want_turn); |
||
225 | INIFile_WriteEntryAsBool (inifile, L"options", L"show game clock", options.want_clock); |
||
226 | INIFile_WriteEntryAsUnsignedLong (inifile, L"options", L"game clock color", options.clock_color); |
||
227 | INIFile_WriteEntryAsBool (inifile, L"options", L"show game history", options.want_history); |
||
228 | INIFile_WriteEntryAsUnsignedLong (inifile, L"options", L"game history color", options.history_color); |
||
229 | INIFile_WriteEntryAsBool (inifile, L"options", L"use sepia filter for past moves", options.want_sepiafilter); |
||
230 | INIFile_WriteEntryAsBool (inifile, L"options", L"auto-rotate board", options.want_autorotateon1vs1); |
||
231 | INIFile_WriteEntryAsLong (inifile, L"options", L"rotation speed", options.rotate_speed); |
||
232 | |||
233 | // [display] |
||
234 | INIFile_WriteEntryAsBool (inifile, L"display", L"fullscreen", options.want_fullscreen); |
||
3 | pmbaty | 235 | INIFile_WriteEntryAsLong (inifile, L"display", L"window width", max (options.window_width, 640)); |
236 | INIFile_WriteEntryAsLong (inifile, L"display", L"window height", max (options.window_height, 480)); |
||
1 | pmbaty | 237 | INIFile_WriteEntryAsBool (inifile, L"display", L"enable texture filtering", options.want_filtering); |
238 | INIFile_WriteEntryAsBool (inifile, L"display", L"enable high quality filtering", options.want_hiquality); |
||
239 | INIFile_WriteEntryAsBool (inifile, L"display", L"enable specular lighting", options.want_specularlighting); |
||
240 | INIFile_WriteEntryAsBool (inifile, L"display", L"enable board reflections", options.want_reflections); |
||
241 | |||
242 | // [sound] |
||
243 | INIFile_WriteEntryAsBool (inifile, L"sound", L"play sounds", options.want_sounds); |
||
244 | |||
245 | // [theme] |
||
246 | WRITE_WIDESTRING (L"theme", L"theme name", (wantedtheme_name[0] != 0 ? wantedtheme_name : L"Marble")); |
||
247 | INIFile_WriteEntryAsBool (inifile, L"theme", L"show grid", want_grid); |
||
248 | INIFile_WriteEntryAsBool (inifile, L"theme", L"flat icons instead", want_flaticons); |
||
249 | INIFile_WriteEntryAsBool (inifile, L"theme", L"use custom background", want_custombackground); |
||
250 | WRITE_WIDESTRING (L"theme", L"custom background picture", custombackground_pathname); |
||
251 | |||
252 | // [engine] |
||
32 | pmbaty | 253 | WRITE_WIDESTRING (L"engine", L"program", options.engine.program); |
1 | pmbaty | 254 | INIFile_WriteEntryAsLong (inifile, L"engine", L"allowed depth", options.engine.depth); |
255 | INIFile_WriteEntryAsLong (inifile, L"engine", L"maximum depth", options.engine.max_depth); |
||
256 | INIFile_WriteEntryAsLong (inifile, L"engine", L"blunder chances", options.engine.blunder_chances); |
||
257 | INIFile_WriteEntryAsLong (inifile, L"engine", L"obstinacy", options.engine.obstinacy_level); |
||
29 | pmbaty | 258 | INIFile_WriteEntryAsBool (inifile, L"engine", L"expert mode", options.engine.is_expert_mode); |
1 | pmbaty | 259 | |
260 | // [network] |
||
261 | WRITE_WIDESTRING (L"network", L"server address", options.network.server_address); |
||
262 | INIFile_WriteEntryAsLong (inifile, L"network", L"server port", options.network.server_port); |
||
263 | WRITE_WIDESTRING (L"network", L"login", options.network.login); |
||
264 | WRITE_WIDESTRING (L"network", L"password", options.network.password); |
||
265 | INIFile_WriteEntryAsBool (inifile, L"network", L"show server messages", options.network.want_servermessages); |
||
266 | INIFile_WriteEntryAsBool (inifile, L"network", L"show public chat", options.network.want_publicchat); |
||
267 | INIFile_WriteEntryAsBool (inifile, L"network", L"show MOTD on connect", options.network.want_motdonconnect); |
||
268 | |||
14 | pmbaty | 269 | // [registration] |
270 | WRITE_WIDESTRING (L"registration", L"user email", options.registration.user_email); |
||
271 | INIFile_WriteEntryAsLong (inifile, L"registration", L"activation code", options.registration.activation_code); |
||
272 | |||
1 | pmbaty | 273 | // [smilies] |
274 | for (smiley_index = 0; smiley_index < smiley_count; smiley_index++) |
||
275 | { |
||
276 | smiley = &smilies[smiley_index]; // quick access to smiley |
||
277 | |||
278 | // have a copy of the smiley's name and convert spaces back to quotes before saving them |
||
279 | wcscpy_s (smiley_name, WCHAR_SIZEOF (smiley_name), smiley->name); |
||
280 | smiley_name[0] = L'"'; // convert leading space to a quote |
||
281 | smiley_name[wcslen (smiley_name) - 1] = L'"'; // convert ending space to a quote |
||
282 | WRITE_WIDESTRING (L"smilies", smiley_name, smiley->filename); |
||
283 | |||
284 | // cycle through all the other smileys to see if another one uses the same data... |
||
285 | for (smiley_index2 = smiley_index; smiley_index2 < smiley_count; smiley_index2++) |
||
286 | if (smilies[smiley_index2].rtf_data == smiley->rtf_data) |
||
287 | break; // break as soon as we find one that uses the same data |
||
288 | if (smiley_index2 == smiley_count) |
||
289 | SAFE_free ((void **) &smiley->rtf_data); // only free data when we find none |
||
290 | } |
||
291 | SAFE_free ((void **) &smilies); |
||
292 | smiley_count = 0; |
||
293 | |||
294 | // now save the INI file |
||
11 | pmbaty | 295 | swprintf_s (filename, WCHAR_SIZEOF (filename), L"%s/config.ini", app_path); |
1 | pmbaty | 296 | INIFile_SaveINIFile (filename, inifile); |
297 | |||
298 | return; // finished |
||
299 | } |