From a66c99119bba028425d3ee855a3d2faf0eb6b739 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 22 Nov 2024 01:40:54 -0800 Subject: [PATCH] Add `Switch.FileSystem.openSdmc()` --- .changeset/calm-carpets-visit.md | 5 +++++ packages/runtime/src/$.ts | 1 + packages/runtime/src/switch/file-system.ts | 15 +++++++++++++++ source/fsdev.c | 16 ++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 .changeset/calm-carpets-visit.md diff --git a/.changeset/calm-carpets-visit.md b/.changeset/calm-carpets-visit.md new file mode 100644 index 00000000..c8f8aec9 --- /dev/null +++ b/.changeset/calm-carpets-visit.md @@ -0,0 +1,5 @@ +--- +"@nx.js/runtime": patch +--- + +Add `Switch.FileSystem.openSdmc()` diff --git a/packages/runtime/src/$.ts b/packages/runtime/src/$.ts index ff597785..e4d02100 100644 --- a/packages/runtime/src/$.ts +++ b/packages/runtime/src/$.ts @@ -164,6 +164,7 @@ export interface Init { fsInit(c: ClassOf): void; fsMount(fs: FileSystem, name: string): void; fsOpenBis(id: number): FileSystem; + fsOpenSdmc(): 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 fb475733..9ea3d930 100644 --- a/packages/runtime/src/switch/file-system.ts +++ b/packages/runtime/src/switch/file-system.ts @@ -66,5 +66,20 @@ export class FileSystem { static openBis(id: number): FileSystem { return proto($.fsOpenBis(id), FileSystem); } + + /** + * Opens a file system partition for the SD card. + * + * @example + * + * ```typescript + * const fs = Switch.FileSystem.openSdmc(); + * console.log(fs.freeSpace()); + * console.log(fs.totalSpace()); + * ``` + */ + static openSdmc(): FileSystem { + return proto($.fsOpenSdmc(), FileSystem); + } } $.fsInit(FileSystem); diff --git a/source/fsdev.c b/source/fsdev.c index 7deb62b4..627221eb 100644 --- a/source/fsdev.c +++ b/source/fsdev.c @@ -102,6 +102,21 @@ static JSValue nx_fs_open_bis(JSContext *ctx, JSValueConst this_val, int argc, return obj; } +static JSValue nx_fs_open_sdmc(JSContext *ctx, JSValueConst this_val, int argc, + JSValueConst *argv) { + nx_file_system_t *file_system = js_mallocz(ctx, sizeof(nx_file_system_t)); + if (!file_system) { + return JS_EXCEPTION; + } + Result rc = fsOpenSdCardFileSystem(&file_system->fs); + if (R_FAILED(rc)) { + return nx_throw_libnx_error(ctx, rc, "fsOpenSdCardFileSystem()"); + } + 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 = @@ -774,6 +789,7 @@ static const JSCFunctionListEntry function_list[] = { JS_CFUNC_DEF("fsInit", 1, nx_fs_init), 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("saveDataInit", 1, nx_save_data_init), JS_CFUNC_DEF("saveDataMount", 1, nx_save_data_mount),