bool DoomGame::loadConfig()

in doom_py/src/lib/ViZDoomGame.cpp [694:1023]


    bool DoomGame::loadConfig(std::string filename) {
        bool success = true;
        std::ifstream file(filename.c_str());
        
        if(!file.good() )
        {
            throw FileDoesNotExistException(filename);
            //std::cerr<<"WARNING! Loading config from: \""<<filename<<"\" failed. Something's wrong with the file. Check your spelling and permissions.\n";
            return false;
        }
        std::string line;
        int line_number = 0;

    /* Process every line. */
        while(!file.eof())
        {
            ++line_number;
            using namespace boost::algorithm;

            std::getline(file, line);

        /* Ignore empty and comment lines */
            trim_all(line);

            if(line.empty() || line[0] == '#'){
                continue;
            }


        bool append = false; //it looks for +=

        /* Check if '=' is there */
            size_t equals_sign_pos = line.find_first_of('=');
            size_t append_sign_pos = line.find("+=");

            std::string key;
            std::string val;
            std::string raw_val;
            if( append_sign_pos != std::string::npos){
                key = line.substr(0, append_sign_pos);
                val = line.substr(append_sign_pos + 2);
                append = true;
            }
            else if( equals_sign_pos != std::string::npos ){
                key = line.substr(0, equals_sign_pos);
                val = line.substr(equals_sign_pos + 1);
            }
            else
            {
                std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Syntax erorr in line #"<<line_number<<". Line ignored.\n";
                success = false;
                continue;
            }

            
            raw_val = val;
            trim_all(key);
            trim_all(val);
            std::string original_val = val;
            to_lower(val);
            to_lower(key);
            if(key.empty())
            {
                std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Empty key in line #"<<line_number<<". Line ignored.\n";
                success = false;
                continue;
            }


        /* Parse enum list properties */

            if(key == "available_buttons" || key == "availablebuttons"){
                std::vector<std::string> str_buttons;
                int start_line = line_number;
                bool parse_success = DoomGame::ParseListProperty(line_number, val, file, str_buttons );
                if(parse_success){
                    unsigned int i = 0;
                    try{
                        std::vector<Button> buttons;
                        for( i = 0; i < str_buttons.size(); ++i ){
                            buttons.push_back(DoomGame::StringToButton(str_buttons[i]));

                        }
                        if (!append)
                            this->clearAvailableButtons();
                        for( i = 0; i < buttons.size(); ++i ){
                            this->addAvailableButton(buttons[i]);
                        }
                    }
                    catch(std::exception){
                        std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Unsupported value in lines "<<start_line<<"-"<<line_number<<": "<<str_buttons[i]<<". Lines ignored.\n";
                        success = false;
                    }
                }
                else{
                    std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Syntax error in lines "<<start_line<<"-"<<line_number<<". Lines ignored.\n";
                    success = false;
                }
  
                continue;
            }

            if(key == "available_game_variables" || key == "availablegamevariables"){
                std::vector<std::string> str_variables;
                int start_line = line_number;
                bool parse_success = DoomGame::ParseListProperty(line_number, val, file, str_variables );
                if(parse_success){
                    unsigned int i = 0;
                    try{
                        std::vector<GameVariable> variables;
                        for( i = 0; i < str_variables.size(); ++i ){
                            variables.push_back(DoomGame::StringToGameVariable(str_variables[i]));

                        }
                        if(!append)
                            this->clearAvailableGameVariables();
                        for( i = 0; i < variables.size(); ++i ){
                            this->addAvailableGameVariable(variables[i]);
                        }
                    }
                    catch(std::exception){
                        std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Unsupported value in lines "<<start_line<<"-"<<line_number<<": "<<str_variables[i]<<". Lines ignored.\n";
                        success = false;
                    }
                }
                else{
                    std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Syntax error in lines "<<start_line<<"-"<<line_number<<". Lines ignored.\n";
                    success = false;
                }

                continue;
            }           

        /* Parse game args which ae string but enables "+=" */
            if(key == "game_args" || key == "game_args"){
            	if(!append){
            		this->clearGameArgs();
            	}
                this->addGameArgs(original_val);
                continue;
            }
        /* Check if "+=" was not used for non-list property */
            if(append){
                std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". \"+=\" is not supported for non-list properties. Line #"<<line_number<<" ignored.\n";
                success = false;
                continue;
            }

           	
        /* Check if value is not empty */
            if(val.empty())
            {
                std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Empty value in line #"<<line_number<<". Line ignored.\n";
                success = false;
                continue;
            }
        
        /* Parse int properties */
            try{
                if (key =="seed" || key == "seed"){
                    this->setSeed(StringToUint(val));
                    continue;
                }
                if (key == "episode_timeout" || key == "episodetimeout"){
                    this->setEpisodeTimeout(StringToUint(val));
                    continue;
                }
                if (key == "episode_start_time" || key == "episodestarttime"){
                    this->setEpisodeStartTime(StringToUint(val));
                    continue;
                }
                if (key == "doom_skill" || key == "doomskill"){
                    this->setDoomSkill(StringToUint(val));
                    continue;
                }
            }
            catch(boost::bad_lexical_cast &){
                std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Unsigned int value expected insted of: "<<raw_val<<" in line #"<<line_number<<". Line ignored.\n";
                success = false;
                continue;
            }

        /* Parse float properties */
            try{
                if (key =="living_reward" || key =="livingreward"){
                    this->setLivingReward(boost::lexical_cast<double>(val));
                    continue;
                }
                if (key == "deathpenalty" || key == "death_penalty"){
                    this->setDeathPenalty(boost::lexical_cast<double>(val));
                    continue;
                }
            }
            catch(boost::bad_lexical_cast &){
                std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Float value expected insted of: "<<raw_val<<" in line #"<<line_number<<". Line ignored.\n";
                success = false;
                continue;
            }
                        
        /* Parse string properties */
            if(key == "doom_map" || key == "doommap"){
                this->setDoomMap(val);
                continue;
            }
            if(key == "vizdoom_path" || key == "vizdoompath"){
                this->setViZDoomPath(original_val);
                continue;
            }
            if(key == "doom_game_path" || key == "doomgamepath"){
                this->setDoomGamePath(original_val);
                continue;
            }
            if(key == "doom_scenario_path" || key == "doomscenariopath"){
                this->setDoomScenarioPath(original_val);
                continue;
            }
            if(key == "doom_config_path" || key == "doomconfigpath"){
                this->setDoomConfigPath(original_val);
                continue;
            }

    
        /* Parse bool properties */
            try{
                if (key =="console_enabled" || key =="consoleenabled"){
                    this->setConsoleEnabled(StringToBool(val));
                    continue;
                }
                if (key =="sound_enabled" || key =="soundenabled"){
                    this->setSoundEnabled(StringToBool(val));
                    continue;
                }
                if (key =="render_hud" || key =="renderhud"){
                    this->setRenderHud(StringToBool(val));
                    continue;
                }
                if (key =="render_weapon" || key =="renderweapon"){
                    this->setRenderWeapon(StringToBool(val));
                    continue;
                }
                if (key =="render_crosshair" || key =="rendercrosshair"){
                    this->setRenderCrosshair(StringToBool(val));
                    continue;
                }
                if (key =="render_particles" || key =="renderparticles"){
                    this->setRenderParticles(StringToBool(val));
                    continue;
                }
                if (key =="render_decals" || key =="renderdecals"){
                    this->setRenderDecals(StringToBool(val));
                    continue;
                }
                if (key =="window_visible" || key =="windowvisible"){
                    this->setWindowVisible(StringToBool(val));
                    continue;
                }
               
            }
            catch( std::exception ){
                std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Boolean value expected insted of: "<<raw_val<<" in line #"<<line_number<<". Line ignored.\n";
                success = false;
                continue;
                            
            }

        /* Parse enum properties */

            if(key =="mode")
            {
                if(val == "spectator"){
                    this->setMode(SPECTATOR);
                    continue;
                }
                if(val == "player"){
                    this->setMode(PLAYER);
                    continue;
                }
                if(val == "spectator"){
                    this->setMode(SPECTATOR);
                    continue;
                }
                if(val == "async_player"){
                    this->setMode(ASYNC_PLAYER);
                    continue;
                }
                if(val == "async_spectator"){
                    this->setMode(ASYNC_SPECTATOR);
                    continue;
                }
                std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". (ASYNC_)SPECTATOR || PLAYER expected instead of: "<<raw_val<<" in line #"<<line_number<<". Line ignored.\n";
                success = false;
                continue;  
            }

            try{
                if(key == "screen_resolution" || key == "screenresolution"){
                    this->setScreenResolution(StringToResolution(val));
                    continue;
                }
                if(key == "screen_format" || key == "screenformat"){
                    this->setScreenFormat(StringToFormat(val));
                    continue;
                }
                if(key == "button_max_value" || key == "buttonmaxvalue"){
                    size_t space = val.find_first_of(" ");
                    if(space == std::string::npos)
                        throw std::exception();
                    Button button = DoomGame::StringToButton(val.substr(0,space));
                    val = val.substr(space+1);
                    unsigned int max_value = boost::lexical_cast<unsigned int>(val);
                    if(val[0] == '-')
                        throw boost::bad_lexical_cast();
                    this->setButtonMaxValue(button,max_value);
                    continue;
                }
            }
            catch(std::exception&)
            {
                std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Unsupported value: "<<raw_val<<" in line #"<<line_number<<". Line ignored.\n";
                success = false;
                continue;
            }

            std::cerr<<"WARNING! Loading config from: \""<<filename<<"\". Unsupported key: "<<key<<" in line #"<<line_number<<". Line ignored.\n";
            success = false;
            
        }

        return success;
    }