From 3ddb523198def124bdd5f3170cf9f70ecd3f6d45 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sun, 22 Dec 2024 15:43:55 -0800 Subject: [PATCH] Add `Switch.FileSystem.openWithId()` --- .changeset/grumpy-games-sleep.md | 5 ++++ packages/runtime/src/$.ts | 6 +++++ packages/runtime/src/switch/file-system.ts | 28 ++++++++++++++++++++ source/fsdev.c | 30 ++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 .changeset/grumpy-games-sleep.md diff --git a/.changeset/grumpy-games-sleep.md b/.changeset/grumpy-games-sleep.md new file mode 100644 index 00000000..3fbfdd03 --- /dev/null +++ b/.changeset/grumpy-games-sleep.md @@ -0,0 +1,5 @@ +--- +"@nx.js/runtime": patch +--- + +Add `Switch.FileSystem.openWithId()` diff --git a/packages/runtime/src/$.ts b/packages/runtime/src/$.ts index 71e2dfa2..c8333954 100644 --- a/packages/runtime/src/$.ts +++ b/packages/runtime/src/$.ts @@ -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): void; saveDataCreateSync(info: SaveDataCreationInfo, nacp?: ArrayBuffer): void; saveDataMount(saveData: SaveData, name: string): void; diff --git a/packages/runtime/src/switch/file-system.ts b/packages/runtime/src/switch/file-system.ts index 8dd64b91..d9598446 100644 --- a/packages/runtime/src/switch/file-system.ts +++ b/packages/runtime/src/switch/file-system.ts @@ -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); diff --git a/source/fsdev.c b/source/fsdev.c index 3583892d..6d559f47 100644 --- a/source/fsdev.c +++ b/source/fsdev.c @@ -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 = @@ -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),