Subversion Repositories Games.Chess Giants

Rev

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