@gameslate/sdk 0.3.5 → 0.3.6
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/README.md +9 -0
- package/lib/host/live-sandbox.js +10 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/plugin/contract.d.ts +7 -1
- package/lib/plugin/sandbox-guest.js +4 -0
- package/lib/sandbox-protocol.d.ts +2 -0
- package/lib/signature.d.ts +46 -0
- package/lib/signature.js +80 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,6 +38,15 @@ gameslate-plugin build ./my-plugin --out ./dist/my-plugin --tested-with 2.3.0
|
|
|
38
38
|
gameslate-plugin validate ./dist/my-plugin
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
+
For closed-source games, `memory.scan({ pattern: '48 8B 05 ?? ?? ?? ??', offset: 3, kind: 'rip32' })`
|
|
42
|
+
finds an address by a byte pattern in the game's own code (once per session, exactly one match),
|
|
43
|
+
so the plugin survives patches. It is optional: check `memory.scan` exists (cores before 0.3.6,
|
|
44
|
+
and emulators, don't have it).
|
|
45
|
+
|
|
46
|
+
Catalogue counters can declare a `role` (`health`, `magic`, `armor`, `keys`) and `format:
|
|
47
|
+
'percent'` so the core's AI and chips find them; declare your `variants` (`{ id, label,
|
|
48
|
+
shuffled? }`) for every `variant` your decoder emits.
|
|
49
|
+
|
|
41
50
|
`live.js` and `assets.js` run in the core's sandbox, built for a neutral platform: no Node APIs,
|
|
42
51
|
no file system, no network. Read the game through `GameMemory`, the player's files through
|
|
43
52
|
`InputFile`, and bytes with `Bytes`. Return data (states, drawings, pixels, meshes); the core
|
package/lib/host/live-sandbox.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GameStateSchema, PackViewsSchema, SandboxDescriptionSchema, } from '../index.js';
|
|
1
|
+
import { SignatureSchema, GameStateSchema, PackViewsSchema, SandboxDescriptionSchema, } from '../index.js';
|
|
2
2
|
import { errorText, QuickJsGuest } from './quickjs-guest.js';
|
|
3
3
|
export { loadQuickJs } from './quickjs-guest.js';
|
|
4
4
|
/*
|
|
@@ -193,6 +193,15 @@ export class PluginSandbox {
|
|
|
193
193
|
refreshMappings: handle => {
|
|
194
194
|
this.memory(handle).refreshMappings();
|
|
195
195
|
},
|
|
196
|
+
/* the guest's signature is untrusted: parsed here, before the core scans anything */
|
|
197
|
+
scan: (handle, signature) => {
|
|
198
|
+
const memory = this.memory(handle);
|
|
199
|
+
if (!memory.scan)
|
|
200
|
+
throw new Error('signature scans are not available for this game');
|
|
201
|
+
const sig = SignatureSchema.parse(JSON.parse(vm().getString(signature)));
|
|
202
|
+
const at = memory.scan(sig);
|
|
203
|
+
return at === null ? vm().null : vm().newString(at.toString());
|
|
204
|
+
},
|
|
196
205
|
};
|
|
197
206
|
}
|
|
198
207
|
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
package/lib/plugin/contract.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AssetList, AssetOutput, Bytes, GameCapability, GamePack, GameState, InputFile, InputPick } from '../index.js';
|
|
1
|
+
import type { AssetList, AssetOutput, Bytes, GameCapability, GamePack, GameState, InputFile, InputPick, Signature } from '../index.js';
|
|
2
2
|
/**
|
|
3
3
|
* What a plugin may do with the matched game's memory: read it, and nothing else. The core opens
|
|
4
4
|
* and closes the process; a plugin never sees a pid, a path or a file handle.
|
|
@@ -15,6 +15,12 @@ export interface GameMemory {
|
|
|
15
15
|
resolveSymbol(name: string): bigint | null;
|
|
16
16
|
/** Re-read the process's memory map (after a level load moves heap regions). */
|
|
17
17
|
refreshMappings(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Where a byte pattern in the game's own executable points (issue #14): null when it isn't
|
|
20
|
+
* there; throws when it matches more than once. Run once per session and cached by the core.
|
|
21
|
+
* Absent on cores before sdk 0.3.6 and for memory without an executable image (emulators).
|
|
22
|
+
*/
|
|
23
|
+
scan?(signature: Signature): bigint | null;
|
|
18
24
|
}
|
|
19
25
|
/** A memory-read state source for one game. Packs export theirs from games/<id>/source.ts. */
|
|
20
26
|
export interface GamePlugin {
|
|
@@ -14,6 +14,10 @@ function memoryFor(host, handle, info) {
|
|
|
14
14
|
return at === null ? null : BigInt(at);
|
|
15
15
|
},
|
|
16
16
|
refreshMappings: () => host.refreshMappings(handle),
|
|
17
|
+
scan: signature => {
|
|
18
|
+
const at = host.scan(handle, JSON.stringify(signature));
|
|
19
|
+
return at === null ? null : BigInt(at);
|
|
20
|
+
},
|
|
17
21
|
};
|
|
18
22
|
}
|
|
19
23
|
function describe(plugin) {
|
|
@@ -1378,6 +1378,8 @@ export interface SandboxHostApi {
|
|
|
1378
1378
|
isValidRange(handle: number, address: string, byteLength: number): boolean;
|
|
1379
1379
|
resolveSymbol(handle: number, name: string): string | null;
|
|
1380
1380
|
refreshMappings(handle: number): void;
|
|
1381
|
+
/** a signature scan (JSON of a Signature); the address as a decimal string, or null */
|
|
1382
|
+
scan(handle: number, signature: string): string | null;
|
|
1381
1383
|
}
|
|
1382
1384
|
/** A player's file as the asset guest sees it: a handle for `inputRead`, its name and size. */
|
|
1383
1385
|
export declare const SandboxInputFileSchema: z.ZodObject<{
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const SignatureSchema: z.ZodEffects<z.ZodObject<{
|
|
3
|
+
pattern: z.ZodEffects<z.ZodString, string, string>;
|
|
4
|
+
offset: z.ZodDefault<z.ZodNumber>;
|
|
5
|
+
kind: z.ZodEnum<["match", "rip32", "abs32", "abs64"]>;
|
|
6
|
+
instructionEnd: z.ZodDefault<z.ZodNumber>;
|
|
7
|
+
}, "strict", z.ZodTypeAny, {
|
|
8
|
+
kind: "match" | "rip32" | "abs32" | "abs64";
|
|
9
|
+
pattern: string;
|
|
10
|
+
offset: number;
|
|
11
|
+
instructionEnd: number;
|
|
12
|
+
}, {
|
|
13
|
+
kind: "match" | "rip32" | "abs32" | "abs64";
|
|
14
|
+
pattern: string;
|
|
15
|
+
offset?: number | undefined;
|
|
16
|
+
instructionEnd?: number | undefined;
|
|
17
|
+
}>, {
|
|
18
|
+
kind: "match" | "rip32" | "abs32" | "abs64";
|
|
19
|
+
pattern: string;
|
|
20
|
+
offset: number;
|
|
21
|
+
instructionEnd: number;
|
|
22
|
+
}, {
|
|
23
|
+
kind: "match" | "rip32" | "abs32" | "abs64";
|
|
24
|
+
pattern: string;
|
|
25
|
+
offset?: number | undefined;
|
|
26
|
+
instructionEnd?: number | undefined;
|
|
27
|
+
}>;
|
|
28
|
+
export type Signature = z.input<typeof SignatureSchema>;
|
|
29
|
+
export type ParsedSignature = z.output<typeof SignatureSchema>;
|
|
30
|
+
/** A pattern as bytes and which of them must match. */
|
|
31
|
+
export declare function parsePattern(pattern: string): {
|
|
32
|
+
bytes: Uint8Array;
|
|
33
|
+
fixed: boolean[];
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Every offset in `haystack` where the pattern matches (at most `limit`). The first byte is
|
|
37
|
+
* fixed (the schema insists), so the native indexOf finds candidates.
|
|
38
|
+
*/
|
|
39
|
+
export declare function findPattern(haystack: Uint8Array, pattern: string, limit?: number): number[];
|
|
40
|
+
/** How many bytes past the field `resolveSignature` needs to read. */
|
|
41
|
+
export declare function fieldBytes(sig: ParsedSignature): number;
|
|
42
|
+
/**
|
|
43
|
+
* The address a match points to. `fieldAddress` is match + offset; `field` holds the bytes there
|
|
44
|
+
* (little-endian, `fieldBytes(sig)` of them).
|
|
45
|
+
*/
|
|
46
|
+
export declare function resolveSignature(sig: ParsedSignature, fieldAddress: bigint, field: Uint8Array): bigint;
|
package/lib/signature.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/*
|
|
3
|
+
* Signature scans (issue #14): finding where a closed-source game keeps something by a byte
|
|
4
|
+
* pattern in its code, so a plugin survives patches and store versions instead of hard-coding an
|
|
5
|
+
* address. The core runs the scan over the game's executable image; this module is the pure part
|
|
6
|
+
* (patterns, matching, and turning a match into an address) shared by the core and tests.
|
|
7
|
+
*
|
|
8
|
+
* pattern "48 8B 05 ?? ?? ?? ??" hex bytes, `??` for any byte
|
|
9
|
+
* offset bytes from the match's start to the field of interest
|
|
10
|
+
* kind what the field is:
|
|
11
|
+
* match the field's own address
|
|
12
|
+
* rip32 a 32-bit displacement relative to the end of the instruction (x86-64 RIP
|
|
13
|
+
* addressing); `instructionEnd` counts any bytes after the displacement (an
|
|
14
|
+
* immediate) that still belong to the instruction
|
|
15
|
+
* abs32 / abs64 an absolute pointer stored there
|
|
16
|
+
*/
|
|
17
|
+
const PATTERN_RE = /^(?:[0-9A-Fa-f]{2}|\?\?)(?: (?:[0-9A-Fa-f]{2}|\?\?))*$/;
|
|
18
|
+
/* Long enough to be unique in a game's code, short enough to read in one go. */
|
|
19
|
+
const MAX_PATTERN_BYTES = 64;
|
|
20
|
+
export const SignatureSchema = z
|
|
21
|
+
.object({
|
|
22
|
+
pattern: z
|
|
23
|
+
.string()
|
|
24
|
+
.regex(PATTERN_RE, 'hex bytes separated by spaces, ?? for any byte')
|
|
25
|
+
.refine(p => !p.startsWith('??'), 'must start with a fixed byte'),
|
|
26
|
+
offset: z.number().int().min(-4096).max(4096).default(0),
|
|
27
|
+
kind: z.enum(['match', 'rip32', 'abs32', 'abs64']),
|
|
28
|
+
instructionEnd: z.number().int().min(0).max(16).default(0),
|
|
29
|
+
})
|
|
30
|
+
.strict()
|
|
31
|
+
.refine(s => s.pattern.split(' ').length <= MAX_PATTERN_BYTES, 'pattern is too long');
|
|
32
|
+
/** A pattern as bytes and which of them must match. */
|
|
33
|
+
export function parsePattern(pattern) {
|
|
34
|
+
const tokens = pattern.split(' ');
|
|
35
|
+
return {
|
|
36
|
+
bytes: Uint8Array.from(tokens, t => (t === '??' ? 0 : parseInt(t, 16))),
|
|
37
|
+
fixed: tokens.map(t => t !== '??'),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Every offset in `haystack` where the pattern matches (at most `limit`). The first byte is
|
|
42
|
+
* fixed (the schema insists), so the native indexOf finds candidates.
|
|
43
|
+
*/
|
|
44
|
+
export function findPattern(haystack, pattern, limit = 2) {
|
|
45
|
+
const { bytes, fixed } = parsePattern(pattern);
|
|
46
|
+
const first = bytes[0] ?? 0;
|
|
47
|
+
const hits = [];
|
|
48
|
+
for (let i = haystack.indexOf(first); i >= 0 && i + bytes.length <= haystack.length; i = haystack.indexOf(first, i + 1)) {
|
|
49
|
+
let k = 1;
|
|
50
|
+
while (k < bytes.length && (!fixed[k] || haystack[i + k] === bytes[k]))
|
|
51
|
+
k++;
|
|
52
|
+
if (k === bytes.length) {
|
|
53
|
+
hits.push(i);
|
|
54
|
+
if (hits.length >= limit)
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return hits;
|
|
59
|
+
}
|
|
60
|
+
/** How many bytes past the field `resolveSignature` needs to read. */
|
|
61
|
+
export function fieldBytes(sig) {
|
|
62
|
+
return sig.kind === 'abs64' ? 8 : sig.kind === 'match' ? 0 : 4;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The address a match points to. `fieldAddress` is match + offset; `field` holds the bytes there
|
|
66
|
+
* (little-endian, `fieldBytes(sig)` of them).
|
|
67
|
+
*/
|
|
68
|
+
export function resolveSignature(sig, fieldAddress, field) {
|
|
69
|
+
const view = new DataView(field.buffer, field.byteOffset, field.byteLength);
|
|
70
|
+
switch (sig.kind) {
|
|
71
|
+
case 'match':
|
|
72
|
+
return fieldAddress;
|
|
73
|
+
case 'rip32':
|
|
74
|
+
return fieldAddress + 4n + BigInt(sig.instructionEnd) + BigInt(view.getInt32(0, true));
|
|
75
|
+
case 'abs32':
|
|
76
|
+
return BigInt(view.getUint32(0, true));
|
|
77
|
+
case 'abs64':
|
|
78
|
+
return view.getBigUint64(0, true);
|
|
79
|
+
}
|
|
80
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gameslate/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"description": "Build plugins for GameSlate, the second-screen companion for PC games: the plugin contract, game-state and catalogue schemas, and the plugin builder.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|