Skip to content

Commit 66ceb15

Browse files
committed
Allow Switch.Application "icon" to be larger than 131072 (0x20000) bytes
1 parent 7f6e4d7 commit 66ceb15

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

.changeset/three-chefs-sell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nx.js/runtime": patch
3+
---
4+
5+
Allow `Switch.Application` "icon" to be larger than 131072 (0x20000) bytes

source/ns.c

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ nx_app_t *nx_get_app(JSContext *ctx, JSValueConst obj) {
1212
static void finalizer_app(JSRuntime *rt, JSValue val) {
1313
nx_app_t *app = JS_GetOpaque(val, nx_app_class_id);
1414
if (app) {
15+
if (app->icon) {
16+
js_free_rt(rt, app->icon);
17+
}
1518
js_free_rt(rt, app);
1619
}
1720
}
@@ -84,11 +87,16 @@ static JSValue nx_ns_app_new(JSContext *ctx, JSValueConst this_val, int argc,
8487

8588
// Seek to the icon section offset and read the icon data
8689
fseek(file, asset_header_offset + icon_section_offset, SEEK_SET);
87-
fread(&data->data.icon, icon_section_size, 1, file);
90+
data->icon = js_malloc(ctx, icon_section_size);
91+
if (!data->icon) {
92+
fclose(file);
93+
return JS_EXCEPTION;
94+
}
95+
fread(data->icon, icon_section_size, 1, file);
8896

8997
// Seek to the nacp section offset and read the nacp data
9098
fseek(file, asset_header_offset + nacp_section_offset, SEEK_SET);
91-
fread(&data->data.nacp, nacp_section_size, 1, file);
99+
fread(&data->nacp, nacp_section_size, 1, file);
92100

93101
free(asset_header);
94102
fclose(file);
@@ -106,24 +114,35 @@ static JSValue nx_ns_app_new(JSContext *ctx, JSValueConst this_val, int argc,
106114
u32 icon_section_size = *(u32 *)(asset_header + 0x10);
107115
u32 nacp_section_offset = *(u32 *)(asset_header + 0x18);
108116
u32 nacp_section_size = *(u32 *)(asset_header + 0x20);
109-
memcpy(&data->data.icon, asset_header + icon_section_offset,
117+
data->icon = js_mallocz(ctx, icon_section_size);
118+
if (!data->icon) {
119+
return JS_EXCEPTION;
120+
}
121+
memcpy(data->icon, asset_header + icon_section_offset,
110122
icon_section_size);
111-
memcpy(&data->data.nacp, asset_header + nacp_section_offset,
123+
memcpy(&data->nacp, asset_header + nacp_section_offset,
112124
nacp_section_size);
113125
data->icon_size = icon_section_size;
114126
loaded = true;
115127
}
116128

117129
if (!loaded) {
118130
size_t outSize;
131+
NsApplicationControlData buf;
119132
Result rc = nsGetApplicationControlData(
120-
NsApplicationControlSource_Storage, application_id, &data->data,
121-
sizeof(data->data), &outSize);
133+
NsApplicationControlSource_Storage, application_id, &buf,
134+
sizeof(NsApplicationControlData), &outSize);
122135
if (R_FAILED(rc)) {
123136
return nx_throw_libnx_error(ctx, rc,
124137
"nsGetApplicationControlData()");
125138
}
126-
data->icon_size = outSize > 0 ? outSize - sizeof(data->data.nacp) : 0;
139+
data->icon_size = outSize > 0 ? outSize - sizeof(buf.nacp) : 0;
140+
data->icon = js_malloc(ctx, data->icon_size);
141+
if (!data->icon) {
142+
return JS_EXCEPTION;
143+
}
144+
memcpy(data->icon, &buf.icon, data->icon_size);
145+
memcpy(&data->nacp, &buf.nacp, sizeof(NacpStruct));
127146
}
128147

129148
JSValue app = JS_NewObjectClass(ctx, nx_app_class_id);
@@ -137,7 +156,7 @@ static JSValue nx_ns_app_id(JSContext *ctx, JSValueConst this_val, int argc,
137156
if (!app) {
138157
return JS_EXCEPTION;
139158
}
140-
return JS_NewBigUint64(ctx, app->data.nacp.presence_group_id);
159+
return JS_NewBigUint64(ctx, app->nacp.presence_group_id);
141160
}
142161

143162
static JSValue nx_ns_app_nacp(JSContext *ctx, JSValueConst this_val, int argc,
@@ -146,8 +165,7 @@ static JSValue nx_ns_app_nacp(JSContext *ctx, JSValueConst this_val, int argc,
146165
if (!app) {
147166
return JS_EXCEPTION;
148167
}
149-
return JS_NewArrayBufferCopy(ctx, (uint8_t *)&app->data.nacp,
150-
sizeof(app->data.nacp));
168+
return JS_NewArrayBufferCopy(ctx, (uint8_t *)&app->nacp, sizeof(app->nacp));
151169
}
152170

153171
static JSValue nx_ns_app_icon(JSContext *ctx, JSValueConst this_val, int argc,
@@ -159,8 +177,7 @@ static JSValue nx_ns_app_icon(JSContext *ctx, JSValueConst this_val, int argc,
159177
if (app->icon_size <= 0) {
160178
return JS_UNDEFINED;
161179
}
162-
return JS_NewArrayBufferCopy(ctx, (uint8_t *)&app->data.icon,
163-
app->icon_size);
180+
return JS_NewArrayBufferCopy(ctx, (uint8_t *)app->icon, app->icon_size);
164181
}
165182

166183
static JSValue nx_ns_app_name(JSContext *ctx, JSValueConst this_val, int argc,
@@ -170,7 +187,7 @@ static JSValue nx_ns_app_name(JSContext *ctx, JSValueConst this_val, int argc,
170187
return JS_EXCEPTION;
171188
}
172189
NacpLanguageEntry *langEntry;
173-
Result rc = nacpGetLanguageEntry(&app->data.nacp, &langEntry);
190+
Result rc = nacpGetLanguageEntry(&app->nacp, &langEntry);
174191
if (R_FAILED(rc)) {
175192
return nx_throw_libnx_error(ctx, rc, "nacpGetLanguageEntry()");
176193
}
@@ -188,7 +205,7 @@ static JSValue nx_ns_app_author(JSContext *ctx, JSValueConst this_val, int argc,
188205
return JS_EXCEPTION;
189206
}
190207
NacpLanguageEntry *langEntry;
191-
Result rc = nacpGetLanguageEntry(&app->data.nacp, &langEntry);
208+
Result rc = nacpGetLanguageEntry(&app->nacp, &langEntry);
192209
if (R_FAILED(rc)) {
193210
return nx_throw_libnx_error(ctx, rc, "nacpGetLanguageEntry()");
194211
}
@@ -227,7 +244,7 @@ static JSValue nx_ns_app_version(JSContext *ctx, JSValueConst this_val,
227244
if (!app) {
228245
return JS_EXCEPTION;
229246
}
230-
return JS_NewString(ctx, app->data.nacp.display_version);
247+
return JS_NewString(ctx, app->nacp.display_version);
231248
}
232249

233250
static JSValue nx_ns_app_init(JSContext *ctx, JSValueConst this_val, int argc,

source/ns.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#include "types.h"
33

44
typedef struct {
5+
void *icon;
56
size_t icon_size;
6-
NsApplicationControlData data;
7+
NacpStruct nacp;
78
} nx_app_t;
89

910
nx_app_t *nx_get_app(JSContext *ctx, JSValueConst obj);

0 commit comments

Comments
 (0)