Skip to content

Commit

Permalink
Merge pull request #4917 from leoetlino/config
Browse files Browse the repository at this point in the history
New configuration namespace
  • Loading branch information
Helios747 committed Feb 26, 2017
2 parents 06428c3 + 88a21dd commit 0dde642
Show file tree
Hide file tree
Showing 10 changed files with 876 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Source/Core/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
set(SRCS Analytics.cpp
CDUtils.cpp
ColorUtil.cpp
Config/Config.cpp
Config/Layer.cpp
Config/Section.cpp
ENetUtil.cpp
FileSearch.cpp
FileUtil.cpp
Expand Down
7 changes: 7 additions & 0 deletions Source/Core/Common/Common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
<ClInclude Include="CommonFuncs.h" />
<ClInclude Include="CommonPaths.h" />
<ClInclude Include="CommonTypes.h" />
<ClInclude Include="Config\Config.h" />
<ClInclude Include="Config\Enums.h" />
<ClInclude Include="Config\Layer.h" />
<ClInclude Include="Config\Section.h" />
<ClInclude Include="CPUDetect.h" />
<ClInclude Include="DebugInterface.h" />
<ClInclude Include="ENetUtil.h" />
Expand Down Expand Up @@ -149,6 +153,9 @@
<ClCompile Include="Analytics.cpp" />
<ClCompile Include="CDUtils.cpp" />
<ClCompile Include="ColorUtil.cpp" />
<ClCompile Include="Config\Config.cpp" />
<ClCompile Include="Config\Layer.cpp" />
<ClCompile Include="Config\Section.cpp" />
<ClCompile Include="ENetUtil.cpp" />
<ClCompile Include="FileSearch.cpp" />
<ClCompile Include="FileUtil.cpp" />
Expand Down
7 changes: 7 additions & 0 deletions Source/Core/Common/Common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<ClInclude Include="CommonFuncs.h" />
<ClInclude Include="CommonPaths.h" />
<ClInclude Include="CommonTypes.h" />
<ClInclude Include="Config\Config.h" />
<ClInclude Include="Config\Enums.h" />
<ClInclude Include="Config\Layer.h" />
<ClInclude Include="Config\Section.h" />
<ClInclude Include="CPUDetect.h" />
<ClInclude Include="DebugInterface.h" />
<ClInclude Include="ENetUtil.h" />
Expand Down Expand Up @@ -232,6 +236,9 @@
<ItemGroup>
<ClCompile Include="CDUtils.cpp" />
<ClCompile Include="ColorUtil.cpp" />
<ClCompile Include="Config\Config.cpp" />
<ClCompile Include="Config\Layer.cpp" />
<ClCompile Include="Config\Section.cpp" />
<ClCompile Include="ENetUtil.cpp" />
<ClCompile Include="FileSearch.cpp" />
<ClCompile Include="FileUtil.cpp" />
Expand Down
140 changes: 140 additions & 0 deletions Source/Core/Common/Config/Config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <algorithm>
#include <list>
#include <map>

#include "Common/Assert.h"
#include "Common/Config/Config.h"

namespace Config
{
static Layers s_layers;
static std::list<ConfigChangedCallback> s_callbacks;

void InvokeConfigChangedCallbacks();

Section* GetOrCreateSection(System system, const std::string& section_name)
{
return s_layers[LayerType::Meta]->GetOrCreateSection(system, section_name);
}

Layers* GetLayers()
{
return &s_layers;
}

void AddLayer(std::unique_ptr<Layer> layer)
{
s_layers[layer->GetLayer()] = std::move(layer);
InvokeConfigChangedCallbacks();
}

void AddLayer(std::unique_ptr<ConfigLayerLoader> loader)
{
AddLayer(std::make_unique<Layer>(std::move(loader)));
}

void AddLoadLayer(std::unique_ptr<Layer> layer)
{
layer->Load();
AddLayer(std::move(layer));
}

void AddLoadLayer(std::unique_ptr<ConfigLayerLoader> loader)
{
AddLoadLayer(std::make_unique<Layer>(std::move(loader)));
}

Layer* GetLayer(LayerType layer)
{
if (!LayerExists(layer))
return nullptr;
return s_layers[layer].get();
}

void RemoveLayer(LayerType layer)
{
s_layers.erase(layer);
InvokeConfigChangedCallbacks();
}
bool LayerExists(LayerType layer)
{
return s_layers.find(layer) != s_layers.end();
}

void AddConfigChangedCallback(ConfigChangedCallback func)
{
s_callbacks.emplace_back(func);
}

void InvokeConfigChangedCallbacks()
{
for (const auto& callback : s_callbacks)
callback();
}

// Explicit load and save of layers
void Load()
{
for (auto& layer : s_layers)
layer.second->Load();
}

void Save()
{
for (auto& layer : s_layers)
layer.second->Save();
}

void Init()
{
// This layer always has to exist
s_layers[LayerType::Meta] = std::make_unique<RecursiveLayer>();
}

void Shutdown()
{
s_layers.clear();
s_callbacks.clear();
}

static const std::map<System, std::string> system_to_name = {
{System::Main, "Dolphin"}, {System::GCPad, "GCPad"}, {System::WiiPad, "Wiimote"},
{System::GCKeyboard, "GCKeyboard"}, {System::GFX, "Graphics"}, {System::Logger, "Logger"},
{System::Debugger, "Debugger"}, {System::UI, "UI"},
};

const std::string& GetSystemName(System system)
{
return system_to_name.at(system);
}

System GetSystemFromName(const std::string& name)
{
const auto system = std::find_if(system_to_name.begin(), system_to_name.end(),
[&name](const auto& entry) { return entry.second == name; });
if (system != system_to_name.end())
return system->first;

_assert_msg_(COMMON, false, "Programming error! Couldn't convert '%s' to system!", name.c_str());
return System::Main;
}

const std::string& GetLayerName(LayerType layer)
{
static const std::map<LayerType, std::string> layer_to_name = {
{LayerType::Base, "Base"},
{LayerType::GlobalGame, "Global GameINI"},
{LayerType::LocalGame, "Local GameINI"},
{LayerType::Netplay, "Netplay"},
{LayerType::Movie, "Movie"},
{LayerType::CommandLine, "Command Line"},
{LayerType::CurrentRun, "Current Run"},
{LayerType::Meta, "Top"},
};
return layer_to_name.at(layer);
}
}
63 changes: 63 additions & 0 deletions Source/Core/Common/Config/Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <functional>
#include <map>
#include <memory>
#include <string>

#include "Common/Config/Enums.h"
#include "Common/Config/Layer.h"
#include "Common/Config/Section.h"

namespace Config
{
using Layers = std::map<LayerType, std::unique_ptr<Layer>>;
using ConfigChangedCallback = std::function<void()>;

// Common function used for getting configuration
Section* GetOrCreateSection(System system, const std::string& section_name);

// Layer management
Layers* GetLayers();
void AddLayer(std::unique_ptr<Layer> layer);
void AddLayer(std::unique_ptr<ConfigLayerLoader> loader);
void AddLoadLayer(std::unique_ptr<Layer> layer);
void AddLoadLayer(std::unique_ptr<ConfigLayerLoader> loader);
Layer* GetLayer(LayerType layer);
void RemoveLayer(LayerType layer);
bool LayerExists(LayerType layer);

void AddConfigChangedCallback(ConfigChangedCallback func);
void InvokeConfigChangedCallbacks();

// Explicit load and save of layers
void Load();
void Save();

// Often used functions for getting or setting configuration on the base layer for the main system
template <typename T>
T Get(const std::string& section_name, const std::string& key, const T& default_value)
{
auto base_layer = GetLayer(Config::LayerType::Base);
return base_layer->GetOrCreateSection(Config::System::Main, section_name)
->Get(key, default_value);
}

template <typename T>
void Set(const std::string& section_name, const std::string& key, const T& value)
{
auto base_layer = GetLayer(Config::LayerType::Base);
base_layer->GetOrCreateSection(Config::System::Main, section_name)->Set(key, value);
}

void Init();
void Shutdown();

const std::string& GetSystemName(System system);
System GetSystemFromName(const std::string& system);
const std::string& GetLayerName(LayerType layer);
}
32 changes: 32 additions & 0 deletions Source/Core/Common/Config/Enums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

namespace Config
{
enum class LayerType
{
Base,
GlobalGame,
LocalGame,
Movie,
Netplay,
CommandLine,
CurrentRun,
Meta,
};

enum class System
{
Main,
GCPad,
WiiPad,
GCKeyboard,
GFX,
Logger,
Debugger,
UI,
};
}

0 comments on commit 0dde642

Please sign in to comment.