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,455 @@
1
+ --!optimize 2
2
+ --!strict
3
+ -- Keep Trade — true escrow. Spendable balance drops on Reserve; Commit only
4
+ -- credits with receipts. Same-server SaveWait both sides; rollback if a save
5
+ -- fails so a crash cannot duplicate. Offline credits are idempotent.
6
+
7
+ local HttpService = game:GetService("HttpService")
8
+
9
+ local MAX_TRADES_PER_DAY = 40
10
+ local TRADE_COOLDOWN = 8
11
+ local MAX_OFFERS = 12
12
+ local MAX_AMOUNT = 1e12
13
+ local MAX_RECEIPTS = 128
14
+ local MAX_RECOVERY = 16
15
+ local KINDS = { Currency = true, Stack = true, Instance = true }
16
+
17
+ local Trade = {}
18
+
19
+ local function overlay(dest: any, src: any)
20
+ if typeof(dest) ~= "table" or typeof(src) ~= "table" then
21
+ return
22
+ end
23
+ for key in dest do
24
+ dest[key] = nil
25
+ end
26
+ for key, value in src do
27
+ dest[key] = value
28
+ end
29
+ end
30
+
31
+ local function deepCopy(value: any): any
32
+ if typeof(value) ~= "table" then
33
+ return value
34
+ end
35
+ local out = {}
36
+ for key, item in value do
37
+ out[key] = deepCopy(item)
38
+ end
39
+ return out
40
+ end
41
+
42
+ local function tradeBucket(data: any)
43
+ data.Trade = data.Trade or {
44
+ DayKey = "",
45
+ TradesToday = 0,
46
+ LastTradeAt = 0,
47
+ ActiveTradeId = "",
48
+ RecoveryTradeIds = {},
49
+ Reserved = {},
50
+ Receipts = {},
51
+ }
52
+ data.Trade.Reserved = data.Trade.Reserved or {}
53
+ data.Trade.Receipts = data.Trade.Receipts or {}
54
+ data.Trade.RecoveryTradeIds = data.Trade.RecoveryTradeIds or {}
55
+ return data.Trade
56
+ end
57
+
58
+ local function opId(): string
59
+ return HttpService:GenerateGUID(false)
60
+ end
61
+
62
+ local function dayKey(): string
63
+ return os.date("!%Y-%m-%d")
64
+ end
65
+
66
+ local function reserveKey(kind: string, ref: string): string
67
+ return kind .. "|" .. ref
68
+ end
69
+
70
+ local function pruneReceipts(trade: any)
71
+ local receipts = trade.Receipts
72
+ local n = 0
73
+ for _ in receipts do
74
+ n += 1
75
+ end
76
+ if n <= MAX_RECEIPTS then
77
+ return
78
+ end
79
+ local keys = {}
80
+ for key in receipts do
81
+ table.insert(keys, key)
82
+ end
83
+ local drop = n - MAX_RECEIPTS
84
+ for i = 1, drop do
85
+ receipts[keys[i]] = nil
86
+ end
87
+ end
88
+
89
+ local function inventoryItems(data: any): { any }
90
+ data.Inventory = data.Inventory or { Items = {} }
91
+ data.Inventory.Items = data.Inventory.Items or {}
92
+ return data.Inventory.Items
93
+ end
94
+
95
+ local function validAmount(amount: number): boolean
96
+ return typeof(amount) == "number" and amount == amount and amount > 0 and amount <= MAX_AMOUNT and amount % 1 == 0
97
+ end
98
+
99
+ local function validKind(kind: string, ref: string): boolean
100
+ return KINDS[kind] == true and typeof(ref) == "string" and ref ~= ""
101
+ end
102
+
103
+ local function saveWait(profile: any): boolean
104
+ if typeof(profile.SaveWait) == "function" then
105
+ return profile:SaveWait() == true
106
+ end
107
+ profile:Save()
108
+ return true
109
+ end
110
+
111
+ function Trade.Ensure(data: any)
112
+ return tradeBucket(data)
113
+ end
114
+
115
+ function Trade.Begin(profileA: any, profileB: any): string
116
+ if profileA == nil or profileB == nil or profileA == profileB then
117
+ return ""
118
+ end
119
+ if not profileA:IsActive() or not profileB:IsActive() then
120
+ return ""
121
+ end
122
+ local a = tradeBucket(profileA.Data)
123
+ local b = tradeBucket(profileB.Data)
124
+ if a.ActiveTradeId ~= "" or b.ActiveTradeId ~= "" then
125
+ return ""
126
+ end
127
+ local now = os.time()
128
+ local today = dayKey()
129
+ if a.DayKey ~= today then
130
+ a.DayKey = today
131
+ a.TradesToday = 0
132
+ end
133
+ if b.DayKey ~= today then
134
+ b.DayKey = today
135
+ b.TradesToday = 0
136
+ end
137
+ if a.TradesToday >= MAX_TRADES_PER_DAY or b.TradesToday >= MAX_TRADES_PER_DAY then
138
+ return ""
139
+ end
140
+ if now - (a.LastTradeAt or 0) < TRADE_COOLDOWN or now - (b.LastTradeAt or 0) < TRADE_COOLDOWN then
141
+ return ""
142
+ end
143
+ local id = opId()
144
+ a.ActiveTradeId = id
145
+ b.ActiveTradeId = id
146
+ table.insert(a.RecoveryTradeIds, id)
147
+ table.insert(b.RecoveryTradeIds, id)
148
+ while #a.RecoveryTradeIds > MAX_RECOVERY do
149
+ table.remove(a.RecoveryTradeIds, 1)
150
+ end
151
+ while #b.RecoveryTradeIds > MAX_RECOVERY do
152
+ table.remove(b.RecoveryTradeIds, 1)
153
+ end
154
+ return id
155
+ end
156
+
157
+ function Trade.Reserve(profile: any, kind: string, ref: string, amount: number, tradeId: string, extra: any?): boolean
158
+ if not profile or not profile:IsActive() then
159
+ return false
160
+ end
161
+ if not validKind(kind, ref) or not validAmount(amount) or typeof(tradeId) ~= "string" or tradeId == "" then
162
+ return false
163
+ end
164
+ local trade = tradeBucket(profile.Data)
165
+ if trade.ActiveTradeId ~= tradeId then
166
+ return false
167
+ end
168
+ local key = reserveKey(kind, ref)
169
+ if trade.Reserved[key] ~= nil then
170
+ return false
171
+ end
172
+ local held: any = { TradeId = tradeId, Kind = kind, Ref = ref, Amount = amount }
173
+ if kind == "Currency" then
174
+ profile.Data.Currencies = profile.Data.Currencies or {}
175
+ local have = profile.Data.Currencies[ref] or 0
176
+ if typeof(have) ~= "number" or have < amount then
177
+ return false
178
+ end
179
+ profile.Data.Currencies[ref] = have - amount
180
+ elseif kind == "Stack" then
181
+ local items = inventoryItems(profile.Data)
182
+ local found = false
183
+ for index, stack in items do
184
+ if typeof(stack) == "table" and stack.ItemId == ref then
185
+ if (stack.Amount or 0) < amount or stack.Locked == true then
186
+ return false
187
+ end
188
+ stack.Amount -= amount
189
+ if stack.Amount <= 0 then
190
+ table.remove(items, index)
191
+ end
192
+ found = true
193
+ break
194
+ end
195
+ end
196
+ if not found then
197
+ return false
198
+ end
199
+ elseif kind == "Instance" then
200
+ local uid = if extra then extra.UID else ref
201
+ if typeof(uid) ~= "string" or uid == "" then
202
+ return false
203
+ end
204
+ local list = inventoryItems(profile.Data)
205
+ local found = false
206
+ for index, item in list do
207
+ if typeof(item) == "table" and item.UID == uid then
208
+ if item.Locked == true or (typeof(item.Escrow) == "string" and item.Escrow ~= "" and item.Escrow ~= tradeId) then
209
+ return false
210
+ end
211
+ held.Item = deepCopy(item)
212
+ held.UID = uid
213
+ item.Escrow = tradeId
214
+ table.remove(list, index)
215
+ found = true
216
+ break
217
+ end
218
+ end
219
+ if not found then
220
+ return false
221
+ end
222
+ end
223
+ trade.Reserved[key] = held
224
+ return true
225
+ end
226
+
227
+ function Trade.ReceiptKey(operationId: string, kind: string, ref: string): string
228
+ return operationId .. "|" .. kind .. "|" .. ref
229
+ end
230
+
231
+ function Trade.AlreadyReceived(profile: any, operationId: string, kind: string, ref: string): boolean
232
+ local trade = tradeBucket(profile.Data)
233
+ return trade.Receipts[Trade.ReceiptKey(operationId, kind, ref)] ~= nil
234
+ end
235
+
236
+ function Trade.Credit(profile: any, kind: string, ref: string, amount: number, operationId: string, extra: any?)
237
+ if not validKind(kind, ref) or not validAmount(amount) or typeof(operationId) ~= "string" or operationId == "" then
238
+ return
239
+ end
240
+ if kind == "Instance" then
241
+ local item = extra and (extra.Item or extra)
242
+ if typeof(item) ~= "table" then
243
+ return
244
+ end
245
+ end
246
+ local trade = tradeBucket(profile.Data)
247
+ local key = Trade.ReceiptKey(operationId, kind, ref)
248
+ if trade.Receipts[key] then
249
+ return
250
+ end
251
+ trade.Receipts[key] = amount
252
+ pruneReceipts(trade)
253
+ if kind == "Currency" then
254
+ profile.Data.Currencies = profile.Data.Currencies or {}
255
+ profile.Data.Currencies[ref] = (profile.Data.Currencies[ref] or 0) + amount
256
+ return
257
+ end
258
+ if kind == "Stack" then
259
+ local items = inventoryItems(profile.Data)
260
+ for _, stack in items do
261
+ if typeof(stack) == "table" and stack.ItemId == ref then
262
+ stack.Amount = (stack.Amount or 0) + amount
263
+ stack.TransferredBy = operationId
264
+ return
265
+ end
266
+ end
267
+ table.insert(items, {
268
+ ItemId = ref,
269
+ Amount = amount,
270
+ Escrow = "",
271
+ TransferredBy = operationId,
272
+ })
273
+ return
274
+ end
275
+ if kind == "Instance" then
276
+ local item = extra and (extra.Item or extra)
277
+ if typeof(item) ~= "table" then
278
+ return
279
+ end
280
+ local copy = deepCopy(item)
281
+ copy.Escrow = nil
282
+ copy.TransferredBy = operationId
283
+ if typeof(copy.UID) ~= "string" or copy.UID == "" then
284
+ copy.UID = operationId
285
+ end
286
+ table.insert(inventoryItems(profile.Data), copy)
287
+ end
288
+ end
289
+
290
+ function Trade.ReleaseEscrow(profile: any, kind: string, ref: string, amount: number, tradeId: string): any?
291
+ local trade = tradeBucket(profile.Data)
292
+ local key = reserveKey(kind, ref)
293
+ local reserved = trade.Reserved[key]
294
+ if not reserved or reserved.TradeId ~= tradeId or reserved.Amount < amount then
295
+ return nil
296
+ end
297
+ local extra = { Item = reserved.Item, UID = reserved.UID }
298
+ reserved.Amount -= amount
299
+ if reserved.Amount <= 0 then
300
+ trade.Reserved[key] = nil
301
+ end
302
+ return extra
303
+ end
304
+
305
+ function Trade.DebitReserved(profile: any, kind: string, ref: string, amount: number, tradeId: string): boolean
306
+ return Trade.ReleaseEscrow(profile, kind, ref, amount, tradeId) ~= nil
307
+ end
308
+
309
+ local function refundReserved(profile: any, reserved: any)
310
+ if reserved.Kind == "Currency" then
311
+ profile.Data.Currencies = profile.Data.Currencies or {}
312
+ profile.Data.Currencies[reserved.Ref] = (profile.Data.Currencies[reserved.Ref] or 0) + reserved.Amount
313
+ elseif reserved.Kind == "Stack" then
314
+ local items = inventoryItems(profile.Data)
315
+ for _, stack in items do
316
+ if typeof(stack) == "table" and stack.ItemId == reserved.Ref then
317
+ stack.Amount = (stack.Amount or 0) + reserved.Amount
318
+ return
319
+ end
320
+ end
321
+ table.insert(items, { ItemId = reserved.Ref, Amount = reserved.Amount, Escrow = "" })
322
+ elseif reserved.Kind == "Instance" and typeof(reserved.Item) == "table" then
323
+ local item = deepCopy(reserved.Item)
324
+ item.Escrow = nil
325
+ table.insert(inventoryItems(profile.Data), item)
326
+ end
327
+ end
328
+
329
+ function Trade.Abort(profile: any, tradeId: string)
330
+ if not profile then
331
+ return
332
+ end
333
+ local trade = tradeBucket(profile.Data)
334
+ if trade.ActiveTradeId ~= tradeId then
335
+ return
336
+ end
337
+ for key, reserved in trade.Reserved do
338
+ if reserved.TradeId == tradeId then
339
+ refundReserved(profile, reserved)
340
+ trade.Reserved[key] = nil
341
+ end
342
+ end
343
+ trade.ActiveTradeId = ""
344
+ end
345
+
346
+ function Trade.AbortIfActive(profile: any)
347
+ if not profile or typeof(profile.Data) ~= "table" then
348
+ return
349
+ end
350
+ local trade = tradeBucket(profile.Data)
351
+ if typeof(trade.ActiveTradeId) == "string" and trade.ActiveTradeId ~= "" then
352
+ Trade.Abort(profile, trade.ActiveTradeId)
353
+ end
354
+ end
355
+
356
+ local function applyOffers(fromProfile: any, toProfile: any, offers: { any }, tradeId: string, operationId: string): boolean
357
+ if typeof(offers) ~= "table" or #offers > MAX_OFFERS then
358
+ return false
359
+ end
360
+ for _, offer in offers do
361
+ if typeof(offer) ~= "table" or not validKind(offer.Kind, offer.Ref) or not validAmount(offer.Amount) then
362
+ return false
363
+ end
364
+ local extra = Trade.ReleaseEscrow(fromProfile, offer.Kind, offer.Ref, offer.Amount, tradeId)
365
+ if extra == nil then
366
+ return false
367
+ end
368
+ Trade.Credit(toProfile, offer.Kind, offer.Ref, offer.Amount, operationId, extra)
369
+ end
370
+ return true
371
+ end
372
+
373
+ function Trade.Commit(store: any, profileA: any, profileB: any, offersA: { any }, offersB: { any }): boolean
374
+ if not profileA or not profileB or not profileA:IsActive() or not profileB:IsActive() then
375
+ return false
376
+ end
377
+ local a = tradeBucket(profileA.Data)
378
+ local b = tradeBucket(profileB.Data)
379
+ local tradeId = a.ActiveTradeId
380
+ if tradeId == "" or tradeId ~= b.ActiveTradeId then
381
+ return false
382
+ end
383
+ local destA = profileA.Data
384
+ local destB = profileB.Data
385
+ local snapA = deepCopy(destA)
386
+ local snapB = deepCopy(destB)
387
+ local operationId = opId()
388
+ if not applyOffers(profileA, profileB, offersA, tradeId, operationId) or not applyOffers(profileB, profileA, offersB, tradeId, operationId) then
389
+ overlay(destA, snapA)
390
+ overlay(destB, snapB)
391
+ return false
392
+ end
393
+ a = tradeBucket(destA)
394
+ b = tradeBucket(destB)
395
+ a.ActiveTradeId = ""
396
+ b.ActiveTradeId = ""
397
+ a.TradesToday += 1
398
+ b.TradesToday += 1
399
+ a.LastTradeAt = os.time()
400
+ b.LastTradeAt = os.time()
401
+ local okA = saveWait(profileA)
402
+ local okB = saveWait(profileB)
403
+ if not okA or not okB then
404
+ overlay(destA, snapA)
405
+ overlay(destB, snapB)
406
+ saveWait(profileA)
407
+ saveWait(profileB)
408
+ return false
409
+ end
410
+ return true
411
+ end
412
+
413
+ function Trade.SendOffline(store: any, key: string, payload: any): boolean
414
+ if typeof(store) ~= "table" or typeof(key) ~= "string" or key == "" or typeof(payload) ~= "table" then
415
+ return false
416
+ end
417
+ if payload.Credit then
418
+ if not validKind(payload.Kind, payload.Ref) or not validAmount(payload.Amount) then
419
+ return false
420
+ end
421
+ if typeof(payload.OperationId) ~= "string" or payload.OperationId == "" then
422
+ payload.OperationId = opId()
423
+ end
424
+ end
425
+ return store:MessageAsync(key, {
426
+ Kind = "Trade",
427
+ Payload = payload,
428
+ })
429
+ end
430
+
431
+ function Trade.HandleMessage(profile: any, message: any, processed: () -> ())
432
+ if typeof(message) ~= "table" or message.Kind ~= "Trade" then
433
+ return
434
+ end
435
+ local body = message.Payload
436
+ if typeof(body) ~= "table" then
437
+ processed()
438
+ return
439
+ end
440
+ if body.Abort then
441
+ if typeof(body.TradeId) == "string" then
442
+ Trade.Abort(profile, body.TradeId)
443
+ end
444
+ processed()
445
+ return
446
+ end
447
+ if body.Credit then
448
+ Trade.Credit(profile, body.Kind, body.Ref, body.Amount, body.OperationId, body.Extra)
449
+ processed()
450
+ return
451
+ end
452
+ processed()
453
+ end
454
+
455
+ return Trade
@@ -0,0 +1,205 @@
1
+ --!strict
2
+ -- Keep — player data (DataService alias). Server/Client are compiled from CL++.
3
+ -- Path tokens, Store, and tagged-buffer replication stay Luau.
4
+
5
+ local RunService = game:GetService("RunService")
6
+
7
+ local Path = require(script.Path)
8
+ local Data = require(script.Data)
9
+ local EnumModule = require(script.Enum)
10
+ local Store = require(script.Store)
11
+ local Trade = require(script.Trade)
12
+
13
+ export type Path<V = unknown> = Path.PathToken<V>
14
+ export type PathArray = Path.Path
15
+ export type PathLike<V = unknown> = Path<V> | PathArray
16
+ export type PathTree<T> = Path.PathTree<T>
17
+ export type Data<T> = Data.Data<T>
18
+ export type OrderedListOptions = Data.OrderedListOptions
19
+ export type OrderedListEntry<V> = Data.OrderedListEntry<V>
20
+ export type DataBufferStats = {
21
+ Bytes: number,
22
+ Messages: number,
23
+ LastPacketBytes: number,
24
+ LastUtilization: number,
25
+ }
26
+
27
+ export type ServerApi<T> = {
28
+ Init: (
29
+ self: ServerApi<T>,
30
+ options: {
31
+ Template: T,
32
+ StoreName: string?,
33
+ UseMock: boolean?,
34
+ KeyPrefix: string?,
35
+ Exclude: { PathArray }?,
36
+ StrictPaths: boolean?,
37
+ AutoCreateMissingTables: boolean?,
38
+ }
39
+ ) -> ServerApi<T>,
40
+ WaitFor: (self: ServerApi<T>, player: Player) -> Data<T>,
41
+ Get: (self: ServerApi<T>, player: Player) -> Data<T>?,
42
+ HasData: (self: ServerApi<T>, player: Player) -> boolean,
43
+ GetProfile: (self: ServerApi<T>, player: Player) -> unknown?,
44
+ GetBufferStats: (self: ServerApi<T>, player: Player?) -> DataBufferStats?,
45
+ Destroy: (self: ServerApi<T>) -> (),
46
+ Paths: PathTree<T>,
47
+ }
48
+
49
+ export type ClientApi<T> = {
50
+ Init: (self: ClientApi<T>) -> Data<T>,
51
+ WaitForData: (self: ClientApi<T>) -> Data<T>,
52
+ Get: (self: ClientApi<T>) -> Data<T>?,
53
+ GetBufferStats: (self: ClientApi<T>) -> DataBufferStats?,
54
+ Destroy: (self: ClientApi<T>) -> (),
55
+ Paths: PathTree<T>,
56
+ }
57
+
58
+ local function createUnavailableContext(contextName: string)
59
+ local rootPaths = Path.rootToken()
60
+
61
+ return {
62
+ Init = function()
63
+ error(`DataService.{contextName}.Init is not available in this runtime context`, 2)
64
+ end,
65
+ WaitFor = function()
66
+ error(`DataService.{contextName}.WaitFor is not available in this runtime context`, 2)
67
+ end,
68
+ WaitForData = function()
69
+ error(`DataService.{contextName}.WaitForData is not available in this runtime context`, 2)
70
+ end,
71
+ Get = function()
72
+ error(`DataService.{contextName}.Get is not available in this runtime context`, 2)
73
+ end,
74
+ HasData = function()
75
+ error(`DataService.{contextName}.HasData is not available in this runtime context`, 2)
76
+ end,
77
+ GetProfile = function()
78
+ error(`DataService.{contextName}.GetProfile is not available in this runtime context`, 2)
79
+ end,
80
+ GetBufferStats = function()
81
+ return nil
82
+ end,
83
+ Destroy = function() end,
84
+ Paths = rootPaths,
85
+ }
86
+ end
87
+
88
+ local DataService = {
89
+ Paths = Path.rootToken(),
90
+ Enum = EnumModule,
91
+ Server = nil :: ServerApi<unknown>?,
92
+ Client = nil :: ClientApi<unknown>?,
93
+ Store = Store,
94
+ Trade = Trade,
95
+ }
96
+
97
+ if RunService:IsServer() then
98
+ DataService.Server = require(script.Server) :: ServerApi<unknown>
99
+ DataService.Client = createUnavailableContext("Client") :: ClientApi<unknown>
100
+ DataService.Paths = DataService.Server.Paths
101
+ else
102
+ DataService.Client = require(script.Client) :: ClientApi<unknown>
103
+ DataService.Server = createUnavailableContext("Server") :: ServerApi<unknown>
104
+ DataService.Paths = DataService.Client.Paths
105
+ end
106
+
107
+ local function sessionOf(player: Player?): Data<unknown>?
108
+ if RunService:IsServer() then
109
+ if player == nil or DataService.Server == nil then
110
+ return nil
111
+ end
112
+ return DataService.Server:Get(player)
113
+ end
114
+ if DataService.Client == nil then
115
+ return nil
116
+ end
117
+ return DataService.Client:Get()
118
+ end
119
+
120
+ function DataService.Get<V>(player: Player, path: PathLike<V>): V?
121
+ local data = sessionOf(player)
122
+ if data == nil then
123
+ return nil
124
+ end
125
+ return data:Get(path) :: V
126
+ end
127
+
128
+ function DataService.GetPersisted<V>(player: Player, path: PathLike<V>): V?
129
+ local data = sessionOf(player)
130
+ if data == nil then
131
+ return nil
132
+ end
133
+ return data:GetPersisted(path) :: V
134
+ end
135
+
136
+ function DataService.Set<V>(player: Player, path: PathLike<V>, value: V)
137
+ if not RunService:IsServer() then
138
+ return
139
+ end
140
+ local data = sessionOf(player)
141
+ if data then
142
+ data:Set(path, value)
143
+ end
144
+ end
145
+
146
+ function DataService.SetTransient<V>(player: Player, path: PathLike<V>, value: V)
147
+ local data = sessionOf(player)
148
+ if data then
149
+ data:SetTransient(path, value)
150
+ end
151
+ end
152
+
153
+ function DataService.UpdateTransient<V>(player: Player, path: PathLike<V>, value: V)
154
+ local data = sessionOf(player)
155
+ if data then
156
+ data:UpdateTransient(path, function()
157
+ return value
158
+ end)
159
+ end
160
+ end
161
+
162
+ function DataService.ClearTransient(player: Player, path: PathLike<unknown>)
163
+ local data = sessionOf(player)
164
+ if data then
165
+ data:ClearTransient(path)
166
+ end
167
+ end
168
+
169
+ function DataService.GetOrderedList<V>(player: Player, path: PathLike<{ V }>): { Data.OrderedListEntry<V> }?
170
+ local data = sessionOf(player)
171
+ if data == nil then
172
+ return nil
173
+ end
174
+ return data:GetOrderedList(path, { key = "Priority" })
175
+ end
176
+
177
+ function DataService.GetOrderedListWithPriority<V>(player: Player, path: PathLike<{ V }>): { Data.OrderedListEntry<V> }?
178
+ local data = sessionOf(player)
179
+ if data == nil then
180
+ return nil
181
+ end
182
+ return data:GetOrderedListWithPriority(path, "Priority")
183
+ end
184
+
185
+ function DataService.WaitFor<T>(player: Player): Data<T>
186
+ if RunService:IsServer() then
187
+ assert(DataService.Server, "DataService.Server is unavailable")
188
+ return DataService.Server:WaitFor(player) :: Data<T>
189
+ end
190
+ assert(DataService.Client, "DataService.Client is unavailable")
191
+ return DataService.Client:WaitForData() :: Data<T>
192
+ end
193
+
194
+ function DataService.Observe<V>(
195
+ player: Player,
196
+ path: PathLike<V>,
197
+ callback: (newValue: V, oldValue: V) -> ()
198
+ ): RBXScriptConnection
199
+ local data = DataService.WaitFor(player)
200
+ return data:GetChangedSignal(path):Connect(callback)
201
+ end
202
+
203
+ export type DataService = typeof(DataService)
204
+
205
+ return DataService