Skip to content

Commit

Permalink
feat: add more
Browse files Browse the repository at this point in the history
  • Loading branch information
futrime committed Jan 29, 2024
1 parent 4d8c3c0 commit 871617b
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 127 deletions.
24 changes: 24 additions & 0 deletions src/baselib/BaseLib.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- ---------------------
-- For require
-- ---------------------
package.path = "plugins/lib/?.lua;" .. package.path


-- ---------------------
-- For Compatibility
-- ---------------------
file = File
lxl = ll;
-- DirectionAngle.valueOf = DirectionAngle.toFacing
LXL_Block = LLSE_Block
LXL_BlockEntity = LLSE_BlockEntity
LXL_Container = LLSE_Container
LXL_Device = LLSE_Device
LXL_Entity = LLSE_Entity
LXL_SimpleForm = LLSE_SimpleForm
LXL_CustomForm = LLSE_CustomForm
LXL_Item = LLSE_Item
LXL_Player = LLSE_Player
LXL_Objective = LLSE_Objective
ll.export = ll.exports
ll.import = ll.imports
87 changes: 78 additions & 9 deletions src/lse/Entry.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,96 @@
#include "LegacyScriptEngine.h"
#include "Entry.h"

#include "api/EventAPI.h"
#include "api/MoreGlobal.h"
#include "engine/GlobalShareData.h"
#include "engine/LocalShareData.h"
#include "engine/MessageSystem.h"
#include "main/EconomicSystem.h"
#include "main/Loader.h"
#include "main/SafeGuardRecord.h"

#include <functional>
#include <ll/api/io/FileUtils.h>
#include <ll/api/plugin/NativePlugin.h>
#include <memory>


namespace lse {

namespace {
std::unique_ptr<LegacyScriptEngine> legacyScriptEngine; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)

std::unique_ptr<std::reference_wrapper<ll::plugin::NativePlugin>>
selfPluginInstance; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)

} // namespace

LegacyScriptEngine& getLegacyScriptEngine() { return *legacyScriptEngine; }
auto enable(ll::plugin::NativePlugin& /*self*/) -> bool {
auto& logger = getSelfPluginInstance().getLogger();

logger.info("enabling...");

extern "C" {
_declspec(dllexport) bool ll_plugin_load(ll::plugin::NativePlugin& self) {
legacyScriptEngine = std::make_unique<LegacyScriptEngine>(self);
return true;
}

_declspec(dllexport) bool ll_plugin_enable(ll::plugin::NativePlugin& /*unused*/) {
return legacyScriptEngine->enable();
auto getSelfPluginInstance() -> ll::plugin::NativePlugin& {
if (!selfPluginInstance) {
throw std::runtime_error("selfPluginInstance is null");
}

return *selfPluginInstance;
}

auto loadBaseLib() -> bool;
auto load(ll::plugin::NativePlugin& self) -> bool {
auto& logger = self.getLogger();

try {
logger.info("loading...");

selfPluginInstance = std::make_unique<std::reference_wrapper<ll::plugin::NativePlugin>>(self);

ll::i18n::load(self.getLangDir());

InitLocalShareData();
InitGlobalShareData();
InitSafeGuardRecord();
EconomySystem::init();

loadBaseLib();

// TODO: Load main

LoadDebugEngine();

InitBasicEventListeners();
InitMessageSystem();
MoreGlobal::Init();

return true;

} catch (std::exception& err) {
logger.error("failed to load: {}", err.what());
return false;
}
}

auto loadBaseLib(ll::plugin::NativePlugin& self) -> bool {
auto path = self.getPluginDir() / "BaseLib.lua";

auto content = ll::file_utils::readFile(path);

if (!content) {
throw std::runtime_error("failed to read " + path.string());
}

depends.emplace(path.string(), *content);
}

extern "C" {

_declspec(dllexport) auto ll_plugin_load(ll::plugin::NativePlugin& self) -> bool { return load(self); }

_declspec(dllexport) auto ll_plugin_enable(ll::plugin::NativePlugin& self) -> bool { return enable(self); }

// LegacyScriptEngine should not be disabled or unloaded.
}

Expand Down
4 changes: 2 additions & 2 deletions src/lse/Entry.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include "LegacyScriptEngine.h"
#include <ll/api/plugin/NativePlugin.h>

namespace lse {

[[nodiscard]] LegacyScriptEngine& getLegacyScriptEngine();
[[nodiscard]] auto getSelfPluginInstance() -> ll::plugin::NativePlugin&;

} // namespace lse
120 changes: 60 additions & 60 deletions src/lse/LegacyScriptEngine.cpp
Original file line number Diff line number Diff line change
@@ -1,82 +1,82 @@
#include "LegacyScriptEngine.h"
// #include "LegacyScriptEngine.h"

#include "../api/EventAPI.h"
#include "../api/MoreGlobal.h"
#include "../engine/GlobalShareData.h"
#include "../engine/LocalShareData.h"
#include "../engine/MessageSystem.h"
#include "../main/BuiltinCommands.h"
#include "../main/SafeGuardRecord.h"
// #include "../api/EventAPI.h"
// #include "../api/MoreGlobal.h"
// #include "../engine/GlobalShareData.h"
// #include "../engine/LocalShareData.h"
// #include "../engine/MessageSystem.h"
// #include "../main/BuiltinCommands.h"
// #include "../main/SafeGuardRecord.h"

#include <ll/api/plugin/NativePlugin.h>
// #include <ll/api/plugin/NativePlugin.h>

#ifdef LLSE_BACKEND_PYTHON
#include "../main/PythonHelper.h"
#endif
// #ifdef LLSE_BACKEND_PYTHON
// #include "../main/PythonHelper.h"
// #endif

ll::Logger logger(LLSE_LOADER_NAME);
// ll::Logger logger(LLSE_LOADER_NAME);

extern void LoadDepends();
extern void LoadMain();
extern void BindAPIs(ScriptEngine* engine);
extern void LoadDebugEngine();
// extern void LoadDepends();
// extern void LoadMain();
// extern void BindAPIs(ScriptEngine* engine);
// extern void LoadDebugEngine();

namespace lse {
// namespace lse {

LegacyScriptEngine::LegacyScriptEngine(ll::plugin::NativePlugin& self) : mSelf(self) {
mSelf.getLogger().info("loading...");
// LegacyScriptEngine::LegacyScriptEngine(ll::plugin::NativePlugin& self) : mSelf(self) {
// mSelf.getLogger().info("loading...");

// Code for loading the plugin goes here.
// Load i18n files
ll::i18n::load(u8"plugins/LeviLamina/lang");
// // Code for loading the plugin goes here.
// // Load i18n files
// ll::i18n::load(u8"plugins/LeviLamina/lang");

// Init global share data
InitLocalShareData();
InitGlobalShareData();
InitSafeGuardRecord();
// // Init global share data
// InitLocalShareData();
// InitGlobalShareData();
// InitSafeGuardRecord();

EconomySystem::init();
// EconomySystem::init();

#ifdef LLSE_BACKEND_PYTHON
// This fix is used for Python3.10's bug:
// The thread will freeze when creating a new engine while another thread is
// blocking to read stdin Side effects: sys.stdin cannot be used after this
// patch. More info to see: https://github.com/python/cpython/issues/83526
//
// Attention! When CPython is upgraded, this fix must be re-adapted or
// removed!!
//
PythonHelper::FixPython310Stdin::patchPython310CreateStdio();
// #ifdef LLSE_BACKEND_PYTHON
// // This fix is used for Python3.10's bug:
// // The thread will freeze when creating a new engine while another thread is
// // blocking to read stdin Side effects: sys.stdin cannot be used after this
// // patch. More info to see: https://github.com/python/cpython/issues/83526
// //
// // Attention! When CPython is upgraded, this fix must be re-adapted or
// // removed!!
// //
// PythonHelper::FixPython310Stdin::patchPython310CreateStdio();

PythonHelper::initPythonRuntime();
#endif
// PythonHelper::initPythonRuntime();
// #endif

// Pre-load depending libs
LoadDepends();
// // Pre-load depending libs
// LoadDepends();

// Load plugins
LoadMain();
// // Load plugins
// LoadMain();

// Register real-time debug
LoadDebugEngine();
// // Register real-time debug
// LoadDebugEngine();

// Register basic event listeners
InitBasicEventListeners();
// // Register basic event listeners
// InitBasicEventListeners();

// Init message system
InitMessageSystem();
// // Init message system
// InitMessageSystem();

MoreGlobal::Init();
}
// MoreGlobal::Init();
// }

ll::plugin::NativePlugin& LegacyScriptEngine::getSelf() const { return mSelf; }
// ll::plugin::NativePlugin& LegacyScriptEngine::getSelf() const { return mSelf; }

bool LegacyScriptEngine::enable() {
mSelf.getLogger().info("enabling...");
// bool LegacyScriptEngine::enable() {
// mSelf.getLogger().info("enabling...");

// Code for enabling the plugin goes here.
RegisterDebugCommand();
return true;
}
// // Code for enabling the plugin goes here.
// RegisterDebugCommand();
// return true;
// }

} // namespace lse
// } // namespace lse
26 changes: 0 additions & 26 deletions src/lse/LegacyScriptEngine.h

This file was deleted.

17 changes: 17 additions & 0 deletions src/lse/Macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#ifndef LEGACY_SCRIPT_ENGINE_BACKEND

#error "LEGACY_SCRIPT_ENGINE_BACKEND is not defined!"

#endif

#if LEGACY_SCRIPT_ENGINE_BACKEND == lua

#define LLSE_BACKEND_LUA

#elif LEGACY_SCRIPT_ENGINE_BACKEND == quickjs

#define LLSE_BACKEND_QUICKJS

#endif
Loading

0 comments on commit 871617b

Please sign in to comment.