@unibridge/sdk 0.5.0 → 0.8.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.
Files changed (105) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +67 -0
  3. package/dist/client.d.ts +6 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +59 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/commands/contracts.d.ts +8 -0
  8. package/dist/commands/contracts.d.ts.map +1 -0
  9. package/dist/commands/contracts.js +2 -0
  10. package/dist/commands/contracts.js.map +1 -0
  11. package/dist/commands/define.d.ts +19 -0
  12. package/dist/commands/define.d.ts.map +1 -0
  13. package/dist/commands/define.js +20 -0
  14. package/dist/commands/define.js.map +1 -0
  15. package/dist/commands/domain/contract.d.ts +10 -0
  16. package/dist/commands/domain/contract.d.ts.map +1 -0
  17. package/dist/commands/domain/contract.js +12 -0
  18. package/dist/commands/domain/contract.js.map +1 -0
  19. package/dist/commands/execute/client.d.ts +4 -0
  20. package/dist/commands/execute/client.d.ts.map +1 -0
  21. package/dist/commands/execute/client.js +5 -0
  22. package/dist/commands/execute/client.js.map +1 -0
  23. package/dist/commands/execute/contract.d.ts +4 -0
  24. package/dist/commands/execute/contract.d.ts.map +1 -0
  25. package/dist/commands/execute/contract.js +10 -0
  26. package/dist/commands/execute/contract.js.map +1 -0
  27. package/dist/commands/gameobject/contract.d.ts +224 -0
  28. package/dist/commands/gameobject/contract.d.ts.map +1 -0
  29. package/dist/commands/gameobject/contract.js +101 -0
  30. package/dist/commands/gameobject/contract.js.map +1 -0
  31. package/dist/commands/log/contract.d.ts +46 -0
  32. package/dist/commands/log/contract.d.ts.map +1 -0
  33. package/dist/commands/log/contract.js +31 -0
  34. package/dist/commands/log/contract.js.map +1 -0
  35. package/dist/commands/logs/contract.d.ts +46 -0
  36. package/dist/commands/logs/contract.d.ts.map +1 -0
  37. package/dist/commands/logs/contract.js +31 -0
  38. package/dist/commands/logs/contract.js.map +1 -0
  39. package/dist/commands/registry.d.ts +188 -0
  40. package/dist/commands/registry.d.ts.map +1 -0
  41. package/dist/commands/registry.js +9 -0
  42. package/dist/commands/registry.js.map +1 -0
  43. package/dist/commands/runtime.d.ts +7 -0
  44. package/dist/commands/runtime.d.ts.map +1 -0
  45. package/dist/commands/runtime.js +2 -0
  46. package/dist/commands/runtime.js.map +1 -0
  47. package/dist/commands/scene/client.d.ts +4 -0
  48. package/dist/commands/scene/client.d.ts.map +1 -0
  49. package/dist/commands/scene/client.js +7 -0
  50. package/dist/commands/scene/client.js.map +1 -0
  51. package/dist/commands/scene/contract.d.ts +127 -0
  52. package/dist/commands/scene/contract.d.ts.map +1 -0
  53. package/dist/commands/scene/contract.js +79 -0
  54. package/dist/commands/scene/contract.js.map +1 -0
  55. package/dist/commands/status/client.d.ts +4 -0
  56. package/dist/commands/status/client.d.ts.map +1 -0
  57. package/dist/commands/status/client.js +7 -0
  58. package/dist/commands/status/client.js.map +1 -0
  59. package/dist/commands/status/contract.d.ts +18 -0
  60. package/dist/commands/status/contract.d.ts.map +1 -0
  61. package/dist/commands/status/contract.js +16 -0
  62. package/dist/commands/status/contract.js.map +1 -0
  63. package/dist/commands/test/contract.d.ts +111 -0
  64. package/dist/commands/test/contract.d.ts.map +1 -0
  65. package/dist/commands/test/contract.js +71 -0
  66. package/dist/commands/test/contract.js.map +1 -0
  67. package/dist/connection.d.ts +37 -0
  68. package/dist/connection.d.ts.map +1 -0
  69. package/dist/connection.js +311 -0
  70. package/dist/connection.js.map +1 -0
  71. package/dist/hash.d.ts +4 -0
  72. package/dist/hash.d.ts.map +1 -0
  73. package/dist/hash.js +41 -0
  74. package/dist/hash.js.map +1 -0
  75. package/dist/index.d.ts +5 -0
  76. package/dist/index.d.ts.map +1 -0
  77. package/dist/index.js +3 -0
  78. package/dist/index.js.map +1 -0
  79. package/dist/project.d.ts +6 -0
  80. package/dist/project.d.ts.map +1 -0
  81. package/dist/project.js +71 -0
  82. package/dist/project.js.map +1 -0
  83. package/dist/types.d.ts +55 -0
  84. package/dist/types.d.ts.map +1 -0
  85. package/dist/types.js +2 -0
  86. package/dist/types.js.map +1 -0
  87. package/package.json +31 -5
  88. package/src/client.ts +0 -76
  89. package/src/commands/contracts.ts +0 -4
  90. package/src/commands/define.ts +0 -56
  91. package/src/commands/domain/contract.ts +0 -15
  92. package/src/commands/execute/contract.ts +0 -12
  93. package/src/commands/registry.ts +0 -6
  94. package/src/commands/runtime.ts +0 -7
  95. package/src/commands/scene/contract.ts +0 -46
  96. package/src/commands/status/contract.ts +0 -19
  97. package/src/connection.test.ts +0 -330
  98. package/src/connection.ts +0 -382
  99. package/src/hash.test.ts +0 -48
  100. package/src/hash.ts +0 -50
  101. package/src/index.ts +0 -10
  102. package/src/project.test.ts +0 -93
  103. package/src/project.ts +0 -99
  104. package/src/types.ts +0 -64
  105. package/tsconfig.json +0 -16
package/dist/hash.js ADDED
@@ -0,0 +1,41 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { realpathSync } from 'node:fs';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ function canonicalizeProjectPath(projectPath) {
6
+ const resolved = path.resolve(projectPath);
7
+ let canonical = resolved;
8
+ try {
9
+ canonical = realpathSync.native(resolved);
10
+ }
11
+ catch {
12
+ // Fall back to the resolved path when it does not exist yet.
13
+ }
14
+ canonical = canonical.replace(/\\/g, '/');
15
+ canonical = canonical.replace(/\/+$/g, '');
16
+ if (process.platform === 'win32') {
17
+ canonical = canonical.toLowerCase();
18
+ }
19
+ return canonical;
20
+ }
21
+ export function projectHash(projectPath) {
22
+ const canonical = canonicalizeProjectPath(projectPath);
23
+ return createHash('sha256').update(canonical).digest('hex').slice(0, 12);
24
+ }
25
+ function stateBaseDir() {
26
+ if (process.platform === 'win32') {
27
+ return path.join(os.tmpdir(), 'unibridge');
28
+ }
29
+ return '/tmp/unibridge';
30
+ }
31
+ export function stateDir(projectPath) {
32
+ return path.join(stateBaseDir(), projectHash(projectPath));
33
+ }
34
+ export function pipePath(projectPath) {
35
+ const hash = projectHash(projectPath);
36
+ if (process.platform === 'win32') {
37
+ return `\\\\.\\pipe\\unibridge-${hash}`;
38
+ }
39
+ return path.join(stateBaseDir(), hash, 'bridge.sock');
40
+ }
41
+ //# sourceMappingURL=hash.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.js","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,EAAE,MAAM,SAAS,CAAA;AACxB,OAAO,IAAI,MAAM,WAAW,CAAA;AAE5B,SAAS,uBAAuB,CAAC,WAAmB;IAClD,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAA;IAC1C,IAAI,SAAS,GAAG,QAAQ,CAAA;IAExB,IAAI,CAAC;QACH,SAAS,GAAG,YAAY,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;IAC3C,CAAC;IAAC,MAAM,CAAC;QACP,6DAA6D;IAC/D,CAAC;IAED,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;IACzC,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;IAE1C,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;QACjC,SAAS,GAAG,SAAS,CAAC,WAAW,EAAE,CAAA;IACrC,CAAC;IAED,OAAO,SAAS,CAAA;AAClB,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,WAAmB;IAC7C,MAAM,SAAS,GAAG,uBAAuB,CAAC,WAAW,CAAC,CAAA;IACtD,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;AAC1E,CAAC;AAED,SAAS,YAAY;IACnB,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;QACjC,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,EAAE,WAAW,CAAC,CAAA;IAC5C,CAAC;IAED,OAAO,gBAAgB,CAAA;AACzB,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,WAAmB;IAC1C,OAAO,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,WAAW,CAAC,WAAW,CAAC,CAAC,CAAA;AAC5D,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,WAAmB;IAC1C,MAAM,IAAI,GAAG,WAAW,CAAC,WAAW,CAAC,CAAA;IACrC,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;QACjC,OAAO,0BAA0B,IAAI,EAAE,CAAA;IACzC,CAAC;IAED,OAAO,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,IAAI,EAAE,aAAa,CAAC,CAAA;AACvD,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { init, findUnityProject, isPluginInstalled } from './project.ts';
2
+ export { createClient } from './client.ts';
3
+ export type * from './commands/contracts.ts';
4
+ export type { InitOptions, InitResult, CommandResponse, ClientOptions, UniBridgeClient, } from './types.ts';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,mBAAmB,yBAAyB,CAAA;AAC5C,YAAY,EACV,WAAW,EACX,UAAU,EACV,eAAe,EACf,aAAa,EACb,eAAe,GAChB,MAAM,YAAY,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { init, findUnityProject, isPluginInstalled } from "./project.js";
2
+ export { createClient } from "./client.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA"}
@@ -0,0 +1,6 @@
1
+ import type { InitOptions, InitResult } from './types.ts';
2
+ export declare function findUnityProject(startPath?: string): string;
3
+ export declare function parseUnityVersion(projectPath: string): string;
4
+ export declare function isPluginInstalled(projectPath: string): boolean;
5
+ export declare function init(options?: InitOptions): Promise<InitResult>;
6
+ //# sourceMappingURL=project.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"project.d.ts","sourceRoot":"","sources":["../src/project.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AA8BzD,wBAAgB,gBAAgB,CAAC,SAAS,GAAE,MAAc,GAAG,MAAM,CAkBlE;AAED,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAS7D;AAED,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAG9D;AAED,wBAAsB,IAAI,CAAC,OAAO,GAAE,WAAgB,GAAG,OAAO,CAAC,UAAU,CAAC,CA6BzE"}
@@ -0,0 +1,71 @@
1
+ import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
2
+ import path from 'node:path';
3
+ import { cwd } from 'node:process';
4
+ const PLUGIN_NAME = 'com.msanatan.unibridge';
5
+ const SDK_VERSION = JSON.parse(readFileSync(path.join(import.meta.dirname, '..', 'package.json'), 'utf-8')).version;
6
+ const DEFAULT_GIT_SOURCE = `https://github.com/msanatan/unibridge.git?path=unity#v${SDK_VERSION}`;
7
+ function readManifest(projectPath) {
8
+ const manifestPath = path.join(projectPath, 'Packages', 'manifest.json');
9
+ return JSON.parse(readFileSync(manifestPath, 'utf-8'));
10
+ }
11
+ function writeManifestAtomic(projectPath, manifest) {
12
+ const manifestPath = path.join(projectPath, 'Packages', 'manifest.json');
13
+ const tempPath = `${manifestPath}.tmp`;
14
+ const content = `${JSON.stringify(manifest, null, 2)}\n`;
15
+ writeFileSync(tempPath, content, 'utf-8');
16
+ renameSync(tempPath, manifestPath);
17
+ }
18
+ export function findUnityProject(startPath = cwd()) {
19
+ let current = path.resolve(startPath);
20
+ while (true) {
21
+ const assetsPath = path.join(current, 'Assets');
22
+ const versionPath = path.join(current, 'ProjectSettings', 'ProjectVersion.txt');
23
+ if (existsSync(assetsPath) && existsSync(versionPath)) {
24
+ return current;
25
+ }
26
+ const parent = path.dirname(current);
27
+ if (parent === current) {
28
+ throw new Error('Unity project not found. Run inside a Unity project or pass --project <path>.');
29
+ }
30
+ current = parent;
31
+ }
32
+ }
33
+ export function parseUnityVersion(projectPath) {
34
+ const versionPath = path.join(projectPath, 'ProjectSettings', 'ProjectVersion.txt');
35
+ const content = readFileSync(versionPath, 'utf-8');
36
+ const match = content.match(/m_EditorVersion:\s*(\S+)/);
37
+ if (!match) {
38
+ throw new Error(`Could not parse m_EditorVersion from ${versionPath}`);
39
+ }
40
+ return match[1];
41
+ }
42
+ export function isPluginInstalled(projectPath) {
43
+ const manifest = readManifest(projectPath);
44
+ return Boolean(manifest.dependencies?.[PLUGIN_NAME]);
45
+ }
46
+ export async function init(options = {}) {
47
+ const projectPath = findUnityProject(options.projectPath);
48
+ const unityVersion = parseUnityVersion(projectPath);
49
+ const manifest = readManifest(projectPath);
50
+ const dependencies = { ...(manifest.dependencies ?? {}) };
51
+ const pluginSource = options.source?.type ?? 'git';
52
+ const pluginReference = options.source?.type === 'local'
53
+ ? `file:${options.source.path}`
54
+ : options.source?.type === 'git'
55
+ ? options.source.url
56
+ : DEFAULT_GIT_SOURCE;
57
+ if (dependencies[PLUGIN_NAME] !== pluginReference) {
58
+ dependencies[PLUGIN_NAME] = pluginReference;
59
+ writeManifestAtomic(projectPath, {
60
+ ...manifest,
61
+ dependencies,
62
+ });
63
+ }
64
+ return {
65
+ projectPath,
66
+ unityVersion,
67
+ pluginVersion: SDK_VERSION,
68
+ pluginSource,
69
+ };
70
+ }
71
+ //# sourceMappingURL=project.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"project.js","sourceRoot":"","sources":["../src/project.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAC7E,OAAO,IAAI,MAAM,WAAW,CAAA;AAC5B,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AAGlC,MAAM,WAAW,GAAG,wBAAwB,CAAA;AAE5C,MAAM,WAAW,GACf,IAAI,CAAC,KAAK,CACR,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,cAAc,CAAC,EAAE,OAAO,CAAC,CAE9E,CAAC,OAAO,CAAA;AAET,MAAM,kBAAkB,GACtB,yDAAyD,WAAW,EAAE,CAAA;AAMxE,SAAS,YAAY,CAAC,WAAmB;IACvC,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,EAAE,eAAe,CAAC,CAAA;IACxE,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,YAAY,EAAE,OAAO,CAAC,CAAa,CAAA;AACpE,CAAC;AAED,SAAS,mBAAmB,CAAC,WAAmB,EAAE,QAAkB;IAClE,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,EAAE,eAAe,CAAC,CAAA;IACxE,MAAM,QAAQ,GAAG,GAAG,YAAY,MAAM,CAAA;IACtC,MAAM,OAAO,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,QA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';
2
+ import type { allCommands } from './commands/registry.ts';
3
+ export interface InitOptions {
4
+ projectPath?: string;
5
+ source?: GitSource | LocalSource;
6
+ }
7
+ export interface GitSource {
8
+ type: 'git';
9
+ url: string;
10
+ }
11
+ export interface LocalSource {
12
+ type: 'local';
13
+ path: string;
14
+ }
15
+ export interface InitResult {
16
+ projectPath: string;
17
+ unityVersion: string;
18
+ pluginVersion: string;
19
+ pluginSource: 'git' | 'local';
20
+ }
21
+ export interface CommandRequest {
22
+ id: string;
23
+ command: string;
24
+ params: Record<string, unknown>;
25
+ }
26
+ export interface CommandResponse {
27
+ id: string;
28
+ success: boolean;
29
+ result?: unknown;
30
+ error?: string;
31
+ }
32
+ export interface ServerMetadata {
33
+ pid: number;
34
+ unityVersion: string;
35
+ pluginVersion: string;
36
+ protocolVersion: number;
37
+ capabilities?: {
38
+ executeEnabled?: boolean;
39
+ };
40
+ projectPath?: string;
41
+ }
42
+ export interface TimeoutOptions {
43
+ connectTimeout?: number;
44
+ commandTimeout?: number;
45
+ reconnectTimeout?: number;
46
+ }
47
+ export interface ClientOptions extends TimeoutOptions {
48
+ projectPath?: string;
49
+ enableExecute?: boolean;
50
+ }
51
+ export type UniBridgeClient = CommandMethods<typeof allCommands> & {
52
+ readonly projectPath: string;
53
+ close(): void;
54
+ };
55
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAC1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AAEzD,MAAM,WAAW,WAAW;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,MAAM,CAAC,EAAE,SAAS,GAAG,WAAW,CAAA;CACjC;AAED,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,KAAK,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;CACZ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.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,17 +1,43 @@
1
1
  {
2
2
  "name": "@unibridge/sdk",
3
- "version": "0.5.0",
4
- "description": "",
3
+ "version": "0.8.0",
4
+ "description": "UniBridge is a TypeScript SDK for interacting with the Unity Editor.",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
7
7
  "test": "node --test src/*.test.ts src/**/*.test.ts",
8
8
  "build": "tsc -p tsconfig.json",
9
9
  "prepublishOnly": "npm run build"
10
10
  },
11
- "keywords": [],
12
- "author": "",
13
- "license": "ISC",
11
+ "keywords": [
12
+ "unibridge",
13
+ "unity",
14
+ "sdk",
15
+ "typescript",
16
+ "automation"
17
+ ],
18
+ "author": {
19
+ "name": "Marcus Sanatan",
20
+ "url": "https://gameboymarcus.com"
21
+ },
22
+ "license": "MIT",
14
23
  "type": "module",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/msanatan/unibridge.git",
27
+ "directory": "packages/sdk"
28
+ },
29
+ "homepage": "https://github.com/msanatan/unibridge/tree/main/packages/sdk",
30
+ "bugs": {
31
+ "url": "https://github.com/msanatan/unibridge/issues"
32
+ },
33
+ "files": [
34
+ "dist",
35
+ "README.md",
36
+ "LICENSE"
37
+ ],
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
15
41
  "types": "./dist/index.d.ts",
16
42
  "exports": {
17
43
  ".": "./dist/index.js"
package/src/client.ts DELETED
@@ -1,76 +0,0 @@
1
- import { randomUUID } from 'node:crypto'
2
- import { PipeConnection } from './connection.ts'
3
- import { pipePath } from './hash.ts'
4
- import { findUnityProject } from './project.ts'
5
- import type { ClientOptions, CommandResponse, UniBridgeClient } from './types.ts'
6
- import { buildClientMethods } from './commands/define.ts'
7
- import type { CommandRuntime, ExecuteGuard } from './commands/runtime.ts'
8
- import { allCommands } from './commands/registry.ts'
9
-
10
- export class UniBridgeError extends Error {
11
- constructor(message: string) {
12
- super(message)
13
- this.name = 'UniBridgeError'
14
- }
15
- }
16
-
17
- function unwrap(response: CommandResponse): unknown {
18
- if (!response.success) {
19
- throw new UniBridgeError(response.error ?? 'Command failed')
20
- }
21
- return response.result
22
- }
23
-
24
- function createRuntime(
25
- sendCommand: (command: string, params: Record<string, unknown>) => Promise<CommandResponse>,
26
- ensureExecuteEnabled: () => void,
27
- ): CommandRuntime & ExecuteGuard {
28
- return {
29
- async send(command: string, params: Record<string, unknown>): Promise<unknown> {
30
- return unwrap(await sendCommand(command, params))
31
- },
32
- ensureExecuteEnabled,
33
- }
34
- }
35
-
36
- export function createClient(options: ClientOptions = {}): UniBridgeClient {
37
- const projectPath = options.projectPath ?? findUnityProject()
38
- const connection = new PipeConnection({
39
- projectPath,
40
- connectTimeout: options.connectTimeout,
41
- commandTimeout: options.commandTimeout,
42
- reconnectTimeout: options.reconnectTimeout,
43
- })
44
- const callerExecuteEnabled = options.enableExecute ?? true
45
-
46
- async function sendCommand(
47
- command: string,
48
- params: Record<string, unknown>,
49
- ): Promise<CommandResponse> {
50
- await connection.connect(pipePath(projectPath))
51
- return connection.send({ id: randomUUID(), command, params })
52
- }
53
-
54
- function ensureExecuteEnabled(): void {
55
- if (!callerExecuteEnabled) {
56
- throw new UniBridgeError('Execute is disabled by client or plugin configuration.')
57
- }
58
-
59
- const metadata = connection.serverMetadata()
60
- const serverExecuteEnabled = metadata?.capabilities?.executeEnabled ?? true
61
- if (!serverExecuteEnabled) {
62
- throw new UniBridgeError('Execute is disabled by client or plugin configuration.')
63
- }
64
- }
65
-
66
- const runtime = createRuntime(sendCommand, ensureExecuteEnabled)
67
-
68
- return {
69
- projectPath,
70
- ...buildClientMethods(runtime, allCommands),
71
- close(): void {
72
- connection.disconnect()
73
- },
74
- }
75
- }
76
-
@@ -1,4 +0,0 @@
1
- export type { DomainReloadResult } from './domain/contract.ts'
2
- export type { ExecuteResult } from './execute/contract.ts'
3
- export type { SceneActiveResult, SceneCreateResult, SceneInfo, SceneOpenResult } from './scene/contract.ts'
4
- export type { StatusResult } from './status/contract.ts'
@@ -1,56 +0,0 @@
1
- import * as v from 'valibot'
2
- import type { CommandRuntime, ExecuteGuard } from './runtime.ts'
3
-
4
- export interface CommandDef<
5
- TMethod extends string,
6
- TArgs extends unknown[],
7
- TResult,
8
- > {
9
- readonly method: TMethod
10
- readonly wire: string
11
- readonly params: (...args: TArgs) => Record<string, unknown>
12
- readonly result: v.GenericSchema<unknown, TResult>
13
- readonly guard?: 'execute'
14
- }
15
-
16
- export function defineCommand<
17
- TMethod extends string,
18
- TArgs extends unknown[],
19
- TResult,
20
- >(def: CommandDef<TMethod, TArgs, TResult>): CommandDef<TMethod, TArgs, TResult> {
21
- return def
22
- }
23
-
24
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
- export type InferResult<T> = T extends CommandDef<string, any[], infer R> ? R : never
26
-
27
- type CommandMethod<T> = T extends CommandDef<string, infer TArgs, infer TResult>
28
- ? (...args: TArgs) => Promise<TResult>
29
- : never
30
-
31
- export type CommandMethods<T extends readonly CommandDef<string, any[], any>[]> = {
32
- [K in T[number] as K['method']]: CommandMethod<K>
33
- }
34
-
35
- export async function invokeCommand<TArgs extends unknown[], TResult>(
36
- def: CommandDef<string, TArgs, TResult>,
37
- runtime: CommandRuntime & ExecuteGuard,
38
- ...args: TArgs
39
- ): Promise<TResult> {
40
- if (def.guard === 'execute') {
41
- runtime.ensureExecuteEnabled()
42
- }
43
- const params = def.params(...args)
44
- const raw = await runtime.send(def.wire, params)
45
- return v.parse(def.result, raw)
46
- }
47
-
48
- export function buildClientMethods<
49
- const T extends readonly CommandDef<string, any[], any>[],
50
- >(runtime: CommandRuntime & ExecuteGuard, defs: T): CommandMethods<T> {
51
- const methods: Record<string, (...args: unknown[]) => Promise<unknown>> = {}
52
- for (const def of defs) {
53
- methods[def.method] = (...args: unknown[]) => invokeCommand(def, runtime, ...args)
54
- }
55
- return methods as CommandMethods<T>
56
- }
@@ -1,15 +0,0 @@
1
- import * as v from 'valibot'
2
- import { defineCommand, type InferResult } from '../define.ts'
3
-
4
- export const DomainReloadResultSchema = v.object({
5
- triggered: v.boolean(),
6
- })
7
-
8
- export const domainReloadCommand = defineCommand({
9
- method: 'domainReload',
10
- wire: 'domain.reload',
11
- params: () => ({}),
12
- result: DomainReloadResultSchema,
13
- })
14
-
15
- export type DomainReloadResult = InferResult<typeof domainReloadCommand>
@@ -1,12 +0,0 @@
1
- import * as v from 'valibot'
2
- import { defineCommand, type InferResult } from '../define.ts'
3
-
4
- export const executeCommand = defineCommand({
5
- method: 'execute',
6
- wire: 'execute',
7
- params: (code: string) => ({ code }),
8
- result: v.unknown(),
9
- guard: 'execute',
10
- })
11
-
12
- export type ExecuteResult = InferResult<typeof executeCommand>
@@ -1,6 +0,0 @@
1
- import { domainReloadCommand } from './domain/contract.ts'
2
- import { executeCommand } from './execute/contract.ts'
3
- import { sceneActiveCommand, sceneCreateCommand, sceneOpenCommand } from './scene/contract.ts'
4
- import { statusCommand } from './status/contract.ts'
5
-
6
- export const allCommands = [domainReloadCommand, executeCommand, statusCommand, sceneActiveCommand, sceneCreateCommand, sceneOpenCommand] as const
@@ -1,7 +0,0 @@
1
- export interface CommandRuntime {
2
- send(command: string, params: Record<string, unknown>): Promise<unknown>
3
- }
4
-
5
- export interface ExecuteGuard {
6
- ensureExecuteEnabled(): void
7
- }
@@ -1,46 +0,0 @@
1
- import * as v from 'valibot'
2
- import { defineCommand, type InferResult } from '../define.ts'
3
-
4
- const SceneInfoSchema = v.object({
5
- name: v.string(),
6
- path: v.string(),
7
- isDirty: v.boolean(),
8
- })
9
-
10
- export const SceneActiveResultSchema = v.object({
11
- scene: SceneInfoSchema,
12
- })
13
-
14
- export const sceneActiveCommand = defineCommand({
15
- method: 'sceneActive',
16
- wire: 'scene.active',
17
- params: () => ({}),
18
- result: SceneActiveResultSchema,
19
- })
20
-
21
- export const SceneOpenResultSchema = v.object({
22
- scene: SceneInfoSchema,
23
- })
24
-
25
- export const sceneOpenCommand = defineCommand({
26
- method: 'sceneOpen',
27
- wire: 'scene.open',
28
- params: (path: string) => ({ path }),
29
- result: SceneOpenResultSchema,
30
- })
31
-
32
- export const SceneCreateResultSchema = v.object({
33
- scene: SceneInfoSchema,
34
- })
35
-
36
- export const sceneCreateCommand = defineCommand({
37
- method: 'sceneCreate',
38
- wire: 'scene.create',
39
- params: (path: string) => ({ path }),
40
- result: SceneCreateResultSchema,
41
- })
42
-
43
- export type SceneInfo = v.InferOutput<typeof SceneInfoSchema>
44
- export type SceneActiveResult = InferResult<typeof sceneActiveCommand>
45
- export type SceneOpenResult = InferResult<typeof sceneOpenCommand>
46
- export type SceneCreateResult = InferResult<typeof sceneCreateCommand>
@@ -1,19 +0,0 @@
1
- import * as v from 'valibot'
2
- import { defineCommand, type InferResult } from '../define.ts'
3
-
4
- export const StatusResultSchema = v.object({
5
- projectPath: v.string(),
6
- unityVersion: v.string(),
7
- pluginVersion: v.string(),
8
- activeScene: v.string(),
9
- playMode: v.string(),
10
- })
11
-
12
- export const statusCommand = defineCommand({
13
- method: 'status',
14
- wire: 'status',
15
- params: () => ({}),
16
- result: StatusResultSchema,
17
- })
18
-
19
- export type StatusResult = InferResult<typeof statusCommand>