-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwildcatsavefile.cpp
94 lines (62 loc) · 2.41 KB
/
wildcatsavefile.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
#include "wildcatsavefile.h"
#include "helpers.h"
#include "scanner.hpp"
#include <SDL_log.h>
#include <SDL_messagebox.h>
#include <fstream>
WildcatSaveFile::WildcatSaveFile() {}
void WildcatSaveFile::writeToDisk(const std::filesystem::path &file) {
std::ofstream ofs(file, std::ios::binary);
ofs.write((char *)&header, sizeof(header));
ofs.write((char *)&version, sizeof(version));
for (auto &channel : channels) {
size_t cNameSize = channel.name.size() + 1;
ofs.write((char *)&cNameSize, sizeof(size_t));
ofs.write((char *)channel.name.c_str(), cNameSize);
ofs.write((char *)&channel.frequency, sizeof(float));
ofs.write((char *)&channel.mod, sizeof(int));
ofs.write((char *)&channel.lockout, sizeof(int));
ofs.write((char *)&channel.delay, sizeof(int));
ofs.write((char *)&channel.priority, sizeof(int));
}
ofs.close();
SDL_Log("Saved data to: %s", file.c_str());
}
void WildcatSaveFile::readFromDisk(const std::filesystem::path &file) {
std::ifstream ifs(file, std::ios::binary);
ifs.read((char *)&header, sizeof(header));
if (header != SCANNER_SAVE_DATA_HEADER) {
Helper_ErrorMsg("Invalid header in save file: " + file.generic_string());
std::exit(EXIT_FAILURE);
}
ifs.read((char *)&version, sizeof(version));
if (version != SCANNER_SAVE_DATA_VERSION) {
Helper_ErrorMsg("Invalid version in save file: " + file.generic_string());
std::exit(EXIT_FAILURE);
}
for (int i = 0; i < SCANNER_CHANNELS; i++) {
auto &channel = channels[i];
size_t cNameSize = channel.name.size();
ifs.read((char *)&cNameSize, sizeof(size_t));
char *name = (char *)malloc(cNameSize);
ifs.read((char *)name, cNameSize);
channel.name = name;
free(name);
ifs.read((char *)&channel.frequency, sizeof(float));
ifs.read((char *)&channel.mod, sizeof(int));
channel.internalState.modSelected = (int)channel.mod;
ifs.read((char *)&channel.lockout, sizeof(int));
channel.internalState.lockoutSelected = (int)channel.lockout;
ifs.read((char *)&channel.delay, sizeof(int));
if (channel.delay == -10) {
channel.internalState.delaySelected = 0;
} else if (channel.delay == -5) {
channel.internalState.delaySelected = 1;
} else {
channel.internalState.delaySelected = channel.delay + 2;
}
ifs.read((char *)&channel.priority, sizeof(int));
}
ifs.close();
SDL_Log("Loaded data from: %s", file.c_str());
}