Skip to content

Commit

Permalink
EEex_GetSpellData() should demand spell data
Browse files Browse the repository at this point in the history
- EEex_GetSpellData() would only work if the spell had previously been accessed by the engine.
- Reworked functions that directly called dimmGetResObject().
- EEex_HookAction() now preserves internal registers.
  • Loading branch information
Bubb13 committed Mar 27, 2020
1 parent 64c7e0a commit cbefd13
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
71 changes: 41 additions & 30 deletions EEex/copy/EEex_AHo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ function EEex_SetActionPointY(actionData, newY)
end

function EEex_HookAction(actionData)

local hooksCopy = EEex_HookActionFunctions
EEex_HookActionFunctions = {}
for _, hook in ipairs(hooksCopy) do
hook(actionData)
end

local actorID = EEex_ReadDword(actionData - 0x2C4)
if actorID > 0 and EEex_GetActorStat(actorID, 999) > 0 then
EEex_IterateActorEffects(actorID, function(eData)
Expand All @@ -114,9 +116,11 @@ function EEex_HookAction(actionData)
end
end)
end

for _, hook in pairs(EEex_HookActionGlobalFunctions) do
hook(actionData, actionData - 0x2F8)
end

end

-- Here's another way you can use action hooks. If you give a creature an opcode 401 effect,
Expand Down Expand Up @@ -221,20 +225,20 @@ EEex_AddActionHookGlobal("EXAPPLSP", function(actionData, creatureData)
EEex_SetActionID(actionData, 147)
EEex_WriteDword(actionData + 0x20, EEex_ReadWord(actionData + 0x40, 0x0))
EEex_ApplyEffectToActor(targetID, {
["opcode"] = 146,
["target"] = 2,
["timing"] = 1,
["parameter1"] = casterLevel,
["parameter2"] = 1,
["casterlvl"] = casterLevel,
["resource"] = spellRES,
["source_x"] = EEex_ReadDword(creatureData + 0x8),
["source_y"] = EEex_ReadDword(creatureData + 0xC),
["target_x"] = targetX,
["target_y"] = targetY,
["source_target"] = targetID,
["source_id"] = sourceID
})
["opcode"] = 146,
["target"] = 2,
["timing"] = 1,
["parameter1"] = casterLevel,
["parameter2"] = 1,
["casterlvl"] = casterLevel,
["resource"] = spellRES,
["source_x"] = EEex_ReadDword(creatureData + 0x8),
["source_y"] = EEex_ReadDword(creatureData + 0xC),
["target_x"] = targetX,
["target_y"] = targetY,
["source_target"] = targetID,
["source_id"] = sourceID
})
end
elseif actionID == 95 then
local spellRES = EEex_GetActorSpellRES(sourceID)
Expand All @@ -247,39 +251,44 @@ EEex_AddActionHookGlobal("EXAPPLSP", function(actionData, creatureData)
EEex_SetActionID(actionData, 147)
EEex_WriteDword(actionData + 0x20, EEex_ReadWord(actionData + 0x40, 0x0))
EEex_ApplyEffectToActor(sourceID, {
["opcode"] = 148,
["target"] = 2,
["timing"] = 1,
["parameter1"] = casterLevel,
["parameter2"] = 1,
["casterlvl"] = casterLevel,
["resource"] = spellRES,
["source_x"] = EEex_ReadDword(creatureData + 0x8),
["source_y"] = EEex_ReadDword(creatureData + 0xC),
["target_x"] = targetX,
["target_y"] = targetY,
["source_target"] = sourceID,
["source_id"] = sourceID
})
["opcode"] = 148,
["target"] = 2,
["timing"] = 1,
["parameter1"] = casterLevel,
["parameter2"] = 1,
["casterlvl"] = casterLevel,
["resource"] = spellRES,
["source_x"] = EEex_ReadDword(creatureData + 0x8),
["source_y"] = EEex_ReadDword(creatureData + 0xC),
["target_x"] = targetX,
["target_y"] = targetY,
["source_target"] = sourceID,
["source_id"] = sourceID
})
end
end
end)

function EEex_InstallActionHook()

local hookName = "EEex_HookAction"
local hookNameAddress = EEex_Malloc(#hookName + 1)
EEex_WriteString(hookNameAddress, hookName)

local hookAddress = EEex_WriteAssemblyAuto({[[
!push_[esp]
!mov_[esp+byte]_ecx 04
!push_all_registers
!mov_edi_ecx
!push_esi
!call >CAIAction::Decode
!push_dword ]], {hookNameAddress, 4}, [[
!push_[dword] *_g_lua
!call >_lua_getglobal
!add_esp_byte 08
!push_edi
!fild_[esp]
!sub_esp_byte 04
!fstp_qword:[esp]
Expand All @@ -296,6 +305,8 @@ function EEex_InstallActionHook()
!call >_lua_pcallk
!add_esp_byte 18
!pop_all_registers
!add_esp_byte 04
!jmp_dword >CGameSprite::SetCurrAction()_after_decode
]]})

Expand Down
24 changes: 10 additions & 14 deletions EEex/copy/M__EEex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2466,35 +2466,31 @@ end
-- because offset 0x34 in the SPL file is the spell's level.
-- Warning: this will crash if the spell is not in the game.
function EEex_GetSpellData(resref)
local resrefLocation = EEex_Malloc(0x8)
EEex_WriteLString(resrefLocation + 0x0, resref, 8)
local eax = EEex_Call(EEex_Label("dimmGetResObject"), {0x0, 0x3EE, resrefLocation}, nil, 0xC)
EEex_Free(resrefLocation)
if eax ~= 0x0 then
return EEex_ReadDword(eax + 0x40)
local CRes = EEex_DemandCRes(resref, "SPL")
if CRes ~= 0x0 then
return EEex_ReadDword(CRes + 0x40)
else
return 0
return 0x0
end
end

function EEex_IsSpellValid(resrefLocation)
local eax = EEex_Call(EEex_Label("dimmGetResObject"), {0x0, 0x3EE, resrefLocation}, nil, 0xC)
local eax = EEex_GetSpellData(EEex_ReadLString(resrefLocation, 8))
return eax ~= 0x0
end

function EEex_GetSpellDescription(resrefLocation)
local eax = EEex_Call(EEex_Label("dimmGetResObject"), {0x0, 0x3EE, resrefLocation}, nil, 0xC)
return Infinity_FetchString(EEex_ReadDword(EEex_ReadDword(eax + 0x40) + 0x50))
local eax = EEex_GetSpellData(EEex_ReadLString(resrefLocation, 8))
return Infinity_FetchString(EEex_ReadDword(eax + 0x50))
end

function EEex_GetSpellIcon(resrefLocation)
local eax = EEex_Call(EEex_Label("dimmGetResObject"), {0x0, 0x3EE, resrefLocation}, nil, 0xC)
return EEex_ReadString(EEex_ReadDword(eax + 0x40) + 0x3A)
local eax = EEex_GetSpellData(EEex_ReadLString(resrefLocation, 8))
return EEex_ReadString(eax + 0x3A)
end

function EEex_GetSpellName(resrefLocation)
local eax = EEex_Call(EEex_Label("dimmGetResObject"), {0x0, 0x3EE, resrefLocation}, nil, 0xC)
local step1 = EEex_ReadDword(eax + 0x40)
local step1 = EEex_GetSpellData(EEex_ReadLString(resrefLocation, 8))
if step1 ~= 0x0 then
return Infinity_FetchString(EEex_ReadDword(step1 + 0x8))
else
Expand Down

0 comments on commit cbefd13

Please sign in to comment.