2012-06-26 20:23:05 +00:00
|
|
|
// * This file is part of the COLOBOT source code
|
|
|
|
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
|
|
|
|
// *
|
|
|
|
// * 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://www.gnu.org/licenses/.
|
|
|
|
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
#include "common/profile.h"
|
2012-10-17 19:55:45 +00:00
|
|
|
|
2012-09-26 20:57:43 +00:00
|
|
|
#include "common/logger.h"
|
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
|
|
|
|
|
|
|
|
2013-02-16 21:37:43 +00:00
|
|
|
template<> CProfile* CSingleton<CProfile>::m_instance = nullptr;
|
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
|
|
|
|
2012-09-26 22:30:47 +00:00
|
|
|
CProfile::CProfile() :
|
|
|
|
m_profileNeedSave(false)
|
2012-08-09 21:04:29 +00:00
|
|
|
{
|
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
2012-08-09 21:04:29 +00:00
|
|
|
CProfile::~CProfile()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-26 22:30:47 +00:00
|
|
|
if (m_profileNeedSave)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bp::ini_parser::write_ini("colobot.ini", m_propertyTree);
|
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
|
|
|
GetLogger()->Info("Error on storing profile: %s\n", e.what());
|
|
|
|
}
|
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-09 21:04:29 +00:00
|
|
|
bool CProfile::InitCurrentDirectory()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
bp::ini_parser::read_ini("colobot.ini", m_propertyTree);
|
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
|
|
|
GetLogger()->Info("Error on parsing profile: %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
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-26 22:30:47 +00:00
|
|
|
|
2012-08-09 21:04:29 +00:00
|
|
|
bool CProfile::SetLocalProfileString(std::string section, std::string key, std::string value)
|
|
|
|
{
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_propertyTree.put(section + "." + key, value);
|
2012-09-26 22:30:47 +00:00
|
|
|
m_profileNeedSave = true;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
|
|
|
GetLogger()->Info("Error on parsing profile: %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
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-09 21:04:29 +00:00
|
|
|
bool CProfile::GetLocalProfileString(std::string section, std::string key, std::string &buffer)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
buffer = m_propertyTree.get<std::string>(section + "." + key);
|
2012-08-09 21:04:29 +00:00
|
|
|
}
|
2012-09-26 20:57:43 +00:00
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
|
|
|
GetLogger()->Info("Error on parsing profile: %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
|
|
|
}
|
|
|
|
|
2012-08-09 21:04:29 +00:00
|
|
|
|
|
|
|
bool CProfile::SetLocalProfileInt(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);
|
2012-09-28 19:03:28 +00:00
|
|
|
m_profileNeedSave = true;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
|
|
|
GetLogger()->Info("Error on parsing profile: %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
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-09 21:04:29 +00:00
|
|
|
bool CProfile::GetLocalProfileInt(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
|
|
|
|
{
|
|
|
|
value = m_propertyTree.get<int>(section + "." + key);
|
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
|
|
|
GetLogger()->Info("Error on parsing profile: %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;
|
|
|
|
}
|
|
|
|
|
2012-08-09 21:04:29 +00:00
|
|
|
|
|
|
|
bool CProfile::SetLocalProfileFloat(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);
|
2012-09-28 19:03:28 +00:00
|
|
|
m_profileNeedSave = true;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
|
|
|
GetLogger()->Info("Error on parsing profile: %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
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-09 21:04:29 +00:00
|
|
|
bool CProfile::GetLocalProfileFloat(std::string section, std::string key, float &value)
|
|
|
|
{
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
value = m_propertyTree.get<float>(section + "." + key);
|
|
|
|
}
|
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
|
|
|
GetLogger()->Info("Error on parsing profile: %s\n", e.what());
|
2012-09-26 22:30:47 +00:00
|
|
|
return false;
|
2012-09-26 20:57:43 +00:00
|
|
|
}
|
2012-08-09 21:04:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-08-12 13:00:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
std::vector< std::string > CProfile::GetLocalProfileSection(std::string section, std::string key)
|
|
|
|
{
|
|
|
|
std::vector< std::string > ret_list;
|
2012-09-26 22:30:47 +00:00
|
|
|
boost::regex re(key + "[0-9]*"); //we want to match all key followed my any number
|
2012-08-12 13:00:37 +00:00
|
|
|
|
2012-09-26 20:57:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
for(bp::ptree::value_type const & v : m_propertyTree.get_child(section))
|
|
|
|
{
|
2012-09-26 22:30:47 +00:00
|
|
|
if (boost::regex_search(v.first, re))
|
2012-09-26 20:57:43 +00:00
|
|
|
{
|
|
|
|
ret_list.push_back(v.second.get_value<std::string>());
|
|
|
|
}
|
|
|
|
}
|
2012-08-12 13:00:37 +00:00
|
|
|
}
|
2012-09-26 20:57:43 +00:00
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
|
|
|
GetLogger()->Info("Error on parsing profile: %s\n", e.what());
|
|
|
|
}
|
2012-08-12 13:00:37 +00:00
|
|
|
|
|
|
|
return ret_list;
|
|
|
|
}
|
2013-03-17 18:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
void CProfile::SetUserDir(std::string dir)
|
|
|
|
{
|
|
|
|
m_userDirectory = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string CProfile::GetUserBasedPath(std::string dir, std::string default_dir)
|
|
|
|
{
|
|
|
|
std::string path = dir;
|
|
|
|
boost::replace_all(path, "\\", "/");
|
|
|
|
if (dir.find("/") == std::string::npos) {
|
|
|
|
path = default_dir + "/" + dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_userDirectory.length() > 0) {
|
|
|
|
boost::replace_all(path, "%user%", m_userDirectory);
|
|
|
|
} else {
|
|
|
|
boost::replace_all(path, "%user%", default_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs::path(path).make_preferred().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CProfile::CopyFileToTemp(std::string filename)
|
|
|
|
{
|
|
|
|
std::string src, dst;
|
|
|
|
std::string tmp_user_dir = m_userDirectory;
|
|
|
|
|
|
|
|
src = GetUserBasedPath(filename, "textures");
|
|
|
|
SetUserDir("temp");
|
|
|
|
dst = GetUserBasedPath(filename, "textures");
|
|
|
|
SetUserDir(tmp_user_dir);
|
|
|
|
|
|
|
|
fs::create_directory(fs::path(dst).parent_path().make_preferred().string());
|
|
|
|
fs::copy_file(src, dst, fs::copy_option::overwrite_if_exists);
|
|
|
|
if (fs::exists(dst)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|