<?xml version="1.0" encoding="utf-8"?>
<CheatTable UsesMono="1" CheatEngineTableVersion="52">
  <CheatEntries>
    <CheatEntry>
      <ID>0</ID>
      <Description>"Run Console Command"</Description>
      <VariableType>Auto Assembler Script</VariableType>
      <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then
  return
end

local command = inputQuery("The Long Dark - Dev Console", "Enter command:", "")

local rec = memrec
local uncheckTimer = createTimer()
uncheckTimer.Interval = 1
uncheckTimer.OnTimer = function(timer)
  timer.destroy()
  rec.Active = false
end

if command == nil or command == "" then
  -- Bail out if the user cancelled or submitted an empty string
  return
end

LaunchMonoDataCollector()
mono_initialize()

local consoleClass = mono_findClass("", "uConsole")
local runCommandMethod = mono_class_findMethod(consoleClass, "RunCommand")

mono_invoke_method(nil, runCommandMethod, nil, {{type=vtString, value=command}})

[DISABLE]
</AssemblerScript>
    </CheatEntry>
    <CheatEntry>
      <ID>2</ID>
      <Description>"Bind Hotkeys"</Description>
      <VariableType>Auto Assembler Script</VariableType>
      <AssemblerScript>{$lua}

[ENABLE]

if syntaxcheck then
  return
end

LaunchMonoDataCollector()
mono_initialize()

function runConsoleCommand(command)
  local consoleClass = mono_findClass("", "uConsole")
  local runCommandMethod = mono_class_findMethod(consoleClass, "RunCommand")

  mono_invoke_method(nil, runCommandMethod, nil, {{type=vtString, value=command}})
end

function quickSave()
  local gameManagerClass = mono_findClass("", "GameManager")
  local saveMethod = mono_class_findMethod(gameManagerClass, "SaveGameAndDisplayHUDMessage")

  mono_invoke_method(nil, saveMethod, nil, {})
end

function quickLoad()
  local sgsClass = mono_findClass("", "SaveGameSystem")
  local gmClass = mono_findClass("", "GameManager")
  local getSlotMethod = mono_class_findMethod(sgsClass, "GetNewestSaveSlotForActiveGame")
  local loadSlotMethod = mono_class_findMethod(gmClass, "LoadSaveGameSlot")
  local slot = mono_invoke_method(nil, getSlotMethod, nil, {})

  mono_invoke_method(nil, loadSlotMethod, nil, {slot})
end

local bindings = {
  [VK_F1] = function() runConsoleCommand("god") end,
  [VK_F2] = function() runConsoleCommand("lamp_infinite") end,
  [VK_F3] = function() runConsoleCommand("ghost") end,
  [VK_F4] = function() runConsoleCommand("fly") end,
  [VK_F5] = function() quickSave() end,
  [VK_F6] = function() quickLoad() end,
  [VK_F7] = function() runConsoleCommand("force_gear_weight 0") end,
}

hotkeys = hotkeys or {}

for key, callback in pairs(bindings) do
  table.insert(hotkeys, createHotkey(callback, key))
end

[DISABLE]

if hotkeys then
  for index, hotkey in ipairs(hotkeys) do
    hotkey.destroy()
  end

  hotkeys = nil
end

</AssemblerScript>
    </CheatEntry>
    <CheatEntry>
      <ID>14</ID>
      <Description>"Safehouse Anywhere"</Description>
      <VariableType>Auto Assembler Script</VariableType>
      <AssemblerScript>[ENABLE]

assert(SafehouseManager.InCustomizableSafehouse, 48 89 5C 24 08) // mov [rsp+08],rbx

alloc(newmem_safehouse, 256, SafehouseManager.InCustomizableSafehouse)

newmem_safehouse:
  mov al,1
  ret

SafehouseManager.InCustomizableSafehouse:
  jmp newmem_safehouse

[DISABLE]

SafehouseManager.InCustomizableSafehouse:
  db 48 89 5C 24 08 // mov [rsp+08],rbx

dealloc(newmem_safehouse)

</AssemblerScript>
    </CheatEntry>
    <CheatEntry>
      <ID>22</ID>
      <Description>"Place Anywhere"</Description>
      <VariableType>Auto Assembler Script</VariableType>
      <AssemblerScript>[ENABLE]

assert(PlayerManager.DoPositionCheck, 48 8B C4 48 89 48 08) // mov rax,rsp ; mov [rax+08],rcx

alloc(newmem_placement, 256, PlayerManager.DoPositionCheck)
label(trampoline_placement)

newmem_placement:
  sub rsp,28
  call trampoline_placement
  add rsp,28
  xor eax,eax
  ret

trampoline_placement:
  mov rax,rsp
  mov [rax+08],rcx
  jmp PlayerManager.DoPositionCheck+7

PlayerManager.DoPositionCheck:
  jmp newmem_placement
  nop
  nop

[DISABLE]

PlayerManager.DoPositionCheck:
  db 48 8B C4 48 89 48 08 // mov rax,rsp ; mov [rax+08],rcx

dealloc(newmem_placement)

</AssemblerScript>
    </CheatEntry>
    <CheatEntry>
      <ID>23</ID>
      <Description>"Fishing Anywhere"</Description>
      <VariableType>Auto Assembler Script</VariableType>
      <AssemblerScript>[ENABLE]

aobscanregion(gaps_found,TLD.Gameplay.FishingHolePlacementArea.GetActivePlacementSettings,TLD.Gameplay.FishingHolePlacementArea.GetActivePlacementSettings+E9,48 8B 40 20 48 83 C4 28 C3)   // mov rax,[rax+20] ; add rsp,28 ; ret
aobscanregion(gaps_empty,TLD.Gameplay.FishingHolePlacementArea.GetActivePlacementSettings,TLD.Gameplay.FishingHolePlacementArea.GetActivePlacementSettings+E9,33 C0 48 83 C4 28 C3)         // xor eax,eax ; add rsp,28 ; ret
registersymbol(gaps_found)
registersymbol(gaps_empty)

alloc(newmem_gaps,512,gaps_found)
label(newmem_gaps_empty)
label(found_done)
label(empty_done)
label(no_old_handle)
label(cached_settings)
label(gc_handle)
label(hits_real)
label(hits_substituted)
registersymbol(cached_settings)
registersymbol(gc_handle)
registersymbol(hits_real)
registersymbol(hits_substituted)

newmem_gaps:                        // an area is active
  mov rax,[rax+20]                  // its settings
  inc dword ptr [hits_real]
  test rax,rax
  je found_done
  cmp rax,[cached_settings]
  je found_done                     // already holding this object
  mov [cached_settings],rax
  mov ecx,[gc_handle]               // let go of the previous object, if any
  test ecx,ecx
  je no_old_handle
  call GameAssembly.il2cpp_gchandle_free
no_old_handle:
  mov rcx,[cached_settings]         // hold the new object so the GC can't free it
  xor edx,edx
  call GameAssembly.il2cpp_gchandle_new
  mov [gc_handle],eax
  mov rax,[cached_settings]         // return value for the caller
found_done:
  add rsp,28
  ret

newmem_gaps_empty:                  // no active area: hand back the held settings
  mov rax,[cached_settings]
  test rax,rax
  je empty_done
  inc dword ptr [hits_substituted]
empty_done:
  add rsp,28
  ret

cached_settings:
  dq 0
gc_handle:
  dd 0
hits_real:
  dd 0
hits_substituted:
  dd 0

gaps_found:
  jmp newmem_gaps
  nop 3
gaps_empty:
  jmp newmem_gaps_empty
  nop

{$lua}
if syntaxcheck then return end
if tldGapsTimer then tldGapsTimer.destroy() end
local lastCached, lastSub = 0, 0
tldGapsTimer = createTimer(getMainForm())
tldGapsTimer.Interval = 500
tldGapsTimer.OnTimer = function()
  local cached = readQword("cached_settings") or 0
  local sub = readInteger("hits_substituted") or 0
  if cached ~= lastCached then
    print(string.format("[fishing] holding settings object at %X (gc handle %d)", cached, readInteger("gc_handle") or 0))
    lastCached = cached
  end
  if sub ~= lastSub then
    print(string.format("[fishing] substituted held settings (%d times so far)", sub))
    lastSub = sub
  end
end
{$asm}

[DISABLE]

{$lua}
if syntaxcheck then return end
if tldGapsTimer then tldGapsTimer.destroy() tldGapsTimer = nil end
{$asm}

gaps_found:
  db 48 8B 40 20 48 83 C4 28    // mov rax,[rax+20] ; add rsp,28
gaps_empty:
  db 33 C0 48 83 C4 28          // xor eax,eax ; add rsp,28

unregistersymbol(gaps_found)
unregistersymbol(gaps_empty)
unregistersymbol(cached_settings)
unregistersymbol(gc_handle)
unregistersymbol(hits_real)
unregistersymbol(hits_substituted)
dealloc(newmem_gaps)

</AssemblerScript>
    </CheatEntry>
  </CheatEntries>
  <CheatCodes>
    <CodeEntry>
      <Description>Code :mov rax,rsp</Description>
      <AddressString>GameAssembly.dll+CEA730</AddressString>
      <Before>
        <Byte>CC</Byte>
        <Byte>CC</Byte>
        <Byte>CC</Byte>
        <Byte>CC</Byte>
        <Byte>CC</Byte>
      </Before>
      <Actual>
        <Byte>48</Byte>
        <Byte>8B</Byte>
        <Byte>C4</Byte>
      </Actual>
      <After>
        <Byte>48</Byte>
        <Byte>89</Byte>
        <Byte>48</Byte>
        <Byte>08</Byte>
        <Byte>53</Byte>
      </After>
    </CodeEntry>
    <CodeEntry>
      <Description>Code :mov [rsp+08],rbx</Description>
      <AddressString>GameAssembly.dll+A72470</AddressString>
      <Before>
        <Byte>CC</Byte>
        <Byte>CC</Byte>
        <Byte>CC</Byte>
        <Byte>CC</Byte>
        <Byte>CC</Byte>
      </Before>
      <Actual>
        <Byte>48</Byte>
        <Byte>89</Byte>
        <Byte>5C</Byte>
        <Byte>24</Byte>
        <Byte>08</Byte>
      </Actual>
      <After>
        <Byte>48</Byte>
        <Byte>89</Byte>
        <Byte>74</Byte>
        <Byte>24</Byte>
        <Byte>10</Byte>
      </After>
    </CodeEntry>
  </CheatCodes>
  <UserdefinedSymbols/>
</CheatTable>
