-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
146 lines (134 loc) · 3.88 KB
/
main.cpp
File metadata and controls
146 lines (134 loc) · 3.88 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
#include <algorithm>
#include <iterator>
#include <cassert>
#include <filesystem>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <fstream>
//From http://www.richelbilderbeek.nl/CppSaveContainer.htm
template <class Container>
void save_container(const Container& c, const std::string& filename)
{
std::ofstream f(filename.c_str());
std::copy(c.begin(),c.end(),std::ostream_iterator<typename Container::value_type>(f,"\n"));
}
///Determines if a filename is a regular file
///From http://www.richelbilderbeek.nl/CppIsRegularFile.htm
bool is_regular_file(const std::string& filename)
{
std::fstream f;
f.open(filename.c_str(),std::ios::in);
return f.is_open();
}
///FileToVector reads a file and converts it to a std::vector<std::string>
///From http://www.richelbilderbeek.nl/CppFileToVector.htm
std::vector<std::string> to_vector(const std::string& filename)
{
assert(is_regular_file(filename));
std::vector<std::string> v;
std::ifstream in(filename.c_str());
for (int i=0; !in.eof(); ++i)
{
std::string s;
std::getline(in,s);
v.push_back(s);
}
return v;
}
int find_function_definition_first_line(
const std::string& function_name,
const std::vector<std::string>& text
)
{
const int sz = text.size();
const std::string hit{"int " + function_name + "(int"};
for (int i{0}; i != sz; ++i)
{
if (text[i].substr(0, hit.size()) == hit)
{
if (text[i].find(';') != std::string::npos) continue;
return i;
}
}
assert(!"Should not get here");
return -1;
}
int find_function_definition_last_line(
const std::string& function_name,
const std::vector<std::string>& text
)
{
const int sz = text.size();
const std::string hit{"}"};
const int start_index{find_function_definition_first_line(function_name, text)};
for (int i{start_index}; i != sz; ++i)
{
if (text[i].substr(0, hit.size()) == hit)
{
return i;
}
}
assert(!"Should not get here");
return -1;
}
/// Simplify a function
std::vector<std::string> simplify_function(
const std::string& function_name,
std::vector<std::string> text
)
{
/*
int GetSystemCost(int Num)
{
[...]
return 0;
}
*/
assert(find_function_definition_first_line(function_name, text) != -1);
assert(find_function_definition_last_line(function_name, text) != -1);
const int start_index{find_function_definition_first_line(function_name, text)};
text[start_index] = "int " + function_name + "(int)";
const int from{start_index + 2};
const int end_index{find_function_definition_last_line(function_name, text)};
const int to{end_index - 1};
assert(from < to);
assert(from < end_index);
assert(to < end_index);
for (int i{from}; i!=to; ++i)
{
text[i] = "";
}
text[to] = "return 0;";
return text;
}
void patch_file(
const std::filesystem::path& filename,
const std::string& function_name
)
{
const auto text{to_vector(filename)};
const auto new_text{simplify_function(function_name, text)};
save_container(new_text, filename);
}
int main(int argc, char* argv[])
{
std::filesystem::path workshop_path{"../astro_menace_cheat/astromenace/src/menu/menu_workshop_workshop.cpp"};
std::filesystem::path shipyard_path{"../astro_menace_cheat/astromenace/src/menu/menu_workshop_shipyard.cpp"};
std::filesystem::path weaponry_path{"../astro_menace_cheat/astromenace/src/menu/menu_workshop_weaponry.cpp"};
const std::string workshop_function_name{"GetSystemCost"};
const std::string shipyard_function_name{"GetWorkshopShipCost"};
const std::string weaponry_function_name{"GetWeaponBaseCost"};
if (argc == 4)
{
workshop_path = std::string(argv[1]);
shipyard_path = std::string(argv[2]);
weaponry_path = std::string(argv[3]);
}
patch_file(workshop_path, workshop_function_name);
patch_file(shipyard_path, shipyard_function_name);
patch_file(weaponry_path, weaponry_function_name);
}