commit ced2a4c3e4db89ed39f76ab7da8d54d22c040fa8 Author: David Skrundz Date: Sun Sep 15 02:06:18 2024 -0600 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c4c4ffc --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.zip diff --git a/.vscode/.lua-format b/.vscode/.lua-format new file mode 100644 index 0000000..c729fb2 --- /dev/null +++ b/.vscode/.lua-format @@ -0,0 +1,33 @@ +column_limit: 100 +indent_width: 2 +use_tab: false +tab_width: 4 +continuation_indent_width: 4 +keep_simple_control_block_one_line: false +keep_simple_function_one_line: false +align_args: true +break_after_functioncall_lp: false +break_before_functioncall_rp: false +align_parameter: true +chop_down_parameter: true +break_after_functiondef_lp: true +break_before_functiondef_rp: true +align_table_field: true +break_after_table_lb: true +break_before_table_rb: true +chop_down_table: false +chop_down_kv_table: true +column_table_limit: 100 +column_table_limit_kv: 100 +table_sep: "," +extra_sep_at_table_end: true +break_after_operator: false +single_quote_to_double_quote: true +double_quote_to_single_quote: false +spaces_before_call: 1 +spaces_inside_functiondef_parens: false +spaces_inside_functioncall_parens: false +spaces_inside_table_braces: false +spaces_around_equals_in_field: true +line_breaks_after_function_body: 1 +line_separator: input diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..9ac1454 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "koihik.vscode-lua-format", + ], +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3a7a685 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "editor.detectIndentation": false, + "editor.tabSize": 4, + "editor.insertSpaces": false, + "[lua]": { + "editor.detectIndentation": false, + "editor.tabSize": 2, + "editor.insertSpaces": true, + }, + "vscode-lua-format.configPath": ".vscode/.lua-format" +} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..2f4b83c --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,6 @@ +How to Contribute +================= + +We'd love to accept your patches and contributions to this project. +We just need you to follow the Contributor License Agreement outlined +in the latest v0.0.x of https://github.com/Skrunix/license diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..161d3b0 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,7 @@ +Skrunix Software License +======================== + +Permission to use, copy, modify, and/or distribute this software is +outlined in the latest v0.0.x of https://github.com/Skrunix/license + +THE SOFTWARE IS PROVIDED "AS IS" diff --git a/README.md b/README.md new file mode 100644 index 0000000..416a4cc --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# Standard Library + +Factorio Modding Standard Library + +--- + +### Data +```lua +fmsl.log.color = true +``` + +### Control +```lua +fmsl = require("__fmsl__.fmsl") +``` diff --git a/changelog.txt b/changelog.txt new file mode 100644 index 0000000..54150e4 --- /dev/null +++ b/changelog.txt @@ -0,0 +1,9 @@ +--------------------------------------------------------------------------------------------------- +Version: 0.0.1 +Date: 15.09.2024 + Features: + - Initial release + Scripting: + - table.remove_key + - fmsl.table.merge + - fmsl.array.concat diff --git a/data.lua b/data.lua new file mode 100644 index 0000000..6715473 --- /dev/null +++ b/data.lua @@ -0,0 +1,4 @@ +require("std.array") +require("std.table") + +fmsl = {log = require("std.log")} diff --git a/fmsl.lua b/fmsl.lua new file mode 100644 index 0000000..2d5dc98 --- /dev/null +++ b/fmsl.lua @@ -0,0 +1,4 @@ +require("std.array") +require("std.table") + +return {box = require("fmsl.box"), position = require("fmsl.position"), log = require("std.log")} diff --git a/fmsl/box.lua b/fmsl/box.lua new file mode 100644 index 0000000..712c91c --- /dev/null +++ b/fmsl/box.lua @@ -0,0 +1,48 @@ +-- Bounding-box defined by two points +-- +-- Represented as {left_top = Position, right_bottom = Position} or {Position, Position} +local Box = {} + +-- Asserts that the box is valid +-- @param box: Box or Any +-- @return ERROR +function Box.assert(box) + local box_type = type(box) + if box_type ~= "table" then + error("Unsupported box type: " .. box_type) + end + + local box_left_top = box.left_top or box[1] + local box_left_top_type = type(box_left_top) + if box_left_top_type ~= "table" then + error("Box has invalid left_top type: " .. box_left_top_type) + end + fmsl.position.assert(box_left_top) + + local box_right_bottom = box.right_bottom or box[2] + local box_right_bottom_type = type(box_right_bottom) + if box_right_bottom_type ~= "table" then + error("Box has invalid right_bottom type: " .. box_right_bottom_type) + end + fmsl.position.assert(box_right_bottom) +end + +-- Create a new box +-- @param center: Position +-- @param radius: double +-- @return Box +function Box.center_radius(center, radius) + fmsl.position.assert(center) + + local radius_type = type(radius) + if radius_type ~= "number" then + error("Radius has invalid x type: " .. radius_type) + end + + return { + left_top = fmsl.position.sub(center, radius), + right_bottom = fmsl.position.add(center, radius), + } +end + +return Box diff --git a/fmsl/position.lua b/fmsl/position.lua new file mode 100644 index 0000000..853bd17 --- /dev/null +++ b/fmsl/position.lua @@ -0,0 +1,62 @@ +-- Coordinates on a surface +-- +-- Represented as {x = double, y = double} or {double, double} +local Position = {} + +-- Asserts that the position is valid +-- @param position: Position or Any +-- @return ERROR +function Position.assert(position) + local position_type = type(position) + if position_type ~= "table" then + error("Unsupported position type: " .. position_type) + end + + local position_x_type = type(position.x or position[1]) + if position_x_type ~= "number" then + error("Position has invalid x type: " .. position_x_type) + end + + local position_y_type = type(position.y or position[2]) + if position_y_type ~= "number" then + error("Position has invalid y type: " .. position_x_type) + end +end + +-- Add the offset to the position +-- @param position: Position +-- @param offset: Position or double +-- @return Position +function Position.add(position, offset) + fmsl.position.assert(position) + + if type(offset) == "number" then + return {x = (position.x or position[1]) + offset, y = (position.y or position[2]) + offset} + end + + if type(offset) == "table" then + return {x = (position.x or position[1]) + offset, y = (position.y or position[2]) + offset} + end + + error("Unsupported offset type: " .. type(offset)) +end + +-- Subtract the offset to the position +-- @param position: Position +-- @param offset: Position or double +-- @return Position +function Position.sub(position, offset) + fmsl.position.assert(position) + + if type(offset) == "number" then + return {x = (position.x or position[1]) - offset, y = (position.y or position[2]) - offset} + end + + if type(offset) == "table" then + return {x = (position.x or position[1]) - offset, y = (position.y or position[2]) - offset} + end + + error("Unsupported offset type: " .. type(offset)) +end + +return Position diff --git a/info.json b/info.json new file mode 100644 index 0000000..4b7d9fe --- /dev/null +++ b/info.json @@ -0,0 +1,11 @@ +{ + "name": "fmsl", + "version": "0.0.1", + "title": "Standard Library", + "description": "Factorio Modding Standard Library", + "author": "David Skrundz", + "contact": "david@skrundz.ca", + "homepage": "https://git.skrundz.dev/skrundztorio/fmsl", + "factorio_version": "1.1", + "dependencies": [] +} diff --git a/package.py b/package.py new file mode 100755 index 0000000..588c21e --- /dev/null +++ b/package.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +import json +import os +from zipfile import ZipFile + +modFiles = [ + "info.json", + "changelog.txt", + "thumbnail.png", + "LICENSE.md", + + "data.lua", + "fmsl.lua", +] +modFolders = [ + "fmsl", + "std", +] + +with open("info.json") as file: + modInfo = json.load(file) + +mod_name = modInfo["name"] +mod_version = modInfo["version"] +zipName = f"{mod_name}_{mod_version}" + +with ZipFile(f"{zipName}.zip", 'w') as modZip: + for file in modFiles: + modZip.write(file, arcname=f"{zipName}/{file}") + for folder in modFolders: + for root, dirs, files in os.walk(folder): + for file in files: + filePath = os.path.join(root, file) + archivePath = os.path.relpath(filePath, os.path.join(folder, '..')) + modZip.write(filePath, arcname=f"{zipName}/{archivePath}") diff --git a/std/array.lua b/std/array.lua new file mode 100644 index 0000000..0626a5f --- /dev/null +++ b/std/array.lua @@ -0,0 +1,14 @@ +-- Concatenate multiple arrays into a new table, in order +-- @param ...: Table +-- @return Table +function table.concat_new(...) + local result = {} + for _, array in ipairs({...}) do + if array then + for _, value in ipairs(array) do + table.insert(result, value) + end + end + end + return result +end diff --git a/std/log.lua b/std/log.lua new file mode 100644 index 0000000..a7ddaed --- /dev/null +++ b/std/log.lua @@ -0,0 +1,13 @@ +local Log = { + color = false, + checkmarks = { + [false] = {[false] = "✗", [true] = "✓"}, + [true] = {[false] = "❌", [true] = "✅"}, + }, +} + +function Log.checkmark(success) + return Log.checkmarks[Log.color][success] +end + +return Log diff --git a/std/table.lua b/std/table.lua new file mode 100644 index 0000000..0cf81e1 --- /dev/null +++ b/std/table.lua @@ -0,0 +1,24 @@ +-- Remove a key from a table and return the value +-- @param table: Table +-- @param key: Any +-- @return Any +function table.remove_key(table, key) + local value = table[key] + table[key] = nil + return value +end + +-- Merge multiple tables into a new table, with later tables taking precedence +-- @param ...: Table +-- @return Table +function table.merge_new(...) + local result = {} + for _, table in ipairs({...}) do + if table then + for key, value in pairs(table) do + result[key] = value + end + end + end + return result +end diff --git a/thumbnail.png b/thumbnail.png new file mode 100644 index 0000000..2d14215 Binary files /dev/null and b/thumbnail.png differ