Skip to content

Commit

Permalink
Add Switch.FileSystem.openWithId()
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Dec 22, 2024
1 parent a7e3d01 commit 3ddb523
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-games-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nx.js/runtime": patch
---

Add `Switch.FileSystem.openWithId()`
6 changes: 6 additions & 0 deletions packages/runtime/src/$.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ export interface Init {
fsMount(fs: FileSystem, name: string): void;
fsOpenBis(id: number): FileSystem;
fsOpenSdmc(): FileSystem;
fsOpenWithId(
titleId: bigint,
type: number,
path: string,
attributes: number,
): FileSystem;
saveDataInit(c: ClassOf<SaveData>): void;
saveDataCreateSync(info: SaveDataCreationInfo, nacp?: ArrayBuffer): void;
saveDataMount(saveData: SaveData, name: string): void;
Expand Down
28 changes: 28 additions & 0 deletions packages/runtime/src/switch/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,33 @@ export class FileSystem {
static openSdmc(): FileSystem {
return proto($.fsOpenSdmc(), FileSystem);
}

/**
* Opens a file system partition for the application with the specified title ID.
* The file system type is specified by the `FsFileSystemType` parameter.
*
* @example
*
* ```typescript
* import { FsFileSystemType } from '@nx.js/constants';
*
* // Open and mount the "User" partition
* const fs = Switch.FileSystem.openWithId(
* 0x0100000000001000n,
* FsFileSystemType.ContentMeta,
* );
* const url = fs.mount();
*
* // Read the file entries at the root of the file system
* console.log(Switch.readDirSync(url));
* ```
*
* @param titleId The title ID of the file system to open.
* @param type The `FsFileSystemType` of the file system to open.
* @param path The base path of the file system to open. Defaults to `/`.
*/
static openWithId(titleId: bigint, type: number, path?: string): FileSystem {
return proto($.fsOpenWithId(titleId, type, path ?? '/', 0), FileSystem);
}
}
$.fsInit(FileSystem);
30 changes: 30 additions & 0 deletions source/fsdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,35 @@ static JSValue nx_fs_open_sdmc(JSContext *ctx, JSValueConst this_val, int argc,
return obj;
}

static JSValue nx_fs_open_with_id(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv) {
u64 title_id;
FsFileSystemType type;
FsContentAttributes attributes;
if (JS_ToBigUint64(ctx, &title_id, argv[0]) ||
JS_ToUint32(ctx, &type, argv[1]) ||
JS_ToUint32(ctx, &attributes, argv[3])) {
return JS_EXCEPTION;
}
const char *path = JS_ToCString(ctx, argv[2]);
if (!path) {
return JS_EXCEPTION;
}
nx_file_system_t *file_system = js_mallocz(ctx, sizeof(nx_file_system_t));
if (!file_system) {
JS_FreeCString(ctx, path);
return JS_EXCEPTION;
}
Result rc = fsOpenFileSystemWithId(&file_system->fs, title_id, type, path,
attributes);
if (R_FAILED(rc)) {
return nx_throw_libnx_error(ctx, rc, "fsOpenFileSystemWithId()");
}
JSValue obj = JS_NewObjectClass(ctx, nx_file_system_class_id);
JS_SetOpaque(obj, file_system);
return obj;
}

static JSValue nx_fs_free_space(JSContext *ctx, JSValueConst this_val, int argc,
JSValueConst *argv) {
nx_file_system_t *file_system =
Expand Down Expand Up @@ -675,6 +704,7 @@ static const JSCFunctionListEntry function_list[] = {
JS_CFUNC_DEF("fsMount", 1, nx_fs_mount),
JS_CFUNC_DEF("fsOpenBis", 1, nx_fs_open_bis),
JS_CFUNC_DEF("fsOpenSdmc", 1, nx_fs_open_sdmc),
JS_CFUNC_DEF("fsOpenWithId", 1, nx_fs_open_with_id),

JS_CFUNC_DEF("saveDataInit", 1, nx_save_data_init),
JS_CFUNC_DEF("saveDataMount", 1, nx_save_data_mount),
Expand Down

0 comments on commit 3ddb523

Please sign in to comment.