forked from redo/BlockLua
Merge branch 'master'
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#ifndef WINVER
|
||||
#define WINVER 0x0501
|
||||
#endif
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include <cstring>
|
||||
|
||||
int bll_error_handler(lua_State *L) {
|
||||
lua_getfield(L, LUA_GLOBALSINDEX, "_bllua_on_error");
|
||||
if (!lua_isfunction(L, -1)) {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#define _H_LUAINTERP_SHARED
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#define BLL_ARG_COUNT 20
|
||||
#define BLL_ARG_MAX 8192
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "luainterp.hpp"
|
||||
|
||||
int bll_TsCall(lua_State *L, const char *oname, const char *fname, int argc,
|
||||
int ofs) {
|
||||
ADDR obj = (ADDR)NULL;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "BlFuncs.hpp"
|
||||
#include "BlHooks.hpp"
|
||||
#include "luainterp.hpp"
|
||||
|
||||
bool bll_LuaCall(const char *fname, int argc, const char *argv[]) {
|
||||
lua_getglobal(gL, fname);
|
||||
for (int i = 0; i < argc; i++) {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
-- This Lua code provides some built-in utilities for writing Lua add-ons
|
||||
-- It is eval'd automatically once BLLua3 has loaded the TS API and environment
|
||||
-- It only has access to the sandboxed lua environment, just like user code.
|
||||
@@ -7,7 +6,10 @@ ts = _bllua_ts
|
||||
|
||||
-- Provide limited OS functions
|
||||
os = os or {}
|
||||
---@diagnostic disable-next-line: duplicate-set-field
|
||||
function os.time() return math.floor(tonumber(_bllua_ts.call('getSimTime')) / 1000) end
|
||||
|
||||
---@diagnostic disable-next-line: duplicate-set-field
|
||||
function os.clock() return tonumber(_bllua_ts.call('getSimTime')) / 1000 end
|
||||
|
||||
-- Virtual file class, emulating a file object as returned by io.open
|
||||
@@ -82,7 +84,9 @@ local function new_file_obj(fn)
|
||||
return file
|
||||
end
|
||||
|
||||
local function tflip(t) local u = {}; for _, n in ipairs(t) do u[n] = true end; return u; end
|
||||
local function tflip(t)
|
||||
local u = {}; for _, n in ipairs(t) do u[n] = true end; return u;
|
||||
end
|
||||
local allowed_zip_dirs = tflip {
|
||||
'add-ons', 'base', 'config', 'saves', 'screenshots', 'shaders'
|
||||
}
|
||||
@@ -98,7 +102,8 @@ local function io_open_absolute(fn, mode)
|
||||
if not exist then return nil, err end
|
||||
|
||||
if mode ~= nil and mode ~= 'r' and mode ~= 'rb' then
|
||||
return nil, 'Files in zips can only be opened in read mode' end
|
||||
return nil, 'Files in zips can only be opened in read mode'
|
||||
end
|
||||
|
||||
-- return a temp lua file object with the data
|
||||
local fi = new_file_obj(fn)
|
||||
@@ -106,6 +111,7 @@ local function io_open_absolute(fn, mode)
|
||||
end
|
||||
|
||||
io = io or {}
|
||||
---@diagnostic disable-next-line: duplicate-set-field
|
||||
function io.open(fn, mode, errn)
|
||||
errn = errn or 1
|
||||
|
||||
@@ -126,13 +132,19 @@ function io.open(fn, mode, errn)
|
||||
return fi, err, fn
|
||||
end
|
||||
end
|
||||
|
||||
---@diagnostic disable-next-line: duplicate-set-field
|
||||
function io.lines(fn)
|
||||
local fi, err, fn2 = io.open(fn, nil, 2)
|
||||
if not fi then error('Error opening file \'' .. fn2 .. '\': ' .. err, 2) end
|
||||
return fi:lines()
|
||||
end
|
||||
|
||||
---@diagnostic disable-next-line: duplicate-set-field
|
||||
function io.type(f)
|
||||
---@diagnostic disable-next-line: undefined-field
|
||||
if type(f) == 'table' and f._is_file then
|
||||
---@diagnostic disable-next-line: undefined-field
|
||||
return f._is_open and 'file' or 'closed file'
|
||||
else
|
||||
return _bllua_io_type(f)
|
||||
@@ -198,7 +210,8 @@ local function isValidCode(code)
|
||||
end
|
||||
function _bllua_smarteval(code)
|
||||
if (not code:find('^print%(')) and isValidCode('print(' .. code .. ')') then
|
||||
code = 'print('..code..')' end
|
||||
code = 'print(' .. code .. ')'
|
||||
end
|
||||
local f, e = loadstring(code)
|
||||
if f then
|
||||
return f()
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
-- todo
|
||||
-- Matrix class with math operators
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
-- Basic functionality that should be standard in Lua
|
||||
|
||||
|
||||
@@ -7,6 +6,7 @@
|
||||
function table.empty(t)
|
||||
return next(t) ~= nil
|
||||
end
|
||||
|
||||
-- Apply a function to each key in a table
|
||||
function table.map(f, ...)
|
||||
local ts = { ... }
|
||||
@@ -18,6 +18,7 @@ function table.map(f, ...)
|
||||
end
|
||||
return u
|
||||
end
|
||||
|
||||
function table.map_list(f, ...)
|
||||
local ts = { ... }
|
||||
local u = {}
|
||||
@@ -28,18 +29,21 @@ function table.map_list(f, ...)
|
||||
end
|
||||
return u
|
||||
end
|
||||
|
||||
-- Swap keys/values
|
||||
function table.swap(t)
|
||||
local u = {}
|
||||
for k, v in pairs(t) do u[v] = k end
|
||||
return u
|
||||
end
|
||||
|
||||
-- Reverse a list
|
||||
function table.reverse(l)
|
||||
local m = {}
|
||||
for i = 1, #l do m[#l - i + 1] = l[i] end
|
||||
return m
|
||||
end
|
||||
|
||||
-- Whether a table is a list/array (has only monotonic integer keys)
|
||||
function table.islist(t)
|
||||
local n = 0
|
||||
@@ -49,6 +53,7 @@ function table.islist(t)
|
||||
end
|
||||
return n == #t
|
||||
end
|
||||
|
||||
-- Append contents of other tables to first table
|
||||
function table.append(t, ...)
|
||||
local a = { ... }
|
||||
@@ -57,6 +62,7 @@ function table.append(t, ...)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
-- Create a new table containing all keys from any number of tables
|
||||
-- latter tables in the arg list override prior ones
|
||||
-- overlaps, NOT appends, integer keys
|
||||
@@ -68,6 +74,7 @@ function table.join(...)
|
||||
end
|
||||
return w
|
||||
end
|
||||
|
||||
-- Whether a table contains a certain value in any key
|
||||
function table.contains(t, s)
|
||||
for _, v in pairs(t) do
|
||||
@@ -75,29 +82,34 @@ function table.contains(t,s)
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function table.contains_list(t, s)
|
||||
for _, v in ipairs(t) do
|
||||
if v == s then return true end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- Copy a table to another table
|
||||
function table.copy(t)
|
||||
local u = {}
|
||||
for k, v in pairs(t) do u[k] = v end
|
||||
return u
|
||||
end
|
||||
|
||||
function table.copy_list(l)
|
||||
local m = {}
|
||||
for i, v in ipairs(l) do m[i] = v end
|
||||
return m
|
||||
end
|
||||
|
||||
-- Sort a table in a new copy
|
||||
function table.sortcopy(t, f)
|
||||
local u = table.copy_list(t)
|
||||
table.sort(u, f)
|
||||
return u
|
||||
end
|
||||
|
||||
-- Remove a value from a table
|
||||
function table.removevalue(t, r)
|
||||
local rem = {}
|
||||
@@ -106,6 +118,7 @@ function table.removevalue(t, r)
|
||||
end
|
||||
for _, k in ipairs(rem) do t[k] = nil end
|
||||
end
|
||||
|
||||
function table.removevalue_list(t, r)
|
||||
for i = #t, 1, -1 do
|
||||
if t[i] == r then
|
||||
@@ -113,6 +126,7 @@ function table.removevalue_list(t, r)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Export tables into formatted executable strings
|
||||
local function tabs(tabLevel)
|
||||
return (' '):rep(tabLevel)
|
||||
@@ -186,7 +200,6 @@ function table.tostring(t)
|
||||
return tableToString(t, 0, {})
|
||||
end
|
||||
|
||||
|
||||
-- String
|
||||
|
||||
-- Split string into table by separator
|
||||
@@ -194,7 +207,8 @@ end
|
||||
-- if regex is not true, sep is treated as a regex pattern
|
||||
function string.split(str, sep, noregex)
|
||||
if type(str) ~= 'string' then
|
||||
error('string.split: argument #1: expected string, got '..type(str), 2) end
|
||||
error('string.split: argument #1: expected string, got ' .. type(str), 2)
|
||||
end
|
||||
if sep == nil or sep == '' then
|
||||
local t = {}
|
||||
local ns = #str
|
||||
@@ -220,6 +234,7 @@ function string.split(str, sep, noregex)
|
||||
'string.split: argument #2: expected string or nil, got ' .. type(sep), 2)
|
||||
end
|
||||
end
|
||||
|
||||
-- Split string to a list of char bytes
|
||||
function string.bytes(s)
|
||||
local b = {}
|
||||
@@ -229,11 +244,13 @@ function string.bytes(s)
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
-- Trim leading and trailing whitespace
|
||||
function string.trim(s, ws)
|
||||
ws = ws or ' \t\r\n'
|
||||
return s:gsub('^[' .. ws .. ']+', ''):gsub('[' .. ws .. ']+$', '') .. ''
|
||||
end
|
||||
|
||||
-- String slicing and searching using [] operator
|
||||
local str_meta = getmetatable('')
|
||||
local str_meta_index_old = str_meta.__index
|
||||
@@ -249,15 +266,20 @@ function str_meta.__index(s,k)
|
||||
return string.sub(s, a, b)
|
||||
end
|
||||
end
|
||||
|
||||
-- String iterator
|
||||
function string.chars(s)
|
||||
local i = 0
|
||||
return function()
|
||||
i = i + 1
|
||||
if i<=#s then return s:sub(i,i)
|
||||
else return nil end
|
||||
if i <= #s then
|
||||
return s:sub(i, i)
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Escape sequences
|
||||
local defaultEscapes = {
|
||||
['\\'] = '\\\\',
|
||||
@@ -277,6 +299,7 @@ function string.escape(s, escapes)
|
||||
end
|
||||
return table.concat(t)
|
||||
end
|
||||
|
||||
local defaultEscapeChar = '\\'
|
||||
local defaultUnescapes = {
|
||||
['\\'] = '\\',
|
||||
@@ -307,7 +330,6 @@ function string.unescape(s, escapeChar, unescapes)
|
||||
return table.concat(t)
|
||||
end
|
||||
|
||||
|
||||
-- IO
|
||||
|
||||
io = io or {}
|
||||
@@ -319,6 +341,7 @@ function io.readall(filename)
|
||||
fi:close()
|
||||
return s
|
||||
end
|
||||
|
||||
-- Write data to file all at once, return true if success / false,err if failure
|
||||
function io.writeall(filename, data)
|
||||
local fi, err = io.open(filename, 'wb')
|
||||
@@ -328,23 +351,26 @@ function io.writeall(filename, data)
|
||||
return true, nil
|
||||
end
|
||||
|
||||
|
||||
-- Math
|
||||
|
||||
-- Round
|
||||
function math.round(x)
|
||||
return math.floor(x + 0.5)
|
||||
end
|
||||
|
||||
-- Mod that accounts for floating point inaccuracy
|
||||
function math.mod(a, b)
|
||||
local m = a % b
|
||||
if m==0 or math.abs(m)<1e-15 or math.abs(m-b)<1e-15 then return 0
|
||||
else return m end
|
||||
if m == 0 or math.abs(m) < 1e-15 or math.abs(m - b) < 1e-15 then
|
||||
return 0
|
||||
else
|
||||
return m
|
||||
end
|
||||
end
|
||||
|
||||
-- Clamp value between min and max
|
||||
function math.clamp(v, n, x)
|
||||
return math.min(x, math.max(v, n))
|
||||
end
|
||||
|
||||
|
||||
print(' Executed std.lua')
|
||||
|
||||
Reference in New Issue
Block a user