This plugin has a table( a list of aliases for sorting equipment and what bank its stroed at) a few aliases to generators new aliases. and for testing some of these functions. Those are all working properly so I didn't include them.
My main problem is getting the tables to save properly.
Using serialize,save() either: Saves the aliases without a send field, or after I modified some addxml.lua functions and including them worked but showed a warning about a clipboard argument not being used for each alias processed. Not a big deal but spammy every time the plugin is loaded/saved.
Everything I have tried to get the table to save using my modified addxml.save() either: returned string or nil when expecting table., or Renamed the table so the plugin fails on reload. or Completely overwrites the table with last alias processed. and I even caused a crash with an accidentally loop. any help at all will be greatly appreciated.
<script>
<![CDATA[
require "serialize"
require "tprint"
function tweak (t)
assert (type (t) == "table", "Table must be supplied to save an alias")
for text, info in pairs (t) do
if text == "name" then
tprint(alias_save(t.name))
--here is where I was trying to convert to save
--needs to save as vic[#vic+1]
end
end
end
function OnPluginInstall ()
vic = {}
assert (loadstring (GetVariable("newsets") or GetVariable("oldsets"))) ()
loadtable()
end -- function OnPluginInstall
function testtable ()
for text, info in ipairs (vic) do
tweak(info)
end -- for
end
function loadtable ()
if vic ~= nil then
if type(vic) == "table" then
for text, info in ipairs (vic) do
alias_create(info)
end
end
else
print("Test Tables Loaded <<<tables are empty>>>")
vic = {}
end
end
function cleartable (tab)
assert(type (tab) == "table", "ERROR: That was not a table you tried to
clear")
if tab ~= nil then
tab = {}
end
end
function OnPluginSaveState ()
for text, info in ipairs (vic) do
tweak (info)
tprint (info)
end
end
function savetable ()
SaveState()
end
function printtable (name, line, wildcards)
for text, info in pairs (vic) do
print(text)
tprint(info)
end -- for
print("<<<<<<<<<<END OF ALIASES>>>>>>>>>>>>>>>>>")
end
function addtotable (name, line, wildcards)
local gear = Trim(wildcards [3] or "")
local target = Trim(wildcards [1] or "")
local location = Trim(wildcards [2] or "")
if room ~= nil then
vic [#vic + 1] = {
match = "wear fset "..target,
send = "get ".. gear .." from "..location,
sequence = 100,
keep_evaluating = true,
enabled = true,
name = gear,
send_to = 10,
}
alias_create {
match = "wear fset "..target,
send = "get ".. gear .." from "..location,
sequence = 100,
keep_evaluating = true,
enabled = true,
name = gear,
send_to = 10,
}
ColourNote ("green", "black", "Added alias to match on 'wear fset "..target.."', sending 'get ".. gear .." from "..location.."'")
end
end -- addtotable
xml_replace = {
["<"] = "<",
[">"] = ">",
["&"] = "&",
['"'] = """,
}
function fixhtml (s)
return (string.gsub (tostring (s), '[<>&"]',
function (str)
return xml_replace [str] or str
end ))
end -- fixhtml
function alias_create (t)
add_alias (t, "alias", "aliases")
if t.name and t.script then
SetAliasOption (t.name, "script", t.script)
end
end
function add_alias (t, datam, data)
assert (type (t) == "table", "Table must be supplied to add a " .. datam)
local k, v
local xml = {}
local send = fixhtml (t.send)
t.send = nil --removing this and using serialize.save() works but spammy
for k, v in pairs (t) do
if v == true then
v = "y"
elseif v == false then
v = "n"
end
table.insert (xml, k .. '="' .. fixhtml (v) .. '"')
end
assert (ImportXML (string.format (
"<%s><%s %s ><send>%s</send></%s></%s>",
data,
datam,
table.concat (xml, "\n"),
send,
datam,
data)
) == 1, "Import of " .. datam .. " failed")
end
function alias_save (name)
local itemtype = 1
local xml = ExportXML (itemtype, name)
assert (xml ~= "", "Can't find that alias : " .. name)
local xmlnodes = assert (utils.xmlread (xml), "Bad XML")
local result = xmlnodes.nodes [1].nodes [1].attributes
if xmlnodes.nodes [1].nodes [1].nodes then
if xmlnodes.nodes [1].nodes [1].nodes [1].name == "send" then
result.send = xmlnodes.nodes [1].nodes [1].nodes [1].content
end
end
return result
end
]]>
</script>
edited to highlight and fix some spacing
[EDIT] Code tags added. |