2014-10-14 13:11:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
2023-08-06 21:15:48 +00:00
|
|
|
* Copyright (C) 2001-2023, Daniel Roux, EPSITEC SA & TerranovaTeam
|
2015-08-22 14:40:02 +00:00
|
|
|
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
|
2014-10-14 13:11:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see http://gnu.org/licenses
|
|
|
|
*/
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
2015-07-19 13:30:30 +00:00
|
|
|
#include "common/config_file.h"
|
2012-10-17 19:55:45 +00:00
|
|
|
|
2014-08-12 19:24:33 +00:00
|
|
|
#include "common/logger.h"
|
|
|
|
|
2015-08-02 11:09:48 +00:00
|
|
|
#include "common/resources/inputstream.h"
|
|
|
|
#include "common/resources/outputstream.h"
|
|
|
|
|
2016-07-24 12:38:49 +00:00
|
|
|
#include "common/system/system.h"
|
|
|
|
|
2014-10-07 20:05:16 +00:00
|
|
|
#include <memory>
|
2012-09-09 15:51:10 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <cstring>
|
2012-09-26 20:57:43 +00:00
|
|
|
#include <boost/property_tree/ini_parser.hpp>
|
2012-09-26 22:30:47 +00:00
|
|
|
#include <boost/regex.hpp>
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-26 20:57:43 +00:00
|
|
|
namespace bp = boost::property_tree;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2015-07-19 13:30:30 +00:00
|
|
|
CConfigFile::CConfigFile()
|
2015-07-19 19:35:55 +00:00
|
|
|
: m_needsSave(false)
|
|
|
|
, m_useCurrentDirectory(false)
|
2015-07-19 13:30:30 +00:00
|
|
|
, m_loaded(false)
|
2012-08-09 21:04:29 +00:00
|
|
|
{
|
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
2015-07-19 13:30:30 +00:00
|
|
|
CConfigFile::~CConfigFile()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2015-07-19 19:35:55 +00:00
|
|
|
if (m_needsSave)
|
|
|
|
{
|
|
|
|
GetLogger()->Warn("Config file was not properly saved! Saving now...\n");
|
|
|
|
Save();
|
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 13:30:30 +00:00
|
|
|
void CConfigFile::SetUseCurrentDirectory(bool useCurrentDirectory)
|
2014-08-12 18:03:56 +00:00
|
|
|
{
|
2014-08-12 19:24:33 +00:00
|
|
|
m_useCurrentDirectory = useCurrentDirectory;
|
2014-08-12 18:03:56 +00:00
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2015-07-19 13:30:30 +00:00
|
|
|
bool CConfigFile::Init()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
2014-10-07 20:05:16 +00:00
|
|
|
std::unique_ptr<std::istream> stream;
|
2014-10-15 19:31:43 +00:00
|
|
|
bool good;
|
2014-10-07 20:05:16 +00:00
|
|
|
if (m_useCurrentDirectory)
|
|
|
|
{
|
2022-02-26 17:48:51 +00:00
|
|
|
auto inputStream = std::make_unique<std::ifstream>("./colobot.ini");
|
2014-10-15 19:28:40 +00:00
|
|
|
good = inputStream->good();
|
2015-08-10 18:43:00 +00:00
|
|
|
stream = std::move(inputStream);
|
2014-10-07 20:05:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-26 17:48:51 +00:00
|
|
|
auto inputStream = std::make_unique<CInputStream>("colobot.ini");
|
2014-10-15 19:28:40 +00:00
|
|
|
good = inputStream->is_open();
|
2015-08-10 18:43:00 +00:00
|
|
|
stream = std::move(inputStream);
|
2014-10-07 20:05:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-15 19:28:40 +00:00
|
|
|
if (good)
|
2014-10-07 20:05:16 +00:00
|
|
|
{
|
|
|
|
bp::ini_parser::read_ini(*stream, m_propertyTree);
|
2015-07-19 13:30:30 +00:00
|
|
|
m_loaded = true;
|
2014-10-07 20:05:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-19 19:35:55 +00:00
|
|
|
GetLogger()->Error("Error on parsing config file: failed to open file\n");
|
2014-07-24 21:38:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
2015-07-19 19:35:55 +00:00
|
|
|
GetLogger()->Error("Error on parsing config file: %s\n", e.what());
|
2012-09-26 22:30:47 +00:00
|
|
|
return false;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
return true;
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 13:30:30 +00:00
|
|
|
bool CConfigFile::Save()
|
2013-05-04 09:56:03 +00:00
|
|
|
{
|
2015-07-19 19:35:55 +00:00
|
|
|
if (m_needsSave)
|
2013-05-04 09:56:03 +00:00
|
|
|
{
|
2015-07-19 19:35:55 +00:00
|
|
|
try
|
2015-07-19 13:30:30 +00:00
|
|
|
{
|
2015-07-19 19:35:55 +00:00
|
|
|
std::unique_ptr<std::ostream> stream;
|
|
|
|
bool good;
|
|
|
|
if (m_useCurrentDirectory)
|
|
|
|
{
|
2022-02-26 17:48:51 +00:00
|
|
|
auto outputStream = std::make_unique<std::ofstream>("./colobot.ini");
|
2015-07-19 19:35:55 +00:00
|
|
|
good = outputStream->good();
|
2015-08-10 18:43:00 +00:00
|
|
|
stream = std::move(outputStream);
|
2015-07-19 19:35:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-26 17:48:51 +00:00
|
|
|
auto outputStream = std::make_unique<COutputStream>("colobot.ini");
|
2015-07-19 19:35:55 +00:00
|
|
|
good = outputStream->is_open();
|
2015-08-10 18:43:00 +00:00
|
|
|
stream = std::move(outputStream);
|
2015-07-19 19:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (good)
|
|
|
|
{
|
|
|
|
bp::ini_parser::write_ini(*stream, m_propertyTree);
|
|
|
|
m_needsSave = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GetLogger()->Error("Error on storing config file: failed to open file\n");
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-19 13:30:30 +00:00
|
|
|
}
|
2015-07-19 19:35:55 +00:00
|
|
|
catch (std::exception & e)
|
2015-07-19 13:30:30 +00:00
|
|
|
{
|
2015-07-19 19:35:55 +00:00
|
|
|
GetLogger()->Error("Error on storing config file: %s\n", e.what());
|
2013-05-04 09:56:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-09-26 22:30:47 +00:00
|
|
|
|
2015-07-19 13:30:30 +00:00
|
|
|
bool CConfigFile::SetStringProperty(std::string section, std::string key, std::string value)
|
2012-08-09 21:04:29 +00:00
|
|
|
{
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_propertyTree.put(section + "." + key, value);
|
2015-07-19 19:35:55 +00:00
|
|
|
m_needsSave = true;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
2015-07-19 19:35:55 +00:00
|
|
|
GetLogger()->Error("Error on editing config file: %s\n", e.what());
|
2012-09-26 22:30:47 +00:00
|
|
|
return false;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
return true;
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2015-08-15 16:26:27 +00:00
|
|
|
bool CConfigFile::GetStringProperty(std::string section, std::string key, std::string &value)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
2015-08-15 16:26:27 +00:00
|
|
|
std::string readValue = m_propertyTree.get<std::string>(section + "." + key);
|
|
|
|
value = std::move(readValue);
|
2012-08-09 21:04:29 +00:00
|
|
|
}
|
2012-09-26 20:57:43 +00:00
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
2015-08-15 17:45:49 +00:00
|
|
|
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s\n", e.what());
|
2012-09-26 22:30:47 +00:00
|
|
|
return false;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
return true;
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 13:30:30 +00:00
|
|
|
bool CConfigFile::SetIntProperty(std::string section, std::string key, int value)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_propertyTree.put(section + "." + key, value);
|
2015-07-19 19:35:55 +00:00
|
|
|
m_needsSave = true;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
2015-07-19 19:35:55 +00:00
|
|
|
GetLogger()->Error("Error on editing config file: %s\n", e.what());
|
2012-09-26 22:30:47 +00:00
|
|
|
return false;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
return true;
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 13:30:30 +00:00
|
|
|
bool CConfigFile::GetIntProperty(std::string section, std::string key, int &value)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
2015-08-15 16:26:27 +00:00
|
|
|
int readValue = m_propertyTree.get<int>(section + "." + key);
|
|
|
|
value = readValue;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
2015-08-15 17:45:49 +00:00
|
|
|
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s\n", e.what());
|
2012-09-26 22:30:47 +00:00
|
|
|
return false;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-15 16:26:27 +00:00
|
|
|
bool CConfigFile::SetBoolProperty(std::string section, std::string key, bool value)
|
|
|
|
{
|
|
|
|
return SetIntProperty(section, key, value ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CConfigFile::GetBoolProperty(std::string section, std::string key, bool& value)
|
|
|
|
{
|
|
|
|
int intValue = 0;
|
|
|
|
bool result = GetIntProperty(section, key, intValue);
|
|
|
|
if (result)
|
|
|
|
{
|
2015-08-15 17:45:49 +00:00
|
|
|
if (intValue == 0)
|
|
|
|
{
|
|
|
|
value = false;
|
|
|
|
}
|
|
|
|
else if (intValue == 1)
|
|
|
|
{
|
|
|
|
value = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-15 17:54:06 +00:00
|
|
|
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing bool property %s.%s (expected 0 or 1, not %d)\n",
|
|
|
|
section.c_str(), key.c_str(), intValue);
|
2015-08-15 17:45:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-15 16:26:27 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2012-08-09 21:04:29 +00:00
|
|
|
|
2015-07-19 13:30:30 +00:00
|
|
|
bool CConfigFile::SetFloatProperty(std::string section, std::string key, float value)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_propertyTree.put(section + "." + key, value);
|
2015-07-19 19:35:55 +00:00
|
|
|
m_needsSave = true;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
2015-07-19 19:35:55 +00:00
|
|
|
GetLogger()->Error("Error on editing config file: %s\n", e.what());
|
2012-09-26 22:30:47 +00:00
|
|
|
return false;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
return true;
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 13:30:30 +00:00
|
|
|
bool CConfigFile::GetFloatProperty(std::string section, std::string key, float &value)
|
2012-08-09 21:04:29 +00:00
|
|
|
{
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
2015-08-15 16:26:27 +00:00
|
|
|
float readValue = m_propertyTree.get<float>(section + "." + key);
|
|
|
|
value = readValue;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
2015-08-15 17:45:49 +00:00
|
|
|
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s\n", e.what());
|
2015-07-19 13:30:30 +00:00
|
|
|
return false;
|
2012-08-12 13:00:37 +00:00
|
|
|
}
|
2015-07-19 13:30:30 +00:00
|
|
|
return true;
|
2012-08-12 13:00:37 +00:00
|
|
|
}
|