diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a74f60b..bf80ebc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -29,4 +29,10 @@ jobs: with: name: ${{ github.event.repository.name }}-windows-x64-${{ github.sha }} path: | - bin/ + bin/DLL/ + + - uses: actions/upload-artifact@v3 + with: + name: PDB + path: | + bin/PDB/ diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 015da94..d01ad80 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,7 +11,7 @@ jobs: uses: actions/checkout@v2 with: submodules: recursive - + - uses: actions/checkout@v4 - uses: xmake-io/github-action-setup-xmake@v1 @@ -29,7 +29,13 @@ jobs: with: name: ${{ github.event.repository.name }}-windows-x64-${{ github.sha }} path: | - bin/ + bin/DLL/ + + - uses: actions/upload-artifact@v3 + with: + name: PDB + path: | + bin/PDB/ upload-to-release: needs: @@ -38,24 +44,35 @@ jobs: contents: write runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v4 - - uses: actions/download-artifact@v3 + - name: Download Plugin + uses: actions/download-artifact@v3 with: name: ${{ github.event.repository.name }}-windows-x64-${{ github.sha }} - path: release/ + path: release/Plugin/ - - run: | - cp LICENSE README.md release/ + - name: Download PDB + uses: actions/download-artifact@v3 + with: + name: PDB + path: release/PDB/ + + - name: Copy additional files + run: | + cp LICENSE README.md release/Plugin/ - name: Archive release run: | - cd release + cd release/Plugin zip -r ../${{ github.event.repository.name }}-windows-x64.zip * cd .. - - uses: softprops/action-gh-release@v1 + - name: Create GitHub Release + id: create_release + uses: softprops/action-gh-release@v1 with: - append_body: true files: | - ${{ github.event.repository.name }}-windows-x64.zip + release/${{ github.event.repository.name }}-windows-x64.zip + release/PDB/${{ github.event.repository.name }}.pdb diff --git a/SDK-GMLIB b/SDK-GMLIB index b1e7ebd..9935ed4 160000 --- a/SDK-GMLIB +++ b/SDK-GMLIB @@ -1 +1 @@ -Subproject commit b1e7ebd770e667604016f93dc43359dc8212c043 +Subproject commit 9935ed40635eacf491ad40fd7a93629ee34847bc diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index ab5d461..90e4da1 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -45,7 +45,49 @@ const GMLIB_API = { registerShapelessRecipe: ll.import("GMLib_ModAPI", "registerShapelessRecipe"), isVersionMatched: ll.import("GMLIB_API", "isVersionMatched"), getVersion_LRCA: ll.import("GMLIB_API", "getVersion_LRCA"), - getVersion_GMLIB: ll.import("GMLIB_API", "getVersion_GMLIB") + getVersion_GMLIB: ll.import("GMLIB_API", "getVersion_GMLIB"), + getPlayerPosition: ll.import("GMLIB_API", "getPlayerPosition"), + setPlayerPosition: ll.import("GMLIB_API", "setPlayerPosition"), + playerHasScore: ll.import("GMLIB_API", "playerHasScore"), + getPlayerScore: ll.import("GMLIB_API", "getPlayerScore"), + addPlayerScore: ll.import("GMLIB_API", "addPlayerScore"), + reducePlayerScore: ll.import("GMLIB_API", "reducePlayerScore"), + setPlayerScore: ll.import("GMLIB_API", "setPlayerScore"), + resetPlayerScore: ll.import("GMLIB_API", "resetPlayerScore"), + resetPlayerScores: ll.import("GMLIB_API", "resetPlayerScores"), + entityHasScore: ll.import("GMLIB_API", "entityHasScore"), + getEntityScore: ll.import("GMLIB_API", "getEntityScore"), + addEntityScore: ll.import("GMLIB_API", "addEntityScore"), + reduceEntityScore: ll.import("GMLIB_API", "reduceEntityScore"), + setEntityScore: ll.import("GMLIB_API", "setEntityScore"), + resetEntityScore: ll.import("GMLIB_API", "resetEntityScore"), + resetEntityScores: ll.import("GMLIB_API", "resetEntityScores"), + fakePlayerHasScore: ll.import("GMLIB_API", "fakePlayerHasScore"), + getFakePlayerScore: ll.import("GMLIB_API", "getFakePlayerScore"), + addFakePlayerScore: ll.import("GMLIB_API", "addFakePlayerScore"), + reduceFakePlayerScore: ll.import("GMLIB_API", "reduceFakePlayerScore"), + setFakePlayerScore: ll.import("GMLIB_API", "setFakePlayerScore"), + resetFakePlayerScore: ll.import("GMLIB_API", "resetFakePlayerScore"), + resetFakePlayerScores: ll.import("GMLIB_API", "resetFakePlayerScores"), + addObjective: ll.import("GMLIB_API", "addObjective"), + addObjectiveWithDisplayName: ll.import("GMLIB_API", "addObjectiveWithDisplayName"), + getDisplayName: ll.import("GMLIB_API", "getDisplayName"), + setDisplayName: ll.import("GMLIB_API", "setDisplayName"), + removeObjective: ll.import("GMLIB_API", "removeObjective"), + setDisplayObjective: ll.import("GMLIB_API", "setDisplayObjective"), + clearDisplayObjective: ll.import("GMLIB_API", "clearDisplayObjective"), + getAllObjectives: ll.import("GMLIB_API", "getAllObjectives"), + getAllScoreboardPlayers: ll.import("GMLIB_API", "getAllScoreboardPlayers"), + getAllScoreboardFakePlayers: ll.import("GMLIB_API", "getAllScoreboardFakePlayers"), + getAllScoreboardEntities: ll.import("GMLIB_API", "getAllScoreboardEntities"), + getPlayerFromUuid: ll.import("GMLIB_API", "getPlayerFromUuid"), + getPlayerFromUniqueId: ll.import("GMLIB_API", "getPlayerFromUniqueId"), + getEntityFromUniqueId: ll.import("GMLIB_API", "getEntityFromUniqueId"), + getWorldSpawn: ll.import("GMLIB_API", "getWorldSpawn"), + setWorldSpawn: ll.import("GMLIB_API", "setWorldSpawn"), + getPlayerSpawnPoint: ll.import("GMLIB_API", "getPlayerSpawnPoint"), + setPlayerSpawnPoint: ll.import("GMLIB_API", "setPlayerSpawnPoint"), + clearPlayerSpawnPoint: ll.import("GMLIB_API", "clearPlayerSpawnPoint") } const FloatingTextList = []; @@ -237,13 +279,16 @@ class Minecraft { static getPlayerNbt(uuid) { let snbt = GMLIB_API.getPlayerNbt(uuid); + if (snbt == "null") { + return null; + } let nbt = NBT.parseSNBT(snbt); return nbt; } - static setPlayerNbt(uuid, nbt) { + static setPlayerNbt(uuid, nbt, forceCreate = true) { let snbt = nbt.toSNBT(); - return GMLIB_API.setPlayerNbt(uuid, snbt); + return GMLIB_API.setPlayerNbt(uuid, snbt, forceCreate); } static setPlayerNbtTags(uuid, nbt, tags) { @@ -251,6 +296,38 @@ class Minecraft { return GMLIB_API.setPlayerNbtTags(uuid, snbt, tags); } + static getPlayerPosition(uuid) { + let pos = GMLIB_API.getPlayerPosition(uuid); + if (pos.dimid == -1) { + return null; + } + return pos; + } + + static setPlayerPosition(uuid, pos) { + return GMLIB_API.setPlayerPosition(uuid, pos); + } + + static getWorldSpawn() { + return GMLIB_API.getWorldSpawn(); + } + + static setWorldSpawn(pos) { + return GMLIB_API.setWorldSpawn(pos); + } + + static getPlayerSpawnPoint() { + return GMLIB_API.getPlayerSpawnPoint(); + } + + static setPlayerSpawnPoint(pos) { + GMLIB_API.setPlayerSpawnPoint(pos); + } + + static clearPlayerSpawnPoint() { + GMLIB_API.clearPlayerSpawnPoint(); + } + static setEducationFeatureEnabled() { return GMLIB_API.setEducationFeatureEnabled(); } @@ -406,4 +483,218 @@ class GMLIB { } } -module.exports = { StaticFloatingText, DynamicFloatingText, Minecraft, Recipes, Experiments, GMLIB }; \ No newline at end of file +class Scoreboard { + constructor() { + throw new Error("Static class cannot be instantiated"); + } + + // Targets + static getAllTrackedEntities() { + return GMLIB_API.getAllScoreboardEntities(); + } + + static getAllTrackedPlayers() { + return GMLIB_API.getAllScoreboardPlayers(); + } + + static getAllTrackedFakePlayers() { + return GMLIB_API.getAllScoreboardFakePlayers(); + } + + static getAllTrackedTargets() { + let result = []; + GMLIB_API.getAllScoreboardPlayers().forEach((uuid) => { + let key = { + "Type": "Player", + "Uuid": uuid + }; + result.push(key); + }); + GMLIB_API.getAllScoreboardEntities().forEach((uniqueId) => { + let key = { + "Type": "Entity", + "UniqueId": uniqueId + }; + result.push(key); + }); + GMLIB_API.getAllScoreboardFakePlayers().forEach((name) => { + let key = { + "Type": "FakePlayer", + "Name": name + }; + result.push(key); + }); + return result; + } + + // Objectives + static addObjective(name, displayName = "") { + if (displayName == "") { + return GMLIB_API.addObjective(name); + } + return GMLIB_API.addObjectiveWithDisplayName(name, displayName); + } + + static removeObjective(name) { + return GMLIB_API.removeObjective(name); + } + + static getAllObjectives() { + return GMLIB_API.getAllObjectives(); + } + + static getDisplayName(objective) { + return GMLIB_API.getDisplayName(objective); + } + + static setDisplayName(objective, displayName) { + return GMLIB_API.setDisplayName(objective, displayName); + } + + static setDisplay(objective, slot, order = 0) { + GMLIB_API.setDisplayObjective(objective, slot, order); + } + + static clearDisplay(slot) { + GMLIB_API.clearDisplayObjective(slot); + } + + // Player Score + static getPlayerScore(uuid, objective) { + if (GMLIB_API.playerHasScore(uuid, objective)) { + return GMLIB_API.getPlayerScore(uuid, objective); + } + return null; + } + + static addPlayerScore(uuid, objective, value) { + return GMLIB_API.addPlayerScore(uuid, objective, value); + } + + static reducePlayerScore(uuid, objective, value) { + return GMLIB_API.reducePlayerScore(uuid, objective, value); + } + + static setPlayerScore(uuid, objective, value) { + return GMLIB_API.setPlayerScore(uuid, objective, value); + } + + static resetPlayerScore(uuid, objective) { + return GMLIB_API.resetPlayerScore(uuid, objective); + } + + static resetPlayerScores(uuid) { + return GMLIB_API.resetPlayerScores(uuid); + } + + // FakePlayer Score + static getFakePlayerScore(name, objective) { + if (GMLIB_API.fakePlayerHasScore(name, objective)) { + return GMLIB_API.getFakePlayerScore(name, objective); + } + return null; + } + + static addFakePlayerScore(name, objective, value) { + return GMLIB_API.addFakePlayerScore(name, objective, value); + } + + static reduceFakePlayerScore(name, objective, value) { + return GMLIB_API.reduceFakePlayerScore(name, objective, value); + } + + static setFakePlayerScore(name, objective, value) { + return GMLIB_API.setFakePlayerScore(name, objective, value); + } + + static resetFakePlayerScore(name, objective) { + return GMLIB_API.resetFakePlayerScore(name, objective); + } + + static resetFakePlayerScores(name) { + return GMLIB_API.resetFakePlayerScores(name); + } + + // Entity Score + static getEntityScore(uniqueId, objective) { + if (GMLIB_API.entityHasScore(uniqueId, objective)) { + return GMLIB_API.getEntityScore(uniqueId, objective); + } + return null; + } + + static addEntityScore(uniqueId, objective, value) { + return GMLIB_API.addEntityScore(uniqueId, objective, value); + } + + static reduceEntityScore(uniqueId, objective, value) { + return GMLIB_API.reduceEntityScore(uniqueId, objective, value); + } + + static setEntityScore(uniqueId, objective, value) { + return GMLIB_API.setEntityScore(uniqueId, objective, value); + } + + static resetEntityScore(uniqueId, objective) { + return GMLIB_API.resetEntityScore(uniqueId, objective); + } + + static resetEntityScores(uniqueId) { + return GMLIB_API.resetEntityScores(uniqueId); + } + +} + +class JsonConfig { + constructor(path, defultValue = {}) { + this.mData = defultValue; + this.mPath = path; + } + + init() { + if (File.exists(this.mPath)) { + let existDataStr = File.readFrom(this.mPath); + this.mData = Object.assign({}, this.mData, JSON.parse(existDataStr)); + } + this.save(); + } + + save(format = 4) { + let dataStr = JSON.stringify(this.mData, null, format); + File.writeTo(this.mPath, dataStr); + } + + getData() { + return this.mData; + } + + get(key, defultValue = null) { + let result = this.getData()[key]; + if (!result && defultValue != null) { + this.set(key, defultValue); + return defultValue; + } + return result; + } + + set(key, value) { + this.getData()[key] = value; + this.save(); + } + + delete(key) { + delete this.getData()[key]; + this.save(); + } +} + +module.exports = { + StaticFloatingText, + DynamicFloatingText, + Minecraft, + Recipes, + Experiments, + GMLIB, + Scoreboard, + JsonConfig +}; \ No newline at end of file diff --git a/manifest.json b/manifest.json index edd34e8..2dea787 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,7 @@ { "name": "${pluginName}", "entry": "${pluginFile}", + "version": "0.9.4", "type": "native", "dependencies": [ { diff --git a/scripts/after_build.lua b/scripts/after_build.lua index 3c1f6d1..d3fc616 100644 --- a/scripts/after_build.lua +++ b/scripts/after_build.lua @@ -88,10 +88,11 @@ function pack_plugin(target,plugin_define) local manifest_path = find_file("manifest.json", os.projectdir()) if manifest_path then local manifest = io.readfile(manifest_path) - local bindir = path.join(os.projectdir(), "bin") + local bindir = path.join(os.projectdir(), "bin/DLL") + local pdbdir = path.join(os.projectdir(), "bin/PDB") local outputdir = path.join(bindir, plugin_define.pluginName) local targetfile = path.join(outputdir, plugin_define.pluginFile) - local pdbfile = path.join(outputdir, path.basename(plugin_define.pluginFile) .. ".pdb") + local pdbfile = path.join(pdbdir, path.basename(plugin_define.pluginFile) .. ".pdb") local libfile = path.join(os.projectdir(), "lib") local manifestfile = path.join(outputdir, "manifest.json") local oritargetfile = target:targetfile() diff --git a/src/CompatibilityApi.cpp b/src/CompatibilityApi.cpp index 729172f..26e21dd 100644 --- a/src/CompatibilityApi.cpp +++ b/src/CompatibilityApi.cpp @@ -1,38 +1,60 @@ #include "Global.h" +#include + +bool isInteger(const std::string& str) { + std::regex pattern("^[+-]?\\d+$"); + return std::regex_match(str, pattern); +} + +ActorUniqueID parseScriptUniqueID(std::string uniqueId) { + if (!isInteger(uniqueId)) { + return ActorUniqueID::INVALID_ID; + } + return ActorUniqueID(std::stoll(uniqueId)); +} void Export_Compatibility_API() { RemoteCall::exportAs("GMLIB_API", "getServerMspt", []() -> float { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return 0.0f; } return level->getServerMspt(); }); RemoteCall::exportAs("GMLIB_API", "getServerCurrentTps", []() -> float { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return 0.0f; } return level->getServerCurrentTps(); }); RemoteCall::exportAs("GMLIB_API", "getServerAverageTps", []() -> float { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return 0.0f; } return level->getServerAverageTps(); }); RemoteCall::exportAs("GMLIB_API", "getAllPlayerUuids", []() -> std::vector { - return GMLIB_Player::getAllUuids(); + std::vector result; + std::vector uuids = GMLIB_Player::getAllUuids(); + for (auto& uuid : uuids) { + result.push_back(uuid.asString()); + } + return result; }); RemoteCall::exportAs("GMLIB_API", "getPlayerNbt", [](std::string uuid) -> std::string { auto uid = mce::UUID::fromString(uuid); - return GMLIB_Player::getPlayerNbt(uid)->toSnbt(); + auto nbt = GMLIB_Player::getPlayerNbt(uid); + if (nbt) { + return nbt->toSnbt(); + } + return "null"; }); - RemoteCall::exportAs("GMLIB_API", "setPlayerNbt", [](std::string uuid, std::string snbt) -> bool { + RemoteCall::exportAs("GMLIB_API", "setPlayerNbt", [](std::string uuid, std::string snbt, bool forceCreate) -> bool { auto uid = mce::UUID::fromString(uuid); auto nbt = CompoundTag::fromSnbt(snbt); - return GMLIB_Player::setPlayerNbt(uid, *nbt); + return GMLIB_Player::setPlayerNbt(uid, *nbt, forceCreate); }); RemoteCall::exportAs( "GMLIB_API", @@ -141,4 +163,256 @@ void Export_Compatibility_API() { RemoteCall::exportAs("GMLIB_API", "chooseResourcePackI18nLanguage", [](std::string code) -> void { I18n::chooseLanguage(code); }); + RemoteCall::exportAs("GMLIB_API", "getPlayerPosition", [](std::string uuid) -> std::pair { + auto uid = mce::UUID::fromString(uuid); + auto pos = GMLIB_Player::getPlayerPosition(uid); + if (pos.has_value()) { + return pos.value(); + } + return { + {0, 0, 0}, + -1 + }; + }); + RemoteCall::exportAs("GMLIB_API", "setPlayerPosition", [](std::string uuid, std::pair pos) -> bool { + auto uid = mce::UUID::fromString(uuid); + return GMLIB_Player::setPlayerPosition(uid, pos.first, pos.second); + }); + RemoteCall::exportAs("GMLIB_API", "playerHasScore", [](std::string uuid, std::string obj) -> bool { + auto uid = mce::UUID::fromString(uuid); + if (auto result = GMLIB_Player::getPlayerScore(uid, obj)) { + return true; + } + return false; + }); + RemoteCall::exportAs("GMLIB_API", "getPlayerScore", [](std::string uuid, std::string obj) -> int { + auto uid = mce::UUID::fromString(uuid); + if (auto result = GMLIB_Player::getPlayerScore(uid, obj)) { + return result.value(); + } + return 0; + }); + RemoteCall::exportAs("GMLIB_API", "addPlayerScore", [](std::string uuid, std::string obj, int value) -> bool { + auto uid = mce::UUID::fromString(uuid); + if (auto res = GMLIB_Player::setPlayerScore(uid, obj, value, PlayerScoreSetFunction::Add)) { + return true; + } + return false; + }); + RemoteCall::exportAs("GMLIB_API", "reducePlayerScore", [](std::string uuid, std::string obj, int value) -> bool { + auto uid = mce::UUID::fromString(uuid); + if (auto res = GMLIB_Player::setPlayerScore(uid, obj, value, PlayerScoreSetFunction::Subtract)) { + return true; + } + return false; + }); + RemoteCall::exportAs("GMLIB_API", "setPlayerScore", [](std::string uuid, std::string obj, int value) -> bool { + auto uid = mce::UUID::fromString(uuid); + if (auto res = GMLIB_Player::setPlayerScore(uid, obj, value)) { + return true; + } + return false; + }); + RemoteCall::exportAs("GMLIB_API", "resetPlayerScore", [](std::string uuid, std::string obj) -> bool { + auto uid = mce::UUID::fromString(uuid); + return GMLIB_Player::resetPlayerScore(uid, obj); + }); + RemoteCall::exportAs("GMLIB_API", "resetPlayerScores", [](std::string uuid) -> bool { + auto uid = mce::UUID::fromString(uuid); + return GMLIB_Player::resetPlayerScore(uid); + }); + RemoteCall::exportAs("GMLIB_API", "entityHasScore", [](std::string uniqueId, std::string obj) -> bool { + auto auid = parseScriptUniqueID(uniqueId); + if (auto result = GMLIB_Scoreboard::getInstance()->getScore(obj, auid)) { + return true; + } + return false; + }); + RemoteCall::exportAs("GMLIB_API", "getEntityScore", [](std::string uniqueId, std::string obj) -> int { + auto auid = parseScriptUniqueID(uniqueId); + if (auto result = GMLIB_Scoreboard::getInstance()->getScore(obj, auid)) { + return result.value(); + } + return 0; + }); + RemoteCall::exportAs("GMLIB_API", "addEntityScore", [](std::string uniqueId, std::string obj, int value) -> bool { + auto auid = parseScriptUniqueID(uniqueId); + if (auto res = GMLIB_Scoreboard::getInstance()->setScore(obj, auid, value, PlayerScoreSetFunction::Add)) { + return true; + } + return false; + }); + RemoteCall::exportAs( + "GMLIB_API", + "reduceEntityScore", + [](std::string uniqueId, std::string obj, int value) -> bool { + auto auid = parseScriptUniqueID(uniqueId); + if (auto res = + GMLIB_Scoreboard::getInstance()->setScore(obj, auid, value, PlayerScoreSetFunction::Subtract)) { + return true; + } + return false; + } + ); + RemoteCall::exportAs("GMLIB_API", "setEntityScore", [](std::string uniqueId, std::string obj, int value) -> bool { + auto auid = parseScriptUniqueID(uniqueId); + if (auto res = GMLIB_Scoreboard::getInstance()->setScore(obj, auid, value)) { + return true; + } + return false; + }); + RemoteCall::exportAs("GMLIB_API", "resetEntityScore", [](std::string uniqueId, std::string obj) -> bool { + auto auid = parseScriptUniqueID(uniqueId); + return GMLIB_Scoreboard::getInstance()->resetScore(obj, auid); + }); + RemoteCall::exportAs("GMLIB_API", "resetEntityScores", [](std::string uniqueId) -> bool { + auto auid = parseScriptUniqueID(uniqueId); + return GMLIB_Scoreboard::getInstance()->resetScore(auid); + }); + RemoteCall::exportAs("GMLIB_API", "fakePlayerHasScore", [](std::string name, std::string obj) -> bool { + if (auto result = GMLIB_Scoreboard::getInstance()->getScore(obj, name)) { + return true; + } + return false; + }); + RemoteCall::exportAs("GMLIB_API", "getFakePlayerScore", [](std::string name, std::string obj) -> int { + if (auto result = GMLIB_Scoreboard::getInstance()->getScore(obj, name)) { + return result.value(); + } + return 0; + }); + RemoteCall::exportAs("GMLIB_API", "addFakePlayerScore", [](std::string name, std::string obj, int value) -> bool { + if (auto res = GMLIB_Scoreboard::getInstance()->setScore(obj, name, value, PlayerScoreSetFunction::Add)) { + return true; + } + return false; + }); + RemoteCall::exportAs( + "GMLIB_API", + "reduceFakePlayerScore", + [](std::string name, std::string obj, int value) -> bool { + if (auto res = + GMLIB_Scoreboard::getInstance()->setScore(obj, name, value, PlayerScoreSetFunction::Subtract)) { + return true; + } + return false; + } + ); + RemoteCall::exportAs("GMLIB_API", "setFakePlayerScore", [](std::string name, std::string obj, int value) -> bool { + if (auto res = GMLIB_Scoreboard::getInstance()->setScore(obj, name, value)) { + return true; + } + return false; + }); + RemoteCall::exportAs("GMLIB_API", "resetFakePlayerScore", [](std::string name, std::string obj) -> bool { + return GMLIB_Scoreboard::getInstance()->resetScore(obj, name); + }); + RemoteCall::exportAs("GMLIB_API", "resetFakePlayerScores", [](std::string name) -> bool { + return GMLIB_Scoreboard::getInstance()->resetScore(name); + }); + RemoteCall::exportAs("GMLIB_API", "addObjective", [](std::string obj) -> bool { + if (auto res = GMLIB_Scoreboard::getInstance()->getInstance()->addObjective(obj)) { + return true; + } + return false; + }); + RemoteCall::exportAs( + "GMLIB_API", + "addObjectiveWithDisplayName", + [](std::string obj, std::string displayName) -> bool { + if (auto res = GMLIB_Scoreboard::getInstance()->getInstance()->addObjective(obj, displayName)) { + return true; + } + return false; + } + ); + RemoteCall::exportAs("GMLIB_API", "getDisplayName", [](std::string obj) -> std::string { + if (auto result = GMLIB_Scoreboard::getInstance()->getInstance()->getObjectiveDisplayName(obj)) { + return result.value(); + } + return ""; + }); + RemoteCall::exportAs("GMLIB_API", "setDisplayName", [](std::string obj, std::string displayName) -> bool { + return GMLIB_Scoreboard::getInstance()->getInstance()->setObjectiveDisplayName(obj, displayName); + }); + RemoteCall::exportAs("GMLIB_API", "removeObjective", [](std::string obj) -> bool { + return GMLIB_Scoreboard::getInstance()->getInstance()->removeObjective(obj); + }); + RemoteCall::exportAs("GMLIB_API", "setDisplayObjective", [](std::string obj, std::string slot, int order) -> void { + return GMLIB_Scoreboard::getInstance()->getInstance()->setObjectiveDisplay( + obj, + slot, + (ObjectiveSortOrder)order + ); + }); + RemoteCall::exportAs("GMLIB_API", "clearDisplayObjective", [](std::string slot) -> void { + return GMLIB_Scoreboard::getInstance()->getInstance()->clearObjectiveDisplay(slot); + }); + RemoteCall::exportAs("GMLIB_API", "getAllObjectives", []() -> std::vector { + auto objs = GMLIB_Scoreboard::getInstance()->getInstance()->getObjectives(); + std::vector result; + for (auto obj : objs) { + result.push_back(obj->getName()); + } + return result; + }); + RemoteCall::exportAs("GMLIB_API", "getAllScoreboardPlayers", []() -> std::vector { + auto uuids = GMLIB_Scoreboard::getInstance()->getInstance()->getAllPlayerUuids(); + std::vector result; + for (auto& uuid : uuids) { + result.push_back(uuid.asString()); + } + return result; + }); + RemoteCall::exportAs("GMLIB_API", "getAllScoreboardFakePlayers", []() -> std::vector { + auto names = GMLIB_Scoreboard::getInstance()->getInstance()->getAllFakePlayers(); + std::vector result; + for (auto name : names) { + result.push_back(name); + } + return result; + }); + RemoteCall::exportAs("GMLIB_API", "getAllScoreboardEntities", []() -> std::vector { + auto auids = GMLIB_Scoreboard::getInstance()->getInstance()->getAllEntities(); + std::vector result; + for (auto auid : auids) { + result.push_back(std::to_string(auid.get())); + } + return result; + }); + RemoteCall::exportAs("GMLIB_API", "getPlayerFromUuid", [](std::string uuid) -> Player* { + auto uid = mce::UUID::fromString(uuid); + return ll::service::getLevel()->getPlayer(uuid); + }); + RemoteCall::exportAs("GMLIB_API", "getPlayerFromUniqueId", [](std::string uniqueId) -> Actor* { + auto auid = parseScriptUniqueID(uniqueId); + return ll::service::getLevel()->getPlayer(auid); + }); + RemoteCall::exportAs("GMLIB_API", "getEntityFromUniqueId", [](std::string uniqueId) -> Actor* { + auto auid = parseScriptUniqueID(uniqueId); + return ll::service::getLevel()->fetchEntity(auid); + }); + RemoteCall::exportAs("GMLIB_API", "getWorldSpawn", []() -> std::pair { + return {GMLIB_Level::getInstance()->getWorldSpawn(), 0}; + }); + RemoteCall::exportAs("GMLIB_API", "setWorldSpawn", [](std::pair pos) -> bool { + if (pos.second != 0) { + return false; + } + GMLIB_Level::getInstance()->setWorldSpawn(pos.first); + return true; + }); + RemoteCall::exportAs("GMLIB_API", "getPlayerSpawnPoint", [](Player* pl) -> std::pair { + auto player = (GMLIB_Player*)pl; + auto res = player->getSpawnPoint(); + return {res.first, res.second}; + }); + RemoteCall::exportAs("GMLIB_API", "setPlayerSpawnPoint", [](Player* pl, std::pair pos) -> void { + auto player = (GMLIB_Player*)pl; + player->setSpawnPoint(pos.first, pos.second); + }); + RemoteCall::exportAs("GMLIB_API", "clearPlayerSpawnPoint", [](Player* pl) -> void { + auto player = (GMLIB_Player*)pl; + player->clearSpawnPoint(); + }); } \ No newline at end of file diff --git a/src/Entry.cpp b/src/Entry.cpp index c786469..cac3698 100644 --- a/src/Entry.cpp +++ b/src/Entry.cpp @@ -5,17 +5,12 @@ ll::Logger logger(PLUGIN_NAME); namespace GMLIB_LegacyRemoteCallApi { -namespace { +std::unique_ptr& LRCA::getInstance() { + static std::unique_ptr instance; + return instance; +} -std::unique_ptr> - selfPluginInstance; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -auto disable(ll::plugin::NativePlugin& /*self*/) -> bool { return false; } - -auto enable(ll::plugin::NativePlugin& /*self*/) -> bool { return true; } - -auto load(ll::plugin::NativePlugin& self) -> bool { - selfPluginInstance = std::make_unique>(self); +bool LRCA::load() { Export_Legacy_GMLib_ModAPI(); Export_Legacy_GMLib_ServerAPI(); Export_Compatibility_API(); @@ -28,31 +23,10 @@ auto load(ll::plugin::NativePlugin& self) -> bool { return true; } -auto unload(ll::plugin::NativePlugin& self) -> bool { return false; } +bool LRCA::enable() { return true; } -} // namespace - -auto getSelfPluginInstance() -> ll::plugin::NativePlugin& { - if (!selfPluginInstance) { - throw std::runtime_error("selfPluginInstance is null"); - } - - return *selfPluginInstance; -} +bool LRCA::disable() { return true; } } // namespace GMLIB_LegacyRemoteCallApi -extern "C" { -_declspec(dllexport) auto ll_plugin_disable(ll::plugin::NativePlugin& self) -> bool { - return GMLIB_LegacyRemoteCallApi::disable(self); -} -_declspec(dllexport) auto ll_plugin_enable(ll::plugin::NativePlugin& self) -> bool { - return GMLIB_LegacyRemoteCallApi::enable(self); -} -_declspec(dllexport) auto ll_plugin_load(ll::plugin::NativePlugin& self) -> bool { - return GMLIB_LegacyRemoteCallApi::load(self); -} -_declspec(dllexport) auto ll_plugin_unload(ll::plugin::NativePlugin& self) -> bool { - return GMLIB_LegacyRemoteCallApi::unload(self); -} -} +LL_REGISTER_PLUGIN(GMLIB_LegacyRemoteCallApi::LRCA, GMLIB_LegacyRemoteCallApi::LRCA::getInstance()); diff --git a/src/Entry.h b/src/Entry.h index 3bd0e52..6b8d129 100644 --- a/src/Entry.h +++ b/src/Entry.h @@ -1,9 +1,33 @@ #pragma once - #include +#include namespace GMLIB_LegacyRemoteCallApi { -[[nodiscard]] auto getSelfPluginInstance() -> ll::plugin::NativePlugin&; +class LRCA { + +public: + static std::unique_ptr& getInstance(); + + LRCA(ll::plugin::NativePlugin& self) : mSelf(self) {} + + [[nodiscard]] ll::plugin::NativePlugin& getSelf() const { return mSelf; } + + /// @return True if the plugin is loaded successfully. + bool load(); + + /// @return True if the plugin is enabled successfully. + bool enable(); + + /// @return True if the plugin is disabled successfully. + bool disable(); + + // TODO: Implement this method if you need to unload the plugin. + // /// @return True if the plugin is unloaded successfully. + // bool unload(); + +private: + ll::plugin::NativePlugin& mSelf; +}; } // namespace GMLIB_LegacyRemoteCallApi diff --git a/src/Global.h b/src/Global.h index 2938e1c..5bee402 100644 --- a/src/Global.h +++ b/src/Global.h @@ -4,7 +4,7 @@ #include #define PLUGIN_NAME "GMLIB-LRCA" -#define LIB_VERSION SemVersion(0, 9, 1, "", "") +#define LIB_VERSION SemVersion(0, 9, 4, "", "") extern ll::Logger logger; diff --git a/src/LegacyModApi.cpp b/src/LegacyModApi.cpp index b9b5846..413b54e 100644 --- a/src/LegacyModApi.cpp +++ b/src/LegacyModApi.cpp @@ -22,7 +22,7 @@ void Export_Legacy_GMLib_ModAPI() { std::string result, int count, std::string unlock) -> void { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return; } @@ -45,7 +45,7 @@ void Export_Legacy_GMLib_ModAPI() { std::string result, int count, std::string unlock) -> void { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return; } @@ -63,7 +63,7 @@ void Export_Legacy_GMLib_ModAPI() { "GMLib_ModAPI", "registerFurnaceRecipe", [](std::string recipe_id, std::string input, std::string output, std::vector tags) -> void { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return; } @@ -76,7 +76,7 @@ void Export_Legacy_GMLib_ModAPI() { "GMLib_ModAPI", "registerBrewingMixRecipe", [](std::string recipe_id, std::string input, std::string output, std::string reagent) -> void { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return; } @@ -88,7 +88,7 @@ void Export_Legacy_GMLib_ModAPI() { "GMLib_ModAPI", "registerBrewingContainerRecipe", [](std::string recipe_id, std::string input, std::string output, std::string reagent) -> void { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return; } @@ -106,7 +106,7 @@ void Export_Legacy_GMLib_ModAPI() { std::string base, std::string addition, std::string result) -> void { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return; } @@ -123,7 +123,7 @@ void Export_Legacy_GMLib_ModAPI() { "GMLib_ModAPI", "registerSmithingTrimRecipe", [](std::string recipe_id, std::string smithing_template, std::string base, std::string addition) -> void { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return; } @@ -139,7 +139,7 @@ void Export_Legacy_GMLib_ModAPI() { std::string output, int output_data, int output_count) -> void { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return; } @@ -162,23 +162,23 @@ void Export_Legacy_GMLib_ModAPI() { } }); RemoteCall::exportAs("GMLib_ModAPI", "setExperimentEnabled", [](int experiment_id, bool value) -> void { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return; } auto map = GMLIB_Level::getAllExperiments(); if (map.count(experiment_id)) { - GMLIB_Level::getLevel()->setExperimentEnabled(((AllExperiments)experiment_id), value); + GMLIB_Level::getInstance()->setExperimentEnabled(((AllExperiments)experiment_id), value); } else ll::Logger("Server").error("Experiment ID '{}' does not exist!", experiment_id); }); RemoteCall::exportAs("GMLib_ModAPI", "getExperimentEnabled", [](int experiment_id) -> bool { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return false; } auto map = GMLIB_Level::getAllExperiments(); if (map.count(experiment_id)) { - return GMLIB_Level::getLevel()->getExperimentEnabled(((AllExperiments)experiment_id)); + return GMLIB_Level::getInstance()->getExperimentEnabled(((AllExperiments)experiment_id)); } else { ll::Logger("Server").error("Experiment ID '{}' does not exist!", experiment_id); return false; diff --git a/src/LegacyServerApi.cpp b/src/LegacyServerApi.cpp index 41d617b..812579d 100644 --- a/src/LegacyServerApi.cpp +++ b/src/LegacyServerApi.cpp @@ -13,14 +13,14 @@ void Export_Legacy_GMLib_ServerAPI() { RemoteCall::exportAs("GMLib_ServerAPI", "setForceTrustSkins", []() -> void { GMLIB_Level::setForceTrustSkin(); }); RemoteCall::exportAs("GMLib_ServerAPI", "enableCoResourcePack", []() -> void { GMLIB_Level::setCoResourcePack(); }); RemoteCall::exportAs("GMLib_ServerAPI", "getLevelName", []() -> std::string { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return ""; } return level->getLevelName(); }); RemoteCall::exportAs("GMLib_ServerAPI", "setLevelName", [](std::string name) -> void { - auto level = GMLIB_Level::getLevel(); + auto level = GMLIB_Level::getInstance(); if (!level) { return; } diff --git a/tooth.json b/tooth.json index e688327..735ca71 100644 --- a/tooth.json +++ b/tooth.json @@ -1,7 +1,7 @@ { "format_version": 2, "tooth": "github.com/GroupMountain/GMLIB-LegacyRemoteCallApi", - "version": "0.9.3", + "version": "0.9.4", "info": { "name": "GMLIB-LegacyRemoteCallApi", "description": "Legacy RemoteCall API for GMLIB", @@ -14,7 +14,7 @@ "library" ] }, - "asset_url": "https://github.com/GroupMountain/GMLIB-LegacyRemoteCallApi/releases/download/v0.9.3/GMLIB-LegacyRemoteCallApi-windows-x64.zip", + "asset_url": "https://github.com/GroupMountain/GMLIB-LegacyRemoteCallApi/releases/download/v0.9.4/GMLIB-LegacyRemoteCallApi-windows-x64.zip", "dependencies": { "github.com/GroupMountain/GMLIB": ">=0.9.6" }, diff --git a/xmake.lua b/xmake.lua index 547f3e8..b60986c 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,4 +1,4 @@ -add_rules("mode.debug", "mode.release", "mode.releasedbg") +add_rules("mode.debug", "mode.release") add_repositories("liteldev-repo https://github.com/LiteLDev/xmake-repo.git") @@ -47,7 +47,8 @@ target("GMLIB-LegacyRemoteCallApi") -- Change this to your plugin name. "/utf-8" ) add_defines( - "_HAS_CXX23=1" -- To enable C++23 features + "NOMINMAX", + "UNICODE" ) add_files( "src/**.cpp" @@ -68,7 +69,8 @@ target("GMLIB-LegacyRemoteCallApi") -- Change this to your plugin name. ) set_exceptions("none") -- To avoid conflicts with /EHa set_kind("shared") - set_languages("cxx23") + set_languages("c++23") + set_symbols("debug") after_build(function (target) local plugin_packer = import("scripts.after_build")