From f2af58a6538cbd40dc8fe2395eb3d3c45ff9c409 Mon Sep 17 00:00:00 2001 From: Nero <41307858+nero@users.noreply.github.com> Date: Thu, 16 Jun 2022 21:10:45 +0000 Subject: [PATCH] Add 'On' and 'Prototype' helpers, introduce model installer files --- Install_00/Install.ini | 4 +++ Install_00/On.lua | 51 +++++++++++++++++++++++++++++++++++++ Install_00/Prototype.lua | 54 ++++++++++++++++++++++++++++++++++++++++ Installation.eep | 6 +++++ import.sh | 14 +++++++++++ 5 files changed, 129 insertions(+) create mode 100644 Install_00/Install.ini create mode 100644 Install_00/On.lua create mode 100644 Install_00/Prototype.lua create mode 100644 Installation.eep create mode 100644 import.sh diff --git a/Install_00/Install.ini b/Install_00/Install.ini new file mode 100644 index 0000000..5e0ee16 --- /dev/null +++ b/Install_00/Install.ini @@ -0,0 +1,4 @@ +[EEPInstall] +EEPVersion = 10 +File001 = "On.lua","LUA\On.lua" +File002 = "Prototype.lua","LUA\Prototype.Lua" diff --git a/Install_00/On.lua b/Install_00/On.lua new file mode 100644 index 0000000..0e37288 --- /dev/null +++ b/Install_00/On.lua @@ -0,0 +1,51 @@ +local Callbacks = {} + +function On(Name, Funktion) + -- Nur Strings erlaubt! + if type(Name) ~= "string" then + error("Ungueltiger Callback-Name "..tostring(Name)) + return + end + if not Callbacks[Name] then + -- Bestehende globale Funktionen duerfen nicht ueberschrieben werden + if _G[Name] ~= nil then + error(Name.."() bereits definiert") + return + end + -- Bei Weichen und Signalen muss EEP vorher informiert werden + -- Item ist normal "Switch" oder "Signal" + local Item, Number = string.match(Name, 'EEPOn(.+)_(%d+)') + if Number ~= nil then + local Registration = "EEPRegister"..Item + if _G[Registration](Number) == 0 then + error(Registration.."("..tostring(Number)..") fehlgeschlagen") + end + end + -- Lambda-Untertabelle fuer diesen EEP Callback eroeffnen + Callbacks[Name] = {} + -- Handler installieren, der alle Lambdas ausfuehrt + _G[Name] = function(...) + for cnt = 1, #Callbacks[Name] do + Callbacks[Name][cnt](...) + end + -- EEPMain stoppt, geben wir hier nicht 1 zurueck + return 1 + end + end + table.insert(Callbacks[Name], Funktion) +end + +-- EEPMain-Callback registrieren +function Main(Funktion) + On("EEPMain", Funktion) +end + +-- Signal-Callback registrieren +function OnSignal(Signal, Funktion) + On("EEPOnSignal_"..tostring(Signal), Funktion) +end + +-- Weichen-Callbacks registrieren +function OnSwitch(Switch, Funktion) + On("EEPOnSwitch_"..tostring(Switch), Funktion) +end diff --git a/Install_00/Prototype.lua b/Install_00/Prototype.lua new file mode 100644 index 0000000..46a7f76 --- /dev/null +++ b/Install_00/Prototype.lua @@ -0,0 +1,54 @@ +Prototype={} + +function Prototype:init() end -- dummy constructor + +-- function for injecting our functionality into an table +function Prototype:__set_parent(newparent) + setmetatable(self, { + -- non-overwritten keys are to be fetched from parent + __index=newparent, + -- construct a child when called directly + __call=function(parent, ...) + local newchild = {} + self.__set_parent(newchild, parent) + newchild:init(...) + return newchild + end + }) +end + +-- serialize our object into a string +function Prototype:__tostring() + local t=type(self) + if t=="table" then + local r="" + -- load and sort table keys + local tkeys={} + for k in pairs(self) do table.insert(tkeys, k) end + table.sort(tkeys) + -- iterate over our table + for _,k in ipairs(tkeys) do + local v = Prototype.__tostring(self[k]) + -- ignore nil values, they unset the key anyways + if v~= nil and v ~= "nil" then + if r~="" then r=r.."," end + -- short format: foo="bar", saves bytes + if type(k)=="string" and k:match("^[%l%u_][%w_]*$") then + r=r..k.."="..v + -- long format: ["foo"]="bar", allows weird keys + else + r=r.."["..Prototype.__tostring(k).."]="..v + end + end + end + return "{"..r.."}" + elseif t=="number" then + return tostring(self) + elseif t=="string" then + return "\""..self:gsub("\\","\\\\"):gsub("\"","\\\"").."\"" + else + return "nil" + end +end + +Prototype:__set_parent(nil) -- enable __call diff --git a/Installation.eep b/Installation.eep new file mode 100644 index 0000000..ab6626f --- /dev/null +++ b/Installation.eep @@ -0,0 +1,6 @@ +[Install_00] +Name_GER = "KsKit v1.1" +Name_ENG = "KsKit v1.1" +Desc_GER = "Script-Kollektion von Nero" +Desc_ENG = "Nero's script collection" +Script = "Install_00\Install.ini" diff --git a/import.sh b/import.sh new file mode 100644 index 0000000..898c06c --- /dev/null +++ b/import.sh @@ -0,0 +1,14 @@ +#!/bin/sh +SOURCE="$1" +fail() { + echo "$1" >&2 + exit 1 +} +verbose() { + echo "$@" + "$@" +} +grep ^File Install_00/Install.ini|sed 's/","/ /;s/"//g;s|\\|/|'|while read _ _ lname rname; do + test -f "${SOURCE}/${rname}" || fail "${rname} not found" + verbose cp "${SOURCE}/${rname}" "Install_00/${lname}" +done