feat: version 2.1.0

This commit is contained in:
AURUMVORXX
2025-04-22 17:26:54 +03:00
parent decd60070c
commit 2a0dc6245f
6 changed files with 448 additions and 417 deletions

View File

@@ -10,14 +10,15 @@ pip install git+https://github.com/AURUMVORXX/PyG2O.git
```
3. Launch websocket client in your Squirrel scripts
```
// PyG2O_Start(url, reconnect, silent)
// reconnect - auto reconnect if server stopped
// PyG2O(url, silent, max_reconnect_attempts)
// silent - disable information prints
// max_reconnect_attempts - maximum reconnect attempts if server will stop the connection (0 - infinite attempts). This value doesn't reset on connection
// Start server before any events
PyG2O_Start("ws://localhost:8080", true, false)
addEventHandler("onInit"...
// Start server
local srv = PyG2O("ws://localhost:8080", true, 15)
srv.start()
// Stop server
srv.stop()
```
4. In your application, launch asyncio event loop and websocket server
```python

View File

@@ -3,20 +3,18 @@ addEventHandler("onPlayerUseCheat", function(playerid, type)
{
local data = {
event = "onPlayerUseCheat",
args = {
playerid = playerid,
type = type
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onBan", function(banInfo)
{
local data = {
event = "onBan",
args = {
ban = {
mac = "mac" in banInfo ? banInfo["mac"] : "-1",
ip = "ip" in banInfo ? banInfo["ip"] : "-1",
@@ -25,50 +23,48 @@ addEventHandler("onBan", function(banInfo)
timestamp = "timestamp" in banInfo ? banInfo["timestamp"] : -1
}
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onExit", function()
{
local data = {
event = "onExit",
args = {}
event = "onExit"
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onTick", function()
{
local data = {
event = "onTick",
args = {}
event = "onTick"
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onTime", function(day, hour, min)
{
local data = {
event = "onTime",
args = {
day = day,
hour = hour,
min = min,
}
min = min
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onUnban", function(banInfo)
{
local data = {
event = "onUnban",
args = {
ban = {
mac = "mac" in banInfo ? banInfo["mac"] : "-1",
ip = "ip" in banInfo ? banInfo["ip"] : "-1",
@@ -77,540 +73,501 @@ addEventHandler("onUnban", function(banInfo)
timestamp = "timestamp" in banInfo ? banInfo["timestamp"] : -1
}
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onNpcActionFinished", function(npc_id, action_type, action_id, result)
{
local data = {
event = "onNpcActionFinished",
args = {
npc_id = npc_id,
action_type = action_type,
action_id = action_id,
result = result
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onNpcActionSent", function(npc_id, action_type, action_id)
{
local data = {
event = "onNpcActionSent",
args = {
npc_id = npc_id,
action_type = action_type,
action_id = action_id,
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onNpcChangeHostPlayer", function(npc_id, current_id, previous_id)
{
local data = {
event = "onNpcChangeHostPlayer",
args = {
npc_id = npc_id,
current_id = current_id,
previous_id = previous_id
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onNpcCreated", function(npc_id)
{
local data = {
event = "onNpcCreated",
args = {
npc_id = npc_id
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onNpcDestroyed", function(npc_id)
{
local data = {
event = "onNpcDestroyed",
args = {
event = "onNpcCreated",
npc_id = npc_id
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerChangeColor", function(playerid, r, g, b)
{
local data = {
event = "onPlayerChangeColor",
args = {
playerid = playerid,
r = r,
g = g,
b = b
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerChangeFocus", function(playerid, oldFocusId, newFocusId)
{
local data = {
event = "onPlayerChangeFocus",
args = {
playerid = playerid,
oldFocusId = oldFocusId,
newFocusId = newFocusId
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerChangeHealth", function(playerid, previous, current)
{
local data = {
event = "onPlayerChangeHealth",
args = {
playerid = playerid,
previous = previous,
current = current
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerChangeMana", function(playerid, previous, current)
{
local data = {
event = "onPlayerChangeMana",
args = {
playerid = playerid,
previous = previous,
current = current
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerChangeMaxHealth", function(playerid, previous, current)
{
local data = {
event = "onPlayerChangeMaxHealth",
args = {
playerid = playerid,
previous = previous,
current = current
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerChangeMaxMana", function(playerid, previous, current)
{
local data = {
event = "onPlayerChangeMaxMana",
args = {
playerid = playerid,
previous = previous,
current = current
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerChangeWeaponMode", function(playerid, previous, current)
{
local data = {
event = "onPlayerChangeWeaponMode",
args = {
playerid = playerid,
previous = previous,
current = current
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerChangeWorld", function(playerid, previous, current)
{
local data = {
event = "onPlayerChangeWorld",
args = {
playerid = playerid,
previous = previous,
current = current
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerCommand", function(playerid, command, params)
{
local data = {
event = "onPlayerCommand",
args = {
playerid = playerid,
command = command,
params = params
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerDamage", function(playerid, killerid, description)
{
local data = {
event = "onPlayerDamage",
args = {
playerid = playerid,
killerid = killerid,
obj_DamageDescription = {
name = "desc",
data = _PyG2O_Serialize(description)
}
desc = {
obj_name = "DamageDescription",
obj_data = _globalInstance._serializeObject(description)
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerDead", function(playerid, killerid)
{
local data = {
event = "onPlayerDead",
args = {
playerid = playerid,
killerid = killerid
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerDisconnect", function(playerid, reason)
{
local data = {
event = "onPlayerDisconnect",
args = {
playerid = playerid,
reason = reason
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerDropItem", function(playerid, itemGround)
{
local data = {
event = "onPlayerDropItem",
args = {
playerid = playerid,
obj_ItemGround = {
name = "itemGround",
data = _PyG2O_Serialize(itemGround)
}
itemGround = {
obj_name = "ItemGround",
obj_data = _globalInstance._serializeObject(itemGround)
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerEnterWorld", function(playerid, world)
{
local data = {
event = "onPlayerEnterWorld",
args = {
playerid = playerid,
world = world
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerEquipAmulet", function(playerid, instance)
{
local data = {
event = "onPlayerEquipAmulet",
args = {
playerid = playerid,
instance = instance
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerEquipArmor", function(playerid, instance)
{
local data = {
event = "onPlayerEquipArmor",
args = {
playerid = playerid,
instance = instance
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerEquipBelt", function(playerid, instance)
{
local data = {
event = "onPlayerEquipBelt",
args = {
playerid = playerid,
instance = instance
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerEquipHandItem", function(playerid, hand, instance)
{
local data = {
event = "onPlayerEquipHandItem",
args = {
playerid = playerid,
hand = hand,
instance = instance
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerEquipHelmet", function(playerid, instance)
{
local data = {
event = "onPlayerEquipHelmet",
args = {
playerid = playerid,
instance = instance
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerEquipMeleeWeapon", function(playerid, instance)
{
local data = {
event = "onPlayerEquipMeleeWeapon",
args = {
playerid = playerid,
instance = instance
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerEquipRangedWeapon", function(playerid, instance)
{
local data = {
event = "onPlayerEquipRangedWeapon",
args = {
playerid = playerid,
instance = instance
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerEquipRing", function(playerid, handId, instance)
{
local data = {
event = "onPlayerEquipRing",
args = {
playerid = playerid,
handId = handId,
instance = instance
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerEquipShield", function(playerid, instance)
{
local data = {
event = "onPlayerEquipShield",
args = {
playerid = playerid,
instance = instance
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerEquipSpell", function(playerid, slotId, instance)
{
local data = {
event = "onPlayerEquipSpell",
args = {
playerid = playerid,
slotId = slotId,
instance = instance
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerJoin", function(playerid)
{
local data = {
event = "onPlayerJoin",
args = {
playerid = playerid
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerMessage", function(playerid, message)
{
local data = {
event = "onPlayerMessage",
args = {
playerid = playerid,
message = message
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerMobInteract", function(playerid, from, to)
{
local data = {
event = "onPlayerMobInteract",
args = {
playerid = playerid,
from = from,
to = to
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerRespawn", function(playerid)
{
local data = {
event = "onPlayerRespawn",
args = {
playerid = playerid
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerShoot", function(playerid, munition)
{
local data = {
event = "onPlayerShoot",
args = {
playerid = playerid,
munition = munition
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerSpellCast", function(playerid, instance, spellLevel)
{
local data = {
event = "onPlayerSpellCast",
args = {
playerid = playerid,
instance = instance,
spellLevel = spellLevel
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerSpellSetup", function(playerid, instance)
{
local data = {
event = "onPlayerSpellSetup",
args = {
playerid = playerid,
instance = instance
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerTakeItem", function(playerid, itemGround)
{
local data = {
event = "onPlayerTakeItem",
args = {
playerid = playerid,
obj_ItemGround = {
name = "itemGround",
data = _PyG2O_Serialize(itemGround)
}
itemGround = {
obj_name = "ItemGround",
obj_data = _globalInstance._serializeObject(itemGround)
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerTeleport", function(playerid, vobName)
{
local data = {
event = "onPlayerTeleport",
args = {
playerid = playerid,
vobName = vobName
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});
addEventHandler("onPlayerToggleFaceAni", function(playerid, aniName, toggle)
{
local data = {
event = "onPlayerToggleFaceAni",
args = {
playerid = playerid,
aniName = aniName,
toggle = toggle
}
}
_PyG2O_Send(data);
if (_globalInstance != -1)
_globalInstance._send("event", data);
});

View File

@@ -1,54 +1,99 @@
local _pyg2o_server_connection = -1;
local _silent = false;
local _url = -1
local _reconnect = false;
_globalInstance <- -1;
function PyG2O_Start(url, reconnect = false, silent = false)
class PyG2O
{
_silent = silent;
_connection = -1;
_silent = false;
_url = -1;
_reconnect_attempts = 0;
_max_reconnect_attempts = 0;
_constantsInitialized = false;
_messageHandlers = null;
constructor(url, silent = false, max_reconnect_attempts = 0)
{
_url = url;
_reconnect = reconnect;
_max_reconnect_attempts = max_reconnect_attempts;
_silent = silent;
_messageHandlers = {};
_pyg2o_server_connection = WebsocketClient();
_pyg2o_server_connection.silent = silent;
_pyg2o_server_connection.setUrl(url);
_pyg2o_server_connection.start();
_connection = WebsocketClient();
_connection.silent = _silent;
_connection.setUrl(_url);
if (!_silent)
print("[PyG2O] Initializing connection on " + url)
}
_connection.onOpen = _onOpen.bindenv(this);
_connection.onClose = _onClose.bindenv(this);
_connection.onMessage = _onMessage.bindenv(this);
function _PyG2O_Send(data)
{
if (_pyg2o_server_connection == -1)
_registerMessage("call", _message_call.bindenv(this));
if (_globalInstance == -1)
_globalInstance = this;
}
function start()
{
_connection.start();
if (_connection.running)
print("[PyG2O] Initializing connection on " + _url);
}
function stop()
{
_connection.stop();
if (!_connection.running)
print("[PyG2O] Stopped connection");
}
function _registerMessage(type, handler)
{
if (type in _messageHandlers)
return;
_pyg2o_server_connection.send(JSON.dump_ansi(data));
}
_messageHandlers[type] <- handler;
}
function _PyG2O_InitializeConstants()
{
local const_data = {
"type": "const_init",
"args": getconsttable()
};
function _callMessage(type, data)
{
if(!(type in _messageHandlers))
return;
_PyG2O_Send(const_data);
}
_messageHandlers[type](data);
}
function _PyG2O_GetClassName(object)
{
function _send(type, data, uuid = "none")
{
local sendData = {
"type": type,
"data": data,
"uuid": uuid
}
_connection.send(JSON.dump_ansi(sendData, 2));
}
function _initializeConstants()
{
if (_constantsInitialized)
return;
_send("init_constants", getconsttable());
}
function _getClassName(object)
{
if (object instanceof DamageDescription)
return "DamageDescription";
else if (object instanceof ItemGround)
return "ItemGround";
return null;
}
}
function _PyG2O_Serialize(object)
{
function _serializeObject(object)
{
local cls = object.getclass();
local tab = {};
@@ -74,53 +119,35 @@ function _PyG2O_Serialize(object)
}
return tab;
}
}
addEventHandler("onWebsocketConnect", function(socket, url)
{
if (socket != _pyg2o_server_connection)
return;
_PyG2O_InitializeConstants();
function _onOpen(url)
{
_initializeConstants();
if (!_silent)
print("[PyG2O] Successfully connected to " + url);
});
}
addEventHandler("onWebsocketClose", function(socket, url, message)
{
if (socket != _pyg2o_server_connection || _reconnect)
function _onClose(url, message)
{
if (_max_reconnect_attempts == 0)
return;
_pyg2o_server_connection = -1;
});
addEventHandler("onWebsocketMessage", function(socket, url, message)
{
if (socket != _pyg2o_server_connection)
_reconnect_attempts += 1;
if (_reconnect_attempts < _max_reconnect_attempts)
return;
_connection.stop();
}
function _onMessage(url, message)
{
local request = JSON.parse_ansi(message);
if ("uuid" in request)
{
local result = compilestring(request["args"])();
local className = _PyG2O_GetClassName(result);
if (className != null)
{
className = format("obj_%s", className)
if (!("type" in request) ||
!("uuid" in request) ||
!("data" in request))
return;
request["args"] = {};
request["args"].rawset(className, {});
local objTab = request["args"].rawget(className);
objTab["name"] <- "result";
objTab["data"] <- _PyG2O_Serialize(result);
_callMessage(request["type"], request);
}
else
{
request["args"] =
{
"result": result
};
}
_pyg2o_server_connection.send(JSON.dump_ansi(request, 2));
}
});
}

16
include/messages.nut Normal file
View File

@@ -0,0 +1,16 @@
function _message_call(data)
{
local result = compilestring(data["data"])();
local className = _getClassName(result);
if (className != null)
{
data["data"] = {};
data["data"]["obj_name"] <- className;
data["data"]["obj_data"] <- _serializeObject(result);
}
else
data["data"] = result;
_send("result", data["data"], data["uuid"]);
}

View File

@@ -1,4 +1,5 @@
<server>
<script src="messages.nut" type="server" />
<script src="main.nut" type="server" />
<script src="events.nut" type="server" />
</server>

View File

@@ -20,14 +20,31 @@ class PythonWebsocketServer:
self.silent = silent
self.logger = logger if logger is not None else logging.root
self.requests_list: dict[str, asyncio.Future] = dict()
self._messageHandlers: dict[str, callable] = dict()
self._requests_list: dict[str, asyncio.Future] = dict()
self._stop_event: asyncio.Event = asyncio.Event()
self.connected_socket: Optional[websockets.ClientConnection] = None
self._connected_socket: Optional[websockets.ClientConnection] = None
self._registerMessage('event', self._message_event)
self._registerMessage('init_constants', self._message_init_constants)
self._registerMessage('result', self._message_call_result)
@classmethod
async def get_server(cls):
return cls._current_server
def _registerMessage(self, type: str, handler: callable):
if type in self._messageHandlers:
return
self._messageHandlers[type] = handler
async def _callMessage(self, type: str, data: dict):
if type not in self._messageHandlers:
return
await self._messageHandlers[type](data)
async def start(self):
async with websockets.serve(
self.handle_connection,
@@ -42,40 +59,75 @@ class PythonWebsocketServer:
async def stop(self):
PythonWebsocketServer._current_server = None
self.connected_socket = None
self._connected_socket = None
self._stop_event.set()
async def make_request(self, data: str):
if (self.connected_socket is None):
if (self._connected_socket is None):
return None
request_id = str(uuid.uuid4())
self.requests_list[request_id] = asyncio.get_running_loop().create_future()
self._requests_list[request_id] = asyncio.get_running_loop().create_future()
request = {
'type': 'call',
'uuid': request_id,
'args': data,
'data': data,
}
request = json.dumps(request)
request = request.replace("'", '\\"')
formatted_request = request.replace("'", '\\"')
formatted_request = formatted_request.replace("False", 'false')
formatted_request = formatted_request.replace("True", 'true')
await self.connected_socket.send(formatted_request)
await self._connected_socket.send(request)
result = await asyncio.wait_for(
self.requests_list[request_id],
self._requests_list[request_id],
timeout=30
)
return result
async def _message_event(self, data: dict):
if (not isinstance(data['data'], dict) or
'event' not in data['data']):
return
eventName = data['data']['event']
del data['data']['event']
if 'desc' in data['data']:
obj_name = data['data']['desc']['obj_name']
obj_data = data['data']['desc']['obj_data']
data['data']['desc'] = _deserialize(obj_name, obj_data)
elif 'itemGround' in data['data']:
obj_name = data['data']['itemGround']['obj_name']
obj_data = data['data']['itemGround']['obj_data']
data['data']['itemGround'] = _deserialize(obj_name, obj_data)
asyncio.create_task(callEvent(eventName, **data['data']))
async def _message_init_constants(self, data: dict):
if data['data'] is not dict:
return
Constant._update(data['data'])
async def _message_call_result(self, data: dict):
if data['uuid'] not in self._requests_list:
return
result = data['data']
if (isinstance(data['data'], dict) and
'obj_name' in data['data'] and
'obj_data' in data['data']):
result = _deserialize(result['obj_name'], result['obj_data'])
self._requests_list[data['uuid']].set_result(result)
del self._requests_list[data['uuid']]
async def handle_connection(self, websocket: websockets.ClientConnection):
if (websocket.remote_address[0] not in self.whitelist or self.connected_socket is not None):
if (websocket.remote_address[0] not in self.whitelist or self._connected_socket is not None):
await websocket.close(4000, 'Connection denied')
return
self.connected_socket = websocket
self._connected_socket = websocket
self.is_connected = websocket
if (not self.silent):
self.logger.info(f'Client connected: {websocket.remote_address}')
@@ -86,35 +138,12 @@ class PythonWebsocketServer:
async for message in websocket:
message_json = json.loads(message)
if ('type' not in message_json or
'uuid' not in message_json or
'data' not in message_json):
return
# Deserializing objects
if ('args' in message_json):
formatted_args = dict()
for key, value in message_json['args'].items():
if not key.startswith('obj_'):
formatted_args[key] = value
continue
formatted_args[value['name']] = _deserialize(key, value['data'])
message_json['args'] = formatted_args
# Processing events
if ('event' in message_json):
asyncio.create_task(callEvent(message_json['event'], **formatted_args))
continue
# Processing requests from Squirrel side
if ('type' in message_json):
Constant._update(message_json['args'])
continue
# Processing made requests
if (
'uuid' in message_json and
message_json['uuid'] in self.requests_list.keys()
):
self.requests_list[message_json['uuid']].set_result(next(iter(message_json['args'].values())))
await self._callMessage(message_json['type'], message_json)
except json.JSONDecodeError as e:
self.logger.exception(f'[PyG2O] JSON Exception: {e}')
@@ -124,5 +153,5 @@ class PythonWebsocketServer:
if (not self.silent):
self.logger.info('Client disconnected')
self.is_connected = None
self.connected_socket = None
self._connected_socket = None
asyncio.create_task(callEvent('onWebsocketDisconnect', **{}))