-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsavedata.lua
136 lines (130 loc) · 4.51 KB
/
savedata.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
if not ItemTrig then return end
local function _prepTriggersToSave(tList)
local s = {}
for i = 1, #tList do
s[i] = tList[i]:serialize()
end
s = table.concat(s)
--
-- serialized strings are capped at 2000 chars; we need to split them up
--
s = ItemTrig.splitByCount(s, 1500)
return s
end
local savedVars = ItemTrig.ISavedata:new("ItemTrigSavedata", nil, 2)
--
-- If we change the savedata format, we'll want to call:
--
-- savedVars:addUpdateRoutine(myFunction)
--
-- where myFunction takes two args: an ISavedataCharacter instance and
-- its prior version. Update routines will be run in order, so we can
-- just keep adding 'em and literally update from version to version.
--
-- However, if we need to update trigger data, we will need to do that
-- separately, since update routines only run on the original saved
-- data (trigger data is serialized to keep its size to a minimum; we
-- can't work with it in that form). Code for updating triggers is in
-- ItemTrig.Savedata:load, below.
--
do -- Define defaults
local _defaults = {
serializedTriggers = {}
}
savedVars.defaults = _defaults
end
local function _saveTriggers(characterID, tList)
savedVars:character(characterID):data().serializedTriggers = _prepTriggersToSave(tList)
end
ItemTrig.Savedata = {
interface = savedVars,
prefs = nil, -- if not nil, then it is a field in the character data; should autosave
triggers = {},
}
function ItemTrig.Savedata:save(characterID)
_saveTriggers(characterID, self.triggers)
end
function ItemTrig.Savedata:load(characterID)
local character = savedVars:character(characterID)
local cData = character:data()
do -- set up prefs
local p = cData.prefs
if not p then
cData.prefs = {}
p = cData.prefs
end
self.prefs = p
end
self.triggers = self:loadTriggersFor(characterID)
if ItemTrig.prefs:get("updateGalleryTriggers") ~= false then -- update gallery triggers, if need be
local gallery -- lazy load
local indexedGallery = {}
local function _lazyLoad()
gallery = ItemTrig.retrieveTriggerGallery()
for i = 1, #gallery do
local t = gallery[i]
if t.galleryID then
indexedGallery[t.galleryID] = t
end
end
end
--
local list = self.triggers
for i = 1, #list do
local trigger = list[i]
local id = trigger.galleryID
if id then
if not gallery then
_lazyLoad()
end
if indexedGallery[id] then
trigger:updateGalleryTrigger(indexedGallery[id]) -- checks and whatnot are done here
end
end
end
end
do -- update triggers
--
-- Unfortunately, although ISavedata offers the ability to register
-- "update handlers," to be called behind the scenes, we can't use
-- those. This is because trigger data is serialized into strings in
-- order to minimize its saved size, and the update handlers work on
-- the original savedata rather than what we've parsed and loaded for
-- our use. We have to update trigger data here. ISavedata's version-
-- ing system is still useful for keeping track of what to update and
-- when, which is nice.
--
if character._initiallyLoadedVersion < 2 then
--
-- v1.0.10
-- Condition "Total Count" uses the wrong enum value to
-- identify the Craft Bag.
--
local baseToUpdate = ItemTrig.tableConditions[11]
for i = 1, #self.triggers do
local trigger = self.triggers[i]
local cList = trigger.conditions
for j = 1, #cList do
local condition = cList[j]
if condition.base == baseToUpdate then
local bagIndex = condition.args[1]
if bagIndex == BAG_SUBSCRIBER_BANK then
condition.args[1] = BAG_VIRTUAL
end
end
end
end
end
character:setIsUpToDate()
end
end
function ItemTrig.Savedata:loadTriggersFor(characterID)
local interface = savedVars:character(characterID)
do -- saved triggers
local s = table.concat(interface:data().serializedTriggers or {})
if s:len() == 0 then
return {}
end
return ItemTrig.parseTrigger(s)
end
end