feat: Added proper launch under venv

This commit is contained in:
AURUMVORXX
2025-03-26 14:46:17 +03:00
parent 03dfd1408c
commit 5c249525c9
9 changed files with 45 additions and 159 deletions

View File

@@ -1,6 +1,6 @@
## Changelog ## Changelog
- Renamed entry point module to ``pyg2o_entry`` - Creating new embedded modules changed for using existing ones (needed for proper launch under venv; reverse compatible change)
- Added option to change entry point name (read more: <link>) - Entry point module now will be added to ``sys.path`` on the server launch
- Added ability to connect additional package folders (read: use virtual environments), read more: <link> - Server will now throw an exception if entry point module doesn't exist
- Added proper exception handling on the module loading time - Fix for ``Packet.sendToAll`` had incorrect argument list

View File

@@ -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 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 <path_to_your_venv>/scripts/activate.bat
G2O_Server.x64.exe
```
## Examples ## Examples
You can find default (example) scripts in [this repository](https://github.com/AURUMVORXX/PyG2O-DefaultScripts) You can find default (example) scripts in [this repository](https://github.com/AURUMVORXX/PyG2O-DefaultScripts)

View File

@@ -35,7 +35,7 @@ class Packet(sqg2o.Packet):
**Parameters:** **Parameters:**
* `int` **reliability**: the reliability type, for more information see [Reliability](../../constants/reliability.md). * `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): def writeInt8(self, value : int):
""" """

View File

@@ -0,0 +1 @@
# Have to be empty for C++ to fill it up

View File

@@ -0,0 +1 @@
# Have to be empty for C++ to fill it up

View File

@@ -15,7 +15,9 @@
namespace py = pybind11; namespace py = pybind11;
PYBIND11_EMBEDDED_MODULE(sqg2o, m) { void registerSquirrelObjects()
{
py::module_ m = py::module_::import("sqg2o");
py::class_<PyPacket>(m, "Packet") py::class_<PyPacket>(m, "Packet")
.def(py::init<>()) .def(py::init<>())

6
source/bind.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef _BINDH_
#define _BINDH_
void registerSquirrelObjects();
#endif

View File

@@ -4,154 +4,9 @@
#include "sqconstants.h" #include "sqconstants.h"
namespace py = pybind11; namespace py = pybind11;
extern py::module_ g2o;
#define GET_CONST(constName, constType) Sqrat::ConstTable().GetSlot(constName).Cast<constType>() #define GET_CONST(constName, constType) Sqrat::ConstTable().GetSlot(constName).Cast<constType>()
// 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() void registerSquirrelConstants()
{ {
py::module_ cts = py::module_::import("sqg2oconst"); py::module_ cts = py::module_::import("sqg2oconst");

View File

@@ -3,6 +3,7 @@
#include "sqevents.h" #include "sqevents.h"
#include "sqconstants.h" #include "sqconstants.h"
#include "squirrel/functions.h" #include "squirrel/functions.h"
#include "bind.h"
namespace py = pybind11; namespace py = pybind11;
py::scoped_interpreter guard{}; py::scoped_interpreter guard{};
@@ -16,16 +17,20 @@ extern "C" SQRESULT SQRAT_API sqmodule_load(HSQUIRRELVM vm, HSQAPI api)
try try
{ {
registerSquirrelConstants(); py::dict locals = py::dict();
py::exec(R"( py::exec(R"(
import site import site
import json import json
import sys
import importlib import importlib
import importlib.util
venv_path = 'test_venv/Lib/site-packages'
site.addsitedir('.') 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' entry_point = 'pyg2o_entry'
try: try:
@@ -36,12 +41,20 @@ extern "C" SQRESULT SQRAT_API sqmodule_load(HSQUIRRELVM vm, HSQAPI api)
pass pass
try: 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: except Exception as e:
print(e) print(e)
)"); )", py::globals(), locals);
registerSquirrelConstants();
registerSquirrelObjects();
g2o = py::module_::import("g2o"); 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) catch (py::error_already_set &e)
{ {