From 3aea6a98ef74dc3ea2d53c74a49992b4adc85f38 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 21 Jan 2023 15:24:18 +0300 Subject: [PATCH] Fix loading custom interface frms --- src/art.cc | 34 ++++++++++++++++++++++++++++++++++ src/art.h | 1 + src/interface.cc | 36 +++++++++++------------------------- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/art.cc b/src/art.cc index bf17d69..1157f60 100644 --- a/src/art.cc +++ b/src/art.cc @@ -1079,6 +1079,40 @@ static int artReadHeader(Art* art, File* stream) return 0; } +// NOTE: Original function was slightly different, but never used. Basically +// it's a memory allocating variant of `artRead` (which reads data into given +// buffer). This function is useful to load custom `frm` files since `Art` now +// needs more memory then it's on-disk size (due to memory padding). +// +// 0x419EC0 +Art* artLoad(const char* path) +{ + File* stream = fileOpen(path, "rb"); + if (stream == nullptr) { + return nullptr; + } + + Art header; + if (artReadHeader(&header, stream) != 0) { + fileClose(stream); + return nullptr; + } + + fileClose(stream); + + unsigned char* data = reinterpret_cast(internal_malloc(artGetDataSize(&header))); + if (data == nullptr) { + return nullptr; + } + + if (artRead(path, data) != 0) { + internal_free(data); + return nullptr; + } + + return reinterpret_cast(data); +} + // 0x419FC0 int artRead(const char* path, unsigned char* data) { diff --git a/src/art.h b/src/art.h index 0073cdc..f7c31b4 100644 --- a/src/art.h +++ b/src/art.h @@ -147,6 +147,7 @@ int _art_alias_num(int a1); int artCritterFidShouldRun(int a1); int artAliasFid(int fid); int buildFid(int objectType, int frmId, int animType, int a4, int rotation); +Art* artLoad(const char* path); int artRead(const char* path, unsigned char* data); int artWrite(const char* path, unsigned char* data); diff --git a/src/interface.cc b/src/interface.cc index 7d492c3..3204faa 100644 --- a/src/interface.cc +++ b/src/interface.cc @@ -2483,30 +2483,26 @@ static void customInterfaceBarInit() { gInterfaceBarContentOffset = gInterfaceBarWidth - 640; - char path[COMPAT_MAX_PATH]; - snprintf(path, sizeof(path), "art\\intrface\\HR_IFACE_%d.FRM", gInterfaceBarWidth); + if (gInterfaceBarContentOffset > 0 && screenGetWidth() > 640) { + char path[COMPAT_MAX_PATH]; + snprintf(path, sizeof(path), "art\\intrface\\HR_IFACE_%d.FRM", gInterfaceBarWidth); - int size; - if (dbGetFileSize(path, &size) != 0 || gInterfaceBarContentOffset <= 0 || screenGetWidth() <= 640) { + gCustomInterfaceBarBackground = artLoad(path); + } + + if (gCustomInterfaceBarBackground != nullptr) { + gInterfaceBarIsCustom = true; + } else { gInterfaceBarContentOffset = 0; gInterfaceBarWidth = 640; gInterfaceBarIsCustom = false; - } else { - gInterfaceBarIsCustom = true; - - gCustomInterfaceBarBackground = (Art*)(malloc(size)); - if (artRead(path, (unsigned char*)gCustomInterfaceBarBackground) != 0) { - gInterfaceBarIsCustom = false; - free(gCustomInterfaceBarBackground); - gCustomInterfaceBarBackground = nullptr; - } } } static void customInterfaceBarExit() { if (gCustomInterfaceBarBackground != nullptr) { - free(gCustomInterfaceBarBackground); + internal_free(gCustomInterfaceBarBackground); gCustomInterfaceBarBackground = nullptr; } } @@ -2585,21 +2581,11 @@ static void sidePanelsShow() static void sidePanelsDraw(const char* path, int win, bool isLeading) { - int size; - if (dbGetFileSize(path, &size) != 0) { - return; - } - - Art* image = reinterpret_cast(internal_malloc(size)); + Art* image = artLoad(path); if (image == nullptr) { return; } - if (artRead(path, reinterpret_cast(image)) != 0) { - internal_free(image); - return; - } - unsigned char* imageData = artGetFrameData(image, 0, 0); int imageWidth = artGetWidth(image, 0, 0);