Skip to content

Commit

Permalink
refactor: charset utf8
Browse files Browse the repository at this point in the history
角色卡模板统一按utf-8编码读取
同步webui接口交互中的编码
  • Loading branch information
mystringEmpty committed Nov 29, 2023
1 parent 50a4cb4 commit 74c6e3a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 54 deletions.
29 changes: 12 additions & 17 deletions Dice/CharacterCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@
#include "DiceJS.h"
#include "DiceMod.h"

#define Text2UTF8(s) (s ? (isUTF8 ? s : GBKtoUTF8(s)) : "")
AttrShape::AttrShape(const tinyxml2::XMLElement* node, bool isUTF8) {
//#define TextOrEmpty(s) (s ? s : "")
AttrShape::AttrShape(const tinyxml2::XMLElement* node) {
if (auto exp{ node->GetText() }) {
string s{ Text2UTF8(exp) };
if (auto text{ node->Attribute("text") }) {
if (auto lower{ toLower(text) }; lower == "dicexp") {
textType = AttrShape::TextType::Dicexp;
defVal = s;
defVal = exp;
return;
}
else if (lower == "javascript") {
textType = AttrShape::TextType::JavaScript;
defVal = s;
defVal = exp;
return;
}
}
defVal = AttrVar::parse(s);
defVal = AttrVar::parse(exp);
}
}
AttrVar AttrShape::init(const CharaCard* pc) {
Expand Down Expand Up @@ -106,43 +105,39 @@ dict_ci<ptr<CardTemp>> CardModels{
{"COC7", std::make_shared<CardTemp>(ModelCOC7),},
};

CardPreset::CardPreset(const tinyxml2::XMLElement* d, bool isUTF8) {
CardPreset::CardPreset(const tinyxml2::XMLElement* d) {
for (auto elem = d->FirstChildElement(); elem; elem = elem->NextSiblingElement()) {
if (auto name{ elem->Attribute("name") }) {
shapes[Text2UTF8(name)] = { elem,isUTF8 };
shapes[name] = elem;
}
}
}
int loadCardTemp(const std::filesystem::path& fpPath, dict_ci<CardTemp>& m) {
tinyxml2::XMLDocument doc;
if (auto err{ doc.LoadFile(fpPath.string().c_str()) }; tinyxml2::XML_SUCCESS == err) {
bool isUTF8(false);
if (auto declar{ doc.FirstChild()->ToDeclaration() }) {
isUTF8 = string(declar->Value()).find("UTF-8") != string::npos;
}
if (auto root{ doc.FirstChildElement() }) {
if (auto tp_name{ root->Attribute("name") }) {
auto& tp{ m[Text2UTF8(tp_name)] };
auto& tp{ m[tp_name] };
for (auto elem = root->FirstChildElement(); elem; elem = elem->NextSiblingElement()) {
if (string tag{ elem->Name()}; tag == "alias") {
if (auto text{ elem->GetText() })readini(Text2UTF8(text), tp.replaceName);
if (auto text{ elem->GetText() })readini(text, tp.replaceName);
}
else if (tag == "basic") {
tp.vBasicList.clear();
for (auto kid = elem->FirstChildElement(); kid; kid = kid->NextSiblingElement()) {
if (auto text{ kid->GetText() })tp.vBasicList.push_back(getLines(Text2UTF8(text)));
if (auto text{ kid->GetText() })tp.vBasicList.push_back(getLines(text));
}
}
else if (tag == "init") {
for (auto kid = elem->FirstChildElement(); kid; kid = kid->NextSiblingElement()) {
if (auto name{ kid->Attribute("name") }) {
tp.AttrShapes[Text2UTF8(name)] = { kid,isUTF8 };
tp.AttrShapes[name] = kid;
}
}
}
else if (tag == "presets") {
for (auto kid = elem->FirstChildElement(); kid; kid = kid->NextSiblingElement()) {
if (auto opt{ kid->Attribute("name") })tp.presets[Text2UTF8(opt)] = CardPreset(kid, isUTF8);
if (auto opt{ kid->Attribute("name") })tp.presets[opt] = CardPreset(kid);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Dice/CharacterCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AttrShape {
enum class DataType : unsigned char { Any, Nature, Int, };
enum class TextType : unsigned char { Plain, Dicexp, JavaScript, };
AttrShape() = default;
AttrShape(const tinyxml2::XMLElement* node, bool isUTF8);
AttrShape(const tinyxml2::XMLElement* node);
AttrShape(int i) :defVal(i) {}
AttrShape(const string& s):defVal(s){}
AttrShape(const string& s, TextType tt) :defVal(s), textType(tt){}
Expand All @@ -69,7 +69,7 @@ class CardPreset {
shapes[key] = { val, AttrShape::TextType::Dicexp };
}
}
CardPreset(const tinyxml2::XMLElement* d, bool isUTF8);
CardPreset(const tinyxml2::XMLElement* d);
};

class CardTemp
Expand Down
71 changes: 36 additions & 35 deletions Dice/DiceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class BasicInfoApiHandler : public CivetHandler
j["code"] = 0;
j["msg"] = "ok";
j["data"] = nlohmann::json::object();
j["data"]["version"] = GBKtoUTF8(Dice_Full_Ver);
j["data"]["version"] = Dice_Full_Ver;
j["data"]["qq"] = console.DiceMaid;
j["data"]["nick"] = GBKtoUTF8(getMsg("strSelfName"));
j["data"]["running_time"] = GBKtoUTF8(printDuringTime(time(nullptr) - llStartTime));
j["data"]["nick"] = getMsg("strSelfName");
j["data"]["running_time"] = printDuringTime(time(nullptr) - llStartTime);
j["data"]["cmd_count"] = std::to_string(FrqMonitor::sumFrqTotal.load());
j["data"]["cmd_count_today"] = today->get("frq").to_str();
ret = j.dump();
Expand All @@ -56,7 +56,7 @@ class BasicInfoApiHandler : public CivetHandler
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}

Expand All @@ -82,15 +82,15 @@ class CustomMsgApiHandler : public CivetHandler
j["data"] = nlohmann::json::array();
for (const auto& [key,val] : GlobalMsg)
{
j["data"].push_back({{"name", GBKtoUTF8(key)}, {"value", GBKtoUTF8(val)}, {"remark", GBKtoUTF8(getComment(key))}});
j["data"].push_back({{"name", key}, {"value", val}, {"remark", getComment(key)}});
}
ret = j.dump();
}
catch(const std::exception& e)
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}

Expand All @@ -110,13 +110,13 @@ class CustomMsgApiHandler : public CivetHandler
{
std::unique_lock lock(GlobalMsgMutex);
for(const auto& item: j["data"]){
GlobalMsg[UTF8toGBK(item["name"].get<std::string>())] = UTF8toGBK(item["value"].get<std::string>());
EditedMsg[UTF8toGBK(item["name"].get<std::string>())] = UTF8toGBK(item["value"].get<std::string>());
GlobalMsg[item["name"]] = item["value"];
EditedMsg[item["name"]] = item["value"];
}
saveJMap(DiceDir / "conf" / "CustomMsg.json", EditedMsg);
}
else if (j["action"].get<std::string>() == "reset") {
fmt->msg_reset(UTF8toGBK(j["data"]["name"].get<std::string>()));
fmt->msg_reset(j["data"]["name"]);
}
else
{
Expand All @@ -131,7 +131,7 @@ class CustomMsgApiHandler : public CivetHandler
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}
mg_send_http_ok(conn, "application/json", ret.length());
Expand Down Expand Up @@ -162,7 +162,7 @@ class CustomReplyApiHandler : public CivetHandler
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}

Expand All @@ -183,7 +183,7 @@ class CustomReplyApiHandler : public CivetHandler
for(const auto& item: j["data"])
{
ptr<DiceMsgReply> trigger{ std::make_shared<DiceMsgReply>() };
trigger->title = UTF8toGBK(item["name"].get<std::string>());
trigger->title = item["name"];
trigger->readJson(item);
fmt->set_reply(trigger->title, trigger);
}
Expand All @@ -192,7 +192,7 @@ class CustomReplyApiHandler : public CivetHandler
{
for(const auto& item: j["data"])
{
fmt->del_reply(UTF8toGBK(item["name"].get<std::string>()));
fmt->del_reply(item["name"]);
}
}
else
Expand All @@ -208,7 +208,7 @@ class CustomReplyApiHandler : public CivetHandler
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}
mg_send_http_ok(conn, "application/json", ret.length());
Expand All @@ -230,11 +230,11 @@ class ModListApiHandler : public CivetHandler
j["count"] = fmt->modOrder.size();
j["data"] = nlohmann::json::array();
for (const auto& mod : fmt->modOrder) {
j["data"].push_back({ {"name", GBKtoUTF8(mod->name)} ,
{"title", GBKtoUTF8(mod->title.empty() ? mod->name : mod->title)},
{"ver", GBKtoUTF8(mod->ver.exp)},
{"author", GBKtoUTF8(mod->author)},
{"brief", GBKtoUTF8(mod->brief)},
j["data"].push_back({ {"name", mod->name} ,
{"title", mod->title.empty() ? mod->name : mod->title},
{"ver", mod->ver.exp},
{"author", mod->author},
{"brief", mod->brief},
{"active", mod->active},
});
}
Expand All @@ -244,7 +244,7 @@ class ModListApiHandler : public CivetHandler
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}

Expand All @@ -264,15 +264,15 @@ class ModListApiHandler : public CivetHandler
}
else if(j["action"] == "delete") {
for (const auto& item : j["data"]) {
fmt->uninstall(UTF8toGBK(item["name"].get<std::string>()));
fmt->uninstall(item["name"]);
}
}
else if (j["action"] == "reorder") {
size_t oldIdx{ j["data"]["oldIndex"] }, newIndex{ j["data"]["newIndex"]};
if(!fmt->reorder(oldIdx,newIndex))throw std::runtime_error("Invalid Index");
}
else if (j["action"] == "install") {
string name{ UTF8toGBK(j["data"]["name"].get<std::string>()) };
string name{ j["data"]["name"] };
#ifndef __ANDROID__
if (j["data"].count("repo") && fmt->mod_clone(name, j["data"]["repo"]))goto Success;
#endif
Expand All @@ -293,7 +293,7 @@ class ModListApiHandler : public CivetHandler
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}
mg_send_http_ok(conn, "application/json", ret.length());
Expand All @@ -318,9 +318,9 @@ class AdminConfigHandler : public CivetHandler
{
const int value = console[item.first.c_str()];
j["data"].push_back({
{"name", GBKtoUTF8(item.first)},
{"name", item.first},
{"value", value},
{"remark", GBKtoUTF8(console.confComment.find(item.first)->second)},
{"remark", console.confComment.find(item.first)->second},
});
}
j["count"] = j["data"].size();
Expand All @@ -330,7 +330,7 @@ class AdminConfigHandler : public CivetHandler
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}
mg_send_http_ok(conn, "application/json", ret.length());
Expand All @@ -349,7 +349,7 @@ class AdminConfigHandler : public CivetHandler
{
for(const auto& item: j["data"])
{
console.set(UTF8toGBK(item["name"].get<std::string>()), item["value"].get<int>());
console.set(item["name"], item["value"].get<int>());
}
}
else
Expand All @@ -365,7 +365,7 @@ class AdminConfigHandler : public CivetHandler
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}
mg_send_http_ok(conn, "application/json", ret.length());
Expand Down Expand Up @@ -394,7 +394,7 @@ class MasterHandler : public CivetHandler
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}
mg_send_http_ok(conn, "application/json", ret.length());
Expand Down Expand Up @@ -435,7 +435,7 @@ class MasterHandler : public CivetHandler
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}
mg_send_http_ok(conn, "application/json", ret.length());
Expand All @@ -455,10 +455,11 @@ class UrlApiHandler : public CivetHandler {
if (Network::GET(url, ret)) {
j["code"] = 0;
j["msg"] = ret;
console.log("getUrl:" + ret,0);
}
else {
j["code"] = -1;
j["msg"] = GBKtoUTF8(ret);
j["msg"] = LocaltoUTF8(ret);
console.log(ret, 0);
}
}
Expand All @@ -472,7 +473,7 @@ class UrlApiHandler : public CivetHandler {
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}

Expand All @@ -490,15 +491,15 @@ class UrlApiHandler : public CivetHandler {
if (!Network::POST(j["url"], j["content"], j.count("header") ? j["header"] : "Content-Type: application/json", ret)) {
nlohmann::json j2 = nlohmann::json::object();
j2["code"] = -1;
j2["msg"] = GBKtoUTF8(ret);
j2["msg"] = ret;
ret = j2.dump();
}
}
catch (const std::exception& e)
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}
mg_send_http_ok(conn, "application/json", ret.length());
Expand Down Expand Up @@ -538,7 +539,7 @@ class WebUIPasswordHandler : public CivetHandler
{
nlohmann::json j = nlohmann::json::object();
j["code"] = -1;
j["msg"] = GBKtoUTF8(e.what());
j["msg"] = e.what();
ret = j.dump();
}
mg_send_http_ok(conn, "application/json", ret.length());
Expand Down

0 comments on commit 74c6e3a

Please sign in to comment.