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