Add 'On' and 'Prototype' helpers, introduce model installer files

This commit is contained in:
Nero 2022-06-16 21:10:45 +00:00
parent 1ae94ca7ad
commit f2af58a653
5 changed files with 129 additions and 0 deletions

4
Install_00/Install.ini Normal file
View File

@ -0,0 +1,4 @@
[EEPInstall]
EEPVersion = 10
File001 = "On.lua","LUA\On.lua"
File002 = "Prototype.lua","LUA\Prototype.Lua"

51
Install_00/On.lua Normal file
View File

@ -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

54
Install_00/Prototype.lua Normal file
View File

@ -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

6
Installation.eep Normal file
View File

@ -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"

14
import.sh Normal file
View File

@ -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