Skip to content

Commit

Permalink
feat: add NpcDialogueForm
Browse files Browse the repository at this point in the history
  • Loading branch information
KobeBryant114514 committed Oct 10, 2024
1 parent efacbeb commit 5cc610a
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 6 deletions.
55 changes: 54 additions & 1 deletion lib/FormAPI-JS.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const getNextCallbackId = ll.import("GMLIB_FormAPI", "getNextFormCallbackId");

class ServerSettingForm {
constructor() {
throw new Error("Static class cannot be instantiated");
}

static getDefaultPriority() {
return ll.import("GMLIB_ServerSettingForm", "getDefaultPriority")();
}
Expand Down Expand Up @@ -123,7 +127,56 @@ class ServerSettingForm {
static removeElement(id) {
return ll.import("GMLIB_ServerSettingForm", "removeElement")(id);
}
}

class NpcDialogueForm {
constructor(npcName, sceneName, dialogue) {
this.mFormId = ll.import("GMLIB_NpcDialogueForm", "createForm")(npcName, sceneName, dialogue);
}

addButton(button) {
return ll.import("GMLIB_NpcDialogueForm", "addButton")(this.mFormId, button);
}

sendTo(pl, callback = (pl, index, type) => { }, free = true) {
let callbackId = getNextCallbackId();
ll.export(callback, "GMLIB_FORM_CALLBACK", callbackId);
ll.import("GMLIB_NpcDialogueForm", "sendTo")(this.mFormId, pl, callbackId);
if (free) {
this.destroy();
}
}

destroy() {
ll.import("GMLIB_NpcDialogueForm", "destroyForm")(this.mFormId);
}
}

class ChestForm {
constructor(npcName, sceneName, dialogue) {
this.mFormId = ll.import("GMLIB_NpcDialogueForm", "createForm")(npcName, sceneName, dialogue);
}

addButton(button) {
return ll.import("GMLIB_NpcDialogueForm", "addButton")(this.mFormId, button);
}

sendTo(pl, free = true) {
//let callbackId = getNextCallbackId();
//ll.export(callback, "GMLIB_FORM_CALLBACK", callbackId);
//ll.import("GMLIB_NpcDialogueForm", "sendTo")(this.mFormId, pl, callbackId);
//if (free) {
// this.destroy();
//}
}

destroy() {
//ll.import("GMLIB_NpcDialogueForm", "destroyForm")(this.mFormId);
}
}

module.exports = { ServerSettingForm };
module.exports = {
ServerSettingForm,
NpcDialogueForm,
ChestForm
};
80 changes: 75 additions & 5 deletions src/FormAPI.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
#include "Global.h"

#include <GMLIB/Server/FormAPI/ChestForm.h>

using namespace GMLIB::Server::Form;
using namespace ll::hash_utils;

class LegacyScriptFormManager {
private:
int64 mNextFormCallbackId = 0;
// std::unordered_map<int64, ll::event::ListenerPtr> mEventListeners;
int64 mNextFormCallbackId = 0;
int64 mNextFormId = 0;
std::unordered_map<int64, std::unique_ptr<NpcDialogueForm>> mNpcDialogueForms;
std::unordered_map<int64, std::unique_ptr<ChestForm>> mChestForms;

public:
std::string getNextFormCallbackId() {
mNextFormCallbackId++;
return "GMLIB_EVENT_" + std::to_string(mNextFormCallbackId);
}

int64 getNextFormId() {
mNextFormId++;
return mNextFormId;
}

int64 createNpcDialogueForm(std::string const& npcName, std::string const& sceneName, std::string const& dialogue) {
auto formId = LegacyScriptFormManager::getInstance().getNextFormId();
auto formPtr = std::make_unique<NpcDialogueForm>(npcName, sceneName, dialogue);
mNpcDialogueForms[formId] = std::move(formPtr);
return formId;
}

bool destroyNpcDialogueForm(int64 formId) {
if (mNpcDialogueForms.contains(formId)) {
mNpcDialogueForms.erase(formId);
return true;
}
return false;
}

optional_ref<NpcDialogueForm> getNpcDialogueForm(int64 formId) {
if (mNpcDialogueForms.contains(formId)) {
return mNpcDialogueForms[formId].get();
}
return {};
}

public:
static LegacyScriptFormManager& getInstance() {
static std::unique_ptr<LegacyScriptFormManager> instance;
Expand Down Expand Up @@ -89,12 +120,14 @@ class LegacyScriptFormManager {


void Export_Form_API() {
RemoteCall::exportAs("GMLIB_ServerSettingForm", "getDefaultPriority", []() -> int {
return ServerSettingForm::getDefaultPriority();
});
//////////////////////////////// Form Manager /////////////////////////////////
RemoteCall::exportAs("GMLIB_FormAPI", "getNextFormCallbackId", []() -> std::string {
return LegacyScriptFormManager::getInstance().getNextFormCallbackId();
});
////////////////////////////// ServerSettingForm //////////////////////////////
RemoteCall::exportAs("GMLIB_ServerSettingForm", "getDefaultPriority", []() -> int {
return ServerSettingForm::getDefaultPriority();
});
RemoteCall::exportAs("GMLIB_ServerSettingForm", "hasTitle", []() -> bool { return ServerSettingForm::hasTitle(); });
RemoteCall::exportAs("GMLIB_ServerSettingForm", "getTitle", []() -> std::string {
return ServerSettingForm::getTitle();
Expand Down Expand Up @@ -229,4 +262,41 @@ void Export_Form_API() {
RemoteCall::exportAs("GMLIB_ServerSettingForm", "removeElement", [](uint id) -> bool {
return ServerSettingForm::removeElement(id);
});
////////////////////////////// NpcDialogueForm //////////////////////////////
RemoteCall::exportAs(
"GMLIB_NpcDialogueForm",
"createForm",
[](std::string const& npcName, std::string const& sceneName, std::string const& dialogue) -> int64 {
return LegacyScriptFormManager::getInstance().createNpcDialogueForm(npcName, sceneName, dialogue);
}
);
RemoteCall::exportAs("GMLIB_NpcDialogueForm", "destroyForm", [](int64 formId) -> bool {
return LegacyScriptFormManager::getInstance().destroyNpcDialogueForm(formId);
});
RemoteCall::exportAs("GMLIB_NpcDialogueForm", "addButton", [](int64 formId, std::string const& button) -> int {
if (auto formPtr = LegacyScriptFormManager::getInstance().getNpcDialogueForm(formId)) {
return formPtr->addButton(button);
}
return -1;
});
RemoteCall::exportAs(
"GMLIB_NpcDialogueForm",
"sendTo",
[](int64 formId, Player* pl, std::string const& callbackId) -> void {
if (auto formPtr = LegacyScriptFormManager::getInstance().getNpcDialogueForm(formId)) {
formPtr->sendTo(*pl, [callbackId](Player& pl, int index, NpcRequestPacket::RequestType type) -> void {
try {
if (RemoteCall::hasFunc("GMLIB_FORM_CALLBACK", callbackId)) {
auto const& callback = RemoteCall::importAs<void(Player*, int index, int type)>(
"GMLIB_FORM_CALLBACK",
callbackId
);
callback(&pl, index, (int)type);
}
} catch (...) {}
});
}
}
);
////////////////////////////// ChestForm //////////////////////////////
}

0 comments on commit 5cc610a

Please sign in to comment.