Skip to content

Commit

Permalink
Add FsDev#url, remove FsDev#name
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Mar 23, 2024
1 parent 8f3df37 commit 8d00088
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-pillows-unite.md
@@ -0,0 +1,5 @@
---
"nxjs-runtime": patch
---

Add `FsDev#url`, remove `FsDev#name`
9 changes: 5 additions & 4 deletions packages/runtime/src/switch/fsdev.ts
@@ -1,13 +1,14 @@
import { $ } from '../$';
import { URL } from '../polyfills/url';

/**
* Represents a mounted filesystem device, such as the Save Data store for an Application / Profile pair.
*/
export class FsDev {
name: string;
url: URL;

constructor(name: string) {
this.name = name;
this.url = new URL(`${name}:/`);
}

/**
Expand All @@ -25,7 +26,7 @@ export class FsDev {
* ```
*/
commit() {
$.fsdevCommitDevice(this.name);
$.fsdevCommitDevice(this.url.protocol);
}

/**
Expand All @@ -42,6 +43,6 @@ export class FsDev {
* ```
*/
unmount() {
$.fsdevUnmountDevice(this.name);
$.fsdevUnmountDevice(this.url.protocol);
}
}
118 changes: 88 additions & 30 deletions source/fsdev.c
@@ -1,5 +1,14 @@
#include "fsdev.h"

void strip_trailing_colon(char *str)
{
size_t len = strlen(str);
if (str[len - 1] == ':')
{
str[len - 1] = '\0';
}
}

static JSValue nx_fsdev_create_save_data(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
AccountUid uid;
Expand All @@ -12,37 +21,38 @@ static JSValue nx_fsdev_create_save_data(JSContext *ctx, JSValueConst this_val,

size_t nacp_size;
NacpStruct *nacp = (NacpStruct *)JS_GetArrayBuffer(ctx, &nacp_size, argv[0]);
if (nacp_size != sizeof(NacpStruct)) {
if (nacp_size != sizeof(NacpStruct))
{
return JS_ThrowTypeError(ctx, "Invalid NACP buffer (got %ld bytes, expected %ld)", nacp_size, sizeof(NacpStruct));
}

FsSaveDataAttribute attr;
FsSaveDataCreationInfo info;
FsSaveDataMetaInfo meta;
memset(&attr, 0, sizeof(attr));
memset(&info, 0, sizeof(info));
memset(&meta, 0, sizeof(meta));
FsSaveDataAttribute attr;
FsSaveDataCreationInfo info;
FsSaveDataMetaInfo meta;
memset(&attr, 0, sizeof(attr));
memset(&info, 0, sizeof(info));
memset(&meta, 0, sizeof(meta));

attr.application_id = nacp->save_data_owner_id;
attr.uid = uid;
attr.system_save_data_id = 0;
attr.save_data_type = FsSaveDataType_Account;
attr.save_data_rank = 0;
attr.save_data_index = 0;
attr.application_id = nacp->save_data_owner_id;
attr.uid = uid;
attr.system_save_data_id = 0;
attr.save_data_type = FsSaveDataType_Account;
attr.save_data_rank = 0;
attr.save_data_index = 0;

info.journal_size = nacp->user_account_save_data_journal_size;
info.save_data_size = nacp->user_account_save_data_size;
info.available_size = 0x4000;
info.owner_id = nacp->save_data_owner_id;
info.flags = 0;
info.save_data_space_id = FsSaveDataSpaceId_User;
info.journal_size = nacp->user_account_save_data_journal_size;
info.save_data_size = nacp->user_account_save_data_size;
info.available_size = 0x4000;
info.owner_id = nacp->save_data_owner_id;
info.flags = 0;
info.save_data_space_id = FsSaveDataSpaceId_User;

meta.size = 0x40060;
meta.type = FsSaveDataMetaType_Thumbnail;
meta.size = 0x40060;
meta.type = FsSaveDataMetaType_Thumbnail;

Result rc = fsCreateSaveDataFileSystem(&attr, &info, &meta);
if (R_FAILED(rc))
{
Result rc = fsCreateSaveDataFileSystem(&attr, &info, &meta);
if (R_FAILED(rc))
{
JSValue err = JS_NewError(ctx);
u32 module = R_MODULE(rc);
u32 desc = R_DESCRIPTION(rc);
Expand All @@ -53,7 +63,7 @@ static JSValue nx_fsdev_create_save_data(JSContext *ctx, JSValueConst this_val,
JS_SetPropertyStr(ctx, err, "description", JS_NewUint32(ctx, desc));
JS_SetPropertyStr(ctx, err, "value", JS_NewUint32(ctx, R_VALUE(rc)));
return JS_Throw(ctx, err);
}
}

return JS_UNDEFINED;
}
Expand All @@ -69,14 +79,16 @@ static JSValue nx_fsdev_mount_save_data(JSContext *ctx, JSValueConst this_val, i
}
size_t nacp_size;
NacpStruct *nacp = (NacpStruct *)JS_GetArrayBuffer(ctx, &nacp_size, argv[1]);
if (nacp_size != sizeof(NacpStruct)) {
if (nacp_size != sizeof(NacpStruct))
{
return JS_ThrowTypeError(ctx, "Invalid NACP buffer (got %ld bytes, expected %ld)", nacp_size, sizeof(NacpStruct));
}
const char *name = JS_ToCString(ctx, argv[0]);
if (!name)
{
return JS_EXCEPTION;
}
strip_trailing_colon((char *)name);
Result rc = fsdevMountSaveData(name, nacp->save_data_owner_id, uid);
JS_FreeCString(ctx, name);
if (R_FAILED(rc))
Expand All @@ -90,19 +102,60 @@ static JSValue nx_fsdev_mount_save_data(JSContext *ctx, JSValueConst this_val, i
JS_SetPropertyStr(ctx, err, "module", JS_NewUint32(ctx, module));
JS_SetPropertyStr(ctx, err, "description", JS_NewUint32(ctx, desc));
JS_SetPropertyStr(ctx, err, "value", JS_NewUint32(ctx, R_VALUE(rc)));
//JSValue argv_arr = JS_NewArray(ctx);
//for (int i = 0; i < argc; i++) {
// JSValue argv_arr = JS_NewArray(ctx);
// for (int i = 0; i < argc; i++) {
// JS_SetPropertyUint32(ctx, argv_arr, i, JS_DupValue(ctx, argv[i]));
//}
//JS_SetPropertyStr(ctx, err, "argv", argv_arr);
// }
// JS_SetPropertyStr(ctx, err, "argv", argv_arr);
return JS_Throw(ctx, err);
}
return JS_UNDEFINED;
}

// static JSValue nx_fsdev_mount_cache_storage(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
//{
// size_t nacp_size;
// NacpStruct *nacp = (NacpStruct *)JS_GetArrayBuffer(ctx, &nacp_size, argv[0]);
// if (nacp_size != sizeof(NacpStruct)) {
// return JS_ThrowTypeError(ctx, "Invalid NACP buffer (got %ld bytes, expected %ld)", nacp_size, sizeof(NacpStruct));
// }
// const char *name = JS_ToCString(ctx, argv[0]);
// if (!name)
// {
// return JS_EXCEPTION;
// }
// strip_trailing_colon(name);
// Result rc = fsdevMountCacheStorage(name, nacp->cache, uid);
// JS_FreeCString(ctx, name);
// if (R_FAILED(rc))
// {
// JSValue err = JS_NewError(ctx);
// u32 module = R_MODULE(rc);
// u32 desc = R_DESCRIPTION(rc);
// char message[256];
// snprintf(message, 256, "fsdevMountSaveData() failed (module: %u, description: %u)", module, desc);
// JS_DefinePropertyValueStr(ctx, err, "message", JS_NewString(ctx, message), JS_PROP_C_W);
// JS_SetPropertyStr(ctx, err, "module", JS_NewUint32(ctx, module));
// JS_SetPropertyStr(ctx, err, "description", JS_NewUint32(ctx, desc));
// JS_SetPropertyStr(ctx, err, "value", JS_NewUint32(ctx, R_VALUE(rc)));
// //JSValue argv_arr = JS_NewArray(ctx);
// //for (int i = 0; i < argc; i++) {
// // JS_SetPropertyUint32(ctx, argv_arr, i, JS_DupValue(ctx, argv[i]));
// //}
// //JS_SetPropertyStr(ctx, err, "argv", argv_arr);
// return JS_Throw(ctx, err);
// }
// return JS_UNDEFINED;
// }

static JSValue nx_fsdev_commit_device(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
const char *name = JS_ToCString(ctx, argv[0]);
if (!name)
{
return JS_EXCEPTION;
}
strip_trailing_colon((char *)name);
Result rc = fsdevCommitDevice(name);
JS_FreeCString(ctx, name);
if (R_FAILED(rc))
Expand All @@ -116,6 +169,11 @@ static JSValue nx_fsdev_commit_device(JSContext *ctx, JSValueConst this_val, int
static JSValue nx_fsdev_unmount_device(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
const char *name = JS_ToCString(ctx, argv[0]);
if (!name)
{
return JS_EXCEPTION;
}
strip_trailing_colon((char *)name);
Result rc = fsdevUnmountDevice(name);
JS_FreeCString(ctx, name);
if (R_FAILED(rc))
Expand Down

0 comments on commit 8d00088

Please sign in to comment.