Persistent Tables: refactoring, added ability to set default table

This commit is contained in:
bi 2018-06-07 13:18:24 +03:00
parent 9b9211a8b1
commit 14a6bdfa0f
2 changed files with 18 additions and 15 deletions

View file

@ -1,30 +1,35 @@
local vmf = get_mod("VMF") local vmf = get_mod("VMF")
Managers.vmf = Managers.vmf or {} -- @TODO: move it to on_reload when it will be implemented in vt1
Managers.vmf.persistent_tables = Managers.vmf.persistent_tables or {} Managers.vmf.persistent_tables = Managers.vmf.persistent_tables or {}
local _persistent_tables = Managers.vmf.persistent_tables local _persistent_tables = Managers.vmf.persistent_tables
-- #################################################################################################################### -- #####################################################################################################################
-- ##### VMFMod ####################################################################################################### -- ##### VMFMod ########################################################################################################
-- #################################################################################################################### -- #####################################################################################################################
VMFMod.persistent_table = function (self, table_name) --[[
This function will create a table (or will use the one passed as 'default_table' argument) and then return this table
on the first call. After that, it will just pass already created table without recreating it. It will continue
returning already created table even after mods reloading.
* table_name [string]: table identifier; has the mod namespace, meaning that 2 different mods will get 2 different
tables when using the same table_name
* default_table [table] : (optional) table that will replace empty table created by default (on the 1st call)
--]]
function VMFMod:persistent_table(table_name, default_table)
if vmf.check_wrong_argument_type(self, "persistent_table", "table_name", table_name, "string") then if vmf.check_wrong_argument_type(self, "persistent_table", "table_name", table_name, "string") or
vmf.check_wrong_argument_type(self, "persistent_table", "default_table", default_table, "table", "nil")
then
return return
end end
local mod_name = self:get_name() local mod_name = self:get_name()
_persistent_tables[mod_name] = _persistent_tables[mod_name] or {} _persistent_tables[mod_name] = _persistent_tables[mod_name] or default_table or {}
local mod_tables = _persistent_tables[mod_name] local mod_tables = _persistent_tables[mod_name]
mod_tables[table_name] = mod_tables[table_name] or {} mod_tables[table_name] = mod_tables[table_name] or {}
return mod_tables[table_name] return mod_tables[table_name]
end end
-- ####################################################################################################################
-- ##### Script #######################################################################################################
-- ####################################################################################################################
vmf.persistent_data = vmf:persistent_table("persistent_data")

View file

@ -11,8 +11,6 @@ local vmf_mod_object = {}
-- #################################################################################################################### -- ####################################################################################################################
function vmf_mod_object:init() function vmf_mod_object:init()
Managers.vmf = Managers.vmf or {} -- @TODO: move mod data to on_reload when it will be implemented in vt1
dofile("scripts/mods/vmf/modules/vmf_mod_manager") dofile("scripts/mods/vmf/modules/vmf_mod_manager")
dofile("scripts/mods/vmf/modules/core/events") dofile("scripts/mods/vmf/modules/core/events")
dofile("scripts/mods/vmf/modules/core/settings") dofile("scripts/mods/vmf/modules/core/settings")