Some changes regarding MapGen and other modes

This commit is contained in:
2025-09-26 06:42:53 +07:00
parent bb530451d5
commit 330a847888
15 changed files with 56 additions and 28 deletions

View File

@@ -0,0 +1,91 @@
Api = {}
---@alias Domain table<string,Mod|SubDomain> Global table for accessing public classes provided by mods.
---@alias SubDomain table<string,Mod>
---@type Domain
Core = {}
---@type Domain
Environment = {}
---@type Domain
Game = {}
---@type SubDomain
Game.Items = {}
---@type SubDomain
Game.Mobs = {}
---@type SubDomain
Game.Structures = {}
Ui = {}
local allowedDomains = {
Api = true,
Core = true,
Environment = true,
Game = true,
Ui = true
}
--- Adds a mod to the global space under specified domain structure
---@param mod Mod The mod table containing functionality to expose
---@param location string Dot-separated path within domain structure (e.g. "Items.Log")
---@param force boolean? Whether to overwrite the target location.
Api.addModToGlobalSpace = function(mod, location, force)
-- Ensure `mod` type and that it's not empty
Ensure.argType(mod, 'table', 1, 'Api.addModToGlobalSpace')
-- Ensure `location` type and that it's not empty
Ensure.argType(location, 'string', 2, 'Api.addModToGlobalSpace')
Ensure.stringArgNotEmpty(location, 2, 'Api.addModToGlobalSpace')
-- Ensure `location` matches this format: 'field.subfield.subsubfield.<...>'
-- TODO: баг: ликвидные имена не определяются шаблоном
--[[if not location:match('^[%a_][%w_]*([%.][%a_][%w_]*)*$') then
error('bad argument #2 to \'Api.addModToGlobalSpace\' (invalid format)')
end--]]
-- Split `location` into fields -- TODO: !!!Make a String function!!!
local fields = {}
local domain = nil
for field in string.gmatch(location, '[^.]+') do
if domain ~= nil then
table.insert(fields, field)
else
domain = field
end
end
-- Ensure the domain is allowed
Ensure.argIsAllowed(domain, allowedDomains , 2, 'Api.addModToGlobalSpace')
-- Handle case where we're modifying the domain root directly
if #fields == 0 then
error('Not allowed to directly write into the \''.. domain ..'\' domain')
end
-- Start traversing from the domain's table
local current = _G[domain]
assert(type(current) == 'table', 'The global domain is not a table')
-- Traverse each field of the location
local num_fields = #fields
for i = 1, num_fields - 1 do
local field = fields[i]
if current[field] == nil then
current[field] = {}
end
current = current[field]
assert(type(current) == 'table', 'intermediate field is not a table')
end
-- Assign mod to the final field
local last_field = fields[num_fields]
if force and current[last_field] ~= nil then
error(string.format('field \'%s\' already exists in \'%s\'', last_field, location))
end
current[last_field] = mod
end

View File

@@ -0,0 +1,3 @@
name = smc__api__api_loader
description = Include this mod in your `depends` to ensure all necessary APIs are loaded for your mod
depends = smc__api__ensure, smc__api__math, smc__api__mod, smc__api__require, smc__api__table, smc__api__string, smc__api__utf8,