Subversion Repositories Games.Chess Giants

Rev

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