@quenty/saveslot 2.7.1 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/package.json +4 -4
- package/src/Client/SaveSlotServiceClient.lua +1 -0
- package/src/Server/Binders/HasSaveSlots.ExportImport.spec.lua +1 -1
- package/src/Server/Binders/HasSaveSlots.SharedStore.spec.lua +37 -0
- package/src/Server/Binders/HasSaveSlots.TransferableEphemeral.spec.lua +92 -12
- package/src/Server/Binders/HasSaveSlots.lua +169 -1010
- package/src/Server/Binders/HasSaveSlots.spec.lua +10 -10
- package/src/Server/Cmdr/SaveSlotCmdrService.lua +565 -318
- package/src/Server/Cmdr/SaveSlotCmdrService.spec.lua +420 -0
- package/src/Server/HasSaveSlotsDataStore.lua +1658 -0
- package/src/Server/OfflineSaveSlots.lua +170 -0
- package/src/Server/OfflineSaveSlots.spec.lua +174 -0
- package/src/Server/SaveSlotService.lua +64 -2
- package/src/Shared/Cmdr/SaveSlotCmdrUtils.lua +206 -2
- package/src/Shared/Cmdr/SaveSlotCmdrUtils.spec.lua +68 -1
- package/src/Shared/SaveSlotExportUtils.lua +65 -1
- package/src/Shared/SaveSlotExportUtils.spec.lua +36 -0
|
@@ -0,0 +1,1658 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
The save slot system over a [DataStore], with no [Player] attached.
|
|
4
|
+
|
|
5
|
+
This is the whole slot model -- the roster, the per-slot stores, selection, create/delete/reset,
|
|
6
|
+
export/import, ephemeral slots and playtime -- expressed against an injected datastore rather than
|
|
7
|
+
against a bound player. [HasSaveSlots] is the binder that owns one of these for a live player and
|
|
8
|
+
supplies the player-shaped parts (replication, remotes, teleport data, summary providers).
|
|
9
|
+
|
|
10
|
+
Splitting it out is what makes a slot reachable for a player who is not in this server: admin
|
|
11
|
+
tooling borrows that player's datastore (see `PlayerDataStoreHandle`) and builds one of these over
|
|
12
|
+
it, getting the same slot semantics with no [Player] instance in sight.
|
|
13
|
+
|
|
14
|
+
The slot roster is still modelled as Folders with attributes, because that representation is what
|
|
15
|
+
the client reads. Offline, those folders simply live in an unparented container and replicate to
|
|
16
|
+
nobody.
|
|
17
|
+
|
|
18
|
+
:::info
|
|
19
|
+
Constructed with a *promise* of a datastore rather than a datastore, so every method can gate on
|
|
20
|
+
the same load promise the player path always gated on. Callers do not have to wait before using it.
|
|
21
|
+
:::
|
|
22
|
+
|
|
23
|
+
@server
|
|
24
|
+
@class HasSaveSlotsDataStore
|
|
25
|
+
]=]
|
|
26
|
+
|
|
27
|
+
local require = require(script.Parent.loader).load(script)
|
|
28
|
+
|
|
29
|
+
local HttpService = game:GetService("HttpService")
|
|
30
|
+
|
|
31
|
+
local BaseObject = require("BaseObject")
|
|
32
|
+
local Brio = require("Brio")
|
|
33
|
+
local DataStoreStage = require("DataStoreStage")
|
|
34
|
+
local InMemoryDataStore = require("InMemoryDataStore")
|
|
35
|
+
local Maid = require("Maid")
|
|
36
|
+
local Observable = require("Observable")
|
|
37
|
+
local Promise = require("Promise")
|
|
38
|
+
local Rx = require("Rx")
|
|
39
|
+
local RxBrioUtils = require("RxBrioUtils")
|
|
40
|
+
local SaveSlotCodeUtils = require("SaveSlotCodeUtils")
|
|
41
|
+
local SaveSlotConstants = require("SaveSlotConstants")
|
|
42
|
+
local SaveSlotData = require("SaveSlotData")
|
|
43
|
+
local SaveSlotExportUtils = require("SaveSlotExportUtils")
|
|
44
|
+
local ValueObject = require("ValueObject")
|
|
45
|
+
|
|
46
|
+
-- The caller-supplied fields for a new slot. SlotId and SlotIndex are assigned by PromiseCreateSlot
|
|
47
|
+
-- itself (from a fresh GUID and the slotIndex argument), so they are never taken from here.
|
|
48
|
+
-- TimePlayed seeds the new slot's accrued playtime, which is what lets a copy of a slot (duplicate or
|
|
49
|
+
-- export/import) carry the playtime that belongs to the progress being copied.
|
|
50
|
+
export type SaveSlotCreateMetadata = {
|
|
51
|
+
SlotName: string?,
|
|
52
|
+
Summary: SaveSlotData.SaveSlotSummary?,
|
|
53
|
+
TimePlayed: number?,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
--[=[
|
|
57
|
+
The state and collaborators this class cannot derive from a datastore alone.
|
|
58
|
+
|
|
59
|
+
The four value objects are *borrowed*, never owned: the player path passes the ones backed by
|
|
60
|
+
replicated player attributes, so writing them replicates, while offline tooling passes detached
|
|
61
|
+
ones. Either way this class only reads and writes them, and never destroys them.
|
|
62
|
+
|
|
63
|
+
@interface HasSaveSlotsDataStoreOptions
|
|
64
|
+
.ActiveSlotId ValueObject<SlotId?>
|
|
65
|
+
.LastActiveSlotId ValueObject<SlotId?>
|
|
66
|
+
.ActiveTransferableEphemeralKey ValueObject<string?>
|
|
67
|
+
.MaxSlotCount ValueObject<number>
|
|
68
|
+
.SharedDataStoreService any -- SaveSlotSharedDataStoreService
|
|
69
|
+
.PromisePreSelect ((SlotId) -> Promise<boolean>)? -- veto hook, defaults to allowing every selection
|
|
70
|
+
.UserId number? -- identity for generated share codes
|
|
71
|
+
.UserName string? -- identity for generated share codes
|
|
72
|
+
.TrackPlaytime boolean? -- accrue play sessions from selection changes, defaults to true
|
|
73
|
+
@within HasSaveSlotsDataStore
|
|
74
|
+
]=]
|
|
75
|
+
export type HasSaveSlotsDataStoreOptions = {
|
|
76
|
+
ActiveSlotId: ValueObject.ValueObject<SaveSlotData.SlotId?>,
|
|
77
|
+
LastActiveSlotId: ValueObject.ValueObject<SaveSlotData.SlotId?>,
|
|
78
|
+
ActiveTransferableEphemeralKey: ValueObject.ValueObject<string?>,
|
|
79
|
+
MaxSlotCount: ValueObject.ValueObject<number>,
|
|
80
|
+
SharedDataStoreService: any,
|
|
81
|
+
PromisePreSelect: ((SaveSlotData.SlotId) -> Promise.Promise<boolean>)?,
|
|
82
|
+
UserId: number?,
|
|
83
|
+
UserName: string?,
|
|
84
|
+
TrackPlaytime: boolean?,
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
local HasSaveSlotsDataStore = {}
|
|
88
|
+
HasSaveSlotsDataStore.ClassName = "HasSaveSlotsDataStore"
|
|
89
|
+
HasSaveSlotsDataStore.__index = HasSaveSlotsDataStore
|
|
90
|
+
-- Runtime inheritance only, typed as `any`, and the inherited surface supplied structurally below.
|
|
91
|
+
-- [HasSaveSlots] names this type across every delegation, so intersecting the full BaseObject type
|
|
92
|
+
-- here overflows the old solver's complexity budget over there ("Code is too complex").
|
|
93
|
+
setmetatable(HasSaveSlotsDataStore :: any, BaseObject)
|
|
94
|
+
|
|
95
|
+
-- Minimal structural surface inherited from BaseObject. See the setmetatable note above.
|
|
96
|
+
type BaseObjectLike = {
|
|
97
|
+
_maid: Maid.Maid,
|
|
98
|
+
Destroy: (self: any) -> (),
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type HasSaveSlotsDataStore =
|
|
102
|
+
typeof(setmetatable(
|
|
103
|
+
{} :: {
|
|
104
|
+
_slotContainer: Folder,
|
|
105
|
+
_slotMap: { [SaveSlotData.SlotId]: Folder },
|
|
106
|
+
-- The in-memory stores backing ephemeral (session-only) slots, keyed by slot id. A slot's
|
|
107
|
+
-- IsEphemeral property is the discriminator (see _isEphemeral); this holds the store objects a
|
|
108
|
+
-- property can't. See PromiseSelectEphemeralSlot.
|
|
109
|
+
_ephemeralStores: { [SaveSlotData.SlotId]: InMemoryDataStore.InMemoryDataStore },
|
|
110
|
+
-- The shared-store key each transferable ephemeral slot was loaded from, so a teleport can
|
|
111
|
+
-- re-save its live state under that key and carry it forward. See PromiseSelectTransferableEphemeralSlot.
|
|
112
|
+
_transferableEphemeralKeys: { [SaveSlotData.SlotId]: string },
|
|
113
|
+
_codeGenerator: SaveSlotCodeUtils.CodeGenerator,
|
|
114
|
+
_loadPromise: Promise.Promise<{}>,
|
|
115
|
+
_dataStore: any,
|
|
116
|
+
_systemStore: any,
|
|
117
|
+
_metadataStore: any,
|
|
118
|
+
_lastActiveSlotId: SaveSlotData.SlotId?,
|
|
119
|
+
_sharedSaveSlotDataStoreService: any,
|
|
120
|
+
_promisePreSelect: ((SaveSlotData.SlotId) -> Promise.Promise<boolean>)?,
|
|
121
|
+
_userId: number?,
|
|
122
|
+
_userName: string?,
|
|
123
|
+
_playSessionSlotId: SaveSlotData.SlotId?,
|
|
124
|
+
_playSessionStart: number?,
|
|
125
|
+
_playSessionLastFlush: number?,
|
|
126
|
+
|
|
127
|
+
ActiveSlotId: ValueObject.ValueObject<SaveSlotData.SlotId?>,
|
|
128
|
+
LastActiveSlotId: ValueObject.ValueObject<SaveSlotData.SlotId?>,
|
|
129
|
+
ActiveTransferableEphemeralKey: ValueObject.ValueObject<string?>,
|
|
130
|
+
MaxSlotCount: ValueObject.ValueObject<number>,
|
|
131
|
+
SlotContainerParent: ValueObject.ValueObject<Instance?>,
|
|
132
|
+
},
|
|
133
|
+
{} :: typeof({ __index = HasSaveSlotsDataStore })
|
|
134
|
+
))
|
|
135
|
+
& BaseObjectLike
|
|
136
|
+
|
|
137
|
+
--[=[
|
|
138
|
+
Builds the slot system over a datastore that may not have loaded yet.
|
|
139
|
+
|
|
140
|
+
@param promiseDataStore Promise<DataStore>
|
|
141
|
+
@param options HasSaveSlotsDataStoreOptions
|
|
142
|
+
@return HasSaveSlotsDataStore
|
|
143
|
+
]=]
|
|
144
|
+
function HasSaveSlotsDataStore.new(
|
|
145
|
+
promiseDataStore: Promise.Promise<any>,
|
|
146
|
+
options: HasSaveSlotsDataStoreOptions
|
|
147
|
+
): HasSaveSlotsDataStore
|
|
148
|
+
local self: HasSaveSlotsDataStore = setmetatable(BaseObject.new() :: any, HasSaveSlotsDataStore)
|
|
149
|
+
|
|
150
|
+
assert(promiseDataStore, "No promiseDataStore")
|
|
151
|
+
assert(options, "No options")
|
|
152
|
+
|
|
153
|
+
self.ActiveSlotId = assert(options.ActiveSlotId, "No ActiveSlotId")
|
|
154
|
+
self.LastActiveSlotId = assert(options.LastActiveSlotId, "No LastActiveSlotId")
|
|
155
|
+
self.ActiveTransferableEphemeralKey =
|
|
156
|
+
assert(options.ActiveTransferableEphemeralKey, "No ActiveTransferableEphemeralKey")
|
|
157
|
+
self.MaxSlotCount = assert(options.MaxSlotCount, "No MaxSlotCount")
|
|
158
|
+
|
|
159
|
+
self._sharedSaveSlotDataStoreService = assert(options.SharedDataStoreService, "No SharedDataStoreService")
|
|
160
|
+
self._promisePreSelect = options.PromisePreSelect
|
|
161
|
+
self._userId = options.UserId
|
|
162
|
+
self._userName = options.UserName
|
|
163
|
+
|
|
164
|
+
self._slotContainer = self._maid:Add(Instance.new("Folder"))
|
|
165
|
+
self._slotContainer.Name = SaveSlotConstants.METADATA_CONTAINER_NAME
|
|
166
|
+
self._slotContainer.Archivable = false
|
|
167
|
+
|
|
168
|
+
-- Where the slot roster replicates from. Nothing here knows about players, so the owner mounts it:
|
|
169
|
+
-- the binder mounts the bound player, and offline tooling mounts nothing and the roster stays
|
|
170
|
+
-- unparented, replicating to no one.
|
|
171
|
+
self.SlotContainerParent = self._maid:Add(ValueObject.new(nil))
|
|
172
|
+
self._maid:GiveTask(self.SlotContainerParent:Observe():Subscribe(function(parent: Instance?)
|
|
173
|
+
self._slotContainer.Parent = parent
|
|
174
|
+
end))
|
|
175
|
+
|
|
176
|
+
self._slotMap = {}
|
|
177
|
+
self._ephemeralStores = {}
|
|
178
|
+
self._transferableEphemeralKeys = {}
|
|
179
|
+
self._codeGenerator = SaveSlotCodeUtils.generateDefaultCode
|
|
180
|
+
|
|
181
|
+
self._loadPromise = self._maid:GivePromise(self:_promiseLoadSlots(promiseDataStore))
|
|
182
|
+
|
|
183
|
+
if options.TrackPlaytime ~= false then
|
|
184
|
+
self:_setupPlaytimeTracking()
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
return self
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
--[=[
|
|
191
|
+
Returns the folder holding the slot's replicated metadata, or nil when there is no such slot.
|
|
192
|
+
The folder is owned by this object; do not parent or destroy it.
|
|
193
|
+
|
|
194
|
+
@param slotId SlotId?
|
|
195
|
+
@return Folder?
|
|
196
|
+
]=]
|
|
197
|
+
function HasSaveSlotsDataStore.GetSlotFolder(self: HasSaveSlotsDataStore, slotId: SaveSlotData.SlotId?): Folder?
|
|
198
|
+
if slotId == nil then
|
|
199
|
+
return nil
|
|
200
|
+
end
|
|
201
|
+
return self._slotMap[slotId]
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
--[=[
|
|
205
|
+
Returns the active slot's id, or nil when nothing is selected.
|
|
206
|
+
|
|
207
|
+
@return SlotId?
|
|
208
|
+
]=]
|
|
209
|
+
function HasSaveSlotsDataStore.GetActiveSlotId(self: HasSaveSlotsDataStore): SaveSlotData.SlotId?
|
|
210
|
+
return self.ActiveSlotId.Value
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
--[=[
|
|
214
|
+
Returns the slot the player is on, or would resume on: the active slot when one is selected, and
|
|
215
|
+
the persisted "Continue" target otherwise. The synchronous twin of
|
|
216
|
+
[HasSaveSlotsDataStore.PromiseLastActiveSlotId].
|
|
217
|
+
|
|
218
|
+
This is the one to read when acting on "their current slot" without a live session. Freshly opened
|
|
219
|
+
against a datastore, nothing is selected yet -- the stored pointer loads as the last-active slot,
|
|
220
|
+
and [HasSaveSlotsDataStore.GetActiveSlotId] stays nil until something selects.
|
|
221
|
+
|
|
222
|
+
@return SlotId?
|
|
223
|
+
]=]
|
|
224
|
+
function HasSaveSlotsDataStore.GetLastActiveSlotId(self: HasSaveSlotsDataStore): SaveSlotData.SlotId?
|
|
225
|
+
return self.ActiveSlotId.Value or self._lastActiveSlotId
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
--[=[
|
|
229
|
+
Returns whether the active slot is an ephemeral (session-only) one. False when nothing is active.
|
|
230
|
+
|
|
231
|
+
@return boolean
|
|
232
|
+
]=]
|
|
233
|
+
function HasSaveSlotsDataStore.IsActiveSlotEphemeral(self: HasSaveSlotsDataStore): boolean
|
|
234
|
+
return self:_isEphemeral(self.ActiveSlotId.Value)
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
--[=[
|
|
238
|
+
Returns the slot's metadata, or nil when there is no such slot.
|
|
239
|
+
|
|
240
|
+
@param slotId SlotId?
|
|
241
|
+
@return SaveSlotMetadata?
|
|
242
|
+
]=]
|
|
243
|
+
function HasSaveSlotsDataStore.GetSlotMetadata(
|
|
244
|
+
self: HasSaveSlotsDataStore,
|
|
245
|
+
slotId: SaveSlotData.SlotId?
|
|
246
|
+
): SaveSlotData.SaveSlotMetadata?
|
|
247
|
+
local slot = self:GetSlotFolder(slotId)
|
|
248
|
+
if not slot then
|
|
249
|
+
return nil
|
|
250
|
+
end
|
|
251
|
+
return SaveSlotData:Get(slot)
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
--[=[
|
|
255
|
+
Returns the active slot's metadata, or nil when nothing is selected. Unlike
|
|
256
|
+
[HasSaveSlotsDataStore.GetSlotList] this does see an ephemeral slot, which is the only way to read
|
|
257
|
+
the metadata of a session in progress.
|
|
258
|
+
|
|
259
|
+
@return SaveSlotMetadata?
|
|
260
|
+
]=]
|
|
261
|
+
function HasSaveSlotsDataStore.GetActiveSlotMetadata(self: HasSaveSlotsDataStore): SaveSlotData.SaveSlotMetadata?
|
|
262
|
+
return self:GetSlotMetadata(self.ActiveSlotId.Value)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
--[=[
|
|
266
|
+
Returns the persisted slots, ordered by index. Ephemeral slots are excluded, matching
|
|
267
|
+
[SaveSlotDataService.GetSlotList] -- read the active slot directly with
|
|
268
|
+
[HasSaveSlotsDataStore.GetActiveSlotMetadata] to see one.
|
|
269
|
+
|
|
270
|
+
Ordered because the underlying map is not: an unordered listing would shuffle between calls, and
|
|
271
|
+
the tooling reading this prints it.
|
|
272
|
+
|
|
273
|
+
@return { SaveSlotMetadata }
|
|
274
|
+
]=]
|
|
275
|
+
function HasSaveSlotsDataStore.GetSlotList(self: HasSaveSlotsDataStore): { SaveSlotData.SaveSlotMetadata }
|
|
276
|
+
local slotList = {}
|
|
277
|
+
|
|
278
|
+
for slotId, slot in self._slotMap do
|
|
279
|
+
if not self:_isEphemeral(slotId) then
|
|
280
|
+
table.insert(slotList, SaveSlotData:Get(slot))
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
table.sort(slotList, function(a, b)
|
|
285
|
+
return a.SlotIndex < b.SlotIndex
|
|
286
|
+
end)
|
|
287
|
+
|
|
288
|
+
return slotList
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
--[=[
|
|
292
|
+
Returns the id of the persisted slot at the given index, or nil when there is none. Ephemeral
|
|
293
|
+
slots are never addressable by index.
|
|
294
|
+
|
|
295
|
+
@param slotIndex number
|
|
296
|
+
@return SlotId?
|
|
297
|
+
]=]
|
|
298
|
+
function HasSaveSlotsDataStore.GetSlotIdFromIndex(self: HasSaveSlotsDataStore, slotIndex: number): SaveSlotData.SlotId?
|
|
299
|
+
for slotId, slot in self._slotMap do
|
|
300
|
+
if not self:_isEphemeral(slotId) and slotIndex == SaveSlotData.SlotIndex:Get(slot) then
|
|
301
|
+
return slotId
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
return nil
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
--[=[
|
|
308
|
+
Returns the [DataStoreStage] backing the slot's saved data. For the default slot this is the
|
|
309
|
+
player's root store; for an ephemeral slot it is an in-memory store.
|
|
310
|
+
|
|
311
|
+
@param slotId SlotId
|
|
312
|
+
@return DataStoreStage
|
|
313
|
+
]=]
|
|
314
|
+
function HasSaveSlotsDataStore.GetSlotStore(
|
|
315
|
+
self: HasSaveSlotsDataStore,
|
|
316
|
+
slotId: SaveSlotData.SlotId
|
|
317
|
+
): DataStoreStage.DataStoreStage
|
|
318
|
+
return self:_getSlotStore(slotId)
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
--[=[
|
|
322
|
+
Observes the [DataStoreStage] for the active slot as a [Brio]
|
|
323
|
+
]=]
|
|
324
|
+
function HasSaveSlotsDataStore.ObserveActiveSlotStoreBrio(self: HasSaveSlotsDataStore): Observable.Observable<
|
|
325
|
+
Brio.Brio<DataStoreStage.DataStoreStage>
|
|
326
|
+
>
|
|
327
|
+
return Rx.fromPromise(self._loadPromise):Pipe({
|
|
328
|
+
Rx.switchMap(function()
|
|
329
|
+
return self.ActiveSlotId
|
|
330
|
+
:ObserveBrio(function(slotId: SaveSlotData.SlotId?)
|
|
331
|
+
return (slotId ~= nil)
|
|
332
|
+
end)
|
|
333
|
+
:Pipe({
|
|
334
|
+
RxBrioUtils.map(function(slotId: SaveSlotData.SlotId)
|
|
335
|
+
return self:_getSlotStore(slotId)
|
|
336
|
+
end) :: any,
|
|
337
|
+
}) :: any
|
|
338
|
+
end) :: any,
|
|
339
|
+
}) :: any
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
--[=[
|
|
343
|
+
Returns the [DataStoreStage] for the active slot
|
|
344
|
+
]=]
|
|
345
|
+
function HasSaveSlotsDataStore.PromiseActiveSlotStore(
|
|
346
|
+
self: HasSaveSlotsDataStore
|
|
347
|
+
): Promise.Promise<DataStoreStage.DataStoreStage?>
|
|
348
|
+
return (self._loadPromise :: any):Then(function()
|
|
349
|
+
if not self.ActiveSlotId.Value then
|
|
350
|
+
return (Promise :: any).resolved(nil)
|
|
351
|
+
end
|
|
352
|
+
return self:_getSlotStore(self.ActiveSlotId.Value)
|
|
353
|
+
end)
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
--[=[
|
|
357
|
+
Promises that all slots have loaded
|
|
358
|
+
]=]
|
|
359
|
+
function HasSaveSlotsDataStore.PromiseSlotsLoaded(self: HasSaveSlotsDataStore): Promise.Promise<any>
|
|
360
|
+
return self._loadPromise
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
--[=[
|
|
364
|
+
Returns whether the slot with the given ID exists
|
|
365
|
+
]=]
|
|
366
|
+
function HasSaveSlotsDataStore.PromiseHasSlot(
|
|
367
|
+
self: HasSaveSlotsDataStore,
|
|
368
|
+
slotId: SaveSlotData.SlotId?
|
|
369
|
+
): Promise.Promise<boolean>
|
|
370
|
+
return (self._loadPromise :: any):Then(function()
|
|
371
|
+
return slotId and ((self._slotMap[slotId] :: Folder?) ~= nil)
|
|
372
|
+
end)
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
-- Runs the injected pre-select veto, if there is one. A caller that registered none allows every
|
|
376
|
+
-- selection, which is what offline tooling and a binder without SaveSlotService both want.
|
|
377
|
+
function HasSaveSlotsDataStore._promiseRunPreSelect(
|
|
378
|
+
self: HasSaveSlotsDataStore,
|
|
379
|
+
slotId: SaveSlotData.SlotId
|
|
380
|
+
): Promise.Promise<boolean>
|
|
381
|
+
local promisePreSelect = self._promisePreSelect
|
|
382
|
+
if not promisePreSelect then
|
|
383
|
+
return (Promise :: any).resolved(true)
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
return promisePreSelect(slotId)
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
--[=[
|
|
390
|
+
Selects the slot with the given ID
|
|
391
|
+
]=]
|
|
392
|
+
function HasSaveSlotsDataStore.PromiseSelectSlot(
|
|
393
|
+
self: HasSaveSlotsDataStore,
|
|
394
|
+
slotId: SaveSlotData.SlotId
|
|
395
|
+
): Promise.Promise<any>
|
|
396
|
+
return (self._loadPromise :: any):Then(function()
|
|
397
|
+
if slotId == self.ActiveSlotId.Value then
|
|
398
|
+
return -- Already set
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
local slot = self._slotMap[slotId]
|
|
402
|
+
if not slot then
|
|
403
|
+
return (Promise :: any).rejected(`Slot \{{slotId}\} not found`)
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
local function setSlot()
|
|
407
|
+
self.ActiveSlotId.Value = slotId
|
|
408
|
+
SaveSlotData.LastPlayedTime:Set(slot, os.time())
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
local function promiseSetSlot()
|
|
412
|
+
return self._maid:GivePromise(self:_promiseRunPreSelect(slotId)):Then(function(allowed: boolean)
|
|
413
|
+
if not allowed then
|
|
414
|
+
return (Promise :: any).rejected(`Slot \{{slotId}\} refused by a pre-select callback`)
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
return setSlot()
|
|
418
|
+
end)
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
-- Initialize or save and switch
|
|
422
|
+
if self.ActiveSlotId.Value == nil then
|
|
423
|
+
return promiseSetSlot()
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
-- Leaving an ephemeral slot has nothing to persist, so switch without a datastore flush (the flush
|
|
427
|
+
-- exists to save the outgoing slot's progress, and an ephemeral slot has none).
|
|
428
|
+
if self:_isEphemeral(self.ActiveSlotId.Value) then
|
|
429
|
+
return promiseSetSlot()
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
return self._maid:GivePromise(self._dataStore:Save()):Then(promiseSetSlot)
|
|
433
|
+
end)
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
--[=[
|
|
437
|
+
Clears the active slot selection, returning the player to a no-slot state --
|
|
438
|
+
the counterpart to [HasSaveSlotsDataStore.PromiseSelectSlot], backing a "back to menu"
|
|
439
|
+
affordance. The active slot's progress is flushed first (mirroring the save
|
|
440
|
+
PromiseSelectSlot runs when switching away), and the last-active slot is
|
|
441
|
+
remembered, so [HasSaveSlotsDataStore.PromiseSelectLastSaveSlot] can resume it later.
|
|
442
|
+
A no-op when no slot is active.
|
|
443
|
+
]=]
|
|
444
|
+
function HasSaveSlotsDataStore.PromiseDeselectSlot(self: HasSaveSlotsDataStore): Promise.Promise<()>
|
|
445
|
+
return (self._loadPromise :: any):Then(function()
|
|
446
|
+
if self.ActiveSlotId.Value == nil then
|
|
447
|
+
return -- Already deselected
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
-- An ephemeral slot has nothing to flush, so clear it without a datastore save.
|
|
451
|
+
if self:_isEphemeral(self.ActiveSlotId.Value) then
|
|
452
|
+
self.ActiveSlotId.Value = nil
|
|
453
|
+
return
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
return self._dataStore:Save():Then(function()
|
|
457
|
+
self.ActiveSlotId.Value = nil
|
|
458
|
+
end)
|
|
459
|
+
end)
|
|
460
|
+
end
|
|
461
|
+
|
|
462
|
+
--[=[
|
|
463
|
+
Creates a slot at the given index
|
|
464
|
+
]=]
|
|
465
|
+
function HasSaveSlotsDataStore.PromiseCreateSlot(
|
|
466
|
+
self: HasSaveSlotsDataStore,
|
|
467
|
+
slotIndex: number,
|
|
468
|
+
metadata: SaveSlotCreateMetadata?
|
|
469
|
+
): Promise.Promise<SaveSlotData.SlotId>
|
|
470
|
+
return (self._loadPromise :: any):Then(function()
|
|
471
|
+
if (slotIndex < 1) or (slotIndex > self.MaxSlotCount.Value) then
|
|
472
|
+
return (Promise :: any).rejected(`Index must be in range [1, {self.MaxSlotCount.Value}]`)
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
for existingSlotId, slot in self._slotMap do
|
|
476
|
+
if self:_isEphemeral(existingSlotId) then
|
|
477
|
+
continue -- ephemeral slots carry no meaningful index; never let one block a real index
|
|
478
|
+
end
|
|
479
|
+
if slotIndex == SaveSlotData.SlotIndex:Get(slot) then
|
|
480
|
+
return (Promise :: any).rejected(`Slot {slotIndex} already exists`)
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
local slotId = HttpService:GenerateGUID(false)
|
|
485
|
+
local data = {
|
|
486
|
+
SlotId = slotId,
|
|
487
|
+
SlotIndex = slotIndex,
|
|
488
|
+
SlotName = (metadata and metadata.SlotName) or `Slot {slotIndex}`,
|
|
489
|
+
CreatedTime = os.time(),
|
|
490
|
+
Summary = metadata and metadata.Summary,
|
|
491
|
+
TimePlayed = metadata and metadata.TimePlayed,
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
self:_buildSlot(slotId, data, true)
|
|
495
|
+
return slotId
|
|
496
|
+
end)
|
|
497
|
+
end
|
|
498
|
+
|
|
499
|
+
--[=[
|
|
500
|
+
Exports a slot's saved data into a plain, serializable [SaveSlotExportUtils.SaveSlotExport].
|
|
501
|
+
Rejects the main/default slot by default: its store is the player's shared root datastore, so
|
|
502
|
+
exporting it would leak the SaveSlots system data and universe-scoped global data living
|
|
503
|
+
alongside it. Only isolated non-main slot substores are exportable.
|
|
504
|
+
|
|
505
|
+
`allowMainSlot` opts out of that refusal for trusted admin tooling (see [SaveSlotCmdrService]).
|
|
506
|
+
The SaveSlots system data is always stripped from the result, so the export never carries the
|
|
507
|
+
slot roster or the other slots' saved data. Universe-scoped global data sharing the root store
|
|
508
|
+
is indistinguishable from the main slot's own data and is still carried, which is why this is
|
|
509
|
+
not exposed through [SaveSlotService].
|
|
510
|
+
|
|
511
|
+
@param slotId SlotId
|
|
512
|
+
@param allowMainSlot boolean? -- defaults to false
|
|
513
|
+
@return Promise<SaveSlotExportUtils.SaveSlotExport>
|
|
514
|
+
]=]
|
|
515
|
+
function HasSaveSlotsDataStore.PromiseExportSlot(
|
|
516
|
+
self: HasSaveSlotsDataStore,
|
|
517
|
+
slotId: SaveSlotData.SlotId,
|
|
518
|
+
allowMainSlot: boolean?
|
|
519
|
+
): Promise.Promise<SaveSlotExportUtils.SaveSlotExport>
|
|
520
|
+
return (self._loadPromise :: any):Then(function()
|
|
521
|
+
local slot = self._slotMap[slotId]
|
|
522
|
+
if not slot then
|
|
523
|
+
return (Promise :: any).rejected(`Slot \{{slotId}\} not found`)
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
local isMainSlot = SaveSlotExportUtils.isMainSlotIndex(SaveSlotData.SlotIndex:Get(slot))
|
|
527
|
+
if isMainSlot and not allowMainSlot then
|
|
528
|
+
return (Promise :: any).rejected("Cannot export the main slot")
|
|
529
|
+
end
|
|
530
|
+
|
|
531
|
+
-- As in PromiseDuplicateSlot: bring the live session's accrued time into TimePlayed before it is
|
|
532
|
+
-- read, so an export of the active slot carries its playtime up to this moment.
|
|
533
|
+
self:_flushPlaytime()
|
|
534
|
+
|
|
535
|
+
local metadata = SaveSlotData:Get(slot)
|
|
536
|
+
return self:_getSlotStore(slotId):LoadAll({}):Then(function(sourceData)
|
|
537
|
+
local data = if type(sourceData) == "table" then table.clone(sourceData) else {}
|
|
538
|
+
|
|
539
|
+
if isMainSlot then
|
|
540
|
+
-- The main slot's store is the player's shared root, so LoadAll pulls the SaveSlots system
|
|
541
|
+
-- data with it -- the slot roster and every non-main slot's saved data. Drop it so the
|
|
542
|
+
-- export is only this slot, and so importing it can't nest a whole roster inside a slot
|
|
543
|
+
-- (mirrors PromiseDuplicateSlot, which strips the same key for the same reason).
|
|
544
|
+
data[SaveSlotConstants.SYSTEM_STORE_KEY] = nil
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
return SaveSlotExportUtils.create(data, metadata.SlotName, metadata.Summary, metadata.TimePlayed)
|
|
548
|
+
end)
|
|
549
|
+
end)
|
|
550
|
+
end
|
|
551
|
+
|
|
552
|
+
--[=[
|
|
553
|
+
Imports an exported slot into a fresh slot, seeding the new slot's store with the exported data.
|
|
554
|
+
The destination defaults to the lowest free non-main index; passing `targetSlotIndex` puts the
|
|
555
|
+
import at that index instead, which must be free (delete or reset what is there first). Never uses
|
|
556
|
+
the main/default index either way -- importing onto the shared root store would wipe the player's
|
|
557
|
+
global data. Resolves to the new slot's id. Rejects a malformed export, a taken or main-slot
|
|
558
|
+
destination, or when no non-main index is free.
|
|
559
|
+
|
|
560
|
+
@param export SaveSlotExportUtils.SaveSlotExport
|
|
561
|
+
@param targetSlotIndex number? -- defaults to the lowest free non-main index
|
|
562
|
+
@return Promise<SlotId>
|
|
563
|
+
]=]
|
|
564
|
+
function HasSaveSlotsDataStore.PromiseImportSlot(
|
|
565
|
+
self: HasSaveSlotsDataStore,
|
|
566
|
+
export: SaveSlotExportUtils.SaveSlotExport,
|
|
567
|
+
targetSlotIndex: number?
|
|
568
|
+
): Promise.Promise<SaveSlotData.SlotId>
|
|
569
|
+
return (self._loadPromise :: any):Then(function()
|
|
570
|
+
if not SaveSlotExportUtils.isSaveSlotExport(export) then
|
|
571
|
+
return (Promise :: any).rejected("Bad save slot export")
|
|
572
|
+
end
|
|
573
|
+
|
|
574
|
+
-- Lowest free index strictly above the main slot: an imported slot must never occupy the
|
|
575
|
+
-- default index, whose store is the shared root datastore.
|
|
576
|
+
local usedIndices = {}
|
|
577
|
+
for existingSlotId, slot in self._slotMap do
|
|
578
|
+
if not self:_isEphemeral(existingSlotId) then
|
|
579
|
+
usedIndices[SaveSlotData.SlotIndex:Get(slot)] = true
|
|
580
|
+
end
|
|
581
|
+
end
|
|
582
|
+
|
|
583
|
+
local freeIndex: number
|
|
584
|
+
if targetSlotIndex then
|
|
585
|
+
if targetSlotIndex <= SaveSlotConstants.DEFAULT_SLOT_INDEX then
|
|
586
|
+
return (Promise :: any).rejected("Cannot import onto the main slot")
|
|
587
|
+
end
|
|
588
|
+
if usedIndices[targetSlotIndex] then
|
|
589
|
+
return (Promise :: any).rejected(`Slot {targetSlotIndex} already exists`)
|
|
590
|
+
end
|
|
591
|
+
|
|
592
|
+
freeIndex = targetSlotIndex
|
|
593
|
+
else
|
|
594
|
+
freeIndex = SaveSlotConstants.DEFAULT_SLOT_INDEX + 1
|
|
595
|
+
while usedIndices[freeIndex] do
|
|
596
|
+
freeIndex += 1
|
|
597
|
+
end
|
|
598
|
+
if freeIndex > self.MaxSlotCount.Value then
|
|
599
|
+
return (Promise :: any).rejected("No free non-main slot index available")
|
|
600
|
+
end
|
|
601
|
+
end
|
|
602
|
+
|
|
603
|
+
return self:PromiseCreateSlot(freeIndex, {
|
|
604
|
+
SlotName = export.slotName,
|
|
605
|
+
Summary = export.summary,
|
|
606
|
+
-- Absent on exports taken before playtime was carried; those import as before, with no playtime.
|
|
607
|
+
TimePlayed = export.timePlayed,
|
|
608
|
+
}):Then(function(newSlotId: SaveSlotData.SlotId)
|
|
609
|
+
-- freeIndex is always > DEFAULT_SLOT_INDEX, so this store is an isolated substore
|
|
610
|
+
-- (never the shared root); a plain Overwrite cannot touch system or global data.
|
|
611
|
+
self:_getSlotStore(newSlotId):Overwrite(export.data)
|
|
612
|
+
|
|
613
|
+
-- Flush so the imported slot survives a crash before the next autosave.
|
|
614
|
+
return self._dataStore:Save():Then(function()
|
|
615
|
+
return newSlotId
|
|
616
|
+
end)
|
|
617
|
+
end)
|
|
618
|
+
end)
|
|
619
|
+
end
|
|
620
|
+
|
|
621
|
+
--[=[
|
|
622
|
+
Exports a non-main slot (see [HasSaveSlotsDataStore.PromiseExportSlot]) and writes it to the shared
|
|
623
|
+
save slot store under the given key, tagged as a share code (see [SaveSlotExportUtils.Kind]) so the
|
|
624
|
+
teleport arrival path will not load it.
|
|
625
|
+
|
|
626
|
+
@param slotId SlotId
|
|
627
|
+
@param key string
|
|
628
|
+
@param allowMainSlot boolean? -- defaults to false, see [HasSaveSlotsDataStore.PromiseExportSlot]
|
|
629
|
+
@return Promise<boolean>
|
|
630
|
+
]=]
|
|
631
|
+
function HasSaveSlotsDataStore.PromiseSaveSlotToSharedDataStore(
|
|
632
|
+
self: HasSaveSlotsDataStore,
|
|
633
|
+
slotId: SaveSlotData.SlotId,
|
|
634
|
+
key: string,
|
|
635
|
+
allowMainSlot: boolean?
|
|
636
|
+
): Promise.Promise<boolean>
|
|
637
|
+
return self:PromiseExportSlot(slotId, allowMainSlot):Then(function(export)
|
|
638
|
+
return self._sharedSaveSlotDataStoreService:PromiseWrite(
|
|
639
|
+
key,
|
|
640
|
+
SaveSlotExportUtils.withKind(export, SaveSlotExportUtils.Kind.CODE),
|
|
641
|
+
self:_ownerUserIds()
|
|
642
|
+
)
|
|
643
|
+
end)
|
|
644
|
+
end
|
|
645
|
+
|
|
646
|
+
-- The owning user ids to tag a shared-store write with, so the entry is discoverable by the
|
|
647
|
+
-- data-erasure tooling that answers for the player whose save it holds. Empty offline, where the
|
|
648
|
+
-- identity is not injected.
|
|
649
|
+
function HasSaveSlotsDataStore._ownerUserIds(self: HasSaveSlotsDataStore): { number }?
|
|
650
|
+
return if self._userId then { self._userId } else nil
|
|
651
|
+
end
|
|
652
|
+
|
|
653
|
+
--[=[
|
|
654
|
+
Reads a share code's export from the shared save slot store and imports it into a fresh non-main
|
|
655
|
+
slot (see [HasSaveSlotsDataStore.PromiseImportSlot]). Rejects when no export is stored under the
|
|
656
|
+
key, or when the entry there is a transfer snapshot rather than a share code -- a transfer key is
|
|
657
|
+
replicated to every client (see the arrival path in
|
|
658
|
+
[HasSaveSlotsDataStore.PromiseSelectTransferableEphemeralSlot]), so redeeming one into a kept slot
|
|
659
|
+
would turn a public handle into a permanent copy of someone else's save.
|
|
660
|
+
|
|
661
|
+
@param key string
|
|
662
|
+
@return Promise<SlotId>
|
|
663
|
+
]=]
|
|
664
|
+
function HasSaveSlotsDataStore.PromiseImportSlotFromSharedDataStore(
|
|
665
|
+
self: HasSaveSlotsDataStore,
|
|
666
|
+
key: string
|
|
667
|
+
): Promise.Promise<SaveSlotData.SlotId>
|
|
668
|
+
-- Maid-owned like the other shared-store reads: this one is not even gated behind _loadPromise, so
|
|
669
|
+
-- nothing upstream rejects it when the player leaves mid-read.
|
|
670
|
+
return self._maid:GivePromise(self._sharedSaveSlotDataStoreService:PromiseRead(key)):Then(function(export)
|
|
671
|
+
if not self.Destroy then
|
|
672
|
+
return (Promise :: any).rejected() -- Destroyed. Empty, so it stays out of the logs
|
|
673
|
+
end
|
|
674
|
+
|
|
675
|
+
if export == nil then
|
|
676
|
+
return (Promise :: any).rejected(`No save slot stored under \{{key}\}`)
|
|
677
|
+
end
|
|
678
|
+
if
|
|
679
|
+
SaveSlotExportUtils.isSaveSlotExport(export)
|
|
680
|
+
and not SaveSlotExportUtils.canLoadAs(export, SaveSlotExportUtils.Kind.CODE)
|
|
681
|
+
then
|
|
682
|
+
return (Promise :: any).rejected(`\{{key}\} is not a share code`)
|
|
683
|
+
end
|
|
684
|
+
|
|
685
|
+
return self:PromiseImportSlot(export)
|
|
686
|
+
end)
|
|
687
|
+
end
|
|
688
|
+
|
|
689
|
+
--[=[
|
|
690
|
+
Resumes a transfer: loads the snapshot stored under the given shared-store key into a fresh
|
|
691
|
+
ephemeral slot and selects it, keeping the key so the next teleport carries the slot forward again
|
|
692
|
+
(see the transferable-ephemeral teleport provider in [SaveSlotService]). Like every ephemeral slot it
|
|
693
|
+
is never persisted, stays out of the slot list, and is torn down on deselect.
|
|
694
|
+
|
|
695
|
+
:::warning
|
|
696
|
+
The key comes from the arriving client's teleport band, so this loads whatever a client asks for.
|
|
697
|
+
Only entries written as transfers are accepted -- a share code, which players hand to each other, is
|
|
698
|
+
refused here and redeemed through [HasSaveSlotsDataStore.PromiseImportEphemeralSaveSlotFromCode]
|
|
699
|
+
instead. Entries predating that tag are refused too; see [SaveSlotExportUtils.canLoadAs].
|
|
700
|
+
:::
|
|
701
|
+
|
|
702
|
+
@param key string
|
|
703
|
+
@return Promise<SlotId>
|
|
704
|
+
]=]
|
|
705
|
+
function HasSaveSlotsDataStore.PromiseSelectTransferableEphemeralSlot(
|
|
706
|
+
self: HasSaveSlotsDataStore,
|
|
707
|
+
key: string
|
|
708
|
+
): Promise.Promise<SaveSlotData.SlotId>
|
|
709
|
+
return self:_promiseSelectEphemeralFromSharedStore(key, SaveSlotExportUtils.Kind.TRANSFER, key)
|
|
710
|
+
end
|
|
711
|
+
|
|
712
|
+
--[=[
|
|
713
|
+
Redeems a share code into a fresh transferable ephemeral slot and selects it (see
|
|
714
|
+
[HasSaveSlotsDataStore.PromiseSelectTransferableEphemeralSlot] for what that slot is).
|
|
715
|
+
|
|
716
|
+
The slot transfers onward under a freshly minted key rather than the code it came from: the transfer
|
|
717
|
+
key is handed to the client to carry across teleports, so reusing the code as that key would publish
|
|
718
|
+
the code and hand every holder of it a slot the arrival path would load. The code entry is left
|
|
719
|
+
untouched, so the same code still redeems for whoever else holds it.
|
|
720
|
+
|
|
721
|
+
@param code string
|
|
722
|
+
@return Promise<SlotId>
|
|
723
|
+
]=]
|
|
724
|
+
function HasSaveSlotsDataStore.PromiseImportEphemeralSaveSlotFromCode(
|
|
725
|
+
self: HasSaveSlotsDataStore,
|
|
726
|
+
code: string
|
|
727
|
+
): Promise.Promise<SaveSlotData.SlotId>
|
|
728
|
+
return self:_promiseSelectEphemeralFromSharedStore(
|
|
729
|
+
code,
|
|
730
|
+
SaveSlotExportUtils.Kind.CODE,
|
|
731
|
+
HttpService:GenerateGUID(false)
|
|
732
|
+
)
|
|
733
|
+
end
|
|
734
|
+
|
|
735
|
+
-- Shared body of the two ways into a transferable ephemeral slot: read `key`, accept it only as
|
|
736
|
+
-- `kind`, and select the resulting slot as one that transfers onward under `transferKey`.
|
|
737
|
+
function HasSaveSlotsDataStore._promiseSelectEphemeralFromSharedStore(
|
|
738
|
+
self: HasSaveSlotsDataStore,
|
|
739
|
+
key: string,
|
|
740
|
+
kind: SaveSlotExportUtils.SaveSlotExportKind,
|
|
741
|
+
transferKey: string
|
|
742
|
+
): Promise.Promise<SaveSlotData.SlotId>
|
|
743
|
+
return (self._loadPromise :: any):Then(function()
|
|
744
|
+
-- Maid-owned for the same reason as _promiseLoadSlots: this read runs on the join path (a teleport
|
|
745
|
+
-- arriving with an ephemeral key) and outlives a leave, and its continuation builds a slot.
|
|
746
|
+
return self._maid:GivePromise(self._sharedSaveSlotDataStoreService:PromiseRead(key)):Then(function(export)
|
|
747
|
+
if not self.Destroy then
|
|
748
|
+
-- Destroyed. Rejecting keeps the Promise<SlotId> contract honest -- a nil fulfilment reads as
|
|
749
|
+
-- "imported, no slot" to a caller like SaveSlotCmdrService. Empty, so it stays out of the logs.
|
|
750
|
+
return (Promise :: any).rejected()
|
|
751
|
+
end
|
|
752
|
+
|
|
753
|
+
if export == nil then
|
|
754
|
+
return (Promise :: any).rejected(`No save slot stored under \{{key}\}`)
|
|
755
|
+
end
|
|
756
|
+
if not SaveSlotExportUtils.isSaveSlotExport(export) then
|
|
757
|
+
return (Promise :: any).rejected("Bad save slot export in shared store")
|
|
758
|
+
end
|
|
759
|
+
if not SaveSlotExportUtils.canLoadAs(export, kind) then
|
|
760
|
+
return (Promise :: any).rejected(`\{{key}\} is not a save slot {kind}`)
|
|
761
|
+
end
|
|
762
|
+
|
|
763
|
+
local slotId = HttpService:GenerateGUID(false)
|
|
764
|
+
self:_buildSlot(slotId, {
|
|
765
|
+
SlotId = slotId,
|
|
766
|
+
SlotIndex = SaveSlotConstants.EPHEMERAL_SLOT_INDEX,
|
|
767
|
+
SlotName = export.slotName or "Transfer",
|
|
768
|
+
CreatedTime = os.time(),
|
|
769
|
+
Summary = export.summary,
|
|
770
|
+
}, true, true)
|
|
771
|
+
|
|
772
|
+
-- Seed the in-memory store before selecting so a consumer of ObserveActiveSlotStoreBrio never
|
|
773
|
+
-- observes an empty active slot; then remember the key this slot transfers under.
|
|
774
|
+
self:_getSlotStore(slotId):Overwrite(export.data)
|
|
775
|
+
self._transferableEphemeralKeys[slotId] = transferKey
|
|
776
|
+
|
|
777
|
+
return self:PromiseSelectSlot(slotId):Then(function()
|
|
778
|
+
return slotId
|
|
779
|
+
end)
|
|
780
|
+
end)
|
|
781
|
+
end)
|
|
782
|
+
end
|
|
783
|
+
|
|
784
|
+
--[=[
|
|
785
|
+
Builds this player's teleport slice for a transferable ephemeral slot: re-saves the active slot's
|
|
786
|
+
*current live* data to the shared store under its key and returns a slice carrying that key. Resolves
|
|
787
|
+
nil when the active slot is not a transferable ephemeral slot. A failed re-save degrades to nil so a
|
|
788
|
+
teleport is never blocked (the destination then re-loads the last saved state). Asynchronous -- it is
|
|
789
|
+
consumed through [TeleportDataService.PromiseBuildTeleportData].
|
|
790
|
+
|
|
791
|
+
@return Promise<{ [string]: any }?>
|
|
792
|
+
]=]
|
|
793
|
+
function HasSaveSlotsDataStore.PromiseBuildEphemeralTransferSlice(
|
|
794
|
+
self: HasSaveSlotsDataStore
|
|
795
|
+
): Promise.Promise<{ [string]: any }?>
|
|
796
|
+
return (self._loadPromise :: any):Then(function(): any
|
|
797
|
+
local slotId = self.ActiveSlotId.Value
|
|
798
|
+
if not slotId then
|
|
799
|
+
return nil
|
|
800
|
+
end
|
|
801
|
+
local storeKey = self._transferableEphemeralKeys[slotId]
|
|
802
|
+
if not storeKey then
|
|
803
|
+
return nil
|
|
804
|
+
end
|
|
805
|
+
|
|
806
|
+
return self:PromiseExportSlot(slotId)
|
|
807
|
+
:Then(function(export)
|
|
808
|
+
-- Tagged as a transfer, which is what makes it loadable from the key the client carries --
|
|
809
|
+
-- and what keeps a share code, written under its own key as a code, out of that path.
|
|
810
|
+
return self._sharedSaveSlotDataStoreService:PromiseWrite(
|
|
811
|
+
storeKey,
|
|
812
|
+
SaveSlotExportUtils.withKind(export, SaveSlotExportUtils.Kind.TRANSFER),
|
|
813
|
+
self:_ownerUserIds()
|
|
814
|
+
)
|
|
815
|
+
end)
|
|
816
|
+
:Then(function()
|
|
817
|
+
return { [SaveSlotConstants.TELEPORT_DATA_EPHEMERAL_KEY] = storeKey }
|
|
818
|
+
end)
|
|
819
|
+
:Catch(function()
|
|
820
|
+
return nil
|
|
821
|
+
end)
|
|
822
|
+
end)
|
|
823
|
+
end
|
|
824
|
+
|
|
825
|
+
--[=[
|
|
826
|
+
Overrides the share-code generator for this player's exports (see [SaveSlotCodeUtils.CodeGenerator]).
|
|
827
|
+
Games inject a custom format; the default is [SaveSlotCodeUtils.generateDefaultCode]. Usually set
|
|
828
|
+
game-wide via [SaveSlotService.SetCodeGenerator] rather than per player.
|
|
829
|
+
|
|
830
|
+
@param generator CodeGenerator
|
|
831
|
+
]=]
|
|
832
|
+
function HasSaveSlotsDataStore.SetCodeGenerator(
|
|
833
|
+
self: HasSaveSlotsDataStore,
|
|
834
|
+
generator: SaveSlotCodeUtils.CodeGenerator
|
|
835
|
+
): ()
|
|
836
|
+
assert(type(generator) == "function", "Bad generator")
|
|
837
|
+
self._codeGenerator = generator
|
|
838
|
+
end
|
|
839
|
+
|
|
840
|
+
-- Builds a code for a slot from the configured generator, taking the owner's identity from the
|
|
841
|
+
-- injected options (absent offline, where there may be no name to resolve) and the slot's
|
|
842
|
+
-- name/index from its metadata.
|
|
843
|
+
function HasSaveSlotsDataStore._generateCode(self: HasSaveSlotsDataStore, slotId: SaveSlotData.SlotId): string
|
|
844
|
+
local slot = self._slotMap[slotId]
|
|
845
|
+
local metadata = if slot then SaveSlotData:Get(slot) else nil
|
|
846
|
+
|
|
847
|
+
return self._codeGenerator({
|
|
848
|
+
userId = self._userId,
|
|
849
|
+
userName = self._userName,
|
|
850
|
+
slotName = metadata and metadata.SlotName,
|
|
851
|
+
slotIndex = metadata and metadata.SlotIndex,
|
|
852
|
+
})
|
|
853
|
+
end
|
|
854
|
+
|
|
855
|
+
--[=[
|
|
856
|
+
Exports a slot to the shared store under a fresh generated code and resolves to that code. The code
|
|
857
|
+
is a shareable handle other sessions load with
|
|
858
|
+
[HasSaveSlotsDataStore.PromiseImportEphemeralSaveSlotFromCode]. Defaults to the active slot. Refuses
|
|
859
|
+
the main slot unless `allowMainSlot` is set (see [HasSaveSlotsDataStore.PromiseExportSlot]). The code
|
|
860
|
+
format comes from the configured generator (see [HasSaveSlotsDataStore.SetCodeGenerator]).
|
|
861
|
+
|
|
862
|
+
@param slotId SlotId? -- defaults to the active slot
|
|
863
|
+
@param allowMainSlot boolean? -- defaults to false, see [HasSaveSlotsDataStore.PromiseExportSlot]
|
|
864
|
+
@return Promise<string>
|
|
865
|
+
]=]
|
|
866
|
+
function HasSaveSlotsDataStore.PromiseExportSaveSlotToCode(
|
|
867
|
+
self: HasSaveSlotsDataStore,
|
|
868
|
+
slotId: SaveSlotData.SlotId?,
|
|
869
|
+
allowMainSlot: boolean?
|
|
870
|
+
): Promise.Promise<string>
|
|
871
|
+
return (self._loadPromise :: any):Then(function()
|
|
872
|
+
local targetSlotId = slotId or self.ActiveSlotId.Value
|
|
873
|
+
if not targetSlotId then
|
|
874
|
+
return (Promise :: any).rejected("No slot to export")
|
|
875
|
+
end
|
|
876
|
+
|
|
877
|
+
local code = self:_generateCode(targetSlotId)
|
|
878
|
+
return self:PromiseSaveSlotToSharedDataStore(targetSlotId, code, allowMainSlot):Then(function()
|
|
879
|
+
return code
|
|
880
|
+
end)
|
|
881
|
+
end)
|
|
882
|
+
end
|
|
883
|
+
|
|
884
|
+
--[=[
|
|
885
|
+
Exports a slot as a raw JSON string (no shared store), for direct inspection or attaching to a bug
|
|
886
|
+
report. Defaults to the active slot. Refuses the main slot unless `allowMainSlot` is set (see
|
|
887
|
+
[HasSaveSlotsDataStore.PromiseExportSlot]).
|
|
888
|
+
|
|
889
|
+
@param slotId SlotId? -- defaults to the active slot
|
|
890
|
+
@param allowMainSlot boolean? -- defaults to false, see [HasSaveSlotsDataStore.PromiseExportSlot]
|
|
891
|
+
@return Promise<string>
|
|
892
|
+
]=]
|
|
893
|
+
function HasSaveSlotsDataStore.PromiseExportSaveSlotToJson(
|
|
894
|
+
self: HasSaveSlotsDataStore,
|
|
895
|
+
slotId: SaveSlotData.SlotId?,
|
|
896
|
+
allowMainSlot: boolean?
|
|
897
|
+
): Promise.Promise<string>
|
|
898
|
+
return (self._loadPromise :: any):Then(function()
|
|
899
|
+
local targetSlotId = slotId or self.ActiveSlotId.Value
|
|
900
|
+
if not targetSlotId then
|
|
901
|
+
return (Promise :: any).rejected("No slot to export")
|
|
902
|
+
end
|
|
903
|
+
|
|
904
|
+
return self:PromiseExportSlot(targetSlotId, allowMainSlot):Then(function(export)
|
|
905
|
+
return HttpService:JSONEncode(export)
|
|
906
|
+
end)
|
|
907
|
+
end)
|
|
908
|
+
end
|
|
909
|
+
|
|
910
|
+
--[=[
|
|
911
|
+
Duplicates the slot with the given ID into a new slot at the lowest free index,
|
|
912
|
+
copying its saved data and accrued playtime. Resolves to the new slot's id. The copy is not
|
|
913
|
+
selected, its timestamps and session counters start fresh, and its name is suffixed with
|
|
914
|
+
" (Copy)". Rejects when the source slot is missing or every index is in use.
|
|
915
|
+
|
|
916
|
+
An ephemeral slot may be duplicated: the copy is a real, persisted slot seeded with the ephemeral
|
|
917
|
+
slot's live in-memory data, which is how a throwaway session is turned into a save. It keeps the
|
|
918
|
+
source's name unsuffixed (the copy is the first real slot for that session, not a second copy of an
|
|
919
|
+
existing save) and, like any duplicate, is not selected -- see
|
|
920
|
+
[HasSaveSlotsDataStore.PromisePersistEphemeralSlot] for the version that continues play on the new slot.
|
|
921
|
+
]=]
|
|
922
|
+
function HasSaveSlotsDataStore.PromiseDuplicateSlot(
|
|
923
|
+
self: HasSaveSlotsDataStore,
|
|
924
|
+
slotId: SaveSlotData.SlotId
|
|
925
|
+
): Promise.Promise<SaveSlotData.SlotId>
|
|
926
|
+
return (self._loadPromise :: any):Then(function()
|
|
927
|
+
local sourceSlot = self._slotMap[slotId]
|
|
928
|
+
if not sourceSlot then
|
|
929
|
+
return (Promise :: any).rejected(`Slot \{{slotId}\} not found`)
|
|
930
|
+
end
|
|
931
|
+
|
|
932
|
+
local sourceIsEphemeral = self:_isEphemeral(slotId)
|
|
933
|
+
|
|
934
|
+
-- The source is usually the live active slot, whose current session has not landed in TimePlayed
|
|
935
|
+
-- yet; fold it in first so the copy carries the playtime the player is actually looking at.
|
|
936
|
+
self:_flushPlaytime()
|
|
937
|
+
|
|
938
|
+
-- Lowest free positive index, filling gaps left by deletions (mirrors PromiseSelectNewSaveSlot).
|
|
939
|
+
local usedIndices = {}
|
|
940
|
+
for existingSlotId, slot in self._slotMap do
|
|
941
|
+
if not self:_isEphemeral(existingSlotId) then
|
|
942
|
+
usedIndices[SaveSlotData.SlotIndex:Get(slot)] = true
|
|
943
|
+
end
|
|
944
|
+
end
|
|
945
|
+
|
|
946
|
+
local freeIndex = 1
|
|
947
|
+
while usedIndices[freeIndex] do
|
|
948
|
+
freeIndex += 1
|
|
949
|
+
end
|
|
950
|
+
if freeIndex > self.MaxSlotCount.Value then
|
|
951
|
+
return (Promise :: any).rejected("All slots are already in use")
|
|
952
|
+
end
|
|
953
|
+
|
|
954
|
+
local sourceMetadata = SaveSlotData:Get(sourceSlot)
|
|
955
|
+
|
|
956
|
+
-- Read the source's saved data before creating the copy so a read failure leaves no orphan slot.
|
|
957
|
+
return self:_getSlotStore(slotId):LoadAll({}):Then(function(sourceData)
|
|
958
|
+
-- The default slot shares the player's root store with the SaveSlots system data. Never carry
|
|
959
|
+
-- that system key across into the copy's saved data.
|
|
960
|
+
local slotData = if type(sourceData) == "table" then table.clone(sourceData) else {}
|
|
961
|
+
slotData[SaveSlotConstants.SYSTEM_STORE_KEY] = nil
|
|
962
|
+
|
|
963
|
+
return self
|
|
964
|
+
:PromiseCreateSlot(freeIndex, {
|
|
965
|
+
SlotName = if sourceIsEphemeral
|
|
966
|
+
then sourceMetadata.SlotName
|
|
967
|
+
else `{sourceMetadata.SlotName} (Copy)`,
|
|
968
|
+
Summary = sourceMetadata.Summary,
|
|
969
|
+
-- Playtime describes the progress being copied, not the slot it happens to live in, so
|
|
970
|
+
-- it travels with that progress rather than resetting the copy to a never-played slot.
|
|
971
|
+
TimePlayed = sourceMetadata.TimePlayed,
|
|
972
|
+
})
|
|
973
|
+
:Then(function(newSlotId: SaveSlotData.SlotId)
|
|
974
|
+
local destStore = self:_getSlotStore(newSlotId)
|
|
975
|
+
|
|
976
|
+
if destStore == self._dataStore then
|
|
977
|
+
-- The copy is the default slot, whose store is the shared root. Merge so the SaveSlots
|
|
978
|
+
-- system store living alongside it survives (a plain Overwrite would wipe it).
|
|
979
|
+
destStore:OverwriteMerge(slotData)
|
|
980
|
+
else
|
|
981
|
+
destStore:Overwrite(slotData)
|
|
982
|
+
end
|
|
983
|
+
|
|
984
|
+
-- Flush so the duplicated data survives a crash before the next autosave.
|
|
985
|
+
return self._dataStore:Save():Then(function()
|
|
986
|
+
return newSlotId
|
|
987
|
+
end)
|
|
988
|
+
end)
|
|
989
|
+
end)
|
|
990
|
+
end)
|
|
991
|
+
end
|
|
992
|
+
|
|
993
|
+
--[=[
|
|
994
|
+
Turns an ephemeral slot into a real save: duplicates it into a fresh persisted slot at the lowest free
|
|
995
|
+
index (see [HasSaveSlotsDataStore.PromiseDuplicateSlot]) and selects that slot, so play continues on
|
|
996
|
+
data that is now being written. Defaults to the active slot, which in practice is the only ephemeral
|
|
997
|
+
slot there is -- one stops existing the moment it stops being active. Resolves to the new slot's id.
|
|
998
|
+
|
|
999
|
+
The copy is taken from the ephemeral slot's live in-memory data at the moment this runs, and selecting
|
|
1000
|
+
the new slot retires the ephemeral one, so anything written to the old store between the copy and the
|
|
1001
|
+
selection is dropped. Rejects when no slot is active, the slot is missing, the slot is not ephemeral,
|
|
1002
|
+
or every index is in use.
|
|
1003
|
+
|
|
1004
|
+
@param slotId SlotId? -- defaults to the active slot
|
|
1005
|
+
@return Promise<SlotId>
|
|
1006
|
+
]=]
|
|
1007
|
+
function HasSaveSlotsDataStore.PromisePersistEphemeralSlot(
|
|
1008
|
+
self: HasSaveSlotsDataStore,
|
|
1009
|
+
slotId: SaveSlotData.SlotId?
|
|
1010
|
+
): Promise.Promise<SaveSlotData.SlotId>
|
|
1011
|
+
return (self._loadPromise :: any):Then(function()
|
|
1012
|
+
local targetSlotId = slotId or self.ActiveSlotId.Value
|
|
1013
|
+
if not targetSlotId then
|
|
1014
|
+
return (Promise :: any).rejected("No slot to persist")
|
|
1015
|
+
end
|
|
1016
|
+
|
|
1017
|
+
if not self._slotMap[targetSlotId] then
|
|
1018
|
+
return (Promise :: any).rejected(`Slot \{{targetSlotId}\} not found`)
|
|
1019
|
+
end
|
|
1020
|
+
|
|
1021
|
+
if not self:_isEphemeral(targetSlotId) then
|
|
1022
|
+
return (Promise :: any).rejected(`Slot \{{targetSlotId}\} is already persisted`)
|
|
1023
|
+
end
|
|
1024
|
+
|
|
1025
|
+
return self:PromiseDuplicateSlot(targetSlotId):Then(function(newSlotId: SaveSlotData.SlotId)
|
|
1026
|
+
return self:PromiseSelectSlot(newSlotId):Then(function()
|
|
1027
|
+
return newSlotId
|
|
1028
|
+
end)
|
|
1029
|
+
end)
|
|
1030
|
+
end)
|
|
1031
|
+
end
|
|
1032
|
+
|
|
1033
|
+
--[=[
|
|
1034
|
+
Deletes the slot with the given ID. Deleting an ephemeral slot ends that session -- it is deselected
|
|
1035
|
+
and retired along with its in-memory store -- rather than being refused for being the active slot.
|
|
1036
|
+
]=]
|
|
1037
|
+
function HasSaveSlotsDataStore.PromiseDeleteSlot(
|
|
1038
|
+
self: HasSaveSlotsDataStore,
|
|
1039
|
+
slotId: SaveSlotData.SlotId
|
|
1040
|
+
): Promise.Promise<any>
|
|
1041
|
+
return (self._loadPromise :: any):Then(function()
|
|
1042
|
+
-- An ephemeral slot only exists while it is active, so deleting one is retiring it. Nothing was
|
|
1043
|
+
-- ever persisted, so there is no stored data to wipe afterwards -- hence the early return.
|
|
1044
|
+
if self:_isEphemeral(slotId) then
|
|
1045
|
+
if slotId == self.ActiveSlotId.Value then
|
|
1046
|
+
return self:PromiseDeselectSlot()
|
|
1047
|
+
end
|
|
1048
|
+
self:_destroyEphemeralSlot(slotId)
|
|
1049
|
+
return (Promise :: any).resolved()
|
|
1050
|
+
end
|
|
1051
|
+
|
|
1052
|
+
if slotId == self.ActiveSlotId.Value then
|
|
1053
|
+
return (Promise :: any).rejected("Cannot delete active slot")
|
|
1054
|
+
end
|
|
1055
|
+
|
|
1056
|
+
local slot = self._slotMap[slotId]
|
|
1057
|
+
if not slot then
|
|
1058
|
+
return (Promise :: any).rejected(`Slot \{{slotId}\} not found`)
|
|
1059
|
+
end
|
|
1060
|
+
|
|
1061
|
+
self._maid[slotId] = nil
|
|
1062
|
+
|
|
1063
|
+
-- The continue pointer must not outlive its slot. When the deleted slot is the one the player
|
|
1064
|
+
-- would resume on (e.g. the just-deselected active slot, or a never-reselected last-active),
|
|
1065
|
+
-- clear the last-active memory so "Continue" stops offering a slot that no longer exists.
|
|
1066
|
+
if slotId == self._lastActiveSlotId then
|
|
1067
|
+
self._lastActiveSlotId = nil
|
|
1068
|
+
self.LastActiveSlotId.Value = nil
|
|
1069
|
+
end
|
|
1070
|
+
|
|
1071
|
+
-- Wipe default slot
|
|
1072
|
+
local slotIndex = SaveSlotData.SlotIndex:Get(slot)
|
|
1073
|
+
local deletePromise = nil
|
|
1074
|
+
|
|
1075
|
+
if slotIndex == SaveSlotConstants.DEFAULT_SLOT_INDEX then
|
|
1076
|
+
deletePromise = self._dataStore:PromiseKeyList():Then(function(keys)
|
|
1077
|
+
for _, key in keys do
|
|
1078
|
+
if key ~= SaveSlotConstants.SYSTEM_STORE_KEY then
|
|
1079
|
+
self._dataStore:Delete(key)
|
|
1080
|
+
end
|
|
1081
|
+
end
|
|
1082
|
+
self._metadataStore:Delete(slotId)
|
|
1083
|
+
end)
|
|
1084
|
+
else
|
|
1085
|
+
-- Or delete slot from substore
|
|
1086
|
+
self._systemStore:GetSubStore(SaveSlotConstants.SLOT_STORE_KEY):Delete(slotId)
|
|
1087
|
+
self._metadataStore:Delete(slotId)
|
|
1088
|
+
deletePromise = (Promise :: any).resolved()
|
|
1089
|
+
end
|
|
1090
|
+
|
|
1091
|
+
-- Flush the deletion to prevent stale reads
|
|
1092
|
+
return deletePromise:Then(function()
|
|
1093
|
+
return self._dataStore:Save()
|
|
1094
|
+
end)
|
|
1095
|
+
end)
|
|
1096
|
+
end
|
|
1097
|
+
|
|
1098
|
+
--[=[
|
|
1099
|
+
Deletes every slot for the player and clears the active/last-active
|
|
1100
|
+
selection, resetting the player to a fresh state. Resolves once all slots
|
|
1101
|
+
are gone.
|
|
1102
|
+
]=]
|
|
1103
|
+
function HasSaveSlotsDataStore.PromiseDeleteAllSlots(self: HasSaveSlotsDataStore): Promise.Promise<any>
|
|
1104
|
+
return (self._loadPromise :: any):Then(function()
|
|
1105
|
+
-- Clear the selection first so the previously active slot is deletable
|
|
1106
|
+
self.ActiveSlotId.Value = nil
|
|
1107
|
+
self._lastActiveSlotId = nil
|
|
1108
|
+
self.LastActiveSlotId.Value = nil
|
|
1109
|
+
|
|
1110
|
+
local slotIds = {}
|
|
1111
|
+
for slotId in self._slotMap do
|
|
1112
|
+
table.insert(slotIds, slotId)
|
|
1113
|
+
end
|
|
1114
|
+
|
|
1115
|
+
-- Delete sequentially to avoid concurrent datastore saves
|
|
1116
|
+
local promise = (Promise :: any).resolved()
|
|
1117
|
+
for _, slotId in slotIds do
|
|
1118
|
+
promise = promise:Then(function()
|
|
1119
|
+
return self:PromiseDeleteSlot(slotId)
|
|
1120
|
+
end)
|
|
1121
|
+
end
|
|
1122
|
+
return promise
|
|
1123
|
+
end)
|
|
1124
|
+
end
|
|
1125
|
+
|
|
1126
|
+
--[=[
|
|
1127
|
+
Resets the slot with the given id to a fresh empty one -- equivalent to deleting the slot
|
|
1128
|
+
and creating a new one at the same index. The slot keeps its index and name; its saved data
|
|
1129
|
+
and metadata (timestamps) start fresh. Resolves to the new slot id.
|
|
1130
|
+
|
|
1131
|
+
When the reset slot is the active slot, the selection clears and then reselects the fresh
|
|
1132
|
+
slot: everything bound to [HasSaveSlotsDataStore.ObserveActiveSlotStoreBrio] tears down as the
|
|
1133
|
+
selection clears and rebuilds against the empty store on reselect, so consumers reset
|
|
1134
|
+
reactively without wiping their own state. A non-active slot is left unselected, and its
|
|
1135
|
+
"Continue" pointer (when it was the last-active slot) is carried across to the fresh id so
|
|
1136
|
+
the reset slot stays resumable. Rejects when the slot is missing.
|
|
1137
|
+
]=]
|
|
1138
|
+
function HasSaveSlotsDataStore.PromiseResetSlot(
|
|
1139
|
+
self: HasSaveSlotsDataStore,
|
|
1140
|
+
slotId: SaveSlotData.SlotId
|
|
1141
|
+
): Promise.Promise<SaveSlotData.SlotId>
|
|
1142
|
+
return (self._loadPromise :: any):Then(function(): any
|
|
1143
|
+
local slot = self._slotMap[slotId]
|
|
1144
|
+
if not slot then
|
|
1145
|
+
return (Promise :: any).rejected(`Slot \{{slotId}\} not found`)
|
|
1146
|
+
end
|
|
1147
|
+
|
|
1148
|
+
if self:_isEphemeral(slotId) then
|
|
1149
|
+
return (Promise :: any).rejected("Cannot reset an ephemeral slot")
|
|
1150
|
+
end
|
|
1151
|
+
|
|
1152
|
+
local metadata = SaveSlotData:Get(slot)
|
|
1153
|
+
local wasActive = (slotId == self.ActiveSlotId.Value)
|
|
1154
|
+
-- PromiseDeleteSlot clears the continue pointer when it targets the last-active slot; capture it
|
|
1155
|
+
-- now so the non-active path can move it onto the fresh slot id below.
|
|
1156
|
+
local wasLastActive = (slotId == self._lastActiveSlotId)
|
|
1157
|
+
|
|
1158
|
+
if wasActive then
|
|
1159
|
+
-- Clear the selection so the slot is deletable, skipping the deselect flush that would
|
|
1160
|
+
-- persist progress we are about to delete.
|
|
1161
|
+
self.ActiveSlotId.Value = nil
|
|
1162
|
+
end
|
|
1163
|
+
|
|
1164
|
+
return self:PromiseDeleteSlot(slotId)
|
|
1165
|
+
:Then(function()
|
|
1166
|
+
return self:PromiseCreateSlot(metadata.SlotIndex, { SlotName = metadata.SlotName })
|
|
1167
|
+
end)
|
|
1168
|
+
:Then(function(newSlotId: SaveSlotData.SlotId)
|
|
1169
|
+
if wasActive then
|
|
1170
|
+
-- Reselecting restores the continue pointer via the ActiveSlotId hook.
|
|
1171
|
+
return self:PromiseSelectSlot(newSlotId):Then(function()
|
|
1172
|
+
return newSlotId
|
|
1173
|
+
end)
|
|
1174
|
+
end
|
|
1175
|
+
|
|
1176
|
+
-- Non-active reset never reselects, so carry the resume pointer onto the fresh id
|
|
1177
|
+
-- ourselves when this slot was the one "Continue" would resume.
|
|
1178
|
+
if wasLastActive then
|
|
1179
|
+
self._lastActiveSlotId = newSlotId
|
|
1180
|
+
self.LastActiveSlotId.Value = newSlotId
|
|
1181
|
+
end
|
|
1182
|
+
|
|
1183
|
+
return newSlotId
|
|
1184
|
+
end)
|
|
1185
|
+
end)
|
|
1186
|
+
end
|
|
1187
|
+
|
|
1188
|
+
--[=[
|
|
1189
|
+
Resets the active slot to a fresh empty one -- see [HasSaveSlotsDataStore.PromiseResetSlot]. The slot
|
|
1190
|
+
keeps its index and name; its saved data and metadata (timestamps) start fresh, and the fresh
|
|
1191
|
+
slot stays selected. Resolves to the new slot id, or nil when no slot is active.
|
|
1192
|
+
]=]
|
|
1193
|
+
function HasSaveSlotsDataStore.PromiseResetActiveSlot(
|
|
1194
|
+
self: HasSaveSlotsDataStore
|
|
1195
|
+
): Promise.Promise<SaveSlotData.SlotId?>
|
|
1196
|
+
return (self._loadPromise :: any):Then(function(): any
|
|
1197
|
+
local slotId = self.ActiveSlotId.Value
|
|
1198
|
+
if not slotId then
|
|
1199
|
+
return nil -- Nothing selected to reset
|
|
1200
|
+
end
|
|
1201
|
+
|
|
1202
|
+
return self:PromiseResetSlot(slotId)
|
|
1203
|
+
end)
|
|
1204
|
+
end
|
|
1205
|
+
|
|
1206
|
+
--[=[
|
|
1207
|
+
Sets the metadata for the slot with the given ID
|
|
1208
|
+
]=]
|
|
1209
|
+
function HasSaveSlotsDataStore.PromiseSetSlotMetadata(
|
|
1210
|
+
self: HasSaveSlotsDataStore,
|
|
1211
|
+
slotId: SaveSlotData.SlotId,
|
|
1212
|
+
data: SaveSlotData.SaveSlotMetadata
|
|
1213
|
+
): Promise.Promise<any>
|
|
1214
|
+
if data.SlotId and (data.SlotId ~= slotId) then
|
|
1215
|
+
return (Promise :: any).rejected("SlotId is locked")
|
|
1216
|
+
end
|
|
1217
|
+
|
|
1218
|
+
return (self._loadPromise :: any):Then(function()
|
|
1219
|
+
local slot = self._slotMap[slotId]
|
|
1220
|
+
|
|
1221
|
+
-- Routing depends on immutable indices to distinguish the default slot
|
|
1222
|
+
if data.SlotIndex and (data.SlotIndex ~= SaveSlotData.SlotIndex:Get(slot)) then
|
|
1223
|
+
return (Promise :: any).rejected("SlotIndex is locked")
|
|
1224
|
+
end
|
|
1225
|
+
|
|
1226
|
+
SaveSlotData:Set(slot, data)
|
|
1227
|
+
end)
|
|
1228
|
+
end
|
|
1229
|
+
|
|
1230
|
+
--[=[
|
|
1231
|
+
Gets the metadata for the slot with the given ID
|
|
1232
|
+
]=]
|
|
1233
|
+
function HasSaveSlotsDataStore.PromiseGetSlotMetadata(
|
|
1234
|
+
self: HasSaveSlotsDataStore,
|
|
1235
|
+
slotId: SaveSlotData.SlotId
|
|
1236
|
+
): Promise.Promise<SaveSlotData.SaveSlotMetadata?>
|
|
1237
|
+
return (self._loadPromise :: any):Then(function()
|
|
1238
|
+
local slot = self._slotMap[slotId]
|
|
1239
|
+
return (Promise :: any).resolved(slot and SaveSlotData:Get(slot))
|
|
1240
|
+
end)
|
|
1241
|
+
end
|
|
1242
|
+
|
|
1243
|
+
--[=[
|
|
1244
|
+
Returns the slot ID from the given index
|
|
1245
|
+
]=]
|
|
1246
|
+
function HasSaveSlotsDataStore.PromiseSlotIdFromIndex(
|
|
1247
|
+
self: HasSaveSlotsDataStore,
|
|
1248
|
+
slotIndex: number
|
|
1249
|
+
): Promise.Promise<SaveSlotData.SlotId?>
|
|
1250
|
+
return (self._loadPromise :: any):Then(function()
|
|
1251
|
+
for slotId, slot in self._slotMap do
|
|
1252
|
+
if self:_isEphemeral(slotId) then
|
|
1253
|
+
continue -- ephemeral slots are never addressable by index
|
|
1254
|
+
end
|
|
1255
|
+
if slotIndex == SaveSlotData.SlotIndex:Get(slot) then
|
|
1256
|
+
return (Promise :: any).resolved(slotId)
|
|
1257
|
+
end
|
|
1258
|
+
end
|
|
1259
|
+
return (Promise :: any).resolved(nil)
|
|
1260
|
+
end)
|
|
1261
|
+
end
|
|
1262
|
+
|
|
1263
|
+
--[=[
|
|
1264
|
+
Gets the last active slot ID
|
|
1265
|
+
]=]
|
|
1266
|
+
function HasSaveSlotsDataStore.PromiseLastActiveSlotId(
|
|
1267
|
+
self: HasSaveSlotsDataStore
|
|
1268
|
+
): Promise.Promise<SaveSlotData.SlotId?>
|
|
1269
|
+
return (self._loadPromise :: any):Then(function()
|
|
1270
|
+
return self.ActiveSlotId.Value or self._lastActiveSlotId
|
|
1271
|
+
end)
|
|
1272
|
+
end
|
|
1273
|
+
|
|
1274
|
+
--[=[
|
|
1275
|
+
Selects the player's last active slot if one still exists, resolving to the
|
|
1276
|
+
selected slot id -- or nil when there is no slot to continue on. Backs a
|
|
1277
|
+
"Continue" affordance that every save-slot consumer tends to need.
|
|
1278
|
+
]=]
|
|
1279
|
+
function HasSaveSlotsDataStore.PromiseSelectLastSaveSlot(
|
|
1280
|
+
self: HasSaveSlotsDataStore
|
|
1281
|
+
): Promise.Promise<SaveSlotData.SlotId?>
|
|
1282
|
+
return self:PromiseLastActiveSlotId():Then(function(lastActiveSlotId: SaveSlotData.SlotId?)
|
|
1283
|
+
if not lastActiveSlotId then
|
|
1284
|
+
return nil
|
|
1285
|
+
end
|
|
1286
|
+
|
|
1287
|
+
return self:PromiseHasSlot(lastActiveSlotId):Then(function(hasLastSlot: boolean)
|
|
1288
|
+
if not hasLastSlot then
|
|
1289
|
+
return nil
|
|
1290
|
+
end
|
|
1291
|
+
|
|
1292
|
+
return self:PromiseSelectSlot(lastActiveSlotId):Then(function()
|
|
1293
|
+
return lastActiveSlotId
|
|
1294
|
+
end)
|
|
1295
|
+
end)
|
|
1296
|
+
end)
|
|
1297
|
+
end
|
|
1298
|
+
|
|
1299
|
+
--[=[
|
|
1300
|
+
Creates a new slot at the lowest free index and selects it, resolving to the
|
|
1301
|
+
new slot id -- or nil when every slot is already in use. Backs a "New Game"
|
|
1302
|
+
affordance.
|
|
1303
|
+
]=]
|
|
1304
|
+
function HasSaveSlotsDataStore.PromiseSelectNewSaveSlot(
|
|
1305
|
+
self: HasSaveSlotsDataStore
|
|
1306
|
+
): Promise.Promise<SaveSlotData.SlotId?>
|
|
1307
|
+
return (self._loadPromise :: any):Then(function(): any
|
|
1308
|
+
local usedIndices = {}
|
|
1309
|
+
for existingSlotId, slot in self._slotMap do
|
|
1310
|
+
if not self:_isEphemeral(existingSlotId) then
|
|
1311
|
+
usedIndices[SaveSlotData.SlotIndex:Get(slot)] = true
|
|
1312
|
+
end
|
|
1313
|
+
end
|
|
1314
|
+
|
|
1315
|
+
-- Lowest free positive index. Fills gaps left by deletions (delete slot 2 of
|
|
1316
|
+
-- [1,2,3] and the next new slot reuses index 2). With a finite MaxSlotCount this
|
|
1317
|
+
-- returns nil once [1, MaxSlotCount] is full; with an unbounded count (math.huge)
|
|
1318
|
+
-- there is always a next integer, so it never returns nil.
|
|
1319
|
+
local freeIndex = 1
|
|
1320
|
+
while usedIndices[freeIndex] do
|
|
1321
|
+
freeIndex += 1
|
|
1322
|
+
end
|
|
1323
|
+
|
|
1324
|
+
if freeIndex > self.MaxSlotCount.Value then
|
|
1325
|
+
return nil
|
|
1326
|
+
end
|
|
1327
|
+
|
|
1328
|
+
return self:PromiseCreateSlot(freeIndex):Then(function(slotId: SaveSlotData.SlotId)
|
|
1329
|
+
return self:PromiseSelectSlot(slotId):Then(function()
|
|
1330
|
+
return slotId
|
|
1331
|
+
end)
|
|
1332
|
+
end)
|
|
1333
|
+
end)
|
|
1334
|
+
end
|
|
1335
|
+
|
|
1336
|
+
--[=[
|
|
1337
|
+
Creates a fresh ephemeral slot and selects it, resolving to its id. An ephemeral slot is selectable and
|
|
1338
|
+
active exactly like a real one -- it drives [HasSaveSlotsDataStore.ObserveActiveSlotStoreBrio],
|
|
1339
|
+
summaries, and playtime the same way, and its metadata replicates to the client like any other slot's,
|
|
1340
|
+
so UI can render the active session's name and summary -- but it is never persisted: no metadata is
|
|
1341
|
+
written, its data store is in-memory, and it is torn down the moment it stops being the active slot. It
|
|
1342
|
+
is also excluded from the save list ([SaveSlotDataService.GetSlotList] /
|
|
1343
|
+
[SaveSlotDataService.ObserveSlotList]), so it never shows up as something the player can return to.
|
|
1344
|
+
Selecting it never disturbs the persisted active-slot pointer or the "Continue" target, so the real slot
|
|
1345
|
+
the player came from resumes untouched afterward. Backs a throwaway session (e.g. exploring a lobby)
|
|
1346
|
+
that must leave no trace on save data.
|
|
1347
|
+
|
|
1348
|
+
@param metadata SaveSlotCreateMetadata? -- optional SlotName/Summary for the in-memory slot
|
|
1349
|
+
@return Promise<SlotId>
|
|
1350
|
+
]=]
|
|
1351
|
+
function HasSaveSlotsDataStore.PromiseSelectEphemeralSlot(
|
|
1352
|
+
self: HasSaveSlotsDataStore,
|
|
1353
|
+
metadata: SaveSlotCreateMetadata?
|
|
1354
|
+
): Promise.Promise<SaveSlotData.SlotId>
|
|
1355
|
+
return (self._loadPromise :: any):Then(function()
|
|
1356
|
+
local slotId = HttpService:GenerateGUID(false)
|
|
1357
|
+
local data = {
|
|
1358
|
+
SlotId = slotId,
|
|
1359
|
+
SlotIndex = SaveSlotConstants.EPHEMERAL_SLOT_INDEX,
|
|
1360
|
+
SlotName = (metadata and metadata.SlotName) or "Ephemeral",
|
|
1361
|
+
CreatedTime = os.time(),
|
|
1362
|
+
Summary = metadata and metadata.Summary,
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
self:_buildSlot(slotId, data, true, true)
|
|
1366
|
+
|
|
1367
|
+
return self:PromiseSelectSlot(slotId):Then(function()
|
|
1368
|
+
return slotId
|
|
1369
|
+
end)
|
|
1370
|
+
end)
|
|
1371
|
+
end
|
|
1372
|
+
|
|
1373
|
+
-- Maid-owning every hop is what makes this safe, for the same reason as the selection chain in
|
|
1374
|
+
-- SaveSlotService: a session-locked or retrying read settles long after the player left, and Promise has no
|
|
1375
|
+
-- cancellation, so owning only the outermost promise detaches nothing upstream. Calling into this destroyed
|
|
1376
|
+
-- (metatable-stripped) object -- or into its destroyed stores -- throws, and because a datastore load
|
|
1377
|
+
-- resolves inside its UpdateAsync transform, that throw surfaces as a "Transform function error" and takes
|
|
1378
|
+
-- the transform's write down with it. The liveness checks cover a settle that lands mid-teardown, before
|
|
1379
|
+
-- this promise's own maid task was reached. See SaveSlotLateSettle.spec.
|
|
1380
|
+
function HasSaveSlotsDataStore._promiseLoadSlots(
|
|
1381
|
+
self: HasSaveSlotsDataStore,
|
|
1382
|
+
promiseDataStore: Promise.Promise<any>
|
|
1383
|
+
): Promise.Promise<{}>
|
|
1384
|
+
return self._maid:GivePromise(promiseDataStore):Then(function(dataStore)
|
|
1385
|
+
if not self.Destroy then
|
|
1386
|
+
return nil -- Destroyed
|
|
1387
|
+
end
|
|
1388
|
+
|
|
1389
|
+
self._dataStore = dataStore
|
|
1390
|
+
self._systemStore = dataStore:GetSubStore(SaveSlotConstants.SYSTEM_STORE_KEY)
|
|
1391
|
+
self._metadataStore = self._systemStore:GetSubStore(SaveSlotConstants.METADATA_STORE_KEY)
|
|
1392
|
+
|
|
1393
|
+
return self._maid:GivePromise(self._metadataStore:LoadAll({})):Then(function(metadata)
|
|
1394
|
+
if not self.Destroy then
|
|
1395
|
+
return nil -- Destroyed
|
|
1396
|
+
end
|
|
1397
|
+
|
|
1398
|
+
for slotId, data in metadata do
|
|
1399
|
+
self:_buildSlot(slotId, data)
|
|
1400
|
+
end
|
|
1401
|
+
|
|
1402
|
+
return self._maid
|
|
1403
|
+
:GivePromise(self._systemStore:Load("activeSlotId"))
|
|
1404
|
+
:Then(function(activeId: SaveSlotData.SlotId?)
|
|
1405
|
+
if not self.Destroy then
|
|
1406
|
+
return nil -- Destroyed
|
|
1407
|
+
end
|
|
1408
|
+
|
|
1409
|
+
self._lastActiveSlotId = activeId
|
|
1410
|
+
self.LastActiveSlotId.Value = activeId
|
|
1411
|
+
|
|
1412
|
+
-- The persisted active-slot pointer and the replicated "Continue" target only ever track real
|
|
1413
|
+
-- slots. This replaces StoreOnValueChange so an ephemeral selection is invisible to both:
|
|
1414
|
+
-- entering one leaves them pinned to the real slot, and the ephemeral slot is torn down the
|
|
1415
|
+
-- moment it stops being active. We track the id we are leaving to know which of those to do.
|
|
1416
|
+
local previousActiveSlotId: SaveSlotData.SlotId? = activeId
|
|
1417
|
+
|
|
1418
|
+
self._maid:GiveTask(self.ActiveSlotId.Changed:Connect(function()
|
|
1419
|
+
local active = self.ActiveSlotId.Value
|
|
1420
|
+
|
|
1421
|
+
-- Replicate the active transferable-ephemeral slot's shared-store key (nil otherwise) so a
|
|
1422
|
+
-- client-initiated teleport can carry it forward (SaveSlotServiceClient's provider reads this).
|
|
1423
|
+
self.ActiveTransferableEphemeralKey.Value = if active
|
|
1424
|
+
then self._transferableEphemeralKeys[active]
|
|
1425
|
+
else nil
|
|
1426
|
+
|
|
1427
|
+
local leftSlotId = previousActiveSlotId
|
|
1428
|
+
-- Read before the retire below, while the outgoing slot is still in the map.
|
|
1429
|
+
local leavingEphemeral = self:_isEphemeral(leftSlotId)
|
|
1430
|
+
previousActiveSlotId = active
|
|
1431
|
+
|
|
1432
|
+
-- Persist the pointer + remember the Continue target only for real-slot transitions
|
|
1433
|
+
-- (real -> real, real -> nil deselect, nil -> real). Skip both ephemeral cases: entering an
|
|
1434
|
+
-- ephemeral slot must stay invisible to persistence, and leaving one back to no slot must
|
|
1435
|
+
-- leave the real pointer pinned where it was.
|
|
1436
|
+
local enteringEphemeral = self:_isEphemeral(active)
|
|
1437
|
+
local leavingEphemeralToMenu = leavingEphemeral and active == nil
|
|
1438
|
+
if not (enteringEphemeral or leavingEphemeralToMenu) then
|
|
1439
|
+
self._systemStore:Store("activeSlotId", active)
|
|
1440
|
+
if active ~= nil then
|
|
1441
|
+
self._lastActiveSlotId = active
|
|
1442
|
+
self.LastActiveSlotId.Value = active
|
|
1443
|
+
end
|
|
1444
|
+
end
|
|
1445
|
+
|
|
1446
|
+
-- An ephemeral slot exists only while it is the active slot; retire the one we just left.
|
|
1447
|
+
if leavingEphemeral and leftSlotId ~= active then
|
|
1448
|
+
self:_destroyEphemeralSlot(leftSlotId :: SaveSlotData.SlotId)
|
|
1449
|
+
end
|
|
1450
|
+
end))
|
|
1451
|
+
|
|
1452
|
+
-- Matches the liveness-guard returns above, which make this callback's inferred return
|
|
1453
|
+
-- type nil; falling off the end instead returns no values at all.
|
|
1454
|
+
return nil
|
|
1455
|
+
end)
|
|
1456
|
+
end)
|
|
1457
|
+
end)
|
|
1458
|
+
end
|
|
1459
|
+
|
|
1460
|
+
function HasSaveSlotsDataStore._getSlotStore(
|
|
1461
|
+
self: HasSaveSlotsDataStore,
|
|
1462
|
+
slotId: SaveSlotData.SlotId
|
|
1463
|
+
): DataStoreStage.DataStoreStage
|
|
1464
|
+
local ephemeralStore = self._ephemeralStores[slotId]
|
|
1465
|
+
if ephemeralStore then
|
|
1466
|
+
return ephemeralStore
|
|
1467
|
+
end
|
|
1468
|
+
|
|
1469
|
+
local slot = self._slotMap[slotId]
|
|
1470
|
+
if slot and (SaveSlotData.SlotIndex:Get(slot) == SaveSlotConstants.DEFAULT_SLOT_INDEX) then
|
|
1471
|
+
return self._dataStore
|
|
1472
|
+
end
|
|
1473
|
+
return self._systemStore:GetSubStore(SaveSlotConstants.SLOT_STORE_KEY):GetSubStore(slotId)
|
|
1474
|
+
end
|
|
1475
|
+
|
|
1476
|
+
local MUTABLE_METADATA_KEYS =
|
|
1477
|
+
{ "SlotName", "CreatedTime", "LastPlayedTime", "Summary", "TimePlayed", "PlayCount", "LastSessionLength" }
|
|
1478
|
+
|
|
1479
|
+
function HasSaveSlotsDataStore._buildSlot(
|
|
1480
|
+
self: HasSaveSlotsDataStore,
|
|
1481
|
+
slotId: SaveSlotData.SlotId,
|
|
1482
|
+
data: SaveSlotData.SaveSlotMetadata,
|
|
1483
|
+
isNew: boolean?,
|
|
1484
|
+
isEphemeral: boolean?
|
|
1485
|
+
): ()
|
|
1486
|
+
local maid = Maid.new()
|
|
1487
|
+
self._maid[slotId] = maid
|
|
1488
|
+
|
|
1489
|
+
local slot = maid:Add(Instance.new("Folder"))
|
|
1490
|
+
slot.Name = slotId
|
|
1491
|
+
slot.Archivable = false
|
|
1492
|
+
|
|
1493
|
+
local attributes = SaveSlotData:Create(slot)
|
|
1494
|
+
attributes.SlotId.Value = slotId
|
|
1495
|
+
attributes.SlotIndex.Value = data.SlotIndex
|
|
1496
|
+
|
|
1497
|
+
if isEphemeral then
|
|
1498
|
+
-- Ephemeral: seed the metadata in memory with no write-back wiring, and back the slot with an in-memory
|
|
1499
|
+
-- store. The folder still joins the replicated container so the client can read the active slot's
|
|
1500
|
+
-- metadata (name, summary, playtime) the same way it reads a real slot's -- what makes the slot
|
|
1501
|
+
-- ephemeral is that nothing is persisted, not that it is invisible. The IsEphemeral attribute is what
|
|
1502
|
+
-- keeps it out of the save list; SaveSlotDataService's list reads filter on it.
|
|
1503
|
+
--
|
|
1504
|
+
-- The in-memory store's lifetime is owned by this slot's maid (which is in turn owned by self._maid),
|
|
1505
|
+
-- so it is Destroyed -- and becomes GC-eligible -- the instant the slot is retired (_destroyEphemeralSlot)
|
|
1506
|
+
-- or the player unbinds. Nothing outside this object holds a lasting reference: while the slot is active
|
|
1507
|
+
-- ObserveActiveSlotStoreBrio hands it out inside a Brio that dies when the slot deselects, releasing it.
|
|
1508
|
+
attributes.IsEphemeral.Value = true
|
|
1509
|
+
self._ephemeralStores[slotId] = maid:Add(InMemoryDataStore.new(slotId))
|
|
1510
|
+
|
|
1511
|
+
for _, key in MUTABLE_METADATA_KEYS do
|
|
1512
|
+
attributes[key].Value = data[key]
|
|
1513
|
+
end
|
|
1514
|
+
|
|
1515
|
+
-- Parent last, so the folder replicates carrying IsEphemeral -- a client that saw it land without the
|
|
1516
|
+
-- flag would briefly list a throwaway slot as a save.
|
|
1517
|
+
slot.Parent = self._slotContainer
|
|
1518
|
+
|
|
1519
|
+
self._slotMap[slotId] = slot
|
|
1520
|
+
maid:GiveTask(function()
|
|
1521
|
+
self._slotMap[slotId] = nil
|
|
1522
|
+
self._ephemeralStores[slotId] = nil
|
|
1523
|
+
self._transferableEphemeralKeys[slotId] = nil
|
|
1524
|
+
end)
|
|
1525
|
+
return
|
|
1526
|
+
end
|
|
1527
|
+
|
|
1528
|
+
local metadataStore = self._metadataStore:GetSubStore(slotId)
|
|
1529
|
+
|
|
1530
|
+
-- Store immutable SlotIndex once on creation
|
|
1531
|
+
if isNew then
|
|
1532
|
+
metadataStore:Store("SlotIndex", data.SlotIndex)
|
|
1533
|
+
end
|
|
1534
|
+
|
|
1535
|
+
-- Store mutable metadata on change
|
|
1536
|
+
for _, key in MUTABLE_METADATA_KEYS do
|
|
1537
|
+
attributes[key].Value = data[key]
|
|
1538
|
+
maid:GiveTask(metadataStore:StoreOnValueChange(key, attributes[key]))
|
|
1539
|
+
|
|
1540
|
+
if isNew then
|
|
1541
|
+
metadataStore:Store(key, attributes[key].Value)
|
|
1542
|
+
end
|
|
1543
|
+
end
|
|
1544
|
+
|
|
1545
|
+
slot.Parent = self._slotContainer
|
|
1546
|
+
|
|
1547
|
+
self._slotMap[slotId] = slot
|
|
1548
|
+
|
|
1549
|
+
maid:GiveTask(function()
|
|
1550
|
+
self._slotMap[slotId] = nil
|
|
1551
|
+
end)
|
|
1552
|
+
end
|
|
1553
|
+
|
|
1554
|
+
-- Whether the slot is an ephemeral (session-only, never-persisted) slot. The slot's own IsEphemeral property
|
|
1555
|
+
-- is the single source of truth (set once at creation in _buildSlot); nil-safe so callers can pass a possibly
|
|
1556
|
+
-- nil active-slot id directly.
|
|
1557
|
+
function HasSaveSlotsDataStore._isEphemeral(self: HasSaveSlotsDataStore, slotId: SaveSlotData.SlotId?): boolean
|
|
1558
|
+
if slotId == nil then
|
|
1559
|
+
return false
|
|
1560
|
+
end
|
|
1561
|
+
local slot = self._slotMap[slotId] :: Folder?
|
|
1562
|
+
return slot ~= nil and SaveSlotData.IsEphemeral:Get(slot) == true
|
|
1563
|
+
end
|
|
1564
|
+
|
|
1565
|
+
-- Tears down the ephemeral slot's maid (folder, in-memory store, and every map entry via the teardown task
|
|
1566
|
+
-- registered in _buildSlot). Idempotent -- clearing an already-cleared maid key is a no-op.
|
|
1567
|
+
function HasSaveSlotsDataStore._destroyEphemeralSlot(self: HasSaveSlotsDataStore, slotId: SaveSlotData.SlotId): ()
|
|
1568
|
+
self._maid[slotId] = nil
|
|
1569
|
+
end
|
|
1570
|
+
|
|
1571
|
+
--[=[
|
|
1572
|
+
Accrues per-slot playtime automatically. A "session" spans the time a slot is the active slot:
|
|
1573
|
+
selecting a slot begins one (bumping PlayCount), and deselecting, switching, or unbinding ends
|
|
1574
|
+
it. Elapsed wall time is folded into the slot's TimePlayed from a datastore saving callback, so
|
|
1575
|
+
it persists on exactly the cadence the data is written -- always fresh at save time, with no
|
|
1576
|
+
separate timer -- and again at each session boundary.
|
|
1577
|
+
|
|
1578
|
+
Skipped entirely when the owner opts out (`TrackPlaytime = false`), which is what offline admin
|
|
1579
|
+
tooling wants: editing a slot from a console is not someone playing it, and no session is ever
|
|
1580
|
+
begun, so `_flushPlaytime` stays a no-op for the object's whole life.
|
|
1581
|
+
]=]
|
|
1582
|
+
function HasSaveSlotsDataStore._setupPlaytimeTracking(self: HasSaveSlotsDataStore): ()
|
|
1583
|
+
self._playSessionSlotId = nil
|
|
1584
|
+
self._playSessionStart = nil
|
|
1585
|
+
self._playSessionLastFlush = nil
|
|
1586
|
+
|
|
1587
|
+
-- The active slot bounds the session: end the previous one (if any) and begin the new one.
|
|
1588
|
+
self._maid:GiveTask(self.ActiveSlotId.Changed:Connect(function()
|
|
1589
|
+
self:_endPlaySession()
|
|
1590
|
+
|
|
1591
|
+
local activeSlotId = self.ActiveSlotId.Value
|
|
1592
|
+
if activeSlotId ~= nil then
|
|
1593
|
+
self:_beginPlaySession(activeSlotId)
|
|
1594
|
+
end
|
|
1595
|
+
end))
|
|
1596
|
+
|
|
1597
|
+
-- Fold accrued time into TimePlayed just before every save so the written value is current. The
|
|
1598
|
+
-- callback runs before the save serializes staged data (see DataStore._syncData), so the flush is
|
|
1599
|
+
-- captured by that same save -- including the final save-on-leave.
|
|
1600
|
+
self._maid:GivePromise(self._loadPromise):Then(function()
|
|
1601
|
+
self._maid:GiveTask(self._dataStore:AddSavingCallback(function()
|
|
1602
|
+
self:_flushPlaytime()
|
|
1603
|
+
end))
|
|
1604
|
+
end)
|
|
1605
|
+
|
|
1606
|
+
-- Best-effort flush on unbind for the case where the binder tears down before that final save.
|
|
1607
|
+
self._maid:GiveTask(function()
|
|
1608
|
+
self:_endPlaySession()
|
|
1609
|
+
end)
|
|
1610
|
+
end
|
|
1611
|
+
|
|
1612
|
+
function HasSaveSlotsDataStore._beginPlaySession(self: HasSaveSlotsDataStore, slotId: SaveSlotData.SlotId): ()
|
|
1613
|
+
local now = os.time()
|
|
1614
|
+
self._playSessionSlotId = slotId
|
|
1615
|
+
self._playSessionStart = now
|
|
1616
|
+
self._playSessionLastFlush = now
|
|
1617
|
+
|
|
1618
|
+
local slot = self._slotMap[slotId]
|
|
1619
|
+
if slot then
|
|
1620
|
+
SaveSlotData.PlayCount:Set(slot, (SaveSlotData.PlayCount:Get(slot) or 0) + 1)
|
|
1621
|
+
end
|
|
1622
|
+
end
|
|
1623
|
+
|
|
1624
|
+
function HasSaveSlotsDataStore._flushPlaytime(self: HasSaveSlotsDataStore): ()
|
|
1625
|
+
local slotId = self._playSessionSlotId
|
|
1626
|
+
if slotId == nil then
|
|
1627
|
+
return
|
|
1628
|
+
end
|
|
1629
|
+
|
|
1630
|
+
local slot = self._slotMap[slotId]
|
|
1631
|
+
if not slot then
|
|
1632
|
+
return
|
|
1633
|
+
end
|
|
1634
|
+
|
|
1635
|
+
local now = os.time()
|
|
1636
|
+
|
|
1637
|
+
-- Add only the time since the last flush so repeated flushes within a session never double count.
|
|
1638
|
+
local sinceFlush = now - (self._playSessionLastFlush or now)
|
|
1639
|
+
if sinceFlush > 0 then
|
|
1640
|
+
SaveSlotData.TimePlayed:Set(slot, (SaveSlotData.TimePlayed:Get(slot) or 0) + sinceFlush)
|
|
1641
|
+
self._playSessionLastFlush = now
|
|
1642
|
+
end
|
|
1643
|
+
|
|
1644
|
+
SaveSlotData.LastSessionLength:Set(slot, now - (self._playSessionStart or now))
|
|
1645
|
+
end
|
|
1646
|
+
|
|
1647
|
+
function HasSaveSlotsDataStore._endPlaySession(self: HasSaveSlotsDataStore): ()
|
|
1648
|
+
if self._playSessionSlotId == nil then
|
|
1649
|
+
return
|
|
1650
|
+
end
|
|
1651
|
+
|
|
1652
|
+
self:_flushPlaytime()
|
|
1653
|
+
self._playSessionSlotId = nil
|
|
1654
|
+
self._playSessionStart = nil
|
|
1655
|
+
self._playSessionLastFlush = nil
|
|
1656
|
+
end
|
|
1657
|
+
|
|
1658
|
+
return HasSaveSlotsDataStore
|