@quenty/saveslot 2.2.3 → 2.4.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 CHANGED
@@ -3,6 +3,18 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.4.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@2.3.0...@quenty/saveslot@2.4.0) (2026-07-27)
7
+
8
+ ### Features
9
+
10
+ - **saveslot:** run consumer callbacks before a slot is selected ([#758](https://github.com/Quenty/NevermoreEngine/issues/758)) ([f2d8c1b](https://github.com/Quenty/NevermoreEngine/commit/f2d8c1b5e5108ff85df98749a35a54706e77ae9a))
11
+
12
+ # [2.3.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@2.2.3...@quenty/saveslot@2.3.0) (2026-07-27)
13
+
14
+ ### Features
15
+
16
+ - **saveslot:** save slot commands take a players argument ([#756](https://github.com/Quenty/NevermoreEngine/issues/756)) ([724b9b4](https://github.com/Quenty/NevermoreEngine/commit/724b9b40e4acdb4700d841e514e578332c60e155))
17
+
6
18
  ## [2.2.3](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@2.2.2...@quenty/saveslot@2.2.3) (2026-07-27)
7
19
 
8
20
  **Note:** Version bump only for package @quenty/saveslot
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/saveslot",
3
- "version": "2.2.3",
3
+ "version": "2.4.0",
4
4
  "description": "PlayerDataStoreService wrapper for save slots",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -57,5 +57,5 @@
57
57
  "publishConfig": {
58
58
  "access": "public"
59
59
  },
60
- "gitHead": "b2fa2984c2ea6e83136e5a7cbf57b14038d9a265"
60
+ "gitHead": "69f0c5a4f30d047c838a2a7f568ce02450f5e813"
61
61
  }
@@ -158,6 +158,39 @@ describe("HasSaveSlots.PromiseExportSlot / PromiseImportSlot", function()
158
158
  end)
159
159
  end)
160
160
 
161
+ it("exports the main slot when allowMainSlot is set", function()
162
+ runWithContext(function(context)
163
+ local hasSaveSlots = context.hasSaveSlots
164
+
165
+ local mainSlotId = createSelectAndWrite(hasSaveSlots, SaveSlotConstants.DEFAULT_SLOT_INDEX)
166
+
167
+ local export = awaitValueOf(hasSaveSlots:PromiseExportSlot(mainSlotId, true))
168
+ expect(export.data.Coins).toEqual(7)
169
+ expect(export.data.World_2.Eggs).toEqual(3)
170
+ end)
171
+ end)
172
+
173
+ it("strips the SaveSlots system data from a main slot export", function()
174
+ runWithContext(function(context)
175
+ local hasSaveSlots = context.hasSaveSlots
176
+
177
+ local mainSlotId = createSelectAndWrite(hasSaveSlots, SaveSlotConstants.DEFAULT_SLOT_INDEX)
178
+ createSelectAndWrite(hasSaveSlots, 2)
179
+
180
+ local export = awaitValueOf(hasSaveSlots:PromiseExportSlot(mainSlotId, true))
181
+ expect(export.data[SaveSlotConstants.SYSTEM_STORE_KEY]).toBeNil()
182
+ end)
183
+ end)
184
+
185
+ it("still refuses the main slot when allowMainSlot is false", function()
186
+ runWithContext(function(context)
187
+ local hasSaveSlots = context.hasSaveSlots
188
+
189
+ local mainSlotId = awaitValueOf(hasSaveSlots:PromiseCreateSlot(SaveSlotConstants.DEFAULT_SLOT_INDEX))
190
+ expect(awaitResolved(hasSaveSlots:PromiseExportSlot(mainSlotId, false))).toEqual(false)
191
+ end)
192
+ end)
193
+
161
194
  it("refuses to export a missing slot", function()
162
195
  runWithContext(function(context)
163
196
  local hasSaveSlots = context.hasSaveSlots
@@ -0,0 +1,208 @@
1
+ --!strict
2
+ --[[
3
+ Where a selection asks SaveSlotService whether it may proceed, and what it does with the answer. The
4
+ fan-out behind that answer is covered in SaveSlotService.spec.
5
+
6
+ @class HasSaveSlots.PreSelect.spec.lua
7
+ ]]
8
+ local require = require(script.Parent.loader).load(script)
9
+
10
+ local DataStoreMock = require("DataStoreMock")
11
+ local Jest = require("Jest")
12
+ local PlayerDataStoreService = require("PlayerDataStoreService")
13
+ local PlayerMock = require("PlayerMock")
14
+ local Promise = require("Promise")
15
+ local PromiseTestUtils = require("PromiseTestUtils")
16
+ local ServiceBag = require("ServiceBag")
17
+
18
+ local Workspace = game:GetService("Workspace")
19
+
20
+ local afterEach = Jest.Globals.afterEach
21
+ local describe = Jest.Globals.describe
22
+ local expect = Jest.Globals.expect
23
+ local it = Jest.Globals.it
24
+
25
+ local FAKE_USER_ID = 424243
26
+
27
+ local activeContext: any = nil
28
+
29
+ afterEach(function()
30
+ if activeContext then
31
+ local context = activeContext
32
+ activeContext = nil
33
+ context.destroy()
34
+ end
35
+ end)
36
+
37
+ type Ask = { slotId: string, previousSlotId: string? }
38
+
39
+ local function setup()
40
+ local serviceBag = ServiceBag.new()
41
+ serviceBag:GetService(require("TeleportDataService"))
42
+ serviceBag:GetService(require("SaveSlotSharedDataStoreService"))
43
+ local playerDataStoreService: PlayerDataStoreService.PlayerDataStoreService =
44
+ serviceBag:GetService(PlayerDataStoreService) :: any
45
+ local binder = serviceBag:GetService(require("HasSaveSlots"))
46
+ serviceBag:Init()
47
+ playerDataStoreService:SetRobloxDataStore(DataStoreMock.new())
48
+ serviceBag:Start()
49
+
50
+ local fakePlayer = PlayerMock.new({ UserId = FAKE_USER_ID })
51
+ fakePlayer.Parent = Workspace
52
+
53
+ local hasSaveSlots = assert(binder:Bind(fakePlayer), "Failed to bind HasSaveSlots")
54
+ hasSaveSlots.MaxSlotCount.Value = 5
55
+
56
+ -- Booting SaveSlotService here would drag a second PlayerMockService into a DataModel another suite is
57
+ -- already using one in, so the question put to it is answered by the test.
58
+ local asks: { Ask } = {}
59
+ local verdict: any = nil
60
+ hasSaveSlots._promisePreSelectFromSaveSlotService = function(_self: any, slotId: string): any
61
+ table.insert(asks, { slotId = slotId, previousSlotId = hasSaveSlots.ActiveSlotId.Value })
62
+
63
+ return verdict or Promise.resolved(true)
64
+ end
65
+
66
+ local destroyed = false
67
+ local function destroy()
68
+ if destroyed then
69
+ return
70
+ end
71
+ destroyed = true
72
+ if activeContext and activeContext.destroy == destroy then
73
+ activeContext = nil
74
+ end
75
+
76
+ fakePlayer:Destroy()
77
+ serviceBag:Destroy()
78
+ end
79
+
80
+ local context = {
81
+ serviceBag = serviceBag,
82
+ fakePlayer = fakePlayer,
83
+ hasSaveSlots = hasSaveSlots,
84
+ asks = asks,
85
+ answerWith = function(promise: any)
86
+ verdict = promise
87
+ end,
88
+ destroy = destroy,
89
+ }
90
+ activeContext = context
91
+
92
+ return context
93
+ end
94
+
95
+ local function await(promise: any): any
96
+ assert(PromiseTestUtils.awaitSettled(promise, 10), "promise never settled")
97
+ local ok, value = promise:Yield()
98
+ assert(ok, `promise rejected: {tostring(value)}`)
99
+ return value
100
+ end
101
+
102
+ describe("HasSaveSlots pre-select", function()
103
+ it("asks before the slot becomes active", function()
104
+ local context = setup()
105
+
106
+ local slotId = await(context.hasSaveSlots:PromiseCreateSlot(1))
107
+ await(context.hasSaveSlots:PromiseSelectSlot(slotId))
108
+
109
+ expect(#context.asks).toEqual(1)
110
+ expect(context.asks[1].slotId).toEqual(slotId)
111
+ expect(context.asks[1].previousSlotId).toBeNil()
112
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(slotId)
113
+
114
+ context.destroy()
115
+ end)
116
+
117
+ it("reports the selection being replaced when switching slots", function()
118
+ local context = setup()
119
+
120
+ local firstId = await(context.hasSaveSlots:PromiseCreateSlot(1))
121
+ local secondId = await(context.hasSaveSlots:PromiseCreateSlot(2))
122
+ await(context.hasSaveSlots:PromiseSelectSlot(firstId))
123
+ await(context.hasSaveSlots:PromiseSelectSlot(secondId))
124
+
125
+ expect(#context.asks).toEqual(2)
126
+ expect(context.asks[2].slotId).toEqual(secondId)
127
+ expect(context.asks[2].previousSlotId).toEqual(firstId)
128
+
129
+ context.destroy()
130
+ end)
131
+
132
+ it("asks for a new slot and for an ephemeral slot alike", function()
133
+ local context = setup()
134
+
135
+ local newId = await(context.hasSaveSlots:PromiseSelectNewSaveSlot())
136
+ local ephemeralId = await(context.hasSaveSlots:PromiseSelectEphemeralSlot({ SlotName = "Throwaway" }))
137
+
138
+ expect(#context.asks).toEqual(2)
139
+ expect(context.asks[1].slotId).toEqual(newId)
140
+ expect(context.asks[2].slotId).toEqual(ephemeralId)
141
+
142
+ context.destroy()
143
+ end)
144
+
145
+ it("does not ask when the slot is already active", function()
146
+ local context = setup()
147
+
148
+ local slotId = await(context.hasSaveSlots:PromiseCreateSlot(1))
149
+ await(context.hasSaveSlots:PromiseSelectSlot(slotId))
150
+ await(context.hasSaveSlots:PromiseSelectSlot(slotId))
151
+
152
+ expect(#context.asks).toEqual(1)
153
+
154
+ context.destroy()
155
+ end)
156
+
157
+ it("holds the selection until the answer settles", function()
158
+ local context = setup()
159
+
160
+ local gate = Promise.new()
161
+ context.answerWith(gate)
162
+
163
+ local slotId = await(context.hasSaveSlots:PromiseCreateSlot(1))
164
+ local selection = context.hasSaveSlots:PromiseSelectSlot(slotId)
165
+
166
+ expect(PromiseTestUtils.awaitSettled(selection, 0.5)).toEqual(false)
167
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toBeNil()
168
+
169
+ gate:Resolve(true)
170
+
171
+ await(selection)
172
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(slotId)
173
+
174
+ context.destroy()
175
+ end)
176
+
177
+ it("rejects the selection and leaves the active slot alone when refused", function()
178
+ local context = setup()
179
+
180
+ context.answerWith(Promise.resolved(false))
181
+
182
+ local slotId = await(context.hasSaveSlots:PromiseCreateSlot(1))
183
+ local selection = context.hasSaveSlots:PromiseSelectSlot(slotId)
184
+
185
+ assert(PromiseTestUtils.awaitSettled(selection, 10), "selection never settled")
186
+ expect(selection:IsRejected()).toEqual(true)
187
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toBeNil()
188
+
189
+ context.destroy()
190
+ end)
191
+
192
+ it("leaves the previous selection standing when a switch is refused", function()
193
+ local context = setup()
194
+
195
+ local firstId = await(context.hasSaveSlots:PromiseCreateSlot(1))
196
+ local secondId = await(context.hasSaveSlots:PromiseCreateSlot(2))
197
+ await(context.hasSaveSlots:PromiseSelectSlot(firstId))
198
+
199
+ context.answerWith(Promise.resolved(false))
200
+ local selection = context.hasSaveSlots:PromiseSelectSlot(secondId)
201
+
202
+ assert(PromiseTestUtils.awaitSettled(selection, 10), "selection never settled")
203
+ expect(selection:IsRejected()).toEqual(true)
204
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(firstId)
205
+
206
+ context.destroy()
207
+ end)
208
+ end)
@@ -173,6 +173,22 @@ function HasSaveSlots.PromiseHasSlot(self: HasSaveSlots, slotId: SaveSlotData.Sl
173
173
  end)
174
174
  end
175
175
 
176
+ -- SaveSlotService owns the callbacks and runs them. Reached through the module instance rather than by
177
+ -- name, because `require("SaveSlotService")` from here -- at any depth, even inside this function -- is a
178
+ -- cyclic module dependency: SaveSlotService requires this module. A binder bound without the service in
179
+ -- its bag has nothing to ask, and allows the selection.
180
+ function HasSaveSlots._promisePreSelectFromSaveSlotService(
181
+ self: HasSaveSlots,
182
+ slotId: SaveSlotData.SlotId
183
+ ): Promise.Promise<boolean>
184
+ local serviceModule = script.Parent.Parent:FindFirstChild("SaveSlotService")
185
+ if not serviceModule or not self._serviceBag:HasService(serviceModule) then
186
+ return (Promise :: any).resolved(true)
187
+ end
188
+
189
+ return self._serviceBag:GetService(serviceModule):PromisePreSelect(self._obj, slotId, self.ActiveSlotId.Value)
190
+ end
191
+
176
192
  --[=[
177
193
  Selects the slot with the given ID
178
194
  ]=]
@@ -192,20 +208,28 @@ function HasSaveSlots.PromiseSelectSlot(self: HasSaveSlots, slotId: SaveSlotData
192
208
  SaveSlotData.LastPlayedTime:Set(slot, os.time())
193
209
  end
194
210
 
211
+ local function promiseSetSlot()
212
+ return self:_promisePreSelectFromSaveSlotService(slotId):Then(function(allowed: boolean)
213
+ if not allowed then
214
+ return (Promise :: any).rejected(`Slot \{{slotId}\} refused by a pre-select callback`)
215
+ end
216
+
217
+ return setSlot()
218
+ end)
219
+ end
220
+
195
221
  -- Initialize or save and switch
196
222
  if self.ActiveSlotId.Value == nil then
197
- setSlot()
198
- return
223
+ return promiseSetSlot()
199
224
  end
200
225
 
201
226
  -- Leaving an ephemeral slot has nothing to persist, so switch without a datastore flush (the flush
202
227
  -- exists to save the outgoing slot's progress, and an ephemeral slot has none).
203
228
  if self:_isEphemeral(self.ActiveSlotId.Value) then
204
- setSlot()
205
- return
229
+ return promiseSetSlot()
206
230
  end
207
231
 
208
- return self._dataStore:Save():Then(setSlot)
232
+ return self._dataStore:Save():Then(promiseSetSlot)
209
233
  end)
210
234
  end
211
235
 
@@ -273,16 +297,24 @@ end
273
297
 
274
298
  --[=[
275
299
  Exports a slot's saved data into a plain, serializable [SaveSlotExportUtils.SaveSlotExport].
276
- Rejects the main/default slot: its store is the player's shared root datastore, so exporting it
277
- would leak the SaveSlots system data and universe-scoped global data living alongside it. Only
278
- isolated non-main slot substores are exportable.
300
+ Rejects the main/default slot by default: its store is the player's shared root datastore, so
301
+ exporting it would leak the SaveSlots system data and universe-scoped global data living
302
+ alongside it. Only isolated non-main slot substores are exportable.
303
+
304
+ `allowMainSlot` opts out of that refusal for trusted admin tooling (see [SaveSlotCmdrService]).
305
+ The SaveSlots system data is always stripped from the result, so the export never carries the
306
+ slot roster or the other slots' saved data. Universe-scoped global data sharing the root store
307
+ is indistinguishable from the main slot's own data and is still carried, which is why this is
308
+ not exposed through [SaveSlotService].
279
309
 
280
310
  @param slotId SlotId
311
+ @param allowMainSlot boolean? -- defaults to false
281
312
  @return Promise<SaveSlotExportUtils.SaveSlotExport>
282
313
  ]=]
283
314
  function HasSaveSlots.PromiseExportSlot(
284
315
  self: HasSaveSlots,
285
- slotId: SaveSlotData.SlotId
316
+ slotId: SaveSlotData.SlotId,
317
+ allowMainSlot: boolean?
286
318
  ): Promise.Promise<SaveSlotExportUtils.SaveSlotExport>
287
319
  return (self._loadPromise :: any):Then(function()
288
320
  local slot = self._slotMap[slotId]
@@ -290,13 +322,23 @@ function HasSaveSlots.PromiseExportSlot(
290
322
  return (Promise :: any).rejected(`Slot \{{slotId}\} not found`)
291
323
  end
292
324
 
293
- if SaveSlotExportUtils.isMainSlotIndex(SaveSlotData.SlotIndex:Get(slot)) then
325
+ local isMainSlot = SaveSlotExportUtils.isMainSlotIndex(SaveSlotData.SlotIndex:Get(slot))
326
+ if isMainSlot and not allowMainSlot then
294
327
  return (Promise :: any).rejected("Cannot export the main slot")
295
328
  end
296
329
 
297
330
  local metadata = SaveSlotData:Get(slot)
298
331
  return self:_getSlotStore(slotId):LoadAll({}):Then(function(sourceData)
299
332
  local data = if type(sourceData) == "table" then table.clone(sourceData) else {}
333
+
334
+ if isMainSlot then
335
+ -- The main slot's store is the player's shared root, so LoadAll pulls the SaveSlots system
336
+ -- data with it -- the slot roster and every non-main slot's saved data. Drop it so the
337
+ -- export is only this slot, and so importing it can't nest a whole roster inside a slot
338
+ -- (mirrors PromiseDuplicateSlot, which strips the same key for the same reason).
339
+ data[SaveSlotConstants.SYSTEM_STORE_KEY] = nil
340
+ end
341
+
300
342
  return SaveSlotExportUtils.create(data, metadata.SlotName, metadata.Summary)
301
343
  end)
302
344
  end)
@@ -358,14 +400,16 @@ end
358
400
 
359
401
  @param slotId SlotId
360
402
  @param key string
403
+ @param allowMainSlot boolean? -- defaults to false, see [HasSaveSlots.PromiseExportSlot]
361
404
  @return Promise<boolean>
362
405
  ]=]
363
406
  function HasSaveSlots.PromiseSaveSlotToSharedDataStore(
364
407
  self: HasSaveSlots,
365
408
  slotId: SaveSlotData.SlotId,
366
- key: string
409
+ key: string,
410
+ allowMainSlot: boolean?
367
411
  ): Promise.Promise<boolean>
368
- return self:PromiseExportSlot(slotId):Then(function(export)
412
+ return self:PromiseExportSlot(slotId, allowMainSlot):Then(function(export)
369
413
  return self._sharedSaveSlotDataStoreService:PromiseWrite(key, export)
370
414
  end)
371
415
  end
@@ -523,15 +567,18 @@ end
523
567
  --[=[
524
568
  Exports a slot to the shared store under a fresh generated code and resolves to that code. The code
525
569
  is a shareable handle other sessions load with [HasSaveSlots.PromiseImportEphemeralSaveSlotFromCode].
526
- Defaults to the active slot. Refuses the main slot (see [HasSaveSlots.PromiseExportSlot]). The code
527
- format comes from the configured generator (see [HasSaveSlots.SetCodeGenerator]).
570
+ Defaults to the active slot. Refuses the main slot unless `allowMainSlot` is set (see
571
+ [HasSaveSlots.PromiseExportSlot]). The code format comes from the configured generator (see
572
+ [HasSaveSlots.SetCodeGenerator]).
528
573
 
529
574
  @param slotId SlotId? -- defaults to the active slot
575
+ @param allowMainSlot boolean? -- defaults to false, see [HasSaveSlots.PromiseExportSlot]
530
576
  @return Promise<string>
531
577
  ]=]
532
578
  function HasSaveSlots.PromiseExportSaveSlotToCode(
533
579
  self: HasSaveSlots,
534
- slotId: SaveSlotData.SlotId?
580
+ slotId: SaveSlotData.SlotId?,
581
+ allowMainSlot: boolean?
535
582
  ): Promise.Promise<string>
536
583
  return (self._loadPromise :: any):Then(function()
537
584
  local targetSlotId = slotId or self.ActiveSlotId.Value
@@ -540,7 +587,7 @@ function HasSaveSlots.PromiseExportSaveSlotToCode(
540
587
  end
541
588
 
542
589
  local code = self:_generateCode(targetSlotId)
543
- return self:PromiseSaveSlotToSharedDataStore(targetSlotId, code):Then(function()
590
+ return self:PromiseSaveSlotToSharedDataStore(targetSlotId, code, allowMainSlot):Then(function()
544
591
  return code
545
592
  end)
546
593
  end)
@@ -562,14 +609,17 @@ end
562
609
 
563
610
  --[=[
564
611
  Exports a slot as a raw JSON string (no shared store), for direct inspection or attaching to a bug
565
- report. Defaults to the active slot. Refuses the main slot (see [HasSaveSlots.PromiseExportSlot]).
612
+ report. Defaults to the active slot. Refuses the main slot unless `allowMainSlot` is set (see
613
+ [HasSaveSlots.PromiseExportSlot]).
566
614
 
567
615
  @param slotId SlotId? -- defaults to the active slot
616
+ @param allowMainSlot boolean? -- defaults to false, see [HasSaveSlots.PromiseExportSlot]
568
617
  @return Promise<string>
569
618
  ]=]
570
619
  function HasSaveSlots.PromiseExportSaveSlotToJson(
571
620
  self: HasSaveSlots,
572
- slotId: SaveSlotData.SlotId?
621
+ slotId: SaveSlotData.SlotId?,
622
+ allowMainSlot: boolean?
573
623
  ): Promise.Promise<string>
574
624
  return (self._loadPromise :: any):Then(function()
575
625
  local targetSlotId = slotId or self.ActiveSlotId.Value
@@ -577,7 +627,7 @@ function HasSaveSlots.PromiseExportSaveSlotToJson(
577
627
  return (Promise :: any).rejected("No slot to export")
578
628
  end
579
629
 
580
- return self:PromiseExportSlot(targetSlotId):Then(function(export)
630
+ return self:PromiseExportSlot(targetSlotId, allowMainSlot):Then(function(export)
581
631
  return HttpService:JSONEncode(export)
582
632
  end)
583
633
  end)