Skip to content

Commit

Permalink
Enable localStorage based on "userAccountSaveDataSize"
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Dec 9, 2024
1 parent aeb147d commit fdbd150
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-camels-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nx.js/runtime": patch
---

Enable `localStorage` based on "userAccountSaveDataSize"
11 changes: 7 additions & 4 deletions packages/runtime/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { $ } from './$';
import { readFileSync, removeSync, writeFileSync } from './fs';
import { URL } from './polyfills/url';
import { Profile } from './switch/profile';
import { Application, getSaveDataOwnerId } from './switch/ns';
import { Application } from './switch/ns';
import { INTERNAL_SYMBOL } from './internal';
import {
def,
Expand Down Expand Up @@ -121,12 +121,15 @@ Object.defineProperty(globalThis, 'localStorage', {
get() {
const { self } = Application;

// If the app's NACP dpes not contain a save data owner ID,
// If the app's NACP does not define a `userAccountSaveDataSize`,
// then `localStorage` returns `undefined`. This can be useful
// to prevent the profile selector from being shown when 3rd
// party modules unwantingly attempt to access `localStorage`.
const saveDataOwnerId = getSaveDataOwnerId(self);
if (!saveDataOwnerId) {
const userAccountSaveDataSize = new DataView(self.nacp).getBigUint64(
0x3080,
true,
);
if (!userAccountSaveDataSize) {
Object.defineProperty(globalThis, 'localStorage', { value: undefined });
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/switch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export * from '../fs';
export * from './inspect';
export * from './savedata';
export * from './nifm';
export { Application } from './ns';
export * from './ns';
export * from './irsensor';
export * from './profile';
export * from './album';
Expand Down
5 changes: 1 addition & 4 deletions packages/runtime/src/switch/ns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ function _init() {
init = true;
}

export const getSaveDataOwnerId = (app: Application) =>
new DataView(app.nacp).getBigUint64(0x3078, true);

/**
* Represents an installed application (game) on the console,
* or a homebrew application (`.nro` file).
Expand Down Expand Up @@ -172,7 +169,7 @@ export class Application {
}

filterSaveData(fn?: (saveData: SaveData) => boolean) {
const id = getSaveDataOwnerId(this);
const id = new DataView(this.nacp).getBigUint64(0x3078, true);
return Iterator.from(SaveData).filter((s) => {
if (s.applicationId !== id) return false;
return fn ? fn(s) : true;
Expand Down

0 comments on commit fdbd150

Please sign in to comment.