From 495f0dc68abb8415ec206de4ddaed3b4650375da Mon Sep 17 00:00:00 2001 From: Hersi Date: Sat, 19 Aug 2023 04:25:27 +0200 Subject: [PATCH] lots of refactoring added core scripts, which overwrite and/or extend built-in functionality --- scripts/api/filesys.lua | 17 --- scripts/api/platform.lua | 39 ------- scripts/api/platform/win/filesys.lua | 46 -------- scripts/common/filereader.lua | 105 ----------------- scripts/common/gameconfig.lua | 33 +++++- scripts/common/globals.lua | 22 ---- scripts/common/util.lua | 64 ----------- scripts/common/version.lua | 26 ++--- scripts/components/footer.lua | 2 +- scripts/core/filesys.lua | 13 +++ scripts/core/game.lua | 108 ++++++++++++++++++ scripts/core/init.lua | 6 + scripts/core/math.lua | 50 ++++++++ scripts/core/os.lua | 47 ++++++++ .../platform/filesys.lua} | 52 ++++----- .../{api => core}/platform/linux/filesys.lua | 25 ++-- scripts/core/platform/win/filesys.lua | 56 +++++++++ scripts/core/string.lua | 23 ++++ scripts/core/table.lua | 13 +++ scripts/language/call.lua | 22 ---- scripts/language/init.lua | 15 +++ scripts/lib/easing.lua | 43 +++---- scripts/result.lua | 2 +- scripts/titlescreen/boot.lua | 2 +- scripts/titlescreen/mainmenu.lua | 4 +- .../titlescreen/modeselect/modeselectpage.lua | 2 +- .../titlescreen/service/versioninfopage.lua | 2 +- scripts/titlescreen/title.lua | 4 +- 28 files changed, 442 insertions(+), 401 deletions(-) delete mode 100644 scripts/api/filesys.lua delete mode 100644 scripts/api/platform.lua delete mode 100644 scripts/api/platform/win/filesys.lua delete mode 100644 scripts/common/filereader.lua delete mode 100644 scripts/common/globals.lua create mode 100644 scripts/core/filesys.lua create mode 100644 scripts/core/game.lua create mode 100644 scripts/core/init.lua create mode 100644 scripts/core/math.lua create mode 100644 scripts/core/os.lua rename scripts/{api/platform/filesys_impl.lua => core/platform/filesys.lua} (71%) rename scripts/{api => core}/platform/linux/filesys.lua (50%) create mode 100644 scripts/core/platform/win/filesys.lua create mode 100644 scripts/core/string.lua create mode 100644 scripts/core/table.lua delete mode 100644 scripts/language/call.lua create mode 100644 scripts/language/init.lua diff --git a/scripts/api/filesys.lua b/scripts/api/filesys.lua deleted file mode 100644 index 0609dd0..0000000 --- a/scripts/api/filesys.lua +++ /dev/null @@ -1,17 +0,0 @@ -require "api.platform" - -local FileSys = require "api.platform.filesys_impl" - -if os.name() == Platform.WINDOWS then - require "api.platform.win.filesys" -elseif os.name() == Platform.LINUX then - require "api.platform.linux.filesys" -elseif os.name() == Platform.MACOS then - game.Log("MacOS specific implementation missing, loading linux filesys module", game.LOGGER_WARNING) - require "api.platform.linux.filesys" -else - game.Log("OS Platform not recognized, loading linux filesys module", game.LOGGER_WARNING) - require "api.platform.linux.filesys" -end - -return FileSys diff --git a/scripts/api/platform.lua b/scripts/api/platform.lua deleted file mode 100644 index 817301a..0000000 --- a/scripts/api/platform.lua +++ /dev/null @@ -1,39 +0,0 @@ - ----@note setup code from: https://stackoverflow.com/a/30960054 -local BinaryFormat = package.cpath:match("%p[\\|/]?%p(%a+)") - -if BinaryFormat == "dll" then - function os.name() - return "Windows" - end -elseif BinaryFormat == "so" then - function os.name() - return "Linux" - end -elseif BinaryFormat == "dylib" then - function os.name() - return "MacOS" - end -end - -BinaryFormat = nil - -Platform = { - WINDOWS = "Windows", - LINUX = "Linux", - MACOS = "MacOS" -} - ----Get OS platform lua is running on -function GetPlatform() - return os.name() -end - ----Merge base module table with implementation table, overwriting base ----@param base any ----@param implementation any -function MergeModules(base, implementation) - for key, value in pairs(implementation) do - base[key] = value - end -end diff --git a/scripts/api/platform/win/filesys.lua b/scripts/api/platform/win/filesys.lua deleted file mode 100644 index 826c3e4..0000000 --- a/scripts/api/platform/win/filesys.lua +++ /dev/null @@ -1,46 +0,0 @@ -local FileSys = require "api.platform.filesys_impl" - -FileSys.sep = "\\" - -function FileSys.getcwd() - local cwd, popen = "", io.popen - local pfile, err = popen("cd") - - if not pfile or err ~= 0 then - game.Log(tostring(FileSys) .. ".getcwd() : popen failed executing " .. tostring(err), game.LOGGER_ERROR) - return nil - end - - cwd = pfile:read() - pfile:close() - - return cwd -end - -local baseNormpath = FileSys.normpath -function FileSys.normpath(path) - path = baseNormpath(path) - - path = path:gsub("/", "\\") - - return path -end - -function FileSys.scandir(path) - local i, t, popen = 0, {}, io.popen - local pfile, err = popen('dir "' .. path .. '" /b /ad') - - if not pfile or err ~= 0 then - game.Log(tostring(FileSys) .. ".scandir() : popen failed executing " .. tostring(err), game.LOGGER_ERROR) - return nil - end - - for filename in pfile:lines() do - i = i + 1 - t[i] = filename - end - - pfile:close() - - return t -end diff --git a/scripts/common/filereader.lua b/scripts/common/filereader.lua deleted file mode 100644 index 122a0a5..0000000 --- a/scripts/common/filereader.lua +++ /dev/null @@ -1,105 +0,0 @@ -require("common.globals") ---file reader utility functions - ----Get game path ----@return string, string -local function getGamePath() - return debug.getinfo(1,"S").source:sub(2):match("(.*)([\\/])skins") -- this is very hacky :) -end - -local function readBytes(_file) - local out = {} - repeat - local buffer = _file:read(4*1024) - for c in (buffer or ''):gmatch(".") do - table.insert(out, c:byte()) - end - until not buffer - return out -end - ----Read a file in the game folder by lines ----@param path string relative path to game file ----@param mode? openmode default "r" ----@return nil|string[] -function ReadGameFileLines(path, mode) - mode = mode or "r" - - local gamepath, sep = getGamePath() - - local lines = {} - - local f = io.open(gamepath .. sep .. path, mode) - if not f then return nil end - - for line in f:lines("l") do - table.insert(lines, line) - end - f:close() - - return lines -end - ----Read a file in the game folder ----@param path string # relative path to game file ----@param mode? openmode # default "r" ----@return nil|string|integer[] -function ReadGameFile(path, mode) - mode = mode or "r" - - local gamepath, sep = getGamePath() - local out - - local f = io.open(gamepath .. sep .. path, mode) - if not f then return nil end - - if mode:match(".*b") then - out = readBytes(f) - else - out = f:read("a") - end - f:close() - - return out -end - ----Find patterns in file ----@param path string # relative path to game file ----@param pattern string # search pattern ----@return table # {{group1, group2, ...}, ...} -function FindPatterns(path, pattern) - local matches = {} - for _, line in ipairs(ReadGameFileLines(path, "r")) do - if line:match(pattern) then - table.insert(matches, {line:match(pattern)}) - end - end - return matches -end - ---- Check if a file or directory exists in this path ----@param file string # relative path to game file ----@return boolean # file exists ----@return string? # error message -function IsFileExists(file) - local gamepath, sep = getGamePath() - file = gamepath .. sep .. file - - local ok, err, code = os.rename(file, file) - if not ok then - game.Log("err: "..err..", code: "..code, game.LOGGER_DEBUG) - if code == 13 then - -- Permission denied, but it exists - return true - end - end - return ok, err -end - ---- Check if a directory exists in this path ----@param path string # relative path to game directory ----@return boolean # directory exists -function IsDir(path) - -- "/" works on both Unix and Windows - return IsFileExists(path .. "/") -end diff --git a/scripts/common/gameconfig.lua b/scripts/common/gameconfig.lua index 06accbf..d01adca 100644 --- a/scripts/common/gameconfig.lua +++ b/scripts/common/gameconfig.lua @@ -1,11 +1,36 @@ -require("common.filereader") +require("core.game") GameConfig = {} -function RefreshConfig() - for _, match in ipairs(FindPatterns("Main.cfg", "(%w*)%s*=%s*\"?([^\"%s]*)\"?")) do +---Find patterns in file +---@param path string # relative path to game file +---@param pattern string # search pattern +---@return table? matches # {{group1, group2, ...}, ...} +---@return string? errmsg +local function match_all(path, pattern) + local matches = {} + local text, err = game.file.open(path, "r") + if not text then + return nil, err + end + for line in text:lines() do + if line:match(pattern) then + table.insert(matches, {line:match(pattern)}) + end + end + return matches +end + +function GameConfig.refresh() + local config, err = match_all("Main.cfg", "(%w*)%s*=%s*\"?([^\"%s]*)\"?") + if not config then + game.Log("Unable to refresh GameConfig, " .. err, game.LOGGER_ERROR) + return + end + + for _, match in ipairs(config) do GameConfig[match[1]] = match[2] end end -RefreshConfig() \ No newline at end of file +GameConfig.refresh() diff --git a/scripts/common/globals.lua b/scripts/common/globals.lua deleted file mode 100644 index 029b028..0000000 --- a/scripts/common/globals.lua +++ /dev/null @@ -1,22 +0,0 @@ ----Drewol, what are you doing? Why is there no game.LOGGER_DEBUG? -game.LOGGER_DEBUG = 0 - --- add missing inputs -game.MOUSE_LEFT = 0 -game.MOUSE_RIGHT = 1 -game.MOUSE_MIDDLE = 2 - -game.KNOB_LEFT = 0 -game.KNOB_RIGHT = 1 - --- some cool extensions to the builtins ---------------------------------------- - ----Looks for the last match of pattern in the string. ----@param s string ----@param pattern string ----@param init? integer ----@param plain? boolean -function string.rfind(s, pattern, init, plain) - pattern = pattern:reverse() - return s:len() - s:reverse():find(pattern, init, plain) + 1 -end diff --git a/scripts/common/util.lua b/scripts/common/util.lua index 039a8bf..66a3183 100644 --- a/scripts/common/util.lua +++ b/scripts/common/util.lua @@ -1,64 +1,7 @@ -local function split(s, delimiter) - result = {}; - for match in (s..delimiter):gmatch("(.-)"..delimiter) do - table.insert(result, match); - end - return result; -end - -local function filter(tableIn, predicate) - local out = {} - for _, val in ipairs(tableIn) do - if predicate(val) then - table.insert(out, val) - end - end - return out -end - -local function clamp(x, min, max) - if x < min then - x = min - end - if x > max then - x = max - end - - return x -end - -local function round(num) - return num + (2^52 + 2^51) - (2^52 + 2^51) -end - -local function sign(x) - return ( - (x > 0) and 1 - or - (x < 0) and -1 - or - 0 - ) -end - -local function roundToZero(x) - if x < 0 then - return math.ceil(x) - elseif x > 0 then - return math.floor(x) - else - return 0 - end -end - local function areaOverlap(x, y, areaX, areaY, areaW, areaH) return x > areaX and y > areaY and x < areaX + areaW and y < areaY + areaH end -local function lerp(x, x0, y0, x1, y1) - return y0 + (x - x0) * (y1 - y0) / (x1 - x0) -end - --modulo operation for index value local function modIndex(index, mod) return (index - 1) % mod + 1 @@ -76,14 +19,7 @@ local function firstAlphaNum(s) end return { - split = split, - filter = filter, - clamp = clamp, - round = round, - sign = sign, - roundToZero = roundToZero, areaOverlap = areaOverlap, - lerp = lerp, modIndex = modIndex, firstAlphaNum = firstAlphaNum, } diff --git a/scripts/common/version.lua b/scripts/common/version.lua index b8339de..022e076 100644 --- a/scripts/common/version.lua +++ b/scripts/common/version.lua @@ -1,21 +1,21 @@ -local MAJOR = 0 -local MINOR = 2 -local PATCH = 2 +local major = 0 +local minor = 2 +local patch = 2 -local function getLongVersion() - return "USC:E:G:S:" .. MAJOR .. MINOR .. PATCH +local function long_version() + return "USC:E:G:S:" .. major .. minor .. patch end ---Get version string ---@return string -local function getVersion() - return table.concat({MAJOR, MINOR, PATCH}, ".") +local function version() + return table.concat({major, minor, patch}, ".") end return { - MAJOR = MAJOR, - MINOR = MINOR, - PATCH = PATCH, - getLongVersion = getLongVersion, - getVersion = getVersion -} \ No newline at end of file + major = major, + minor = minor, + patch = patch, + long_version = long_version, + version = version +} diff --git a/scripts/components/footer.lua b/scripts/components/footer.lua index 89660ef..b345d0c 100644 --- a/scripts/components/footer.lua +++ b/scripts/components/footer.lua @@ -31,7 +31,7 @@ local function drawFooter() gfx.FontSize(20) gfx.TextAlign(gfx.TEXT_ALIGN_LEFT + gfx.TEXT_ALIGN_TOP) gfx.FillColor(255, 255, 255, 255) - gfx.Text("EXPERIMENTALGEAR " .. version.MAJOR .. "." .. version.MINOR .. "." .. version.PATCH .. "", 8, 1895) + gfx.Text("EXPERIMENTALGEAR " .. version.major .. "." .. version.minor .. "." .. version.patch .. "", 8, 1895) end local function progressTransitions(deltaTime) diff --git a/scripts/core/filesys.lua b/scripts/core/filesys.lua new file mode 100644 index 0000000..c94fbe1 --- /dev/null +++ b/scripts/core/filesys.lua @@ -0,0 +1,13 @@ +require("core.os") + +if os.platform() == Platform.WINDOWS then + require "core.platform.win.filesys" +elseif os.platform() == Platform.LINUX then + require "core.platform.linux.filesys" +elseif os.platform() == Platform.MACOS then + game.Log("MacOS platform not implemented, loading linux filesys module as fallback", game.LOGGER_WARNING) + require "core.platform.linux.filesys" +else + game.Log("OS Platform not recognized, loading linux filesys module", game.LOGGER_WARNING) + require "core.platform.linux.filesys" +end diff --git a/scripts/core/game.lua b/scripts/core/game.lua new file mode 100644 index 0000000..44396ad --- /dev/null +++ b/scripts/core/game.lua @@ -0,0 +1,108 @@ +---Drewol, what are you doing? Why is there no game.LOGGER_DEBUG? + +game.LOGGER_DEBUG = 0 + +-- add missing inputs + +game.MOUSE_LEFT = 0 +game.MOUSE_RIGHT = 1 +game.MOUSE_MIDDLE = 2 + +game.KNOB_LEFT = 0 +game.KNOB_RIGHT = 1 + +--file reader utility functions +require("core.filesys") + +game.file = { + __name = "game.file" +} + +---Get game path +---@return string, string # path, separator +local function get_game_path() + return debug.getinfo(1,"S").source:sub(2):match("(.*)([\\/])skins") -- this is very hacky :) +end + +---Read bytes out of binary file +---@param _file file* +---@return integer[] +local function read_bytes(_file) + local out = {} + repeat + local buffer = _file:read(4*1024) + for c in (buffer or ''):gmatch(".") do + table.insert(out, c:byte()) + end + until not buffer + return out +end + +---Read a file in the game folder +---@param path string # relative path to game file +---@param mode? openmode # default "r" +---@return file*? lines +---@return string? errmsg +function game.file.open(path, mode) + mode = mode or "r" + local gamepath, sep = get_game_path() + + return io.open(gamepath .. sep .. path, mode) +end + +---Read a file in the game folder +---@param path string # relative path to game file +---@return string? text +---@return string? errmsg +function game.file.readfile(path) + local gamepath, sep = get_game_path() + local out + + local f, err = io.open(gamepath .. sep .. path) + if not f then + return nil, err + end + + out = f:read("a") + f:close() + + return out +end + +---Read a file in the game folder +---@param path string # relative path to game file +---@return integer[]? bytes +---@return string? errmsg +function game.file.readbinary(path) + local gamepath, sep = get_game_path() + local out + + local f, err = io.open(gamepath .. sep .. path, "rb") + if not f then + return nil, err + end + + out = read_bytes(f) + f:close() + + return out +end + +--- Check if a file or directory exists at the path +---@param path string # relative path to game file +---@return boolean # file exists +---@return string? # error message +function game.file.exists(path) + local gamepath, sep = get_game_path() + path = gamepath .. sep .. path + return filesys.exists(path) +end + +--- Check if a directory exists in this path +---@param path string # relative path to game directory +---@return boolean # directory exists +---@return string? # error message +function game.file.isdir(path) + -- "/" works on both Unix and Windows + return game.file.exists(path .. "/") +end diff --git a/scripts/core/init.lua b/scripts/core/init.lua new file mode 100644 index 0000000..3182c41 --- /dev/null +++ b/scripts/core/init.lua @@ -0,0 +1,6 @@ +require("core.game") +require("core.os") +require("core.filesys") +require("core.math") +require("core.string") +require("core.table") diff --git a/scripts/core/math.lua b/scripts/core/math.lua new file mode 100644 index 0000000..92416ed --- /dev/null +++ b/scripts/core/math.lua @@ -0,0 +1,50 @@ +---Clamp value between minimum and maximum +---@param x number +---@param min number +---@param max number +---@return number +function math.clamp(x, min, max) + if x < min then + x = min + end + + if x > max then + x = max + end + + return x +end + +---Return closest numerical value to number +---@param num number +---@return integer +---@note From https://stackoverflow.com/a/76576968 +function math.round(num) + return math.floor((math.floor(num*2) + 1)/2) +end + +---Return sign of number +---@param x number +---@return integer +function math.sign(x) + if (x > 0) then + return 1 + end + + if (x < 0) then + return -1 + end + + return 0 +end + +---Linear interpolation +---@param x number # x to interpolate +---@param x0 number # x value begin +---@param x1 number # x value end +---@param y0 number # y value at x0 +---@param y1 number # y value at x1 +---@return number # interpolated y value at x +function math.lerp(x, x0, x1, y0, y1) + return y0 + (x - x0) * (y1 - y0) / (x1 - x0) +end diff --git a/scripts/core/os.lua b/scripts/core/os.lua new file mode 100644 index 0000000..152f9bf --- /dev/null +++ b/scripts/core/os.lua @@ -0,0 +1,47 @@ +---@enum Platform +Platform = { + WINDOWS = 0, + LINUX = 1, + MACOS = 2, + UNKNOWN = 3 +} + +function os.platform() + return Platform.UNKNOWN +end + +function os.name() + return "Unknown" +end + +---@note setup code from: https://stackoverflow.com/a/30960054 +local binary_format = package.cpath:match("%p[\\|/]?%p(%a+)") + +if binary_format == "dll" then + function os.platform() + return Platform.WINDOWS + end + + function os.name() + return "Windows" + end + +elseif binary_format == "so" then + function os.platform() + return Platform.LINUX + end + + function os.name() + return "Linux" + end + +elseif binary_format == "dylib" then + function os.platform() + return Platform.MACOS + end + + function os.name() + return "MacOS" + end + +end diff --git a/scripts/api/platform/filesys_impl.lua b/scripts/core/platform/filesys.lua similarity index 71% rename from scripts/api/platform/filesys_impl.lua rename to scripts/core/platform/filesys.lua index d96ad93..2e94256 100644 --- a/scripts/api/platform/filesys_impl.lua +++ b/scripts/core/platform/filesys.lua @@ -1,36 +1,38 @@ -require "common.globals" +---@diagnostic disable: duplicate-set-field ----@class FileSys -local FileSys = { - __name = "FileSys", +require("core.string") + +---@class filesys +filesys = { + __name = "filesys", sep = "/" } ---Return a normalized absolutized version of `path` ---@param path string ---@return string -function FileSys.abspath(path) - return FileSys.normpath(FileSys.join(FileSys.getcwd(), path)) +function filesys.abspath(path) + return filesys.normpath(filesys.join(filesys.getcwd(), path)) end ---Return the base name of `path` ---@param path string ---@return string -function FileSys.basename(path) - return string.sub(path, string.rfind(path, FileSys.sep, 1, true) + 1) +function filesys.basename(path) + return string.sub(path, string.rfind(path, filesys.sep, 1, true) + 1) end ---Return the directory name of `path` ---@param path string ---@return string -function FileSys.dirname(path) - return FileSys.split(path)[1] +function filesys.dirname(path) + return filesys.split(path)[1] end ---Return true if `path` refers to an existing path ---@param path string ---@return boolean, string? -function FileSys.exists(path) +function filesys.exists(path) local ok, err, code = os.rename(path, path) if not ok then game.Log("err: "..err..", code: "..code, game.LOGGER_DEBUG) @@ -44,8 +46,8 @@ end ---Return a string representing the current working directory ---@return string -function FileSys.getcwd() - assert(false, FileSys.__name .. ".getcwd() : Not Implemented") +function filesys.getcwd() + assert(false, filesys.__name .. ".getcwd() : Not Implemented") return "" end @@ -53,12 +55,12 @@ end ---@param path string ---@param ... string ---@return string -function FileSys.join(path, ...) +function filesys.join(path, ...) local vargs = { select(1, ...) } local joinedpath = path for _, value in ipairs(vargs) do - joinedpath = joinedpath .. FileSys.sep .. value + joinedpath = joinedpath .. filesys.sep .. value end return joinedpath @@ -67,8 +69,8 @@ end ---Normalize `path`, collapse redundant separators and up references ---@param path string ---@return string -function FileSys.normpath(path) - local sep = FileSys.sep +function filesys.normpath(path) + local sep = filesys.sep --remove multiple slashes path = path:gsub("("..sep..")"..sep.."+", "%1") @@ -93,8 +95,8 @@ end ---@param path string ---@param relativeTo? string ---@return string -function FileSys.relpath(path, relativeTo) - relativeTo = relativeTo or FileSys.getcwd() +function filesys.relpath(path, relativeTo) + relativeTo = relativeTo or filesys.getcwd() path = path:sub(path:find(relativeTo, 1, true)[2] + 1) return path end @@ -102,25 +104,23 @@ end ---Return a list of files and directory names in `path` ---@param path string ---@return string[] -function FileSys.scandir(path) - assert(false, FileSys.__name .. ".scandir() : Not Implemented") +function filesys.scandir(path) + assert(false, filesys.__name .. ".scandir() : Not Implemented") return {} end ---Split `path` to (head, tail), tail is the last component, head is everything else ---@param path string ---@return string, string -function FileSys.split(path) - local lastSep = path:rfind(FileSys.sep, 1, true) +function filesys.split(path) + local lastSep = path:rfind(filesys.sep, 1, true) return path:sub(1, lastSep), path:sub(lastSep + 1) end ---Split `path` to (root, ext), such that `root + ext == path`, ext is either empty or starts with a '.' ---@param path string ---@return string, string -function FileSys.splitext(path) +function filesys.splitext(path) local lastSep = path:rfind(".", 1, true) return path:sub(1, lastSep - 1), path:sub(lastSep) end - -return FileSys diff --git a/scripts/api/platform/linux/filesys.lua b/scripts/core/platform/linux/filesys.lua similarity index 50% rename from scripts/api/platform/linux/filesys.lua rename to scripts/core/platform/linux/filesys.lua index b97f3f6..cabc5dc 100644 --- a/scripts/api/platform/linux/filesys.lua +++ b/scripts/core/platform/linux/filesys.lua @@ -1,29 +1,36 @@ -local FileSys = require "api.platform.filesys_impl" +---@diagnostic disable: duplicate-set-field +require("core.platform.filesys") -FileSys.sep = "/" +filesys.sep = "/" -function FileSys.getcwd() +---Return a string representing the current working directory +---@return string +function filesys.getcwd() local cwd, popen = "", io.popen local pfile, err = popen("pwd") if not pfile or err ~= 0 then - game.Log(tostring(FileSys) .. ".getcwd() : popen failed executing " .. tostring(err), game.LOGGER_ERROR) - return nil + game.Log(tostring(filesys) .. ".getcwd() : popen failed executing " .. tostring(err), game.LOGGER_ERROR) + return "" end - cwd = pfile:read() + cwd = pfile:read() or "" + pfile:close() return cwd end -function FileSys.scandir(path) +---Return a list of files and directory names in `path` +---@param path string +---@return string[] +function filesys.scandir(path) local i, t, popen = 0, {}, io.popen local pfile, err = popen('find "' .. path .. '" -maxdepth 1 -print0') if not pfile or err ~= 0 then - game.Log(tostring(FileSys) .. ".scandir() : popen failed executing " .. tostring(err), game.LOGGER_ERROR) - return nil + game.Log(tostring(filesys) .. ".scandir() : popen failed executing " .. tostring(err), game.LOGGER_ERROR) + return {} end for filename in pfile:lines() do diff --git a/scripts/core/platform/win/filesys.lua b/scripts/core/platform/win/filesys.lua new file mode 100644 index 0000000..f398fd1 --- /dev/null +++ b/scripts/core/platform/win/filesys.lua @@ -0,0 +1,56 @@ +---@diagnostic disable: duplicate-set-field +require("core.platform.filesys") + +filesys.sep = "\\" + +---Return a string representing the current working directory +---@return string +function filesys.getcwd() + local cwd, popen = "", io.popen + local pfile, err = popen("cd") + + if not pfile or err ~= 0 then + game.Log(tostring(filesys) .. ".getcwd() : popen failed executing " .. tostring(err), game.LOGGER_ERROR) + return "" + end + + cwd = pfile:read() or "" + + pfile:close() + + return cwd +end + +local _normpath = filesys.normpath +---Normalize `path`, collapse redundant separators and up references +---@param path string +---@return string +function filesys.normpath(path) + path = _normpath(path) + + path = path:gsub("/", "\\") + + return path +end + +---Return a list of files and directory names in `path` +---@param path string +---@return string[] +function filesys.scandir(path) + local i, t, popen = 0, {}, io.popen + local pfile, err = popen('dir "' .. path .. '" /b /ad') + + if not pfile or err ~= 0 then + game.Log(tostring(filesys) .. ".scandir() : popen failed executing " .. tostring(err), game.LOGGER_ERROR) + return {} + end + + for filename in pfile:lines() do + i = i + 1 + t[i] = filename + end + + pfile:close() + + return t +end diff --git a/scripts/core/string.lua b/scripts/core/string.lua new file mode 100644 index 0000000..7d17e09 --- /dev/null +++ b/scripts/core/string.lua @@ -0,0 +1,23 @@ +---Looks for the last match of pattern in the string. +---@param s string +---@param pattern string +---@param init? integer +---@param plain? boolean +function string.rfind(s, pattern, init, plain) + pattern = pattern:reverse() + return s:len() - s:reverse():find(pattern, init, plain) + 1 +end + +---Split string into list +---@param s string +---@param delimiter? string # default " " +---@return string[] +function string.split(s, delimiter) + delimiter = delimiter or " " + + local result = {} + for match in (s..delimiter):gmatch("(.-)"..delimiter) do + table.insert(result, match) + end + return result +end diff --git a/scripts/core/table.lua b/scripts/core/table.lua new file mode 100644 index 0000000..24f7f38 --- /dev/null +++ b/scripts/core/table.lua @@ -0,0 +1,13 @@ +---Filter elements of a table where the predicate is true +---@param t table +---@param predicate fun(x: any): boolean +---@return table +function table.filter(t, predicate) + local out = {} + for _, val in ipairs(t) do + if predicate(val) then + table.insert(out, val) + end + end + return out +end diff --git a/scripts/language/call.lua b/scripts/language/call.lua deleted file mode 100644 index 39aa2c0..0000000 --- a/scripts/language/call.lua +++ /dev/null @@ -1,22 +0,0 @@ -local call = nil - -local EN = require("language.EN") -local DE = require("language.DE") -local SK = require("language.SK") -local HU = require("language.HU") -local test2 = require("language.test2") - -if game.GetSkinSetting('words') == "EN" then - call = EN -elseif game.GetSkinSetting('words') == "DE" then - call = DE -elseif game.GetSkinSetting('words') == "SK" then - call = SK -elseif game.GetSkinSetting('words') == "HU" then - call = HU -elseif game.GetSkinSetting('words') == "test2" then - call = test2 -end - - -return call \ No newline at end of file diff --git a/scripts/language/init.lua b/scripts/language/init.lua new file mode 100644 index 0000000..6f46421 --- /dev/null +++ b/scripts/language/init.lua @@ -0,0 +1,15 @@ +local language = nil + +if game.GetSkinSetting('words') == "EN" then + language = require("language.EN") +elseif game.GetSkinSetting('words') == "DE" then + language = require("language.DE") +elseif game.GetSkinSetting('words') == "SK" then + language = require("language.SK") +elseif game.GetSkinSetting('words') == "HU" then + language = require("language.HU") +elseif game.GetSkinSetting('words') == "test2" then + language = require("language.test2") +end + +return language diff --git a/scripts/lib/easing.lua b/scripts/lib/easing.lua index f02b327..c2f0a1e 100644 --- a/scripts/lib/easing.lua +++ b/scripts/lib/easing.lua @@ -29,13 +29,6 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -- c = change == ending - beginning -- d = duration (total time) -local sin = math.sin -local cos = math.cos -local pi = math.pi -local sqrt = math.sqrt -local abs = math.abs -local asin = math.asin - local function linear(t, b, c, d) return c * t / d + b end @@ -164,15 +157,15 @@ local function outInQuint(t, b, c, d) end local function inSine(t, b, c, d) - return -c * cos(t / d * (pi / 2)) + c + b + return -c * math.cos(t / d * (math.pi / 2)) + c + b end local function outSine(t, b, c, d) - return c * sin(t / d * (pi / 2)) + b + return c * math.sin(t / d * (math.pi / 2)) + b end local function inOutSine(t, b, c, d) - return -c / 2 * (cos(pi * t / d) - 1) + b + return -c / 2 * (math.cos(math.pi * t / d) - 1) + b end local function outInSine(t, b, c, d) @@ -221,21 +214,21 @@ end local function inCirc(t, b, c, d) t = t / d - return(-c * (sqrt(1 - (t ^ 2)) - 1) + b) + return(-c * (math.sqrt(1 - (t ^ 2)) - 1) + b) end local function outCirc(t, b, c, d) t = t / d - 1 - return(c * sqrt(1 - (t ^ 2)) + b) + return(c * math.sqrt(1 - (t ^ 2)) + b) end local function inOutCirc(t, b, c, d) t = t / d * 2 if t < 1 then - return -c / 2 * (sqrt(1 - t * t) - 1) + b + return -c / 2 * (math.sqrt(1 - t * t) - 1) + b else t = t - 2 - return c / 2 * (sqrt(1 - t * t) + 1) + b + return c / 2 * (math.sqrt(1 - t * t) + 1) + b end end @@ -258,16 +251,16 @@ local function inElastic(t, b, c, d, a, p) local s - if not a or a < abs(c) then + if not a or a < math.abs(c) then a = c s = p / 4 else - s = p / (2 * pi) * asin(c/a) + s = p / (2 * math.pi) * math.asin(c/a) end t = t - 1 - return -(a * (2 ^ (10 * t)) * sin((t * d - s) * (2 * pi) / p)) + b + return -(a * (2 ^ (10 * t)) * math.sin((t * d - s) * (2 * math.pi) / p)) + b end -- a: amplitud @@ -283,14 +276,14 @@ local function outElastic(t, b, c, d, a, p) local s - if not a or a < abs(c) then + if not a or a < math.abs(c) then a = c s = p / 4 else - s = p / (2 * pi) * asin(c/a) + s = p / (2 * math.pi) * math.asin(c/a) end - return a * (2 ^ (10 * t)) * sin((t * d - s) * (2 * pi) / p) + c + b + return a * (2 ^ (10 * t)) * math.sin((t * d - s) * (2 * math.pi) / p) + c + b end -- p = period @@ -307,19 +300,19 @@ local function inOutElastic(t, b, c, d, a, p) local s - if not a or a < abs(c) then + if not a or a < math.abs(c) then a = c s = p / 4 else - s = p / (2 * pi) * asin(c / a) + s = p / (2 * math.pi) * math.asin(c / a) end if t < 1 then t = t - 1 - return -0.5 * (a * (2 ^ (10 * t)) * sin((t * d - s) * (2 * pi) / p)) + b + return -0.5 * (a * (2 ^ (10 * t)) * math.sin((t * d - s) * (2 * math.pi) / p)) + b else t = t - 1 - return a * (2 ^ (-10 * t)) * sin((t * d - s) * (2 * pi) / p ) * 0.5 + c + b + return a * (2 ^ (-10 * t)) * math.sin((t * d - s) * (2 * math.pi) / p ) * 0.5 + c + b end end @@ -443,4 +436,4 @@ return { outBounce = outBounce, inOutBounce = inOutBounce, outInBounce = outInBounce, -} \ No newline at end of file +} diff --git a/scripts/result.lua b/scripts/result.lua index 08bc78a..d3f1dc3 100644 --- a/scripts/result.lua +++ b/scripts/result.lua @@ -4,7 +4,7 @@ local Background = require('components.background'); local Footer = require('components.footer'); local Numbers = require('components.numbers') local DiffRectangle = require('components.diff_rectangle'); -local lang = require("language.call") +local lang = require("language") local creww = game.GetSkinSetting("single_idol") diff --git a/scripts/titlescreen/boot.lua b/scripts/titlescreen/boot.lua index 697595b..3f45fc1 100644 --- a/scripts/titlescreen/boot.lua +++ b/scripts/titlescreen/boot.lua @@ -33,7 +33,7 @@ function BootPage.new(params) self:onInvalidation(reason) end - self:addField(ServiceField.new{posX = 32, posY = 32, label = Version.getLongVersion(), value = ""}) + self:addField(ServiceField.new{posX = 32, posY = 32, label = Version.long_version(), value = ""}) self:addField(ServiceField.new{posX = 64, posY = 64, label = "UNNAMED SDVX CLONE STARTUP...", value = ""}) local valueOffX = 220 diff --git a/scripts/titlescreen/mainmenu.lua b/scripts/titlescreen/mainmenu.lua index 523be84..ac31c9c 100644 --- a/scripts/titlescreen/mainmenu.lua +++ b/scripts/titlescreen/mainmenu.lua @@ -5,7 +5,7 @@ local Footer = require("components.footer") local Wallpaper = require("components.wallpaper") local Background = require("components.background") local Dim = require("common.dimensions") -local lang = require("language.call") +local lang = require("language") local util = require("common.util") local cursorIndex = 3 @@ -516,4 +516,4 @@ function MainMenuPage:drawForeground(deltaTime) Footer.draw(deltaTime) end -return MainMenuPage \ No newline at end of file +return MainMenuPage diff --git a/scripts/titlescreen/modeselect/modeselectpage.lua b/scripts/titlescreen/modeselect/modeselectpage.lua index dcc01f7..5a5b654 100644 --- a/scripts/titlescreen/modeselect/modeselectpage.lua +++ b/scripts/titlescreen/modeselect/modeselectpage.lua @@ -2,7 +2,7 @@ require "common.class" local Dim = require "common.dimensions" -local Lang = require "language.call" +local Lang = require "language" local AudioSample = require "api.audiosample" local Page = require "api.page.page" diff --git a/scripts/titlescreen/service/versioninfopage.lua b/scripts/titlescreen/service/versioninfopage.lua index 6f73e6e..2a25db5 100644 --- a/scripts/titlescreen/service/versioninfopage.lua +++ b/scripts/titlescreen/service/versioninfopage.lua @@ -35,7 +35,7 @@ function VersionInfoPage.new(params) local logStr = ReadGameFile("log_usc-game.exe.txt") or ReadGameFile("log_usc-game.txt") local list = ListField.new{selectedIndex = 2, locked = true} - list:addField(ServiceField.new{label = "SKIN ID CODE", value = Version.getLongVersion(), MARGIN = {0, 0, 0, 24}}) + list:addField(ServiceField.new{label = "SKIN ID CODE", value = Version.long_version(), MARGIN = {0, 0, 0, 24}}) list:addField(UpdateField.new{label = "USC VERSION", value = getGameLogValue("Version", logStr)}) list:addField(ServiceField.new{label = "USC BRANCH", value = GameConfig["UpdateChannel"]}) list:addField(ServiceField.new{label = "USC GIT COMMIT", value = getGameLogValue("Git commit", logStr), MARGIN = {0, 0, 0, 24}}) diff --git a/scripts/titlescreen/title.lua b/scripts/titlescreen/title.lua index 714e022..5add9d1 100644 --- a/scripts/titlescreen/title.lua +++ b/scripts/titlescreen/title.lua @@ -7,7 +7,7 @@ local Image = require "api.image" local Page = require "api.page.page" -local versionString = Version.getLongVersion() +local versionString = Version.long_version() ---@class TitlePage : Page ---@field background_img Image @@ -51,4 +51,4 @@ function TitlePage:drawForeground(deltaTime) gfx.Text(versionString, 10, 10) end -return TitlePage \ No newline at end of file +return TitlePage