I'm trying to store data in a file sufficient to create a metatable 'class' in such a format that it can be loaded in as a function.
The basic idea is here: How do I load data from another lua file?
I have been able to create the file, and load it in successfully. I know all the data is being read because I got syntax errors until the file was correctly formatted. But I cannot access fields in the data.
Edit: thanks to the poster below who pointed out I needed quotes on string values in the written file. Based on that I have it almost working
However, I still have an issue with setting the metatables on the file function after loading. Each plugin object has several banks, and when I load them in, I'm getting an extra bank sub-table. It is either a duplicate of one of the tables (not always the same) and sometimes an malformed table that doesn't implement the tostring method. Here is the code:
local Bank = {}
Bank.__index = Bank
function Bank.new(name,hue,sat,preset)
local self = setmetatable({}, Bank)
self.name = name
self.hue = hue or 0
self.sat = sat or 0
self.params = {}
self.presets = {preset} or {}
return self
end
function Bank:addPreset(name)
table.insert(self.presets,name)
end
function Bank:setParam(control,name)
self.params[control]=name
end
function Bank:getParam(control)
return self.params.name
end
function getParamStr(params)
paramT= {}
paramStr = 'params = {'
for control,param in pairs(params) do
table.insert(paramT,control..' = '..esc(param))
end
paramStr = paramStr..table.concat(paramT,',')..'},'
return paramStr
end
local Plugin = {}
Plugin.__index = Plugin
function Plugin:__tostring()
bankT = {}
plugStr = 'return {\n'..'name = '..esc(self.name)..', '
bankPre = '\n banks = {\n '
for bankName in pairs(self.banks) do
table.insert(bankT,tostring(self.banks[bankName]))
end
return plugStr..getParamStr(self.params)..bankPre..table.concat(bankT,',\n ')..'\n }\n'..'}'
end
function Bank:__tostring()
rtnStr = ' '..self.name..' = { '..'name = '..esc(self.name)..', hue = '..self.hue..', sat = '..self.sat..', '
..getParamStr(self.params)..'\n'
..' presets = {\"'..table.concat (self.presets,'\", \"')..'\" }\n'
..' }'
return rtnStr
end
function Plugin.new(vstName)
local self = setmetatable({}, Plugin)
self.name = vstName
self.params = {}
self.banks = {}
return self
end
function Plugin:getPresetList()
return table.sort(self.presets)
end
function Plugin:addBank(bankName,hue,sat,preset)
--just quietly overwrite any existing bank? for now...
self.banks[bankName] = Bank.new(bankName,hue,sat,preset)
end
function Plugin:addPresetToBank(bankName,preset)
self.banks[bankName]:addPreset(preset)
end
function Plugin:getBank(bank)
return self.banks[bank]
end
function Plugin:setColor(hue,sat)
self.hue = hue
self.sat = sat
end
function Plugin:getColor(level)
return 'hue: '..self.hue..' and sat: '..self.sat
end
function Plugin:setParam(control,name)
self.params[control]=name
end
function Plugin:getParam(control)
--print(self.params.control)
return self.params[control]
end
function Bank.init(data)
local self = setmetatable(data,Bank)
return self
end
function Plugin.load(filename)
local f = assert(loadfile('c:\\lua\\'..filename..'.lua'))
data = f()
local self = setmetatable(data,Plugin)
for bankName,table in pairs(self.banks) do
self.banks.bankName = Bank.init(table)
end
return self
end
function Plugin:save()
filename = 'c:\\lua\\'..self.name..'.lua'
local file = io.open(filename,'w')
file:write(tostring(self))
file:close()
end
function Plugin.test(name)
plug = Plugin.new(name)
plug:setColor(120,.08)
plug:setParam('A1','Reverb')
plug:setParam('A2','Chorus')
plug:addBank('favorites',120,.05,'Ice Bell')
plug:addBank('pianos',10,.09,'Black')
plug:addPresetToBank('pianos','Grandeur')
plug:addPresetToBank('favorites','OB pad')
curBank = plug:getBank('favorites')
curBank:setParam('A5','Wah')
plug:getBank('pianos'):setParam('A1','Reverb')
return plug
end
plugName = 'test'
plug = Plugin.test(plugName)
plug:save()
plug2 = Plugin.load(plugName)
print(plug2)
plug2.name = 'test2'
plug2:save()
The data file generated looks like this:
return {
name = "test", params = {A1 = "Reverb",A2 = "Chorus"},
banks = {
favorites = { name = "favorites", hue = 120, sat = 0.05, params = {A5 = "Wah"},
presets = {"Ice Bell", "OB pad" }
},
pianos = { name = "pianos", hue = 10, sat = 0.09, params = {A1 = "Reverb"},
presets = {"Black", "Grandeur" }
}
}
}
but after loading I get something like this:
return {
name = "test2", params = {A1 = "Reverb",A2 = "Chorus"},
banks = {
table: 00e8eae0,
favorites = { name = "favorites", hue = 120, sat = 0.05, params = {A5 = "Wah"},
presets = {"Ice Bell", "OB pad" }
},
favorites = { name = "favorites", hue = 120, sat = 0.05, params = {A5 = "Wah"},
presets = {"Ice Bell", "OB pad" }
}
}
}
Not sure where the 'phantom' table is coming from... I also haven't figured out how to avoid duplicating the bank name in the file, but that's not a big deal.
Thanks in advance!!!!
EDIT 1: Making my fields actual strings, and Changing the Plugin init method to this:
Seems to be working correctly.
EDIT 2:
The problem with strange Bank tables was a syntax error in the Plugin.Load method:
If anybody can explain why the former delivered unpredictable results, that would be interesting to know. It's not obvious to me. But it works great now.