From 5c249525c9315761fda29da53b4bae6cb913adc6 Mon Sep 17 00:00:00 2001 From: AURUMVORXX Date: Wed, 26 Mar 2025 14:46:17 +0300 Subject: [PATCH] feat: Added proper launch under venv --- CHANGELOG.md | 8 +- python/docs/index.md | 10 ++- python/g2o/classes/packets.py | 2 +- python/g2o/sqg2o/__init__.py | 1 + python/g2o/sqg2oconst/__init__.py | 1 + source/bind.cpp | 4 +- source/bind.h | 6 ++ source/constants/sqconstants.cpp | 145 ------------------------------ source/main.cpp | 27 ++++-- 9 files changed, 45 insertions(+), 159 deletions(-) create mode 100644 python/g2o/sqg2o/__init__.py create mode 100644 python/g2o/sqg2oconst/__init__.py create mode 100644 source/bind.h diff --git a/CHANGELOG.md b/CHANGELOG.md index deb8384..dfff88e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## Changelog -- Renamed entry point module to ``pyg2o_entry`` -- Added option to change entry point name (read more: ) -- Added ability to connect additional package folders (read: use virtual environments), read more: -- Added proper exception handling on the module loading time \ No newline at end of file +- Creating new embedded modules changed for using existing ones (needed for proper launch under venv; reverse compatible change) +- Entry point module now will be added to ``sys.path`` on the server launch +- Server will now throw an exception if entry point module doesn't exist +- Fix for ``Packet.sendToAll`` had incorrect argument list \ No newline at end of file diff --git a/python/docs/index.md b/python/docs/index.md index 9d8a5f8..c0eabfe 100644 --- a/python/docs/index.md +++ b/python/docs/index.md @@ -27,8 +27,16 @@ If you make package that uses PyG2O functions, then this package also should als } ``` -## How to add additional packages folder (use virtual environment) +## How to add additional packages folder You can create ``.pth`` file in your root server folder and it will be added to your ``sys.path`` before entry point +## How to launch under venv +Create and launch ``.bat`` file with the following content +``` +@echo off +CALL /scripts/activate.bat +G2O_Server.x64.exe +``` + ## Examples You can find default (example) scripts in [this repository](https://github.com/AURUMVORXX/PyG2O-DefaultScripts) \ No newline at end of file diff --git a/python/g2o/classes/packets.py b/python/g2o/classes/packets.py index a8877b1..96e9377 100644 --- a/python/g2o/classes/packets.py +++ b/python/g2o/classes/packets.py @@ -35,7 +35,7 @@ class Packet(sqg2o.Packet): **Parameters:** * `int` **reliability**: the reliability type, for more information see [Reliability](../../constants/reliability.md). """ - return super().send(playerid, reliability) + return super().send(reliability) def writeInt8(self, value : int): """ diff --git a/python/g2o/sqg2o/__init__.py b/python/g2o/sqg2o/__init__.py new file mode 100644 index 0000000..8fc5bf4 --- /dev/null +++ b/python/g2o/sqg2o/__init__.py @@ -0,0 +1 @@ +# Have to be empty for C++ to fill it up \ No newline at end of file diff --git a/python/g2o/sqg2oconst/__init__.py b/python/g2o/sqg2oconst/__init__.py new file mode 100644 index 0000000..8fc5bf4 --- /dev/null +++ b/python/g2o/sqg2oconst/__init__.py @@ -0,0 +1 @@ +# Have to be empty for C++ to fill it up \ No newline at end of file diff --git a/source/bind.cpp b/source/bind.cpp index 4a2ded5..833ffe1 100644 --- a/source/bind.cpp +++ b/source/bind.cpp @@ -15,7 +15,9 @@ namespace py = pybind11; -PYBIND11_EMBEDDED_MODULE(sqg2o, m) { +void registerSquirrelObjects() +{ + py::module_ m = py::module_::import("sqg2o"); py::class_(m, "Packet") .def(py::init<>()) diff --git a/source/bind.h b/source/bind.h new file mode 100644 index 0000000..f629a3c --- /dev/null +++ b/source/bind.h @@ -0,0 +1,6 @@ +#ifndef _BINDH_ +#define _BINDH_ + +void registerSquirrelObjects(); + +#endif \ No newline at end of file diff --git a/source/constants/sqconstants.cpp b/source/constants/sqconstants.cpp index ba3a4a0..b891cf9 100644 --- a/source/constants/sqconstants.cpp +++ b/source/constants/sqconstants.cpp @@ -4,154 +4,9 @@ #include "sqconstants.h" namespace py = pybind11; -extern py::module_ g2o; #define GET_CONST(constName, constType) Sqrat::ConstTable().GetSlot(constName).Cast() -// Sadly, I have to split definition and declaration of constants, because pybind doesn't allow embeding modules inside functions -// And in the global scope, Squirrel VM and const table is not yet exist - -PYBIND11_EMBEDDED_MODULE(sqg2oconst, m) { - m.attr("DAMAGE_CTX") = 0; - m.attr("EQUIPMENT_CTX") = 0; - - m.attr("DAMAGE_UNKNOW") = 0; - m.attr("DAMAGE_BARRIER") = 0; - m.attr("DAMAGE_BLUNT") = 0; - m.attr("DAMAGE_EDGE") = 0; - m.attr("DAMAGE_FIRE") = 0; - m.attr("DAMAGE_FLY") = 0; - m.attr("DAMAGE_MAGIC") = 0; - m.attr("DAMAGE_POINT") = 0; - m.attr("DAMAGE_FALL") = 0; - - m.attr("DEBUG_MODE") = false; - m.attr("SERVER_SIDE") = false; - m.attr("CLIENT_SIDE") = false; - - m.attr("HAND_LEFT") = 0; - m.attr("HAND_RIGHT") = 0; - - m.attr("ITEM_CAT_NONE") = 0; - m.attr("ITEM_CAT_NF") = 0; - m.attr("ITEM_CAT_FF") = 0; - m.attr("ITEM_CAT_MUN") = 0; - m.attr("ITEM_CAT_ARMOR") = 0; - m.attr("ITEM_CAT_FOOD") = 0; - m.attr("ITEM_CAT_DOCS") = 0; - m.attr("ITEM_CAT_POTION") = 0; - m.attr("ITEM_CAT_LIGHT") = 0; - m.attr("ITEM_CAT_RUNE") = 0; - m.attr("ITEM_CAT_MAGIC") = 0; - - m.attr("ITEM_FLAG_DAG") = 0; - m.attr("ITEM_FLAG_SWD") = 0; - m.attr("ITEM_FLAG_AXE") = 0; - m.attr("ITEM_FLAG_2HD_SWD") = 0; - m.attr("ITEM_FLAG_2HD_AXE") = 0; - m.attr("ITEM_FLAG_SHIELD") = 0; - m.attr("ITEM_FLAG_BOW") = 0; - m.attr("ITEM_FLAG_CROSSBOW") = 0; - m.attr("ITEM_FLAG_RING") = 0; - m.attr("ITEM_FLAG_AMULET") = 0; - m.attr("ITEM_FLAG_BELT") = 0; - m.attr("ITEM_FLAG_DROPPED") = 0; - m.attr("ITEM_FLAG_MI") = 0; - m.attr("ITEM_FLAG_MULTI") = 0; - m.attr("ITEM_FLAG_NFOCUS") = 0; - m.attr("ITEM_FLAG_CREATEAMMO") = 0; - m.attr("ITEM_FLAG_NSPLIT") = 0; - m.attr("ITEM_FLAG_DRINK") = 0; - m.attr("ITEM_FLAG_TORCH") = 0; - m.attr("ITEM_FLAG_THROW") = 0; - m.attr("ITEM_FLAG_ACTIVE") = 0; - - m.attr("ITEM_WEAR_NO") = 0; - m.attr("ITEM_WEAR_TORSO") = 0; - m.attr("ITEM_WEAR_HEAD") = 0; - m.attr("ITEM_WEAR_LIGHT") = 0; - - m.attr("ATTACK_RUN") = 0; - m.attr("ATTACK_FORWARD") = 0; - m.attr("ATTACK_LEFT") = 0; - m.attr("ATTACK_RIGHT") = 0; - m.attr("ACTION_CLEAR_QUEUE") = 0; - m.attr("ACTION_APPLY_OVERLAY") = 0; - m.attr("ACTION_REMOVE_OVERLAY") = 0; - m.attr("ACTION_PLAY_ANI") = 0; - m.attr("ACTION_STOP_ANI") = 0; - m.attr("ACTION_EQUIP_ITEM") = 0; - m.attr("ACTION_UNEQUIP_ITEM") = 0; - m.attr("ACTION_WEAPON_MODE") = 0; - m.attr("ACTION_DRAW_WEAPON") = 0; - m.attr("ACTION_REMOVE_WEAPON") = 0; - m.attr("ACTION_USE_ITEM") = 0; - m.attr("ACTION_USE_ITEM_TO_STATE") = 0; - m.attr("ACTION_READY_SPELL") = 0; - m.attr("ACTION_UNREADY_SPELL") = 0; - m.attr("ACTION_ATTACK_MELEE_WEAPON") = 0; - m.attr("ACTION_ATTACK_RANGED_WEAPON") = 0; - m.attr("ACTION_SPELL_CAST") = 0; - m.attr("ACTION_USE_MOB_SCHEME") = 0; - m.attr("ACTION_SHOOT_AT") = 0; - m.attr("ACTION_START_AIM_AT") = 0; - m.attr("ACTION_STOP_AIM_AT") = 0; - m.attr("ACTION_SCRIPT") = 0; - - m.attr("UNRELIABLE") = 0; - m.attr("UNRELIABLE_SEQUENCED") = 0; - m.attr("RELIABLE") = 0; - m.attr("RELIABLE_ORDERED") = 0; - m.attr("RELIABLE_SEQUENCED") = 0; - - m.attr("WEAPON_1H") = 0; - m.attr("WEAPON_2H") = 0; - m.attr("WEAPON_BOW") = 0; - m.attr("WEAPON_CBOW") = 0; - - m.attr("TALENT_1H") = 0; - m.attr("TALENT_2H") = 0; - m.attr("TALENT_BOW") = 0; - m.attr("TALENT_CROSSBOW") = 0; - m.attr("TALENT_PICK_LOCKS") = 0; - m.attr("TALENT_PICKPOCKET") = 0; - m.attr("TALENT_MAGE") = 0; - m.attr("TALENT_SNEAK") = 0; - m.attr("TALENT_REGENERATE") = 0; - m.attr("TALENT_FIREMASTER") = 0; - m.attr("TALENT_ACROBATIC") = 0; - m.attr("TALENT_PICKPOCKET_UNUSED") = 0; - m.attr("TALENT_SMITH") = 0; - m.attr("TALENT_RUNES") = 0; - m.attr("TALENT_ALCHEMY") = 0; - m.attr("TALENT_THROPHY") = 0; - m.attr("TALENT_A") = 0; - m.attr("TALENT_B") = 0; - m.attr("TALENT_C") = 0; - m.attr("TALENT_D") = 0; - m.attr("TALENT_E") = 0; - m.attr("TALENT_MAX") = 0; - - m.attr("WEAPONMODE_NONE") = 0; - m.attr("WEAPONMODE_FIST") = 0; - m.attr("WEAPONMODE_DAG") = 0; - m.attr("WEAPONMODE_1HS") = 0; - m.attr("WEAPONMODE_2HS") = 0; - m.attr("WEAPONMODE_BOW") = 0; - m.attr("WEAPONMODE_CBOW") = 0; - m.attr("WEAPONMODE_MAG") = 0; - m.attr("WEAPONMODE_MAX") = 0; - - m.attr("WEATHER_SNOW") = 0; - m.attr("WEATHER_RAIN") = 0; - - m.attr("AC_SPEED_HACK") = 0; - - m.attr("DISCONNECTED") = 0; - m.attr("LOST_CONNECTION") = 0; - m.attr("HAS_CRASHED") = 0; -} - void registerSquirrelConstants() { py::module_ cts = py::module_::import("sqg2oconst"); diff --git a/source/main.cpp b/source/main.cpp index 97f727d..8c91fac 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -3,6 +3,7 @@ #include "sqevents.h" #include "sqconstants.h" #include "squirrel/functions.h" +#include "bind.h" namespace py = pybind11; py::scoped_interpreter guard{}; @@ -16,16 +17,20 @@ extern "C" SQRESULT SQRAT_API sqmodule_load(HSQUIRRELVM vm, HSQAPI api) try { - registerSquirrelConstants(); - + py::dict locals = py::dict(); py::exec(R"( import site import json + import sys import importlib - - venv_path = 'test_venv/Lib/site-packages' + import importlib.util site.addsitedir('.') - + + spec = importlib.util.find_spec("g2o") + if spec is not None: + if spec.submodule_search_locations: + sys.path.append(spec.submodule_search_locations[0]) + entry_point = 'pyg2o_entry' try: @@ -36,12 +41,20 @@ extern "C" SQRESULT SQRAT_API sqmodule_load(HSQUIRRELVM vm, HSQAPI api) pass try: - importlib.import_module(entry_point) + spec = importlib.util.find_spec(entry_point) + if spec is not None: + if spec.submodule_search_locations: + sys.path.append(spec.submodule_search_locations[0]) except Exception as e: print(e) - )"); + )", py::globals(), locals); + registerSquirrelConstants(); + registerSquirrelObjects(); g2o = py::module_::import("g2o"); + + py::module_ importlib = py::module_::import("importlib"); + importlib.attr("import_module")(locals["entry_point"]); } catch (py::error_already_set &e) {