-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.cpp
More file actions
174 lines (135 loc) · 6.44 KB
/
settings.cpp
File metadata and controls
174 lines (135 loc) · 6.44 KB
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.h"
#include "logging.h"
#include "utils.h"
#include <iostream>
#include <fstream>
#include <map>
#include <QDebug>
#include <QFile>
namespace Pico{
namespace Settings{
Pico::Logging::Funcs logging;
std::string FD_CONFIG::SERVER_ADDRESS = "lobby.faforever.com";
int FD_CONFIG::SERVER_PORT = 8001;
std::string FD_CONFIG::LOGIN = "Player";
std::string FD_CONFIG::PASSWORD = "foo";
std::string FD_CONFIG::HASHWORD = "";
std::string FD_CONFIG::LOG_PATH = "logs/";
std::string FD_CONFIG::GAME_PATH = "X:/GAMES/SCFA/Supreme Commander - Forged Alliance";
std::string FD_CONFIG::FAF_PATH = "X:/GAMES/SCFA/FAForever";
std::string FD_CONFIG::CUSTOM_PATH = "X:/Users/Player/Documents/My Games/Gas Powered Games/Supreme Commander - Forged Alliance";
void InitializeSettings(){
logging.Write("INITIALIZE_CONFIG => Starting config initialization.");
std::ostringstream newSettingsStream;
std::string configPath = HC_CONFIG.SETTINGS_FILE;
std::ifstream t(configPath);
std::string settingsContent((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
bool exists = Utils::file_exists(configPath);
if (!exists){
CreateConfigFile();
}
std::vector<std::string> settingsLines = Pico::Utils::explode(settingsContent, '\n');
for (int i = 0; i < (int)settingsLines.size(); i++){
std::string thisLine = settingsLines.at(i);
if (thisLine.find(":") == std::string::npos){
continue;
}
std::vector<std::string> lineContents;
size_t pos = thisLine.find_first_of(":");
lineContents.push_back(thisLine.substr(0, pos));
lineContents.push_back(thisLine.substr(pos+1, thisLine.length()));
std::string settingName = lineContents.at(0);
std::string settingValue = "";
if (lineContents.size() > 1){
settingValue = lineContents.at(1);
}
bool readData = true;
switch (Pico::Utils::str2int(settingName.data())){
default:{
readData = false;
break;
}
case Pico::Utils::str2int("SERVER_ADDRESS"):{
FD_CONFIG::SERVER_ADDRESS = settingValue;
break;
}
case Pico::Utils::str2int("SERVER_PORT"):{
FD_CONFIG::SERVER_PORT = std::stoi(settingValue);
break;
}
case Pico::Utils::str2int("LOGIN"):{
FD_CONFIG::LOGIN = settingValue;
break;
}
case Pico::Utils::str2int("PASSWORD"):{
if (settingValue.length() > 0){
QByteArray hashwordBA = QCryptographicHash::hash(QByteArray::fromStdString(settingValue),QCryptographicHash::Sha256).toHex();
QString hashword = QString(hashwordBA);
settingValue = "";
FD_CONFIG::HASHWORD = hashword.toStdString();
}
break;
}
case Pico::Utils::str2int("HASHWORD"):{
if (FD_CONFIG::HASHWORD.length() == 0){
FD_CONFIG::HASHWORD = settingValue;
}
settingValue = FD_CONFIG::HASHWORD;
break;
}
case Pico::Utils::str2int("LOG_PATH"):{
FD_CONFIG::LOG_PATH = settingValue;
break;
}
case Pico::Utils::str2int("GAME_PATH"):{
FD_CONFIG::GAME_PATH = settingValue;
break;
}
case Pico::Utils::str2int("FAF_PATH"):{
FD_CONFIG::FAF_PATH = settingValue;
break;
}
case Pico::Utils::str2int("CUSTOM_PATH"):{
FD_CONFIG::CUSTOM_PATH = settingValue;
break;
}
}
if (readData){
logging.Write("INITIALIZE_CONFIG => SET "+QString::fromStdString(settingName)+" TO ["+QString::fromStdString(settingValue)+"]");
newSettingsStream << settingName << ":" << settingValue << std::endl;
}
}
std::string newSettings(newSettingsStream.str());
logging.Write("INITIALIZE_CONFIG => Finished config initialization.");
QFile oldSettingsFile(QString::fromStdString(configPath));
oldSettingsFile.remove();
QFile newSettingsFile (QString::fromStdString(configPath));
if (newSettingsFile.open(QIODevice::ReadWrite) )
{
QTextStream stream(&newSettingsFile);
stream << QString::fromStdString(newSettings);
newSettingsFile.close();
logging.Write("INITIALIZE_CONFIG => Successfully wrote to disk.");
}
}
void CreateConfigFile(){
/*
std::string configPath = HC_CONFIG.SETTINGS_FILE;
QFile newSettingsFile (QString::fromStdString(configPath));
if (newSettingsFile.open(QIODevice::WriteOnly) )
{
QTextStream stream( &newSettingsFile);
//std::vector<std::vector<QString, QString>> settingsList = {{"SERVER_ADDRESS", FD_CONFIG::SERVER_ADDRESS}};
std::map<QString, QString> defaultSettings;
defaultSettings.insert(std::pair<QString, QString>("SERVER_ADDRESS", FD_CONFIG::SERVER_ADDRESS));
for (const auto& pair : defaultSettings) {
stream << pair.first << ":" << pair.second << std::endl;
}
newSettingsFile.close();
logging.Write("From CREATE_CONFIG_FILE => Created a new config file.");
}
*/
}
}
}