-
Notifications
You must be signed in to change notification settings - Fork 0
/
filemasterclass.cpp
131 lines (109 loc) · 4.38 KB
/
filemasterclass.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
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
#include "filemasterclass.h"
#include <filesystem>
#include <fstream>
#include <sys\\stat.h>
// Получить путь исполняемого файла; ожидается корневая папка набора
std::string *fileMasterClass::getCurrentPath() { return ¤tPath; }
// Проверить существование файла в корневой папке
bool fileMasterClass::doesFileExist(std::string &name) {
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}
// Найти первую картинку в корневой папке
std::string fileMasterClass::findFirstPng() {
std::string path = currentPath;
std::string ext = ".png";
for (auto &p : std::filesystem::directory_iterator(path))
if (p.path().extension() == ext && p.path().stem().string() != "pack") {
return p.path().stem().string();
break;
}
return "";
}
// Проверить существование текстуры в /textures/item
bool fileMasterClass::doesTextureFileExist(std::string skinName) {
std::string path = currentPath + "/assets/minecraft/textures/item";
for (auto &p : std::filesystem::directory_iterator(path)) {
if (p.path().stem().string() + ".png" == skinName) {
return true;
break;
}
}
return false;
}
// Проверить существование шаблона
bool fileMasterClass::doesTemplateExist(std::string templateName) {
std::string path = currentPath + "/assets/minecraft/models/item/template";
std::string ext = ".json";
for (auto &p : std::filesystem::directory_iterator(path)) {
if (p.path().extension() == ext &&
p.path().stem().string() == templateName) {
return true;
break;
}
}
return false;
}
// Создание тотема
void fileMasterClass::createNewTotem(std::string skinName) {
std::string firstPng = findFirstPng();
if (doesFileExist(skinName)) {
std::string sourcePath = currentPath + "/" + skinName;
std::string destPath =
currentPath + "/assets/minecraft/textures/item/" + skinName;
std::filesystem::copy(sourcePath, destPath,
std::filesystem::copy_options::overwrite_existing);
} else if (!firstPng.empty()) {
std::string sourcePath = currentPath + "/" + firstPng + ".png";
std::string destPath =
currentPath + "/assets/minecraft/textures/item/" + skinName;
std::filesystem::rename(sourcePath, destPath);
}
}
// Открыть файл в потоке
std::string getFile(std::ifstream &is) {
std::string contents;
for (char ch; is.get(ch); contents.push_back(ch)) {
}
return contents;
}
// Заменить все подстроки в строке
void findReplace(std::string &fileContents, const std::string &replacedString,
const std::string &replacementString) {
auto pos = fileContents.find(replacedString);
while (pos != std::string::npos) {
fileContents.replace(pos, replacedString.length(), replacementString);
pos = fileContents.find(replacedString, pos);
}
}
// Изменить название текстуры в файле тотема после экспорта
void fileMasterClass::changeTemplateTexture(std::string skinName) {
std::string totemInPath =
currentPath + "/assets/minecraft/models/item/temp_template.json";
std::string totemOutPath =
currentPath + "/assets/minecraft/models/item/totem_of_undying.json";
std::ifstream inputFile(totemInPath);
std::ofstream outputFile(totemOutPath);
std::string contents = getFile(inputFile);
findReplace(contents, "template_texture", skinName);
outputFile << contents;
inputFile.close();
outputFile.close();
std::remove(totemInPath.c_str());
}
// Экспорт шаблона
void fileMasterClass::exportTemplate(std::string templateName,
std::string skinName) {
std::string sourcePath = currentPath +
"/assets/minecraft/models/item/template/" +
templateName + ".json";
std::string destPath =
currentPath + "/assets/minecraft/models/item/temp_template.json";
std::filesystem::copy(sourcePath, destPath,
std::filesystem::copy_options::overwrite_existing);
changeTemplateTexture(skinName);
}
fileMasterClass::fileMasterClass() {
std::filesystem::path bufferPath = std::filesystem::current_path();
currentPath = bufferPath.generic_string();
}