Safe Calls: moved to a separate file

This commit is contained in:
bi 2018-06-11 19:52:40 +03:00
parent 4824e6a5c8
commit 1b5111d067
3 changed files with 64 additions and 65 deletions

View file

@ -0,0 +1,63 @@
local vmf = get_mod("VMF")
-- #####################################################################################################################
-- ##### Local functions ###############################################################################################
-- #####################################################################################################################
local function pack_pcall(status, ...)
return status, {n = select('#', ...), ...}
end
local function print_error_callstack(error_message)
if type(error_message) == "table" and error_message.error then
error_message = error_message.error
end
print("Error: " .. tostring(error_message) .. "\n" .. Script.callstack())
return error_message
end
-- #####################################################################################################################
-- ##### VMFMod ########################################################################################################
-- #####################################################################################################################
function VMFMod:pcall(...)
return vmf.xpcall(self, "(pcall)", ...)
end
function VMFMod:dofile(file_path)
local _, return_values = pack_pcall(vmf.xpcall_dofile(self, "(dofile)", file_path))
return unpack(return_values, 1, return_values.n)
end
-- #####################################################################################################################
-- ##### VMF internal functions and variables ##########################################################################
-- #####################################################################################################################
function vmf.xpcall(mod, error_prefix, func, ...)
local success, return_values = pack_pcall(xpcall(func, print_error_callstack, ...))
if not success then
mod:error("%s: %s", error_prefix, return_values[1])
return success
end
return success, unpack(return_values, 1, return_values.n)
end
function vmf.xpcall_no_return_values(mod, error_prefix, func, ...)
local success, error_message = xpcall(func, print_error_callstack, ...)
if not success then
mod:error("%s: %s", error_prefix, error_message)
end
return success
end
function vmf.xpcall_dofile(mod, error_prefix, file_path)
if type(file_path) ~= "string" then
mod:error("%s: file path should be a string.", error_prefix)
return false
end
return vmf.xpcall(mod, error_prefix, dofile, file_path)
end

View file

@ -7,24 +7,6 @@ local _mods_unloading_order = {}
-- ##### Local functions ############################################################################################### -- ##### Local functions ###############################################################################################
-- ##################################################################################################################### -- #####################################################################################################################
-- WORKING WITH XPCALL
local function pack_pcall(status, ...)
return status, {n = select('#', ...), ...}
end
local function print_error_callstack(error_message)
if type(error_message) == "table" and error_message.error then
error_message = error_message.error
end
print("Error: " .. tostring(error_message) .. "\n" .. Script.callstack())
return error_message
end
-- CREATING MOD
local function create_mod(mod_name) local function create_mod(mod_name)
if _mods[mod_name] then if _mods[mod_name] then
@ -150,18 +132,6 @@ function VMFMod:get_config()
return self._data.config return self._data.config
end end
-- CORE FUNCTIONS
function VMFMod:pcall(...)
return vmf.xpcall(self, "(pcall)", ...)
end
function VMFMod:dofile(file_path)
local _, return_values = pack_pcall(vmf.xpcall_dofile(self, "(dofile)", file_path))
return unpack(return_values, 1, return_values.n)
end
-- ##################################################################################################################### -- #####################################################################################################################
-- ##### VMF Initialization ############################################################################################ -- ##### VMF Initialization ############################################################################################
-- ##################################################################################################################### -- #####################################################################################################################
@ -172,41 +142,6 @@ vmf = create_mod("VMF")
-- ##### VMF internal functions and variables ########################################################################## -- ##### VMF internal functions and variables ##########################################################################
-- ##################################################################################################################### -- #####################################################################################################################
-- XPCALL
function vmf.xpcall(mod, error_prefix, func, ...)
local success, return_values = pack_pcall(xpcall(func, print_error_callstack, ...))
if not success then
mod:error("%s: %s", error_prefix, return_values[1])
return success
end
return success, unpack(return_values, 1, return_values.n)
end
function vmf.xpcall_no_return_values(mod, error_prefix, func, ...)
local success, error_message = xpcall(func, print_error_callstack, ...)
if not success then
mod:error("%s: %s", error_prefix, error_message)
end
return success
end
function vmf.xpcall_dofile(mod, error_prefix, file_path)
if type(file_path) ~= "string" then
mod:error("%s: file path should be a string.", error_prefix)
return false
end
return vmf.xpcall(mod, error_prefix, dofile, file_path)
end
-- MOD DATA INITIALIZATION -- MOD DATA INITIALIZATION
function vmf.initialize_mod_data(mod, mod_data) function vmf.initialize_mod_data(mod, mod_data)

View file

@ -12,6 +12,7 @@ local vmf_mod_object = {}
function vmf_mod_object:init() function vmf_mod_object:init()
dofile("scripts/mods/vmf/modules/vmf_mod_manager") dofile("scripts/mods/vmf/modules/vmf_mod_manager")
dofile("scripts/mods/vmf/modules/core/safe_calls")
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")
dofile("scripts/mods/vmf/modules/core/logging") dofile("scripts/mods/vmf/modules/core/logging")