colobot/colobot-base/common/config_file.cpp

203 lines
5.1 KiB
C++
Raw Permalink Normal View History

/*
* 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
*
* 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
*/
2015-07-19 13:30:30 +00:00
#include "common/config_file.h"
2014-08-12 19:24:33 +00:00
#include "common/logger.h"
#include "common/resources/inputstream.h"
#include "common/resources/outputstream.h"
#include "common/system/system.h"
2014-10-07 20:05:16 +00:00
#include <memory>
#include <utility>
#include <cstring>
#include <fstream>
2015-07-19 13:30:30 +00:00
CConfigFile::CConfigFile()
: m_needsSave(false)
, m_useCurrentDirectory(false)
2015-07-19 13:30:30 +00:00
, m_loaded(false)
{
}
2015-07-19 13:30:30 +00:00
CConfigFile::~CConfigFile()
{
if (m_needsSave)
{
GetLogger()->Warn("Config file was not properly saved! Saving now...\n");
Save();
}
}
2015-07-19 13:30:30 +00:00
void CConfigFile::SetUseCurrentDirectory(bool useCurrentDirectory)
{
2014-08-12 19:24:33 +00:00
m_useCurrentDirectory = useCurrentDirectory;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::Init()
{
try
{
2014-10-07 20:05:16 +00:00
std::unique_ptr<std::istream> stream;
bool good;
2014-10-07 20:05:16 +00:00
if (m_useCurrentDirectory)
{
auto inputStream = std::make_unique<std::ifstream>("./colobot.json");
good = inputStream->good();
stream = std::move(inputStream);
2014-10-07 20:05:16 +00:00
}
else
{
auto inputStream = std::make_unique<CInputStream>("colobot.json");
good = inputStream->is_open();
stream = std::move(inputStream);
2014-10-07 20:05:16 +00:00
}
if (good)
2014-10-07 20:05:16 +00:00
{
m_properties = nlohmann::json::parse(*stream);
2015-07-19 13:30:30 +00:00
m_loaded = true;
2014-10-07 20:05:16 +00:00
}
else
{
GetLogger()->Error("Error on parsing config file: failed to open file\n");
2014-07-24 21:38:13 +00:00
return false;
}
}
catch (std::exception & e)
{
GetLogger()->Error("Error on parsing config file: %s\n", e.what());
return false;
}
return true;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::Save()
{
if (m_needsSave)
{
try
2015-07-19 13:30:30 +00:00
{
std::unique_ptr<std::ostream> stream;
bool good;
if (m_useCurrentDirectory)
{
auto outputStream = std::make_unique<std::ofstream>("./colobot.json");
good = outputStream->good();
stream = std::move(outputStream);
}
else
{
auto outputStream = std::make_unique<COutputStream>("colobot.json");
good = outputStream->is_open();
stream = std::move(outputStream);
}
if (good)
{
*stream << m_properties.dump(4);
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
}
catch (std::exception & e)
2015-07-19 13:30:30 +00:00
{
GetLogger()->Error("Error on storing config file: %s\n", e.what());
return false;
}
}
return true;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::SetStringProperty(std::string section, std::string key, std::string value)
{
m_properties[section][key] = value;
m_needsSave = true;
return true;
}
2015-08-15 16:26:27 +00:00
bool CConfigFile::GetStringProperty(std::string section, std::string key, std::string &value)
{
auto element = m_properties[section][key];
if (!element.is_string()) return false;
value = element.get<std::string>();
return true;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::SetIntProperty(std::string section, std::string key, int value)
{
m_properties[section][key] = value;
m_needsSave = true;
return true;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::GetIntProperty(std::string section, std::string key, int &value)
{
auto element = m_properties[section][key];
if (!element.is_number()) return false;
value = element.get<int>();
return true;
}
2015-08-15 16:26:27 +00:00
bool CConfigFile::SetBoolProperty(std::string section, std::string key, bool value)
{
m_properties[section][key] = value;
m_needsSave = true;
return true;
2015-08-15 16:26:27 +00:00
}
bool CConfigFile::GetBoolProperty(std::string section, std::string key, bool& value)
{
auto element = m_properties[section][key];
if (!element.is_boolean()) return false;
value = element.get<bool>();
return true;
2015-08-15 16:26:27 +00:00
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::SetFloatProperty(std::string section, std::string key, float value)
{
m_properties[section][key] = value;
m_needsSave = true;
return true;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::GetFloatProperty(std::string section, std::string key, float &value)
{
auto element = m_properties[section][key];
if (!element.is_number()) return false;
value = element.get<float>();
2015-07-19 13:30:30 +00:00
return true;
}