PtokaX forum

Archive => Archived 4.0 boards => Help with Lua 4 scripts => Topic started by: Corayzon on 13 August, 2004, 11:13:41

Title: A look at the latest cSlave's scripting API
Post by: Corayzon on 13 August, 2004, 11:13:41
here is a little snipet from cslaves API file events.lua

-- Event API for cSlave 1.0
-- Writin by Corayzon (Matty D)
-- Once fully tested remove this line

tPtokaxEvents =
{
-- pTokax Functions
["Main"] = {},
["DataArrival"] = {},
["NewUserConnected"] = {},
["OpConnected"] = {},
["OpDisconnected"] = {},
["UserDisconnected"] = {},
}

tProtocol =
{
-- P2P Protocol
["$MyNick"] = {},
["$Key"] = {},
["$ValidateNick"] = {},
["$MyPass"] = {},
["$Version"] = {},
["$GetNickList"] = {},
["$MyINFO"] = {},
["$GetINFO"] = {},
["$ConnectToMe"] = {},
["$MultiConnectToMe"] = {},
["$RevConnectToMe"] = {},
["$Search"] = {},
["$MultiSearch"] = {},
["$SR"] = {},
["$To:"] = {},
["$Kick"] = {},
["$OpForceMove"] = {},
["$Quit"] = {},
["<"] = {},

-- cSlave Addon Protocol
["$MyIP"] = {},
["$Bot"] = {},

-- Other Protocol Support
["Other"] = {}
}

function doPtokaxEvent(sEvent, vArg1, vArg2)
local iReturn
if tPtokaxEvents[sEvent] then
for sFuncName, fFunction in tPtokaxEvents[sEvent] do
if iReturn == nil then
iReturn = fFunction(vArg1 or nil, vArg2 or nil)
else
fFunction(vArg1 or nil, vArg2 or nil)
end
end
return iReturn
end
end

function doProtocolEvent(sEvent, vArg1, vArg2)
local iReturn
if type(tProtocol[sEvent]) == "table" then
for sFuncName, fFunction in tProtocol[sEvent] do
if iReturn == nil then
iReturn = fFunction(vArg1 or nil, vArg2 or nil)
else
fFunction(vArg1 or nil, vArg2 or nil)
end
end
return iReturn
end
end

function regPtokaxEvent(sEvent, fFunction)
assert(type(sEvent) == "string", "regPtokaxEvent(sEvent, fFunction) - sEvent must be a string!")
assert(type(fFunction) == "function", "regPtokaxEvent(sEvent, fFunction) - fFunction must be a function!")
if not tPtokaxEvents[sEvent] then
return 1, sEvent .. " was not found!"
end
tPtokaxEvents[sEvent][tostring(fFunction)] = fFunction
end

function regProtocolEvent(sEvent, fFunction)
assert(type(sEvent) == "string", "regProtocolEvent(sEvent, fFunction) - sEvent must be a string!")
assert(type(fFunction) == "function", "regProtocolEvent(sEvent, fFunction) - fFunction must be a function!")
if not tProtocol[sEvent] then
return 1, sEvent .. " was not found!"
end
tProtocol[sEvent][tostring(fFunction)] = fFunction
end

noza
Title:
Post by: NightLitch on 13 August, 2004, 11:28:56
Looks nice, how do u execute the tPtokaxEvents ??

I mean the script depends in those:

function Main()
end

etc.

Nice btw.

/NL
Title:
Post by: Corayzon on 13 August, 2004, 11:47:02
the tPtokaxEvents executes the stored functions in the table when doPtokaxEvent(sEvent) is called, ...

it is used by the cSlave addons system so that the addon files may have function Main() and so on while not overwriting the main cslave script.

and it also has a regPtokaxEvent(sEvent, fFunction) so u can dependly pass the ptokax functions to ur own scripts.

*** This is nice, but there is much more to be prode of about the new cslave, but u guys will just have to wait!

and btw, the new cslave is designed for ppl to write scripts in the API. It can execute multiple scripts in a similar fasion to ptokax while keeping all the globals shared (think of what can be done, and without hugh cluttered script files!). And it is also able to execute any script for ptokax within itself so that script passing works correctly. (when one script returns 1, the other scripts inline dont execute!)

and u can even run it with  robo!

enough blabber!

noza
Title: How cSlave execute multple scripts with the same function names
Post by: Corayzon on 13 August, 2004, 11:52:48
This is a snipet from cSlaves startup script.

This executes the scripts in the addons\scripts dir and makes references to the main ptokax functions and stores them into the tPtokaxEvents before loading the next. When its done it reloads the main cslave functions to pointers saved from startup.

function ExecuteAddonScripts()
execute("dir \"cSlave Addons\\Scripts\\*.lua\" /b > \"cSlave System\\Data\\Directory Dumps\\Addons\\Scripts.dir\"")
readfrom("cSlave System/Data/Directory Dumps/Addons/Scripts.dir")
local sLine = read()
while sLine ~= nil do
SendToAll("*** found " .. sLine .. ". Extension = " .. strsub(sLine, strlen(sLine) - 3, strlen(sLine)))
if strsub(sLine, strlen(sLine) - 3, strlen(sLine)) == ".lua" then
ClearCslavePtokaxFunctions()
SendToAll("*** executing " .. sLine)
assert(dofile("cSlave Addons/Scripts/" .. sLine), "Error traceback :- " .. sLine .. "!")
if type(Main) == "function" then
tPtokaxEvents["Main"][sLine] = Main
end
if type(DataArrival) == "function" then
tPtokaxEvents["DataArrival"][sLine] = DataArrival
end
if type(NewUserConnected) == "function" then
tPtokaxEvents["NewUserConnected"][sLine] = NewUserConnected
end
if type(OpConnected) == "function" then
tPtokaxEvents["OpConnected"][sLine] = OpConnected
end
if type(UserDisconnected) == "function" then
tPtokaxEvents["UserDisconnected"][sLine] = UserDisconnected
end
if type(OpDisconnected) == "function" then
tPtokaxEvents["OpDisconnected"][sLine] = OpDisconnected
end
end
sLine = read()
end
RestoreCslavePtokaxFunctions()
readfrom()
end

noza
Title:
Post by: NightLitch on 13 August, 2004, 15:25:20
Looks nice, I must say I understand some and some not hehe...

Keep ut the good work.

Regards / NL
Title:
Post by: Corayzon on 13 August, 2004, 19:50:42
ill be takin that as a complement :rolleyes:

noza