Subversion Repositories Games.Chess Giants

Rev

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