Repository navigation
Expand file tree
/
Copy pathconfig.cpp
More file actions
78 lines (64 loc) · 2.21 KB
/
Copy pathconfig.cpp
File metadata and controls
78 lines (64 loc) · 2.21 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
#include "config.h"
#include <iostream>
#include <fstream>
#include <algorithm>
std::unordered_map<std::string, ConfigElement> config = {
{"skip_sha1_checksum", {"Skips the ROM SHA-1 checksum. This allows using certain ROM hacks such as sound ROM hacks", 0}},
{"use_wasapi_audio", {"Prefer WASAPI audio backend over SDL2. Only works on Windows Vista and later.", 0}},
{"autospawn_mario_on_start", {"When loading a game, automatically spawn Mario.", 0}},
{"mario_in_mocap_cutscenes", {"Replaces CJ with Mario in the 'motion-capture' cutscenes. NOTE: This feature is not finished!", 0}},
};
int getConfig(std::string value)
{
return config.count(value) ? config[value].value : 0;
}
void saveConfig()
{
std::ofstream configfile(CONFIG_FILENAME);
for (auto& mapkey : config)
{
configfile << "# " << mapkey.second.desc << "\n";
configfile << mapkey.first << ": " << mapkey.second.value << "\n\n";
}
std::cout << "sm64-san-andreas.cfg saved\n";
}
void loadConfig()
{
std::ifstream configfile(CONFIG_FILENAME);
if (!configfile.is_open())
{
std::cout << "sm64-san-andreas.cfg not found, creating...\n";
saveConfig(); // saves default config
return;
}
size_t totalCfg = 0; // if less than config.size(), save file to create new keys
std::string line;
while (getline(configfile, line))
{
// clear any whitespaces
line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
if (line[0] == '#' || line.empty())
continue; // ignore comments and empty lines
std::size_t delimiterPos = line.find(":");
if (delimiterPos == -1)
continue; // invalid config syntax
std::string key = line.substr(0, delimiterPos);
std::string value = line.substr(delimiterPos + 1);
if (!config.count(key))
continue; // invalid config key
try
{
config[key].value = std::stoi(value);
totalCfg++;
}
catch(...)
{
continue;
}
}
if (totalCfg < config.size())
{
std::cout << "Updating sm64-san-andreas.cfg with new config keys...\n";
saveConfig();
}
}