forked from Benjamin-Dobell/ge_tts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstanceManager.ttslua
275 lines (211 loc) · 9.31 KB
/
InstanceManager.ttslua
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
local Json = require('ge_tts.Json')
local TableUtils = require('ge_tts.TableUtils')
local SAVE_STATE_IDENTIFIER = "__ge_tts_save__"
---@class ge_tts__static_InstanceManager
local InstanceManager = {}
---@class ge_tts__InstanceManager
---@shape __ge_tts__ObjectState
---@field json nil | string
---@field instanceStates table<string, __ge_tts__InstanceState>
---@shape __ge_tts__InstanceState
---@field json nil | string
---@field object nil | tts__Object
---@type nil | ge_tts__InstanceManager
local currentInstanceManager = nil
setmetatable(InstanceManager, {
__call = function()
local self = --[[---@type ge_tts__InstanceManager]] {}
local Instance = require('ge_tts.Instance') -- Required here to prevent top-level cyclical requires.
---@type table<tts__Object, __ge_tts__ObjectState>
local objectStateMap = {}
---@type table<string, __ge_tts__InstanceState>
local instanceStateMap = {}
---@param object tts__Object
---@return __ge_tts__ObjectState
local function getObjectState(object)
local objectState = objectStateMap[(--[[---@not nil]] object)]
if not objectState then
objectState = {
instanceStates = {}
}
objectStateMap[(--[[---@not nil]] object)] = objectState
end
return objectState
end
---@param instanceGuid string
---@return __ge_tts__InstanceState
local function getInstanceState(instanceGuid)
local instanceState = instanceStateMap[instanceGuid]
if not instanceState then
instanceState = {}
instanceStateMap[instanceGuid] = instanceState
end
return instanceState
end
---@param instance ge_tts__Instance
function self.invalidateSavedState(instance)
local instanceGuid = instance.getInstanceGuid()
local instanceState = getInstanceState(instanceGuid)
instanceState.json = nil
local previousObject = instanceState.object
local currentObject = instance.safeGetObject()
if previousObject ~= currentObject then
if previousObject then
local previousObjectState = getObjectState(--[[---@not nil]] previousObject)
previousObjectState.json = nil
previousObjectState.instanceStates[instanceGuid] = nil
if not next(previousObjectState.instanceStates) then
objectStateMap[--[[---@not nil]] previousObject] = nil
end
end
if currentObject then
local currentObjectState = getObjectState(--[[---@not nil]] currentObject)
currentObjectState.json = nil
currentObjectState.instanceStates[instanceGuid] = instanceState
end
instanceState.object = currentObject
elseif currentObject then
local objectState = getObjectState(--[[---@not nil]] currentObject)
objectState.json = nil
end
end
---@param instance ge_tts__Instance
function self.onInstanceDestroyed(instance)
local instanceGuid = instance.getInstanceGuid()
local instanceState = instanceStateMap[instanceGuid]
if not instanceState then
return
end
local previousObject = instanceState.object
if previousObject then
local previousObjectState = getObjectState(--[[---@not nil]] previousObject)
previousObjectState.json = nil
previousObjectState.instanceStates[instanceGuid] = nil
if not next(previousObjectState.instanceStates) then
objectStateMap[--[[---@not nil]] previousObject] = nil
end
end
instanceStateMap[instanceGuid] = nil
end
---@param instance ge_tts__Instance
---@return string
function self.saveInstanceState(instance)
local state = getInstanceState(instance.getInstanceGuid())
if not state.json then
self.invalidateSavedState(instance)
state.json = Json.encode(instance.save())
end
return instance.getInstanceGuid()
end
---@param instanceGuid string
---@return any
function self.loadInstanceState(instanceGuid)
local instanceState = instanceStateMap[instanceGuid]
if not instanceState or not instanceState.json then
error("No instance state available for instance " .. instanceGuid)
end
return Json.decode(--[[---@not nil]] instanceState.json)
end
--- Persists instance states to associated objects.
function self.save()
for object, objectState in pairs(objectStateMap) do
if not objectState.json then
local json = Json.encode(TableUtils.map(objectState.instanceStates, function(instanceState, instanceGuid)
if not instanceState.json then
local instance = Instance.getInstance(instanceGuid)
if instance then
-- Typically mods will be calling InstanceManager.saveInstanceState() themselves. However, if an instance owns another instance
-- it only needs to encode the instance GUID, which never changes. We don't want to require children instances invalidate their
-- parent instance's state just to trigger a resave. As such, we do *also* auto-save instances, but we *don't* handle instance
-- object/container changes at this point. The auto-save functionality is purely to handle parent-child instance relationships.
instanceState.json = Json.encode((--[[---@not nil]] instance).save())
end
end
return instanceState.json
end))
objectState.json = json
if object ~= nil then
if object.script_code == '' then
object.script_code = '--'
end
object.script_state = SAVE_STATE_IDENTIFIER .. json
end
end
end
end
function self.load()
for _, object in ipairs(getAllObjects()) do
local savedState = object.script_state
if savedState:sub(1, SAVE_STATE_IDENTIFIER:len()) == SAVE_STATE_IDENTIFIER then
local objectJson = savedState:sub(SAVE_STATE_IDENTIFIER:len() + 1)
local objectState = getObjectState(object)
objectState.json = objectJson
local instanceStates = --[[---@type table<string, string>]] Json.decode(objectJson)
for instanceGuid, instanceJson in pairs(instanceStates) do
local instanceState = getInstanceState(instanceGuid)
instanceState.object = object
instanceState.json = instanceJson
objectState.instanceStates[instanceGuid] = instanceState
end
end
end
end
function self.destroy()
objectStateMap = {}
instanceStateMap = {}
if currentInstanceManager then
currentInstanceManager = nil
end
end
return self
end,
})
---@return nil | ge_tts__InstanceManager
function InstanceManager.get()
return currentInstanceManager
end
---@param Instance ge_tts__InstanceManager
function InstanceManager.set(manager)
currentInstanceManager = manager
end
---@param instance ge_tts__Instance
function InstanceManager.invalidateSavedState(instance)
if currentInstanceManager then
return (--[[---@not nil]] currentInstanceManager).invalidateSavedState(instance)
end
end
---@param instance ge_tts__Instance
function InstanceManager.onInstanceDestroyed(instance)
if currentInstanceManager then
return (--[[---@not nil]] currentInstanceManager).onInstanceDestroyed(instance)
end
end
---@param instance ge_tts__Instance
---@return string
function InstanceManager.saveInstanceState(instance)
if not currentInstanceManager then
error("InstanceManager not set")
end
return (--[[---@not nil]] currentInstanceManager).saveInstanceState(instance)
end
---@param instanceGuid string
---@return any
function InstanceManager.loadInstanceState(instanceGuid)
if not currentInstanceManager then
error("InstanceManager not set")
end
return (--[[---@not nil]] currentInstanceManager).loadInstanceState(instanceGuid)
end
function InstanceManager.save()
if not currentInstanceManager then
error("InstanceManager not set")
end
(--[[---@not nil]] currentInstanceManager).save()
end
function InstanceManager.load()
if not currentInstanceManager then
error("InstanceManager not set")
end
(--[[---@not nil]] currentInstanceManager).load()
end
return InstanceManager