-
Notifications
You must be signed in to change notification settings - Fork 0
/
libsonolush.cpp
370 lines (341 loc) · 19.3 KB
/
libsonolush.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#define MKDIR MKDIR_decrypted
#include"../../main.cpp"
#undef MKDIR
#include<bits/stdc++.h>
using namespace std;
void MKDIR(string path) {
filesystem::create_directories(path);
}
bool file_exists(string path) {
return filesystem::exists(path);
}
void copyFolder(string src, string dst, set<string> except, string tmp = "") {
MKDIR(dst);
for (auto& p: filesystem::directory_iterator(src)) {
string path = tmp + "/" + p.path().filename().string();
if (except.find(path) != except.end() && file_exists(dst + "/" + p.path().filename().string())) continue;
if (filesystem::is_directory(p)) copyFolder(p.path().string(), dst + "/" + p.path().filename().string(), except, path);
else
filesystem::copy_file(p.path().string(), dst + "/" + p.path().filename().string(), filesystem::copy_options::overwrite_existing),
cout << "Copied file from \"" << src+ "/" + p.path().filename().string() << "\" to \"" << dst+ "/" + p.path().filename().string() << "\"" << endl;
}
}
void initCustomEngine(char** argv) {
string root_dir = string(argv[2]);
copyFolder("./plugins/libsonolush/source", root_dir, {});
}
void updateCustomEngine(char** argv) {
string root_dir = string(argv[2]);
copyFolder("./plugins/libsonolush/source", root_dir, {"/main.cpp", "/package.json"});
}
string uploadFile(string path) {
ifstream fin(path.c_str());
fin.seekg(0, ios::end);
int len = fin.tellg();
if (len == -1) return "";
fin.seekg(0, ios::beg);
char* filePointerBeg = new char[len];
fin.read(filePointerBeg, len);
unsigned char* fileSha1 = sha1(filePointerBeg, len);
stringstream buffer;
for (int i = 0; i < 20; i++)
buffer << hex << setw(2) << setfill('0') << int(fileSha1[i]);
ofstream fout(("./data/" + buffer.str()).c_str());
fout.write(filePointerBeg, len); fout.close();
free(filePointerBeg); free(fileSha1);
return buffer.str();
}
string getFileSize(string path) {
ifstream fin(path.c_str());
fin.seekg(0, ios::end);
double size = fin.tellg();
stringstream ss;
if (size < 2048) ss << fixed << setprecision(3) << "(" << size << "B)";
else { size /= 1024.0; if (size < 2048) ss << fixed << setprecision(3) << "(" << size << "KB)";
else { size /= 1024.0; if (size < 2048) ss << fixed << setprecision(3) << "(" << size << "MB)";
else { size /= 1024.0; if (size < 2048) ss << fixed << setprecision(3) << "(" << size << "GB)";
else { size /= 1024.0; ss << fixed << setprecision(3) << "(" << size << "TB)"; }}}}
return ss.str();
}
void syncRepository() {
MKDIR("./plugins/libsonolush/source");
cout << "Fetching latest Sonolus.h..." << endl;
string json = get_url("https://api.github.com/repos/SonolusHaniwa/sonolus.h/releases/latest", 5);
Json::Value arr; json_decode(json, arr);
cout << "Latest version: " << arr["tag_name"].asString() << endl;
cout << "Downloading template.zip..." << endl;
string zipball = get_url(arr["zipball_url"].asString(), 5);
string zipPath = "./plugins/libsonolush/source/template.zip";
ofstream fout(zipPath);
fout.write(zipball.c_str(), zipball.size()); fout.close();
cout << "Downloaded template.zip for " << arr["tag_name"].asString() << getFileSize(zipPath) << endl;
vector<string> files = getFileListFromZip(zipPath);
for (int i = 0; i < files.size(); i++) {
if (files[i].back() == '/') continue;
string path = files[i];
path = path.substr(path.find("/") + 1);
if (path.find("/") != string::npos) {
string dir = path.substr(0, path.rfind("/"));
MKDIR("./plugins/libsonolush/source/" + dir);
}
string content = getFileFromZip(zipPath, files[i]);
ofstream fout("./plugins/libsonolush/source/" + path);
fout.write(content.c_str(), content.size());
fout.close();
cout << "Extracted \"" << path << "\" from template.zip" << endl;
}
filesystem::remove(zipPath);
cout << "Sync finished. Current Sonolus.h version: " << arr["tag_name"].asString() << endl;
}
void initBuild(int argc, char** argv) {
string path = argv[3], type = argv[2]; string extendCommand = "";
for (int i = 4; i < argc; i++) extendCommand += " " + string(argv[i]);
stringstream command;
command << "cd \"" << path << "\"";
command << " && echo Compiling " << (type == "particle" ? "particle" : "engine") << " \"" << path << "\"...";
command << " && g++ main.cpp -o main -ljsoncpp -lssl -lcrypto -lz -lpng -lzip ";
if (type == "play") command << "-Dplay";
else if (type == "tutorial") command << "-Dtutorial";
else if (type == "preview") command << "-Dpreview";
else if (type == "watch") command << "-Dwatch";
else if (type == "all") ;
// else if (type == "particle") command << "-Dparticle";
else throw runtime_error("Unknown Compilation Type");
command << " " << extendCommand;
command << " && echo Running scripts...";
command << " && ./main";
if (type == "all") {
command.str("");
command << "cd \"" << path << "\"";
command << " && echo Compiling play mode of engine \"" << path << "\"...";
command << " && g++ main.cpp -o main -ljsoncpp -lssl -lcrypto -lz -lpng -lzip -Dplay && ./main";
command << " && echo Compiling tutorial mode of engine \"" << path << "\"...";
command << " && g++ main.cpp -o main -ljsoncpp -lssl -lcrypto -lz -lpng -lzip -Dtutorial && ./main";
command << " && echo Compiling preview mode of engine \"" << path << "\"...";
command << " && g++ main.cpp -o main -ljsoncpp -lssl -lcrypto -lz -lpng -lzip -Dpreview && ./main";
command << " && echo Compiling watch mode of engine \"" << path << "\"...";
command << " && g++ main.cpp -o main -ljsoncpp -lssl -lcrypto -lz -lpng -lzip -Dwatch && ./main";
}
int res = system(command.str().c_str());
if (res) exit(3);
preload();
string package_json = readFile((path + "/package.json").c_str());
Json::Value arr; json_decode(package_json, arr);
if (arr["level"]["generate"].asString() != "") {
cout << "Generating LevelData..." << endl;
int res = system(("cd \"" + path + "\" && ./main " + arr["level"]["generate"].asString()).c_str());
if (res) exit(3);
}
string engineData = uploadFile((path + "/dist/EngineData").c_str());
string engineDataSize = getFileSize((path + "/dist/EngineData").c_str());
string engineConfiguration = uploadFile((path + "/dist/EngineConfiguration").c_str());
string engineConfigurationSize = getFileSize((path + "/dist/EngineConfiguration").c_str());
string engineTutorialData = uploadFile((path + "/dist/EngineTutorialData").c_str());
string engineTutorialDataSize = getFileSize((path + "/dist/EngineTutorialData").c_str());
string enginePreviewData = uploadFile((path + "/dist/EnginePreviewData").c_str());
string enginePreviewDataSize = getFileSize((path + "/dist/EnginePreviewData").c_str());
string engineWatchData = uploadFile((path + "/dist/EngineWatchData").c_str());
string engineWatchDataSize = getFileSize((path + "/dist/EngineWatchData").c_str());
string engineThumbnail = fileExist((path + "/dist/thumbnail.jpg").c_str()) ?
uploadFile((path + "/dist/thumbnail.jpg").c_str()) : uploadFile((path + "/dist/thumbnail.png").c_str());
string engineThumbnailSize = fileExist((path + "/dist/thumbnail.jpg").c_str()) ?
getFileSize((path + "/dist/thumbnail.jpg").c_str()) : getFileSize((path + "/dist/thumbnail.png").c_str());
string skinThumbnail = engineThumbnail;
string skinThumbnailSize = engineThumbnailSize;
string skinData = uploadFile((path + "/dist/SkinData").c_str());
string skinDataSize = getFileSize((path + "/dist/SkinData").c_str());
string skinTexture = uploadFile((path + "/dist/SkinTexture").c_str());
string skinTextureSize = getFileSize((path + "/dist/SkinTexture").c_str());
string backgroundThumbnail = engineThumbnail;
string backgroundThumbnailSize = engineThumbnailSize;
string backgroundData = uploadFile((path + "/dist/BackgroundData").c_str());
string backgroundDataSize = getFileSize((path + "/dist/BackgroundData").c_str());
string backgroundImage = uploadFile((path + "/dist/BackgroundImage").c_str());
string backgroundImageSize = getFileSize((path + "/dist/BackgroundImage").c_str());
string backgroundConfiguration = uploadFile((path + "/dist/BackgroundConfiguration").c_str());
string backgroundConfigurationSize = getFileSize((path + "/dist/BackgroundConfiguration").c_str());
string effectThumbnail = engineThumbnail;
string effectThumbnailSize = engineThumbnailSize;
string effectData = uploadFile((path + "/dist/EffectData").c_str());
string effectDataSize = getFileSize((path + "/dist/EffectData").c_str());
string effectAudio = uploadFile((path + "/dist/EffectAudio").c_str());
string effectAudioSize = getFileSize((path + "/dist/EffectAudio").c_str());
string particleThumbnail = engineThumbnail;
string particleThumbnailSize = engineThumbnailSize;
string particleData = uploadFile((path + "/dist/ParticleData").c_str());
string particleDataSize = getFileSize((path + "/dist/ParticleData").c_str());
string particleTexture = uploadFile((path + "/dist/ParticleTexture").c_str());
string particleTextureSize = getFileSize((path + "/dist/ParticleTexture").c_str());
string levelCover = engineThumbnail;
string levelCoverSize = engineThumbnailSize;
string levelBgm = uploadFile((path + "/dist/LevelBgm").c_str());
string levelBgmSize = getFileSize((path + "/dist/LevelBgm").c_str());
string levelData = uploadFile((path + "/dist/LevelData").c_str());
string levelDataSize = getFileSize((path + "/dist/LevelData").c_str());
string levelPreview = uploadFile((path + "/dist/LevelPreview").c_str());
string levelPreviewSize = getFileSize((path + "/dist/LevelPreview").c_str());
cout << "===========================================" << endl
<< "Collected Resource Info: " << endl
<< endl
<< "Engine Thumbnail: " << engineThumbnail << engineThumbnailSize << endl
<< "Engine Configuration: " << engineConfiguration << engineConfigurationSize << endl
<< "Engine Play Data: " << engineData << engineDataSize << endl
<< "Engine Tutorial Data: " << engineTutorialData << engineTutorialDataSize << endl
<< "Engine Preview Data: " << enginePreviewData << enginePreviewDataSize << endl
<< "Engine Watch Data: " << engineWatchData << engineWatchDataSize << endl
<< endl
<< "Skin Thumbnail: " << skinThumbnail << skinThumbnailSize << endl
<< "Skin Data: " << skinData << skinDataSize << endl
<< "Skin Texture: " << skinTexture << skinTextureSize << endl
<< endl
<< "Background Thumbnail: " << backgroundThumbnail << backgroundThumbnailSize << endl
<< "Background Data: " << backgroundData << backgroundDataSize << endl
<< "Background Image: " << backgroundImage << backgroundImageSize << endl
<< "Background Configuration: " << backgroundConfiguration << backgroundConfigurationSize << endl
<< endl
<< "Effect Thumbnail: " << effectThumbnail << effectThumbnailSize << endl
<< "Effect Data: " << effectData << effectDataSize << endl
<< "Effect Audio: " << effectAudio << effectAudioSize << endl
<< endl
<< "Particle Thumbnail: " << particleThumbnail << particleThumbnailSize << endl
<< "Particle Data: " << particleData << particleDataSize << endl
<< "Particle Texture: " << particleTexture << particleTextureSize << endl
<< endl
<< "Level Cover: " << levelCover << levelCoverSize << endl
<< "Level Bgm: " << levelBgm << levelBgmSize << endl
<< "Level Data: " << levelData << levelDataSize << endl
<< "Level Preview: " << levelPreview << levelPreviewSize << endl
<< "===========================================" << endl;
if (arr["skin"]["name"].asString() != "") for (int i = 0; i < arr["skin"]["i18n"].size(); i++) {
auto item = arr["skin"]["i18n"][i];
skinsCreate(SkinItem(-1, arr["skin"]["name"].asString(), item["title"].asString(), item["subtitle"].asString(), item["author"].asString(),
SRL<SkinThumbnail>(skinThumbnail, ""), SRL<SkinData>(skinData, ""), SRL<SkinTexture>(skinTexture, ""), {}, item["description"].asString()), item["localization"].asString());
}
if (arr["background"]["name"].asString() != "") for (int i = 0; i < arr["background"]["i18n"].size(); i++) {
auto item = arr["background"]["i18n"][i];
backgroundsCreate(BackgroundItem(-1, arr["background"]["name"].asString(), item["title"].asString(), item["subtitle"].asString(), item["author"].asString(),
SRL<BackgroundThumbnail>(backgroundThumbnail, ""), SRL<BackgroundData>(backgroundData, ""), SRL<BackgroundImage>(backgroundImage, ""), SRL<BackgroundConfiguration>(backgroundConfiguration, ""), {}, item["description"].asString()), item["localization"].asString());
}
if (arr["effect"]["name"].asString() != "") for (int i = 0; i < arr["effect"]["i18n"].size(); i++) {
auto item = arr["effect"]["i18n"][i];
effectsCreate(EffectItem(-1, arr["effect"]["name"].asString(), item["title"].asString(), item["subtitle"].asString(), item["author"].asString(),
SRL<EffectThumbnail>(effectThumbnail, ""), SRL<EffectData>(effectData, ""), SRL<EffectAudio>(effectAudio, ""), {}, item["description"].asString()), item["localization"].asString());
}
if (arr["particle"]["name"].asString() != "") for (int i = 0; i < arr["particle"]["i18n"].size(); i++) {
auto item = arr["particle"]["i18n"][i];
particlesCreate(ParticleItem(-1, arr["particle"]["name"].asString(), item["title"].asString(), item["subtitle"].asString(), item["author"].asString(),
SRL<ParticleThumbnail>(particleThumbnail, ""), SRL<ParticleData>(particleData, ""), SRL<ParticleTexture>(particleTexture, ""), {}, item["description"].asString()), item["localization"].asString());
}
if (arr["engine"]["name"].asString() != "") for (int i = 0; i < arr["engine"]["i18n"].size(); i++) {
auto item = arr["engine"]["i18n"][i];
SkinItem skin; BackgroundItem background; EffectItem effect; ParticleItem particle;
auto tmp = skinsList("name = \"" + item["skin"].asString() + "\"", "");
if (tmp.size() == 0) writeLog(LOG_LEVEL_ERROR, "Failed to find skin \"" + item["skin"].asString() + "\""), exit(0);
skin = tmp[0];
auto tmp2 = backgroundsList("name = \"" + item["background"].asString() + "\"", "");
if (tmp2.size() == 0) writeLog(LOG_LEVEL_ERROR, "Failed to find background \"" + item["background"].asString() + "\""), exit(0);
background = tmp2[0];
auto tmp3 = effectsList("name = \"" + item["effect"].asString() + "\"", "");
if (tmp3.size() == 0) writeLog(LOG_LEVEL_ERROR, "Failed to find effect \"" + item["effect"].asString() + "\""), exit(0);
effect = tmp3[0];
auto tmp4 = particlesList("name = \"" + item["particle"].asString() + "\"", "");
if (tmp4.size() == 0) writeLog(LOG_LEVEL_ERROR, "Failed to find particle \"" + item["particle"].asString() + "\""), exit(0);
particle = tmp4[0];
enginesCreate(EngineItem(-1, arr["engine"]["name"].asString(), item["title"].asString(), item["subtitle"].asString(), item["author"].asString(),
skin, background, effect, particle, SRL<EngineThumbnail>(engineThumbnail, ""), SRL<EngineData>(engineData, ""), SRL<EngineTutorialData>(engineTutorialData, ""), SRL<EnginePreviewData>(enginePreviewData, ""), SRL<EngineWatchData>(engineWatchData, ""),
SRL<EngineConfiguration>(engineConfiguration, ""), {}, SRL<EngineRom>("", ""), item["description"].asString()), item["localization"].asString());
}
if (arr["level"]["name"].asString() != "" && arr["level"]["generate"].asString() != "") for (int i = 0; i < arr["level"]["i18n"].size(); i++) {
auto item = arr["level"]["i18n"][i];
EngineItem engine; UseItem<SkinItem> skin; UseItem<BackgroundItem> background; UseItem<EffectItem> effect; UseItem<ParticleItem> particle;
auto tmp = enginesList("name = \"" + item["engine"].asString() + "\"", "");
if (tmp.size() == 0) writeLog(LOG_LEVEL_ERROR, "Failed to find engine \"" + item["engine"].asString() + "\""), exit(0);
engine = tmp[0];
if (item["skin"].asString() == "") skin.useDefault = true;
else {
auto tmp2 = skinsList("name = \"" + item["skin"].asString() + "\"", "");
if (tmp2.size() == 0) writeLog(LOG_LEVEL_ERROR, "Failed to find skin \"" + item["skin"].asString() + "\""), exit(0);
skin.item = tmp2[0]; skin.useDefault = false;
}
if (item["background"].asString() == "") background.useDefault = true;
else {
auto tmp3 = backgroundsList("name = \"" + item["background"].asString() + "\"", "");
if (tmp3.size() == 0) writeLog(LOG_LEVEL_ERROR, "Failed to find background \"" + item["background"].asString() + "\""), exit(0);
background.item = tmp3[0]; background.useDefault = false;
}
if (item["effect"].asString() == "") effect.useDefault = true;
else {
auto tmp4 = effectsList("name = \"" + item["effect"].asString() + "\"", "");
if (tmp4.size() == 0) writeLog(LOG_LEVEL_ERROR, "Failed to find effect \"" + item["effect"].asString() + "\""), exit(0);
effect.item = tmp4[0]; effect.useDefault = false;
}
if (item["particle"].asString() == "") particle.useDefault = true;
else {
auto tmp5 = particlesList("name = \"" + item["particle"].asString() + "\"", "");
if (tmp5.size() == 0) writeLog(LOG_LEVEL_ERROR, "Failed to find particle \"" + item["particle"].asString() + "\""), exit(0);
particle.item = tmp5[0]; particle.useDefault = false;
}
levelsCreate(LevelItem(-1, arr["level"]["name"].asString(), item["rating"].asInt(), item["title"].asString(), item["artists"].asString(), item["author"].asString(), engine, skin, background, effect, particle,
SRL<LevelCover>(levelCover, ""), SRL<LevelBgm>(levelBgm, ""), SRL<LevelData>(levelData, ""), SRL<LevelPreview>(levelPreview, ""), {}, item["description"].asString()), item["localization"].asString());
}
}
class PluginSonolush: public SonolusServerPlugin {
public:
string onPluginName() const {
return "Sonolus.h Plugin";
}
string onPluginDescription() const {
return "C++ Based Developer Toolkit for Sonolus";
}
string onPluginVersion() const {
return "1.0.0";
}
string onPluginPlatformVersion() const {
return sonolus_server_version;
}
string onPluginAuthor() const {
return "LittleYang0531";
}
string onPluginLicense() const {
return "MIT";
}
string onPluginWebsite() const {
return "https://github.com/SonolusHaniwa/sonolus.h";
}
vector<string> onPluginHelp(char** argv) const {
return {
"Sonolus.h sync: " + string(argv[0]) + " synccpp",
"Sonolus.h update: " + string(argv[0]) + " updatecpp [name]",
"Sonolus.h init: " + string(argv[0]) + " initcpp [name]",
"Sonolus.h build: " + string(argv[0]) + " buildcpp <play/tutorial/preview/watch/all> [name] [args]"
};
}
void onPluginRunner(int argc, char** argv) const {
if (argc < 2) return;
if (string(argv[1]) == "synccpp") {
syncRepository();
exit(0);
} else if (string(argv[1]) == "updatecpp") {
if (argc < 3) return;
updateCustomEngine(argv);
exit(0);
} else if (string(argv[1]) == "initcpp") {
if (argc < 3) return;
initCustomEngine(argv);
exit(0);
} else if (string(argv[1]) == "buildcpp") {
if (argc < 4) return;
initBuild(argc, argv);
serverRunner(argc, argv);
exit(0);
} return;
}
void onPluginRouter(int argc, char** argv, application *app) const {
}
};
PLUMA_INHERIT_PROVIDER(PluginSonolush, SonolusServerPlugin);
PLUMA_CONNECTOR
bool pluma_connect(pluma::Host& host) {
host.add( new PluginSonolushProvider() );
return true;
}