Skip to content

Commit

Permalink
Refactor to utilize ArrayBufferStruct subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Dec 23, 2024
1 parent 61278b4 commit b7aadc6
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 79 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-crews-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nx.js/ncm": patch
---

Refactor to utilize `ArrayBufferStruct` subclasses
3 changes: 2 additions & 1 deletion packages/ncm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"access": "public"
},
"dependencies": {
"@nx.js/constants": "^0.3.0"
"@nx.js/constants": "^0.3.0",
"@nx.js/util": "^0.0.0"
},
"devDependencies": {
"@nx.js/runtime": "workspace:*"
Expand Down
70 changes: 54 additions & 16 deletions packages/ncm/src/content-meta-database.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { SfBufferAttr } from '@nx.js/constants';
import { ncm } from './service';
import type {
import {
NcmContentId,
NcmContentMetaKey,
NcmContentType,
NcmStorageId,
} from './types';
import { u8, view } from '@nx.js/util';

export class NcmContentMetaDatabase {
static open(storageId: NcmStorageId) {
Expand All @@ -17,7 +18,7 @@ export class NcmContentMetaDatabase {
//}
const out = new Switch.Service();
const inArr = new Uint8Array([storageId]);
ncm.dispatchIn(5, inArr.buffer, {
ncm.dispatchIn(5, inArr, {
outObjects: [out],
});
return new NcmContentMetaDatabase(out);
Expand All @@ -32,7 +33,7 @@ export class NcmContentMetaDatabase {
this.#srv = srv;
}

set(key: NcmContentMetaKey, data: ArrayBuffer) {
set(key: NcmContentMetaKey, data: ArrayBuffer | ArrayBufferView) {
//Result ncmContentMetaDatabaseSet(NcmContentMetaDatabase* db, const NcmContentMetaKey* key, const void* data, u64 data_size) {
// return serviceDispatchIn(&db->s, 0, *key,
// .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In },
Expand All @@ -45,7 +46,7 @@ export class NcmContentMetaDatabase {
});
}

get(key: NcmContentMetaKey, data: ArrayBuffer): bigint {
getInto(key: NcmContentMetaKey, data: ArrayBufferView): bigint {
//Result ncmContentMetaDatabaseGet(NcmContentMetaDatabase* db, const NcmContentMetaKey* key, u64* out_size, void* out_data, u64 out_data_size) {
// return serviceDispatchInOut(&db->s, 1, *key, *out_size,
// .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out },
Expand All @@ -60,6 +61,16 @@ export class NcmContentMetaDatabase {
return out[0];
}

get(key: NcmContentMetaKey): ArrayBufferView {
const expectedSize = this.getSize(key);
const data = new Uint8Array(Number(expectedSize));
const size = this.getInto(key, data);
if (size !== expectedSize) {
throw new Error(`Unexpected size: ${size} (expected: ${expectedSize})`);
}
return data;
}

delete(key: NcmContentMetaKey) {
//Result ncmContentMetaDatabaseRemove(NcmContentMetaDatabase* db, const NcmContentMetaKey *key) {
// return serviceDispatchIn(&db->s, 2, *key);
Expand All @@ -79,13 +90,41 @@ export class NcmContentMetaDatabase {
// } in = { type, {0}, *key };
// return serviceDispatchInOut(&db->s, 3, in, *out_content_id);
//}
const inData = new ArrayBuffer(0x18);
const inArr = new Uint8Array(inData);
inArr[0] = type;
inArr.set(new Uint8Array(key), 0x8);
const out = new ArrayBuffer(0x10);
const inData = new Uint8Array(0x18);
inData[0] = type;
inData.set(u8(key), 0x8);
const out = new NcmContentId();
this.#srv.dispatchInOut(3, inData, out);
return out as NcmContentId;
return out;
}

listContentInfo(
key: NcmContentMetaKey,
startIndex: number,
outInfo: ArrayBuffer,
) {
//Result ncmContentMetaDatabaseListContentInfo(NcmContentMetaDatabase* db, s32* out_entries_written, NcmContentInfo* out_info, s32 count, const NcmContentMetaKey* key, s32 start_index) {
// const struct {
// s32 start_index;
// u32 pad;
// NcmContentMetaKey key;
// } in = { start_index, 0, *key };
// return serviceDispatchInOut(&db->s, 4, in, *out_entries_written,
// .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out },
// .buffers = { { out_info, count*sizeof(NcmContentInfo) } },
// );
//}
const inData = new Uint8Array(0x18);
const inView = view(inData);
inView.setInt32(0, startIndex ?? 0, true);
inView.setUint32(4, 0, true);
inData.set(u8(key), 0x8);
const out = new Int32Array(1);
this.#srv.dispatchInOut(4, inData, out, {
bufferAttrs: [SfBufferAttr.HipcMapAlias | SfBufferAttr.Out],
buffers: [outInfo],
});
return out[0];
}

has(key: NcmContentMetaKey): boolean {
Expand All @@ -96,7 +135,7 @@ export class NcmContentMetaDatabase {
// return rc;
//}
const out = new Uint8Array(1);
this.#srv.dispatchInOut(8, key, out.buffer);
this.#srv.dispatchInOut(8, key, out);
return Boolean(out[0] & 1);
}

Expand All @@ -110,13 +149,12 @@ export class NcmContentMetaDatabase {
// if (R_SUCCEEDED(rc) && out) *out = tmp & 1;
// return rc;
//}
const inData = new ArrayBuffer(keys.length * 0x10);
const inArr = new Uint8Array(inData);
const inData = new Uint8Array(keys.length * NcmContentMetaKey.sizeof);
for (let i = 0; i < keys.length; i++) {
inArr.set(new Uint8Array(keys[i]), i * 0x10);
inData.set(u8(keys[i]), i * 0x10);
}
const out = new Uint8Array(1);
this.#srv.dispatchOut(9, out.buffer, {
this.#srv.dispatchOut(9, out, {
bufferAttrs: [SfBufferAttr.HipcMapAlias | SfBufferAttr.In],
buffers: [inData],
});
Expand All @@ -128,7 +166,7 @@ export class NcmContentMetaDatabase {
// return serviceDispatchInOut(&db->s, 10, *key, *out_size);
//}
const out = new BigUint64Array(1);
this.#srv.dispatchInOut(10, key, out.buffer);
this.#srv.dispatchInOut(10, key, out);
return out[0];
}

Expand Down
66 changes: 31 additions & 35 deletions packages/ncm/src/content-storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SfBufferAttr } from '@nx.js/constants';
import { ncm } from './service';
import type { NcmContentId, NcmPlaceHolderId, NcmStorageId } from './types';
import { NcmContentId, NcmPlaceHolderId, NcmStorageId } from './types';
import { decodeCString, u8, view } from '@nx.js/util';

export class NcmContentStorage {
static open(storageId: NcmStorageId) {
Expand All @@ -12,7 +13,7 @@ export class NcmContentStorage {
//}
const out = new Switch.Service();
const inArr = new Uint8Array([storageId]);
ncm.dispatchIn(4, inArr.buffer, {
ncm.dispatchIn(4, inArr, {
outObjects: [out],
});
return new NcmContentStorage(out);
Expand All @@ -31,9 +32,9 @@ export class NcmContentStorage {
//Result ncmContentStorageGeneratePlaceHolderId(NcmContentStorage* cs, NcmPlaceHolderId* out_id) {
// return serviceDispatchOut(&cs->s, 0, *out_id);
//}
const uuid = new ArrayBuffer(0x10);
const uuid = new NcmPlaceHolderId();
this.#srv.dispatchOut(0, uuid);
return uuid as NcmPlaceHolderId;
return uuid;
}

createPlaceHolder(
Expand All @@ -58,11 +59,10 @@ export class NcmContentStorage {
// return serviceDispatchIn(&cs->s, 1, in);
// }
//}
const inData = new ArrayBuffer(0x28);
const inDataView = new Uint8Array(inData);
inDataView.set(new Uint8Array(placeholderId), 0);
inDataView.set(new Uint8Array(contentId), 0x10);
new BigInt64Array(inData, 0x20, 1)[0] = size;
const inData = new Uint8Array(0x28);
inData.set(u8(placeholderId), 0);
inData.set(u8(contentId), NcmPlaceHolderId.sizeof);
view(inData).setBigInt64(0x20, size, true);
this.#srv.dispatchIn(1, inData);
}

Expand All @@ -81,7 +81,7 @@ export class NcmContentStorage {
// return rc;
//}
const out = new Uint8Array(1);
this.#srv.dispatchInOut(3, placeholderId, out.buffer);
this.#srv.dispatchInOut(3, placeholderId, out);
return Boolean(out[0] & 1);
}

Expand All @@ -100,10 +100,9 @@ export class NcmContentStorage {
// .buffers = { { data, data_size } },
// );
//}
const inData = new ArrayBuffer(0x18);
const inDataView = new Uint8Array(inData);
inDataView.set(new Uint8Array(placeholderId), 0);
new BigUint64Array(inData, 0x10, 1)[0] = offset;
const inData = new Uint8Array(0x18);
inData.set(u8(placeholderId), 0);
view(inData).setBigUint64(NcmPlaceHolderId.sizeof, offset, true);
this.#srv.dispatchIn(4, inData, {
bufferAttrs: [SfBufferAttr.HipcMapAlias | SfBufferAttr.In],
buffers: [data],
Expand All @@ -126,10 +125,9 @@ export class NcmContentStorage {
// return serviceDispatchIn(&cs->s, 5, in);
// }
//}
const inData = new ArrayBuffer(0x20);
const inDataView = new Uint8Array(inData);
inDataView.set(new Uint8Array(placeholderId), 0);
inDataView.set(new Uint8Array(contentId), 0x10);
const inData = new Uint8Array(0x20);
inData.set(u8(placeholderId), 0);
inData.set(u8(contentId), NcmPlaceHolderId.sizeof);
this.#srv.dispatchIn(5, inData);
}

Expand All @@ -148,7 +146,7 @@ export class NcmContentStorage {
// return rc;
//}
const out = new Uint8Array(1);
this.#srv.dispatchInOut(7, contentId, out.buffer);
this.#srv.dispatchInOut(7, contentId, out);
return Boolean(out[0] & 1);
}

Expand All @@ -165,15 +163,14 @@ export class NcmContentStorage {
// }
// return rc;
//}
const out = new ArrayBuffer(0x300);
const out = new Uint8Array(0x300);
this.#srv.dispatchIn(8, contentId, {
bufferAttrs: [
SfBufferAttr.FixedSize | SfBufferAttr.HipcPointer | SfBufferAttr.Out,
],
buffers: [out],
});
const nul = new Uint8Array(out).indexOf(0);
return new TextDecoder().decode(out.slice(0, nul));
return decodeCString(out);
}

getPlaceHolderPath(placeholderId: NcmPlaceHolderId): string {
Expand All @@ -189,15 +186,14 @@ export class NcmContentStorage {
// }
// return rc;
//}
const out = new ArrayBuffer(0x300);
const out = new Uint8Array(0x300);
this.#srv.dispatchIn(9, placeholderId, {
bufferAttrs: [
SfBufferAttr.FixedSize | SfBufferAttr.HipcPointer | SfBufferAttr.Out,
],
buffers: [out],
});
const nul = new Uint8Array(out).indexOf(0);
return new TextDecoder().decode(out.slice(0, nul));
return decodeCString(out);
}

cleanupAllPlaceHolder() {
Expand All @@ -214,16 +210,16 @@ export class NcmContentStorage {
// .buffers = { { out_ids, count*sizeof(NcmPlaceHolderId) } },
// );
//}
const outIds = new ArrayBuffer(maxCount * 0x10);
const outIds = new ArrayBuffer(maxCount * NcmPlaceHolderId.sizeof);
const out = new Int32Array(1);
this.#srv.dispatchOut(11, out.buffer, {
this.#srv.dispatchOut(11, out, {
bufferAttrs: [SfBufferAttr.HipcMapAlias | SfBufferAttr.Out],
buffers: [outIds],
});
const outCount = out[0];
const rtn: NcmPlaceHolderId[] = new Array(outCount);
for (let i = 0; i < outCount; i++) {
rtn[i] = outIds.slice(i * 0x10, i * 0x10 + 0x10) as NcmPlaceHolderId;
rtn[i] = new NcmPlaceHolderId(outIds, i * NcmPlaceHolderId.sizeof);
}
return rtn;
}
Expand All @@ -233,7 +229,7 @@ export class NcmContentStorage {
// return serviceDispatchOut(&cs->s, 12, *out_count);
//}
const out = new Int32Array(1);
this.#srv.dispatchOut(12, out.buffer);
this.#srv.dispatchOut(12, out);
return out[0];
}

Expand All @@ -250,14 +246,14 @@ export class NcmContentStorage {
const inData = new Int32Array([startOffset]);
const outIds = new ArrayBuffer(maxCount * 0x10);
const out = new Int32Array(1);
this.#srv.dispatchInOut(13, inData.buffer, out.buffer, {
this.#srv.dispatchInOut(13, inData, out, {
bufferAttrs: [SfBufferAttr.HipcMapAlias | SfBufferAttr.Out],
buffers: [outIds],
});
const outCount = out[0];
const rtn: NcmContentId[] = new Array(outCount);
for (let i = 0; i < outCount; i++) {
rtn[i] = outIds.slice(i * 0x10, i * 0x10 + 0x10) as NcmContentId;
rtn[i] = new NcmContentId(outIds, i * 0x10);
}
return rtn;
}
Expand All @@ -267,7 +263,7 @@ export class NcmContentStorage {
// return _ncmCmdInContentIdOutU64(&cs->s, content_id, (u64*)out_size, 14);
//}
const out = new BigInt64Array(1);
this.#srv.dispatchInOut(14, contentId, out.buffer);
this.#srv.dispatchInOut(14, contentId, out);
return out[0];
}

Expand All @@ -284,7 +280,7 @@ export class NcmContentStorage {
// return _ncmCmdNoInOutU64(&cs->s, (u64*)out_size, 22);
//}
const out = new BigInt64Array(1);
this.#srv.dispatchOut(22, out.buffer);
this.#srv.dispatchOut(22, out);
return out[0];
}

Expand All @@ -294,7 +290,7 @@ export class NcmContentStorage {
// return _ncmCmdNoInOutU64(&cs->s, (u64*)out_size, 23);
//}
const out = new BigInt64Array(1);
this.#srv.dispatchOut(23, out.buffer);
this.#srv.dispatchOut(23, out);
return out[0];
}

Expand All @@ -312,7 +308,7 @@ export class NcmContentStorage {
// return _ncmCmdInPlaceHolderIdOutU64(&cs->s, placeholder_id, (u64*)out_size, 25);
//}
const out = new BigInt64Array(1);
this.#srv.dispatchInOut(25, placeholderId, out.buffer);
this.#srv.dispatchInOut(25, placeholderId, out);
return out[0];
}

Expand Down
Loading

0 comments on commit b7aadc6

Please sign in to comment.