View Single Post
Old 11-16-2019, 02:25 PM   #613
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,430
Default

Quote:
Originally Posted by reapero View Post
i wonder if i am just making things more complicated than they should.
Putting a bunch of values into one string and splitting them back out again is actually the most common way to do this.

If your presets are flat - that is, they don't have any nested tables - it's really easy:

Code:
-- Make sure the characters you use aren't in the data, or you'll have to add
-- some logic to hande them
local keyValueSeparator = "="
local fieldSeparator = "&"

local function hashToExtString(hash)
  local out = {}
  for k, v in pairs(hash) do
    out[#out + 1] = k .. keyValueSeparator .. v
  end
  return out.concat(fieldSeparator)
end

local function hashFromExtString(ext)
  local out = {}
  for pair in ext:gmatch("[^" .. fieldSeparator .. "]+") do
    local k, v = pair:match("(.+)=(.+)")

    if tonumber(v) then
      v = tonumber(v)
    elseif v == "true" then
      v = true
    elseif v == "false" then
      v = false
    end

    out[k] = v
  end

  return out
end


--------------------
-- To save:

local presets = {
  {
    name = "apple",
    min = 5,
    max = 10,
  },
  {
    name = "banana",
    min = 0,
    max = 3,
  },
  {
    name = "cherry",
    min = 2,
    max = 9,
  }
}

for i, preset in pairs(presets) do
  local extString = hashToExtString(preset)
  reaper.SetExtState("MY_PLUGIN", "PRESET_NUMBER_" .. i, extString, true)
end

--------------------
-- To load:

local presets = {}

for i = 1, NUMBER_OF_PRESETS do
  local extString = reaper.GetExtState("MY_PLUGIN", "PRESET_NUMBER_"..i)

  presets[i] = hashFromExtString(extString)
end
(Note: Don't have a chance to test this)

If you do have nested tables, it's a bit more work. Radial Menu stores its settings in a text file, and I found a function that writes all of the table contents as Lua code: https://github.com/ReaTeam/ReaScript...Menu.lua#L5000 . Loading them again is just a matter of reading in the file and telling Lua to execute it.
Lokasenna is offline   Reply With Quote