Skip to content

Commit

Permalink
Wang stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
IntQuant committed Dec 13, 2024
1 parent 2375e1e commit 77455c7
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 14 deletions.
14 changes: 14 additions & 0 deletions quant.ew/files/core/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local base64 = dofile_once("mods/quant.ew/files/resource/base64.lua")
local util = {}

local entity_load_orig = EntityLoad
local mod_text_file_set_content = ModTextFileSetContent

function EntityLoad(path, ...)
if path == "??SAV/world_state.xml" then
Expand Down Expand Up @@ -459,4 +460,17 @@ function util.set_phys_info(entity, data)
return has_set
end

-- Runs code (provided as a string) in a different lua context
function util.run_in_new_context(code)
mod_text_file_set_content("data/ew_code_tmp.lua", code)
local entity = EntityCreateNew()
EntityAddComponent2(entity, "LuaComponent", {
script_source_file = "mods/quant.ew/files/resource/cbs/util_runner.lua",
execute_on_added = true,
remove_after_executed = true,
})
EntityKill(entity)
end


return util
3 changes: 3 additions & 0 deletions quant.ew/files/resource/cbs/util_runner.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Used for util.run_in_new_context(code)
local f = loadfile("data/ew_code_tmp.lua")
f()
2 changes: 0 additions & 2 deletions quant.ew/files/system/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ function module.on_local_player_spawn(my_player)
-- util.load_ephemerial("mods/quant.ew/files/resource/entities/client.xml", 512*3+20, 512*3+10)
-- EntityLoad("mods/quant.ew/files/resource/entities/client.xml", 512*3+20, 512*3+10)

ctx.cap.health.set_max_health(1000)
ctx.cap.health.set_health(1000)
-- util.set_ent_health(player_entity, {1000, 1000})
local wallet = EntityGetFirstComponentIncludingDisabled(player_entity, "WalletComponent")
ComponentSetValue2(wallet, "money", 100000)
Expand Down
98 changes: 86 additions & 12 deletions quant.ew/files/system/wang_hooks/wang_hooks.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
--ModLuaFileAppend("data/scripts/director_helpers.lua", "mods/quant.ew/files/system/wang_hooks/director_helpers.lua")
local rpc = net.new_rpc_namespace()

local module = {}

util.copy_file_content("mods/quant.ew/files/system/wang_hooks/wang_scripts.csv", "data/scripts/wang_scripts.csv")

module.files_with_spawnhooks = {}

for line in string.gmatch(ModTextFileGetContent("mods/quant.ew/files/system/wang_hooks/files_with_spawnhooks.txt"), "(.-)\n") do
Expand All @@ -11,20 +13,36 @@ end

local current_file = nil

local function patch_fn(color, orig_fn_name)
-- print(color, orig_fn_name)
local current_file = current_file
local function detour_name(orig_fn_name)
return "ew_detour_" .. orig_fn_name
end

detour_fn_name = "ew_detour_" .. orig_fn_name
local function generate_detour_fn(orig_fn_name)
local detour_fn_name = detour_name(orig_fn_name)

detour_fn = "function " .. detour_fn_name .. [[(...)
print("Called", "]] .. current_file .. [[", "]] .. detour_fn_name .. [[")
]] .. orig_fn_name .. [[(...)
local detour_fn = "\n".."function " .. detour_fn_name .. [[(x, y, w, h, is_open_path)
if CrossCall("ew_wang_detour", EW_CURRENT_FILE, "]]..orig_fn_name..[[", x, y, w, h, is_open_path) then
return
end
return ]] .. orig_fn_name .. [[(x, y, w, h, is_open_path)
end
]]

return detour_fn
end

local function patch_fn(color, orig_fn_name)
-- print(color, orig_fn_name)
-- Seems like init is special and doesn't work with this detour approach.
if orig_fn_name == "init" then
return nil
end

local detour_fn_name = detour_name(orig_fn_name)
local detour_fn = generate_detour_fn(orig_fn_name)

new_fn_call = "RegisterSpawnFunction( " .. color .. ', "' .. detour_fn_name .. '" )'
repl = new_fn_call .. "\n" .. detour_fn
local new_fn_call = "RegisterSpawnFunction( " .. color .. ', "' .. detour_fn_name .. '" )'
local repl = new_fn_call .. "\n" .. detour_fn
-- print(repl)
return repl
end
Expand All @@ -34,14 +52,70 @@ local function patch_file(filename)
local content = ModTextFileGetContent(filename)
current_file = filename
-- A textbook example of how to NOT use regular expressions.
content = string.gsub(content, 'RegisterSpawnFunction[(][ ]?(.-), "(.-)"[ ]?[)]', patch_fn)
-- content = string.gsub(content, 'RegisterSpawnFunction[(][ ]?(.-), "(.-)"[ ]?[)]', patch_fn)
content = content .. "\n"..'EW_CURRENT_FILE="'..filename..'"\n'

content = content .. generate_detour_fn("spawn_small_enemies")
content = content .. generate_detour_fn("spawn_big_enemies")
-- content = content .. generate_detour_fn("spawn_items")

ModTextFileSetContent(filename, content)
end

function module.on_late_init()
for _, filename in ipairs(module.files_with_spawnhooks) do
patch_file(filename)
if string.sub(filename, 1, 1) ~= "#" then
patch_file(filename)
end
end
end

-- Runs a wang spawn fn if it wasn't called for these coordinates yet.
local function run_spawn_fn(file, fn, x, y, w, h, is_open_path)
-- Check if we have been called already.
-- TODO: it's probably a bad idea to use run flags for that.
-- file shouldn't be significant, as (fn, x, y) seem to be always unique
local flag = "wspwn_"..fn.."_"..x.."_"..y
if GameHasFlagRun(flag) then
util.log("Already spawned")
return
end
GameAddFlagRun(flag)

local is_open_str = "false"
if is_open_path then
is_open_str = "true"
end

util.run_in_new_context(
"function RegisterSpawnFunction(...) end\n"
.. "dofile_once('"..file.."')\n"
-- .. "print("..x..","..y..","..w..","..h..",'"..is_open_str.."')\n"
.. fn.."("..x..","..y..","..w..","..h..","..is_open_str..")\n"
)
end

rpc.opts_reliable()
function rpc.run_spawn_fn(file, fn, x, y, w, h, is_open_path)
if ctx.is_host then
util.log("got request spawn of "..file.." "..fn.. " "..x.." "..y)
run_spawn_fn(file, fn, x, y, w, h, is_open_path)
end
end


util.add_cross_call("ew_wang_detour", function(file, fn, x, y, w, h, is_open_path)
-- print("detour", file, fn, x, y, w, h)

if ctx.is_host then
util.log("[host] requested spawn of "..file.." "..fn.. " "..x.." "..y)
run_spawn_fn(file, fn, x, y, w, h, is_open_path)
else
util.log("requested spawn of "..file.." "..fn.. " "..x.." "..y)
rpc.run_spawn_fn(file, fn, x, y, w, h, is_open_path)
end

return true
end)

return module
30 changes: 30 additions & 0 deletions quant.ew/files/system/wang_hooks/wang_scripts.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ffff0000,ew_detour_spawn_small_enemies,-1,-1,-1,-1
ff800000,ew_detour_spawn_big_enemies,-1,-1,-1,-1
ff00ff00,spawn_items,-1,-1,-1,-1
ffc88d1a,spawn_props,-1,-1,-1,-1
ffc88000,spawn_props2,-1,-1,-1,-1
ffc80040,spawn_props3,-1,-1,-1,-1
ffffff00,spawn_lamp,-1,-1,-1,-1
ffff0aff,load_pixel_scene,-1,-1,-1,-1
ffFF0080,load_pixel_scene2,-1,-1,-1,-1
ffFF8000,spawn_unique_enemy,-1,-1,-1,-1
ffc84040,spawn_unique_enemy2,-1,-1,-1,-1
ff804040,spawn_unique_enemy3,-1,-1,-1,-1
ff96C850,spawn_ghostlamp,-1,-1,-1,-1
ff60A064,spawn_candles,-1,-1,-1,-1
ff50a000,spawn_potion_altar,-1,-1,-1,-1
ffbca0f0,spawn_potions,-1,-1,-1,-1
ff00FF5A,spawn_apparition,-1,-1,-1,-1
ff78FFFF,spawn_heart,-1,-1,-1,-1
ff50A0F0,spawn_wands,-3,-3,-4,-4
ffbf26a6,spawn_portal,-1,-1,-1,-1
ff04A977,spawn_end_portal,-1,-1,-1,-1
ffffd171,spawn_orb,-1,-1,-1,-1
ffffd181,spawn_perk,-1,-1,-1,-1
ffffff81,spawn_all_perks,-1,-1,-1,-1
ffc7eb28,spawn_wand_trap,-1,-1,-1,-1
ffE8FF80,spawn_wand_trap_ignite,-1,-1,-1,-1
ff2768DE,spawn_wand_trap_electricity_source,-1,-1,-1,-1
ff2768DF,spawn_wand_trap_electricity,-1,-1,-1,-1
ff6b4f9b,spawn_moon,-1,-1,-1,-1
ffd7b3e8,spawn_collapse,-1,-1,-1,-1

0 comments on commit 77455c7

Please sign in to comment.