<?xml version="1.0" encoding="utf-8"?>
<CheatTable CheatEngineTableVersion="26">
  <Files>
    <TF1.txt Encoding="Ascii85">lVxCF?DFnh3J;xRaaAZuOv+]BNXrBgA#R3xB@4!#00</TF1.txt>
    <test.lua Encoding="Ascii85">)]R5^C]v6UEy]1/R;)]tO.xZ{P_8ZrPt8p%QPNS!$xlu!$.^6KiIyZL/*kVp*B+q</test.lua>
  </Files>
  <CheatEntries/>
  <UserdefinedSymbols/>
  <LuaScript>--[[
  Load a text file as a string.  If useTableFile is true or if the file isn't
  found on disk, it will try to read the name as a TableFile.  The directory
  should be where the cheat file is, it is the initial directory for the
  dialog when you are saving your cheat table.
--]]
function loadTextFile(name, useTableFile)
  if useTableFile then
    local tableFile = findTableFile(name)
    if not tableFile then return nil, 'Unable to open table file "'..tostring(name)..'"' end
    local ss = createStringStream()
    ss.Position = 0 -- recommended on wiki: https://wiki.cheatengine.org/index.php?title=Lua:Class:TableFile
    ss.CopyFrom(tableFile.Stream, 0)
    local text = ss.DataString
    ss.destroy()
    return text
  else
    local path = getMainForm().saveDialog1.InitialDir..name
    local f, err = io.open(path, "r")
    -- fall back to table file if disk file error (doesn't exist)
    if f == nil then return loadTextFile(name, true) end
    local text = f:read("*all")
    f:close()
    return text
  end
end

--[[
  Save a string to a text file.  If useTableFile is true it will be saved as
  a TableFile.  The directory should be where the cheat file is, it is the
  initial directory for the dialog when you are saving your cheat table.
--]]
function saveTextFile(name, text, useTableFile)
  if useTableFile then
    local tf = findTableFile(name)
    if tf ~= nil then
      tf.delete()
      tf = nil
    end
    tf = createTableFile(name)
    local ss = createStringStream(text)
    tf.Stream.CopyFrom(ss, 0)
    ss.destroy()
    return true
  else
    local path = getMainForm().saveDialog1.InitialDir..name
    local f, err = io.open(path, "w")
    if f == nil then return nil, err end
    f:write(text)
    f:close()
    return true
  end
end


--[[

-- SIMPLE test scripts to run in lua engine

-- step 1: save simple table file
return saveTextFile('TF1.txt', 'Writing table file TF1.txt!', true)

-- step 2: load to make sure we have the data
return loadTextFile('TF1.txt', true)

-- step 3: overwrite simple table file
return saveTextFile('TF1.txt', 'OVERWROTE table file TF1.txt!', true)

-- step 4: check contents again
return loadTextFile('TF1.txt', true)

-- step 5: save simple disk file
return saveTextFile('DF1.txt', 'Writing disk file DF1.txt!')

-- step 6: load to make sure we have the data
return loadTextFile('DF1.txt')

-- step 7: overwrite simple disk file
return saveTextFile('DF1.txt', 'OVERWROTE disk file DF1.txt!')

-- step 8: check contents again
return loadTextFile('DF1.txt')

-- step 9: save lua file and use dofile()
saveTextFile('test.lua', 'return { name = "jgoemat", location = "disk" }')
local obj = dofile('test.lua')
print('Name is:', obj.name, 'Location is:', obj.location)
return obj

-- step 10: save lua as table file and use loadstring()
saveTextFile('test.lua', 'return { name = "jgoemat", location = "table" }', true)
local obj = loadstring(loadTextFile('test.lua', true))()
print('Name is:', obj.name, 'Location is:', obj.location)
return obj

-- step 11: defaults to disk file if exists in both places
return loadTextFile('test.lua')

-- step 12: falls back to table file if disk file doesn't exist
return loadTextFile('TF1.txt')


]]

</LuaScript>
</CheatTable>
