Skip to content

Commit 471f982

Browse files
Add GUIDetails
1 parent 82cfddf commit 471f982

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+591
-747
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ plugins
2828
*.srp
2929
releases
3030
.tmp
31+
api/libsonolus.*
32+
api/package*
33+
api/node_modules

config/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"mysql.username": "root",
66
"mysql.password": "root",
77
"mysql.database": "sonolus",
8-
"sqlite.dbfile": "sonolus.db",
8+
"sqlite.dbfile": ":memory:",
99
"sqlite.sqlfile": "sonolus.sql",
1010
"server.listenHost": "0.0.0.0",
1111
"server.listenPort": 8080,

config/sonolus_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"server.logo": "9d364cdf72de5709f16eae90e1be7f023946f007",
77
"server.banner": "0273968035abcd9bfe8f6b8fa913bb356dd202f2",
88
"server.rootUrl": "127.0.0.1:8080",
9-
"server.data.prefix": "",
9+
"server.data.prefix": "https://stellarity.littleyang.me/data/",
1010
"language.default": "zhs"
1111
}

emain.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ vector<string> playlistVersionList = {"0.0.0"};
4444
#include"modules/mysqli.h"
4545
#include"modules/json.h"
4646
#include"modules/encrypt.h"
47+
DB_Controller db;
4748
#include"items/Items.h"
4849
#include"web/import.h"
4950

@@ -73,6 +74,7 @@ void routerRegister() {
7374
app.addRoute("/%s/list", GUIList);
7475
app.addRoute("/%s/search", GUISearch);
7576
app.addRoute("/%s/jump/%d", GUIJump);
77+
app.addRoute("/%s/%s", GUIDetails);
7678
// app.addRoute("/levels/create", web_levels_create);
7779
// app.addRoute("/skins/create", web_skins_create);
7880
// app.addRoute("/backgrounds/create", web_backgrounds_create);
@@ -114,7 +116,7 @@ void routerRegister() {
114116
}
115117

116118
string cgiRunner(string request) {
117-
(new DB_Controller)->query("SELECT COUNT(*) FROM Level");
119+
db.query("SELECT COUNT(*) FROM Level");
118120

119121
// 适配 Resource Version
120122
levelVersion = upper_bound(levelVersionList.begin(), levelVersionList.end(), Sonolus_Version) - levelVersionList.begin();
@@ -148,6 +150,7 @@ void preload() {
148150
appConfig["server.bannerHash"] = appConfig["server.banner"].asString();
149151
appConfig["server.bannerUrl"] = dataPrefix + appConfig["server.banner"].asString();
150152
log_init(log_target_type);
153+
db = DB_Controller(true);
151154
http_init();
152155
loadConfig();
153156

@@ -224,4 +227,4 @@ extern "C" EMSCRIPTEN_KEEPALIVE void cgi(char* requestId, int len) {
224227
ofstream fout("./response_" + data);
225228
fout.write(res.c_str(), res.size());
226229
fout.close();
227-
}
230+
}

emake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
sqlite3 sonolus.db .dump > sonolus.sql
12
emcc emain.cpp -o api/libsonolus.js -s MODULARIZE -sALLOW_MEMORY_GROWTH\
23
-sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE=['$stringToNewUTF8','$UTF8ToString']\
34
-sEXPORTED_RUNTIME_METHODS=['stringToNewUTF8','UTF8ToString','FS']\
4-
-fwasm-exceptions -sASSERTIONS -lidbfs.js\
5+
-fwasm-exceptions -sASSERTIONS -lnodefs.js\
56
--preload-file config --preload-file i18n --preload-file web --preload-file sonolus.sql\
67
-L./emsdk/lib -I./emsdk/include -ljsoncpp -lcrypto -lsqlite3 -g

items/BackgroundItem.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class BackgroundItem {
8989
int backgroundsNumber(string filter) {
9090
string sql = "SELECT COUNT(*) AS sum FROM Background";
9191
if (filter != "") sql += " WHERE (" + filter + ")";
92-
dbres res = (new DB_Controller)->query(sql.c_str());
92+
dbres res = db.query(sql.c_str());
9393
return atoi(res[0]["sum"].c_str());
9494
}
9595

@@ -98,7 +98,7 @@ vector<BackgroundItem> backgroundsList(string filter, string order, int st = 1,
9898
if (filter != "") sql += " WHERE (" + filter + ")";
9999
if (order != "") sql += " ORDER BY " + order;
100100
sql += " LIMIT " + to_string(st - 1) + ", " + to_string(en - st + 1);
101-
auto res = (new DB_Controller)->query(sql.c_str());
101+
auto res = db.query(sql.c_str());
102102
vector<BackgroundItem> list = {};
103103
sort(res.begin(), res.end(), [](argvar a, argvar b){
104104
if (a["name"] == b["name"]) return (a["localization"] == "default") < (b["localization"] == "default");
@@ -126,21 +126,21 @@ vector<BackgroundItem> backgroundsList(string filter, string order, int st = 1,
126126

127127
int backgroundCreate(BackgroundItem item, string localization = "default") {
128128
stringstream sqlbuffer;
129-
auto res = (new DB_Controller)->query("SELECT id FROM Background WHERE name = \"" + item.name + "\" AND localization = \"" + localization + "\"");
129+
auto res = db.query("SELECT id FROM Background WHERE name = \"" + item.name + "\" AND localization = \"" + localization + "\"");
130130
if (res.size() != 0) {
131131
int id = atoi(res[0]["id"].c_str());
132132
sqlbuffer << "UPDATE Background SET name = \"" << item.name << "\", version = " << item.version << ", title = \"" << item.title << "\", ";
133133
sqlbuffer << "subtitle = \"" << item.subtitle << "\", author = \"" << item.author << "\", thumbnail = \"" << item.thumbnail.hash << "\", ";
134134
sqlbuffer << "data = \"" << item.data.hash << "\", image = \"" << item.image.hash << "\", configuration = \"" << item.configuration.hash << "\", ";
135135
sqlbuffer << "description = \"" << str_replace("\n", "\\n", item.description) << "\", localization = \"" << localization << "\" WHERE id = " << id << ";";
136136
} else {
137-
int id = atoi((new DB_Controller)->query("SELECT COUNT(*) AS sum FROM Background;")[0]["sum"].c_str()) + 1;
137+
int id = atoi(db.query("SELECT COUNT(*) AS sum FROM Background;")[0]["sum"].c_str()) + 1;
138138
sqlbuffer << "INSERT INTO Background (id, name, version, title, subtitle, author, thumbnail, data, image, configuration, description, localization) VALUES (";
139139
sqlbuffer << id << ", \"" << item.name << "\", " << item.version << ", \"" << item.title << "\", ";
140140
sqlbuffer << "\"" << item.subtitle << "\", \"" << item.author << "\", \"" << item.thumbnail.hash << "\", ";
141141
sqlbuffer << "\"" << item.data.hash << "\", \"" << item.image.hash << "\", \"" << item.configuration.hash << "\", ";
142142
sqlbuffer << "\"" << str_replace("\n", "\\n", item.description) << "\", \"" << localization << "\");";
143-
} return (new DB_Controller)->execute(sqlbuffer.str());
143+
} return db.execute(sqlbuffer.str());
144144
}
145145

146146
#endif

items/EffectItem.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class EffectItem {
8585
int effectsNumber(string filter) {
8686
string sql = "SELECT COUNT(*) AS sum FROM Effect";
8787
if (filter != "") sql += " WHERE (" + filter + ")";
88-
dbres res = (new DB_Controller)->query(sql.c_str());
88+
dbres res = db.query(sql.c_str());
8989
return atoi(res[0]["sum"].c_str());
9090
}
9191

@@ -94,7 +94,7 @@ vector<EffectItem> effectsList(string filter, string order, int st = 1, int en =
9494
if (filter != "") sql += " WHERE (" + filter + ")";
9595
if (order != "") sql += " ORDER BY " + order;
9696
sql += " LIMIT " + to_string(st - 1) + ", " + to_string(en - st + 1);
97-
dbres res = (new DB_Controller)->query(sql.c_str());
97+
dbres res = db.query(sql.c_str());
9898
vector<EffectItem> list = {};
9999
sort(res.begin(), res.end(), [](argvar a, argvar b){
100100
if (a["name"] == b["name"]) return (a["localization"] == "default") < (b["localization"] == "default");
@@ -114,14 +114,14 @@ vector<EffectItem> effectsList(string filter, string order, int st = 1, int en =
114114
SRL<EffectAudio>(res[i]["audio"], dataPrefix + res[i]["audio"]),
115115
deserializeTagString(res[i]["tags"]),
116116
str_replace("\\n", "\n", res[i]["description"]),
117-
(appConfig["server.enableSSL"].asBool() ? "https://" : "http://") + appConfig["server.rootUrl"].asString() + "/sonolus/backgrounds/" + res[i]["name"]
117+
(appConfig["server.enableSSL"].asBool() ? "https://" : "http://") + appConfig["server.rootUrl"].asString() + "/sonolus/effects/" + res[i]["name"]
118118
); list.push_back(data);
119119
} return list;
120120
}
121121

122122
int effectCreate(EffectItem item, string localization = "default") {
123123
stringstream sqlbuffer;
124-
auto res = (new DB_Controller)->query("SELECT id FROM Effect WHERE name = \"" + item.name + "\" AND localization = \"" + localization + "\"");
124+
auto res = db.query("SELECT id FROM Effect WHERE name = \"" + item.name + "\" AND localization = \"" + localization + "\"");
125125
if (res.size() != 0) {
126126
int id = atoi(res[0]["id"].c_str());
127127
sqlbuffer << "UPDATE Effect SET name = \"" << item.name << "\", version = " << item.version << ", ";
@@ -130,12 +130,12 @@ int effectCreate(EffectItem item, string localization = "default") {
130130
sqlbuffer << "data = \"" << item.data.hash << "\", audio = \"" << item.audio.hash << "\", ";
131131
sqlbuffer << "description = \"" << str_replace("\n", "\\n", item.description) << "\", localization = \"" << localization << "\" WHERE id = " << id;
132132
} else {
133-
int id = atoi((new DB_Controller)->query("SELECT COUNT(*) AS sum FROM Effect;")[0]["sum"].c_str()) + 1;
133+
int id = atoi(db.query("SELECT COUNT(*) AS sum FROM Effect;")[0]["sum"].c_str()) + 1;
134134
sqlbuffer << "INSERT INTO Effect (id, name, version, title, subtitle, author, thumbnail, data, audio, description, localization) VALUES (";
135135
sqlbuffer << id << ", \"" << item.name << "\", " << item.version << ", \"" << item.title << "\", ";
136136
sqlbuffer << "\"" << item.subtitle << "\", \"" << item.author << "\", \"" << item.thumbnail.hash << "\", ";
137137
sqlbuffer << "\"" << item.data.hash << "\", \"" << item.audio.hash << "\", \"" << str_replace("\n", "\\n", item.description) << "\", \"" << localization << "\")";
138-
} return (new DB_Controller)->execute(sqlbuffer.str());
138+
} return db.execute(sqlbuffer.str());
139139
}
140140

141141
#endif

items/EngineItem.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class EngineItem {
125125
int enginesNumber(string filter) {
126126
string sql = "SELECT COUNT(*) AS sum FROM Engine";
127127
if (filter != "") sql += " WHERE (" + filter + ")";
128-
dbres res = (new DB_Controller)->query(sql.c_str());
128+
dbres res = db.query(sql.c_str());
129129
return atoi(res[0]["sum"].c_str());
130130
}
131131

@@ -134,7 +134,7 @@ vector<EngineItem> enginesList(string filter, string order, int st = 1, int en =
134134
if (filter != "") sql += " WHERE (" + filter + ")";
135135
if (order != "") sql += " ORDER BY " + order;
136136
sql += " LIMIT " + to_string(st - 1) + ", " + to_string(en - st + 1);
137-
dbres res = (new DB_Controller)->query(sql.c_str());
137+
dbres res = db.query(sql.c_str());
138138
vector<EngineItem> list = {};
139139
sort(res.begin(), res.end(), [](argvar a, argvar b){
140140
if (a["name"] == b["name"]) return (a["localization"] == "default") < (b["localization"] == "default");
@@ -163,32 +163,32 @@ vector<EngineItem> enginesList(string filter, string order, int st = 1, int en =
163163
deserializeTagString(res[i]["tags"]),
164164
SRL<EngineRom>(res[i]["rom"], dataPrefix + res[i]["rom"]),
165165
str_replace("\\n", "\n", res[i]["description"]),
166-
(appConfig["server.enableSSL"].asBool() ? "https://" : "http://") + appConfig["server.rootUrl"].asString() + "/sonolus/backgrounds/" + res[i]["name"]
166+
(appConfig["server.enableSSL"].asBool() ? "https://" : "http://") + appConfig["server.rootUrl"].asString() + "/sonolus/engines/" + res[i]["name"]
167167
); list.push_back(data);
168168
} return list;
169169
}
170170

171171
int engineCreate(EngineItem item, string localization = "default") {
172172
stringstream sqlbuffer;
173-
auto res = (new DB_Controller)->query("SELECT id FROM Engine WHERE name = \"" + item.name + "\" AND localization = \"" + localization + "\"");
174-
int skinId = atoi((new DB_Controller)->query("SELECT id FROM Skin WHERE name = \"" + item.skin.name + "\";")[0]["id"].c_str());
175-
int backgroundId = atoi((new DB_Controller)->query("SELECT id FROM Background WHERE name = \"" + item.background.name + "\";")[0]["id"].c_str());
176-
int effectId = atoi((new DB_Controller)->query("SELECT id FROM Effect WHERE name = \"" + item.effect.name + "\";")[0]["id"].c_str());
177-
int particleId = atoi((new DB_Controller)->query("SELECT id FROM Particle WHERE name = \"" + item.particle.name + "\";")[0]["id"].c_str());
173+
auto res = db.query("SELECT id FROM Engine WHERE name = \"" + item.name + "\" AND localization = \"" + localization + "\"");
174+
int skinId = atoi(db.query("SELECT id FROM Skin WHERE name = \"" + item.skin.name + "\";")[0]["id"].c_str());
175+
int backgroundId = atoi(db.query("SELECT id FROM Background WHERE name = \"" + item.background.name + "\";")[0]["id"].c_str());
176+
int effectId = atoi(db.query("SELECT id FROM Effect WHERE name = \"" + item.effect.name + "\";")[0]["id"].c_str());
177+
int particleId = atoi(db.query("SELECT id FROM Particle WHERE name = \"" + item.particle.name + "\";")[0]["id"].c_str());
178178
if (res.size() != 0) {
179179
int id = atoi(res[0]["id"].c_str());
180180
sqlbuffer << "UPDATE Engine SET name = \"" << item.name << "\", version = " << item.version << ", title = \"" << item.title << "\", ";
181181
sqlbuffer << "subtitle = \"" << item.subtitle << "\", author = \"" << item.author << "\", skin = " << skinId << ", background = " << backgroundId << ", ";
182182
sqlbuffer << "effect = " << effectId << ", particle = " << particleId << ", thumbnail = \"" << item.thumbnail.hash << "\", data = \"" << item.data.hash << "\", tutorialData = \"" << item.tutorialData.hash << "\", previewData = \"" << item.previewData.hash << "\", watchData = \"" << item.watchData.hash << "\", ";
183183
sqlbuffer << "configuration = \"" << item.configuration.hash << "\", rom = \"" << item.rom.hash << "\", description = \"" << str_replace("\n", "\\n", item.description) << "\", localization = \"" << localization << "\" WHERE id = " << id << ";";
184184
} else {
185-
int id = atoi((new DB_Controller)->query("SELECT COUNT(*) AS sum FROM Engine;")[0]["sum"].c_str()) + 1;
185+
int id = atoi(db.query("SELECT COUNT(*) AS sum FROM Engine;")[0]["sum"].c_str()) + 1;
186186
sqlbuffer << "INSERT INTO Engine (id, name, version, title, subtitle, author, skin, background, effect, particle, thumbnail, data, configuration, rom, description, localization, tutorialData, previewData, watchData) VALUES (";
187187
sqlbuffer << id << ", \"" << item.name << "\", " << item.version << ", \"" << item.title << "\", ";
188188
sqlbuffer << "\"" << item.subtitle << "\", \"" << item.author << "\", " << skinId << ", " << backgroundId << ", " << effectId << ", " << particleId << ", ";
189189
sqlbuffer << "\"" << item.thumbnail.hash << "\", \"" << item.data.hash << "\", \"" << item.configuration.hash << "\", ";
190190
sqlbuffer << "\"" << item.rom.hash << "\", \"" << str_replace("\n", "\\n", item.description) << "\", \"" << localization << "\", \"" << item.tutorialData.hash << "\", \"" << item.previewData.hash << "\", \"" << item.watchData.hash << "\");";
191-
} return (new DB_Controller)->execute(sqlbuffer.str());
191+
} return db.execute(sqlbuffer.str());
192192
}
193193

194194
#endif

items/Items.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,6 @@ void loadConfig() {
155155
bool checkLogin(http_request request) {
156156
string session = request.argv["sonolus-session"];
157157
if (session == "") session = cookieParam(request)["Sonolus-Session"];
158-
auto res = (new DB_Controller)->query("SELECT * FROM UserSession WHERE session=\"" + session + "\" AND time>=" + to_string(time(NULL) - appConfig["session.expireTime"].asInt64() * 24 * 60 * 60) + " AND userId!=0");
158+
auto res = db.query("SELECT * FROM UserSession WHERE session=\"" + session + "\" AND time>=" + to_string(time(NULL) - appConfig["session.expireTime"].asInt64() * 24 * 60 * 60) + " AND userId!=0");
159159
return res.size();
160160
}

0 commit comments

Comments
 (0)