v2.9.0: MBMNET Shop (eigene App, auf Wunsch installierbar)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d264cd93cd
commit
ca77952a9d
@@ -0,0 +1,477 @@
|
||||
// MBMNET Shop - Homebrew für den 3DS, aus der Universal-DB (über den MBMNET-Server).
|
||||
//
|
||||
// Oben: die gewählte App (Symbol, Name, Beschreibung). Unten: Reiter (Kategorien) und die Liste.
|
||||
// A installiert / aktualisiert / startet, Y entfernt, L/R wechselt die Kategorie, X die Sortierung.
|
||||
#include <3ds.h>
|
||||
#include <citro2d.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "store.h"
|
||||
|
||||
#define TOP_W 400
|
||||
#define BOT_W 320
|
||||
#define SCR_H 240
|
||||
|
||||
#define COL_RED C2D_Color32(0xE4, 0x00, 0x2B, 0xFF)
|
||||
#define COL_RED_DARK C2D_Color32(0x9E, 0x00, 0x1E, 0xFF)
|
||||
#define COL_BG C2D_Color32(0x16, 0x19, 0x1F, 0xFF)
|
||||
#define COL_BG2 C2D_Color32(0x22, 0x26, 0x2E, 0xFF)
|
||||
#define COL_CARD C2D_Color32(0x2A, 0x2F, 0x39, 0xFF)
|
||||
#define COL_CARD_HI C2D_Color32(0x3A, 0x41, 0x4E, 0xFF)
|
||||
#define COL_WHITE C2D_Color32(0xFF, 0xFF, 0xFF, 0xFF)
|
||||
#define COL_MUTED C2D_Color32(0x9A, 0xA3, 0xAF, 0xFF)
|
||||
#define COL_GREEN C2D_Color32(0x22, 0xC5, 0x5E, 0xFF)
|
||||
#define COL_AMBER C2D_Color32(0xF5, 0x9E, 0x0B, 0xFF)
|
||||
|
||||
#define TAB_H 26
|
||||
#define ROW_H 40
|
||||
#define LIST_Y (TAB_H + 2)
|
||||
#define VISIBLE_ROWS ((SCR_H - LIST_Y) / ROW_H + 1)
|
||||
|
||||
// Kategorien (Universal-DB) und ihre Reiter
|
||||
typedef struct { const char *label, *match; } Tab;
|
||||
static const Tab TABS[] = {
|
||||
{ "Alle", NULL }, { "Spiele", "game" }, { "Tools", "utility" }, { "Apps", "app" },
|
||||
{ "Emus", "emulator" }, { "Medien", "media" }, { "Meine", "*installed" },
|
||||
};
|
||||
#define TAB_COUNT (int)(sizeof(TABS) / sizeof(TABS[0]))
|
||||
static const char *SORT_NAMES[] = { "Name", "Beliebt", "Neu" };
|
||||
|
||||
static C2D_TextBuf textBuf, measureBuf; // measureBuf nur zum Messen (wird jedes Mal geleert)
|
||||
static C2D_SpriteSheet sheets[8];
|
||||
|
||||
// --- Zeichenhilfen ------------------------------------------------------------
|
||||
|
||||
static void text(const char *s, float x, float y, float size, u32 color, u32 flags)
|
||||
{
|
||||
C2D_Text t;
|
||||
C2D_TextParse(&t, textBuf, s);
|
||||
C2D_TextOptimize(&t);
|
||||
C2D_DrawText(&t, C2D_WithColor | flags, x, y, 0.5f, size, size, color);
|
||||
}
|
||||
|
||||
static void text_wrap(const char *s, float x, float y, float size, u32 color, float width)
|
||||
{
|
||||
C2D_Text t;
|
||||
C2D_TextParse(&t, textBuf, s);
|
||||
C2D_TextOptimize(&t);
|
||||
C2D_DrawText(&t, C2D_WithColor | C2D_WordWrap, x, y, 0.5f, size, size, color, width);
|
||||
}
|
||||
|
||||
static float text_width(const char *s, float size)
|
||||
{
|
||||
C2D_Text t;
|
||||
float w = 0;
|
||||
C2D_TextBufClear(measureBuf);
|
||||
C2D_TextParse(&t, measureBuf, s);
|
||||
C2D_TextGetDimensions(&t, size, size, &w, NULL);
|
||||
return w;
|
||||
}
|
||||
|
||||
// Text auf eine Breite kürzen ("...")
|
||||
static void fit(char *out, size_t len, const char *s, float size, float width)
|
||||
{
|
||||
snprintf(out, len, "%s", s);
|
||||
if (text_width(out, size) <= width) return;
|
||||
size_t n = strlen(out);
|
||||
while (n > 1) {
|
||||
n--;
|
||||
while (n > 0 && (out[n] & 0xC0) == 0x80) n--; // nicht mitten in einem UTF-8-Zeichen
|
||||
snprintf(out + n, len - n, "...");
|
||||
if (text_width(out, size) <= width) return;
|
||||
}
|
||||
}
|
||||
|
||||
static void round_rect(float x, float y, float w, float h, float r, u32 color)
|
||||
{
|
||||
C2D_DrawRectSolid(x + r, y, 0.5f, w - 2 * r, h, color);
|
||||
C2D_DrawRectSolid(x, y + r, 0.5f, w, h - 2 * r, color);
|
||||
C2D_DrawCircleSolid(x + r, y + r, 0.5f, r, color);
|
||||
C2D_DrawCircleSolid(x + w - r, y + r, 0.5f, r, color);
|
||||
C2D_DrawCircleSolid(x + r, y + h - r, 0.5f, r, color);
|
||||
C2D_DrawCircleSolid(x + w - r, y + h - r, 0.5f, r, color);
|
||||
}
|
||||
|
||||
// Einkaufswagen (wie das Symbol der App)
|
||||
static void draw_cart(float x, float y, float size, u32 bg)
|
||||
{
|
||||
float s = size / 100.0f;
|
||||
round_rect(x, y, size, size, 22 * s, COL_RED_DARK);
|
||||
round_rect(x, y, size, size - 4 * s, 22 * s, COL_RED);
|
||||
float t = 8 * s;
|
||||
C2D_DrawLine(x + 13 * s, y + 23 * s, COL_WHITE, x + 23 * s, y + 23 * s, COL_WHITE, t, 0.5f);
|
||||
C2D_DrawLine(x + 23 * s, y + 23 * s, COL_WHITE, x + 33 * s, y + 61 * s, COL_WHITE, t, 0.5f);
|
||||
C2D_DrawLine(x + 33 * s, y + 61 * s, COL_WHITE, x + 30 * s, y + 70 * s, COL_WHITE, t, 0.5f);
|
||||
C2D_DrawLine(x + 30 * s, y + 70 * s, COL_WHITE, x + 78 * s, y + 70 * s, COL_WHITE, t, 0.5f);
|
||||
C2D_DrawTriangle(x + 27 * s, y + 33 * s, COL_WHITE, x + 84 * s, y + 33 * s, COL_WHITE, x + 77 * s, y + 61 * s, COL_WHITE, 0.5f);
|
||||
C2D_DrawTriangle(x + 27 * s, y + 33 * s, COL_WHITE, x + 77 * s, y + 61 * s, COL_WHITE, x + 33 * s, y + 61 * s, COL_WHITE, 0.5f);
|
||||
C2D_DrawCircleSolid(x + 37 * s, y + 80 * s, 0.5f, 7 * s, COL_WHITE);
|
||||
C2D_DrawCircleSolid(x + 72 * s, y + 80 * s, 0.5f, 7 * s, COL_WHITE);
|
||||
(void)bg;
|
||||
}
|
||||
|
||||
static void draw_icon(const App *a, float x, float y, float size)
|
||||
{
|
||||
C2D_SpriteSheet sh = a->sheet >= 0 && a->sheet < 8 ? sheets[a->sheet] : NULL;
|
||||
if (sh && a->icon >= 0 && (size_t)a->icon < C2D_SpriteSheetCount(sh)) {
|
||||
C2D_Image img = C2D_SpriteSheetGetImage(sh, a->icon);
|
||||
float sc = size / (img.subtex->width ? img.subtex->width : 48);
|
||||
C2D_DrawImageAt(img, x, y, 0.5f, NULL, sc, sc);
|
||||
return;
|
||||
}
|
||||
// kein Symbol: Anfangsbuchstabe auf grauem Feld
|
||||
round_rect(x, y, size, size, size / 6, COL_CARD_HI);
|
||||
char first[8] = { 0 };
|
||||
int n = 1;
|
||||
while (n < 4 && (a->title[n] & 0xC0) == 0x80) n++;
|
||||
memcpy(first, a->title, n);
|
||||
text(first, x + size / 2, y + size * 0.2f, size / 48.0f, COL_WHITE, C2D_AlignCenter);
|
||||
}
|
||||
|
||||
static void size_text(char *out, size_t len, u32 bytes)
|
||||
{
|
||||
if (bytes >= 1024 * 1024) snprintf(out, len, "%.1f MB", bytes / (1024.0 * 1024.0));
|
||||
else if (bytes) snprintf(out, len, "%lu KB", (unsigned long)((bytes + 1023) / 1024));
|
||||
else snprintf(out, len, "?");
|
||||
}
|
||||
|
||||
// --- Liste (gefiltert und sortiert) ---------------------------------------------
|
||||
|
||||
static int *view; // Indizes in den Katalog
|
||||
static int viewCount;
|
||||
static int sortMode;
|
||||
|
||||
static int cmp_name(const void *a, const void *b)
|
||||
{
|
||||
return strcasecmp(catalog_app(*(const int *)a)->title, catalog_app(*(const int *)b)->title);
|
||||
}
|
||||
static int cmp_stars(const void *a, const void *b)
|
||||
{
|
||||
const App *x = catalog_app(*(const int *)a), *y = catalog_app(*(const int *)b);
|
||||
if (x->stars != y->stars) return x->stars > y->stars ? -1 : 1;
|
||||
return strcasecmp(x->title, y->title);
|
||||
}
|
||||
static int cmp_new(const void *a, const void *b)
|
||||
{
|
||||
const App *x = catalog_app(*(const int *)a), *y = catalog_app(*(const int *)b);
|
||||
int c = strcmp(y->updated, x->updated);
|
||||
return c ? c : strcasecmp(x->title, y->title);
|
||||
}
|
||||
|
||||
static bool in_tab(const App *a, int tab)
|
||||
{
|
||||
const char *m = TABS[tab].match;
|
||||
if (!m) return true;
|
||||
if (strcmp(m, "*installed") == 0) return a->installed;
|
||||
if (strcmp(m, "utility") == 0)
|
||||
return !strcmp(a->category, "utility") || !strcmp(a->category, "save-tool") || !strcmp(a->category, "plugin");
|
||||
return strcmp(a->category, m) == 0;
|
||||
}
|
||||
|
||||
static void rebuild_view(int tab)
|
||||
{
|
||||
int n = catalog_count();
|
||||
free(view);
|
||||
view = malloc(sizeof(int) * (n ? n : 1));
|
||||
viewCount = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
if (in_tab(catalog_app(i), tab)) view[viewCount++] = i;
|
||||
qsort(view, viewCount, sizeof(int), sortMode == 1 ? cmp_stars : sortMode == 2 ? cmp_new : cmp_name);
|
||||
}
|
||||
|
||||
// --- Bildschirme ---------------------------------------------------------------
|
||||
|
||||
static void draw_status_badge(const App *a, float right, float y)
|
||||
{
|
||||
AppStatus s = app_status(a);
|
||||
if (s == APP_NEW) return;
|
||||
const char *label = s == APP_UPDATE ? "Update" : "Installiert";
|
||||
u32 col = s == APP_UPDATE ? COL_AMBER : COL_GREEN;
|
||||
float w = text_width(label, 0.38f) + 10;
|
||||
round_rect(right - w, y, w, 15, 7, col);
|
||||
text(label, right - w / 2, y + 1.5f, 0.38f, COL_BG, C2D_AlignCenter);
|
||||
}
|
||||
|
||||
static void draw_top(const App *a, int tab, const char *msg, bool armed)
|
||||
{
|
||||
C2D_DrawRectangle(0, 0, 0, TOP_W, SCR_H, COL_BG2, COL_BG2, COL_BG, COL_BG);
|
||||
// Kopfzeile
|
||||
draw_cart(10, 6, 22, COL_BG2);
|
||||
text("MBMNET Shop", 38, 8, 0.55f, COL_WHITE, 0);
|
||||
char info[64];
|
||||
if (catalog_state() == CAT_READY)
|
||||
snprintf(info, sizeof(info), "%s · %d Apps · %s%s", TABS[tab].label, viewCount, SORT_NAMES[sortMode],
|
||||
catalog_offline() ? " · offline" : "");
|
||||
else snprintf(info, sizeof(info), "v" APP_VERSION);
|
||||
text(info, TOP_W - 10, 10, 0.42f, COL_MUTED, C2D_AlignRight);
|
||||
C2D_DrawRectSolid(0, 32, 0.5f, TOP_W, 2, COL_RED);
|
||||
|
||||
if (!a) {
|
||||
draw_cart(TOP_W / 2 - 36, 60, 72, COL_BG2);
|
||||
const char *m = catalog_state() == CAT_READY ? (tab == TAB_COUNT - 1 ? "Noch nichts aus dem Shop installiert."
|
||||
: "Hier gibt es nichts.")
|
||||
: catalog_message();
|
||||
text_wrap(m, 40, 150, 0.5f, COL_MUTED, TOP_W - 80);
|
||||
return;
|
||||
}
|
||||
|
||||
// App-Kopf: Symbol, Name, Autor, Version, Größe
|
||||
round_rect(10, 42, 56, 56, 8, COL_CARD);
|
||||
draw_icon(a, 14, 46, 48);
|
||||
char line[96], size[16];
|
||||
fit(line, sizeof(line), a->title, 0.7f, TOP_W - 90);
|
||||
text(line, 76, 42, 0.7f, COL_WHITE, 0);
|
||||
fit(line, sizeof(line), a->author, 0.45f, TOP_W - 90);
|
||||
text(line, 76, 64, 0.45f, COL_MUTED, 0);
|
||||
size_text(size, sizeof(size), a->size);
|
||||
snprintf(line, sizeof(line), "%s · %s · %s", a->version[0] ? a->version : "ohne Version",
|
||||
size, strcmp(a->kind, "cia") == 0 ? "CIA" : "3DSX");
|
||||
text(line, 76, 82, 0.42f, COL_MUTED, 0);
|
||||
draw_status_badge(a, TOP_W - 10, 82);
|
||||
|
||||
// Beschreibung oder Meldung
|
||||
round_rect(10, 106, TOP_W - 20, 104, 8, COL_CARD);
|
||||
if (msg[0]) {
|
||||
text_wrap(msg, 20, 114, 0.48f, armed ? COL_AMBER : COL_WHITE, TOP_W - 40);
|
||||
if (job_state() == JOB_RUNNING) {
|
||||
float p = job_progress();
|
||||
round_rect(20, 184, TOP_W - 40, 14, 7, COL_BG);
|
||||
if (p >= 0) round_rect(20, 184, 14 + (TOP_W - 54) * (p > 1 ? 1 : p), 14, 7, COL_RED);
|
||||
}
|
||||
} else {
|
||||
char desc[440];
|
||||
snprintf(desc, sizeof(desc), "%s", a->description ? a->description : "");
|
||||
if (strlen(desc) >= sizeof(desc) - 1) {
|
||||
char *cut = strrchr(desc, ' ');
|
||||
if (cut) snprintf(cut, sizeof(desc) - (cut - desc), " ...");
|
||||
}
|
||||
text_wrap(desc, 20, 114, 0.42f, COL_WHITE, TOP_W - 40);
|
||||
}
|
||||
|
||||
// Tastenhilfe
|
||||
AppStatus s = app_status(a);
|
||||
const char *aLabel = s == APP_NEW ? "A: Installieren" : s == APP_UPDATE ? "A: Aktualisieren"
|
||||
: strcmp(a->kind, "cia") == 0 ? "A: Starten" : "A: Neu laden";
|
||||
snprintf(line, sizeof(line), "%s%s L/R: Kategorie X: Sortierung START: Ende", aLabel,
|
||||
s != APP_NEW ? " Y: Entfernen" : "");
|
||||
text(line, TOP_W / 2, 220, 0.38f, COL_MUTED, C2D_AlignCenter);
|
||||
}
|
||||
|
||||
static void draw_bottom(int tab, int selected, int scroll)
|
||||
{
|
||||
C2D_DrawRectSolid(0, 0, 0, BOT_W, SCR_H, COL_BG);
|
||||
// Reiter
|
||||
float w = (float)BOT_W / TAB_COUNT;
|
||||
for (int i = 0; i < TAB_COUNT; i++) {
|
||||
bool on = i == tab;
|
||||
C2D_DrawRectSolid(i * w, 0, 0.5f, w, TAB_H, on ? COL_RED : COL_BG2);
|
||||
text(TABS[i].label, i * w + w / 2, 6, 0.42f, on ? COL_WHITE : COL_MUTED, C2D_AlignCenter);
|
||||
}
|
||||
if (catalog_state() != CAT_READY) {
|
||||
text_wrap(catalog_state() == CAT_ERROR ? "Kein Katalog. A: nochmal versuchen" : "Einen Moment ...",
|
||||
20, 110, 0.5f, COL_MUTED, BOT_W - 40);
|
||||
return;
|
||||
}
|
||||
// Liste
|
||||
for (int r = 0; r < VISIBLE_ROWS; r++) {
|
||||
int i = scroll + r;
|
||||
if (i >= viewCount) break;
|
||||
const App *a = catalog_app(view[i]);
|
||||
float y = LIST_Y + r * ROW_H;
|
||||
if (i == selected) {
|
||||
C2D_DrawRectSolid(0, y, 0.5f, BOT_W, ROW_H, COL_CARD_HI);
|
||||
C2D_DrawRectSolid(0, y + 6, 0.5f, 4, ROW_H - 12, COL_RED);
|
||||
} else if (i % 2) {
|
||||
C2D_DrawRectSolid(0, y, 0.5f, BOT_W, ROW_H, COL_BG2);
|
||||
}
|
||||
draw_icon(a, 8, y + 4, 32);
|
||||
char line[96];
|
||||
AppStatus s = app_status(a);
|
||||
float right = s == APP_NEW ? BOT_W - 8 : BOT_W - 78;
|
||||
fit(line, sizeof(line), a->title, 0.5f, right - 48);
|
||||
text(line, 48, y + 4, 0.5f, COL_WHITE, 0);
|
||||
fit(line, sizeof(line), a->author, 0.38f, right - 48);
|
||||
text(line, 48, y + 22, 0.38f, COL_MUTED, 0);
|
||||
draw_status_badge(a, BOT_W - 8, y + 12);
|
||||
}
|
||||
// Rollbalken
|
||||
if (viewCount > VISIBLE_ROWS - 1) {
|
||||
float h = SCR_H - LIST_Y, bar = h * (VISIBLE_ROWS - 1) / viewCount;
|
||||
if (bar < 12) bar = 12;
|
||||
float y = LIST_Y + (h - bar) * scroll / (viewCount - (VISIBLE_ROWS - 1));
|
||||
C2D_DrawRectSolid(BOT_W - 3, y, 0.5f, 3, bar, COL_MUTED);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Aktionen --------------------------------------------------------------------
|
||||
|
||||
// Installierte CIA starten (Sprung in die App, der Shop beendet sich)
|
||||
static bool launch(const App *a, char *msg, size_t len)
|
||||
{
|
||||
if (!a->titleId) { snprintf(msg, len, "Starte %s bitte im HOME-Menü.", a->title); return false; }
|
||||
Result rc = APT_PrepareToDoApplicationJump(0, a->titleId, MEDIATYPE_SD);
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
u8 param[0x300] = { 0 }, hmac[0x20] = { 0 };
|
||||
rc = APT_DoApplicationJump(param, sizeof(param), hmac);
|
||||
}
|
||||
if (R_FAILED(rc)) { snprintf(msg, len, "Starte %s bitte im HOME-Menü (%08lX).", a->title, (unsigned long)rc); return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
static void load_sheets(void)
|
||||
{
|
||||
for (int n = 0; n < catalog_sheet_count() && n < 8; n++) {
|
||||
const char *p = catalog_sheet_path(n);
|
||||
if (p[0] && !sheets[n]) sheets[n] = C2D_SpriteSheetLoad(p);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
(void)argc; (void)argv;
|
||||
gfxInitDefault();
|
||||
httpcInit(0);
|
||||
amInit();
|
||||
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
|
||||
C2D_Init(C2D_DEFAULT_MAX_OBJECTS);
|
||||
C2D_Prepare();
|
||||
C3D_RenderTarget *top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);
|
||||
C3D_RenderTarget *bot = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT);
|
||||
textBuf = C2D_TextBufNew(8192);
|
||||
measureBuf = C2D_TextBufNew(512);
|
||||
hidSetRepeatParameters(18, 4);
|
||||
|
||||
catalog_start();
|
||||
int tab = 0, selected = 0, scroll = 0;
|
||||
bool ready = false;
|
||||
char msg[192] = "";
|
||||
int armed = -1; // Sicherheitsabfrage: 0 = installieren, 1 = entfernen
|
||||
bool quit = false;
|
||||
int touchStartY = -1, touchScroll = 0, touchRow = -1;
|
||||
bool dragged = false;
|
||||
|
||||
while (!quit && aptMainLoop()) {
|
||||
hidScanInput();
|
||||
u32 down = hidKeysDown(), rep = hidKeysDownRepeat(), held = hidKeysHeld();
|
||||
bool busy = job_state() == JOB_RUNNING;
|
||||
if (down & KEY_START && !busy) break;
|
||||
|
||||
// Katalog fertig: Symbole laden und Liste aufbauen
|
||||
if (!ready && catalog_state() == CAT_READY) {
|
||||
load_sheets();
|
||||
rebuild_view(tab);
|
||||
ready = true;
|
||||
}
|
||||
if (catalog_state() == CAT_ERROR && down & KEY_A) catalog_start();
|
||||
|
||||
// Ergebnis einer Installation: anzeigen, Liste auffrischen (z. B. Reiter "Meine")
|
||||
JobState js = job_state();
|
||||
if (js == JOB_DONE || js == JOB_FAILED) {
|
||||
snprintf(msg, sizeof(msg), "%s", job_message());
|
||||
job_acknowledge();
|
||||
const App *cur = viewCount ? catalog_app(view[selected]) : NULL;
|
||||
rebuild_view(tab);
|
||||
selected = 0;
|
||||
for (int i = 0; cur && i < viewCount; i++) if (catalog_app(view[i]) == cur) selected = i;
|
||||
}
|
||||
|
||||
if (ready && !busy) {
|
||||
int oldSel = selected, oldTab = tab;
|
||||
if (down & KEY_R) tab = (tab + 1) % TAB_COUNT;
|
||||
if (down & KEY_L) tab = (tab + TAB_COUNT - 1) % TAB_COUNT;
|
||||
if (down & KEY_X) { sortMode = (sortMode + 1) % 3; rebuild_view(tab); selected = scroll = 0; }
|
||||
if (rep & (KEY_DOWN | KEY_CPAD_DOWN)) selected++;
|
||||
if (rep & (KEY_UP | KEY_CPAD_UP)) selected--;
|
||||
if (rep & (KEY_RIGHT | KEY_CPAD_RIGHT)) selected += VISIBLE_ROWS - 1;
|
||||
if (rep & (KEY_LEFT | KEY_CPAD_LEFT)) selected -= VISIBLE_ROWS - 1;
|
||||
|
||||
// Touch: Reiter antippen, Liste ziehen, Zeile antippen (zweimal = Aktion)
|
||||
touchPosition tp;
|
||||
if (down & KEY_TOUCH) {
|
||||
hidTouchRead(&tp);
|
||||
if (tp.py < TAB_H) tab = tp.px * TAB_COUNT / BOT_W;
|
||||
else { touchStartY = tp.py; touchScroll = scroll; dragged = false; touchRow = scroll + (tp.py - LIST_Y) / ROW_H; }
|
||||
} else if (held & KEY_TOUCH && touchStartY >= 0) {
|
||||
hidTouchRead(&tp);
|
||||
int dy = touchStartY - tp.py;
|
||||
if (abs(dy) > 6) dragged = true;
|
||||
if (dragged) {
|
||||
scroll = touchScroll + dy / ROW_H;
|
||||
int maxScroll = viewCount - (VISIBLE_ROWS - 1);
|
||||
if (scroll > maxScroll) scroll = maxScroll;
|
||||
if (scroll < 0) scroll = 0;
|
||||
}
|
||||
} else if (touchStartY >= 0) {
|
||||
if (!dragged && touchRow >= 0 && touchRow < viewCount) {
|
||||
if (touchRow == selected) down |= KEY_A;
|
||||
else selected = touchRow;
|
||||
}
|
||||
touchStartY = -1;
|
||||
}
|
||||
|
||||
if (tab != oldTab) { rebuild_view(tab); selected = scroll = 0; }
|
||||
if (selected >= viewCount) selected = viewCount - 1;
|
||||
if (selected < 0) selected = 0;
|
||||
if (selected != oldSel || tab != oldTab) { msg[0] = 0; armed = -1; }
|
||||
if (selected < scroll) scroll = selected;
|
||||
if (selected >= scroll + VISIBLE_ROWS - 1) scroll = selected - (VISIBLE_ROWS - 2);
|
||||
|
||||
App *a = viewCount ? catalog_app(view[selected]) : NULL;
|
||||
if (a && down & KEY_A) {
|
||||
AppStatus s = app_status(a);
|
||||
if (s == APP_INSTALLED && strcmp(a->kind, "cia") == 0) {
|
||||
armed = -1;
|
||||
if (launch(a, msg, sizeof(msg))) quit = true;
|
||||
} else if (armed == 0) {
|
||||
armed = -1;
|
||||
char size[16];
|
||||
size_text(size, sizeof(size), a->size);
|
||||
snprintf(msg, sizeof(msg), "Lade %s (%s) ...", a->title, size);
|
||||
job_install(a);
|
||||
} else if (catalog_offline()) {
|
||||
snprintf(msg, sizeof(msg), "Keine Verbindung zum MBMNET-Server.\nInstallieren geht gerade nicht.");
|
||||
} else {
|
||||
armed = 0;
|
||||
snprintf(msg, sizeof(msg), "%s %s?\nZum Bestätigen nochmal A drücken (oder nochmal antippen).",
|
||||
a->title, s == APP_UPDATE ? "aktualisieren" : "installieren");
|
||||
}
|
||||
}
|
||||
if (a && down & KEY_Y && app_status(a) != APP_NEW) {
|
||||
if (armed == 1) {
|
||||
armed = -1;
|
||||
snprintf(msg, sizeof(msg), "Entferne %s ...", a->title);
|
||||
job_remove(a);
|
||||
} else {
|
||||
armed = 1;
|
||||
snprintf(msg, sizeof(msg), "%s entfernen?\nZum Bestätigen nochmal Y drücken.", a->title);
|
||||
}
|
||||
}
|
||||
if (down & KEY_B) { msg[0] = 0; armed = -1; }
|
||||
}
|
||||
|
||||
const App *cur = ready && viewCount ? catalog_app(view[selected]) : NULL;
|
||||
C2D_TextBufClear(textBuf);
|
||||
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
|
||||
C2D_TargetClear(top, COL_BG);
|
||||
C2D_SceneBegin(top);
|
||||
draw_top(cur, tab, msg, armed >= 0);
|
||||
C2D_TargetClear(bot, COL_BG);
|
||||
C2D_SceneBegin(bot);
|
||||
draw_bottom(tab, selected, scroll);
|
||||
C3D_FrameEnd(0);
|
||||
}
|
||||
|
||||
store_exit();
|
||||
for (int n = 0; n < 8; n++) if (sheets[n]) C2D_SpriteSheetFree(sheets[n]);
|
||||
free(view);
|
||||
C2D_TextBufDelete(textBuf);
|
||||
C2D_TextBufDelete(measureBuf);
|
||||
C2D_Fini();
|
||||
C3D_Fini();
|
||||
amExit();
|
||||
httpcExit();
|
||||
gfxExit();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// HTTP über den Systemdienst http:C. Alles läuft über den MBMNET-Server (einfaches HTTP),
|
||||
// der Katalog, Symbole und Downloads von Universal-DB und GitHub holt.
|
||||
#include <3ds.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "net.h"
|
||||
|
||||
#define CHUNK (128 * 1024)
|
||||
|
||||
static Result open_request(httpcContext *ctx, const char *url, u32 *status)
|
||||
{
|
||||
Result rc = httpcOpenContext(ctx, HTTPC_METHOD_GET, url, 1);
|
||||
if (R_FAILED(rc)) return rc;
|
||||
httpcAddRequestHeaderField(ctx, "User-Agent", "MBMNET-Shop/" APP_VERSION);
|
||||
httpcSetKeepAlive(ctx, HTTPC_KEEPALIVE_DISABLED);
|
||||
if (R_SUCCEEDED(rc = httpcBeginRequest(ctx)))
|
||||
rc = httpcGetResponseStatusCodeTimeout(ctx, status, 30ULL * 1000 * 1000 * 1000);
|
||||
if (R_FAILED(rc)) httpcCloseContext(ctx);
|
||||
return rc;
|
||||
}
|
||||
|
||||
u32 net_stream(const char *url, NetSink sink, void *ctx, Result *rcOut)
|
||||
{
|
||||
httpcContext http;
|
||||
u32 status = 0, total = 0, got;
|
||||
Result rc = open_request(&http, url, &status);
|
||||
if (rcOut) *rcOut = rc;
|
||||
if (R_FAILED(rc)) return 0;
|
||||
if (status != 200) { httpcCloseContext(&http); return status; }
|
||||
|
||||
httpcGetDownloadSizeState(&http, NULL, &total);
|
||||
u8 *buf = malloc(CHUNK);
|
||||
if (!buf) { httpcCloseContext(&http); if (rcOut) *rcOut = -2; return 0; }
|
||||
do {
|
||||
got = 0;
|
||||
rc = httpcDownloadData(&http, buf, CHUNK, &got);
|
||||
if (got && !sink(ctx, buf, got, total)) { rc = -3; break; } // -3 = Empfänger hat abgebrochen
|
||||
} while (rc == (Result)HTTPC_RESULTCODE_DOWNLOADPENDING);
|
||||
free(buf);
|
||||
httpcCloseContext(&http);
|
||||
if (rcOut) *rcOut = rc;
|
||||
return R_SUCCEEDED(rc) ? 200 : 0;
|
||||
}
|
||||
|
||||
typedef struct { u8 *data; u32 size, cap, max; } MemSink;
|
||||
|
||||
static bool to_memory(void *p, const u8 *data, u32 len, u32 total)
|
||||
{
|
||||
MemSink *m = p;
|
||||
if (m->size + len > m->max) return false;
|
||||
if (m->size + len > m->cap) {
|
||||
u32 cap = m->cap ? m->cap : (total && total <= m->max ? total : 64 * 1024);
|
||||
while (cap < m->size + len) cap *= 2;
|
||||
u8 *bigger = realloc(m->data, cap);
|
||||
if (!bigger) return false;
|
||||
m->data = bigger;
|
||||
m->cap = cap;
|
||||
}
|
||||
memcpy(m->data + m->size, data, len);
|
||||
m->size += len;
|
||||
return true;
|
||||
}
|
||||
|
||||
u32 net_get(const char *url, u8 **out, u32 *size, u32 max, Result *rc)
|
||||
{
|
||||
MemSink m = { NULL, 0, 0, max };
|
||||
u32 status = net_stream(url, to_memory, &m, rc);
|
||||
if (status != 200) { free(m.data); m.data = NULL; m.size = 0; }
|
||||
*out = m.data;
|
||||
*size = m.size;
|
||||
return status;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <3ds/types.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// Empfänger für net_stream: bekommt die Daten stückweise. total = Gesamtgröße (0, wenn unbekannt).
|
||||
// false = abbrechen.
|
||||
typedef bool (*NetSink)(void *ctx, const u8 *data, u32 len, u32 total);
|
||||
|
||||
// Lädt eine URL komplett in den Speicher (höchstens max Bytes). Gibt den HTTP-Status zurück,
|
||||
// 0 bei Netzwerkfehler (Code in *rc). *out mit free() freigeben.
|
||||
u32 net_get(const char *url, u8 **out, u32 *size, u32 max, Result *rc);
|
||||
|
||||
// Lädt eine URL und reicht die Daten an sink weiter. Gibt den HTTP-Status zurück (0 bei Fehler).
|
||||
u32 net_stream(const char *url, NetSink sink, void *ctx, Result *rc);
|
||||
@@ -0,0 +1,476 @@
|
||||
// Katalog, Symbole und installierte Apps des MBMNET Shops.
|
||||
//
|
||||
// Der Katalog kommt vom MBMNET-Server (/shop/list, Quelle Universal-DB) und wird auf der SD
|
||||
// zwischengespeichert, damit der Shop auch ohne Verbindung die installierten Apps zeigt.
|
||||
// Was der Shop installiert hat, steht in SHOP_DIR/installed.txt (nur dann kann er es wieder entfernen).
|
||||
#include <3ds.h>
|
||||
#include <dirent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "net.h"
|
||||
#include "store.h"
|
||||
|
||||
#define LIST_FILE SHOP_DIR "/list.txt"
|
||||
#define INSTALLED_FILE SHOP_DIR "/installed.txt"
|
||||
#define MAX_SHEETS 8
|
||||
|
||||
// --- Katalog ---------------------------------------------------------------------
|
||||
|
||||
static volatile CatalogState catState = CAT_IDLE;
|
||||
static char catMsg[128];
|
||||
static bool offline;
|
||||
static App *apps;
|
||||
static int appCount;
|
||||
static int sheetCount;
|
||||
static char sheetPath[MAX_SHEETS][64];
|
||||
static char *extraInstalled; // Einträge in installed.txt zu Apps, die es im Katalog nicht (mehr) gibt
|
||||
static Thread catThread, jobThread;
|
||||
|
||||
CatalogState catalog_state(void) { return catState; }
|
||||
const char *catalog_message(void) { return catMsg; }
|
||||
bool catalog_offline(void) { return offline; }
|
||||
int catalog_count(void) { return catState == CAT_READY ? appCount : 0; }
|
||||
App *catalog_app(int i) { return &apps[i]; }
|
||||
int catalog_sheet_count(void) { return sheetCount; }
|
||||
const char *catalog_sheet_path(int n) { return n >= 0 && n < sheetCount ? sheetPath[n] : ""; }
|
||||
|
||||
AppStatus app_status(const App *a)
|
||||
{
|
||||
if (!a->installed) return APP_NEW;
|
||||
if (a->version[0] && strcmp(a->version, a->installedVersion) != 0) return APP_UPDATE;
|
||||
return APP_INSTALLED;
|
||||
}
|
||||
|
||||
static bool file_exists(const char *path)
|
||||
{
|
||||
struct stat st;
|
||||
return stat(path, &st) == 0;
|
||||
}
|
||||
|
||||
static bool write_file(const char *path, const void *data, u32 size)
|
||||
{
|
||||
char tmp[96];
|
||||
snprintf(tmp, sizeof(tmp), "%s.neu", path);
|
||||
FILE *f = fopen(tmp, "wb");
|
||||
bool ok = f && fwrite(data, 1, size, f) == size;
|
||||
if (f) fclose(f);
|
||||
if (ok) { remove(path); ok = rename(tmp, path) == 0; }
|
||||
if (!ok) remove(tmp);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static char *read_file(const char *path, u32 *size)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return NULL;
|
||||
fseek(f, 0, SEEK_END);
|
||||
long n = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
char *buf = n >= 0 ? malloc(n + 1) : NULL;
|
||||
if (buf && fread(buf, 1, n, f) != (size_t)n) { free(buf); buf = NULL; }
|
||||
fclose(f);
|
||||
if (buf) { buf[n] = 0; if (size) *size = n; }
|
||||
return buf;
|
||||
}
|
||||
|
||||
// Zerlegt eine Zeile in Felder (Tab-getrennt). Gibt die Anzahl zurück.
|
||||
static int split(char *line, char **fields, int max)
|
||||
{
|
||||
int n = 0;
|
||||
while (n < max) {
|
||||
fields[n++] = line;
|
||||
char *tab = strchr(line, '\t');
|
||||
if (!tab) break;
|
||||
*tab = 0;
|
||||
line = tab + 1;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
#define COPY(dst, src) snprintf(dst, sizeof(dst), "%s", src)
|
||||
|
||||
static bool parse_list(char *text, int *revision)
|
||||
{
|
||||
char *save = NULL, *line = strtok_r(text, "\n", &save);
|
||||
char *f[16];
|
||||
if (!line || split(line, f, 16) < 5 || strcmp(f[0], "MBMNETSHOP") != 0) return false;
|
||||
*revision = atoi(f[2]);
|
||||
int count = atoi(f[3]);
|
||||
sheetCount = atoi(f[4]);
|
||||
if (sheetCount > MAX_SHEETS) sheetCount = MAX_SHEETS;
|
||||
if (count <= 0 || count > 5000) return false;
|
||||
apps = calloc(count, sizeof(App));
|
||||
if (!apps) return false;
|
||||
|
||||
appCount = 0;
|
||||
while ((line = strtok_r(NULL, "\n", &save)) && appCount < count) {
|
||||
if (split(line, f, 16) < 13) continue;
|
||||
App *a = &apps[appCount++];
|
||||
COPY(a->slug, f[0]);
|
||||
COPY(a->title, f[1]);
|
||||
COPY(a->author, f[2]);
|
||||
COPY(a->version, f[3]);
|
||||
COPY(a->category, f[4]);
|
||||
a->sheet = atoi(f[5]);
|
||||
a->icon = atoi(f[6]);
|
||||
COPY(a->kind, f[7]);
|
||||
COPY(a->file, f[8]);
|
||||
a->size = strtoul(f[9], NULL, 10);
|
||||
a->stars = strtoul(f[10], NULL, 10);
|
||||
COPY(a->updated, f[11]);
|
||||
a->description = strdup(f[12]);
|
||||
}
|
||||
return appCount > 0;
|
||||
}
|
||||
|
||||
static App *find_app(const char *slug)
|
||||
{
|
||||
for (int i = 0; i < appCount; i++)
|
||||
if (strcmp(apps[i].slug, slug) == 0) return &apps[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// installed.txt: slug \t Version \t Art \t Titel-ID (hex) \t Dateiname
|
||||
static void load_installed(void)
|
||||
{
|
||||
char *text = read_file(INSTALLED_FILE, NULL);
|
||||
if (!text) return;
|
||||
size_t extraLen = 0;
|
||||
char *save = NULL, *line, *f[8];
|
||||
for (line = strtok_r(text, "\n", &save); line; line = strtok_r(NULL, "\n", &save)) {
|
||||
char copy[256];
|
||||
COPY(copy, line);
|
||||
if (split(line, f, 8) < 5) continue;
|
||||
App *a = find_app(f[0]);
|
||||
if (!a) {
|
||||
// unbekannte App merken, damit sie beim Speichern nicht verloren geht
|
||||
size_t len = strlen(copy);
|
||||
char *bigger = realloc(extraInstalled, extraLen + len + 2);
|
||||
if (!bigger) continue;
|
||||
extraInstalled = bigger;
|
||||
memcpy(extraInstalled + extraLen, copy, len);
|
||||
extraLen += len;
|
||||
extraInstalled[extraLen++] = '\n';
|
||||
extraInstalled[extraLen] = 0;
|
||||
continue;
|
||||
}
|
||||
a->installed = true;
|
||||
COPY(a->installedVersion, f[1]);
|
||||
a->titleId = strtoull(f[3], NULL, 16);
|
||||
if (f[4][0]) COPY(a->file, f[4]);
|
||||
}
|
||||
free(text);
|
||||
}
|
||||
|
||||
static void save_installed(void)
|
||||
{
|
||||
FILE *f = fopen(INSTALLED_FILE, "w");
|
||||
if (!f) return;
|
||||
for (int i = 0; i < appCount; i++) {
|
||||
App *a = &apps[i];
|
||||
if (a->installed)
|
||||
fprintf(f, "%s\t%s\t%s\t%016llX\t%s\n", a->slug, a->installedVersion, a->kind, a->titleId, a->file);
|
||||
}
|
||||
if (extraInstalled) fputs(extraInstalled, f);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
// Alte Symbol-Blätter (andere Revision) löschen
|
||||
static void remove_old_sheets(int revision)
|
||||
{
|
||||
char keep[32];
|
||||
snprintf(keep, sizeof(keep), "icons-%d-", revision);
|
||||
DIR *d = opendir(SHOP_DIR);
|
||||
if (!d) return;
|
||||
struct dirent *e;
|
||||
char path[320];
|
||||
while ((e = readdir(d))) {
|
||||
if (strncmp(e->d_name, "icons-", 6) == 0 && strncmp(e->d_name, keep, strlen(keep)) != 0) {
|
||||
snprintf(path, sizeof(path), SHOP_DIR "/%s", e->d_name);
|
||||
remove(path);
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
static void catalog_worker(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
char url[160];
|
||||
u8 *data = NULL;
|
||||
u32 size = 0;
|
||||
Result rc;
|
||||
mkdir("sdmc:/3ds", 0777);
|
||||
mkdir(SHOP_DIR, 0777);
|
||||
|
||||
snprintf(catMsg, sizeof(catMsg), "Lade den Katalog ...");
|
||||
snprintf(url, sizeof(url), "http://%s/shop/list", SHOP_SERVER);
|
||||
u32 status = net_get(url, &data, &size, 4 * 1024 * 1024, &rc);
|
||||
char *text = NULL;
|
||||
if (status == 200 && size > 10) {
|
||||
write_file(LIST_FILE, data, size);
|
||||
text = realloc(data, size + 1);
|
||||
if (text) text[size] = 0; else free(data);
|
||||
} else {
|
||||
free(data);
|
||||
text = read_file(LIST_FILE, &size);
|
||||
offline = true;
|
||||
if (!text) {
|
||||
if (status) snprintf(catMsg, sizeof(catMsg), "Der Shop-Server antwortet nicht (HTTP %lu).", (unsigned long)status);
|
||||
else snprintf(catMsg, sizeof(catMsg), "Keine Verbindung zum MBMNET-Server (%08lX).\nIst das WLAN an?", (unsigned long)rc);
|
||||
catState = CAT_ERROR;
|
||||
return;
|
||||
}
|
||||
}
|
||||
int revision = 0;
|
||||
bool ok = parse_list(text, &revision);
|
||||
free(text);
|
||||
if (!ok) {
|
||||
snprintf(catMsg, sizeof(catMsg), "Der Katalog ist beschädigt.");
|
||||
catState = CAT_ERROR;
|
||||
return;
|
||||
}
|
||||
load_installed();
|
||||
|
||||
// Symbol-Blätter (einmal pro Katalog-Revision laden)
|
||||
for (int n = 0; n < sheetCount; n++) {
|
||||
snprintf(sheetPath[n], sizeof(sheetPath[n]), SHOP_DIR "/icons-%d-%d.t3x", revision, n);
|
||||
if (file_exists(sheetPath[n])) continue;
|
||||
if (offline) { sheetPath[n][0] = 0; continue; }
|
||||
snprintf(catMsg, sizeof(catMsg), "Lade die Symbole (%d von %d) ...", n + 1, sheetCount);
|
||||
snprintf(url, sizeof(url), "http://%s/shop/sheet/%d", SHOP_SERVER, n);
|
||||
if (net_get(url, &data, &size, 16 * 1024 * 1024, &rc) != 200 || !write_file(sheetPath[n], data, size))
|
||||
sheetPath[n][0] = 0;
|
||||
free(data);
|
||||
}
|
||||
if (!offline) remove_old_sheets(revision);
|
||||
catMsg[0] = 0;
|
||||
catState = CAT_READY;
|
||||
}
|
||||
|
||||
static void start_thread(Thread *t, ThreadFunc fn)
|
||||
{
|
||||
if (*t) { threadJoin(*t, U64_MAX); threadFree(*t); *t = NULL; }
|
||||
s32 prio = 0x30;
|
||||
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
|
||||
*t = threadCreate(fn, NULL, 32 * 1024, prio + 1, -2, false);
|
||||
}
|
||||
|
||||
void catalog_start(void)
|
||||
{
|
||||
if (catState == CAT_LOADING || catState == CAT_READY) return;
|
||||
catState = CAT_LOADING;
|
||||
start_thread(&catThread, catalog_worker);
|
||||
if (!catThread) { snprintf(catMsg, sizeof(catMsg), "Konnte nicht starten."); catState = CAT_ERROR; }
|
||||
}
|
||||
|
||||
// --- Installieren / Entfernen ------------------------------------------------------
|
||||
|
||||
static volatile JobState jobState = JOB_IDLE;
|
||||
static volatile u32 jobDone, jobTotal;
|
||||
static char jobMsg[160];
|
||||
static App *jobApp;
|
||||
static bool jobRemove;
|
||||
|
||||
JobState job_state(void) { return jobState; }
|
||||
float job_progress(void) { return jobTotal ? (float)jobDone / jobTotal : -1.0f; }
|
||||
const char *job_message(void) { return jobMsg; }
|
||||
void job_acknowledge(void) { if (jobState != JOB_RUNNING) jobState = JOB_IDLE; }
|
||||
|
||||
static inline u32 le32(const u8 *p) { return p[0] | p[1] << 8 | p[2] << 16 | (u32)p[3] << 24; }
|
||||
static inline u32 be32(const u8 *p) { return (u32)p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]; }
|
||||
static inline u32 align64(u32 x) { return (x + 63) & ~63u; }
|
||||
|
||||
// Titel-ID aus dem Kopf einer CIA (Kopf, Zertifikate, Ticket, dann die TMD mit der Titel-ID)
|
||||
static u64 cia_title_id(const u8 *d, u32 n)
|
||||
{
|
||||
if (n < 0x20) return 0;
|
||||
u32 off = align64(le32(d)) + align64(le32(d + 8)) + align64(le32(d + 12));
|
||||
if (off + 4 > n) return 0;
|
||||
u32 sigType = be32(d + off), sig;
|
||||
switch (sigType) {
|
||||
case 0x10003: sig = 0x200 + 0x3C; break;
|
||||
case 0x10004: sig = 0x100 + 0x3C; break;
|
||||
case 0x10005: sig = 0x3C + 0x40; break;
|
||||
default: return 0;
|
||||
}
|
||||
u32 hdr = off + 4 + sig;
|
||||
if (hdr + 0x54 > n) return 0;
|
||||
return (u64)be32(d + hdr + 0x4C) << 32 | be32(d + hdr + 0x50);
|
||||
}
|
||||
|
||||
#define HEAD_SIZE (64 * 1024)
|
||||
|
||||
typedef struct {
|
||||
bool cia, started;
|
||||
Handle cia_h;
|
||||
FILE *file;
|
||||
u32 offset;
|
||||
u8 *head; // Anfang der Datei (für Prüfung und Titel-ID)
|
||||
u32 headLen;
|
||||
Result rc;
|
||||
const char *error;
|
||||
} Install;
|
||||
|
||||
static bool write_part(Install *in, const u8 *data, u32 len)
|
||||
{
|
||||
if (!len) return true;
|
||||
if (in->cia) {
|
||||
u32 w = 0;
|
||||
in->rc = FSFILE_Write(in->cia_h, &w, in->offset, data, len, 0);
|
||||
if (R_FAILED(in->rc) || w != len) { in->error = "Schreiben fehlgeschlagen"; return false; }
|
||||
} else if (fwrite(data, 1, len, in->file) != len) {
|
||||
in->error = "SD-Karte voll?";
|
||||
return false;
|
||||
}
|
||||
in->offset += len;
|
||||
jobDone = in->offset;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool install_sink(void *p, const u8 *data, u32 len, u32 total)
|
||||
{
|
||||
Install *in = p;
|
||||
jobTotal = total;
|
||||
// Den Anfang der Datei mitschreiben (Prüfung, Titel-ID)
|
||||
u32 copied = 0;
|
||||
if (in->headLen < HEAD_SIZE && len) {
|
||||
copied = len < HEAD_SIZE - in->headLen ? len : HEAD_SIZE - in->headLen;
|
||||
memcpy(in->head + in->headLen, data, copied);
|
||||
in->headLen += copied;
|
||||
}
|
||||
if (!in->started) {
|
||||
if (in->headLen < 8) return true;
|
||||
if (in->cia ? le32(in->head) != 0x2020 : memcmp(in->head, "3DSX", 4) != 0) {
|
||||
in->error = "Die Datei ist keine gültige App.";
|
||||
return false;
|
||||
}
|
||||
if (in->cia) {
|
||||
in->rc = AM_StartCiaInstall(MEDIATYPE_SD, &in->cia_h);
|
||||
if (R_FAILED(in->rc)) { in->error = "Installation nicht möglich"; return false; }
|
||||
} else if (!(in->file = fopen(SHOP_DIR "/download.tmp", "wb"))) {
|
||||
in->error = "Kann nicht auf die SD-Karte schreiben.";
|
||||
return false;
|
||||
}
|
||||
in->started = true;
|
||||
// Alles bisher Gesammelte schreiben, vom aktuellen Stück dann nur noch den Rest
|
||||
if (!write_part(in, in->head, in->headLen)) return false;
|
||||
data += copied;
|
||||
len -= copied;
|
||||
}
|
||||
return write_part(in, data, len);
|
||||
}
|
||||
|
||||
static void install_worker(App *a)
|
||||
{
|
||||
char url[192];
|
||||
Install in = { .cia = strcmp(a->kind, "cia") == 0 };
|
||||
in.head = malloc(HEAD_SIZE);
|
||||
if (!in.head) { snprintf(jobMsg, sizeof(jobMsg), "Zu wenig Speicher."); jobState = JOB_FAILED; return; }
|
||||
snprintf(url, sizeof(url), "http://%s/shop/get/%s", SHOP_SERVER, a->slug);
|
||||
Result rc = 0;
|
||||
u32 status = net_stream(url, install_sink, &in, &rc);
|
||||
if (status == 200 && !in.started && !in.error) in.error = "Die Datei ist keine gültige App.";
|
||||
bool ok = status == 200 && in.started && !in.error;
|
||||
|
||||
if (in.cia) {
|
||||
if (in.started) {
|
||||
if (ok) {
|
||||
in.rc = AM_FinishCiaInstall(in.cia_h);
|
||||
if (R_FAILED(in.rc)) { ok = false; in.error = "Installation abgelehnt"; }
|
||||
} else {
|
||||
AM_CancelCIAInstall(in.cia_h);
|
||||
}
|
||||
}
|
||||
} else if (in.file) {
|
||||
fclose(in.file);
|
||||
char dest[128];
|
||||
snprintf(dest, sizeof(dest), "sdmc:/3ds/%s", a->file);
|
||||
if (ok) { remove(dest); ok = rename(SHOP_DIR "/download.tmp", dest) == 0; if (!ok) in.error = "Umbenennen fehlgeschlagen"; }
|
||||
else remove(SHOP_DIR "/download.tmp");
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
bool update = a->installed;
|
||||
a->installed = true;
|
||||
COPY(a->installedVersion, a->version);
|
||||
if (in.cia) a->titleId = cia_title_id(in.head, in.headLen);
|
||||
save_installed();
|
||||
snprintf(jobMsg, sizeof(jobMsg), in.cia ? "%s %s!\nDu findest die App jetzt im HOME-Menü."
|
||||
: "%s %s!\nStarte sie über den Homebrew Launcher (/3ds/%s).",
|
||||
a->title, update ? "aktualisiert" : "installiert", a->file);
|
||||
jobState = JOB_DONE;
|
||||
} else {
|
||||
if (in.error && in.rc) snprintf(jobMsg, sizeof(jobMsg), "%s nicht installiert:\n%s (%08lX).", a->title, in.error, (unsigned long)in.rc);
|
||||
else if (in.error) snprintf(jobMsg, sizeof(jobMsg), "%s nicht installiert:\n%s", a->title, in.error);
|
||||
else if (status && status != 200) snprintf(jobMsg, sizeof(jobMsg), "%s nicht installiert:\nDer Server meldet HTTP %lu.", a->title, (unsigned long)status);
|
||||
else snprintf(jobMsg, sizeof(jobMsg), "%s nicht installiert:\nDownload abgebrochen (%08lX).", a->title, (unsigned long)rc);
|
||||
jobState = JOB_FAILED;
|
||||
}
|
||||
free(in.head);
|
||||
}
|
||||
|
||||
static void remove_worker(App *a)
|
||||
{
|
||||
bool ok = true;
|
||||
Result rc = 0;
|
||||
if (strcmp(a->kind, "cia") == 0) {
|
||||
if (a->titleId) {
|
||||
rc = AM_DeleteTitle(MEDIATYPE_SD, a->titleId);
|
||||
// schon von Hand gelöscht? Dann ist es auch gut.
|
||||
AM_TitleEntry info;
|
||||
u64 id = a->titleId;
|
||||
if (R_FAILED(rc) && R_FAILED(AM_GetTitleInfo(MEDIATYPE_SD, 1, &id, &info))) rc = 0;
|
||||
ok = R_SUCCEEDED(rc);
|
||||
}
|
||||
} else {
|
||||
char path[128];
|
||||
snprintf(path, sizeof(path), "sdmc:/3ds/%s", a->file);
|
||||
remove(path);
|
||||
}
|
||||
if (ok) {
|
||||
a->installed = false;
|
||||
a->installedVersion[0] = 0;
|
||||
a->titleId = 0;
|
||||
save_installed();
|
||||
snprintf(jobMsg, sizeof(jobMsg), "%s entfernt.", a->title);
|
||||
jobState = JOB_DONE;
|
||||
} else {
|
||||
snprintf(jobMsg, sizeof(jobMsg), "%s ließ sich nicht entfernen (%08lX).", a->title, (unsigned long)rc);
|
||||
jobState = JOB_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
static void job_worker(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
if (jobRemove) remove_worker(jobApp);
|
||||
else install_worker(jobApp);
|
||||
}
|
||||
|
||||
static void job_start(App *a, bool remove)
|
||||
{
|
||||
if (jobState == JOB_RUNNING) return;
|
||||
jobApp = a;
|
||||
jobRemove = remove;
|
||||
jobDone = jobTotal = 0;
|
||||
jobMsg[0] = 0;
|
||||
jobState = JOB_RUNNING;
|
||||
start_thread(&jobThread, job_worker);
|
||||
if (!jobThread) { snprintf(jobMsg, sizeof(jobMsg), "Konnte nicht starten."); jobState = JOB_FAILED; }
|
||||
}
|
||||
|
||||
void job_install(App *a) { job_start(a, false); }
|
||||
void job_remove(App *a) { job_start(a, true); }
|
||||
|
||||
void store_exit(void)
|
||||
{
|
||||
if (catThread) { threadJoin(catThread, U64_MAX); threadFree(catThread); }
|
||||
if (jobThread) { threadJoin(jobThread, U64_MAX); threadFree(jobThread); }
|
||||
for (int i = 0; i < appCount; i++) free(apps[i].description);
|
||||
free(apps);
|
||||
free(extraInstalled);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
#include <3ds/types.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define SHOP_SERVER "mbmnetapi.mbm.wtf:81"
|
||||
#define SHOP_DIR "sdmc:/3ds/mbmnet-shop"
|
||||
|
||||
typedef enum { APP_NEW, APP_INSTALLED, APP_UPDATE } AppStatus;
|
||||
|
||||
typedef struct {
|
||||
char slug[72], title[64], author[44], version[28], category[16], kind[6], file[72], updated[12];
|
||||
int sheet, icon; // Symbol: Blatt und Nummer darin (-1 = keins)
|
||||
u32 size, stars;
|
||||
char *description;
|
||||
// Installiert (aus SHOP_DIR/installed.txt)
|
||||
bool installed;
|
||||
char installedVersion[28];
|
||||
u64 titleId; // bei CIAs, 0 sonst
|
||||
} App;
|
||||
|
||||
// --- Katalog (läuft im Hintergrund) ---
|
||||
typedef enum { CAT_IDLE, CAT_LOADING, CAT_READY, CAT_ERROR } CatalogState;
|
||||
void catalog_start(void);
|
||||
CatalogState catalog_state(void);
|
||||
const char *catalog_message(void); // Fortschritt bzw. Fehler
|
||||
bool catalog_offline(void); // aus dem Zwischenspeicher auf der SD (Server nicht erreichbar)
|
||||
int catalog_count(void);
|
||||
App *catalog_app(int i);
|
||||
int catalog_sheet_count(void);
|
||||
const char *catalog_sheet_path(int n); // Symbol-Blatt auf der SD (leer, wenn nicht da)
|
||||
AppStatus app_status(const App *a);
|
||||
|
||||
// --- Installieren / Entfernen (läuft im Hintergrund) ---
|
||||
typedef enum { JOB_IDLE, JOB_RUNNING, JOB_DONE, JOB_FAILED } JobState;
|
||||
void job_install(App *a);
|
||||
void job_remove(App *a);
|
||||
JobState job_state(void);
|
||||
float job_progress(void); // 0..1, < 0 wenn unbekannt
|
||||
const char *job_message(void);
|
||||
void job_acknowledge(void); // Ergebnis gesehen, zurück auf JOB_IDLE
|
||||
|
||||
void store_exit(void);
|
||||
Reference in New Issue
Block a user