-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvermgr.cpp
More file actions
178 lines (154 loc) · 4.85 KB
/
Copy pathvermgr.cpp
File metadata and controls
178 lines (154 loc) · 4.85 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
175
176
177
178
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <filesystem>
#include <chrono>
#include <thread>
#include <random>
#define ROOT_DIR "."
#define EXCLUDED_DIR "config_template"
namespace fs = std::filesystem;
void copyTemplates(const fs::path& src, const fs::path& dest)
{
std::cout << "Copying templates..." << std::endl;
try
{
if(!fs::exists(src)) return;
for(const auto& entry : fs::recursive_directory_iterator(src))
{
const auto& path = entry.path();
auto relativePath = fs::relative(path, src);
auto targetPath = dest / relativePath;
if(fs::is_directory(entry.status()))
{
fs::create_directories(targetPath);
}
else if(fs::is_regular_file(entry.status()))
{
fs::create_directories(targetPath.parent_path());
fs::copy_file(path, targetPath, fs::copy_options::overwrite_existing);
}
}
}
catch (const fs::filesystem_error& e)
{
std::cerr << "Filesystem err: " << e.what() << '\n';
}
}
void replaceInFile(const fs::path& filePath, const std::string& target, const std::string& replacement)
{
std::ifstream inFile(filePath);
if(!inFile.is_open())
{
std::cerr << "Err: " << filePath << '\n';
return;
}
std::stringstream buffer;
buffer << inFile.rdbuf();
std::string content = buffer.str();
inFile.close();
size_t pos = 0;
bool modified = false;
while((pos = content.find(target, pos)) != std::string::npos)
{
content.replace(pos, target.length(), replacement);
pos += replacement.length();
modified = true;
}
if(modified)
{
std::ofstream outFile(filePath, std::ios::trunc);
if(!outFile.is_open())
{
std::cerr << "Err: " << filePath << '\n';
return;
}
outFile << content;
outFile.close();
std::cout << "Modified file: " << filePath << '\n';
}
}
void processDirectory(const std::string& dirPath, const std::string& target, const std::string& replacement)
{
try
{
for(const auto& entry : fs::recursive_directory_iterator(dirPath))
{
if(fs::is_regular_file(entry.status()))
{
fs::path p = entry.path();
bool excluded = false;
for(auto const& part : p)
{
if(part == EXCLUDED_DIR)
{
excluded = true;
break;
}
}
if (!excluded)
{
replaceInFile(p, target, replacement);
}
}
}
}
catch(const fs::filesystem_error& e)
{
std::cerr << "Filesystem err: " << e.what() << '\n';
}
}
namespace vermgr
{
namespace util
{
inline uint64_t static_integer_mix(uint64_t x) {
x ^= x >> 30;
x *= 0xbf58476d1ce4e5b9ULL;
x ^= x >> 27;
x *= 0x94d049bb133111ebULL;
return x ^ (x >> 31);
}
inline std::string getHash()
{
auto now = std::chrono::high_resolution_clock::now();
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(
now.time_since_epoch()
).count();
auto tid = std::hash<std::thread::id>{}(std::this_thread::get_id());
int stack_var = 0;
uintptr_t mem_entropy = reinterpret_cast<uintptr_t>(&stack_var);
static std::atomic<uint64_t> counter{14695981039346656037ULL};
uint64_t seq = counter.fetch_add(0x9e3779b97f4a7c15ULL, std::memory_order_relaxed);
uint64_t raw = static_integer_mix(nanos ^ seq ^ (tid * 0x9915059103L) ^ (uintptr_t)&stack_var);
uint64_t z = raw + 0x9e3779b97f4a7c15ULL;
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
uint64_t h = z ^ (z >> 31);
return std::to_string(h);
}
inline std::string removeCharsFromString(std::string s, char c)
{
std::string r = "";
for(size_t i = 0; i < s.size(); ++i)
{
if(s.at(i) != c)
{
r += s.at(i);
}
}
return r;
}
}
}
int main()
{
copyTemplates(EXCLUDED_DIR, ROOT_DIR);
static const std::string searchStr = "!@!NEOFORGE_MOD_VERSION!!";
static std::string v = vermgr::util::getHash();
std::cout << "Version generated: " << v << std::endl;
processDirectory(ROOT_DIR, vermgr::util::removeCharsFromString(searchStr, '@'), v);
std::cout << "Version manager finished." << std::endl;
return 0;
}