-
Notifications
You must be signed in to change notification settings - Fork 6
/
ns.ts
174 lines (159 loc) · 4.16 KB
/
ns.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { $ } from '../$';
import { FsDev } from './fsdev';
import { crypto } from '../crypto';
import { readFileSync } from '../fs';
import { proto, stub } from '../utils';
import type { Profile } from './profile';
let init = false;
let self: Application | undefined;
function _init() {
if (init) return;
addEventListener('unload', $.nsInitialize());
init = true;
}
const genName = () => `s${crypto.randomUUID().replace(/-/g, '').slice(0, 16)}`;
/**
* Represents an installed application (game) on the console,
* or a homebrew application (`.nro` file).
*
* Can be used as an iterator to retrieve the list of installed applications.
*
* @example
*
* ```typescript
* for (const app of Switch.Application) {
* console.log(app.name);
* }
* ```
*/
export class Application {
/**
* The 64-bit unique identifier of the application (`PresenceGroupId`).
*/
declare readonly id: bigint;
/**
* The raw NACP data of the application. Use the `@tootallnate/nacp` module to parse this data.
*/
declare readonly nacp: ArrayBuffer;
/**
* The raw JPEG data for the cover art of the application. Can be decoded with the `Image` class.
*/
declare readonly icon?: ArrayBuffer;
/**
* The name of the application.
*/
declare readonly name: string;
/**
* The version of the application.
*/
declare readonly version: string;
/**
* The author or publisher of the application.
*/
declare readonly author: string;
/**
* Creates an `Application` instance from the ID of an
* installed application.
*
* @example
*
* ```typescript
* const app = new Switch.Application(0x100bc0018138000n);
* console.log(app.name);
* ```
*
* @param id The ID of the installed application.
*/
constructor(id: bigint);
/**
* Creates an `Application` instance from an `ArrayBuffer`
* containing the contents of a `.nro` homebrew application.
*
* @example
*
* ```typescript
* const nro = await Switch.readFile('sdmc:/hbmenu.nro');
* const app = Switch.Application.fromNro(nro);
* console.log(app.name);
* ```
*
* @param nro The contents of the `.nro` file.
*/
constructor(nro: ArrayBuffer);
constructor(v: bigint | ArrayBuffer) {
_init();
return proto($.nsAppNew(v), Application);
}
/**
* Launches the application.
*
* @note This only works for installed applications (__not__ homebrew apps).
*/
launch(): never {
stub();
}
/**
* Creates the Save Data for this {@link Application} for the provided user profile.
*
* @example
*
* ```typescript
* const profile = Switch.currentProfile({ required: true });
* app.createSaveData(profile);
* ```
*
* @param profile The {@link Profile} to create the save data for.
*/
createSaveData(profile: Profile) {
$.fsdevCreateSaveData(this.nacp, profile.uid);
}
/**
* Mounts the save data for this application such that filesystem operations may be used.
*
* @example
*
* ```typescript
* const profile = Switch.currentProfile({ required: true });
* const saveData = app.mountSaveData(profile);
*
* // Use the filesystem functions to do operations on the save mount
* console.log(Switch.readDirSync(saveData.url));
*
* // Make sure to use `saveData.commit()` after any write operations
* const saveStateUrl = new URL('state', saveData.url)
* Switch.writeFileSync(saveStateUrl, 'your app state…');
* saveData.commit();
* ```
*
* @param profile The {@link Profile} which the save data belongs to.
* @param name The name of the device mount for filesystem paths. If not provided, a random name is generated.
*/
mountSaveData(profile: Profile, name = genName()): FsDev {
$.fsdevMountSaveData(name, this.nacp, profile.uid);
return new FsDev(name);
}
/**
* An `Application` instance representing the currently running application.
*/
static get self(): Application {
if (!self) {
_init();
let nro: ArrayBuffer | null = null;
if ($.argv.length) {
nro = readFileSync($.argv[0]);
}
self = proto($.nsAppNew(nro), Application);
}
return self;
}
static *[Symbol.iterator]() {
_init();
let offset = 0;
while (1) {
const id = $.nsAppNext(offset++);
if (!id) break;
yield new Application(id);
}
}
}
$.nsAppInit(Application);