summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Config/Layer.cpp
blob: 61f90538a0527d71467cfe2cb14fe1460b65913a (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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"
#include "Common/Config/Section.h"

namespace Config
{
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();
}

bool Layer::Exists(System system, const std::string& section_name, const std::string& key)
{
  Section* section = GetSection(system, section_name);
  if (!section)
    return false;
  return section->Exists(key);
}

bool Layer::DeleteKey(System system, const std::string& section_name, const std::string& key)
{
  Section* section = GetSection(system, section_name);
  if (!section)
    return false;
  return section->Delete(key);
}

Section* Layer::GetSection(System system, const std::string& section_name)
{
  for (auto& section : m_sections[system])
    if (!strcasecmp(section->m_name.c_str(), section_name.c_str()))
      return section.get();
  return nullptr;
}

Section* Layer::GetOrCreateSection(System system, const std::string& section_name)
{
  Section* section = GetSection(system, section_name);
  if (!section)
  {
    if (m_layer == LayerType::Meta)
    {
      m_sections[system].emplace_back(
          std::make_unique<RecursiveSection>(m_layer, system, section_name));
    }
    else
    {
      m_sections[system].emplace_back(std::make_unique<Section>(m_layer, system, section_name));
    }
    section = m_sections[system].back().get();
  }
  return section;
}

void Layer::Load()
{
  if (m_loader)
    m_loader->Load(this);
  ClearDirty();
  InvokeConfigChangedCallbacks();
}

void Layer::Save()
{
  if (!m_loader || !IsDirty())
    return;

  m_loader->Save(this);
  ClearDirty();
  InvokeConfigChangedCallbacks();
}

LayerType Layer::GetLayer() const
{
  return m_layer;
}

const LayerMap& Layer::GetLayerMap() const
{
  return m_sections;
}

ConfigLayerLoader* Layer::GetLoader() const
{
  return m_loader.get();
}

bool Layer::IsDirty() const
{
  return std::any_of(m_sections.begin(), m_sections.end(), [](const auto& system) {
    return std::any_of(system.second.begin(), system.second.end(),
                       [](const auto& section) { return section->IsDirty(); });
  });
}

void Layer::ClearDirty()
{
  std::for_each(m_sections.begin(), m_sections.end(), [](auto& system) {
    std::for_each(system.second.begin(), system.second.end(),
                  [](auto& section) { section->ClearDirty(); });
  });
}

RecursiveLayer::RecursiveLayer() : Layer(LayerType::Meta)
{
}

Section* RecursiveLayer::GetSection(System system, const std::string& section_name)
{
  // Always queries backwards recursively, so it doesn't matter if it exists or not on this layer
  return GetOrCreateSection(system, section_name);
}

Section* RecursiveLayer::GetOrCreateSection(System system, const std::string& section_name)
{
  Section* section = Layer::GetSection(system, section_name);
  if (!section)
  {
    m_sections[system].emplace_back(
        std::make_unique<RecursiveSection>(m_layer, system, section_name));
    section = m_sections[system].back().get();
  }
  return section;
}
}