feat: version 2.1.0
This commit is contained in:
255
include/main.nut
255
include/main.nut
@@ -1,126 +1,153 @@
|
||||
|
||||
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;
|
||||
_url = url;
|
||||
_reconnect = reconnect;
|
||||
_connection = -1;
|
||||
_silent = false;
|
||||
_url = -1;
|
||||
_reconnect_attempts = 0;
|
||||
_max_reconnect_attempts = 0;
|
||||
|
||||
_pyg2o_server_connection = WebsocketClient();
|
||||
_pyg2o_server_connection.silent = silent;
|
||||
_pyg2o_server_connection.setUrl(url);
|
||||
_pyg2o_server_connection.start();
|
||||
_constantsInitialized = false;
|
||||
_messageHandlers = null;
|
||||
|
||||
if (!_silent)
|
||||
print("[PyG2O] Initializing connection on " + url)
|
||||
}
|
||||
|
||||
function _PyG2O_Send(data)
|
||||
{
|
||||
if (_pyg2o_server_connection == -1)
|
||||
return;
|
||||
|
||||
_pyg2o_server_connection.send(JSON.dump_ansi(data));
|
||||
}
|
||||
|
||||
function _PyG2O_InitializeConstants()
|
||||
{
|
||||
local const_data = {
|
||||
"type": "const_init",
|
||||
"args": getconsttable()
|
||||
};
|
||||
|
||||
_PyG2O_Send(const_data);
|
||||
}
|
||||
|
||||
function _PyG2O_GetClassName(object)
|
||||
{
|
||||
if (object instanceof DamageDescription)
|
||||
return "DamageDescription";
|
||||
else if (object instanceof ItemGround)
|
||||
return "ItemGround";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function _PyG2O_Serialize(object)
|
||||
{
|
||||
local cls = object.getclass();
|
||||
local tab = {};
|
||||
|
||||
if (object instanceof DamageDescription)
|
||||
constructor(url, silent = false, max_reconnect_attempts = 0)
|
||||
{
|
||||
tab["_flags"] <- object.flags;
|
||||
tab["_damage"] <- object.damage;
|
||||
tab["_item_instance"] <- object.item_instance;
|
||||
tab["_distance"] <- object.distance;
|
||||
tab["_spell_id"] <- object.spell_id;
|
||||
tab["_spell_level"] <- object.spell_level;
|
||||
tab["_node"] <- object.node;
|
||||
}
|
||||
else if (object instanceof ItemGround)
|
||||
{
|
||||
tab["_id"] <- object.id;
|
||||
tab["_instance"] <- object.instance;
|
||||
tab["_amount"] <- object.amount;
|
||||
tab["_world"] <- object.world;
|
||||
tab["_virtualWorld"] <- object.virtualWorld;
|
||||
tab["_position"] <- object.getPosition();
|
||||
tab["_rotation"] <- object.getRotation();
|
||||
_url = url;
|
||||
_max_reconnect_attempts = max_reconnect_attempts;
|
||||
_silent = silent;
|
||||
_messageHandlers = {};
|
||||
|
||||
_connection = WebsocketClient();
|
||||
_connection.silent = _silent;
|
||||
_connection.setUrl(_url);
|
||||
|
||||
_connection.onOpen = _onOpen.bindenv(this);
|
||||
_connection.onClose = _onClose.bindenv(this);
|
||||
_connection.onMessage = _onMessage.bindenv(this);
|
||||
|
||||
_registerMessage("call", _message_call.bindenv(this));
|
||||
|
||||
if (_globalInstance == -1)
|
||||
_globalInstance = this;
|
||||
}
|
||||
|
||||
return tab;
|
||||
}
|
||||
|
||||
addEventHandler("onWebsocketConnect", function(socket, url)
|
||||
{
|
||||
if (socket != _pyg2o_server_connection)
|
||||
return;
|
||||
|
||||
_PyG2O_InitializeConstants();
|
||||
if (!_silent)
|
||||
print("[PyG2O] Successfully connected to " + url);
|
||||
});
|
||||
|
||||
addEventHandler("onWebsocketClose", function(socket, url, message)
|
||||
{
|
||||
if (socket != _pyg2o_server_connection || _reconnect)
|
||||
return;
|
||||
|
||||
_pyg2o_server_connection = -1;
|
||||
});
|
||||
|
||||
addEventHandler("onWebsocketMessage", function(socket, url, message)
|
||||
{
|
||||
if (socket != _pyg2o_server_connection)
|
||||
return;
|
||||
|
||||
local request = JSON.parse_ansi(message);
|
||||
if ("uuid" in request)
|
||||
function start()
|
||||
{
|
||||
local result = compilestring(request["args"])();
|
||||
local className = _PyG2O_GetClassName(result);
|
||||
if (className != null)
|
||||
{
|
||||
className = format("obj_%s", className)
|
||||
_connection.start();
|
||||
if (_connection.running)
|
||||
print("[PyG2O] Initializing connection on " + _url);
|
||||
}
|
||||
|
||||
request["args"] = {};
|
||||
request["args"].rawset(className, {});
|
||||
local objTab = request["args"].rawget(className);
|
||||
objTab["name"] <- "result";
|
||||
objTab["data"] <- _PyG2O_Serialize(result);
|
||||
function stop()
|
||||
{
|
||||
_connection.stop();
|
||||
if (!_connection.running)
|
||||
print("[PyG2O] Stopped connection");
|
||||
}
|
||||
|
||||
function _registerMessage(type, handler)
|
||||
{
|
||||
if (type in _messageHandlers)
|
||||
return;
|
||||
|
||||
_messageHandlers[type] <- handler;
|
||||
}
|
||||
|
||||
function _callMessage(type, data)
|
||||
{
|
||||
if(!(type in _messageHandlers))
|
||||
return;
|
||||
|
||||
_messageHandlers[type](data);
|
||||
}
|
||||
|
||||
function _send(type, data, uuid = "none")
|
||||
{
|
||||
local sendData = {
|
||||
"type": type,
|
||||
"data": data,
|
||||
"uuid": uuid
|
||||
}
|
||||
else
|
||||
{
|
||||
request["args"] =
|
||||
{
|
||||
"result": result
|
||||
};
|
||||
}
|
||||
_pyg2o_server_connection.send(JSON.dump_ansi(request, 2));
|
||||
|
||||
_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 _serializeObject(object)
|
||||
{
|
||||
local cls = object.getclass();
|
||||
local tab = {};
|
||||
|
||||
if (object instanceof DamageDescription)
|
||||
{
|
||||
tab["_flags"] <- object.flags;
|
||||
tab["_damage"] <- object.damage;
|
||||
tab["_item_instance"] <- object.item_instance;
|
||||
tab["_distance"] <- object.distance;
|
||||
tab["_spell_id"] <- object.spell_id;
|
||||
tab["_spell_level"] <- object.spell_level;
|
||||
tab["_node"] <- object.node;
|
||||
}
|
||||
else if (object instanceof ItemGround)
|
||||
{
|
||||
tab["_id"] <- object.id;
|
||||
tab["_instance"] <- object.instance;
|
||||
tab["_amount"] <- object.amount;
|
||||
tab["_world"] <- object.world;
|
||||
tab["_virtualWorld"] <- object.virtualWorld;
|
||||
tab["_position"] <- object.getPosition();
|
||||
tab["_rotation"] <- object.getRotation();
|
||||
}
|
||||
|
||||
return tab;
|
||||
}
|
||||
|
||||
function _onOpen(url)
|
||||
{
|
||||
_initializeConstants();
|
||||
if (!_silent)
|
||||
print("[PyG2O] Successfully connected to " + url);
|
||||
}
|
||||
|
||||
function _onClose(url, message)
|
||||
{
|
||||
if (_max_reconnect_attempts == 0)
|
||||
return;
|
||||
|
||||
_reconnect_attempts += 1;
|
||||
if (_reconnect_attempts < _max_reconnect_attempts)
|
||||
return;
|
||||
|
||||
_connection.stop();
|
||||
}
|
||||
|
||||
function _onMessage(url, message)
|
||||
{
|
||||
local request = JSON.parse_ansi(message);
|
||||
if (!("type" in request) ||
|
||||
!("uuid" in request) ||
|
||||
!("data" in request))
|
||||
return;
|
||||
|
||||
_callMessage(request["type"], request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user