cluaupp 1.5.0 → 1.5.1

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.
Files changed (323) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README.md +10 -5
  3. package/api/CHANGELOG.md +3 -0
  4. package/api/chat.js +74 -0
  5. package/api/fixtures/roblox/README.md +6 -0
  6. package/api/fixtures/roblox/aux/sample.d.luau +6 -0
  7. package/api/fixtures/roblox/mini/Mini-API-Dump.json +72 -0
  8. package/api/generated/bench-latest.json +39 -0
  9. package/api/generated/capability-profile.json +76 -0
  10. package/api/generated/lsp-index.json +1 -0
  11. package/api/generated/luau/classes.d.luau +11221 -0
  12. package/api/generated/luau/datatypes.d.luau +327 -0
  13. package/api/generated/luau/enums.d.luau +4918 -0
  14. package/api/generated/luau/globals.d.luau +12 -0
  15. package/api/manifests/roblox-target.manifest.json +22 -0
  16. package/api/normalized/roblox-target.profile.json +237461 -0
  17. package/api/overrides/classes.json +8 -0
  18. package/api/overrides/compatibility.json +9 -0
  19. package/api/overrides/datatypes.json +1579 -0
  20. package/api/overrides/enums.json +8 -0
  21. package/api/overrides/globals.json +8 -0
  22. package/api/overrides/type-mappings.json +9 -0
  23. package/api/roblox-api.lock.json +9 -0
  24. package/data/Mini-API-Dump.json +1 -0
  25. package/editors/vscode/README.md +12 -10
  26. package/editors/vscode/extension.js +82 -0
  27. package/editors/vscode/icons/axiom.svg +4 -0
  28. package/editors/vscode/icons/bloom.svg +5 -0
  29. package/editors/vscode/icons/helm.svg +4 -0
  30. package/editors/vscode/icons/hive.svg +5 -0
  31. package/editors/vscode/icons/mint.svg +4 -0
  32. package/editors/vscode/icons/shift.svg +6 -0
  33. package/editors/vscode/language-configuration-schema.json +24 -0
  34. package/editors/vscode/package.json +172 -12
  35. package/editors/vscode/snippets/axiom.json +12 -0
  36. package/editors/vscode/snippets/bloom.json +12 -0
  37. package/editors/vscode/snippets/helm.json +12 -0
  38. package/editors/vscode/snippets/hive.json +12 -0
  39. package/editors/vscode/snippets/mint.json +12 -0
  40. package/editors/vscode/snippets/shift.json +17 -0
  41. package/editors/vscode/syntaxes/axiom.tmLanguage.json +5 -0
  42. package/editors/vscode/syntaxes/bloom.tmLanguage.json +5 -0
  43. package/editors/vscode/syntaxes/helm.tmLanguage.json +5 -0
  44. package/editors/vscode/syntaxes/hive.tmLanguage.json +5 -0
  45. package/editors/vscode/syntaxes/mint.tmLanguage.json +5 -0
  46. package/editors/vscode/syntaxes/schema.tmLanguage.json +89 -0
  47. package/editors/vscode/syntaxes/shift.tmLanguage.json +5 -0
  48. package/examples/game/.cluaupp/compile-cache/ReplicatedStorage__Shared__Constants__Datas__TemplateData.clh.json +1 -0
  49. package/examples/game/.cluaupp/compile-cache/ServerScriptService__Boot__DataBoot.server.clpp.json +1 -0
  50. package/examples/game/.cluaupp/compile-cache/ServerScriptService__Configuration__ServerConfig.clp.json +1 -0
  51. package/examples/game/.cluaupp/compile-cache/ServerScriptService__Handlers__Guard.server.clpp.json +1 -0
  52. package/examples/game/.cluaupp/compile-cache/ServerScriptService__Handlers__PlayerHandler.server.clpp.json +1 -0
  53. package/examples/game/.cluaupp/compile-cache/StarterPlayer__StarterCharacterScripts__Character__CharacterReady.client.clpp.json +1 -0
  54. package/examples/game/.cluaupp/compile-cache/StarterPlayer__StarterPlayerScripts__Controllers__DataController.client.clpp.json +1 -0
  55. package/examples/game/.cluaupp/compile-cache/index.json +12 -0
  56. package/examples/game/.cluaupp/datamodel-profile.json +94 -0
  57. package/examples/game/.vscode/settings.json +12 -1
  58. package/examples/game/include/clpp/datatypes.clh +337 -373
  59. package/examples/game/include/clpp/generated/datamodel.clh +25 -0
  60. package/examples/game/include/clpp/generated/enums.clh +1 -1
  61. package/examples/game/include/clpp/generated/instances.clh +41135 -41064
  62. package/examples/game/include/clpp/libs/axiom.clh +1 -1
  63. package/examples/game/include/clpp/libs/math.clh +14 -0
  64. package/examples/game/include/clpp/libs/spring.clh +17 -0
  65. package/examples/game/include/clpp/libs/ward.clh +1 -0
  66. package/examples/game/include/clpp/roblox.clh +3 -4
  67. package/examples/game/libs/ArrayIndexer/init.luau +209 -0
  68. package/examples/game/libs/Axiom/init.luau +608 -0
  69. package/examples/game/libs/Bloom/init.luau +433 -0
  70. package/examples/game/libs/Coil/init.luau +215 -0
  71. package/examples/game/libs/Crest/init.luau +426 -0
  72. package/examples/game/libs/Echo/init.luau +417 -0
  73. package/examples/game/libs/Ember/init.luau +365 -0
  74. package/examples/game/libs/Flare/init.luau +717 -0
  75. package/examples/game/libs/Gleam/init.luau +729 -0
  76. package/examples/game/libs/Guide/init.luau +215 -0
  77. package/examples/game/libs/Helm/init.luau +377 -0
  78. package/examples/game/libs/Hive/init.luau +241 -0
  79. package/examples/game/libs/Keep/Client.luau +114 -0
  80. package/examples/game/libs/Keep/Data.luau +883 -0
  81. package/examples/game/libs/Keep/Engine.luau +211 -0
  82. package/examples/game/libs/Keep/Enum.luau +6 -0
  83. package/examples/game/libs/Keep/Enums.luau +10 -0
  84. package/examples/game/libs/Keep/Path.luau +236 -0
  85. package/examples/game/libs/Keep/Replicator.luau +298 -0
  86. package/examples/game/libs/Keep/Server.luau +251 -0
  87. package/examples/game/libs/Keep/Store.luau +652 -0
  88. package/examples/game/libs/Keep/Trade.luau +455 -0
  89. package/examples/game/libs/Keep/init.luau +205 -0
  90. package/examples/game/libs/Lens/init.luau +609 -0
  91. package/examples/game/libs/Mint/init.luau +279 -0
  92. package/examples/game/libs/Net/init.luau +156 -0
  93. package/examples/game/libs/Net/src/codec.luau +224 -0
  94. package/examples/game/libs/Occlude/init.luau +117 -0
  95. package/examples/game/libs/Pin/init.luau +309 -0
  96. package/examples/game/libs/Promise/LICENSE +21 -0
  97. package/examples/game/libs/Promise/_impl/LICENSE +21 -0
  98. package/examples/game/libs/Promise/_impl/init.lua +1991 -0
  99. package/examples/game/libs/Promise/init.lua +1990 -0
  100. package/examples/game/libs/Promise/init.luau +13 -0
  101. package/examples/game/libs/Promise/types.luau +46 -0
  102. package/examples/game/libs/Roster/init.luau +686 -0
  103. package/examples/game/libs/SOURCES.md +31 -0
  104. package/examples/game/libs/Shift/init.luau +238 -0
  105. package/examples/game/libs/Spark/init.luau +487 -0
  106. package/examples/game/libs/Stage/init.luau +178 -0
  107. package/examples/game/libs/Sweep/init.luau +259 -0
  108. package/examples/game/libs/Trace/init.luau +213 -0
  109. package/examples/game/libs/Ward/init.luau +211 -0
  110. package/examples/game/out/ReplicatedStorage/Shared/Constants/Datas/TemplateData.luau +29 -0
  111. package/examples/game/out/ReplicatedStorage/Shared/Constants/Datas/TemplateData.luau.map.json +13 -0
  112. package/examples/game/out/ReplicatedStorage/Shared/Net/Net.luau +83 -0
  113. package/examples/game/out/ReplicatedStorage/Shared/Net/Net.luau.map.json +13 -0
  114. package/examples/game/out/ServerScriptService/Boot/DataBoot.server.luau +18 -0
  115. package/examples/game/out/ServerScriptService/Boot/DataBoot.server.luau.map.json +13 -0
  116. package/examples/game/out/ServerScriptService/Configuration/ServerConfig.luau +14 -0
  117. package/examples/game/out/ServerScriptService/Configuration/ServerConfig.luau.map.json +13 -0
  118. package/examples/game/out/ServerScriptService/Handlers/Guard.server.luau +15 -0
  119. package/examples/game/out/ServerScriptService/Handlers/Guard.server.luau.map.json +13 -0
  120. package/examples/game/out/ServerScriptService/Handlers/PlayerHandler.server.luau +50 -0
  121. package/examples/game/out/ServerScriptService/Handlers/PlayerHandler.server.luau.map.json +13 -0
  122. package/examples/game/out/StarterPlayer/StarterCharacterScripts/Character/CharacterReady.client.luau +12 -0
  123. package/examples/game/out/StarterPlayer/StarterCharacterScripts/Character/CharacterReady.client.luau.map.json +13 -0
  124. package/examples/game/out/StarterPlayer/StarterPlayerScripts/Controllers/DataController.client.luau +16 -0
  125. package/examples/game/out/StarterPlayer/StarterPlayerScripts/Controllers/DataController.client.luau.map.json +13 -0
  126. package/examples/game/src/ReplicatedStorage/Shared/Net/Net.clh +26 -0
  127. package/examples/game/src/ServerScriptService/Boot/DataBoot.server.clpp +1 -1
  128. package/examples/game/src/ServerScriptService/Handlers/PlayerHandler.server.clpp +1 -1
  129. package/examples/game/stylua.toml +9 -0
  130. package/generated/api/build-profile.js +115 -0
  131. package/generated/api/cache.js +44 -0
  132. package/generated/api/coverage.js +32 -0
  133. package/generated/api/diff.js +125 -0
  134. package/generated/api/generators/clpp-headers.js +353 -0
  135. package/generated/api/generators/docs-reference.js +7 -0
  136. package/generated/api/generators/lsp-index.js +81 -0
  137. package/generated/api/generators/luau-defs.js +94 -0
  138. package/generated/api/ide.js +76 -0
  139. package/generated/api/identifier.js +47 -0
  140. package/generated/api/index.js +324 -0
  141. package/generated/api/loader.js +57 -0
  142. package/generated/api/merge.js +88 -0
  143. package/generated/api/model.js +5 -0
  144. package/generated/api/normalize.js +231 -0
  145. package/generated/api/overrides-validate.js +58 -0
  146. package/generated/api/perf.js +12 -0
  147. package/generated/api/registry.js +136 -0
  148. package/generated/api/release.js +44 -0
  149. package/generated/api/resolver.js +250 -0
  150. package/generated/api/sources/aux-types-diff.js +75 -0
  151. package/generated/api/sources/client-tracker.js +11 -0
  152. package/generated/api/sources/creator-docs.js +42 -0
  153. package/generated/api/sources/luau-types.js +34 -0
  154. package/generated/api/sources/mini-dump.js +17 -0
  155. package/generated/api/sources/studio-api.js +8 -0
  156. package/generated/api/sources/studio-apiv2.js +11 -0
  157. package/generated/api/sources/studio-full-api.js +8 -0
  158. package/generated/api/studio-smoke.js +80 -0
  159. package/generated/api/type-parse.js +49 -0
  160. package/generated/api/understander.js +51 -0
  161. package/generated/api/validate.js +135 -0
  162. package/generated/api-cli.js +421 -0
  163. package/generated/cli.js +16 -5
  164. package/generated/clpp/contract.js +3 -3
  165. package/generated/clpp/format-luau.js +236 -0
  166. package/generated/clpp/index.js +13 -1
  167. package/generated/clpp/modules.js +227 -0
  168. package/generated/clpp/postprocess.js +86 -5
  169. package/generated/clpp/rewrite-janitor.js +145 -0
  170. package/generated/clpp/runner.js +14 -2
  171. package/generated/compile.js +82 -0
  172. package/generated/flare/parse.js +12 -1
  173. package/generated/intellisense.js +57 -1
  174. package/generated/native/index.js +217 -16
  175. package/generated/target/authority-check.js +136 -0
  176. package/generated/target/authority-graph.js +74 -0
  177. package/generated/target/build-cache.js +112 -0
  178. package/generated/target/capabilities.js +89 -0
  179. package/generated/target/capability-profile.js +30 -0
  180. package/generated/target/component-contracts.js +60 -0
  181. package/generated/target/context-check.js +160 -0
  182. package/generated/target/datamodel-check.js +170 -0
  183. package/generated/target/datamodel-generate.js +74 -0
  184. package/generated/target/datamodel.js +233 -0
  185. package/generated/target/diagnostics.js +63 -0
  186. package/generated/target/doctor.js +152 -0
  187. package/generated/target/flare-version.js +58 -0
  188. package/generated/target/get-service.js +69 -0
  189. package/generated/target/lifetime-check.js +47 -0
  190. package/generated/target/luau-stamp.js +39 -0
  191. package/generated/target/optimizer-advise.js +88 -0
  192. package/generated/target/optimizer-apply.js +44 -0
  193. package/generated/target/optimizer-soa.js +254 -0
  194. package/generated/target/parallel-check.js +136 -0
  195. package/generated/target/pgo.js +148 -0
  196. package/generated/target/project-docs.js +69 -0
  197. package/generated/target/project-graph.js +71 -0
  198. package/generated/target/roblox.js +63 -0
  199. package/generated/target/security-check.js +41 -0
  200. package/generated/target/source-map.js +25 -0
  201. package/generated/target/studio-bridge.js +112 -0
  202. package/generated/utils/project.js +178 -8
  203. package/generated/utils/rojo-mapper.js +11 -3
  204. package/include/clpp/datatypes.clh +337 -373
  205. package/include/clpp/generated/enums.clh +1 -1
  206. package/include/clpp/generated/instances.clh +41135 -41064
  207. package/include/clpp/libs/axiom.clh +1 -1
  208. package/include/clpp/libs/hive.clh +1 -0
  209. package/include/clpp/libs/keep.clh +2 -0
  210. package/include/clpp/libs/math.clh +14 -0
  211. package/include/clpp/libs/spring.clh +17 -0
  212. package/include/clpp/libs/ward.clh +1 -0
  213. package/include/clpp/roblox.clh +3 -4
  214. package/package.json +10 -3
  215. package/plugins/CluauppNav/Plugin.luau +207 -0
  216. package/plugins/CluauppNav/ProfileLogger.luau +27 -0
  217. package/plugins/CluauppNav/README.md +24 -0
  218. package/runtime/Axiom/init.luau +10 -7
  219. package/runtime/Coil/init.luau +8 -2
  220. package/runtime/Crest/init.luau +1 -0
  221. package/runtime/Flare/init.luau +2 -1
  222. package/runtime/Gleam/init.luau +15 -4
  223. package/runtime/Hive/init.luau +36 -14
  224. package/runtime/Pin/init.luau +5 -2
  225. package/runtime/Roster/init.luau +2 -0
  226. package/runtime/Spark/init.luau +3 -0
  227. package/runtime/Sweep/init.luau +2 -0
  228. package/runtime/Trace/init.luau +3 -0
  229. package/runtime/Ward/init.luau +38 -5
  230. package/scripts/postinstall-editor.js +17 -0
  231. package/skills/README.md +13 -0
  232. package/skills/clpp-hive-flare/SKILL.md +79 -0
  233. package/skills/clpp-language/SKILL.md +123 -0
  234. package/skills/clpp-language/compiler.md +89 -0
  235. package/skills/clpp-style/SKILL.md +95 -0
  236. package/src/api/build-profile.ts +138 -0
  237. package/src/api/cache.ts +45 -0
  238. package/src/api/coverage.ts +46 -0
  239. package/src/api/diff.ts +143 -0
  240. package/src/api/generators/clpp-headers.ts +377 -0
  241. package/src/api/generators/docs-reference.ts +11 -0
  242. package/src/api/generators/lsp-index.ts +106 -0
  243. package/src/api/generators/luau-defs.ts +98 -0
  244. package/src/api/ide.ts +87 -0
  245. package/src/api/identifier.ts +47 -0
  246. package/src/api/index.ts +269 -0
  247. package/src/api/loader.ts +60 -0
  248. package/src/api/merge.ts +126 -0
  249. package/src/api/model.ts +283 -0
  250. package/src/api/normalize.ts +257 -0
  251. package/src/api/overrides-validate.ts +58 -0
  252. package/src/api/perf.ts +15 -0
  253. package/src/api/registry.ts +162 -0
  254. package/src/api/release.ts +47 -0
  255. package/src/api/resolver.ts +255 -0
  256. package/src/api/sources/aux-types-diff.ts +81 -0
  257. package/src/api/sources/client-tracker.ts +10 -0
  258. package/src/api/sources/creator-docs.ts +37 -0
  259. package/src/api/sources/luau-types.ts +30 -0
  260. package/src/api/sources/mini-dump.ts +50 -0
  261. package/src/api/sources/studio-api.ts +7 -0
  262. package/src/api/sources/studio-apiv2.ts +10 -0
  263. package/src/api/sources/studio-full-api.ts +7 -0
  264. package/src/api/studio-smoke.ts +82 -0
  265. package/src/api/type-parse.ts +48 -0
  266. package/src/api/understander.ts +72 -0
  267. package/src/api/validate.ts +160 -0
  268. package/src/api-cli.ts +493 -0
  269. package/src/api.generated.js +37 -22
  270. package/src/cli.ts +21 -6
  271. package/src/clpp/contract.ts +34 -3
  272. package/src/clpp/format-luau.ts +262 -0
  273. package/src/clpp/index.ts +13 -2
  274. package/src/clpp/modules.ts +252 -0
  275. package/src/clpp/postprocess.ts +100 -5
  276. package/src/clpp/rewrite-janitor.ts +156 -0
  277. package/src/clpp/runner.ts +14 -2
  278. package/src/compile.ts +87 -0
  279. package/src/flare/parse.ts +14 -1
  280. package/src/flare/types.ts +2 -0
  281. package/src/intellisense.ts +58 -1
  282. package/src/native/index.ts +225 -16
  283. package/src/target/authority-check.ts +162 -0
  284. package/src/target/authority-graph.ts +80 -0
  285. package/src/target/build-cache.ts +129 -0
  286. package/src/target/capabilities.ts +115 -0
  287. package/src/target/capability-profile.ts +25 -0
  288. package/src/target/component-contracts.ts +64 -0
  289. package/src/target/context-check.ts +177 -0
  290. package/src/target/datamodel-check.ts +188 -0
  291. package/src/target/datamodel-generate.ts +78 -0
  292. package/src/target/datamodel.ts +261 -0
  293. package/src/target/diagnostics.ts +67 -0
  294. package/src/target/doctor.ts +171 -0
  295. package/src/target/flare-version.ts +61 -0
  296. package/src/target/get-service.ts +85 -0
  297. package/src/target/lifetime-check.ts +45 -0
  298. package/src/target/luau-stamp.ts +33 -0
  299. package/src/target/optimizer-advise.ts +93 -0
  300. package/src/target/optimizer-apply.ts +63 -0
  301. package/src/target/optimizer-soa.ts +270 -0
  302. package/src/target/parallel-check.ts +135 -0
  303. package/src/target/pgo.ts +157 -0
  304. package/src/target/project-docs.ts +68 -0
  305. package/src/target/project-graph.ts +75 -0
  306. package/src/target/roblox.ts +78 -0
  307. package/src/target/security-check.ts +40 -0
  308. package/src/target/source-map.ts +36 -0
  309. package/src/target/studio-bridge.ts +140 -0
  310. package/src/types.ts +14 -0
  311. package/src/utils/project.ts +183 -9
  312. package/src/utils/rojo-mapper.ts +12 -3
  313. package/stylua.toml +9 -0
  314. package/templates/game/.vscode/settings.json +7 -1
  315. package/templates/game/include/clpp/datatypes.clh +337 -373
  316. package/templates/game/include/clpp/generated/enums.clh +1 -1
  317. package/templates/game/include/clpp/generated/instances.clh +41135 -41064
  318. package/templates/game/include/clpp/libs/axiom.clh +1 -1
  319. package/templates/game/include/clpp/libs/ward.clh +1 -0
  320. package/templates/game/include/clpp/roblox.clh +3 -4
  321. package/templates/game/src/ServerScriptService/Boot/DataBoot.server.clpp +1 -1
  322. package/templates/game/src/ServerScriptService/Handlers/PlayerHandler.server.clpp +1 -1
  323. package/templates/game/stylua.toml +9 -0
@@ -0,0 +1,652 @@
1
+ --!optimize 2
2
+ --!strict
3
+ -- Keep Store — ProfileStore session contract (MadStudio algorithm), PascalCase, Spark signals.
4
+ -- Improvements: trade messages are first-class; UpdateAsync is serialized per key;
5
+ -- LastSavedData is the receipt source of truth; Mock never touches live DataStores.
6
+
7
+ local DataStoreService = game:GetService("DataStoreService")
8
+ local MessagingService = game:GetService("MessagingService")
9
+ local HttpService = game:GetService("HttpService")
10
+ local RunService = game:GetService("RunService")
11
+
12
+ local Spark = require(script.Parent.Parent.Spark)
13
+ local Trade = require(script.Parent.Trade)
14
+
15
+ local AUTO_SAVE_PERIOD = 300
16
+ local LOAD_REPEAT_PERIOD = 10
17
+ local FIRST_LOAD_REPEAT = 5
18
+ local SESSION_STEAL = 40
19
+ local ASSUME_DEAD = 630
20
+ local START_SESSION_TIMEOUT = 120
21
+ local CRITICAL_STATE_ERROR_COUNT = 5
22
+ local CRITICAL_STATE_ERROR_EXPIRE = 120
23
+ local CRITICAL_STATE_EXPIRE = 120
24
+ local MAX_MESSAGE_QUEUE = 1000
25
+
26
+ local PlaceId = game.PlaceId
27
+ local JobId = if game.JobId ~= "" then game.JobId else "Studio"
28
+ local IsStudio = RunService:IsStudio()
29
+
30
+ local ActiveSessionCheck: { [string]: any } = {}
31
+ local AutoSaveList: { any } = {}
32
+ local AutoSaveIndex = 1
33
+ local LastAutoSave = os.clock()
34
+ local IssueQueue: { number } = {}
35
+ local UpdateQueue: { [string]: { any } } = {}
36
+ local ActiveProfileLoadJobs = 0
37
+ local ActiveProfileSaveJobs = 0
38
+ local CriticalStateStart = 0
39
+ local MockStore: { [string]: any } = {}
40
+ local UserMockStore: { [string]: any } = {}
41
+ local MockFlag = false
42
+
43
+ local Store = {}
44
+ Store.__index = Store
45
+ Store.IsClosing = false
46
+ Store.IsCriticalState = false
47
+ Store.OnError = Spark.new()
48
+ Store.OnOverwrite = Spark.new()
49
+ Store.OnCriticalToggle = Spark.new()
50
+ Store.DataStoreState = "NotReady"
51
+
52
+ local function deepCopy(value: any): any
53
+ if typeof(value) ~= "table" then
54
+ return value
55
+ end
56
+ local out = {}
57
+ for key, item in value do
58
+ out[key] = deepCopy(item)
59
+ end
60
+ return out
61
+ end
62
+
63
+ local function reconcile(data: any, template: any)
64
+ if typeof(template) ~= "table" then
65
+ return data
66
+ end
67
+ if typeof(data) ~= "table" then
68
+ return deepCopy(template)
69
+ end
70
+ for key, value in template do
71
+ if data[key] == nil then
72
+ data[key] = deepCopy(value)
73
+ elseif typeof(value) == "table" then
74
+ reconcile(data[key], value)
75
+ end
76
+ end
77
+ return data
78
+ end
79
+
80
+ local function thisSession(session: any): boolean
81
+ return typeof(session) == "table" and session.PlaceId == PlaceId and session.JobId == JobId
82
+ end
83
+
84
+ local function sessionToken(storeName: string, key: string): string
85
+ return storeName .. "/" .. key
86
+ end
87
+
88
+ local function uniqueId(): string
89
+ return HttpService:GenerateGUID(false)
90
+ end
91
+
92
+ local function requestEnd(session: any)
93
+ if typeof(session) ~= "table" or typeof(session.UniqueSessionId) ~= "string" or session.UniqueSessionId == "" then
94
+ return
95
+ end
96
+ pcall(function()
97
+ MessagingService:PublishAsync("Keep_" .. session.UniqueSessionId, { EndSession = true })
98
+ end)
99
+ end
100
+
101
+ local function registerIssue()
102
+ local now = os.clock()
103
+ table.insert(IssueQueue, now)
104
+ while IssueQueue[1] and now - IssueQueue[1] > CRITICAL_STATE_ERROR_EXPIRE do
105
+ table.remove(IssueQueue, 1)
106
+ end
107
+ if #IssueQueue >= CRITICAL_STATE_ERROR_COUNT then
108
+ Store.IsCriticalState = true
109
+ CriticalStateStart = now
110
+ Store.OnCriticalToggle:Fire(true)
111
+ end
112
+ end
113
+
114
+ local function emptyPayload(template: any)
115
+ return {
116
+ Data = deepCopy(template),
117
+ MetaData = {
118
+ ProfileCreateTime = os.time(),
119
+ SessionLoadCount = 0,
120
+ ActiveSession = nil,
121
+ ForceLoadSession = nil,
122
+ LastUpdate = os.time(),
123
+ UniqueSessionId = "",
124
+ },
125
+ RobloxMetaData = {},
126
+ UserIds = {},
127
+ GlobalUpdates = { 0, {} },
128
+ }
129
+ end
130
+
131
+ local function mockBucket(userMock: boolean)
132
+ return if userMock then UserMockStore else MockStore
133
+ end
134
+
135
+ local function readMock(userMock: boolean, name: string, key: string)
136
+ local bucket = mockBucket(userMock)
137
+ bucket[name] = bucket[name] or {}
138
+ return bucket[name][key]
139
+ end
140
+
141
+ local function writeMock(userMock: boolean, name: string, key: string, payload: any)
142
+ local bucket = mockBucket(userMock)
143
+ bucket[name] = bucket[name] or {}
144
+ bucket[name][key] = payload
145
+ return payload
146
+ end
147
+
148
+ local function waitQueue(token: string)
149
+ UpdateQueue[token] = UpdateQueue[token] or {}
150
+ local waiters = UpdateQueue[token]
151
+ local thread = coroutine.running()
152
+ table.insert(waiters, thread)
153
+ if waiters[1] ~= thread then
154
+ coroutine.yield()
155
+ end
156
+ end
157
+
158
+ local function releaseQueue(token: string)
159
+ local waiters = UpdateQueue[token]
160
+ if not waiters then
161
+ return
162
+ end
163
+ table.remove(waiters, 1)
164
+ local nextThread = waiters[1]
165
+ if nextThread then
166
+ task.spawn(nextThread)
167
+ else
168
+ UpdateQueue[token] = nil
169
+ end
170
+ end
171
+
172
+ local function transformUpdate(store: any, key: string, transform: (any) -> any): (any, any)
173
+ local token = sessionToken(store.Name, key)
174
+ waitQueue(token)
175
+ local payload
176
+ local keyInfo
177
+ if store._store and not store._mockFlag then
178
+ local ok, result, info = pcall(function()
179
+ return store._store:UpdateAsync(key, function(old, info)
180
+ keyInfo = info
181
+ return transform(old)
182
+ end)
183
+ end)
184
+ if ok then
185
+ payload = result
186
+ else
187
+ registerIssue()
188
+ Store.OnError:Fire(tostring(result), store.Name, key)
189
+ end
190
+ else
191
+ local old = readMock(store._userMock, store.Name, key)
192
+ payload = transform(old)
193
+ if payload ~= nil then
194
+ writeMock(store._userMock, store.Name, key, payload)
195
+ end
196
+ end
197
+ releaseQueue(token)
198
+ return payload, keyInfo
199
+ end
200
+
201
+ local Profile = {}
202
+ Profile.__index = Profile
203
+
204
+ function Profile:IsActive(): boolean
205
+ return ActiveSessionCheck[self._token] == self and not Store.IsClosing
206
+ end
207
+
208
+ function Profile:Reconcile()
209
+ self.Data = reconcile(self.Data, self._store.Template)
210
+ end
211
+
212
+ function Profile:AddUserId(userId: number)
213
+ if typeof(userId) ~= "number" then
214
+ return
215
+ end
216
+ if table.find(self.UserIds, userId) == nil then
217
+ table.insert(self.UserIds, userId)
218
+ end
219
+ end
220
+
221
+ function Profile:RemoveUserId(userId: number)
222
+ local index = table.find(self.UserIds, userId)
223
+ if index then
224
+ table.remove(self.UserIds, index)
225
+ end
226
+ end
227
+
228
+ function Profile:MessageHandler(fn: (any, () -> ()) -> ())
229
+ table.insert(self._handlers, fn)
230
+ for _, update in self._updates do
231
+ local index = update[1]
232
+ if not self._locked[index] then
233
+ local processed = false
234
+ fn(deepCopy(update[2]), function()
235
+ processed = true
236
+ self._locked[index] = true
237
+ end)
238
+ if processed then
239
+ break
240
+ end
241
+ end
242
+ end
243
+ end
244
+
245
+ local saveProfileAsync
246
+
247
+ function Profile:Save()
248
+ if self._view then
249
+ error("Keep: Save is not valid in view mode; use SetAsync")
250
+ end
251
+ if not self:IsActive() then
252
+ return
253
+ end
254
+ task.spawn(saveProfileAsync, self, false, "Manual")
255
+ end
256
+
257
+ function Profile:SaveWait(): boolean
258
+ if self._view then
259
+ error("Keep: SaveWait is not valid in view mode; use SetAsync")
260
+ end
261
+ while self._saving do
262
+ task.wait()
263
+ end
264
+ if not self:IsActive() then
265
+ return false
266
+ end
267
+ return saveProfileAsync(self, false, "Manual") == true
268
+ end
269
+
270
+ function Profile:SetAsync()
271
+ if not self._view then
272
+ error("Keep: SetAsync is only valid in view mode")
273
+ end
274
+ saveProfileAsync(self, true, "Manual")
275
+ end
276
+
277
+ function Profile:EndSession()
278
+ if self._view then
279
+ return
280
+ end
281
+ saveProfileAsync(self, true, "Manual")
282
+ end
283
+
284
+ saveProfileAsync = function(profile: any, ending: boolean, reason: string): boolean
285
+ while profile._saving do
286
+ task.wait()
287
+ end
288
+ if profile._ended then
289
+ return false
290
+ end
291
+ if ending then
292
+ if not profile._view then
293
+ Trade.AbortIfActive(profile)
294
+ end
295
+ elseif not profile:IsActive() then
296
+ return false
297
+ end
298
+ profile._saving = true
299
+ ActiveProfileSaveJobs += 1
300
+ profile.OnSave:Fire()
301
+ if ending then
302
+ profile.OnLastSave:Fire(if Store.IsClosing then "Shutdown" else reason)
303
+ end
304
+ local stealRequested = false
305
+ local payload = transformUpdate(profile._store, profile.Key, function(old)
306
+ old = old or emptyPayload(profile._store.Template)
307
+ old.MetaData = old.MetaData or {}
308
+ local incoming = (old.GlobalUpdates and old.GlobalUpdates[2]) or {}
309
+ for _, update in incoming do
310
+ local index = update[1]
311
+ local exists = false
312
+ for _, have in profile._updates do
313
+ if have[1] == index then
314
+ exists = true
315
+ break
316
+ end
317
+ end
318
+ if not exists then
319
+ table.insert(profile._updates, update)
320
+ profile._updateIndex = math.max(profile._updateIndex, index or 0)
321
+ end
322
+ end
323
+ for _, update in profile._updates do
324
+ if not profile._locked[update[1]] then
325
+ for _, fn in profile._handlers do
326
+ local processed = false
327
+ fn(deepCopy(update[2]), function()
328
+ processed = true
329
+ profile._locked[update[1]] = true
330
+ end)
331
+ if processed then
332
+ break
333
+ end
334
+ end
335
+ end
336
+ end
337
+ local remaining = {}
338
+ for _, update in profile._updates do
339
+ if not profile._locked[update[1]] then
340
+ table.insert(remaining, update)
341
+ end
342
+ end
343
+ profile._updates = remaining
344
+ stealRequested = typeof(old.MetaData.ForceLoadSession) == "table" and not thisSession(old.MetaData.ForceLoadSession)
345
+ if ending or stealRequested then
346
+ if thisSession(old.MetaData.ActiveSession) then
347
+ old.MetaData.ActiveSession = nil
348
+ end
349
+ else
350
+ old.MetaData.ActiveSession = {
351
+ PlaceId = PlaceId,
352
+ JobId = JobId,
353
+ UniqueSessionId = profile._sessionId,
354
+ }
355
+ end
356
+ old.Data = profile.Data
357
+ old.RobloxMetaData = profile.RobloxMetaData
358
+ old.UserIds = profile.UserIds
359
+ old.MetaData.LastUpdate = os.time()
360
+ old.GlobalUpdates = { profile._updateIndex, remaining }
361
+ return old
362
+ end)
363
+ if payload then
364
+ profile.LastSavedData = deepCopy(payload.Data)
365
+ profile.OnAfterSave:Fire(profile.LastSavedData)
366
+ end
367
+ if ending or stealRequested then
368
+ if not profile._view then
369
+ profile._ended = true
370
+ ActiveSessionCheck[profile._token] = nil
371
+ for index, item in AutoSaveList do
372
+ if item == profile then
373
+ table.remove(AutoSaveList, index)
374
+ break
375
+ end
376
+ end
377
+ profile.OnSessionEnd:Fire()
378
+ end
379
+ end
380
+ profile._saving = false
381
+ ActiveProfileSaveJobs -= 1
382
+ return payload ~= nil
383
+ end
384
+
385
+ local function attachAutosave(profile: any)
386
+ table.insert(AutoSaveList, profile)
387
+ end
388
+
389
+ local function makeProfile(store: any, key: string, payload: any, view: boolean)
390
+ local token = sessionToken(store.Name, key)
391
+ local profile = setmetatable({
392
+ Data = payload.Data,
393
+ LastSavedData = deepCopy(payload.Data),
394
+ FirstSessionTime = payload.MetaData.ProfileCreateTime or os.time(),
395
+ SessionLoadCount = payload.MetaData.SessionLoadCount or 0,
396
+ Session = payload.MetaData.ActiveSession,
397
+ RobloxMetaData = payload.RobloxMetaData or {},
398
+ UserIds = payload.UserIds or {},
399
+ Key = key,
400
+ IsActive = Profile.IsActive,
401
+ _store = store,
402
+ _token = token,
403
+ _view = view,
404
+ _sessionId = payload.MetaData.UniqueSessionId or uniqueId(),
405
+ _handlers = {},
406
+ _locked = {},
407
+ _updates = (payload.GlobalUpdates and payload.GlobalUpdates[2]) or {},
408
+ _updateIndex = (payload.GlobalUpdates and payload.GlobalUpdates[1]) or 0,
409
+ _saving = false,
410
+ _ended = false,
411
+ OnSave = Spark.new(),
412
+ OnLastSave = Spark.new(),
413
+ OnSessionEnd = Spark.new(),
414
+ OnAfterSave = Spark.new(),
415
+ }, Profile)
416
+ profile.ProfileStore = store
417
+ return profile
418
+ end
419
+
420
+ function Store.SetConstant(name: string, value: number)
421
+ if name == "AUTO_SAVE_PERIOD" then
422
+ AUTO_SAVE_PERIOD = value
423
+ elseif name == "LOAD_REPEAT_PERIOD" then
424
+ LOAD_REPEAT_PERIOD = value
425
+ elseif name == "FIRST_LOAD_REPEAT" then
426
+ FIRST_LOAD_REPEAT = value
427
+ elseif name == "SESSION_STEAL" then
428
+ SESSION_STEAL = value
429
+ elseif name == "ASSUME_DEAD" then
430
+ ASSUME_DEAD = value
431
+ elseif name == "START_SESSION_TIMEOUT" then
432
+ START_SESSION_TIMEOUT = value
433
+ elseif name == "MAX_MESSAGE_QUEUE" then
434
+ MAX_MESSAGE_QUEUE = value
435
+ end
436
+ end
437
+
438
+ function Store.New(name: string, template: any)
439
+ local self = setmetatable({
440
+ Name = name,
441
+ Template = template or {},
442
+ _store = nil :: GlobalDataStore?,
443
+ _mockFlag = false,
444
+ _userMock = false,
445
+ Mock = nil :: any,
446
+ }, Store)
447
+ local ok = pcall(function()
448
+ self._store = DataStoreService:GetDataStore("Keep_" .. name)
449
+ Store.DataStoreState = "Access"
450
+ end)
451
+ if not ok or self._store == nil then
452
+ self._mockFlag = IsStudio
453
+ Store.DataStoreState = if IsStudio then "Access" else "NoAccess"
454
+ end
455
+ self.Mock = setmetatable({
456
+ Name = name,
457
+ Template = self.Template,
458
+ _store = nil,
459
+ _mockFlag = true,
460
+ _userMock = true,
461
+ Mock = nil,
462
+ }, Store)
463
+ self.Mock.Mock = self.Mock
464
+ return self
465
+ end
466
+
467
+ function Store:StartSessionAsync(key: string, params: any)
468
+ params = params or {}
469
+ local steal = params.Steal == true
470
+ local cancel = params.Cancel
471
+ local started = os.clock()
472
+ local firstWait = true
473
+ ActiveProfileLoadJobs += 1
474
+ local profile
475
+ while true do
476
+ if typeof(cancel) == "function" and cancel() then
477
+ break
478
+ end
479
+ if os.clock() - started > START_SESSION_TIMEOUT then
480
+ break
481
+ end
482
+ local sessionId = uniqueId()
483
+ local payload = transformUpdate(self, key, function(old)
484
+ old = old or emptyPayload(self.Template)
485
+ old.MetaData = old.MetaData or {}
486
+ local lock = old.MetaData.ActiveSession
487
+ local last = old.MetaData.LastUpdate or 0
488
+ local dead = os.time() - last >= ASSUME_DEAD
489
+ if lock and not thisSession(lock) and not dead and not steal then
490
+ old.MetaData.ForceLoadSession = { PlaceId = PlaceId, JobId = JobId }
491
+ return old
492
+ end
493
+ old.Data = reconcile(old.Data or {}, self.Template)
494
+ old.MetaData.SessionLoadCount = (old.MetaData.SessionLoadCount or 0) + 1
495
+ old.MetaData.UniqueSessionId = sessionId
496
+ old.MetaData.ActiveSession = { PlaceId = PlaceId, JobId = JobId, UniqueSessionId = sessionId }
497
+ old.MetaData.ForceLoadSession = nil
498
+ old.MetaData.LastUpdate = os.time()
499
+ old.GlobalUpdates = old.GlobalUpdates or { 0, {} }
500
+ return old
501
+ end)
502
+ if payload and thisSession(payload.MetaData.ActiveSession) then
503
+ profile = makeProfile(self, key, payload, false)
504
+ ActiveSessionCheck[profile._token] = profile
505
+ attachAutosave(profile)
506
+ pcall(function()
507
+ MessagingService:SubscribeAsync("Keep_" .. profile._sessionId, function(message)
508
+ if typeof(message.Data) == "table" and message.Data.EndSession and profile:IsActive() then
509
+ saveProfileAsync(profile, true, "External")
510
+ end
511
+ end)
512
+ end)
513
+ break
514
+ end
515
+ if payload then
516
+ requestEnd(payload.MetaData.ActiveSession)
517
+ end
518
+ task.wait(if firstWait then FIRST_LOAD_REPEAT else LOAD_REPEAT_PERIOD)
519
+ firstWait = false
520
+ if os.clock() - started > SESSION_STEAL then
521
+ steal = true
522
+ end
523
+ end
524
+ ActiveProfileLoadJobs -= 1
525
+ return profile
526
+ end
527
+
528
+ function Store:GetAsync(key: string): any
529
+ local payload
530
+ if self._store and not self._mockFlag then
531
+ local ok, result = pcall(function()
532
+ return self._store:GetAsync(key)
533
+ end)
534
+ if ok then
535
+ payload = result
536
+ end
537
+ else
538
+ payload = readMock(self._userMock, self.Name, key)
539
+ end
540
+ if not payload then
541
+ return nil
542
+ end
543
+ return makeProfile(self, key, payload, true)
544
+ end
545
+
546
+ function Store:MessageAsync(key: string, message: any): boolean
547
+ if typeof(message) ~= "table" then
548
+ return false
549
+ end
550
+ local payload = transformUpdate(self, key, function(old)
551
+ old = old or emptyPayload(self.Template)
552
+ old.GlobalUpdates = old.GlobalUpdates or { 0, {} }
553
+ local index = (old.GlobalUpdates[1] or 0) + 1
554
+ old.GlobalUpdates[1] = index
555
+ old.GlobalUpdates[2] = old.GlobalUpdates[2] or {}
556
+ if #old.GlobalUpdates[2] >= MAX_MESSAGE_QUEUE then
557
+ table.remove(old.GlobalUpdates[2], 1)
558
+ end
559
+ table.insert(old.GlobalUpdates[2], { index, message })
560
+ return old
561
+ end)
562
+ return payload ~= nil
563
+ end
564
+
565
+ function Store:RemoveAsync(key: string): boolean
566
+ if self._store and not self._mockFlag then
567
+ local ok = pcall(function()
568
+ self._store:RemoveAsync(key)
569
+ end)
570
+ return ok
571
+ end
572
+ local bucket = mockBucket(self._userMock)
573
+ if bucket[self.Name] then
574
+ bucket[self.Name][key] = nil
575
+ end
576
+ return true
577
+ end
578
+
579
+ function Store:VersionQuery(key: string, sortDirection: any, minDate: any, maxDate: any)
580
+ local versions = {}
581
+ local cursor = 0
582
+ if self._store and not self._mockFlag then
583
+ pcall(function()
584
+ local pages = self._store:ListVersionsAsync(key, sortDirection, minDate, maxDate)
585
+ if pages then
586
+ for _, item in pages:GetCurrentPage() do
587
+ table.insert(versions, item)
588
+ end
589
+ end
590
+ end)
591
+ end
592
+ return {
593
+ Get = function(_self, index: number)
594
+ return versions[index]
595
+ end,
596
+ List = function()
597
+ return versions
598
+ end,
599
+ Next = function()
600
+ cursor += 1
601
+ return versions[cursor]
602
+ end,
603
+ NextAsync = function()
604
+ cursor += 1
605
+ local info = versions[cursor]
606
+ if not info then
607
+ return nil
608
+ end
609
+ return self:GetAsync(key)
610
+ end,
611
+ }
612
+ end
613
+
614
+ if RunService:IsServer() then
615
+ task.spawn(function()
616
+ while true do
617
+ task.wait(1)
618
+ if Store.IsCriticalState and os.clock() - CriticalStateStart > CRITICAL_STATE_EXPIRE then
619
+ Store.IsCriticalState = false
620
+ Store.OnCriticalToggle:Fire(false)
621
+ end
622
+ if os.clock() - LastAutoSave >= 1 and #AutoSaveList > 0 then
623
+ local period = AUTO_SAVE_PERIOD / math.max(#AutoSaveList, 1)
624
+ if os.clock() - LastAutoSave >= period then
625
+ LastAutoSave = os.clock()
626
+ local profile = AutoSaveList[AutoSaveIndex]
627
+ AutoSaveIndex += 1
628
+ if AutoSaveIndex > #AutoSaveList then
629
+ AutoSaveIndex = 1
630
+ end
631
+ if profile and profile:IsActive() then
632
+ task.spawn(saveProfileAsync, profile, false, "Manual")
633
+ end
634
+ end
635
+ end
636
+ end
637
+ end)
638
+ game:BindToClose(function()
639
+ Store.IsClosing = true
640
+ for _, profile in table.clone(AutoSaveList) do
641
+ if profile:IsActive() then
642
+ saveProfileAsync(profile, true, "Shutdown")
643
+ end
644
+ end
645
+ local deadline = os.clock() + 20
646
+ while (ActiveProfileSaveJobs > 0 or ActiveProfileLoadJobs > 0) and os.clock() < deadline do
647
+ task.wait()
648
+ end
649
+ end)
650
+ end
651
+
652
+ return Store