-- Imports ----------------------------------------------------------------------------------------- local core = _G.core local jvmi = _G.jvmi local string = _G.string -- Private ----------------------------------------------------------------------------------------- local private = {} --[[ Extract the short name from a given mod name. Args: name:string - the full name of the mod Returns: the short name of the mod (i.e., without the "jvmi_" prefix) Errors: if the name doesn't have the "jvmi_" prefix ]]-- function private.extract_short_name(name) local prefix_index = string.find(name, "jvmi_", 1, true) if prefix_index ~= 1 then error(name.."does not have a 'jvmi_' prefix") end return string.sub(name, 6) end --[[ Load a script which is local to a specific mod. Args: mod:table - the mod's global table (which must still include the `base_dir` key) ]]-- function private.domodfile(mod, path) dofile(mod.base_dir.."/"..path) end -- Global ------------------------------------------------------------------------------------------ --[[ Register a JVMI mod with the global table. This will create a new table for the mod's use and populate it with a few useful items. The table will be returned, and the mod can then use it to store its own global state. The following keys will be pre-populated for each mod: base_dir:string - the location on disk where the mod lives domodfile:function(path:string) - run `dofile` on a script in the mod's base dir name:string - the full name of the mod, as given short_name:string - the name of the mod without the "jvmi_" prefix The mod's table is also added to `jvmi.mods[mod.short_name]`, so that it may be accessed as needed by other mods. Args: name:string - a string giving the full name of the mod "jvmi_..." Returns: a table the mod can use to store its own data ]]-- function jvmi.register_mod(name) local short_name = private.extract_short_name(name) local mod = jvmi.mods[short_name] if mod then return mod end mod = { base_dir = jvmi.base_dir.."/mods/"..name, name = name, short_name = short_name, } mod.domodfile = function(path) private.domodfile(mod, path) end if jvmi.in_game then mod.S = core.get_translator(name) else mod.S = function(text) return text end end jvmi.mods[short_name] = mod return mod end --[[ Obtain the global table associated with the named mod. This function will return the existing table if the mod is already registered, or attempt to load the mod and return the newly-registered table if not. If this function is called in the "main" env, it will raise an error if the mod isn't already registered, since mods should use the `depends` entry in their `mod.conf` file to ensure their dependencies are loaded instead of using this function. Args: name:string - the full name of the mod to load ]]-- function jvmi.require_mod(name) local short_name = private.extract_short_name(name) local mod = jvmi.mods[short_name] if mod then return mod end if jvmi.env == "main" then error("Update mod.conf:depends to include "..name) end local init_path = jvmi.base_dir.."/mods/"..name.."/init.lua" dofile(init_path) mod = jvmi.mods[short_name] if not mod then error("Loading "..init_path.." didn't register "..name) end return mod end