2019-07-07 08:49:59 +02:00
|
|
|
--
|
|
|
|
-- SSCSM: Server-Sent Client-Side Mods proof-of-concept
|
|
|
|
--
|
2020-11-26 02:00:39 +01:00
|
|
|
-- Copyright © 2019-2020 by luk3yx
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
-- You should have received a copy of the GNU Lesser General Public License
|
|
|
|
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-07-07 08:49:59 +02:00
|
|
|
--
|
|
|
|
|
|
|
|
local modname = minetest.get_current_modname()
|
2019-10-16 03:05:26 +02:00
|
|
|
|
|
|
|
-- If this is running as a CSM (improper installation), load the CSM code.
|
2021-12-05 05:04:23 +01:00
|
|
|
if INIT == "client" then
|
|
|
|
local modpath
|
|
|
|
if minetest.get_modpath then
|
|
|
|
modpath = minetest.get_modpath(modname)
|
|
|
|
else
|
|
|
|
modpath = modname .. ":"
|
|
|
|
end
|
|
|
|
dofile(modpath .. "csm/init.lua")
|
|
|
|
return
|
2019-10-16 03:05:26 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local sscsm = {minify=true}
|
2019-07-07 08:49:59 +02:00
|
|
|
_G[modname] = sscsm
|
|
|
|
local modpath = minetest.get_modpath(modname)
|
|
|
|
|
|
|
|
-- Remove excess whitespace from code to allow larger files to be sent.
|
|
|
|
if sscsm.minify then
|
2021-12-05 05:04:23 +01:00
|
|
|
local f = loadfile(modpath .. "/minify.lua")
|
|
|
|
if f then
|
|
|
|
sscsm.minify_code = f()
|
|
|
|
else
|
|
|
|
minetest.log("warning", "[SSCSM] Could not load minify.lua!")
|
|
|
|
end
|
2019-07-07 08:49:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if not sscsm.minify_code then
|
2021-12-05 05:04:23 +01:00
|
|
|
function sscsm.minify_code(code)
|
|
|
|
assert(type(code) == "string")
|
|
|
|
return code
|
|
|
|
end
|
2019-07-07 08:49:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Register code
|
|
|
|
sscsm.registered_csms = {}
|
|
|
|
local csm_order = false
|
|
|
|
|
|
|
|
-- Recalculate the CSM loading order
|
|
|
|
-- TODO: Make this nicer
|
|
|
|
local function recalc_csm_order()
|
2021-12-05 05:04:23 +01:00
|
|
|
local staging = {}
|
|
|
|
local order = {":init"}
|
|
|
|
local unsatisfied = {}
|
|
|
|
for name, def in pairs(sscsm.registered_csms) do
|
|
|
|
assert(name == def.name)
|
|
|
|
if name:sub(1, 1) ~= ":" then
|
|
|
|
if not def.depends or #def.depends == 0 then
|
|
|
|
table.insert(staging, name)
|
|
|
|
else
|
|
|
|
unsatisfied[name] = {}
|
|
|
|
for _, mod in ipairs(def.depends) do
|
|
|
|
if mod:sub(1, 1) ~= ":" then
|
|
|
|
unsatisfied[name][mod] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
while #staging > 0 do
|
|
|
|
local name = staging[1]
|
|
|
|
for name2, u in pairs(unsatisfied) do
|
|
|
|
if u[name] then
|
|
|
|
u[name] = nil
|
|
|
|
if #u == 0 then
|
|
|
|
table.insert(staging, name2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(order, name)
|
|
|
|
table.remove(staging, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
for name, u in pairs(unsatisfied) do
|
|
|
|
if next(u) then
|
|
|
|
local msg = 'SSCSM "' .. name .. '" has unsatisfied dependencies: '
|
|
|
|
local n = false
|
|
|
|
for dep, _ in pairs(u) do
|
|
|
|
if n then msg = msg .. ", " else n = true end
|
|
|
|
msg = msg .. '"' .. dep .. '"'
|
|
|
|
end
|
|
|
|
minetest.log("error", msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Set csm_order
|
|
|
|
table.insert(order, ":cleanup")
|
|
|
|
csm_order = order
|
2019-07-07 08:49:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Register SSCSMs
|
|
|
|
local block_colon = false
|
|
|
|
sscsm.registered_csms = {}
|
|
|
|
function sscsm.register(def)
|
2021-12-05 05:04:23 +01:00
|
|
|
-- Read files now in case MT decides to block access later.
|
|
|
|
if not def.code and def.file then
|
|
|
|
local f = io.open(def.file, "rb")
|
|
|
|
if not f then
|
|
|
|
error('Invalid "file" parameter passed to sscsm.register_csm.', 2)
|
|
|
|
end
|
|
|
|
def.code = f:read("*a")
|
|
|
|
f:close()
|
|
|
|
def.file = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if type(def.name) ~= "string" or def.name:find("\n")
|
|
|
|
or (def.name:sub(1, 1) == ":" and block_colon) then
|
|
|
|
error('Invalid "name" parameter passed to sscsm.register_csm.', 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
if type(def.code) ~= "string" then
|
|
|
|
error('Invalid "code" parameter passed to sscsm.register_csm.', 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def.code = sscsm.minify_code(def.code)
|
|
|
|
if (#def.name + #def.code) > 65300 then
|
|
|
|
error("The code (or name) passed to sscsm.register_csm is too large."
|
|
|
|
.. " Consider refactoring your SSCSM code.", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Copy the table to prevent mods from betraying our trust.
|
|
|
|
sscsm.registered_csms[def.name] = table.copy(def)
|
|
|
|
if csm_order then recalc_csm_order() end
|
2019-07-07 08:49:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function sscsm.unregister(name)
|
2021-12-05 05:04:23 +01:00
|
|
|
sscsm.registered_csms[name] = nil
|
|
|
|
if csm_order then recalc_csm_order() end
|
2019-07-07 08:49:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Recalculate the CSM order once all other mods are loaded
|
|
|
|
minetest.register_on_mods_loaded(recalc_csm_order)
|
|
|
|
|
|
|
|
-- Handle players joining
|
2019-12-28 22:44:41 +01:00
|
|
|
local has_sscsms = {}
|
2021-12-05 05:04:23 +01:00
|
|
|
local mod_channel = minetest.mod_channel_join("sscsm:exec_pipe")
|
2019-07-07 08:49:59 +02:00
|
|
|
minetest.register_on_modchannel_message(function(channel_name, sender, message)
|
2021-12-05 05:04:23 +01:00
|
|
|
if channel_name ~= "sscsm:exec_pipe" or not sender or
|
|
|
|
not mod_channel:is_writeable() or message ~= "0" or
|
|
|
|
sender:find("\n") or has_sscsms[sender] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
minetest.log("action", "[SSCSM] Sending CSMs on request for " .. sender
|
|
|
|
.. "...")
|
|
|
|
for _, name in ipairs(csm_order) do
|
|
|
|
mod_channel:send_all("0" .. sender .. "\n" .. name
|
|
|
|
.. "\n" .. sscsm.registered_csms[name].code)
|
|
|
|
end
|
2019-07-07 08:49:59 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- Register the SSCSM "builtins"
|
|
|
|
sscsm.register({
|
2021-12-05 05:04:23 +01:00
|
|
|
name = ":init",
|
|
|
|
file = modpath .. "/sscsm_init.lua"
|
2019-07-07 08:49:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
sscsm.register({
|
2021-12-05 05:04:23 +01:00
|
|
|
name = ":cleanup",
|
|
|
|
code = "sscsm._done_loading_()"
|
2019-07-07 08:49:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
block_colon = true
|
|
|
|
|
2019-10-15 04:50:31 +02:00
|
|
|
-- Set the CSM restriction flags
|
2021-12-05 05:04:23 +01:00
|
|
|
local flags = tonumber(minetest.settings:get("csm_restriction_flags"))
|
2019-12-28 22:44:41 +01:00
|
|
|
if not flags or flags ~= flags then
|
2021-12-05 05:04:23 +01:00
|
|
|
flags = 62
|
2019-12-28 22:44:41 +01:00
|
|
|
end
|
2020-07-27 03:52:51 +02:00
|
|
|
flags = math.floor(math.max(flags, 0)) % 64
|
2019-10-15 04:50:31 +02:00
|
|
|
|
2019-12-28 22:44:41 +01:00
|
|
|
do
|
2021-12-05 05:04:23 +01:00
|
|
|
local def = sscsm.registered_csms[":init"]
|
|
|
|
def.code = def.code:gsub("__FLAGS__", tostring(flags))
|
2019-10-15 04:50:31 +02:00
|
|
|
end
|
|
|
|
|
2019-12-28 22:44:41 +01:00
|
|
|
if math.floor(flags / 2) % 2 == 1 then
|
2021-12-05 05:04:23 +01:00
|
|
|
minetest.log("warning", "[SSCSM] SSCSMs enabled, however CSMs cannot "
|
|
|
|
.. "send chat messages! This will prevent SSCSMs from sending "
|
|
|
|
.. "messages to the server.")
|
|
|
|
sscsm.com_write_only = true
|
2019-12-28 22:44:41 +01:00
|
|
|
else
|
2021-12-05 05:04:23 +01:00
|
|
|
sscsm.com_write_only = false
|
2019-12-28 22:44:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- SSCSM communication
|
|
|
|
local function validate_channel(channel)
|
2021-12-05 05:04:23 +01:00
|
|
|
if type(channel) ~= "string" then
|
|
|
|
error("SSCSM com channels must be strings!", 3)
|
|
|
|
end
|
|
|
|
if channel:find("\001", nil, true) then
|
|
|
|
error("SSCSM com channels cannot contain U+0001!", 3)
|
|
|
|
end
|
2019-12-28 22:44:41 +01:00
|
|
|
end
|
|
|
|
|
2020-02-14 09:17:46 +01:00
|
|
|
local msgids = {}
|
2019-12-28 22:44:41 +01:00
|
|
|
function sscsm.com_send(pname, channel, msg)
|
2021-12-05 05:04:23 +01:00
|
|
|
if minetest.is_player(pname) then
|
|
|
|
pname = pname:get_player_name()
|
|
|
|
end
|
|
|
|
validate_channel(channel)
|
|
|
|
if type(msg) == "string" then
|
|
|
|
msg = "\002" .. msg
|
|
|
|
else
|
|
|
|
msg = assert(minetest.write_json(msg))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Short messages can be sent all at once
|
|
|
|
local prefix = "\001SSCSM_COM\001" .. channel .. "\001"
|
|
|
|
if #msg < 65300 then
|
|
|
|
minetest.chat_send_player(pname, prefix .. msg)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- You should never send messages over 128MB to clients
|
|
|
|
assert(#msg < 134217728)
|
|
|
|
|
|
|
|
-- Otherwise split the message into multiple chunks
|
|
|
|
prefix = prefix .. "\001"
|
|
|
|
local id = #msgids + 1
|
|
|
|
local i = 0
|
|
|
|
msgids[id] = true
|
|
|
|
local total_msgs = math.ceil(#msg / 65000)
|
|
|
|
repeat
|
|
|
|
i = i + 1
|
|
|
|
minetest.chat_send_player(pname, prefix .. id .. "\001" .. i ..
|
|
|
|
"\001" .. total_msgs .. "\001" .. msg:sub(1, 65000))
|
|
|
|
msg = msg:sub(65001)
|
|
|
|
until msg == ""
|
|
|
|
|
|
|
|
-- Allow the ID to be reused on the next globalstep.
|
|
|
|
minetest.after(0, function()
|
|
|
|
msgids[id] = nil
|
|
|
|
end)
|
2019-12-28 22:44:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local registered_on_receive = {}
|
|
|
|
function sscsm.register_on_com_receive(channel, func)
|
2021-12-05 05:04:23 +01:00
|
|
|
if not registered_on_receive[channel] then
|
|
|
|
registered_on_receive[channel] = {}
|
|
|
|
end
|
|
|
|
table.insert(registered_on_receive[channel], func)
|
2019-12-28 22:44:41 +01:00
|
|
|
end
|
|
|
|
|
2021-12-05 05:04:23 +01:00
|
|
|
local admin_func = minetest.registered_chatcommands["admin"].func
|
|
|
|
minetest.override_chatcommand("admin", {
|
|
|
|
func = function(name, param)
|
|
|
|
local chan, msg = param:match("^\001SSCSM_COM\001([^\001]*)\001(.*)$")
|
|
|
|
if not chan or not msg then
|
|
|
|
return admin_func(name, param)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get the callbacks
|
|
|
|
local callbacks = registered_on_receive[chan]
|
|
|
|
if not callbacks then return end
|
|
|
|
|
|
|
|
-- Load the message
|
|
|
|
if msg:sub(1, 1) == "\002" then
|
|
|
|
msg = msg:sub(2)
|
|
|
|
else
|
|
|
|
msg = minetest.parse_json(msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Run callbacks
|
|
|
|
for _, func in ipairs(callbacks) do
|
|
|
|
func(name, msg)
|
|
|
|
end
|
|
|
|
end,
|
2019-12-28 22:44:41 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
-- Add a callback for sscsm:com_test
|
2020-02-14 10:32:12 +01:00
|
|
|
local registered_on_loaded = {}
|
|
|
|
function sscsm.register_on_sscsms_loaded(func)
|
2021-12-05 05:04:23 +01:00
|
|
|
table.insert(registered_on_loaded, func)
|
2020-02-14 10:32:12 +01:00
|
|
|
end
|
|
|
|
|
2021-12-05 05:04:23 +01:00
|
|
|
sscsm.register_on_com_receive("sscsm:com_test", function(name, msg)
|
|
|
|
if type(msg) ~= "table" or msg.flags ~= flags or has_sscsms[name] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
has_sscsms[name] = true
|
|
|
|
for _, func in ipairs(registered_on_loaded) do
|
|
|
|
func(name)
|
|
|
|
end
|
2019-12-28 22:44:41 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
function sscsm.has_sscsms_enabled(name)
|
2021-12-05 05:04:23 +01:00
|
|
|
return has_sscsms[name] or false
|
2019-12-28 22:44:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
2021-12-05 05:04:23 +01:00
|
|
|
has_sscsms[player:get_player_name()] = nil
|
2019-12-28 22:44:41 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
function sscsm.com_send_all(channel, msg)
|
2021-12-05 05:04:23 +01:00
|
|
|
for name, _ in pairs(has_sscsms) do
|
|
|
|
sscsm.com_send(name, channel, msg)
|
|
|
|
end
|
2019-12-28 22:44:41 +01:00
|
|
|
end
|