Automatically determine mod handle

Remove the burden fromt he modder to keep the workshop ID in sync by
determening the mod's handle during initial load.

This also removes the requirement to wait until all mods have been
loaded before being able to load packages.
This commit is contained in:
Lucas Schwiderski 2018-12-10 15:54:57 +01:00
parent 0b3b09bada
commit 5d19c8953f
3 changed files with 18 additions and 35 deletions

View file

@ -1,29 +1,29 @@
local vmf = get_mod("VMF") local vmf = get_mod("VMF")
local _initialized = false
local _mod_handles = {}
local _queued_packages = {} local _queued_packages = {}
local _loading_package = nil local _loading_package = nil
local _loaded_packages = {} local _loaded_packages = {}
function VMFMod:load_package(package_name, callback) function VMFMod:load_package(package_name, callback)
if not _initialized then if self:has_package_loaded(package_name) then
self:error("Package manager has not been initialized yet. It can only be used after the `all_mods_loaded` event has been processed.")
return
elseif self:has_package_loaded(package_name) then
self:error("Package '%s' has already been loaded", package_name) self:error("Package '%s' has already been loaded", package_name)
return return
end end
local mod_handle = self:get_internal_data("mod_handle")
if not mod_handle then
self:error("Failed to get mod handle. Package management is not available.")
return
end
if not _loaded_packages[self] then if not _loaded_packages[self] then
_loaded_packages[self] = {} _loaded_packages[self] = {}
end end
local package_handle = string.format("resource_packages/%s/%s", self:get_name(), package_name) local package_handle = string.format("resource_packages/%s/%s", self:get_name(), package_name)
local workshop_id = self:get_internal_data("workshop_id")
local resource_package = Mod.resource_package(_mod_handles[workshop_id], package_handle) local resource_package = Mod.resource_package(mod_handle, package_handle)
local is_loading = self:is_package_loading(package_name) local is_loading = self:is_package_loading(package_name)
@ -51,11 +51,6 @@ function VMFMod:load_package(package_name, callback)
end end
function VMFMod:unload_package(package_name) function VMFMod:unload_package(package_name)
if not _initialized then
self:error("Package manager has not been initialized yet. It can only be used after the `all_mods_loaded` event has been processed.")
return
end
if not self:has_package_loaded(package_name) then if not self:has_package_loaded(package_name) then
self:error("Package '%s' has not been loaded", package_name) self:error("Package '%s' has not been loaded", package_name)
return return
@ -69,11 +64,6 @@ function VMFMod:unload_package(package_name)
end end
function VMFMod:is_package_loading(package_name) function VMFMod:is_package_loading(package_name)
if not _initialized then
self:error("Package manager has not been initialized yet. It can only be used after the `all_mods_loaded` event has been processed.")
return
end
if _loading_package and _loading_package.mod == self and _loading_package.package_name == package_name then if _loading_package and _loading_package.mod == self and _loading_package.package_name == package_name then
return true return true
end end
@ -88,25 +78,12 @@ function VMFMod:is_package_loading(package_name)
end end
function VMFMod:has_package_loaded(package_name) function VMFMod:has_package_loaded(package_name)
if not _initialized then
self:error("Package manager has not been initialized yet. It can only be used after the `all_mods_loaded` event has been processed.")
return
end
local loaded_packages = _loaded_packages[self] local loaded_packages = _loaded_packages[self]
return loaded_packages and loaded_packages[package_name] ~= nil return loaded_packages and loaded_packages[package_name] ~= nil
end end
function VMFMod:is_package_manager_initialized() function VMFMod:is_package_manager_initialized()
return _initialized return self:get_data("mod_handle") ~= nil
end
function vmf.initialize_package_manager()
for _, mod_data in ipairs(Managers.mod._mods) do
_mod_handles[mod_data.id] = mod_data.handle
end
_initialized = true
end end
function vmf.update_package_manager() function vmf.update_package_manager()

View file

@ -115,7 +115,14 @@ function vmf.initialize_mod_data(mod, mod_data)
vmf.set_internal_data(mod, "is_togglable", mod_data.is_togglable or mod_data.is_mutator) vmf.set_internal_data(mod, "is_togglable", mod_data.is_togglable or mod_data.is_mutator)
vmf.set_internal_data(mod, "is_mutator", mod_data.is_mutator) vmf.set_internal_data(mod, "is_mutator", mod_data.is_mutator)
vmf.set_internal_data(mod, "allow_rehooking", mod_data.allow_rehooking) vmf.set_internal_data(mod, "allow_rehooking", mod_data.allow_rehooking)
vmf.set_internal_data(mod, "workshop_id", mod_data.workshop_id)
local mod_manager = Managers.mod
local current_mod_load_index = mod_manager._mod_load_index
if current_mod_load_index then
vmf.set_internal_data(mod, "mod_handle", mod_manager._mods[current_mod_load_index].handle)
else
mod:warning("Could not determine current mod load index. Package management won't be available for this mod.")
end
-- Register mod as mutator @TODO: calling this after options initialization would be better, I guess? -- Register mod as mutator @TODO: calling this after options initialization would be better, I guess?
if mod_data.is_mutator then if mod_data.is_mutator then

View file

@ -65,7 +65,6 @@ function vmf_mod_object:update(dt)
if not vmf.all_mods_were_loaded and Managers.mod._state == "done" then if not vmf.all_mods_were_loaded and Managers.mod._state == "done" then
vmf.generate_keybinds() vmf.generate_keybinds()
vmf.initialize_package_manager()
vmf.initialize_vmf_options_view() vmf.initialize_vmf_options_view()
vmf.create_network_dictionary() vmf.create_network_dictionary()
vmf.ping_vmf_users() vmf.ping_vmf_users()