Skip to content

Commit

Permalink
fix: migrate old form api to lse
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrBox committed Feb 1, 2024
1 parent fdb6ca5 commit 034f4c9
Show file tree
Hide file tree
Showing 7 changed files with 1,126 additions and 91 deletions.
53 changes: 19 additions & 34 deletions src/legacy/api/GuiAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "api/PlayerAPI.h"
#include "engine/EngineOwnData.h"
#include "engine/GlobalShareData.h"
#include "engine/LocalShareData.h"
#include "ll/api/form/SimpleForm.h"

// #include <llapi/SendPacketAPI.h> //todo
#include "ll/api/service/ServerInfo.h"
Expand Down Expand Up @@ -46,26 +44,26 @@ Local<Object> SimpleFormClass::newForm() {
return newp->getScriptObject();
}

ll::form::SimpleForm* SimpleFormClass::extract(Local<Value> v) {
lse::form::SimpleForm* SimpleFormClass::extract(Local<Value> v) {
if (EngineScope::currentEngine()->isInstanceOf<SimpleFormClass>(v))
return EngineScope::currentEngine()->getNativeInstance<SimpleFormClass>(v)->get();
else return nullptr;
}

void SimpleFormClass::sendForm(ll::form::SimpleForm* form, Player* player, script::Local<Function>& callback) {
void SimpleFormClass::sendForm(lse::form::SimpleForm* form, Player* player, script::Local<Function>& callback) {
script::Global<Function> callbackFunc{callback};

form->sendTo(
*player,
[engine{EngineScope::currentEngine()}, callback{std::move(callbackFunc)}](Player& pl, int chosen) {
player,
[engine{EngineScope::currentEngine()}, callback{std::move(callbackFunc)}](Player* pl, int chosen) {
if ((ll::getServerStatus() != ll::ServerStatus::Running)) return;
if (!EngineManager::isValid(engine)) return;
if (callback.isEmpty()) return;

EngineScope scope(engine);
try {
if (chosen < 0) callback.get().call({}, PlayerClass::newPlayer(&pl), Local<Value>());
else callback.get().call({}, PlayerClass::newPlayer(&pl), Number::newNumber(chosen));
if (chosen < 0) callback.get().call({}, PlayerClass::newPlayer(pl), Local<Value>());
else callback.get().call({}, PlayerClass::newPlayer(pl), Number::newNumber(chosen));
}
CATCH_WITHOUT_RETURN("Fail in form callback!")
}
Expand Down Expand Up @@ -102,7 +100,7 @@ Local<Value> SimpleFormClass::addButton(const Arguments& args) {

try {
string image = args.size() >= 2 ? args[1].toStr() : "";
form.appendButton(args[0].toStr(), image);
form.addButton(args[0].toStr(), image);
return this->getScriptObject();
}
CATCH("Fail in addButton!")
Expand All @@ -118,39 +116,26 @@ Local<Object> CustomFormClass::newForm() {
return newp->getScriptObject();
}

ll::form::CustomForm* CustomFormClass::extract(Local<Value> v) {
lse::form::CustomForm* CustomFormClass::extract(Local<Value> v) {
if (EngineScope::currentEngine()->isInstanceOf<CustomFormClass>(v))
return EngineScope::currentEngine()->getNativeInstance<CustomFormClass>(v)->get();
else return nullptr;
}

// 成员函数
void CustomFormClass::sendForm(ll::form::CustomForm* form, Player* player, script::Local<Function>& callback) {
void CustomFormClass::sendForm(lse::form::CustomForm* form, Player* player, script::Local<Function>& callback) {
script::Global<Function> callbackFunc{callback};

form->sendTo(
*player,
[engine{EngineScope::currentEngine()},
callback{std::move(callbackFunc)}](Player& pl, ll::form::CustomFormResult const& data) {
form->sendToForRawJson(
player,
[engine{EngineScope::currentEngine()}, callback{std::move(callbackFunc)}](Player* player, std::string data) {
if (ll::getServerStatus() != ll::ServerStatus::Running) return;
if (!EngineManager::isValid(engine)) return;
if (callback.isEmpty()) return;

EngineScope scope(engine);
try {
nlohmann::ordered_json result;
for (auto& [k, v] : data) {
std::visit(
[&](auto&& val) {
if constexpr (!std::is_same_v<std::remove_cvref_t<decltype(val)>, std::monostate>) {
result.emplace_back(val);
}
},
v
);
}

callback.get().call({}, PlayerClass::newPlayer(&pl), JsonToValue(result));
callback.get().call({}, PlayerClass::newPlayer(player), JsonToValue(data));
}
CATCH_WITHOUT_RETURN("Fail in form callback!")
}
Expand All @@ -173,7 +158,7 @@ Local<Value> CustomFormClass::addLabel(const Arguments& args) {
CHECK_ARG_TYPE(args[0], ValueKind::kString)

try {
form.appendLabel(args[0].toStr());
form.addLabel(args[0].toStr(), args[0].toStr());
return this->getScriptObject();
}
CATCH("Fail in addLabel!")
Expand All @@ -189,7 +174,7 @@ Local<Value> CustomFormClass::addInput(const Arguments& args) {
string placeholder = args.size() >= 2 ? args[1].toStr() : "";
string def = args.size() >= 3 ? args[2].toStr() : "";

form.appendInput(args[0].toStr(), args[0].toStr(), placeholder, def);
form.addInput(args[0].toStr(), args[0].toStr(), placeholder, def);
return this->getScriptObject();
}
CATCH("Fail in addInput!")
Expand All @@ -209,7 +194,7 @@ Local<Value> CustomFormClass::addSwitch(const Arguments& args) {
bool def =
args.size() >= 2 ? args[1].isBoolean() ? args[1].asBoolean().value() : args[1].asNumber().toInt32() : false;

form.appendToggle(args[0].toStr(), args[0].toStr(), def);
form.addToggle(args[0].toStr(), args[0].toStr(), def);
return this->getScriptObject();
}
CATCH("Fail in addSwitch!")
Expand All @@ -228,7 +213,7 @@ Local<Value> CustomFormClass::addDropdown(const Arguments& args) {

int def = args.size() >= 3 ? args[2].asNumber().toInt32() : 0;

form.appendDropdown(args[0].toStr(), args[0].toStr(), options, def);
form.addDropdown(args[0].toStr(), args[0].toStr(), options, def);
return this->getScriptObject();
}
CATCH("Fail in addDropdown!")
Expand All @@ -251,7 +236,7 @@ Local<Value> CustomFormClass::addSlider(const Arguments& args) {
int defValue = args.size() >= 5 ? args[4].asNumber().toInt32() : minValue;
if (defValue < minValue || defValue > maxValue) defValue = minValue;

form.appendSlider(args[0].toStr(), args[0].toStr(), minValue, maxValue, step, defValue);
form.addSlider(args[0].toStr(), args[0].toStr(), minValue, maxValue, step, defValue);
return this->getScriptObject();
}
CATCH("Fail in addSlider!")
Expand All @@ -270,7 +255,7 @@ Local<Value> CustomFormClass::addStepSlider(const Arguments& args) {

int defIndex = args.size() >= 3 ? args[2].asNumber().toInt32() : 0;

form.appendStepSlider(args[0].toStr(), args[0].toStr(), steps, defIndex);
form.addStepSlider(args[0].toStr(), args[0].toStr(), steps, defIndex);
return this->getScriptObject();
}
CATCH("Fail in addStepSlider!")
Expand Down
26 changes: 12 additions & 14 deletions src/legacy/api/GuiAPI.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#pragma once
#include "api/APIHelp.h"
#include "ll/api/form/CustomForm.h"
#include "ll/api/form/FormBase.h"
#include "ll/api/form/SimpleForm.h"
#include "legacyapi/form/FormUI.h"

//////////////////// Classes ////////////////////

class SimpleFormClass : public ScriptClass {
private:
ll::form::SimpleForm form;
lse::form::SimpleForm form;

public:
SimpleFormClass();

ll::form::SimpleForm* get() { return &form; }
lse::form::SimpleForm* get() { return &form; }

static Local<Object> newForm();
static ll::form::SimpleForm* extract(Local<Value> v);
static void sendForm(ll::form::SimpleForm* form, Player* player, script::Local<Function>& callback);
static Local<Object> newForm();
static lse::form::SimpleForm* extract(Local<Value> v);
static void sendForm(lse::form::SimpleForm* form, Player* player, script::Local<Function>& callback);

Local<Value> setTitle(const Arguments& args);
Local<Value> setContent(const Arguments& args);
Expand All @@ -27,16 +25,16 @@ extern ClassDefine<SimpleFormClass> SimpleFormClassBuilder;

class CustomFormClass : public ScriptClass {
private:
ll::form::CustomForm form;
lse::form::CustomForm form;

public:
CustomFormClass();

ll::form::CustomForm* get() { return &form; }
lse::form::CustomForm* get() { return &form; }

static Local<Object> newForm();
static ll::form::CustomForm* extract(Local<Value> v);
static void sendForm(ll::form::CustomForm* form, Player* player, script::Local<Function>& callback);
static Local<Object> newForm();
static lse::form::CustomForm* extract(Local<Value> v);
static void sendForm(lse::form::CustomForm* form, Player* player, script::Local<Function>& callback);

Local<Value> setTitle(const Arguments& args);
Local<Value> addLabel(const Arguments& args);
Expand All @@ -46,4 +44,4 @@ class CustomFormClass : public ScriptClass {
Local<Value> addSlider(const Arguments& args);
Local<Value> addStepSlider(const Arguments& args);
};
extern ClassDefine<CustomFormClass> CustomFormClassBuilder;
extern ClassDefine<CustomFormClass> CustomFormClassBuilder;
83 changes: 40 additions & 43 deletions src/legacy/api/PlayerAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
#include "api/PacketAPI.h"
#include "engine/EngineOwnData.h"
#include "engine/GlobalShareData.h"
#include "legacyapi/form/FormPacketHelper.h"
#include "legacyapi/form/FormUI.h"
#include "ll/api/form/CustomForm.h"
#include "ll/api/form/FormBase.h"
#include "ll/api/form/ModalForm.h"
#include "ll/api/form/SimpleForm.h"
#include "ll/api/service/Bedrock.h"
#include "ll/api/service/PlayerInfo.h"
#include "ll/api/service/ServerInfo.h"
Expand All @@ -44,6 +43,7 @@
#include "mc/network/packet/AddEntityPacket.h"
#include "mc/network/packet/BossEventPacket.h"
#include "mc/network/packet/LevelChunkPacket.h"
#include "mc/network/packet/ModalFormRequestPacket.h"
#include "mc/network/packet/RemoveObjectivePacket.h"
#include "mc/network/packet/ScorePacketInfo.h"
#include "mc/network/packet/SetDisplayObjectivePacket.h"
Expand Down Expand Up @@ -2224,29 +2224,26 @@ Local<Value> PlayerClass::sendSimpleForm(const Arguments& args) {
auto imagesArr = args[3].asArray();
if (imagesArr.size() != textsArr.size() || !imagesArr.get(0).isString()) return Local<Value>();

ll::form::SimpleForm form(args[0].asString().toString(), args[1].asString().toString());
lse::form::SimpleForm form(args[0].asString().toString(), args[1].asString().toString());
for (int i = 0; i < textsArr.size(); ++i) {
Local<Value> img = imagesArr.get(i);
if (img.isString()) {
form.appendButton(textsArr.get(i).asString().toString(), img.asString().toString());
form.addButton(textsArr.get(i).asString().toString(), img.asString().toString());
} else {
form.appendButton(textsArr.get(i).asString().toString());
form.addButton(textsArr.get(i).asString().toString());
}
}
form.sendTo(
*player,
player,
[engine{EngineScope::currentEngine()},
callback{script::Global(args[4].asFunction())}](Player& pl, int chosen) {
callback{script::Global(args[4].asFunction())}](Player* pl, int chosen) {
if ((ll::getServerStatus() != ll::ServerStatus::Running)) return;
if (!EngineManager::isValid(engine)) return;

EngineScope scope(engine);
try {
callback.get().call(
{},
PlayerClass::newPlayer(&pl),
chosen >= 0 ? Number::newNumber(chosen) : Local<Value>()
);
callback.get()
.call({}, PlayerClass::newPlayer(pl), chosen >= 0 ? Number::newNumber(chosen) : Local<Value>());
}
CATCH_IN_CALLBACK("sendSimpleForm")
}
Expand All @@ -2269,23 +2266,23 @@ Local<Value> PlayerClass::sendModalForm(const Arguments& args) {
Player* player = get();
if (!player) return Local<Value>();

ll::form::ModalForm form(
lse::form::ModalForm form(
args[0].asString().toString(),
args[1].asString().toString(),
args[2].asString().toString(),
args[3].asString().toString()
);
form.sendTo(
*player,
player,
[engine{EngineScope::currentEngine()},
callback{script::Global(args[4].asFunction())}](Player& pl, bool chosen) {
callback{script::Global(args[4].asFunction())}](Player* pl, bool chosen) {
if ((ll::getServerStatus() != ll::ServerStatus::Running)) return;
if (!EngineManager::isValid(engine)) return;

EngineScope scope(engine);
try {
callback.get()
.call({}, PlayerClass::newPlayer(&pl), chosen ? Boolean::newBoolean(chosen) : Local<Value>());
.call({}, PlayerClass::newPlayer(pl), chosen ? Boolean::newBoolean(chosen) : Local<Value>());
}
CATCH_IN_CALLBACK("sendModalForm")
}
Expand All @@ -2305,36 +2302,36 @@ Local<Value> PlayerClass::sendCustomForm(const Arguments& args) {
Player* player = get();
if (!player) return Local<Value>();
// Todo
// std::string data = ordered_json::parse(args[0].toStr()).dump();

// player->sendCustomFormPacket(
// data, [id{player->getOrCreateUniqueID()},
// engine{EngineScope::currentEngine()},
// callback{script::Global(args[1].asFunction())}](string result)
// {
// if ((ll::getServerStatus() != ll::ServerStatus::Running))
// return;
// if (!EngineManager::isValid(engine))
// return;

// Player *pl = ll::service::getLevel()->getPlayer(id);
// if (!pl)
// return;

// EngineScope scope(engine);
// try {
// callback.get().call({}, PlayerClass::newPlayer(pl),
// result != "null" ? JsonToValue(result)
// : Local<Value>());
// }
// CATCH_IN_CALLBACK("sendCustomForm")
// });
std::string data = ordered_json::parse(args[0].toStr()).dump();

unsigned formId = lse::form::NewFormId();
ModalFormRequestPacket packet(formId, data);
player->sendNetworkPacket(packet);
lse::form::SetCustomFormPacketCallback(
formId,
[id{player->getOrCreateUniqueID()},
engine{EngineScope::currentEngine()},
callback{script::Global(args[1].asFunction())}](Player* player, string result) {
if ((ll::getServerStatus() != ll::ServerStatus::Running)) return;
if (!EngineManager::isValid(engine)) return;

EngineScope scope(engine);
try {
callback.get().call(
{},
PlayerClass::newPlayer(player),
result != "null" ? JsonToValue(result) : Local<Value>()
);
}
CATCH_IN_CALLBACK("sendCustomForm")
}
);
return Number::newNumber(3);
} catch (const ordered_json::exception& e) {
lse::getSelfPluginInstance().getLogger().error("Fail to parse Json string in sendCustomForm!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
return Local<Value>();
return {};
}
CATCH("Fail in sendCustomForm!");
}
Expand All @@ -2347,7 +2344,7 @@ Local<Value> PlayerClass::sendForm(const Arguments& args) {
Player* player = get();
if (!player) return Local<Value>();

ll::form::SimpleForm* form = SimpleFormClass::extract(args[0]);
lse::form::SimpleForm* form = SimpleFormClass::extract(args[0]);
if (IsInstanceOf<SimpleFormClass>(args[0])) {
Local<Function> callback = args[1].asFunction();
SimpleFormClass::sendForm(SimpleFormClass::extract(args[0]), player, callback);
Expand Down
Loading

0 comments on commit 034f4c9

Please sign in to comment.