Browse Source

Add sscsm.every and use the built-in assert().

master
luk3yx 9 months ago
parent
commit
88f1030e01
5 changed files with 35 additions and 10 deletions
  1. +3
    -0
      README.md
  2. +3
    -3
      csm/init.lua
  3. +3
    -3
      csm_strict/init.lua
  4. +20
    -4
      sscsm_init.lua
  5. +6
    -0
      sscsm_testing.lua

+ 3
- 0
README.md View File

@@ -96,6 +96,9 @@ SSCSMs can access most functions on [client_lua_api.txt](https://github.com/mine
- `sscsm.unregister_chatcommand(name)`: Unregisters a chatcommand.
- `sscsm.get_player_control()`: Returns a table similar to the server-side
`player:get_player_control()`.
- `sscsm.every(interval, func, ...)`: Calls `func` every `interval` seconds
with any extra parameters specified. Use `minetest.register_globalstep`
instead if `interval` is `0`.
- `sscsm.restriction_flags`: The `csm_restriction_flags` setting set in
the server's `minetest.conf`.
- `sscsm.restrictions`: A table based on `csm_restriction_flags`:


+ 3
- 3
csm/init.lua View File

@@ -158,9 +158,9 @@ local base_env = Env:new_empty()
function Env.new() return base_env:copy() end

-- Clone everything
base_env:add_globals('dump', 'dump2', 'error', 'ipairs', 'math', 'next',
'pairs', 'pcall', 'select', 'setmetatable', 'string', 'table', 'tonumber',
'tostring', 'type', 'vector', 'xpcall', '_VERSION')
base_env:add_globals('assert', 'dump', 'dump2', 'error', 'ipairs', 'math',
'next', 'pairs', 'pcall', 'select', 'setmetatable', 'string', 'table',
'tonumber', 'tostring', 'type', 'vector', 'xpcall', '_VERSION')

base_env:set_copy('os', {clock = os.clock, difftime = os.difftime,
time = os.time})


+ 3
- 3
csm_strict/init.lua View File

@@ -166,9 +166,9 @@ local base_env = Env:new_empty()
function Env.new() return base_env:copy() end

-- Clone everything
base_env:add_globals('dump', 'dump2', 'error', 'ipairs', 'math', 'next',
'pairs', 'pcall', 'select', 'setmetatable', 'string', 'table', 'tonumber',
'tostring', 'type', 'vector', 'xpcall', '_VERSION')
base_env:add_globals('assert', 'dump', 'dump2', 'error', 'ipairs', 'math',
'next', 'pairs', 'pcall', 'select', 'setmetatable', 'string', 'table',
'tonumber', 'tostring', 'type', 'vector', 'xpcall', '_VERSION')

base_env:set_copy('os', {clock = os.clock, difftime = os.difftime,
time = os.time})


+ 20
- 4
sscsm_init.lua View File

@@ -6,7 +6,7 @@
-- License: https://git.minetest.land/luk3yx/sscsm/src/branch/master/LICENSE.md
--

-- Make sure both table.unpack and unpack exist.
if table.unpack then
unpack = table.unpack
else
@@ -18,12 +18,15 @@ end
if not rawget then function rawget(n, name) return n[name] end end
if not rawset then function rawset(n, k, v) n[k] = v end end
if not rawequal then function rawequal(a, b) return a == b end end

-- Older versions of the CSM didn't provide assert().
if not assert then
function assert(value, err)
if not value then
error(err or 'Assertion failed!', 2)
function assert(value, ...)
if value then
return value
else
error(... or 'assertion failed!', 2)
end
return value
end
end

@@ -149,6 +152,18 @@ function sscsm.get_player_control()
}
end

-- Call func(...) every <interval> seconds.
local function sscsm_every(interval, func, ...)
minetest.after(interval, sscsm_every, interval, func, ...)
return func(...)
end

function sscsm.every(interval, func, ...)
assert(type(interval) == 'number' and type(func) == 'function',
'Invalid sscsm.every() invocation.')
return sscsm_every(interval, func, ...)
end

-- Allow SSCSMs to know about CSM restriction flags.
-- "__FLAGS__" is replaced with the actual value in init.lua.
local flags = __FLAGS__


+ 6
- 0
sscsm_testing.lua View File

@@ -81,3 +81,9 @@ end)
function yay()
print('yay() called')
end

print('assert = ' .. tostring(assert))
sscsm.every(60, function(param1)
assert(param1 == 123)
print('sscsm.every test called.')
end, 123)

Loading…
Cancel
Save