----------------------------------------------------------------------------------------------------
-- Preserve TableFile Names for Older Cheat Engine Versions
----------------------------------------------------------------------------------------------------
local function readFile(path)
local file = assert(io.open(path, "rb"))
local data = file:read("*a")
file:close()
return assert(data)
end
local function writeFile(path, data)
local file = assert(io.open(path, "wb"))
local result = file:write(data)
file:close()
return assert(result)
end
local function getBackupPath(path)
local base = assert(path:match("^(.*)%.[^%.\\/:]+$"))
return base..".xml"
end
local function decodeXmlAttribute(str)
local s1 = str:gsub(""", '"')
local s2 = s1:gsub("'", "'")
local s3 = s2:gsub("<", "<")
local s4 = s3:gsub(">", ">")
local s5 = s4:gsub("&", "&")
return s5
end
local function isLegacyTableFileName(name)
return not (name:match("^[%a_][%w_%.%-]*$") == nil)
end
local function convertTableFile(attributes, data)
local encodedName = assert(attributes:match('Name="(.-)"'))
local name = decodeXmlAttribute(encodedName)
assert(isLegacyTableFileName(name))
local legacyAttributes = attributes:gsub('%s+Name=".-"', "", 1)
return "<"..name..legacyAttributes..">"
..data
..""..name..">"
end
local function convertTableFiles(files)
return files:gsub("([%s%S]-)", convertTableFile)
end
local function convertLegacyTableXml(xml)
local func = function(openTag, files, closeTag)
return openTag..convertTableFiles(files)..closeTag
end
return xml:gsub("()([%s%S]-)()", func)
end
local function saveLegacyTable(path)
local xml = readFile(path)
writeFile(getBackupPath(path), xml)
writeFile(path, convertLegacyTableXml(xml))
end
local function hookLegacyTableSave(ver)
if (ver < 7.51) then return end
local mf = getMainForm()
local oriSave = mf.miSave.OnClick
local oriSaveAs = mf.SaveButton.Action.OnExecute
local saveAs = function(sender) oriSaveAs(sender); saveLegacyTable(mf.SaveDialog1.FileName) end
local save = function(sender) oriSave(sender); saveLegacyTable(mf.SaveDialog1.FileName) end
mf.SaveButton.Action, mf.Save1.Action = nil, nil
mf.SaveButton.OnClick, mf.Save1.OnClick = saveAs, saveAs
mf.miSave.OnClick = save
end
----------------------------------------------------------------------------------------------------
-- Execute
----------------------------------------------------------------------------------------------------
return hookLegacyTableSave(getCEVersion())
----------------------------------------------------------------------------------------------------