Versioning shouldn't be too hard to implement and I'd think it'd be almost required to implement if looking to replace the ZO_SavedVars. Here's some ideas for doing versioning and default values:
Store version number
I think this would be best done per data group. Account-wide and OS-wide you could put one level up from where the data is stored easily. Per character you either need to put in with the data (and just have the version field be reserved) or put under the account level and prefix with character name.
New format sample:
Lua Code:
SavedVariableName = {
["libASV_Strcuture_1"] = {
libASV_OS_version = 3,
["libASV_OS_user_wide"] = {
--data goes here
},
--Showing first option for characters here
["@acountname1"] = {
libASV_version = 1, --For AccountWide variables
["libASV_AccountWide"] = {},
version_Character1 = 1,
["Character1"] = {},
version_Character2 = 1,
["Character2"] = {}
},
--Showing second option for characters here
["@acountname2"] = {
libASV_version = 1,
["libASV_AccountWide"] = {},
["Character 3"] = {
libASV_version = 1, --Reserved field
--Other data
}
}
}
}
Access functions
You'd need to pass in the version number and an upgrade function. You could do a variable-wide upgrade, but that would expose your underlying structure, which is hardly ideal.
:AccessOSUserWide("SavedVariableName", version, [defaults], [upgradeFunction])
:AccountWide("SavedVariableName", version, [defaults], [upgradeFunction], [accountOverride])
:AllCharacters("SavedVariableName", [accountOverride])
:CharacterWide("SavedVariableName", version, [defaults], [upgradeFunction], [accountOverride], [characterOverride])
The function behavior (except AllCharacters) would be:
Code:
IF not initialized THEN
COPY in default values
RETURN table
END
IF version different THEN
IF upgradeFunction THEN
CALL upgrade function on table
ELSE
COPY in default values
END
END
RETURN table
You'd need to use the deep copy utility function for setting the default variables.
Upgrade function
Parameters:
- existing version
- existing table
Result/return:
Could either have the function modify the table in-place (more efficient) or just return the new table (susceptible to errors if not familiar with tables are reference).
Again, you don't know what to do for upgrade in the default, so you'd just reset to defaults.