added escape sequences to arbitrary strings; reworked callback system

This commit is contained in:
Redo 2019-04-12 23:58:04 -05:00
parent d47e6a50dc
commit 518deaca68
4 changed files with 190 additions and 152 deletions

View File

@ -1,11 +1,11 @@
return function(gate, argv) return function(gate, argv)
if #argv == 1 then if #argv == 1 then
gate.ports[1]:setstate(toboolean(argv[1])) gate.ports[1]:setstate(toboolean(argv[1]))
gate.ports[2]:setstate(toboolean(argv[1])) gate.ports[2]:setstate(toboolean(argv[1]))
else else
gate.ports[1]:setstate(not gate.ports[1].state) gate.ports[1]:setstate(not gate.ports[1].state)
gate.ports[2]:setstate(not gate.ports[2].state) gate.ports[2]:setstate(not gate.ports[2].state)
end end
gate:cb("1\t" .. bool_to_int[gate.ports[1].state]) gate:cb(bool_to_int[gate.ports[1].state])
end end

View File

@ -121,7 +121,7 @@ function llr(){
deleteVariables("$LuaLogic*"); deleteVariables("$LuaLogic*");
$LuaLogic::Path = %path; $LuaLogic::Path = %path;
resetAllOpCallFunc(); //resetAllOpCallFunc();
exec("./lualogic.cs"); exec("./lualogic.cs");
schedule(3000, 0, lualogic_connect, 25000); schedule(3000, 0, lualogic_connect, 25000);
} }

View File

@ -1,125 +1,122 @@
function LuaLogicTCP::sendData(%this) function LuaLogicTCP::sendData(%this)
{ {
cancel(%this.lualogicTick); cancel(%this.lualogicTick);
%this.lualogicTick = %this.schedule(31, "sendData"); %this.lualogicTick = %this.schedule(31, "sendData");
if(%this.data !$= "") if(%this.data !$= "")
{ {
%data = %this.data; %data = %this.data;
while(strpos(%data, ";;") != -1) while(strpos(%data, ";;") != -1)
%data = strReplace(%data, ";;", "; ;"); %data = strReplace(%data, ";;", "; ;");
%this.send(%data @ "\n"); %this.send(%data @ "\n");
%this.data = ""; %this.data = "";
} }
} }
function LuaLogicTCP::onConnected(%this) function LuaLogicTCP::onConnected(%this)
{ {
lualogic_print("tcp connected"); lualogic_print("tcp connected");
%this.data = ""; %this.data = "";
%this.sendData(); %this.sendData();
%this.isConnected = true; %this.isConnected = true;
lualogic_sendoptions(); lualogic_sendoptions();
lualogic_sendgatedefinitions(); lualogic_sendgatedefinitions();
lualogic_sendall(); lualogic_sendall();
} }
function LuaLogicTCP::onLine(%this, %line) function LuaLogicTCP::onLine(%this, %line)
{ {
%cmd = getField(%line, 0); %cmd = getField(%line, 0);
switch$(%cmd) switch$(%cmd)
{ {
case "WU": case "WU":
%state = getField(%line, 1)|0; %state = getField(%line, 1)|0;
%count = getFieldCount(%line); %count = getFieldCount(%line);
if(%state) if(%state)
{ {
for(%i = 2; %i < %count; %i++) for(%i = 2; %i < %count; %i++)
{ {
%brick = getField(%line, %i); %brick = getField(%line, %i);
if(isObject(%brick)) if(isObject(%brick))
%brick.setColorFX(3); %brick.setColorFX(3);
} }
} }
else else
{ {
for(%i = 2; %i < %count; %i++) for(%i = 2; %i < %count; %i++)
{ {
%brick = getField(%line, %i); %brick = getField(%line, %i);
if(isObject(%brick)) if(isObject(%brick))
%brick.setColorFX(0); %brick.setColorFX(0);
} }
} }
case "TPS": case "TPS":
%tz = getField(%line, 1); %tz = getField(%line, 1);
%count = ClientGroup.getCount(); %count = ClientGroup.getCount();
for(%i = 0; %i < %count; %i++) for(%i = 0; %i < %count; %i++)
{ {
%client = ClientGroup.getObject(%i); %client = ClientGroup.getObject(%i);
if(%client.logicLTR) if(%client.logicLTR)
commandToClient(%client, 'bottomPrint', "\c3Logic Tick Rate\c6: " @ %tz, 2, 1); commandToClient(%client, 'bottomPrint', "\c3Logic Tick Rate\c6: " @ %tz, 2, 1);
} }
case "GINFO": case "GINFO":
%client = getField(%line, 1); %client = getField(%line, 1);
if(isObject(%client)) if(isObject(%client))
{ {
%info = getField(%line, 2); %info = getField(%line, 2);
%info = strReplace(%info, "\\c0", "\c0"); %info = strReplace(%info, "\\c0", "\c0");
%info = strReplace(%info, "\\c2", "\c2"); %info = strReplace(%info, "\\c2", "\c2");
%info = strReplace(%info, "\\c5", "\c5"); %info = strReplace(%info, "\\c5", "\c5");
%client.centerPrint(%info, 5); %client.centerPrint(%info, 5);
} }
case "SINFO": case "SINFO":
if(isObject(%client = getField(%line, 1))) if(isObject(%client = getField(%line, 1)))
{ {
%wires = getField(%line, 2); %wires = getField(%line, 2);
%gates = getField(%line, 3); %gates = getField(%line, 3);
%inports = getField(%line, 4); %inports = getField(%line, 4);
%outports = getField(%line, 5); %outports = getField(%line, 5);
messageClient(%client, '', '\c3Wires\c6: %1', %wires); messageClient(%client, '', '\c3Wires\c6: %1', %wires);
messageClient(%client, '', '\c3Gates\c6: %1', %gates); messageClient(%client, '', '\c3Gates\c6: %1', %gates);
messageClient(%client, '', '\c3Ports\c6: %1 inputs | %2 outputs (%3 total)', %inports, %outports, %inports + %outports); messageClient(%client, '', '\c3Ports\c6: %1 inputs | %2 outputs (%3 total)', %inports, %outports, %inports + %outports);
} }
case "CB": case "CB":
%data = getFields(%line, 1, getFieldCount(%line)); %data = getFields(%line, 1, getFieldCount(%line));
%data = nextToken(%data, brick, "\t"); %data = nextToken(%data, brick, "\t");
while(%brick !$= "") while(%brick !$= ""){
{ %data = nextToken(%data, argc, "\t");
%data = nextToken(%data, argc, "\t");
if(%argc > 0) for(%i=0; %i<%argc; %i++){
{ %data = nextToken(%data, av, "\t");
%data = nextToken(%data, args, "\t"); %argv[%i] = lualogic_collapseescape(%av);
for(%i = 1; %i < %argc; %i++) //talk("argv[" @ %i @ "] = " @ %argv[%i]);
{ }
%data = nextToken(%data, arg, "\t");
%args = %args TAB %arg; if(isObject(%brick)){
} %brick.getDatablock().LuaLogic_Callback(%brick, %argv0, %argv1, %argv2, %argv3, %argv4, %argv5, %argv6, %argv7, %argv8, %argv9);
} }
if(isObject(%brick)) %data = nextToken(%data, brick, "\t");
%brick.getDatablock().LuaLogic_Callback(%brick, %args); }
case "TEST":
%data = nextToken(%data, brick, "\t"); talk("Time: " @ getField(%line, 1));
} }
case "TEST": }
talk("Time: " @ getField(%line, 1));
} function LuaLogicTCP::onConnectFailed(%this)
} {
lualogic_print("tcp failed to connect");
function LuaLogicTCP::onConnectFailed(%this) }
{
lualogic_print("tcp failed to connect"); function LuaLogicTCP::onDisconnect(%this)
} {
lualogic_print("tcp disconnected");
function LuaLogicTCP::onDisconnect(%this) %this.isConnected = false;
{ cancel(%this.lualogicTick);
lualogic_print("tcp disconnected"); }
%this.isConnected = false;
cancel(%this.lualogicTick);
}

View File

@ -1,15 +1,4 @@
function lualogic_escapelogicfunction(%text){
%text = strReplace(%text, "\\", "\\/");
%text = strReplace(%text, "\r", "\\r");
%text = strReplace(%text, "\n", "\\n");
%text = strReplace(%text, "\t", "\\t");
%text = strReplace(%text, ";" , "\\:");
return %text;
}
function lualogic_registergatedefinition(%data){ function lualogic_registergatedefinition(%data){
//lualogic_registergatedefinition_auto(%data); //lualogic_registergatedefinition_auto(%data);
@ -36,9 +25,9 @@ function lualogic_registergatedefinition_auto(%data)
%def = %id @ ";" @ %def = %id @ ";" @
%data.logicUIName @ ";" @ %data.logicUIName @ ";" @
%data.logicUIDesc @ ";" @ %data.logicUIDesc @ ";" @
lualogic_escapelogicfunction(%data.logicInit) @ ";" @ lualogic_expandescape(%data.logicInit) @ ";" @
lualogic_escapelogicfunction(%data.logicUpdate) @ ";" @ lualogic_expandescape(%data.logicUpdate) @ ";" @
lualogic_escapelogicfunction(%data.logicInput) @ ";" @ lualogic_expandescape(%data.logicInput) @ ";" @
(%ports = %data.numLogicPorts) (%ports = %data.numLogicPorts)
; ;
@ -141,7 +130,7 @@ function lualogic_sendinput(%gate, %argc, %arg0, %arg1, %arg2, %arg3, %arg4, %ar
{ {
%args = %arg0; %args = %arg0;
for(%i = 1; %i < %argc; %i++) for(%i = 1; %i < %argc; %i++)
%args = %args @ ";" @ %arg[%i]; %args = %args @ ";" @ lualogic_expandescape(%arg[%i]);
if(%argc > 0) if(%argc > 0)
lualogic_send("IN;" @ %gate.getID() @ ";" @ %argc @ ";" @ %args); lualogic_send("IN;" @ %gate.getID() @ ";" @ %argc @ ";" @ %args);
@ -244,3 +233,55 @@ function lualogic_readfile(%filename){
return %filestr; return %filestr;
} }
$LuaLogic::EscapeCount = 0;
$LuaLogic::EscapeIn[$LuaLogic::EscapeCount] = "\\"; $LuaLogic::EscapeOut[$LuaLogic::EscapeCount] = "b"; $LuaLogic::EscapeCount++;
$LuaLogic::EscapeIn[$LuaLogic::EscapeCount] = "\t"; $LuaLogic::EscapeOut[$LuaLogic::EscapeCount] = "t"; $LuaLogic::EscapeCount++;
$LuaLogic::EscapeIn[$LuaLogic::EscapeCount] = "\n"; $LuaLogic::EscapeOut[$LuaLogic::EscapeCount] = "n"; $LuaLogic::EscapeCount++;
$LuaLogic::EscapeIn[$LuaLogic::EscapeCount] = "\r"; $LuaLogic::EscapeOut[$LuaLogic::EscapeCount] = "r"; $LuaLogic::EscapeCount++;
$LuaLogic::EscapeIn[$LuaLogic::EscapeCount] = "\'"; $LuaLogic::EscapeOut[$LuaLogic::EscapeCount] = "a"; $LuaLogic::EscapeCount++;
$LuaLogic::EscapeIn[$LuaLogic::EscapeCount] = "\""; $LuaLogic::EscapeOut[$LuaLogic::EscapeCount] = "q"; $LuaLogic::EscapeCount++;
$LuaLogic::EscapeIn[$LuaLogic::EscapeCount] = ";" ; $LuaLogic::EscapeOut[$LuaLogic::EscapeCount] = "s"; $LuaLogic::EscapeCount++;
$LuaLogic::EscapeIn[$LuaLogic::EscapeCount] = ":" ; $LuaLogic::EscapeOut[$LuaLogic::EscapeCount] = "c"; $LuaLogic::EscapeCount++;
function lualogic_expandescape(%str){
%ostr = "";
%len = strLen(%str);
for(%i=0; %i<%len; %i++){
%ci = getSubStr(%str, %i, 1);
%co = %ci;
for(%j=0; %j<$LuaLogic::EscapeCount; %j++){
if(%ci$=$LuaLogic::EscapeIn[%j]){ %co = "\\" @ $LuaLogic::EscapeOut[%j]; }
}
%ostr = %ostr @ %co;
}
return %ostr;
}
function lualogic_collapseescape(%str){
%ostr = "";
%i = 0;
%len = strLen(%str);
while(%i<%len){
%ci = getSubStr(%str, %i, 1);
%co = %ci;
if(%ci=="\\" && %i<%len-1){
%i++;
%ci = getSubStr(%str, %i, 1);
for(%j=0; %j<$LuaLogic::EscapeCount; %j++){
if(%ci$=$LuaLogic::EscapeOut[%j]){ %co = $LuaLogic::EscapeIn[%j]; }
}
}
%ostr = %ostr @ %co;
%i++;
}
return %ostr;
}