Divergent/mods/Western Goods/gamedata/scripts/western_goods_monkey_patche...

227 lines
9.8 KiB
Plaintext
Raw Normal View History

2024-03-17 20:18:03 -04:00
---==================================================================================================================---
--- ---
--- Original Author(s) : NLTP_ASHES ---
--- Edited : N/A ---
--- Date : 20/02/2024 ---
--- License : Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) ---
--- ---
--- Script used to monkey-patch some functions of base anomaly, or other addons. ---
--- ---
---==================================================================================================================---
-- ---------------------------------------------------------------------------------------------------------------------
-- Constants, global variables and imported functions
-- ---------------------------------------------------------------------------------------------------------------------
-- New callbacks
AddScriptCallback("western_goods_on_campfire_use")
AddScriptCallback("western_goods_on_item_before_use")
AddScriptCallback("western_goods_on_hideout_furniture_light_use")
-- Imported functions
local dbg_printf = western_goods_utils.dbg_printf
-- Configuration
monkey_patches = {
hideout_furniture_light = {
name = "hideout_furniture_light",
patches = {
{ prio=1, script="ui_furniture_light", func="toggle_light", cond = function() return western_goods_core.is_hideout_furniture_installed() end }
}
},
item_before_use = {
name = "item_before_use",
patches = {
{ prio=1, script="itms_manager", func="actor_on_item_before_use", cond = function() return true end },
{ prio=2, script="enhanced_animations", func="originalAOIBU", cond = function() return western_goods_core.is_fdda_installed() end }
}
},
campfire_on_interact = {
name = "campfire_on_interact",
patches = {
{ prio=1, script="western_goods_core", func="use_campfire", cond = function() return true end }
}
},
trader_autoinject_update = {
name = "trader_autoinject_update",
patches = {
{ prio=1, script="trader_autoinject", func="update", cond = function() return true end }
}
},
death_manager_spawn_cosmetics = {
name = "death_manager_spawn_cosmetics",
patches = {
{ prio=1, script="death_manager", func="spawn_cosmetics", cond = function() return true end }
}
},
trade_manager_update = {
name = "trade_manager_update",
patches = {
{ prio=1, script="trade_manager", func="update", cond = function() return true end }
}
},
barter_ui_gui_on_show = {
name = "barter_ui_gui_on_show",
patches = {
{ prio=1, script="barter_ui", func="GUI_on_show", cond = function() return western_goods_core.is_barter_ui_installed() end }
}
},
ui_inventory_update_character = {
name = "ui_inventory_update_character",
patches = {
{ prio=1, script="ui_inventory", class="UIInventory", func="UpdateCharacter", cond = function() return true end }
}
},
ui_inventory_t_mode_update_price = {
name = "ui_inventory_t_mode_update_price",
patches = {
{ prio=1, script="ui_inventory", class="UIInventory", func="TMode_UpdatePrice", cond = function() return true end }
}
},
ui_inventory_t_mode_sell = {
name = "ui_inventory_t_mode_sell",
patches = {
{ prio=1, script="ui_inventory", class="UIInventory", func="TMode_Sell", cond = function() return true end }
}
},
ui_inventory_t_mode_buy = {
name = "ui_inventory_t_mode_buy",
patches = {
{ prio=1, script="ui_inventory", class="UIInventory", func="TMode_Buy", cond = function() return true end }
}
},
surge_manager_get_task_target = {
name = "surge_manager_get_task_target",
patches = {
{ prio=1, script="surge_manager", func="get_task_target", cond = function() return true end }
}
},
arszi_psy_get_psy_health_regeneration = {
name = "arszi_psy_get_psy_health_regeneration",
patches = {
{ prio=1, script="arszi_psy", func="get_psy_health_regeneration", cond = function() return true end }
}
}
}
-- ---------------------------------------------------------------------------------------------------------------------
-- Functions
-- ---------------------------------------------------------------------------------------------------------------------
function find_best_monkey_patch(monkey_patch_group)
-- Filter out invalid monkey-patches (keep only ones that can be executed)
local valid_patches = {}
for _,patch in pairs(monkey_patch_group.patches) do
if patch.cond() then
table.insert(valid_patches, patch)
end
end
-- Find highest priority-monkey patch
local monkey_patch = nil
for idx,patch in pairs(valid_patches) do
monkey_patch = monkey_patch or patch
if monkey_patch.prio <= patch.prio then
monkey_patch = patch
end
end
if not monkey_patch then
dbg_printf("[WG] Fire Source Binder | No applicable monkey-patch for %s", monkey_patch_group.name)
return
end
return monkey_patch
end
function make_monkey_patch(monkey_patch_group, func)
local monkey_patch = this.find_best_monkey_patch(monkey_patch_group)
if not monkey_patch then
return
end
-- Variabilized to fix a highlight bug in my IDE
local script_name = monkey_patch.script
local class_name = monkey_patch.class
local function_name = monkey_patch.func
if class_name then
dbg_printf("[WG] Fire Source Binder | Monkey-patching %s function from class %s in %s.script", function_name, class_name, script_name)
this[monkey_patch_group.name] = _G[script_name][class_name][function_name]
_G[script_name][class_name][function_name] = func
else
dbg_printf("[WG] Fire Source Binder | Monkey-patching %s function in %s.script", function_name, script_name)
this[monkey_patch_group.name] = _G[script_name][function_name]
_G[script_name][function_name] = func
end
end
-- ---------------------------------------------------------------------------------------------------------------------
-- Monkey-patches
-- ---------------------------------------------------------------------------------------------------------------------
make_monkey_patch(monkey_patches.hideout_furniture_light, function(obj_id)
local is_on_before = hf_obj_manager.get_data(obj_id).is_on
this.hideout_furniture_light(obj_id)
local is_on_after = hf_obj_manager.get_data(obj_id).is_on
if not is_on_before and not is_on_after then
SendScriptCallback("western_goods_on_hideout_furniture_light_use", obj_id)
end
end)
make_monkey_patch(monkey_patches.item_before_use, function(obj, flags)
this.item_before_use(obj, flags)
SendScriptCallback("western_goods_on_item_before_use", obj, flags)
end)
make_monkey_patch(monkey_patches.campfire_on_interact, function()
this.campfire_on_interact()
SendScriptCallback("western_goods_on_campfire_use")
end)
make_monkey_patch(monkey_patches.trader_autoinject_update, function(npc)
this.trader_autoinject_update(npc)
western_goods_loot.spawn_trader_loot(npc)
end)
make_monkey_patch(monkey_patches.death_manager_spawn_cosmetics, function(npc, npc_id, npc_comm, npc_rank, visual, rand_condition)
this.death_manager_spawn_cosmetics(npc, npc_id, npc_comm, npc_rank, visual, rand_condition)
western_goods_loot.spawn_corpse_loot(npc, npc_rank, npc_comm)
end)
make_monkey_patch(monkey_patches.trade_manager_update, function(npc, force_refresh)
western_goods_trade_eur_usd.trade_manager_update(npc, force_refresh)
end)
make_monkey_patch(monkey_patches.ui_inventory_update_character, function(self)
this.ui_inventory_update_character(self)
western_goods_trade_eur_usd.ui_inventory_UIInventory_UpdateCharacter(self)
end)
make_monkey_patch(monkey_patches.ui_inventory_t_mode_update_price, function(self, ele_txt, ele_btn, bag)
this.ui_inventory_t_mode_update_price(self, ele_txt, ele_btn, bag)
western_goods_trade_eur_usd.ui_inventory_UIInventory_TMode_UpdatePrice(self, ele_txt, ele_btn, bag)
end)
make_monkey_patch(monkey_patches.ui_inventory_t_mode_sell, function(self)
western_goods_trade_eur_usd.ui_inventory_UIInventory_TMode_Sell(self)
end)
make_monkey_patch(monkey_patches.ui_inventory_t_mode_buy, function(self)
western_goods_trade_eur_usd.ui_inventory_UIInventory_TMode_Buy(self)
end)
make_monkey_patch(monkey_patches.barter_ui_gui_on_show, function(name, path)
western_goods_trade_eur_usd.barter_ui_gui_on_show(name, path)
end)
make_monkey_patch(monkey_patches.surge_manager_get_task_target, function()
return western_goods_ui_gps_locator.surge_manager_get_task_target()
end)
make_monkey_patch(monkey_patches.arszi_psy_get_psy_health_regeneration, function()
local base_psy_heal = this.arszi_psy_get_psy_health_regeneration() or 0
local bonus_psy_heal_magazine = western_goods_ui_readable.get_psy_health_regeneration_magazine() or 0
return base_psy_heal + bonus_psy_heal_magazine
end)