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