Compare commits
10 Commits
master
...
e309e2427b
| Author | SHA1 | Date | |
|---|---|---|---|
| e309e2427b | |||
| ee784869f1 | |||
| 0815a6d229 | |||
| cbd0c29495 | |||
| 01f216f31e | |||
| f447c039c7 | |||
| 30fa81f513 | |||
| 1144504ac3 | |||
| ce3f7c8b3f | |||
| 4d74df209e |
9
.editorconfig
Normal file
9
.editorconfig
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
|
end_of_line = lf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1 @@
|
|||||||
.*
|
build/
|
||||||
!.gitignore
|
|
||||||
|
|||||||
12
.vscode/settings.json
vendored
Normal file
12
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"Lua.diagnostics.globals": [
|
||||||
|
"_bllua_ts",
|
||||||
|
"_bllua_requiresecure",
|
||||||
|
"_bllua_on_unload"
|
||||||
|
],
|
||||||
|
"Lua.runtime.version": "Lua 5.1",
|
||||||
|
"Lua.diagnostics.disable": [
|
||||||
|
"lowercase-global",
|
||||||
|
"undefined-global"
|
||||||
|
]
|
||||||
|
}
|
||||||
BIN
BlockLua.dll
BIN
BlockLua.dll
Binary file not shown.
13
compile.bat
Normal file
13
compile.bat
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
@echo off
|
||||||
|
cd /d %~dp0
|
||||||
|
|
||||||
|
set "PATH=C:\msys64\mingw32\bin;%PATH%"
|
||||||
|
|
||||||
|
if not exist build mkdir build
|
||||||
|
|
||||||
|
set buildargs=-Wall -Werror -m32 -shared -Isrc -Iinc/tsfuncs -Iinc/lua -lpsapi -L. -llua5.1 -static-libgcc -static-libstdc++
|
||||||
|
|
||||||
|
echo on
|
||||||
|
g++ src/bllua4.cpp %buildargs% -o build\BlockLua.dll
|
||||||
|
g++ -DBLLUA_UNSAFE src/bllua4.cpp %buildargs% -o build\BlockLua-Unsafe.dll
|
||||||
|
@echo off
|
||||||
52
compiling.md
Normal file
52
compiling.md
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
## Compiling on Windows with MSYS2 (32-bit)
|
||||||
|
|
||||||
|
Follow these steps to build `BlockLua.dll` for 32-bit Windows using MSYS2's MinGW-w64 i686 toolchain.
|
||||||
|
|
||||||
|
### 1) Install MSYS2
|
||||||
|
|
||||||
|
- Download and install MSYS2 from `https://www.msys2.org/`.
|
||||||
|
- After installing, open the "MSYS2 MSYS" terminal once and update the package database if prompted.
|
||||||
|
|
||||||
|
### 2) Install required packages (i686 / 32-bit)
|
||||||
|
|
||||||
|
Run this in the "MSYS2 MSYS" or "MSYS2 MinGW 32-bit" terminal:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pacman -Sy --needed mingw-w64-i686-toolchain mingw-w64-i686-binutils mingw-w64-i686-lua51
|
||||||
|
```
|
||||||
|
|
||||||
|
What these packages are for:
|
||||||
|
|
||||||
|
- mingw-w64-i686-toolchain: 32-bit C/C++ compiler suite (g++, libstdc++, runtime libs) to build Windows binaries.
|
||||||
|
- mingw-w64-i686-binutils: Linker and binary utilities (ld, as, objdump) used by the compiler and for optional inspection.
|
||||||
|
- mingw-w64-i686-lua51: Lua 5.1 development files for 32-bit (import library). We use its import library to link; at runtime you can use the provided `lua5.1.dll`.
|
||||||
|
|
||||||
|
### 3) Build (PowerShell, recommended)
|
||||||
|
|
||||||
|
- Open PowerShell in the repo root.
|
||||||
|
- Run the script:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
compile.bat
|
||||||
|
```
|
||||||
|
|
||||||
|
What the script does:
|
||||||
|
|
||||||
|
- Temporarily prepends `C:\\msys64\\mingw32\\bin` to PATH so the 32-bit toolchain is used.
|
||||||
|
- Compiles the project with `-m32` and links against `lua5.1`.
|
||||||
|
- Produces `build\BlockLua.dll` and `build\BlockLua-Unsafe.dll`.
|
||||||
|
|
||||||
|
### 4) Optional: Verify 32-bit output
|
||||||
|
|
||||||
|
If you installed binutils, you can check the architecture:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
objdump -f build\BlockLua.dll | Select-String i386
|
||||||
|
```
|
||||||
|
|
||||||
|
You should see `architecture: i386` in the output.
|
||||||
|
|
||||||
|
### Notes
|
||||||
|
|
||||||
|
- Ensure you installed the i686 (32-bit) variants of the packages; x86_64 packages won’t work for a 32-bit build.
|
||||||
|
- If the linker cannot find `-llua5.1`, confirm `mingw-w64-i686-lua51` is installed and you are using the `mingw32` toolchain (not `x86_64`).
|
||||||
@@ -4,7 +4,9 @@
|
|||||||
|
|
||||||
|
|
||||||
-- Utility: Convert a list of strings into a map of string->true
|
-- Utility: Convert a list of strings into a map of string->true
|
||||||
local function tmap(t) local u = {}; for _, n in ipairs(t) do u[n] = true end; return u; end
|
local function tmap(t)
|
||||||
|
local u = {}; for _, n in ipairs(t) do u[n] = true end; return u;
|
||||||
|
end
|
||||||
|
|
||||||
-- Save banned global variables for wrapping with safe functions
|
-- Save banned global variables for wrapping with safe functions
|
||||||
local old_io = io
|
local old_io = io
|
||||||
@@ -104,6 +106,7 @@ function _bllua_io_open(fn, md)
|
|||||||
return nil, err
|
return nil, err
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Allow io.type (works on file handles returned by io.open)
|
-- Allow io.type (works on file handles returned by io.open)
|
||||||
function _bllua_io_type(f)
|
function _bllua_io_type(f)
|
||||||
return old_io.type(f)
|
return old_io.type(f)
|
||||||
@@ -128,6 +131,7 @@ function _bllua_requiresecure(name)
|
|||||||
return old_require(name)
|
return old_require(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
package = {
|
package = {
|
||||||
seeall = old_package.seeall,
|
seeall = old_package.seeall,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
-- This Lua code provides some built-in utilities for writing Lua add-ons
|
-- 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 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.
|
-- It only has access to the sandboxed lua environment, just like user code.
|
||||||
@@ -7,7 +6,10 @@ ts = _bllua_ts
|
|||||||
|
|
||||||
-- Provide limited OS functions
|
-- Provide limited OS functions
|
||||||
os = os or {}
|
os = os or {}
|
||||||
|
---@diagnostic disable-next-line: duplicate-set-field
|
||||||
function os.time() return math.floor(tonumber(_bllua_ts.call('getSimTime')) / 1000) end
|
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
|
function os.clock() return tonumber(_bllua_ts.call('getSimTime')) / 1000 end
|
||||||
|
|
||||||
-- Virtual file class, emulating a file object as returned by io.open
|
-- Virtual file class, emulating a file object as returned by io.open
|
||||||
@@ -82,7 +84,9 @@ local function new_file_obj(fn)
|
|||||||
return file
|
return file
|
||||||
end
|
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 {
|
local allowed_zip_dirs = tflip {
|
||||||
'add-ons', 'base', 'config', 'saves', 'screenshots', 'shaders'
|
'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 not exist then return nil, err end
|
||||||
|
|
||||||
if mode ~= nil and mode ~= 'r' and mode ~= 'rb' then
|
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
|
-- return a temp lua file object with the data
|
||||||
local fi = new_file_obj(fn)
|
local fi = new_file_obj(fn)
|
||||||
@@ -106,6 +111,7 @@ local function io_open_absolute(fn, mode)
|
|||||||
end
|
end
|
||||||
|
|
||||||
io = io or {}
|
io = io or {}
|
||||||
|
---@diagnostic disable-next-line: duplicate-set-field
|
||||||
function io.open(fn, mode, errn)
|
function io.open(fn, mode, errn)
|
||||||
errn = errn or 1
|
errn = errn or 1
|
||||||
|
|
||||||
@@ -126,13 +132,19 @@ function io.open(fn, mode, errn)
|
|||||||
return fi, err, fn
|
return fi, err, fn
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@diagnostic disable-next-line: duplicate-set-field
|
||||||
function io.lines(fn)
|
function io.lines(fn)
|
||||||
local fi, err, fn2 = io.open(fn, nil, 2)
|
local fi, err, fn2 = io.open(fn, nil, 2)
|
||||||
if not fi then error('Error opening file \'' .. fn2 .. '\': ' .. err, 2) end
|
if not fi then error('Error opening file \'' .. fn2 .. '\': ' .. err, 2) end
|
||||||
return fi:lines()
|
return fi:lines()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@diagnostic disable-next-line: duplicate-set-field
|
||||||
function io.type(f)
|
function io.type(f)
|
||||||
|
---@diagnostic disable-next-line: undefined-field
|
||||||
if type(f) == 'table' and f._is_file then
|
if type(f) == 'table' and f._is_file then
|
||||||
|
---@diagnostic disable-next-line: undefined-field
|
||||||
return f._is_open and 'file' or 'closed file'
|
return f._is_open and 'file' or 'closed file'
|
||||||
else
|
else
|
||||||
return _bllua_io_type(f)
|
return _bllua_io_type(f)
|
||||||
@@ -194,8 +206,11 @@ end
|
|||||||
|
|
||||||
-- Exposure to TS
|
-- Exposure to TS
|
||||||
function _bllua_getvar(name) return _G[name] end
|
function _bllua_getvar(name) return _G[name] end
|
||||||
|
|
||||||
function _bllua_setvar(name, val) _G[name] = val end
|
function _bllua_setvar(name, val) _G[name] = val end
|
||||||
|
|
||||||
function _bllua_eval(code) return loadstring(code)() end
|
function _bllua_eval(code) return loadstring(code)() end
|
||||||
|
|
||||||
function _bllua_exec(fn) return dofile(fn, 2) end
|
function _bllua_exec(fn) return dofile(fn, 2) end
|
||||||
|
|
||||||
local function isValidCode(code)
|
local function isValidCode(code)
|
||||||
@@ -204,7 +219,8 @@ local function isValidCode(code)
|
|||||||
end
|
end
|
||||||
function _bllua_smarteval(code)
|
function _bllua_smarteval(code)
|
||||||
if (not code:find('^print%(')) and isValidCode('print(' .. code .. ')') then
|
if (not code:find('^print%(')) and isValidCode('print(' .. code .. ')') then
|
||||||
code = 'print('..code..')' end
|
code = 'print(' .. code .. ')'
|
||||||
|
end
|
||||||
local f, e = loadstring(code)
|
local f, e = loadstring(code)
|
||||||
if f then
|
if f then
|
||||||
return f()
|
return f()
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
-- Basic functionality that should be standard in Lua
|
-- Basic functionality that should be standard in Lua
|
||||||
|
|
||||||
|
|
||||||
@@ -7,6 +6,7 @@
|
|||||||
function table.empty(t)
|
function table.empty(t)
|
||||||
return next(t) ~= nil
|
return next(t) ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Apply a function to each key in a table
|
-- Apply a function to each key in a table
|
||||||
function table.map(f, ...)
|
function table.map(f, ...)
|
||||||
local ts = { ... }
|
local ts = { ... }
|
||||||
@@ -18,6 +18,7 @@ function table.map(f, ...)
|
|||||||
end
|
end
|
||||||
return u
|
return u
|
||||||
end
|
end
|
||||||
|
|
||||||
function table.map_list(f, ...)
|
function table.map_list(f, ...)
|
||||||
local ts = { ... }
|
local ts = { ... }
|
||||||
local u = {}
|
local u = {}
|
||||||
@@ -28,18 +29,21 @@ function table.map_list(f, ...)
|
|||||||
end
|
end
|
||||||
return u
|
return u
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Swap keys/values
|
-- Swap keys/values
|
||||||
function table.swap(t)
|
function table.swap(t)
|
||||||
local u = {}
|
local u = {}
|
||||||
for k, v in pairs(t) do u[v] = k end
|
for k, v in pairs(t) do u[v] = k end
|
||||||
return u
|
return u
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Reverse a list
|
-- Reverse a list
|
||||||
function table.reverse(l)
|
function table.reverse(l)
|
||||||
local m = {}
|
local m = {}
|
||||||
for i = 1, #l do m[#l - i + 1] = l[i] end
|
for i = 1, #l do m[#l - i + 1] = l[i] end
|
||||||
return m
|
return m
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Whether a table is a list/array (has only monotonic integer keys)
|
-- Whether a table is a list/array (has only monotonic integer keys)
|
||||||
function table.islist(t)
|
function table.islist(t)
|
||||||
local n = 0
|
local n = 0
|
||||||
@@ -49,6 +53,7 @@ function table.islist(t)
|
|||||||
end
|
end
|
||||||
return n == #t
|
return n == #t
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Append contents of other tables to first table
|
-- Append contents of other tables to first table
|
||||||
function table.append(t, ...)
|
function table.append(t, ...)
|
||||||
local a = { ... }
|
local a = { ... }
|
||||||
@@ -57,6 +62,7 @@ function table.append(t, ...)
|
|||||||
end
|
end
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Create a new table containing all keys from any number of tables
|
-- Create a new table containing all keys from any number of tables
|
||||||
-- latter tables in the arg list override prior ones
|
-- latter tables in the arg list override prior ones
|
||||||
-- overlaps, NOT appends, integer keys
|
-- overlaps, NOT appends, integer keys
|
||||||
@@ -68,6 +74,7 @@ function table.join(...)
|
|||||||
end
|
end
|
||||||
return w
|
return w
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Whether a table contains a certain value in any key
|
-- Whether a table contains a certain value in any key
|
||||||
function table.contains(t, s)
|
function table.contains(t, s)
|
||||||
for _, v in pairs(t) do
|
for _, v in pairs(t) do
|
||||||
@@ -75,29 +82,34 @@ function table.contains(t,s)
|
|||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function table.contains_list(t, s)
|
function table.contains_list(t, s)
|
||||||
for _, v in ipairs(t) do
|
for _, v in ipairs(t) do
|
||||||
if v == s then return true end
|
if v == s then return true end
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Copy a table to another table
|
-- Copy a table to another table
|
||||||
function table.copy(t)
|
function table.copy(t)
|
||||||
local u = {}
|
local u = {}
|
||||||
for k, v in pairs(t) do u[k] = v end
|
for k, v in pairs(t) do u[k] = v end
|
||||||
return u
|
return u
|
||||||
end
|
end
|
||||||
|
|
||||||
function table.copy_list(l)
|
function table.copy_list(l)
|
||||||
local m = {}
|
local m = {}
|
||||||
for i, v in ipairs(l) do m[i] = v end
|
for i, v in ipairs(l) do m[i] = v end
|
||||||
return m
|
return m
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Sort a table in a new copy
|
-- Sort a table in a new copy
|
||||||
function table.sortcopy(t, f)
|
function table.sortcopy(t, f)
|
||||||
local u = table.copy_list(t)
|
local u = table.copy_list(t)
|
||||||
table.sort(u, f)
|
table.sort(u, f)
|
||||||
return u
|
return u
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Remove a value from a table
|
-- Remove a value from a table
|
||||||
function table.removevalue(t, r)
|
function table.removevalue(t, r)
|
||||||
local rem = {}
|
local rem = {}
|
||||||
@@ -106,6 +118,7 @@ function table.removevalue(t, r)
|
|||||||
end
|
end
|
||||||
for _, k in ipairs(rem) do t[k] = nil end
|
for _, k in ipairs(rem) do t[k] = nil end
|
||||||
end
|
end
|
||||||
|
|
||||||
function table.removevalue_list(t, r)
|
function table.removevalue_list(t, r)
|
||||||
for i = #t, 1, -1 do
|
for i = #t, 1, -1 do
|
||||||
if t[i] == r then
|
if t[i] == r then
|
||||||
@@ -113,6 +126,7 @@ function table.removevalue_list(t, r)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Export tables into formatted executable strings
|
-- Export tables into formatted executable strings
|
||||||
local function tabs(tabLevel)
|
local function tabs(tabLevel)
|
||||||
return (' '):rep(tabLevel)
|
return (' '):rep(tabLevel)
|
||||||
@@ -186,7 +200,6 @@ function table.tostring(t)
|
|||||||
return tableToString(t, 0, {})
|
return tableToString(t, 0, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- String
|
-- String
|
||||||
|
|
||||||
-- Split string into table by separator
|
-- Split string into table by separator
|
||||||
@@ -194,7 +207,8 @@ end
|
|||||||
-- if regex is not true, sep is treated as a regex pattern
|
-- if regex is not true, sep is treated as a regex pattern
|
||||||
function string.split(str, sep, noregex)
|
function string.split(str, sep, noregex)
|
||||||
if type(str) ~= 'string' then
|
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
|
if sep == nil or sep == '' then
|
||||||
local t = {}
|
local t = {}
|
||||||
local ns = #str
|
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)
|
'string.split: argument #2: expected string or nil, got ' .. type(sep), 2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Split string to a list of char bytes
|
-- Split string to a list of char bytes
|
||||||
function string.bytes(s)
|
function string.bytes(s)
|
||||||
local b = {}
|
local b = {}
|
||||||
@@ -229,11 +244,13 @@ function string.bytes(s)
|
|||||||
end
|
end
|
||||||
return b
|
return b
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Trim leading and trailing whitespace
|
-- Trim leading and trailing whitespace
|
||||||
function string.trim(s, ws)
|
function string.trim(s, ws)
|
||||||
ws = ws or ' \t\r\n'
|
ws = ws or ' \t\r\n'
|
||||||
return s:gsub('^[' .. ws .. ']+', ''):gsub('[' .. ws .. ']+$', '') .. ''
|
return s:gsub('^[' .. ws .. ']+', ''):gsub('[' .. ws .. ']+$', '') .. ''
|
||||||
end
|
end
|
||||||
|
|
||||||
-- String slicing and searching using [] operator
|
-- String slicing and searching using [] operator
|
||||||
local str_meta = getmetatable('')
|
local str_meta = getmetatable('')
|
||||||
local str_meta_index_old = str_meta.__index
|
local str_meta_index_old = str_meta.__index
|
||||||
@@ -249,15 +266,20 @@ function str_meta.__index(s,k)
|
|||||||
return string.sub(s, a, b)
|
return string.sub(s, a, b)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- String iterator
|
-- String iterator
|
||||||
function string.chars(s)
|
function string.chars(s)
|
||||||
local i = 0
|
local i = 0
|
||||||
return function()
|
return function()
|
||||||
i = i + 1
|
i = i + 1
|
||||||
if i<=#s then return s:sub(i,i)
|
if i <= #s then
|
||||||
else return nil end
|
return s:sub(i, i)
|
||||||
|
else
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- Escape sequences
|
-- Escape sequences
|
||||||
local defaultEscapes = {
|
local defaultEscapes = {
|
||||||
['\\'] = '\\\\',
|
['\\'] = '\\\\',
|
||||||
@@ -277,6 +299,7 @@ function string.escape(s, escapes)
|
|||||||
end
|
end
|
||||||
return table.concat(t)
|
return table.concat(t)
|
||||||
end
|
end
|
||||||
|
|
||||||
local defaultEscapeChar = '\\'
|
local defaultEscapeChar = '\\'
|
||||||
local defaultUnescapes = {
|
local defaultUnescapes = {
|
||||||
['\\'] = '\\',
|
['\\'] = '\\',
|
||||||
@@ -307,7 +330,6 @@ function string.unescape(s, escapeChar, unescapes)
|
|||||||
return table.concat(t)
|
return table.concat(t)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- IO
|
-- IO
|
||||||
|
|
||||||
io = io or {}
|
io = io or {}
|
||||||
@@ -319,6 +341,7 @@ function io.readall(filename)
|
|||||||
fi:close()
|
fi:close()
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Write data to file all at once, return true if success / false,err if failure
|
-- Write data to file all at once, return true if success / false,err if failure
|
||||||
function io.writeall(filename, data)
|
function io.writeall(filename, data)
|
||||||
local fi, err = io.open(filename, 'wb')
|
local fi, err = io.open(filename, 'wb')
|
||||||
@@ -328,19 +351,23 @@ function io.writeall(filename, data)
|
|||||||
return true, nil
|
return true, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Math
|
-- Math
|
||||||
|
|
||||||
-- Round
|
-- Round
|
||||||
function math.round(x)
|
function math.round(x)
|
||||||
return math.floor(x + 0.5)
|
return math.floor(x + 0.5)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Mod that accounts for floating point inaccuracy
|
-- Mod that accounts for floating point inaccuracy
|
||||||
function math.mod(a, b)
|
function math.mod(a, b)
|
||||||
local m = 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
|
if m == 0 or math.abs(m) < 1e-15 or math.abs(m - b) < 1e-15 then
|
||||||
else return m end
|
return 0
|
||||||
|
else
|
||||||
|
return m
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- Clamp value between min and max
|
-- Clamp value between min and max
|
||||||
function math.clamp(v, n, x)
|
function math.clamp(v, n, x)
|
||||||
return math.min(x, math.max(v, n))
|
return math.min(x, math.max(v, n))
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
|
|
||||||
-- Vector math class with operators
|
-- Vector math class with operators
|
||||||
local vector_meta
|
local vector_meta
|
||||||
local vector_new
|
local vector_new
|
||||||
local function vector_check(v, n, name, argn)
|
local function vector_check(v, n, name, argn)
|
||||||
if not v.__is_vector then
|
if not v.__is_vector then
|
||||||
error('vector ' .. name .. ': argument #' .. (argn or 1)
|
error('vector ' .. name .. ': argument #' .. (argn or 1)
|
||||||
..': expected vector, got '..type(v), n+1) end
|
.. ': expected vector, got ' .. type(v), n + 1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
local function vector_checksamelen(v1, v2, name)
|
local function vector_checksamelen(v1, v2, name)
|
||||||
vector_check(v1, 3, name, 1)
|
vector_check(v1, 3, name, 1)
|
||||||
vector_check(v2, 3, name, 2)
|
vector_check(v2, 3, name, 2)
|
||||||
if #v1 ~= #v2 then
|
if #v1 ~= #v2 then
|
||||||
error('vector ' .. name .. ': vector lengths do not match (lengths are '
|
error('vector ' .. name .. ': vector lengths do not match (lengths are '
|
||||||
..#v1..' and '..#v2..')', 3) end
|
.. #v1 .. ' and ' .. #v2 .. ')', 3)
|
||||||
|
end
|
||||||
return #v1
|
return #v1
|
||||||
end
|
end
|
||||||
local function vector_checklen(v1, v2, name, len)
|
local function vector_checklen(v1, v2, name, len)
|
||||||
@@ -20,7 +21,8 @@ local function vector_checklen(v1, v2, name, len)
|
|||||||
vector_check(v2, 3, name, 2)
|
vector_check(v2, 3, name, 2)
|
||||||
if #v1 ~= len or #v2 ~= len then
|
if #v1 ~= len or #v2 ~= len then
|
||||||
error('vector ' .. name .. ': vector lengths are not ' .. len .. ' (lengths are '
|
error('vector ' .. name .. ': vector lengths are not ' .. len .. ' (lengths are '
|
||||||
..#v1..' and '..#v2..')', 3) end
|
.. #v1 .. ' and ' .. #v2 .. ')', 3)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
local function vector_opnnn(name, op)
|
local function vector_opnnn(name, op)
|
||||||
return function(v1, v2)
|
return function(v1, v2)
|
||||||
@@ -69,15 +71,21 @@ local vector_indices = {x = 1, y = 2, z = 3, w = 4, r = 1, g = 2, b = 3, a = 4}
|
|||||||
local vector_meta = {
|
local vector_meta = {
|
||||||
__is_vector = true,
|
__is_vector = true,
|
||||||
__index = function(t, k)
|
__index = function(t, k)
|
||||||
if tonumber(k) then return rawget(t, k)
|
if tonumber(k) then
|
||||||
elseif vector_indices[k] then return rawget(t, vector_indices[k])
|
return rawget(t, k)
|
||||||
else return getmetatable(t)[k]
|
elseif vector_indices[k] then
|
||||||
|
return rawget(t, vector_indices[k])
|
||||||
|
else
|
||||||
|
return getmetatable(t)[k]
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
__newindex = function(t, k, v)
|
__newindex = function(t, k, v)
|
||||||
if tonumber(k) then rawset(t, k, v)
|
if tonumber(k) then
|
||||||
elseif vector_indices[k] then rawset(t, vector_indices[k], v)
|
rawset(t, k, v)
|
||||||
else return
|
elseif vector_indices[k] then
|
||||||
|
rawset(t, vector_indices[k], v)
|
||||||
|
else
|
||||||
|
return
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
__add = vector_opnnn('add', function(x1, x2) return x1 + x2 end),
|
__add = vector_opnnn('add', function(x1, x2) return x1 + x2 end),
|
||||||
@@ -115,8 +123,11 @@ local vector_meta = {
|
|||||||
local len = #v1
|
local len = #v1
|
||||||
local v3 = {}
|
local v3 = {}
|
||||||
for i = 1, len do
|
for i = 1, len do
|
||||||
if length==0 then v3[i] = 0
|
if length == 0 then
|
||||||
else v3[i] = v1[i]/length end
|
v3[i] = 0
|
||||||
|
else
|
||||||
|
v3[i] = v1[i] / length
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return vector_new(v3)
|
return vector_new(v3)
|
||||||
end,
|
end,
|
||||||
@@ -152,20 +163,28 @@ local vector_meta = {
|
|||||||
rotateByAngleId = function(v1, r)
|
rotateByAngleId = function(v1, r)
|
||||||
--vector_check(v1, 2, 'rotate')
|
--vector_check(v1, 2, 'rotate')
|
||||||
if type(r) ~= 'number' or r % 1 ~= 0 then
|
if type(r) ~= 'number' or r % 1 ~= 0 then
|
||||||
error('vector rotateByAngleId: invalid rotation '..tostring(r), 2) end
|
error('vector rotateByAngleId: invalid rotation ' .. tostring(r), 2)
|
||||||
|
end
|
||||||
r = r % 4
|
r = r % 4
|
||||||
local v2
|
local v2
|
||||||
if r==0 then v2 = vector_new{ v1[1], v1[2], v1[3] }
|
if r == 0 then
|
||||||
elseif r==1 then v2 = vector_new{ v1[2], -v1[1], v1[3] }
|
v2 = vector_new { v1[1], v1[2], v1[3] }
|
||||||
elseif r==2 then v2 = vector_new{ -v1[1], -v1[2], v1[3] }
|
elseif r == 1 then
|
||||||
elseif r==3 then v2 = vector_new{ -v1[2], v1[1], v1[3] }
|
v2 = vector_new { v1[2], -v1[1], v1[3] }
|
||||||
else error('vector rotateByAngleId: invalid rotation '..r, 2) end
|
elseif r == 2 then
|
||||||
|
v2 = vector_new { -v1[1], -v1[2], v1[3] }
|
||||||
|
elseif r == 3 then
|
||||||
|
v2 = vector_new { -v1[2], v1[1], v1[3] }
|
||||||
|
else
|
||||||
|
error('vector rotateByAngleId: invalid rotation ' .. r, 2)
|
||||||
|
end
|
||||||
return v2
|
return v2
|
||||||
end,
|
end,
|
||||||
rotateZ = function(v, r)
|
rotateZ = function(v, r)
|
||||||
--vector_check(v, 2, 'rotate2d')
|
--vector_check(v, 2, 'rotate2d')
|
||||||
if type(r) ~= 'number' then
|
if type(r) ~= 'number' then
|
||||||
error('vector rotateZ: invalid rotation '..tostring(r), 2) end
|
error('vector rotateZ: invalid rotation ' .. tostring(r), 2)
|
||||||
|
end
|
||||||
local len = math.sqrt(v[1] ^ 2 + v[2] ^ 2)
|
local len = math.sqrt(v[1] ^ 2 + v[2] ^ 2)
|
||||||
local ang = math.atan2(v[2], v[1]) + r
|
local ang = math.atan2(v[2], v[1]) + r
|
||||||
local v2 = vector_new { math.cos(ang) * len, math.sin(ang) * len }
|
local v2 = vector_new { math.cos(ang) * len, math.sin(ang) * len }
|
||||||
|
|||||||
Reference in New Issue
Block a user