Subversion Repositories Games.Chess Giants

Rev

Rev 161 | Rev 184 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // config.cpp
  2.  
  3. #include "common.h"
  4.  
  5.  
  6. // useful macros
  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)))
  8. #define WRITE_WIDESTRING(section,key,value) INIFile_WriteEntryAsString (inifile, (section), (key), (value))
  9.  
  10.  
  11. // global variables used in this module only
  12. static wchar_t filename[MAX_PATH];
  13.  
  14.  
  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 temp_string[MAX_PATH];
  24. //   wchar_t smiley_pathname[MAX_PATH];
  25.    wchar_t *smiley_name;
  26.    int language_index;
  27.    int program_index;
  28.    int smiley_index;
  29.    int file_length;
  30.    FILE *fp;
  31.  
  32.    // open the INI file
  33.    swprintf_s (filename, WCHAR_SIZEOF (filename), L"%s/config.ini", app_path);
  34.    inifile = INIFile_LoadINIFile (filename);
  35.  
  36.    // read the INI file (if it doesn't exist, default values will be fed)
  37.  
  38.    // [gameplay]
  39.    options.want_lastmove = (INIFile_ReadEntryAsBool (inifile, L"options", L"highlight last move", true) > 0);
  40.    options.want_possiblemoves = (INIFile_ReadEntryAsBool (inifile, L"options", L"highlight possible moves", true) > 0);
  41.    options.want_threats = (INIFile_ReadEntryAsBool (inifile, L"options", L"highlight king's threats", true) > 0);
  42.    options.want_animations = (INIFile_ReadEntryAsBool (inifile, L"options", L"display part animations", true) > 0);
  43.    options.want_slidinganimations = (INIFile_ReadEntryAsBool (inifile, L"options", L"display part sliding animations", true) > 0);
  44.    options.want_takenparts = (INIFile_ReadEntryAsBool (inifile, L"options", L"show taken parts", true) > 0);
  45.    options.want_turn = (INIFile_ReadEntryAsBool (inifile, L"options", L"show turn", true) > 0);
  46.    options.want_clock = (INIFile_ReadEntryAsBool (inifile, L"options", L"show game clock", true) > 0);
  47.    options.clock_color = INIFile_ReadEntryAsUnsignedLong (inifile, L"options", L"game clock color", 0x00007f00); // dark blue, in RGBA
  48.    options.want_history = (INIFile_ReadEntryAsBool (inifile, L"options", L"show game history", true) > 0);
  49.    options.history_color = INIFile_ReadEntryAsUnsignedLong (inifile, L"options", L"game history color", 0xe7e7e700); // light grey, in RGBA
  50.    options.want_sepiafilter = (INIFile_ReadEntryAsBool (inifile, L"options", L"use sepia filter for past moves", true) > 0);
  51.    options.want_autorotateon1vs1 = (INIFile_ReadEntryAsBool (inifile, L"options", L"auto-rotate board",true) > 0);
  52.    options.rotate_speed = INIFile_ReadEntryAsLong (inifile, L"options", L"rotation speed", 10);
  53.  
  54.    // [display]
  55.    READ_WIDESTRING (temp_string, inifile, L"display", L"language", L"auto"); // first, try to read the language from the config file
  56.    is_language_auto = (wcscmp (temp_string, L"auto") == 0); // remember is language is set to be automatically chosen
  57.    for (language_index = 0; language_index < language_count; language_index++)
  58.       if (_wcsicmp (languages[language_index].name, temp_string) == 0)
  59.       {
  60.          language_id = language_index; // identify the claimed language's ID among the list of known languages
  61.          break; // stop searching as soon as we find it
  62.       }
  63.    if (language_index == language_count)
  64.    {
  65.       GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_SENGLANGUAGE, temp_string, WCHAR_SIZEOF (temp_string)); // if unspecified, query the OS locale
  66.       for (language_index = 0; language_index < language_count; language_index++)
  67.          if (_wcsicmp (languages[language_index].name, temp_string) == 0)
  68.          {
  69.             language_id = language_index; // identify the claimed language's ID among the list of known languages
  70.             break; // stop searching as soon as we find it
  71.          }
  72.    }
  73.    if (language_index == language_count)
  74.    {
  75.       for (language_index = 0; language_index < language_count; language_index++)
  76.          if (_wcsicmp (languages[language_index].name, L"English") == 0)
  77.          {
  78.             language_id = language_index; // if still not found, fallback to the English language
  79.             break; // stop searching as soon as we find it
  80.          }
  81.    }
  82.    if (language_index == language_count)
  83.       language_id = 0; // if still not found, fallback to the first language known
  84.    options.want_fullscreen = (INIFile_ReadEntryAsBool (inifile, L"display", L"fullscreen", false) > 0);
  85.    options.want_maximized = (INIFile_ReadEntryAsBool (inifile, L"display", L"maximized", false) > 0);
  86.    options.window_width = INIFile_ReadEntryAsLong (inifile, L"display", L"window width", 1024);
  87.    options.window_height = INIFile_ReadEntryAsLong (inifile, L"display", L"window height", 768);
  88.    options.want_filtering = (INIFile_ReadEntryAsBool (inifile, L"display", L"enable texture filtering", true) > 0);
  89.    options.want_hiquality = (INIFile_ReadEntryAsBool (inifile, L"display", L"enable high quality filtering", true) > 0);
  90.    options.want_specularlighting = (INIFile_ReadEntryAsBool (inifile, L"display", L"enable specular lighting", true) > 0);
  91.    options.want_reflections = (INIFile_ReadEntryAsBool (inifile, L"display", L"enable board reflections", true) > 0);
  92.  
  93.    // [sound]
  94.    options.want_sounds = (INIFile_ReadEntryAsBool (inifile, L"sound", L"play sounds", true) > 0);
  95.  
  96.    // [theme]
  97.    READ_WIDESTRING (wantedtheme_name, inifile, L"theme", L"theme name", L"Default");
  98.    want_grid = (INIFile_ReadEntryAsBool (inifile, L"theme", L"show grid", true) > 0);
  99.    want_flaticons = (INIFile_ReadEntryAsBool (inifile, L"theme", L"flat icons instead", true) > 0);
  100.    want_custombackground = (INIFile_ReadEntryAsBool (inifile, L"theme", L"use custom background", false) > 0);
  101.    READ_WIDESTRING (custombackground_pathname, inifile, L"theme", L"custom background picture", L"");
  102.  
  103.    // [engine]
  104.    Config_LoadEngines (); // discover all engine parameters from the engines folder
  105.    for (program_index = 0; program_index < options.engine.program_count; program_index++)
  106.    {
  107.       READ_WIDESTRING (temp_string, inifile, L"engine", L"program", L"Stockfish");
  108.       if (_wcsicmp (temp_string, options.engine.programs[program_index].folder) == 0)
  109.          break; // identify the preferred engine, break as soon as we find it
  110.    }
  111.    options.engine.selected_program = (program_index < options.engine.program_count ? program_index : 0); // consistency check
  112.    options.engine.depth = INIFile_ReadEntryAsLong (inifile, L"engine", L"allowed depth", 10);
  113.    options.engine.max_depth = INIFile_ReadEntryAsLong (inifile, L"engine", L"maximum depth", 20);
  114.    options.engine.blunder_chances = INIFile_ReadEntryAsLong (inifile, L"engine", L"blunder chances", 5);
  115.    options.engine.obstinacy_level = INIFile_ReadEntryAsLong (inifile, L"engine", L"obstinacy", -1);
  116.    options.engine.is_expert_mode = (INIFile_ReadEntryAsBool (inifile, L"engine", L"expert mode", false) > 0);
  117.  
  118.    // [network]
  119.    READ_WIDESTRING (options.network.server_address, inifile, L"network", L"server address", L"freechess.org");
  120.    options.network.server_port = INIFile_ReadEntryAsLong (inifile, L"network", L"server port", 5000);
  121.    READ_WIDESTRING (options.network.login, inifile, L"network", L"login", L"guest");
  122.    READ_WIDESTRING (options.network.password, inifile, L"network", L"password", L"");
  123.    options.network.want_servermessages = (INIFile_ReadEntryAsBool (inifile, L"network", L"show server messages", true) > 0);
  124.    options.network.want_publicchat = (INIFile_ReadEntryAsBool (inifile, L"network", L"show public chat", true) > 0);
  125.    options.network.want_motdonconnect = (INIFile_ReadEntryAsBool (inifile, L"network", L"show MOTD on connect", true) > 0);
  126.  
  127.    // [registration]
  128.    READ_WIDESTRING (options.registration.user_email, inifile, L"registration", L"user email", L"");
  129.    options.registration.activation_code = (unsigned __int32) INIFile_ReadEntryAsLong (inifile, L"registration", L"activation code", 0);
  130.  
  131.    // [smilies]
  132.    if (inifile == NULL)
  133.    {
  134.       // if the .ini file doesn't exist yet, re-create one with the default smilies set
  135.       inifile = INIFile_NewINIFile ();
  136.       WRITE_WIDESTRING (L"smilies", L"\"o:)\"", L"angel.txt");
  137.       WRITE_WIDESTRING (L"smilies", L"\"0:)\"", L"angel.txt");
  138.       WRITE_WIDESTRING (L"smilies", L"\"o:-)\"", L"angel.txt");
  139.       WRITE_WIDESTRING (L"smilies", L"\"0:-)\"", L"angel.txt");
  140.       WRITE_WIDESTRING (L"smilies", L"\":-@\"", L"angry.txt");
  141.       WRITE_WIDESTRING (L"smilies", L"\">:(\"", L"angry.txt");
  142.       WRITE_WIDESTRING (L"smilies", L"\">:-(\"", L"angry.txt");
  143.       WRITE_WIDESTRING (L"smilies", L"\":,(\"", L"cry.txt");
  144.       WRITE_WIDESTRING (L"smilies", L"\":,-(\"", L"cry.txt");
  145.       WRITE_WIDESTRING (L"smilies", L"\":,,,(\"", L"cry.txt");
  146.       WRITE_WIDESTRING (L"smilies", L"\":'(\"", L"cry.txt");
  147.       WRITE_WIDESTRING (L"smilies", L"\":'-(\"", L"cry.txt");
  148.       WRITE_WIDESTRING (L"smilies", L"\":'''(\"", L"cry.txt");
  149.       WRITE_WIDESTRING (L"smilies", L"\"t_t\"", L"cry.txt");
  150.       WRITE_WIDESTRING (L"smilies", L"\"t.t\"", L"cry.txt");
  151.       WRITE_WIDESTRING (L"smilies", L"\"8d\"", L"dumb.txt");
  152.       WRITE_WIDESTRING (L"smilies", L"\"8-d\"", L"dumb.txt");
  153.       WRITE_WIDESTRING (L"smilies", L"\"oo\"", L"dumb.txt");
  154.       WRITE_WIDESTRING (L"smilies", L"\":d\"", L"grin.txt");
  155.       WRITE_WIDESTRING (L"smilies", L"\":-d\"", L"grin.txt");
  156.       WRITE_WIDESTRING (L"smilies", L"\":))\"", L"happy.txt");
  157.       WRITE_WIDESTRING (L"smilies", L"\"xd\"", L"laugh.txt");
  158.       WRITE_WIDESTRING (L"smilies", L"\"x-d\"", L"laugh.txt");
  159.       WRITE_WIDESTRING (L"smilies", L"\":x\"", L"mute.txt");
  160.       WRITE_WIDESTRING (L"smilies", L"\":-x\"", L"mute.txt");
  161.       WRITE_WIDESTRING (L"smilies", L"\"plz\"", L"please.txt");
  162.       WRITE_WIDESTRING (L"smilies", L"\":(\"", L"sad.txt");
  163.       WRITE_WIDESTRING (L"smilies", L"\":-(\"", L"sad.txt");
  164.       WRITE_WIDESTRING (L"smilies", L"\":)\"", L"smile.txt");
  165.       WRITE_WIDESTRING (L"smilies", L"\":-)\"", L"smile.txt");
  166.       WRITE_WIDESTRING (L"smilies", L"\"=)\"", L"smile.txt");
  167.       WRITE_WIDESTRING (L"smilies", L"\":s\"", L"sorry.txt");
  168.       WRITE_WIDESTRING (L"smilies", L"\":-s\"", L"sorry.txt");
  169.       WRITE_WIDESTRING (L"smilies", L"\"b)\"", L"star.txt");
  170.       WRITE_WIDESTRING (L"smilies", L"\"b-)\"", L"star.txt");
  171.       WRITE_WIDESTRING (L"smilies", L"\":p\"", L"tongue.txt");
  172.       WRITE_WIDESTRING (L"smilies", L"\":-p\"", L"tongue.txt");
  173.       WRITE_WIDESTRING (L"smilies", L"\";p\"", L"tongue.txt");
  174.       WRITE_WIDESTRING (L"smilies", L"\";-p\"", L"tongue.txt");
  175.       WRITE_WIDESTRING (L"smilies", L"\":/\"", L"unsure.txt");
  176.       WRITE_WIDESTRING (L"smilies", L"\":-/\"", L"unsure.txt");
  177.       WRITE_WIDESTRING (L"smilies", L"\":\\\"", L"unsure.txt");
  178.       WRITE_WIDESTRING (L"smilies", L"\":-\\\"", L"unsure.txt");
  179.       WRITE_WIDESTRING (L"smilies", L"\";)\"", L"wink.txt");
  180.       WRITE_WIDESTRING (L"smilies", L"\";-)\"", L"wink.txt");
  181.    }
  182.    smilies = NULL;
  183.    smiley_count = 0;
  184.    while ((smiley_name = INIFile_GetEntryName (inifile, L"smilies", smiley_count)) != NULL)
  185.    {
  186.       // for each smiley we can read, reallocate smilies array to hold one smiley more
  187.       smilies = (smiley_t *) SAFE_realloc (smilies, smiley_count, smiley_count + 1, sizeof (smiley_t), false);
  188.  
  189.       smiley = &smilies[smiley_count]; // quick access to smiley
  190.  
  191.       // read the smiley name and enclose it between a leading and trailing space
  192.       wcscpy_s (smiley->name, WCHAR_SIZEOF (smiley->name), smiley_name);
  193.       smiley->name[0] = L' '; // turn the first quote into a space
  194.       smiley->name[wcslen (smiley->name) - 1] = L' '; // turn the last quote into a space
  195.  
  196.       // now read the smiley's corresponding picture file
  197.       READ_WIDESTRING (smiley->filename, inifile, L"smilies", smiley_name, L"smile.txt");
  198.  
  199.       // is this filename the same as another smiley we know already ?
  200.       for (smiley_index = 0; smiley_index < smiley_count; smiley_index++)
  201.          if (wcscmp (smilies[smiley_index].filename, smiley->filename) == 0)
  202.             break; // break as soon as we find a match
  203.  
  204.       // did we find a match ?
  205.       if (smiley_index < smiley_count)
  206.       {
  207.          // if so, just copy data and data length from the smiley we already know
  208.          smiley->rtf_data = smilies[smiley_index].rtf_data;
  209.          smiley->rtf_len = smilies[smiley_index].rtf_len;
  210.       }
  211.  
  212.       // else we found no match, we have to read the file ourselves
  213.       else
  214.       {
  215.          // build the smiley picture's file pathname and read that smiley's data as a hex string
  216.          swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), L"%s/data/smilies/%s", app_path, smiley->filename);
  217.          _wfopen_s (&fp, temp_string, L"rb");
  218.          if (fp == NULL)
  219.             continue; // if this smiley picture file can't be opened, ignore this smiley
  220.  
  221.          // get the file length
  222.          fseek (fp, 0, SEEK_END);
  223.          file_length = ftell (fp);
  224.          fseek (fp, 0, SEEK_SET);
  225.  
  226.          // mallocate space for file contents and read it
  227.          file_buffer = (char *) SAFE_malloc (1 + file_length + 1 + 1, sizeof (char), false); // include null terminator
  228.          file_buffer[0] = ' '; // first character is a whitespace
  229.          fread (&file_buffer[1], file_length, sizeof (char), fp);
  230.          file_buffer[1 + file_length] = ' '; // last character is a whitespace
  231.          file_buffer[1 + file_length + 1] = 0; // terminate the char string
  232.          fclose (fp); // finished with the smiley picture file
  233.  
  234.          // convert it to wide char
  235.          smiley->rtf_len = 1 + file_length + 1;
  236.          smiley->rtf_data = (wchar_t *) SAFE_malloc (smiley->rtf_len + 1, sizeof (wchar_t), false); // include null terminator
  237.          ConvertToWideChar (smiley->rtf_data, smiley->rtf_len + 1, file_buffer);
  238.          SAFE_free ((void **) &file_buffer);
  239.       }
  240.  
  241.       smiley_count++; // we know now one smiley more
  242.    }
  243.  
  244.    // close the INI file
  245.    INIFile_FreeINIFile (inifile);
  246.    return; // finished loading config
  247. }
  248.  
  249.  
  250. void Config_LoadEngines (void)
  251. {
  252.    // this function loads the engine-specific parameters. The best place to call it is at the beginning
  253.    // of a game versus the computer, just before the engine executable process is started.
  254.  
  255.    wchar_t temp_string[MAX_PATH];
  256.    engineprogram_t *engineprogram;
  257.    void *engine_inifile;
  258.    WIN32_FIND_DATA wfd;
  259.    HANDLE hFind;
  260.    wchar_t *ptr;
  261.  
  262.    options.engine.programs = NULL;
  263.    options.engine.program_count = 0;
  264.  
  265.    swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), L"%s\\engines\\*.*", app_path); // build the search pattern string out of the path
  266.    hFind = FindFirstFile (temp_string, &wfd); // initiate search from that point
  267.    if (hFind == INVALID_HANDLE_VALUE)
  268.       return; // no engines available (FIXME: error message ?)
  269.  
  270.    // start examining search results...
  271.    do
  272.    {
  273.       if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || (wfd.cFileName[0] == L'.'))
  274.          continue; // skip everything that is NOT a directory, and every directories that begin with a dot
  275.  
  276.       // given the selected engine program, load and interpret the right engine config file
  277.       swprintf_s (filename, WCHAR_SIZEOF (filename), L"%s/engines/%s/engine.ini", app_path, wfd.cFileName);
  278.       engine_inifile = INIFile_LoadINIFile (filename); // see if an ini file exists for this engine
  279.       if (engine_inifile == NULL)
  280.          continue; // engine .ini file not found, skip this one
  281.  
  282.       // reallocate space for one more engine program, and clean up the allocated slot
  283.       options.engine.programs = (engineprogram_t *) SAFE_realloc (options.engine.programs, options.engine.program_count, options.engine.program_count + 1, sizeof (engineprogram_t), true);
  284.       engineprogram = &options.engine.programs[options.engine.program_count]; // quick access to engine
  285.  
  286.       // basic program information
  287.       wcscpy_s (engineprogram->folder, sizeof (engineprogram->folder), wfd.cFileName); // save engine program's folder name
  288.       READ_WIDESTRING (engineprogram->friendly_name, engine_inifile, L"program", L"name", L"Crafty v24.0");
  289.       READ_WIDESTRING (engineprogram->executable_name, engine_inifile, L"program", L"executable", L"crafty.exe");
  290.       READ_WIDESTRING (engineprogram->executable_arguments, engine_inifile, L"program", L"arguments", L"");
  291.  
  292.       // reply string patterns
  293.       READ_WIDESTRING (engineprogram->replystring_move, engine_inifile, L"reply strings", L"move", L"): ");
  294.  
  295.       // engine commands
  296.       READ_WIDESTRING (engineprogram->command_new, engine_inifile, L"commands", L"new game", L"new");
  297.       for (ptr = engineprogram->command_new; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n';
  298.       READ_WIDESTRING (engineprogram->command_setboard, engine_inifile, L"commands", L"setup table from FEN", L"setboard %s");
  299.       for (ptr = engineprogram->command_setboard; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n';
  300.       READ_WIDESTRING (engineprogram->command_go, engine_inifile, L"commands", L"play", L"go");
  301.       for (ptr = engineprogram->command_go; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n';
  302.       READ_WIDESTRING (engineprogram->command_move, engine_inifile, L"commands", L"move", L"%s");
  303.       for (ptr = engineprogram->command_move; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n';
  304.       READ_WIDESTRING (engineprogram->command_force, engine_inifile, L"commands", L"force move", L"force %s");
  305.       for (ptr = engineprogram->command_force; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n';
  306.       READ_WIDESTRING (engineprogram->command_quit, engine_inifile, L"commands", L"quit", L"quit");
  307.       for (ptr = engineprogram->command_quit; *ptr != 0; ptr++) if (*ptr == L';') *ptr = L'\n';
  308.  
  309.       INIFile_FreeINIFile (engine_inifile); // close the engine config file
  310.  
  311.       options.engine.program_count++; // we've identified one engine more
  312.    } while (FindNextFile (hFind, &wfd)); // ...and don't stop as long as there are directory entries to go
  313.  
  314.    FindClose (hFind); // close the search handle
  315.    return; // engine parameters loaded successfully
  316. }
  317.  
  318.  
  319. void Config_Save (void)
  320. {
  321.    // this function saves the software configuration into an INI file.
  322.    // WARNING: it does also free the smilies list !
  323.  
  324.    wchar_t smiley_name[32];
  325.    smiley_t *smiley;
  326.    void *inifile;
  327.    int smiley_index;
  328.    int smiley_index2;
  329.    HKEY hRegistryKey;
  330.  
  331.    // create a config file with the default values
  332.    inifile = INIFile_NewINIFile ();
  333.  
  334.    // [gameplay]
  335.    INIFile_WriteEntryAsBool (inifile, L"options", L"highlight last move", options.want_lastmove);
  336.    INIFile_WriteEntryAsBool (inifile, L"options", L"highlight possible moves", options.want_possiblemoves);
  337.    INIFile_WriteEntryAsBool (inifile, L"options", L"highlight king's threats", options.want_threats);
  338.    INIFile_WriteEntryAsBool (inifile, L"options", L"display part animations", options.want_animations);
  339.    INIFile_WriteEntryAsBool (inifile, L"options", L"display part sliding animations", options.want_slidinganimations);
  340.    INIFile_WriteEntryAsBool (inifile, L"options", L"show taken parts", options.want_takenparts);
  341.    INIFile_WriteEntryAsBool (inifile, L"options", L"show turn", options.want_turn);
  342.    INIFile_WriteEntryAsBool (inifile, L"options", L"show game clock", options.want_clock);
  343.    INIFile_WriteEntryAsUnsignedLong (inifile, L"options", L"game clock color", options.clock_color);
  344.    INIFile_WriteEntryAsBool (inifile, L"options", L"show game history", options.want_history);
  345.    INIFile_WriteEntryAsUnsignedLong (inifile, L"options", L"game history color", options.history_color);
  346.    INIFile_WriteEntryAsBool (inifile, L"options", L"use sepia filter for past moves", options.want_sepiafilter);
  347.    INIFile_WriteEntryAsBool (inifile, L"options", L"auto-rotate board", options.want_autorotateon1vs1);
  348.    INIFile_WriteEntryAsLong (inifile, L"options", L"rotation speed", options.rotate_speed);
  349.  
  350.    // [display]
  351.    WRITE_WIDESTRING (L"display", L"language", (is_language_auto ? L"auto" : languages[language_id].name));
  352.    INIFile_WriteEntryAsBool (inifile, L"display", L"fullscreen", options.want_fullscreen);
  353.    INIFile_WriteEntryAsBool (inifile, L"display", L"maximized", options.want_maximized);
  354.    INIFile_WriteEntryAsLong (inifile, L"display", L"window width", options.window_width);
  355.    INIFile_WriteEntryAsLong (inifile, L"display", L"window height", options.window_height);
  356.    INIFile_WriteEntryAsBool (inifile, L"display", L"enable texture filtering", options.want_filtering);
  357.    INIFile_WriteEntryAsBool (inifile, L"display", L"enable high quality filtering", options.want_hiquality);
  358.    INIFile_WriteEntryAsBool (inifile, L"display", L"enable specular lighting", options.want_specularlighting);
  359.    INIFile_WriteEntryAsBool (inifile, L"display", L"enable board reflections", options.want_reflections);
  360.  
  361.    // [sound]
  362.    INIFile_WriteEntryAsBool (inifile, L"sound", L"play sounds", options.want_sounds);
  363.  
  364.    // [theme]
  365.    WRITE_WIDESTRING (L"theme", L"theme name", (wantedtheme_name[0] != 0 ? wantedtheme_name : L"Marble"));
  366.    INIFile_WriteEntryAsBool (inifile, L"theme", L"show grid", want_grid);
  367.    INIFile_WriteEntryAsBool (inifile, L"theme", L"flat icons instead", want_flaticons);
  368.    INIFile_WriteEntryAsBool (inifile, L"theme", L"use custom background", want_custombackground);
  369.    WRITE_WIDESTRING (L"theme", L"custom background picture", custombackground_pathname);
  370.  
  371.    // [engine]
  372.    WRITE_WIDESTRING (L"engine", L"program", options.engine.programs[options.engine.selected_program].folder);
  373.    INIFile_WriteEntryAsLong (inifile, L"engine", L"allowed depth", options.engine.depth);
  374.    INIFile_WriteEntryAsLong (inifile, L"engine", L"maximum depth", options.engine.max_depth);
  375.    INIFile_WriteEntryAsLong (inifile, L"engine", L"blunder chances", options.engine.blunder_chances);
  376.    INIFile_WriteEntryAsLong (inifile, L"engine", L"obstinacy", options.engine.obstinacy_level);
  377.    INIFile_WriteEntryAsBool (inifile, L"engine", L"expert mode", options.engine.is_expert_mode);
  378.  
  379.    // [network]
  380.    WRITE_WIDESTRING (L"network", L"server address", options.network.server_address);
  381.    INIFile_WriteEntryAsLong (inifile, L"network", L"server port", options.network.server_port);
  382.    WRITE_WIDESTRING (L"network", L"login", options.network.login);
  383.    WRITE_WIDESTRING (L"network", L"password", options.network.password);
  384.    INIFile_WriteEntryAsBool (inifile, L"network", L"show server messages", options.network.want_servermessages);
  385.    INIFile_WriteEntryAsBool (inifile, L"network", L"show public chat", options.network.want_publicchat);
  386.    INIFile_WriteEntryAsBool (inifile, L"network", L"show MOTD on connect", options.network.want_motdonconnect);
  387.  
  388.    // [registration]
  389.    WRITE_WIDESTRING (L"registration", L"user email", options.registration.user_email);
  390.    INIFile_WriteEntryAsLong (inifile, L"registration", L"activation code", (long) options.registration.activation_code);
  391.  
  392.    // [smilies]
  393.    for (smiley_index = 0; smiley_index < smiley_count; smiley_index++)
  394.    {
  395.       smiley = &smilies[smiley_index]; // quick access to smiley
  396.  
  397.       // have a copy of the smiley's name and convert spaces back to quotes before saving them
  398.       wcscpy_s (smiley_name, WCHAR_SIZEOF (smiley_name), smiley->name);
  399.       smiley_name[0] = L'"'; // convert leading space to a quote
  400.       smiley_name[wcslen (smiley_name) - 1] = L'"'; // convert ending space to a quote
  401.       WRITE_WIDESTRING (L"smilies", smiley_name, smiley->filename);
  402.  
  403.       // cycle through all the other smileys to see if another one uses the same data...
  404.       for (smiley_index2 = smiley_index; smiley_index2 < smiley_count; smiley_index2++)
  405.          if (smilies[smiley_index2].rtf_data == smiley->rtf_data)
  406.             break; // break as soon as we find one that uses the same data
  407.       if (smiley_index2 == smiley_count)
  408.          SAFE_free ((void **) &smiley->rtf_data); // only free data when we find none
  409.    }
  410.    SAFE_free ((void **) &smilies);
  411.    smiley_count = 0;
  412.  
  413.    // now save the INI file
  414.    swprintf_s (filename, WCHAR_SIZEOF (filename), L"%s/config.ini", app_path);
  415.    INIFile_SaveINIFile (filename, inifile);
  416.  
  417.    // SAFETY: if the current registration data is good, back it up in the registry if we can
  418.    if (IsRegistrationCorrect (options.registration.user_email, options.registration.activation_code)
  419.       && (RegOpenKeyEx (HKEY_CURRENT_USER, L"SOFTWARE\\Chess Giants", 0, KEY_SET_VALUE, &hRegistryKey) == 0))
  420.    {
  421.       RegSetValueEx (hRegistryKey, L"UserEmail", 0, REG_SZ, (BYTE *) options.registration.user_email, wcslen (options.registration.user_email) * sizeof (wchar_t));
  422.       RegSetValueEx (hRegistryKey, L"ActivationCode", 0, REG_DWORD, (BYTE *) &options.registration.activation_code, sizeof (options.registration.activation_code));
  423.       RegCloseKey (hRegistryKey); // once we've written the data we were interested in, close the registry key
  424.    }
  425.  
  426.    return; // finished
  427. }
  428.