-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
BaseConfigLoader.cpp
103 lines (88 loc) · 3.05 KB
/
BaseConfigLoader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <cstring>
#include <list>
#include <map>
#include <memory>
#include <string>
#include "Common/CommonPaths.h"
#include "Common/CommonTypes.h"
#include "Common/Config/Config.h"
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Common/Logging/Log.h"
#include "Core/Config/Config.h"
#include "Core/ConfigLoaders/BaseConfigLoader.h"
#include "Core/ConfigLoaders/IsSettingSaveable.h"
namespace ConfigLoaders
{
const std::map<Config::System, int> system_to_ini = {
{Config::System::Main, F_DOLPHINCONFIG_IDX},
{Config::System::GCPad, F_GCPADCONFIG_IDX},
{Config::System::WiiPad, F_WIIPADCONFIG_IDX},
{Config::System::GCKeyboard, F_GCKEYBOARDCONFIG_IDX},
{Config::System::GFX, F_GFXCONFIG_IDX},
{Config::System::Logger, F_LOGGERCONFIG_IDX},
{Config::System::Debugger, F_DEBUGGERCONFIG_IDX},
{Config::System::UI, F_UICONFIG_IDX},
};
// INI layer configuration loader
class INIBaseConfigLayerLoader final : public Config::ConfigLayerLoader
{
public:
INIBaseConfigLayerLoader() : ConfigLayerLoader(Config::LayerType::Base) {}
void Load(Config::Layer* config_layer) override
{
for (const auto& system : system_to_ini)
{
IniFile ini;
ini.Load(File::GetUserPath(system.second));
const std::list<IniFile::Section>& system_sections = ini.GetSections();
for (const auto& section : system_sections)
{
const std::string section_name = section.GetName();
Config::Section* config_section =
config_layer->GetOrCreateSection(system.first, section_name);
const IniFile::Section::SectionMap& section_map = section.GetValues();
for (const auto& value : section_map)
config_section->Set(value.first, value.second);
}
}
}
void Save(Config::Layer* config_layer) override
{
const Config::LayerMap& sections = config_layer->GetLayerMap();
for (const auto& system : sections)
{
auto mapping = system_to_ini.find(system.first);
if (mapping == system_to_ini.end())
{
ERROR_LOG(COMMON, "Config can't map system '%s' to an INI file!",
Config::GetSystemName(system.first).c_str());
continue;
}
IniFile ini;
ini.Load(File::GetUserPath(mapping->second));
for (const auto& section : system.second)
{
const std::string section_name = section->GetName();
const Config::SectionValueMap& section_values = section->GetValues();
IniFile::Section* ini_section = ini.GetOrCreateSection(section_name);
for (const auto& value : section_values)
{
if (!IsSettingSaveable({system.first, section->GetName(), value.first}))
continue;
ini_section->Set(value.first, value.second);
}
}
ini.Save(File::GetUserPath(mapping->second));
}
}
};
// Loader generation
std::unique_ptr<Config::ConfigLayerLoader> GenerateBaseConfigLoader()
{
return std::make_unique<INIBaseConfigLayerLoader>();
}
}