@overmvp/kumi 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +14 -2
- package/animation/color-transition.ts +196 -0
- package/animation/cycle.ts +151 -0
- package/animation/ease.ts +29 -0
- package/animation/index.ts +13 -0
- package/animation/package.json +12 -0
- package/animation/presets.ts +376 -0
- package/animation/spec.ts +197 -0
- package/builder/Cargo.lock +192 -0
- package/builder/Cargo.toml +7 -0
- package/builder/css-v1.mjs +334 -0
- package/builder/css-v1.test.mjs +321 -0
- package/builder/generated-output.mjs +61 -0
- package/builder/generated-output.test.mjs +132 -0
- package/builder/jsx-form.mjs +93 -0
- package/builder/jsx-form.test.mjs +48 -0
- package/builder/jsx-lists.mjs +209 -0
- package/builder/link-addon-package.mjs +86 -0
- package/builder/lua-assets.mjs +74 -0
- package/builder/lua-builtin-values.mjs +57 -0
- package/builder/lua-builtin-values.test.mjs +155 -0
- package/builder/lua-loop-closures.mjs +183 -0
- package/builder/lua-loop-closures.test.mjs +89 -0
- package/builder/lua-truthiness.mjs +61 -0
- package/builder/lua-truthiness.test.mjs +113 -0
- package/builder/module-declarations.mjs +594 -0
- package/builder/module-declarations.test.mjs +444 -0
- package/builder/native-kernels.mjs +60 -0
- package/builder/native-lua.d.ts +57 -0
- package/builder/native-routes.mjs +51 -0
- package/builder/no-static-ref-read.mjs +93 -0
- package/builder/no-static-ref-read.test.mjs +181 -0
- package/builder/secure-snippet.mjs +317 -0
- package/builder/secure-snippet.test.mjs +101 -0
- package/builder/shared-lua-runtime.mjs +150 -0
- package/builder/src/addon-link.lua +29 -0
- package/builder/src/arrays.lua +260 -0
- package/builder/src/assets.rs +177 -0
- package/builder/src/classes.lua +75 -0
- package/builder/src/collections.lua +190 -0
- package/builder/src/compiler.rs +2164 -0
- package/builder/src/config.rs +42 -0
- package/builder/src/entries.rs +166 -0
- package/builder/src/errors.lua +15 -0
- package/builder/src/lua_data.rs +231 -0
- package/builder/src/main.rs +367 -0
- package/builder/src/native_ir.rs +65 -0
- package/builder/src/native_signal.rs +47 -0
- package/builder/src/numbers.lua +62 -0
- package/builder/src/project-front.mjs +3331 -0
- package/builder/src/records.lua +75 -0
- package/builder/src/routes.rs +127 -0
- package/builder/src/signal-kernel.fingerprint +1 -0
- package/builder/src/strings.lua +103 -0
- package/builder/src/ts-lower.mjs +68 -0
- package/builder/src/wow_client.rs +253 -0
- package/builder/src/wow_types.rs +564 -0
- package/builder/style-states.mjs +100 -0
- package/builder/style-states.test.mjs +103 -0
- package/builder/vite-assets.mjs +53 -0
- package/builder/vite-project.mjs +10 -0
- package/config.ts +17 -0
- package/form/index.ts +522 -0
- package/form/package.json +13 -0
- package/motion/index.ts +2 -0
- package/motion/package.json +10 -0
- package/package.json +22 -4
- package/router/index.ts +43 -0
- package/router/package.json +12 -0
- package/router/routes.ts +64 -0
- package/scripts/kumi-cli.mjs +70 -0
- package/scripts/link-addons.mjs +51 -0
- package/src/bindings.ts +35 -0
- package/src/context.ts +15 -0
- package/src/css-tokens.ts +234 -0
- package/src/css-v1.ts +58 -0
- package/src/cvars.ts +91 -0
- package/src/element-node.ts +32 -0
- package/src/elements.ts +479 -0
- package/src/feature-scope.ts +82 -0
- package/src/flex-layout.ts +807 -0
- package/src/frame-scheduler.ts +334 -0
- package/src/grid-tracks.ts +165 -0
- package/src/image-imports.d.ts +24 -0
- package/src/images.ts +79 -0
- package/src/index.ts +28 -0
- package/src/jsx-dev-runtime.ts +2 -0
- package/src/jsx-runtime.ts +22 -0
- package/src/jsx.d.ts +12 -0
- package/src/keyed-list.ts +363 -0
- package/src/media-query.ts +144 -0
- package/src/module.ts +277 -0
- package/src/node-selectors.ts +114 -0
- package/src/owned-collections.ts +44 -0
- package/src/providers.ts +235 -0
- package/src/reactive-scope.ts +34 -0
- package/src/reactivity.ts +254 -0
- package/src/result.ts +58 -0
- package/src/runtime.ts +52 -0
- package/src/scheduler.ts +185 -0
- package/src/signal-kernel.ts +30 -0
- package/src/style-classes.ts +51 -0
- package/src/style-states.ts +134 -0
- package/src/style-variables.ts +53 -0
- package/src/text-children.ts +42 -0
- package/src/theme.ts +50 -0
- package/src/transition.ts +18 -0
- package/state/index.ts +3 -0
- package/state/package.json +12 -0
- package/state/store-bindings.ts +25 -0
- package/state/store.ts +82 -0
- package/styles/_layers.scss +14 -0
- package/templates/addon/README.md +14 -0
- package/templates/addon/__ADDON_NAME__.toc +4 -0
- package/templates/addon/eslint.config.mjs +12 -0
- package/templates/addon/gitignore +8 -0
- package/templates/addon/package.json +21 -0
- package/templates/addon/scripts/build.mjs +25 -0
- package/templates/addon/src/app/main.tsx +13 -0
- package/templates/addon/tsconfig.json +24 -0
- package/translate/index.ts +14 -0
- package/translate/package.json +9 -0
- package/utils/colors.ts +76 -0
- package/utils/format.ts +37 -0
- package/utils/index.ts +4 -0
- package/utils/number.ts +17 -0
- package/utils/package.json +7 -0
- package/utils/text.ts +6 -0
- package/validate/index.ts +119 -0
- package/validate/package.json +9 -0
- package/wow/absolute-position.ts +142 -0
- package/wow/animation.ts +428 -0
- package/wow/aura-button.ts +76 -0
- package/wow/aura-container.ts +591 -0
- package/wow/aura-countdown.ts +201 -0
- package/wow/aura-slot.ts +153 -0
- package/wow/bar-shade.ts +93 -0
- package/wow/borrowed-slot.ts +216 -0
- package/wow/button-drag.ts +48 -0
- package/wow/cast-clock.ts +153 -0
- package/wow/cast-text.ts +83 -0
- package/wow/class-colors.ts +126 -0
- package/wow/clock.ts +7 -0
- package/wow/color-picker.ts +128 -0
- package/wow/commands.ts +17 -0
- package/wow/contained-frame.ts +128 -0
- package/wow/contract.ts +42 -0
- package/wow/css-box.ts +110 -0
- package/wow/css-color.ts +12 -0
- package/wow/css-style.ts +109 -0
- package/wow/css-surfaces.ts +398 -0
- package/wow/css-text.ts +399 -0
- package/wow/cvar-schema.ts +32 -0
- package/wow/cvars.ts +98 -0
- package/wow/dropdown-art.ts +39 -0
- package/wow/element-click.ts +20 -0
- package/wow/element-event-coordinator.ts +219 -0
- package/wow/element-events.ts +317 -0
- package/wow/event-dispatcher.ts +151 -0
- package/wow/events.ts +50 -0
- package/wow/font-cache.ts +92 -0
- package/wow/frame-access.ts +31 -0
- package/wow/frame-layer.ts +5 -0
- package/wow/frames.ts +236 -0
- package/wow/heal-prediction.ts +305 -0
- package/wow/image-layers.ts +55 -0
- package/wow/index.ts +97 -0
- package/wow/layout-view.ts +651 -0
- package/wow/lua-string.ts +18 -0
- package/wow/lua-table.ts +18 -0
- package/wow/macro-book.ts +84 -0
- package/wow/map-position.ts +27 -0
- package/wow/media-query.ts +65 -0
- package/wow/module-events.ts +11 -0
- package/wow/native-hooks.ts +168 -0
- package/wow/native-placement.ts +88 -0
- package/wow/native.ts +135 -0
- package/wow/number-format.ts +8 -0
- package/wow/number-input.ts +10 -0
- package/wow/protect.ts +24 -0
- package/wow/protected-layout.ts +133 -0
- package/wow/range-art.ts +74 -0
- package/wow/reparent-frame.ts +99 -0
- package/wow/rounded-texture.ts +156 -0
- package/wow/saved-variables.ts +60 -0
- package/wow/scroll-view.ts +64 -0
- package/wow/secret-ref.ts +14 -0
- package/wow/secure-bindings.ts +89 -0
- package/wow/secure-clicks.ts +432 -0
- package/wow/secure-snippet.ts +235 -0
- package/wow/secure-state.ts +130 -0
- package/wow/secure-target-visibility.ts +89 -0
- package/wow/secure-transaction.ts +331 -0
- package/wow/special-window.ts +269 -0
- package/wow/start-addon.ts +99 -0
- package/wow/state-paint.ts +256 -0
- package/wow/transition.ts +86 -0
- package/wow/unit-button.ts +39 -0
- package/wow/unit-cast.ts +92 -0
- package/wow/unit-health-text.ts +47 -0
- package/wow/view-nodes.ts +304 -0
- package/wow/view.ts +1746 -0
- package/wow/visible-frame-job.ts +34 -0
- package/wow/width-reserve.ts +108 -0
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import type { OwnedArray, OwnedReadonlyArray, OwnedRecord, OwnedSet } from "../src/owned-collections";
|
|
2
|
+
import { currentScope } from "../src/reactive-scope";
|
|
3
|
+
import { fail, ok, type Failure, type Result } from "../src/result";
|
|
4
|
+
import type { RuntimeScope } from "../src/runtime";
|
|
5
|
+
import type { Cancel } from "../src/scheduler";
|
|
6
|
+
import type { Button, Frame } from "./frames";
|
|
7
|
+
import { luaString } from "./lua-string";
|
|
8
|
+
import { weakKeys } from "./lua-table";
|
|
9
|
+
import { api, isSecret, reveal } from "./native";
|
|
10
|
+
import { protectNative } from "./protect";
|
|
11
|
+
import { createProtectedLayoutUpdate } from "./protected-layout";
|
|
12
|
+
|
|
13
|
+
export type SecureFrame = Pick<Frame, "SetAttribute" | "IsProtected" | "IsShown" | "Show" | "Hide" |
|
|
14
|
+
"HasAccessConstraints" | "HasAnyForbiddenAspects" | "GetName" | "GetParent"> & {
|
|
15
|
+
GetAttribute(this: object, name: string): unknown;
|
|
16
|
+
};
|
|
17
|
+
export type SecureButton = SecureFrame & Pick<Button, "RegisterForClicks">;
|
|
18
|
+
export type SecureScript = "OnClick" | "OnEnter" | "OnLeave" | "OnShow" | "OnHide";
|
|
19
|
+
|
|
20
|
+
export interface SecureOptions {
|
|
21
|
+
scope?: RuntimeScope;
|
|
22
|
+
cleanupScope?: RuntimeScope;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface SecureInstallation {
|
|
26
|
+
dispose(this: void): Result<void>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type Step = (this: void) => void | Result<void>;
|
|
30
|
+
type FrameScripts = {
|
|
31
|
+
HasScript(this: object, script: SecureScript): boolean;
|
|
32
|
+
GetScript(this: object, script: SecureScript | "OnAttributeChanged"): unknown;
|
|
33
|
+
};
|
|
34
|
+
type AttributeFrame = { SetAttribute(this: object, name: string, value?: unknown): void };
|
|
35
|
+
|
|
36
|
+
interface Mount {
|
|
37
|
+
scope: RuntimeScope;
|
|
38
|
+
budget: RuntimeScope;
|
|
39
|
+
identity: readonly unknown[];
|
|
40
|
+
handle?: SecureInstallation;
|
|
41
|
+
closing(this: void): boolean;
|
|
42
|
+
dispose(this: void): Result<void>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
declare function rawget<Value>(this: void, registry: object, frame: SecureFrame): Value | undefined;
|
|
46
|
+
declare function rawset<Value>(this: void, registry: object, frame: SecureFrame, value: Value): void;
|
|
47
|
+
|
|
48
|
+
const mounts = weakKeys({});
|
|
49
|
+
const claims = weakKeys({});
|
|
50
|
+
// A nil hole makes the length and iteration boundary of a Lua array ambiguous.
|
|
51
|
+
export const missingInput = {};
|
|
52
|
+
let generation = 0;
|
|
53
|
+
|
|
54
|
+
export function nativeResult<T>(run: (this: void) => Result<T>, fallback: string): Result<T> {
|
|
55
|
+
const [success, value] = protectNative(run);
|
|
56
|
+
if (success) return value;
|
|
57
|
+
const message = reveal(value);
|
|
58
|
+
return fail(typeof message === "string" ? message : fallback);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function unlocked(): boolean {
|
|
62
|
+
return reveal(api.InCombatLockdown()) === false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function secureFrame(frame: SecureFrame, explicit = false): Result<void> {
|
|
66
|
+
const [protectedState, explicitState] = frame.IsProtected();
|
|
67
|
+
return reveal(protectedState) === true && (!explicit || reveal(explicitState) === true)
|
|
68
|
+
&& reveal(frame.HasAccessConstraints()) === false && reveal(frame.HasAnyForbiddenAspects()) === false
|
|
69
|
+
? ok(undefined) : fail(explicit ? "An accessible, explicitly protected frame is required" : "An accessible protected frame is required");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function attributeName(value: string): boolean {
|
|
73
|
+
return !isSecret(value) && typeof value === "string" && luaString.match(value, "^[a-zA-Z][a-zA-Z0-9_-]*$") !== undefined
|
|
74
|
+
&& luaString.match(value, "^kumi%-secure%-") === undefined && luaString.match(value, "^frameref%-") === undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function readableArray(value: unknown): boolean {
|
|
78
|
+
return !isSecret(value) && Array.isArray(value);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function luaLiteral(value: string): string {
|
|
82
|
+
return luaString.format("%q", value);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function positiveInteger(value: unknown): value is number {
|
|
86
|
+
return !isSecret(value) && typeof value === "number" && value > 0 && value <= 9007199254740991 && Math.floor(value) === value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function namedButton(frame: SecureButton): string | undefined {
|
|
90
|
+
const name = reveal(frame.GetName());
|
|
91
|
+
return typeof name === "string" && luaString.match(name, "^[a-zA-Z_][a-zA-Z0-9_]*$") !== undefined ? name : undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function scriptOf(frame: SecureFrame, script: SecureScript | "OnAttributeChanged"): unknown {
|
|
95
|
+
return (frame as SecureFrame & FrameScripts).GetScript(script);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function setAttribute(frame: SecureFrame, name: string, value?: unknown): void {
|
|
99
|
+
(frame as SecureFrame & AttributeFrame).SetAttribute(name, value);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function combineFailure(failure: Failure, cleanup: Result<void>): Failure {
|
|
103
|
+
return cleanup.ok ? failure : fail(failure.reason + ". Cleanup failed: " + cleanup.reason);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export class SecureTransaction {
|
|
107
|
+
private readonly steps = [] as Step[] as OwnedArray<Step>;
|
|
108
|
+
private readonly reservations = [] as Cancel[] as OwnedArray<Cancel>;
|
|
109
|
+
|
|
110
|
+
defer(this: SecureTransaction, undo: Step): void {
|
|
111
|
+
this.steps.push(undo);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
claim(this: SecureTransaction, frame: SecureFrame, key: string): Result<void> {
|
|
115
|
+
let keys = rawget<OwnedSet<string>>(claims, frame);
|
|
116
|
+
if (keys === undefined) {
|
|
117
|
+
keys = new Set<string>() as OwnedSet<string>;
|
|
118
|
+
rawset(claims, frame, keys);
|
|
119
|
+
}
|
|
120
|
+
if (keys.has(key)) return fail("Secure frame already owns " + key);
|
|
121
|
+
keys.add(key);
|
|
122
|
+
this.reservations.push(() => { keys.delete(key); });
|
|
123
|
+
return ok(undefined);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
capture(this: SecureTransaction, frame: SecureFrame, name: string): Result<void> {
|
|
127
|
+
const previous = frame.GetAttribute(name);
|
|
128
|
+
if (isSecret(previous)) return fail("Secure attribute is unreadable: " + name);
|
|
129
|
+
this.defer(() => {
|
|
130
|
+
const current = frame.GetAttribute(name);
|
|
131
|
+
if (isSecret(current) || current !== previous) setAttribute(frame, name, previous);
|
|
132
|
+
});
|
|
133
|
+
return ok(undefined);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
write(this: SecureTransaction, frame: SecureFrame, name: string, value?: unknown): Result<void> {
|
|
137
|
+
const captured = this.capture(frame, name);
|
|
138
|
+
if (!captured.ok) return captured;
|
|
139
|
+
if (frame.GetAttribute(name) !== value) setAttribute(frame, name, value);
|
|
140
|
+
return ok(undefined);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
setHandler(this: SecureTransaction, frame: SecureFrame, name: string, body: string): Result<void> {
|
|
144
|
+
const previous = frame.GetAttribute(name);
|
|
145
|
+
if (isSecret(previous) || previous !== undefined) return fail("Secure handler already exists: " + name);
|
|
146
|
+
this.defer(() => {
|
|
147
|
+
const current = frame.GetAttribute(name);
|
|
148
|
+
if (!isSecret(current) && current === body) setAttribute(frame, name);
|
|
149
|
+
});
|
|
150
|
+
setAttribute(frame, name, body);
|
|
151
|
+
return ok(undefined);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
ref(this: SecureTransaction, frame: SecureFrame, name: string, reference: SecureFrame): Result<void> {
|
|
155
|
+
const attribute = "frameref-" + name;
|
|
156
|
+
const previous = frame.GetAttribute(attribute);
|
|
157
|
+
if (isSecret(previous) || previous !== undefined) return fail("Secure frame reference already exists: " + name);
|
|
158
|
+
let owned: unknown;
|
|
159
|
+
this.defer(() => {
|
|
160
|
+
const current = frame.GetAttribute(attribute);
|
|
161
|
+
if (owned !== undefined && !isSecret(current) && current === owned) setAttribute(frame, attribute);
|
|
162
|
+
});
|
|
163
|
+
const result = nativeResult(() => {
|
|
164
|
+
api.SecureHandlerSetFrameRef(frame, name, reference);
|
|
165
|
+
return ok(undefined);
|
|
166
|
+
}, "Could not set the secure frame reference: " + name);
|
|
167
|
+
const current = frame.GetAttribute(attribute);
|
|
168
|
+
if (!isSecret(current)) owned = current;
|
|
169
|
+
return !result.ok ? result : owned === undefined ? fail("Could not set the secure frame reference: " + name) : result;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
wrap(this: SecureTransaction, frame: SecureFrame, script: SecureScript, header: SecureFrame, before: string, after?: string): Result<void> {
|
|
173
|
+
if (!(frame as SecureFrame & FrameScripts).HasScript(script)) return fail("Frame does not support " + script);
|
|
174
|
+
const reserved = this.claim(frame, script);
|
|
175
|
+
if (!reserved.ok) return reserved;
|
|
176
|
+
|
|
177
|
+
// A native wrap can be covered by another addon's wrap. Disable our own
|
|
178
|
+
// body on release even when the client can only unwrap the top script.
|
|
179
|
+
const marker = "kumi-secure-" + script;
|
|
180
|
+
const token = ++generation;
|
|
181
|
+
const installed = this.write(frame, marker, token);
|
|
182
|
+
if (!installed.ok) return installed;
|
|
183
|
+
const guard = `if self:GetAttribute("${marker}") ~= ${token} then return end\n`;
|
|
184
|
+
const previous = scriptOf(frame, script);
|
|
185
|
+
let wrapped: unknown;
|
|
186
|
+
this.defer(() => {
|
|
187
|
+
if (wrapped !== undefined && scriptOf(frame, script) === wrapped) api.SecureHandlerUnwrapScript(frame, script);
|
|
188
|
+
});
|
|
189
|
+
const result = nativeResult(() => {
|
|
190
|
+
api.SecureHandlerWrapScript(frame, script, header, guard + before, after === undefined ? undefined : guard + after);
|
|
191
|
+
return ok(undefined);
|
|
192
|
+
}, "Could not wrap " + script);
|
|
193
|
+
const current = scriptOf(frame, script);
|
|
194
|
+
if (current !== previous && typeof current === "function") wrapped = current;
|
|
195
|
+
return !result.ok ? result : wrapped === undefined ? fail("The client did not install " + script) : result;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
dispose(this: SecureTransaction): Result<void> {
|
|
199
|
+
let failure: Failure | undefined;
|
|
200
|
+
for (let index = this.steps.length - 1; index >= 0; index--) {
|
|
201
|
+
const undo = this.steps[index];
|
|
202
|
+
const result = nativeResult(() => undo() ?? ok(undefined), "Could not release a secure operation");
|
|
203
|
+
if (result.ok) this.steps.splice(index, 1);
|
|
204
|
+
else failure ??= result;
|
|
205
|
+
}
|
|
206
|
+
if (failure === undefined) {
|
|
207
|
+
for (const release of this.reservations) release();
|
|
208
|
+
this.reservations.length = 0;
|
|
209
|
+
}
|
|
210
|
+
return failure ?? ok(undefined);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function secureUpdate(run: (this: void, transaction: SecureTransaction) => Result<void>): Result<void> {
|
|
215
|
+
const transaction = new SecureTransaction();
|
|
216
|
+
const result = nativeResult(() => run(transaction), "Could not update a secure installation");
|
|
217
|
+
return result.ok ? result : combineFailure(result, transaction.dispose());
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function sameInputs(first: readonly unknown[], second: readonly unknown[]): boolean {
|
|
221
|
+
const previous = first as OwnedReadonlyArray<unknown>;
|
|
222
|
+
const current = second as OwnedReadonlyArray<unknown>;
|
|
223
|
+
return previous.length === current.length && previous.every((value, index) => value === current[index]);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function installSecure<T extends SecureInstallation>(
|
|
227
|
+
frame: SecureFrame, key: string, options: SecureOptions, identity: readonly unknown[],
|
|
228
|
+
setup: (this: void, transaction: SecureTransaction, dispose: SecureInstallation["dispose"],
|
|
229
|
+
update: (this: void, run: (this: void) => Result<void>) => Result<void>) => Result<T>,
|
|
230
|
+
): Result<T> {
|
|
231
|
+
const scope = options.scope ?? currentScope();
|
|
232
|
+
const budget = options.cleanupScope ?? scope;
|
|
233
|
+
if (scope === undefined || budget === undefined || !scope.isActive() || !budget.isActive()) {
|
|
234
|
+
return fail("Secure installation requires active ownership and cleanup scopes");
|
|
235
|
+
}
|
|
236
|
+
return nativeResult(() => {
|
|
237
|
+
if (!unlocked()) return fail("Secure setup requires readable, inactive combat lockdown");
|
|
238
|
+
let entries = rawget<Record<string, Mount | undefined>>(mounts, frame);
|
|
239
|
+
if (entries === undefined) {
|
|
240
|
+
entries = {};
|
|
241
|
+
rawset(mounts, frame, entries);
|
|
242
|
+
}
|
|
243
|
+
const previous = entries[key];
|
|
244
|
+
if (previous !== undefined) {
|
|
245
|
+
if (previous.closing()) {
|
|
246
|
+
const released = previous.dispose();
|
|
247
|
+
if (!released.ok) return released;
|
|
248
|
+
if (entries[key] === previous) return fail("Secure installation is still being released");
|
|
249
|
+
} else {
|
|
250
|
+
if (previous.scope === scope && previous.budget === budget && previous.handle !== undefined
|
|
251
|
+
&& sameInputs(previous.identity, identity)) return ok(previous.handle as T);
|
|
252
|
+
return fail("A scope already owns this secure installation");
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const transaction = new SecureTransaction();
|
|
257
|
+
let closing = false;
|
|
258
|
+
let busy = true;
|
|
259
|
+
let finished = false;
|
|
260
|
+
let stopWaiting: Cancel | undefined;
|
|
261
|
+
let detach: Cancel | undefined;
|
|
262
|
+
// Cleanup can outlive the view. It borrows the selected frame budget
|
|
263
|
+
// and releases its own pending job once it has retired the native state.
|
|
264
|
+
const cleanupOwner: RuntimeScope = {
|
|
265
|
+
...budget as OwnedRecord<RuntimeScope>,
|
|
266
|
+
isActive: () => !finished,
|
|
267
|
+
onDispose: cancel => {
|
|
268
|
+
stopWaiting = cancel;
|
|
269
|
+
return ok(() => {
|
|
270
|
+
const waiting = stopWaiting !== undefined;
|
|
271
|
+
stopWaiting = undefined;
|
|
272
|
+
return waiting;
|
|
273
|
+
});
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
const retry = createProtectedLayoutUpdate(() => {
|
|
277
|
+
const result = release();
|
|
278
|
+
if (!result.ok) return result;
|
|
279
|
+
}, { scope: cleanupOwner, label: "Secure handler cleanup", prewarm: true });
|
|
280
|
+
if (!retry.ok) return retry;
|
|
281
|
+
const waiting = retry.value;
|
|
282
|
+
|
|
283
|
+
const mount: Mount = { scope, budget, identity, closing: () => closing, dispose };
|
|
284
|
+
entries[key] = mount;
|
|
285
|
+
const registered = scope.onDispose(() => {
|
|
286
|
+
const result = dispose();
|
|
287
|
+
if (!result.ok) protectNative(() => api.geterrorhandler()(result.reason));
|
|
288
|
+
});
|
|
289
|
+
if (registered.ok) detach = registered.value;
|
|
290
|
+
const installed = registered.ok
|
|
291
|
+
? nativeResult(() => setup(transaction, dispose, update), "Could not set up a secure installation")
|
|
292
|
+
: registered;
|
|
293
|
+
busy = false;
|
|
294
|
+
if (!installed.ok) return combineFailure(installed, dispose());
|
|
295
|
+
if (closing) return combineFailure(fail("The secure installation scope was disposed during setup"), dispose());
|
|
296
|
+
mount.handle = installed.value;
|
|
297
|
+
return installed;
|
|
298
|
+
|
|
299
|
+
function release(): Result<void> {
|
|
300
|
+
if (finished || busy) return ok(undefined);
|
|
301
|
+
busy = true;
|
|
302
|
+
const released = transaction.dispose();
|
|
303
|
+
busy = false;
|
|
304
|
+
if (released.ok) {
|
|
305
|
+
finished = true;
|
|
306
|
+
if (entries?.[key] === mount) entries[key] = undefined;
|
|
307
|
+
stopWaiting?.();
|
|
308
|
+
detach?.();
|
|
309
|
+
}
|
|
310
|
+
return released;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function dispose(): Result<void> {
|
|
314
|
+
closing = true;
|
|
315
|
+
return finished || busy ? ok(undefined) : waiting.request();
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function update(run: (this: void) => Result<void>): Result<void> {
|
|
319
|
+
if (closing || !scope?.isActive() || busy) return fail("The secure installation is unavailable");
|
|
320
|
+
if (!unlocked()) return fail("Secure updates require readable, inactive combat lockdown");
|
|
321
|
+
busy = true;
|
|
322
|
+
const result = nativeResult(run, "Could not update a secure installation");
|
|
323
|
+
busy = false;
|
|
324
|
+
if (closing) {
|
|
325
|
+
const released = dispose();
|
|
326
|
+
return result.ok ? released : combineFailure(result, released);
|
|
327
|
+
}
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
}, "Could not prepare a secure installation");
|
|
331
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import type { LuaMultiReturn } from "@kumi/wow-generated";
|
|
2
|
+
import type { OwnedLuaArray } from "../src/owned-collections";
|
|
3
|
+
import { currentScope } from "../src/reactive-scope";
|
|
4
|
+
import { fail, ok, type Failure, type Result } from "../src/result";
|
|
5
|
+
import type { RuntimeScope } from "../src/runtime";
|
|
6
|
+
import type { Cancel } from "../src/scheduler";
|
|
7
|
+
import { clock } from "./clock";
|
|
8
|
+
import { canAccessRegion } from "./frame-access";
|
|
9
|
+
import { createFrame, type Frame } from "./frames";
|
|
10
|
+
import { rawField, setRawField } from "./lua-table";
|
|
11
|
+
import { api, isSecret, reveal } from "./native";
|
|
12
|
+
import { protectNative } from "./protect";
|
|
13
|
+
|
|
14
|
+
type CloseHandler = (this: void) => void;
|
|
15
|
+
type SpecialFrames = OwnedLuaArray<unknown>;
|
|
16
|
+
|
|
17
|
+
export interface SpecialWindowProxy {
|
|
18
|
+
setCloseHandler(this: void, close?: CloseHandler): Result<void>;
|
|
19
|
+
cancel(this: void): Result<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface Registration {
|
|
23
|
+
list: SpecialFrames;
|
|
24
|
+
name: string;
|
|
25
|
+
frame: Frame;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare function type(this: void, value: unknown): string;
|
|
29
|
+
type Row = LuaMultiReturn<[key: unknown, value: unknown]>;
|
|
30
|
+
type Rows = Iterable<Row> & { readonly __tstlIterable: "Iterable" }
|
|
31
|
+
& LuaMultiReturn<[(this: void, list: SpecialFrames, key: unknown) => Row, SpecialFrames, undefined]>;
|
|
32
|
+
|
|
33
|
+
declare function pairs(this: void, list: SpecialFrames): Rows;
|
|
34
|
+
declare const table: { remove(this: void, list: SpecialFrames, row: number): unknown };
|
|
35
|
+
|
|
36
|
+
let sequence = 0;
|
|
37
|
+
|
|
38
|
+
function readList(): SpecialFrames | undefined {
|
|
39
|
+
const list = reveal(rawField(api, "UISpecialFrames"));
|
|
40
|
+
if (type(list) === "table") return list as SpecialFrames;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function occupied(value: unknown): boolean {
|
|
44
|
+
return isSecret(value) || value !== undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function findRow(list: SpecialFrames, name: string): Result<number | undefined> {
|
|
48
|
+
let row: number | undefined;
|
|
49
|
+
for (const [key, value] of pairs(list)) {
|
|
50
|
+
if (reveal(value) !== name) continue;
|
|
51
|
+
const index = reveal(key);
|
|
52
|
+
if (typeof index !== "number" || row !== undefined) return fail("Special window name has ambiguous registrations");
|
|
53
|
+
row = index;
|
|
54
|
+
}
|
|
55
|
+
return ok(row);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function ownsRegistration({ list, name, frame }: Registration): boolean {
|
|
59
|
+
return readList() === list && reveal(rawField(api, name)) === frame;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function nativeFailure(problem: unknown): Failure {
|
|
63
|
+
const reason = reveal(problem);
|
|
64
|
+
return fail(typeof reason === "string" ? reason : "Native special window operation failed");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function report(problem: unknown): void {
|
|
68
|
+
protectNative(() => api.geterrorhandler()(problem));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function queueRemoval(registration: Registration): void {
|
|
72
|
+
// The timer outlives its owner. Only the native registration is retained here.
|
|
73
|
+
clock(0, () => {
|
|
74
|
+
const [ran, result] = protectNative(() => {
|
|
75
|
+
if (!ownsRegistration(registration)) return fail("Special window registration was replaced before cleanup");
|
|
76
|
+
const { list, name } = registration;
|
|
77
|
+
const found = findRow(list, name);
|
|
78
|
+
if (!found.ok) return found;
|
|
79
|
+
const row = found.value;
|
|
80
|
+
if (row === undefined) return ok(undefined);
|
|
81
|
+
const length = list.length;
|
|
82
|
+
let dense = row >= 1 && row <= length && row % 1 === 0;
|
|
83
|
+
for (let i = 1; dense && i <= length; i++) dense = occupied(rawField(list, i));
|
|
84
|
+
if (dense) table.remove(list, row);
|
|
85
|
+
else setRawField(list, row, undefined);
|
|
86
|
+
return ok(undefined);
|
|
87
|
+
});
|
|
88
|
+
if (!ran) report(result);
|
|
89
|
+
else if (!result.ok) report(result.reason);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function isUnprotected(frame: Frame): boolean {
|
|
94
|
+
if (!canAccessRegion(frame)) return false;
|
|
95
|
+
const [protectedState] = frame.IsProtected();
|
|
96
|
+
return reveal(protectedState) === false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function bind(registration: Registration, initialOwner: RuntimeScope): Result<SpecialWindowProxy> {
|
|
100
|
+
const { list, name, frame } = registration;
|
|
101
|
+
let owner: RuntimeScope | undefined;
|
|
102
|
+
let unlink: Cancel | undefined;
|
|
103
|
+
let pending: Cancel | undefined;
|
|
104
|
+
let generation = 0;
|
|
105
|
+
let close: CloseHandler | undefined;
|
|
106
|
+
let armed = false;
|
|
107
|
+
let registered = false;
|
|
108
|
+
let waiting = true;
|
|
109
|
+
|
|
110
|
+
function stopPending(): void {
|
|
111
|
+
++generation;
|
|
112
|
+
const stop = pending;
|
|
113
|
+
pending = undefined;
|
|
114
|
+
stop?.();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function cancel(): Result<void> {
|
|
118
|
+
if (owner === undefined) return ok(undefined);
|
|
119
|
+
owner = undefined;
|
|
120
|
+
close = undefined;
|
|
121
|
+
armed = false;
|
|
122
|
+
|
|
123
|
+
let failure: Failure | undefined;
|
|
124
|
+
function attempt(step: (this: void) => void) {
|
|
125
|
+
const [ran, problem] = protectNative(step);
|
|
126
|
+
if (!ran) {
|
|
127
|
+
failure ??= nativeFailure(problem);
|
|
128
|
+
report(problem);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const detach = unlink;
|
|
133
|
+
unlink = undefined;
|
|
134
|
+
if (detach) attempt(detach);
|
|
135
|
+
attempt(stopPending);
|
|
136
|
+
attempt(() => frame.SetScript("OnHide", undefined));
|
|
137
|
+
attempt(() => frame.Hide());
|
|
138
|
+
if (registered) {
|
|
139
|
+
registered = false;
|
|
140
|
+
attempt(() => queueRemoval(registration));
|
|
141
|
+
}
|
|
142
|
+
return failure ?? ok(undefined);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function apply(): Result<void> {
|
|
146
|
+
if (!ownsRegistration(registration)) return fail("Special window registration was replaced");
|
|
147
|
+
if (!isUnprotected(frame)) return fail("Special window frame is protected or inaccessible");
|
|
148
|
+
const found = findRow(list, name);
|
|
149
|
+
if (!found.ok) return found;
|
|
150
|
+
if (registered) {
|
|
151
|
+
if (found.value === undefined) return fail("Special window registration was removed");
|
|
152
|
+
} else {
|
|
153
|
+
const row = list.length + 1;
|
|
154
|
+
if (found.value !== undefined || occupied(rawField(list, row))) return fail("Special window registration collided");
|
|
155
|
+
setRawField(list, row, name);
|
|
156
|
+
registered = true;
|
|
157
|
+
}
|
|
158
|
+
const shown = reveal(frame.IsShown());
|
|
159
|
+
if (typeof shown !== "boolean") return fail("Special window visibility is unreadable");
|
|
160
|
+
armed = close !== undefined;
|
|
161
|
+
if (armed && !shown) frame.Show();
|
|
162
|
+
if (!armed && shown) frame.Hide();
|
|
163
|
+
return ok(undefined);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function scheduleBoundary(): void {
|
|
167
|
+
if (pending !== undefined) return;
|
|
168
|
+
const ticket = ++generation;
|
|
169
|
+
pending = clock(0, () => {
|
|
170
|
+
if (ticket !== generation || !owner?.isActive()) return;
|
|
171
|
+
pending = undefined;
|
|
172
|
+
waiting = false;
|
|
173
|
+
const [ran, result] = protectNative(apply);
|
|
174
|
+
if (ran && result.ok) return;
|
|
175
|
+
cancel();
|
|
176
|
+
if (!ran) report(result);
|
|
177
|
+
else if (!result.ok) report(result.reason);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function onHide(this: void, self: Frame): void {
|
|
182
|
+
if (!armed || self !== frame || !owner?.isActive()) return;
|
|
183
|
+
const [read, hidden] = protectNative(() => reveal(self.IsShown()) === false && ownsRegistration(registration));
|
|
184
|
+
if (!read) {
|
|
185
|
+
report(hidden);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (!hidden) return;
|
|
189
|
+
const selected = close;
|
|
190
|
+
close = undefined;
|
|
191
|
+
armed = false;
|
|
192
|
+
waiting = true;
|
|
193
|
+
if (selected) {
|
|
194
|
+
const [ran, problem] = protectNative(selected);
|
|
195
|
+
if (!ran) report(problem);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function setCloseHandler(next?: CloseHandler): Result<void> {
|
|
200
|
+
if (!owner?.isActive()) return fail("Special window proxy requires an active scope");
|
|
201
|
+
close = next;
|
|
202
|
+
if (!registered) return ok(undefined);
|
|
203
|
+
if (next === undefined) armed = false;
|
|
204
|
+
const [ran, result] = protectNative(() => {
|
|
205
|
+
if (!waiting) return apply();
|
|
206
|
+
if (next === undefined) stopPending();
|
|
207
|
+
else scheduleBoundary();
|
|
208
|
+
return ok(undefined);
|
|
209
|
+
});
|
|
210
|
+
if (ran && result.ok) return result;
|
|
211
|
+
cancel();
|
|
212
|
+
if (ran) return result;
|
|
213
|
+
report(result);
|
|
214
|
+
return nativeFailure(result);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
owner = initialOwner;
|
|
218
|
+
const cleanup = owner.onDispose(() => { cancel(); });
|
|
219
|
+
if (!cleanup.ok) {
|
|
220
|
+
cancel();
|
|
221
|
+
return cleanup;
|
|
222
|
+
}
|
|
223
|
+
unlink = cleanup.value;
|
|
224
|
+
const [installed, problem] = protectNative(() => {
|
|
225
|
+
frame.SetScript("OnHide", onHide);
|
|
226
|
+
scheduleBoundary();
|
|
227
|
+
});
|
|
228
|
+
if (!installed) {
|
|
229
|
+
cancel();
|
|
230
|
+
report(problem);
|
|
231
|
+
return nativeFailure(problem);
|
|
232
|
+
}
|
|
233
|
+
return ok({ setCloseHandler, cancel });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** One native special-window proxy for a manager. First registration and arming occur at the next clock boundary. */
|
|
237
|
+
export function createSpecialWindowProxy(this: void, parent: Frame | undefined, namePrefix: string,
|
|
238
|
+
ownerScope: RuntimeScope | undefined = currentScope()): Result<SpecialWindowProxy> {
|
|
239
|
+
if (!ownerScope?.isActive()) return fail("Special window proxy requires an active scope");
|
|
240
|
+
const prefix = reveal(namePrefix);
|
|
241
|
+
if (typeof prefix !== "string" || prefix === "") return fail("Special window proxy requires a name prefix");
|
|
242
|
+
const [ran, result] = protectNative(() => {
|
|
243
|
+
const list = readList();
|
|
244
|
+
if (list === undefined) return fail("UISpecialFrames is unavailable");
|
|
245
|
+
if (isSecret(parent) || parent !== undefined && !isUnprotected(parent)) return fail("Special window parent is protected or inaccessible");
|
|
246
|
+
|
|
247
|
+
const name = prefix + "_" + ++sequence;
|
|
248
|
+
if (occupied(rawField(api, name))) return fail("Special window global name already exists");
|
|
249
|
+
const found = findRow(list, name);
|
|
250
|
+
if (!found.ok) return found;
|
|
251
|
+
if (found.value !== undefined) return fail("Special window name is already registered");
|
|
252
|
+
|
|
253
|
+
const frame = createFrame("Frame", name, parent);
|
|
254
|
+
if (!isUnprotected(frame)) return fail("Special window frame is protected or inaccessible");
|
|
255
|
+
if (reveal(frame.GetName()) !== name || reveal(rawField(api, name)) !== frame) {
|
|
256
|
+
return fail("Special window frame identity could not be confirmed");
|
|
257
|
+
}
|
|
258
|
+
const [hidden, problem] = protectNative(() => frame.Hide());
|
|
259
|
+
if (!hidden) {
|
|
260
|
+
protectNative(() => frame.Hide());
|
|
261
|
+
report(problem);
|
|
262
|
+
return nativeFailure(problem);
|
|
263
|
+
}
|
|
264
|
+
return bind({ list, name, frame }, ownerScope);
|
|
265
|
+
});
|
|
266
|
+
if (ran) return result;
|
|
267
|
+
report(result);
|
|
268
|
+
return nativeFailure(result);
|
|
269
|
+
}
|