Last active
August 4, 2025 17:17
-
-
Save eprosync/b8eb8e7b80c0cd1dc9c1e7202fcd9b9a to your computer and use it in GitHub Desktop.
Interserve for Starfall - Supports Live Loading, Concept Version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if SF and SF.Libraries and SF.Modules then | |
| local Libraries = SF.Libraries | |
| local Modules = SF.Modules | |
| local function SF_Lib(name, callback) | |
| Libraries[name] = true | |
| Modules[name] = { | |
| [name] = { | |
| source = "interserve", | |
| init = function(instance) | |
| if not instance.player:IsAdmin() then return end | |
| callback(instance, instance.Libraries[name]) | |
| end | |
| } | |
| } | |
| end | |
| if SERVER then | |
| SF_Lib("interserve", function(instance, library) | |
| local function interserve_suid(id) | |
| local player = instance.player | |
| if not IsValid(player) then return end | |
| return util.MD5(player:SteamID64() .. id) | |
| end | |
| instance.interserve = { | |
| registry = {}, | |
| receivers = {} | |
| } | |
| instance:AddHook("deinitialize", function() | |
| local registry = instance.interserve.registry | |
| local receivers = instance.interserve.registry | |
| for i=1, #registry do | |
| interserve:remove(registry[i]) | |
| end | |
| for i=1, #receivers do | |
| interserve:receive(receivers[i], nil) | |
| end | |
| end) | |
| library.add = function(id) | |
| assert(isstring(id), "bad argument #1, expected string") | |
| id = interserve_suid(id) | |
| local r = instance.interserve.registry | |
| if #r > 5 then | |
| SF.Throw("Too many interserve endpoints", 2) | |
| return | |
| end | |
| if r[id] then return end r[id] = true r[#r+1] = id | |
| interserve:add(id) | |
| end | |
| library.remove = function(id) | |
| assert(isstring(id), "bad argument #1, expected string") | |
| id = interserve_suid(id) | |
| local r = instance.interserve.registry | |
| if not r[id] then return end r[id] = nil | |
| for i=1, #r do | |
| if r[i] == id then | |
| table.remove(r, i) | |
| break | |
| end | |
| end | |
| interserve:remove(id) | |
| end | |
| library.failure = function(id, callback) end | |
| library.receive = function(id, callback) | |
| assert(isstring(id), "bad argument #1, expected string") | |
| assert(isfunction(callback), "bad argument #2, expected function") | |
| id = interserve_suid(id) | |
| local receivers = instance.interserve.receivers | |
| if callback then | |
| if #receivers > 5 then | |
| SF.Throw("Too many interserve receivers", 2) | |
| return | |
| end | |
| receivers[id] = callback | |
| receivers[#receivers+1] = id | |
| local handle = function(invoker, data) | |
| invoker = instance.Types.Player.Wrap(invoker) | |
| instance:runFunction(callback, invoker, data) | |
| end | |
| interserve:receive(id, handle) | |
| else | |
| receivers[id] = nil | |
| for i=1, #receivers do | |
| if receivers[i] == id then | |
| table.remove(receivers, i) | |
| break | |
| end | |
| end | |
| interserve:receive(id, nil) | |
| end | |
| end | |
| library.send = function(id, target, data) | |
| assert(isstring(id), "bad argument #1, expected string") | |
| id = interserve_suid(id) | |
| target = instance.Types.Player.Unwrap(target) | |
| assert(isstring(data), "bad argument #3, expected string") | |
| interserve:send(id, target, data) | |
| end | |
| library.broadcast = function(id, data) | |
| id = interserve_suid(id) | |
| interserve:broadcast(id, data) | |
| end | |
| end) | |
| else | |
| SF_Lib("interserve", function(instance, library) | |
| local function interserve_suid(id) | |
| local player = instance.player | |
| if not IsValid(player) then return end | |
| return util.MD5(player:SteamID64() .. id) | |
| end | |
| instance.interserve = { | |
| receivers = {}, | |
| failures = {} | |
| } | |
| instance:AddHook("deinitialize", function() | |
| local receivers = instance.interserve.receivers | |
| for i=1, #receivers do | |
| interserve:receive(receivers[i], nil) | |
| end | |
| end) | |
| library.add = function() end | |
| library.remove = function() end | |
| library.failure = function(id, callback) | |
| assert(isstring(id), "bad argument #1, expected string") | |
| assert(isfunction(callback), "bad argument #2, expected function") | |
| id = interserve_suid(id) | |
| local failures = instance.interserve.failures | |
| if callback then | |
| if #failures > 10 then | |
| SF.Throw("Too many interserve failure handlers", 2) | |
| return | |
| end | |
| failures[id] = callback | |
| failures[#failures+1] = id | |
| interserve:failure(id, function(...) | |
| instance:runFunction(callback, ...) | |
| end) | |
| else | |
| failures[id] = nil | |
| for i=1, #failures do | |
| if failures[i] == id then | |
| table.remove(failures, i) | |
| break | |
| end | |
| end | |
| interserve:failure(id, nil) | |
| end | |
| end | |
| library.receive = function(id, callback) | |
| assert(isstring(id), "bad argument #1, expected string") | |
| assert(isfunction(callback), "bad argument #2, expected function") | |
| id = interserve_suid(id) | |
| local receivers = instance.interserve.receivers | |
| if callback then | |
| if #receivers > 5 then | |
| SF.Throw("Too many interserve receivers", 2) | |
| return | |
| end | |
| receivers[id] = callback | |
| receivers[#receivers+1] = id | |
| interserve:receive(id, function(...) | |
| instance:runFunction(callback, ...) | |
| end) | |
| else | |
| receivers[id] = nil | |
| for i=1, #receivers do | |
| if receivers[i] == id then | |
| table.remove(receivers, i) | |
| break | |
| end | |
| end | |
| interserve:receive(id, nil) | |
| end | |
| end | |
| library.send = function(id, data) | |
| assert(isstring(id), "bad argument #1, expected string") | |
| assert(isstring(data), "bad argument #2, expected string") | |
| id = interserve_suid(id) | |
| interserve:send(id, data) | |
| end | |
| end) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment