@quenty/saveslot 2.5.0 → 2.6.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,12 @@
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.6.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@2.5.0...@quenty/saveslot@2.6.0) (2026-07-28)
7
+
8
+ ### Features
9
+
10
+ - **saveslot:** let save-slot commands address ephemeral slots and persist them ([17fd930](https://github.com/Quenty/NevermoreEngine/commit/17fd930afdfa259eca552072e9a15bfa29f0afe7))
11
+
6
12
  # [2.5.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@2.4.0...@quenty/saveslot@2.5.0) (2026-07-28)
7
13
 
8
14
  **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.5.0",
3
+ "version": "2.6.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": "65232b0907168a4733c7930268142fb86ef75da4"
60
+ "gitHead": "080641f1556a78880a83a034412e16ba5ecd2eda"
61
61
  }
@@ -40,13 +40,6 @@ export type SummaryProvider = (Player, DataStoreStage.DataStoreStage) -> Observa
40
40
  -- identity, so it never collides with a real value (even an empty table) a provider might emit.
41
41
  local NONE = {}
42
42
 
43
- -- An ephemeral slot carries no meaningful index -- a real slot's index positions it in the save list and
44
- -- routes its store, neither of which applies to a throwaway slot. The authoritative "is ephemeral"
45
- -- discriminator is the slot's own IsEphemeral property (see _isEphemeral), and every index-accounting path
46
- -- skips ephemeral slots, so this value never routes anything and any number of ephemeral slots can coexist.
47
- -- 0 is used because a real index is always >= 1 (DEFAULT_SLOT_INDEX is 1, PromiseCreateSlot rejects < 1).
48
- local EPHEMERAL_SLOT_INDEX = 0
49
-
50
43
  -- The caller-supplied fields for a new slot. SlotId and SlotIndex are assigned by PromiseCreateSlot
51
44
  -- itself (from a fresh GUID and the slotIndex argument), so they are never taken from here.
52
45
  export type SaveSlotCreateMetadata = {
@@ -458,7 +451,7 @@ function HasSaveSlots.PromiseSelectTransferableEphemeralSlot(
458
451
  local slotId = HttpService:GenerateGUID(false)
459
452
  self:_buildSlot(slotId, {
460
453
  SlotId = slotId,
461
- SlotIndex = EPHEMERAL_SLOT_INDEX,
454
+ SlotIndex = SaveSlotConstants.EPHEMERAL_SLOT_INDEX,
462
455
  SlotName = export.slotName or "Transfer",
463
456
  CreatedTime = os.time(),
464
457
  Summary = export.summary,
@@ -638,6 +631,12 @@ end
638
631
  copying its saved data. Resolves to the new slot's id. The copy is not selected,
639
632
  its metadata (playtime, timestamps) starts fresh, and its name is suffixed with
640
633
  " (Copy)". Rejects when the source slot is missing or every index is in use.
634
+
635
+ An ephemeral slot may be duplicated: the copy is a real, persisted slot seeded with the ephemeral
636
+ slot's live in-memory data, which is how a throwaway session is turned into a save. It keeps the
637
+ source's name unsuffixed (the copy is the first real slot for that session, not a second copy of an
638
+ existing save) and, like any duplicate, is not selected -- see
639
+ [HasSaveSlots.PromisePersistEphemeralSlot] for the version that continues play on the new slot.
641
640
  ]=]
642
641
  function HasSaveSlots.PromiseDuplicateSlot(
643
642
  self: HasSaveSlots,
@@ -649,9 +648,7 @@ function HasSaveSlots.PromiseDuplicateSlot(
649
648
  return (Promise :: any).rejected(`Slot \{{slotId}\} not found`)
650
649
  end
651
650
 
652
- if self:_isEphemeral(slotId) then
653
- return (Promise :: any).rejected("Cannot duplicate an ephemeral slot")
654
- end
651
+ local sourceIsEphemeral = self:_isEphemeral(slotId)
655
652
 
656
653
  -- Lowest free positive index, filling gaps left by deletions (mirrors PromiseSelectNewSaveSlot).
657
654
  local usedIndices = {}
@@ -677,34 +674,89 @@ function HasSaveSlots.PromiseDuplicateSlot(
677
674
  local slotData = if type(sourceData) == "table" then table.clone(sourceData) else {}
678
675
  slotData[SaveSlotConstants.SYSTEM_STORE_KEY] = nil
679
676
 
680
- return self:PromiseCreateSlot(freeIndex, {
681
- SlotName = `{sourceMetadata.SlotName} (Copy)`,
682
- Summary = sourceMetadata.Summary,
683
- }):Then(function(newSlotId: SaveSlotData.SlotId)
684
- local destStore = self:_getSlotStore(newSlotId)
685
-
686
- if destStore == self._dataStore then
687
- -- The copy is the default slot, whose store is the shared root. Merge so the SaveSlots
688
- -- system store living alongside it survives (a plain Overwrite would wipe it).
689
- destStore:OverwriteMerge(slotData)
690
- else
691
- destStore:Overwrite(slotData)
692
- end
677
+ return self
678
+ :PromiseCreateSlot(freeIndex, {
679
+ SlotName = if sourceIsEphemeral
680
+ then sourceMetadata.SlotName
681
+ else `{sourceMetadata.SlotName} (Copy)`,
682
+ Summary = sourceMetadata.Summary,
683
+ })
684
+ :Then(function(newSlotId: SaveSlotData.SlotId)
685
+ local destStore = self:_getSlotStore(newSlotId)
686
+
687
+ if destStore == self._dataStore then
688
+ -- The copy is the default slot, whose store is the shared root. Merge so the SaveSlots
689
+ -- system store living alongside it survives (a plain Overwrite would wipe it).
690
+ destStore:OverwriteMerge(slotData)
691
+ else
692
+ destStore:Overwrite(slotData)
693
+ end
693
694
 
694
- -- Flush so the duplicated data survives a crash before the next autosave.
695
- return self._dataStore:Save():Then(function()
696
- return newSlotId
695
+ -- Flush so the duplicated data survives a crash before the next autosave.
696
+ return self._dataStore:Save():Then(function()
697
+ return newSlotId
698
+ end)
697
699
  end)
700
+ end)
701
+ end)
702
+ end
703
+
704
+ --[=[
705
+ Turns an ephemeral slot into a real save: duplicates it into a fresh persisted slot at the lowest free
706
+ index (see [HasSaveSlots.PromiseDuplicateSlot]) and selects that slot, so play continues on data that
707
+ is now being written. Defaults to the active slot, which in practice is the only ephemeral slot there
708
+ is -- one stops existing the moment it stops being active. Resolves to the new slot's id.
709
+
710
+ The copy is taken from the ephemeral slot's live in-memory data at the moment this runs, and selecting
711
+ the new slot retires the ephemeral one, so anything written to the old store between the copy and the
712
+ selection is dropped. Rejects when no slot is active, the slot is missing, the slot is not ephemeral,
713
+ or every index is in use.
714
+
715
+ @param slotId SlotId? -- defaults to the active slot
716
+ @return Promise<SlotId>
717
+ ]=]
718
+ function HasSaveSlots.PromisePersistEphemeralSlot(
719
+ self: HasSaveSlots,
720
+ slotId: SaveSlotData.SlotId?
721
+ ): Promise.Promise<SaveSlotData.SlotId>
722
+ return (self._loadPromise :: any):Then(function()
723
+ local targetSlotId = slotId or self.ActiveSlotId.Value
724
+ if not targetSlotId then
725
+ return (Promise :: any).rejected("No slot to persist")
726
+ end
727
+
728
+ if not self._slotMap[targetSlotId] then
729
+ return (Promise :: any).rejected(`Slot \{{targetSlotId}\} not found`)
730
+ end
731
+
732
+ if not self:_isEphemeral(targetSlotId) then
733
+ return (Promise :: any).rejected(`Slot \{{targetSlotId}\} is already persisted`)
734
+ end
735
+
736
+ return self:PromiseDuplicateSlot(targetSlotId):Then(function(newSlotId: SaveSlotData.SlotId)
737
+ return self:PromiseSelectSlot(newSlotId):Then(function()
738
+ return newSlotId
698
739
  end)
699
740
  end)
700
741
  end)
701
742
  end
702
743
 
703
744
  --[=[
704
- Deletes the slot with the given ID
745
+ Deletes the slot with the given ID. Deleting an ephemeral slot ends that session -- it is deselected
746
+ and retired along with its in-memory store -- rather than being refused for being the active slot.
705
747
  ]=]
706
748
  function HasSaveSlots.PromiseDeleteSlot(self: HasSaveSlots, slotId: SaveSlotData.SlotId): Promise.Promise<any>
707
749
  return (self._loadPromise :: any):Then(function()
750
+ -- An ephemeral slot only exists while it is active, so deleting one is retiring it. Nothing was
751
+ -- ever persisted, so there is no stored data to wipe afterwards -- hence the early return.
752
+ if self:_isEphemeral(slotId) then
753
+ if slotId == self.ActiveSlotId.Value then
754
+ return self:PromiseDeselectSlot()
755
+ end
756
+ self:_destroyEphemeralSlot(slotId)
757
+ return (Promise :: any).resolved()
758
+ end
759
+
708
760
  if slotId == self.ActiveSlotId.Value then
709
761
  return (Promise :: any).rejected("Cannot delete active slot")
710
762
  end
@@ -1003,7 +1055,7 @@ function HasSaveSlots.PromiseSelectEphemeralSlot(
1003
1055
  local slotId = HttpService:GenerateGUID(false)
1004
1056
  local data = {
1005
1057
  SlotId = slotId,
1006
- SlotIndex = EPHEMERAL_SLOT_INDEX,
1058
+ SlotIndex = SaveSlotConstants.EPHEMERAL_SLOT_INDEX,
1007
1059
  SlotName = (metadata and metadata.SlotName) or "Ephemeral",
1008
1060
  CreatedTime = os.time(),
1009
1061
  Summary = metadata and metadata.Summary,
@@ -1717,7 +1717,7 @@ describe("HasSaveSlots ephemeral slots", function()
1717
1717
  context.destroy()
1718
1718
  end)
1719
1719
 
1720
- it("refuses to reset or duplicate an ephemeral slot", function()
1720
+ it("refuses to reset an ephemeral slot", function()
1721
1721
  local context = setup()
1722
1722
 
1723
1723
  local ephemeralId = selectEphemeral(context)
@@ -1726,9 +1726,78 @@ describe("HasSaveSlots ephemeral slots", function()
1726
1726
  expect(PromiseTestUtils.awaitSettled(resetPromise, 10)).toEqual(true)
1727
1727
  expect((resetPromise:Yield())).toEqual(false)
1728
1728
 
1729
- local duplicatePromise = context.hasSaveSlots:PromiseDuplicateSlot(ephemeralId)
1730
- expect(PromiseTestUtils.awaitSettled(duplicatePromise, 10)).toEqual(true)
1731
- expect((duplicatePromise:Yield())).toEqual(false)
1729
+ context.destroy()
1730
+ end)
1731
+
1732
+ it("duplicates an ephemeral slot into a real slot carrying its live data", function()
1733
+ local context = setup()
1734
+
1735
+ createAndSelectReal(context, 1)
1736
+ local ephemeralId = selectEphemeral(context, { SlotName = "Lobby run" })
1737
+ resolve(context.hasSaveSlots:PromiseActiveSlotStore()):Store("Coins", 12)
1738
+
1739
+ local newSlotId = resolve(context.hasSaveSlots:PromiseDuplicateSlot(ephemeralId))
1740
+
1741
+ local metadata = resolve(context.hasSaveSlots:PromiseGetSlotMetadata(newSlotId))
1742
+ expect(metadata.IsEphemeral).never.toEqual(true)
1743
+ expect(metadata.SlotName).toEqual("Lobby run")
1744
+
1745
+ local export = resolve(context.hasSaveSlots:PromiseExportSlot(newSlotId))
1746
+ expect(export.data.Coins).toEqual(12)
1747
+
1748
+ -- Duplicating leaves the session running on the ephemeral slot, like any other duplicate.
1749
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(ephemeralId)
1750
+
1751
+ context.destroy()
1752
+ end)
1753
+
1754
+ it("persists the ephemeral slot and continues play on the new real slot", function()
1755
+ local context = setup()
1756
+
1757
+ local ephemeralId = selectEphemeral(context, { SlotName = "Lobby run" })
1758
+ resolve(context.hasSaveSlots:PromiseActiveSlotStore()):Store("Coins", 12)
1759
+
1760
+ local newSlotId = resolve(context.hasSaveSlots:PromisePersistEphemeralSlot())
1761
+
1762
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(newSlotId)
1763
+ expect(resolve(context.hasSaveSlots:PromiseHasSlot(ephemeralId))).toEqual(false)
1764
+
1765
+ local listedIds = {}
1766
+ for _, metadata in (SaveSlotDataService :: any):GetSlotList(context.fakePlayer) do
1767
+ listedIds[metadata.SlotId] = true
1768
+ end
1769
+ expect(listedIds[newSlotId]).toEqual(true)
1770
+
1771
+ -- The copy landed on the default index, whose store is the player's root: the session's data is
1772
+ -- there and the SaveSlots system data alongside it survived the merge.
1773
+ local data = resolve(resolve(context.hasSaveSlots:PromiseActiveSlotStore()):LoadAll({}))
1774
+ expect(data.Coins).toEqual(12)
1775
+ expect(data.SaveSlots).never.toBeNil()
1776
+
1777
+ context.destroy()
1778
+ end)
1779
+
1780
+ it("refuses to persist when the active slot is a real one", function()
1781
+ local context = setup()
1782
+
1783
+ createAndSelectReal(context, 1)
1784
+
1785
+ local persistPromise = context.hasSaveSlots:PromisePersistEphemeralSlot()
1786
+ expect(PromiseTestUtils.awaitSettled(persistPromise, 10)).toEqual(true)
1787
+ expect((persistPromise:Yield())).toEqual(false)
1788
+
1789
+ context.destroy()
1790
+ end)
1791
+
1792
+ it("ends the session when the ephemeral slot is deleted", function()
1793
+ local context = setup()
1794
+
1795
+ local ephemeralId = selectEphemeral(context)
1796
+ resolve(context.hasSaveSlots:PromiseDeleteSlot(ephemeralId))
1797
+
1798
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(nil)
1799
+ expect(resolve(context.hasSaveSlots:PromiseHasSlot(ephemeralId))).toEqual(false)
1800
+ expect(slotContainerHasChild(context, ephemeralId)).toEqual(false)
1732
1801
 
1733
1802
  context.destroy()
1734
1803
  end)
@@ -10,6 +10,7 @@ local HasSaveSlots = require("HasSaveSlots")
10
10
  local Maid = require("Maid")
11
11
  local Promise = require("Promise")
12
12
  local SaveSlotCmdrUtils = require("SaveSlotCmdrUtils")
13
+ local SaveSlotConstants = require("SaveSlotConstants")
13
14
  local SaveSlotDataService = require("SaveSlotDataService")
14
15
  local ServiceBag = require("ServiceBag")
15
16
 
@@ -78,6 +79,14 @@ function SaveSlotCmdrService._registerCommands(self: SaveSlotCmdrService): ()
78
79
  local isActive = (slot.SlotId == activeSlotId)
79
80
  listString ..= `\n"{slot.SlotName}" ({slot.SlotIndex}){isActive and " — Active" or ""}\n{slot.Summary}\n`
80
81
  end
82
+
83
+ -- An ephemeral session is not a save, so it is absent from the slot list above -- but it is what
84
+ -- the player is playing right now and the thing persist-save-slot acts on, so list it under the
85
+ -- reserved index the other commands address it by.
86
+ if self._saveSlotDataService:IsActiveSlotEphemeral(player) then
87
+ local metadata = self._saveSlotDataService:GetActiveSlotMetadata(player)
88
+ listString ..= `\n"{metadata.SlotName}" ({metadata.SlotIndex}) — Active, ephemeral\n{metadata.Summary}\n`
89
+ end
81
90
  end
82
91
 
83
92
  return listString
@@ -128,7 +137,7 @@ function SaveSlotCmdrService._registerCommands(self: SaveSlotCmdrService): ()
128
137
  },
129
138
  }, function(_context, players: { Player }, slotIndex: number)
130
139
  local lines = self:_promisePlayerLines(players, function(hasSaveSlots, player)
131
- local slotId = self._saveSlotDataService:GetSlotIdFromIndex(player, slotIndex)
140
+ local slotId = self:_getSlotIdFromIndex(player, slotIndex)
132
141
  if not slotId then
133
142
  return `{player.Name} has no slot with index {slotIndex}.`
134
143
  end
@@ -268,29 +277,34 @@ function SaveSlotCmdrService._registerCommands(self: SaveSlotCmdrService): ()
268
277
  return "No matching slots to delete."
269
278
  end
270
279
 
271
- local lines = self:_promiseSlotLines(targets, function(hasSaveSlots, entry, player)
272
- -- The active slot can't be deleted while selected, so deselect it first (flushing its
273
- -- progress) when reached. Read live rather than up front, since an earlier deletion in
274
- -- this batch may have already deselected it.
275
- local deselect = if entry.slotId == self._saveSlotDataService:GetActiveSlotId(player)
276
- then hasSaveSlots:PromiseDeselectSlot()
277
- else Promise.resolved()
278
-
279
- return deselect
280
- :Then(function()
281
- return hasSaveSlots:PromiseDeleteSlot(entry.slotId)
282
- end)
283
- :Then(function()
284
- return `{player.Name} deleted slot {entry.slotIndex}.`
285
- end)
286
- end):Wait()
280
+ local lines = self
281
+ :_promiseSlotLines(targets, function(hasSaveSlots, entry, player)
282
+ -- The active slot can't be deleted while selected, so deselect it first (flushing its
283
+ -- progress) when reached. Read live rather than up front, since an earlier deletion in
284
+ -- this batch may have already deselected it. An ephemeral session is left alone:
285
+ -- PromiseDeleteSlot retires it itself, and deselecting first would retire it early,
286
+ -- leaving nothing behind to delete.
287
+ local isActive = (entry.slotId == self._saveSlotDataService:GetActiveSlotId(player))
288
+ local deselect = if isActive and not self._saveSlotDataService:IsActiveSlotEphemeral(player)
289
+ then hasSaveSlots:PromiseDeselectSlot()
290
+ else Promise.resolved()
291
+
292
+ return deselect
293
+ :Then(function()
294
+ return hasSaveSlots:PromiseDeleteSlot(entry.slotId)
295
+ end)
296
+ :Then(function()
297
+ return `{player.Name} deleted slot {entry.slotIndex}.`
298
+ end)
299
+ end)
300
+ :Wait()
287
301
 
288
302
  return `Deleted:\n{table.concat(lines, "\n")}`
289
303
  end)
290
304
 
291
305
  self._cmdrService:RegisterCommand({
292
306
  Name = "duplicate-save-slot",
293
- Description = "Duplicates save slots into new slots at the lowest free indices.",
307
+ Description = "Duplicates save slots into new slots at the lowest free indices. Duplicating an ephemeral session saves it without switching onto the copy -- see persist-save-slot.",
294
308
  Group = "SaveSlots",
295
309
  Args = {
296
310
  {
@@ -323,6 +337,36 @@ function SaveSlotCmdrService._registerCommands(self: SaveSlotCmdrService): ()
323
337
  return `Duplicated:\n{table.concat(lines, "\n")}`
324
338
  end)
325
339
 
340
+ self._cmdrService:RegisterCommand({
341
+ Name = "persist-save-slot",
342
+ Description = "Turns an ephemeral session into a real save slot and switches onto it.",
343
+ Group = "SaveSlots",
344
+ Args = {
345
+ {
346
+ Name = "Players",
347
+ Type = "players",
348
+ Description = "Players to persist the session of (e.g. . for yourself, or * for everyone).",
349
+ },
350
+ },
351
+ }, function(_context, players: { Player })
352
+ -- Takes no slot argument: an ephemeral slot exists only while it is active, so the active slot is
353
+ -- the only one there is to persist.
354
+ local lines = self
355
+ :_promisePlayerLines(players, function(hasSaveSlots, player)
356
+ if not self._saveSlotDataService:IsActiveSlotEphemeral(player) then
357
+ return `{player.Name} has no ephemeral session active.`
358
+ end
359
+
360
+ return hasSaveSlots:PromisePersistEphemeralSlot():Then(function(newSlotId)
361
+ local metadata = self._saveSlotDataService:GetSlotMetadata(player, newSlotId)
362
+ return `{player.Name} persisted their session to slot {metadata.SlotIndex} ("{metadata.SlotName}").`
363
+ end)
364
+ end)
365
+ :Wait()
366
+
367
+ return table.concat(lines, "\n")
368
+ end)
369
+
326
370
  self._cmdrService:RegisterCommand({
327
371
  Name = "reset-save-slot",
328
372
  Description = "Resets save slots to fresh empty ones, keeping their index and name.",
@@ -489,6 +533,20 @@ function SaveSlotCmdrService._registerCommands(self: SaveSlotCmdrService): ()
489
533
  end)
490
534
  end
491
535
 
536
+ -- Resolves a slot index to the player's slot id, including the reserved ephemeral index (see
537
+ -- [SaveSlotCmdrUtils]). The data service's index lookup only walks the save list, which an ephemeral slot
538
+ -- is excluded from, so that one index resolves against the active slot instead.
539
+ function SaveSlotCmdrService._getSlotIdFromIndex(self: SaveSlotCmdrService, player: Player, slotIndex: number): string?
540
+ if slotIndex == SaveSlotConstants.EPHEMERAL_SLOT_INDEX then
541
+ if self._saveSlotDataService:IsActiveSlotEphemeral(player) then
542
+ return self._saveSlotDataService:GetActiveSlotId(player)
543
+ end
544
+ return nil
545
+ end
546
+
547
+ return self._saveSlotDataService:GetSlotIdFromIndex(player, slotIndex)
548
+ end
549
+
492
550
  -- Resolves a slotIndices argument (or nil = the active slot) into de-duplicated { slotIndex, slotId }
493
551
  -- entries for the player. An empty result means nothing matched, and the caller reports it.
494
552
  function SaveSlotCmdrService._resolveSlotEntries(
@@ -507,7 +565,7 @@ function SaveSlotCmdrService._resolveSlotEntries(
507
565
  else
508
566
  local seen = {}
509
567
  for _, slotIndex in slotIndices do
510
- local slotId = self._saveSlotDataService:GetSlotIdFromIndex(player, slotIndex)
568
+ local slotId = self:_getSlotIdFromIndex(player, slotIndex)
511
569
  if slotId and not seen[slotId] then
512
570
  seen[slotId] = true
513
571
  table.insert(entries, { slotIndex = slotIndex, slotId = slotId })
@@ -460,6 +460,20 @@ function SaveSlotService.PromiseResetActiveSlot(self: SaveSlotService, player: P
460
460
  end)
461
461
  end
462
462
 
463
+ --[=[
464
+ Turns the player's ephemeral session into a real save slot and continues play on it, resolving to the
465
+ new slot id. See [HasSaveSlots.PromisePersistEphemeralSlot].
466
+ ]=]
467
+ function SaveSlotService.PromisePersistEphemeralSlot(
468
+ self: SaveSlotService,
469
+ player: Player,
470
+ slotId: SaveSlotData.SlotId?
471
+ ): Promise.Promise<SaveSlotData.SlotId>
472
+ return self._hasSaveSlotsBinder:Promise(player):Then(function(hasSaveSlots)
473
+ return hasSaveSlots:PromisePersistEphemeralSlot(slotId)
474
+ end)
475
+ end
476
+
463
477
  --[=[
464
478
  Exports the player's non-main slot into a serializable table. See [HasSaveSlots.PromiseExportSlot].
465
479
  ]=]
@@ -3,6 +3,10 @@
3
3
  @class SaveSlotCmdrUtils
4
4
  ]=]
5
5
 
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local SaveSlotConstants = require("SaveSlotConstants")
9
+
6
10
  local SaveSlotCmdrUtils = {}
7
11
 
8
12
  function SaveSlotCmdrUtils.registerSlotIndexType(cmdr, saveSlotDataService)
@@ -13,10 +17,36 @@ function SaveSlotCmdrUtils.registerSlotIndexType(cmdr, saveSlotDataService)
13
17
  for _, metadata in slots do
14
18
  table.insert(slotIndices, tostring(metadata.SlotIndex))
15
19
  end
16
- return cmdr.Util.MakeFuzzyFinder(slotIndices)(text)
20
+
21
+ -- The active ephemeral slot is deliberately absent from the slot list -- it is a throwaway
22
+ -- session, not a save -- but it is still the slot the player is actually in, and admin tooling
23
+ -- has to be able to name it (to persist it, export it, or end it). It answers to the reserved
24
+ -- ephemeral index, which no real slot can hold, so it never shadows one. Offering it here is
25
+ -- also what makes "." resolve while a session is active, since Default hands its index back
26
+ -- through this finder.
27
+ if saveSlotDataService:IsActiveSlotEphemeral(player) then
28
+ table.insert(slotIndices, tostring(SaveSlotConstants.EPHEMERAL_SLOT_INDEX))
29
+ end
30
+
31
+ local matches = cmdr.Util.MakeFuzzyFinder(slotIndices)(text)
32
+ if #matches > 0 then
33
+ return matches
34
+ end
35
+
36
+ -- Autocomplete can only ever offer the executor's own slots -- Cmdr resolves types against the
37
+ -- executor -- but these commands target other players, whose slots the executor cannot see. So
38
+ -- any literal index typed out is accepted even when the executor has no such slot, which is what
39
+ -- makes `export-save-slot SomeoneElse 3` possible. A target without that slot is reported by the
40
+ -- command itself, which resolves the index per player.
41
+ local literalIndex = tonumber(text)
42
+ if literalIndex and (literalIndex >= 0) and (literalIndex % 1 == 0) then
43
+ return { tostring(literalIndex) }
44
+ end
45
+
46
+ return matches
17
47
  end,
18
48
  Validate = function(keys)
19
- return #keys > 0, "No matching slot."
49
+ return #keys > 0, "Not a slot index."
20
50
  end,
21
51
  Autocomplete = function(keys)
22
52
  return keys
@@ -13,7 +13,9 @@ local it = Jest.Globals.it
13
13
 
14
14
  local FAKE_PLAYER = newproxy(false)
15
15
 
16
- local function newFakeDataService(state)
16
+ local function newFakeDataService(state: any)
17
+ -- Mirrors the real service: an ephemeral slot is excluded from the slot list but still resolvable as
18
+ -- metadata, which is exactly the split the type has to bridge.
17
19
  return {
18
20
  GetSlotList = function(_self, _player)
19
21
  return state.slots
@@ -21,7 +23,13 @@ local function newFakeDataService(state)
21
23
  GetLastActiveSlotId = function(_self, _player)
22
24
  return state.lastActiveSlotId
23
25
  end,
26
+ IsActiveSlotEphemeral = function(_self, _player)
27
+ return state.ephemeralSlot ~= nil
28
+ end,
24
29
  GetSlotMetadata = function(_self, _player, slotId)
30
+ if state.ephemeralSlot and state.ephemeralSlot.SlotId == slotId then
31
+ return state.ephemeralSlot
32
+ end
25
33
  for _, slot in state.slots do
26
34
  if slot.SlotId == slotId then
27
35
  return slot
@@ -67,7 +75,7 @@ local function newFakeCmdr()
67
75
  return cmdr, registered
68
76
  end
69
77
 
70
- local function registerSlotIndex(state)
78
+ local function registerSlotIndex(state: any)
71
79
  local cmdr, registered = newFakeCmdr()
72
80
  SaveSlotCmdrUtils.registerSlotIndexType(cmdr :: any, newFakeDataService(state))
73
81
  return registered.slotIndex, registered.slotIndices
@@ -118,6 +126,56 @@ describe("SaveSlotCmdrUtils.registerSlotIndexType", function()
118
126
  expect(slotIndex.Parse(slotIndex.Transform("2", FAKE_PLAYER))).toBe(2)
119
127
  end)
120
128
 
129
+ it("suggests the reserved ephemeral index while an ephemeral session is active", function()
130
+ local slotIndex = registerSlotIndex({
131
+ lastActiveSlotId = "id-ephemeral",
132
+ ephemeralSlot = { SlotId = "id-ephemeral", SlotIndex = 0, IsEphemeral = true },
133
+ slots = {
134
+ { SlotId = "id-1", SlotIndex = 1 },
135
+ },
136
+ })
137
+
138
+ -- Transform("") is what "*" and autocomplete expand against.
139
+ expect(slotIndex.Transform("", FAKE_PLAYER)).toEqual({ "1", "0" })
140
+ expect(slotIndex.Default(FAKE_PLAYER)).toBe("0")
141
+ expect(slotIndex.Parse(slotIndex.Transform("0", FAKE_PLAYER))).toBe(0)
142
+ end)
143
+
144
+ it("leaves the ephemeral index out of the suggestions when no session is active", function()
145
+ local slotIndex = registerSlotIndex({
146
+ lastActiveSlotId = "id-1",
147
+ slots = {
148
+ { SlotId = "id-1", SlotIndex = 1 },
149
+ },
150
+ })
151
+
152
+ expect(slotIndex.Transform("", FAKE_PLAYER)).toEqual({ "1" })
153
+ end)
154
+
155
+ it("accepts a literal index the executor has no slot for, so other players can be targeted", function()
156
+ local slotIndex = registerSlotIndex({
157
+ lastActiveSlotId = "id-1",
158
+ slots = {
159
+ { SlotId = "id-1", SlotIndex = 1 },
160
+ },
161
+ })
162
+
163
+ expect((slotIndex.Validate(slotIndex.Transform("3", FAKE_PLAYER)))).toBe(true)
164
+ expect(slotIndex.Parse(slotIndex.Transform("3", FAKE_PLAYER))).toBe(3)
165
+ end)
166
+
167
+ it("rejects text that is not a slot index at all", function()
168
+ local slotIndex = registerSlotIndex({
169
+ lastActiveSlotId = "id-1",
170
+ slots = {
171
+ { SlotId = "id-1", SlotIndex = 1 },
172
+ },
173
+ })
174
+
175
+ expect((slotIndex.Validate(slotIndex.Transform("nope", FAKE_PLAYER)))).toBe(false)
176
+ expect((slotIndex.Validate(slotIndex.Transform("-2", FAKE_PLAYER)))).toBe(false)
177
+ end)
178
+
121
179
  it('exposes Default on the listable type too, so "." works for delete-save-slot', function()
122
180
  local _slotIndex, slotIndices = registerSlotIndex({
123
181
  lastActiveSlotId = "id-1",
@@ -36,6 +36,7 @@ return TieDefinition.new("HasSaveSlots", {
36
36
  PromiseSelectLastSaveSlot = TieDefinition.Types.METHOD,
37
37
  PromiseSelectNewSaveSlot = TieDefinition.Types.METHOD,
38
38
  PromiseSelectEphemeralSlot = TieDefinition.Types.METHOD,
39
+ PromisePersistEphemeralSlot = TieDefinition.Types.METHOD,
39
40
  PromiseDeleteAllSlots = TieDefinition.Types.METHOD,
40
41
 
41
42
  -- Export/import operate on the server's datastore-backed slots and are refused on the main
@@ -16,4 +16,12 @@ return Table.readonly({
16
16
  -- Carries the shared-store key of a transferable ephemeral slot across a teleport (trusted band).
17
17
  TELEPORT_DATA_EPHEMERAL_KEY = "IncomingEphemeralSaveSlotKey",
18
18
  DEFAULT_SLOT_INDEX = 1,
19
+ -- An ephemeral slot carries no meaningful index -- a real slot's index positions it in the save list and
20
+ -- routes its store, neither of which applies to a throwaway slot. The authoritative "is ephemeral"
21
+ -- discriminator is the slot's own IsEphemeral property (see HasSaveSlots._isEphemeral), and every
22
+ -- index-accounting path skips ephemeral slots, so this value never routes anything and any number of
23
+ -- ephemeral slots can coexist. 0 is used because a real index is always >= 1 (DEFAULT_SLOT_INDEX is 1,
24
+ -- PromiseCreateSlot rejects < 1), which also makes it a free address for tooling that has to name the
25
+ -- active ephemeral slot (see SaveSlotCmdrUtils).
26
+ EPHEMERAL_SLOT_INDEX = 0,
19
27
  })