configmanager.cpp

main 131 lines · 3.2 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "configmanager.h"
24
25ConfigManager g_configs;
26
27void ConfigManager::init()
28{
29 m_settings = ConfigPtr(new Config());
30}
31
32void ConfigManager::terminate()
33{
34 if(m_settings) {
35 // ensure settings are saved
36 m_settings->save();
37
38 m_settings->unload();
39 m_settings = nullptr;
40 }
41
42 for(ConfigPtr config : m_configs) {
43 config->unload();
44 config = nullptr;
45 }
46
47 m_configs.clear();
48}
49
50ConfigPtr ConfigManager::getSettings()
51{
52 return m_settings;
53}
54
55ConfigPtr ConfigManager::get(const std::string& file)
56{
57 for(const ConfigPtr& config : m_configs) {
58 if(config->getFileName() == file) {
59 return config;
60 }
61 }
62 return nullptr;
63}
64
65ConfigPtr ConfigManager::loadSettings(const std::string file)
66{
67 if(file.empty()) {
68 g_logger.error("Must provide a configuration file to load.");
69 }
70 else {
71 if(m_settings->load(file)) {
72 return m_settings;
73 }
74 }
75 return nullptr;
76}
77
78ConfigPtr ConfigManager::create(const std::string& file)
79{
80 ConfigPtr config = load(file);
81 if(!config) {
82 config = ConfigPtr(new Config());
83
84 config->load(file);
85 config->save();
86
87 m_configs.push_back(config);
88 }
89 return config;
90}
91
92ConfigPtr ConfigManager::load(const std::string& file)
93{
94 if(file.empty()) {
95 g_logger.error("Must provide a configuration file to load.");
96 return nullptr;
97 }
98 else {
99 ConfigPtr config = get(file);
100 if(!config) {
101 config = ConfigPtr(new Config());
102
103 if(config->load(file)) {
104 m_configs.push_back(config);
105 }
106 else {
107 // cannot load config
108 config = nullptr;
109 }
110 }
111 return config;
112 }
113}
114
115bool ConfigManager::unload(const std::string& file)
116{
117 ConfigPtr config = get(file);
118 if(config) {
119 config->unload();
120 remove(config);
121 config = nullptr;
122 return true;
123 }
124 return false;
125}
126
127void ConfigManager::remove(const ConfigPtr config)
128{
129 m_configs.remove(config);
130}
131
Top