2017-02-16 08:58:40 -07:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "Common/Config/Config.h"
|
|
|
|
#include "Common/Config/Layer.h"
|
|
|
|
|
|
|
|
namespace Config
|
|
|
|
{
|
2017-10-29 13:11:15 -06:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
std::string ValueToString(u16 value)
|
|
|
|
{
|
|
|
|
return StringFromFormat("0x%04x", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ValueToString(u32 value)
|
|
|
|
{
|
|
|
|
return StringFromFormat("0x%08x", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ValueToString(float value)
|
|
|
|
{
|
|
|
|
return StringFromFormat("%#.9g", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ValueToString(double value)
|
|
|
|
{
|
|
|
|
return StringFromFormat("%#.17g", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ValueToString(int value)
|
|
|
|
{
|
|
|
|
return std::to_string(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ValueToString(bool value)
|
|
|
|
{
|
|
|
|
return StringFromBool(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ValueToString(const std::string& value)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 08:58:40 -07:00
|
|
|
ConfigLayerLoader::ConfigLayerLoader(LayerType layer) : m_layer(layer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigLayerLoader::~ConfigLayerLoader() = default;
|
|
|
|
|
|
|
|
LayerType ConfigLayerLoader::GetLayer() const
|
|
|
|
{
|
|
|
|
return m_layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer::Layer(LayerType type) : m_layer(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer::Layer(std::unique_ptr<ConfigLayerLoader> loader)
|
|
|
|
: m_layer(loader->GetLayer()), m_loader(std::move(loader))
|
|
|
|
{
|
|
|
|
Load();
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer::~Layer()
|
|
|
|
{
|
|
|
|
Save();
|
|
|
|
}
|
|
|
|
|
2017-10-29 13:11:15 -06:00
|
|
|
bool Layer::Exists(const ConfigLocation& location) const
|
2017-02-16 08:58:40 -07:00
|
|
|
{
|
2017-10-29 13:11:15 -06:00
|
|
|
const auto iter = m_map.find(location);
|
|
|
|
return iter != m_map.end() && iter->second.has_value();
|
2017-02-16 08:58:40 -07:00
|
|
|
}
|
|
|
|
|
2017-10-29 13:11:15 -06:00
|
|
|
bool Layer::DeleteKey(const ConfigLocation& location)
|
2017-02-16 08:58:40 -07:00
|
|
|
{
|
2017-10-29 13:11:15 -06:00
|
|
|
m_is_dirty = true;
|
|
|
|
bool had_value = m_map[location].has_value();
|
|
|
|
m_map[location].reset();
|
|
|
|
return had_value;
|
2017-02-16 08:58:40 -07:00
|
|
|
}
|
|
|
|
|
2017-10-29 13:11:15 -06:00
|
|
|
void Layer::DeleteAllKeys()
|
2017-02-16 08:58:40 -07:00
|
|
|
{
|
2017-10-29 13:11:15 -06:00
|
|
|
m_is_dirty = true;
|
|
|
|
for (auto& pair : m_map)
|
2017-02-16 08:58:40 -07:00
|
|
|
{
|
2017-10-29 13:11:15 -06:00
|
|
|
pair.second.reset();
|
2017-02-16 08:58:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layer::Load()
|
|
|
|
{
|
|
|
|
if (m_loader)
|
|
|
|
m_loader->Load(this);
|
2017-10-29 13:11:15 -06:00
|
|
|
m_is_dirty = false;
|
2017-02-16 08:58:40 -07:00
|
|
|
InvokeConfigChangedCallbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layer::Save()
|
|
|
|
{
|
2017-10-29 13:11:15 -06:00
|
|
|
if (!m_loader || !m_is_dirty)
|
2017-02-16 08:58:40 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_loader->Save(this);
|
2017-10-29 13:11:15 -06:00
|
|
|
m_is_dirty = false;
|
2017-02-16 08:58:40 -07:00
|
|
|
InvokeConfigChangedCallbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerType Layer::GetLayer() const
|
|
|
|
{
|
|
|
|
return m_layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LayerMap& Layer::GetLayerMap() const
|
|
|
|
{
|
2017-10-29 13:11:15 -06:00
|
|
|
return m_map;
|
2017-02-16 08:58:40 -07:00
|
|
|
}
|
|
|
|
}
|