-
Notifications
You must be signed in to change notification settings - Fork 2
/
settings_loader.cpp
174 lines (138 loc) · 4.11 KB
/
settings_loader.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "settings_loader.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include "logging.hpp"
inline bool exists (const std::string& name) {
std::ifstream f(name.c_str());
return f.good();
}
std::vector<std::string> get_content(const std::string& loc)
{
std::ifstream f;
f.open(loc.c_str());
if(!f.is_open())
{
lg::log("Could not find file");
return std::vector<std::string>();
}
std::vector<std::string> content;
while(f.good())
{
std::string str;
std::getline(f, str);
content.push_back(str);
}
if(content.size() < 6)
{
lg::log("invalid settings file");
return std::vector<std::string>();
}
return content;
}
void settings::load(const std::string& loc)
{
std::vector<std::string> content = get_content(loc);
for(auto& i : content)
{
size_t it = i.find('#');
if(it == std::string::npos)
continue;
if(it > 0)
{
///also remove the preceding space before the comment
///should probably strip all spaces, but this is good enough
if(i[it-1] == ' ')
{
it--;
}
i.erase(it, std::string::npos);
}
}
if(content.size() < 14)
{
lg::log("Invalid settings file");
return;
}
width = atoi(content[0].c_str());
height = atoi(content[1].c_str());
ip = content[2];
quality = atoi(content[3].c_str());
name = content[4];
enable_debugging = content[5] == "DEBUG";
mouse_sens = atof(content[6].c_str());
motion_blur_strength = atof(content[7].c_str());
motion_blur_camera_contribution = atof(content[8].c_str());
use_post_aa = atoi(content[9].c_str());
use_raw_input = atoi(content[10].c_str());
frames_of_input_lag = atoi(content[11].c_str());
horizontal_fov_degrees = atof(content[12].c_str());
use_frametime_management = atoi(content[13].c_str());
is_fullscreen = atoi(content[14].c_str());
if(name.length() == 0)
{
name = "No Name";
}
}
void settings::save(const std::string& loc)
{
std::vector<std::string> content = get_content(loc);
std::vector<std::string> comments;
comments.resize(content.size());
for(int n=0; n<content.size(); n++)
{
std::string i = content[n];
size_t it = i.find('#');
if(it == std::string::npos)
continue;
if(it > 0)
{
if(i[it-1] == ' ')
{
it--;
}
comments[n].append(i.begin() + it, i.end());
}
}
std::vector<std::string> to_save;
int c = 0;
to_save.push_back(std::to_string(width));
to_save.push_back(std::to_string(height));
to_save.push_back(ip);
to_save.push_back(std::to_string(quality));
to_save.push_back(name);
std::string debug_string = enable_debugging == 0 ? "RELEASE" : "DEBUG";
to_save.push_back(debug_string);
to_save.push_back(std::to_string(mouse_sens) + "f");
to_save.push_back(std::to_string(motion_blur_strength));
to_save.push_back(std::to_string(motion_blur_camera_contribution));
to_save.push_back(std::to_string(use_post_aa));
to_save.push_back(std::to_string(use_raw_input));
to_save.push_back(std::to_string(frames_of_input_lag));
to_save.push_back(std::to_string(horizontal_fov_degrees));
to_save.push_back(std::to_string(use_frametime_management));
to_save.push_back(std::to_string(is_fullscreen));
for(int i=0; i<comments.size() && i < to_save.size(); i++)
{
to_save[i].append(comments[i].begin(), comments[i].end());
}
std::string fname = loc + ".bak";
{
std::ofstream out(fname);
for(auto& i : to_save)
{
out << i << std::endl;
}
}
std::string old = loc + ".old";
if(exists(old))
{
remove(old.c_str());
}
///if new filename exists, probably crash
if(exists(loc.c_str()))
rename(loc.c_str(), (loc + ".old").c_str());
rename(fname.c_str(), loc.c_str());
}