-
Notifications
You must be signed in to change notification settings - Fork 11
/
Configuration.cpp
134 lines (119 loc) · 4.34 KB
/
Configuration.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
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
//////////////////////////////////////////////////////////////////////////////
// This file is part of the Journey MMORPG client //
// Copyright © 2015-2016 Daniel Allendorf //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
#include "Configuration.h"
#include <fstream>
namespace jrc
{
Configuration::Configuration()
{
settings.emplace<ServerIP>();
settings.emplace<Fullscreen>();
settings.emplace<VSync>();
settings.emplace<FontPathNormal>();
settings.emplace<FontPathBold>();
settings.emplace<BGMVolume>();
settings.emplace<SFXVolume>();
settings.emplace<SaveLogin>();
settings.emplace<DefaultAccount>();
settings.emplace<DefaultWorld>();
settings.emplace<DefaultChannel>();
settings.emplace<DefaultCharacter>();
settings.emplace<PosSTATS>();
settings.emplace<PosEQINV>();
settings.emplace<PosINV>();
settings.emplace<PosSKILL>();
load();
}
Configuration::~Configuration()
{
save();
}
void Configuration::load()
{
std::unordered_map<std::string, std::string> rawsettings;
std::ifstream file(FILENAME);
if (file.is_open())
{
// Go through the file line for line.
std::string line;
while (getline(file, line))
{
// If the setting is not empty, load the value.
size_t split = line.find('=');
if (split != std::string::npos && split + 2 < line.size())
{
rawsettings.emplace(
line.substr(0, split - 1),
line.substr(split + 2)
);
}
}
}
// Replace default values with loaded values.
for (auto& setting : settings)
{
auto rsiter = rawsettings.find(setting.second->name);
if (rsiter != rawsettings.end())
setting.second->value = rsiter->second;
}
}
void Configuration::save() const
{
// Open the settings file.
std::ofstream config(FILENAME);
if (config.is_open())
{
// Save settings line by line.
for (auto& setting : settings)
{
config << setting.second->to_string() << std::endl;
}
}
}
void Configuration::BoolEntry::save(bool b)
{
value = b ? "true" : "false";
}
bool Configuration::BoolEntry::load() const
{
return value == "true";
}
void Configuration::StringEntry::save(std::string str)
{
value = str;
}
std::string Configuration::StringEntry::load() const
{
return value;
}
void Configuration::PointEntry::save(Point<int16_t> vec)
{
value = vec.to_string();
}
Point<int16_t> Configuration::PointEntry::load() const
{
std::string xstr = value.substr(1, value.find(",") - 1);
std::string ystr = value.substr(
value.find(",") + 1,
value.find(")") - value.find(",") - 1
);
auto x = string_conversion::or_zero<int16_t>(xstr);
auto y = string_conversion::or_zero<int16_t>(ystr);
return{ x, y };
}
}