From e1992eaf41365cc5ac8eefdaac2b466d4cd0e137 Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Tue, 13 Jan 2026 21:32:14 -0600 Subject: [PATCH] Added png factories among fixing asset array for hd textures --- src/port/Engine.cpp | 19 ++++-- src/port/game/GeoLayoutParser.cpp | 6 +- src/port/importer/AssetArrayFactory.cpp | 18 ++++- src/port/importer/RawTextureFactory.cpp | 91 +++++++++++++++++++++++++ src/port/importer/RawTextureFactory.h | 18 +++++ src/port/importer/types/AssetArray.h | 1 + 6 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 src/port/importer/RawTextureFactory.cpp create mode 100644 src/port/importer/RawTextureFactory.h diff --git a/src/port/Engine.cpp b/src/port/Engine.cpp index 0a707f9e..33e91a20 100644 --- a/src/port/Engine.cpp +++ b/src/port/Engine.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #ifdef USE_NETWORKING #include @@ -40,10 +41,10 @@ #include #include "importer/AssetArrayFactory.h" +#include "importer/RawTextureFactory.h" #include "port/importer/GenericArrayFactory.h" #include "controller/controldeck/ControlDeck.h" -#include -#include "port/mods/utils/gfxprint.h" +#include "port/mods/utils/GfxPrint.h" #ifdef __SWITCH__ #include @@ -152,6 +153,12 @@ GameEngine::GameEngine() : dictionary(nullptr) { auto loader = context->GetResourceManager()->GetResourceLoader(); auto blobFactory = std::make_shared(); + + loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, + "Texture", static_cast(Fast::ResourceType::Texture), 0); + loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, + "Texture", static_cast(Fast::ResourceType::Texture), 1); + loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, "Animation", static_cast(SM64::ResourceType::Anim), 0); loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, "AudioBank", @@ -164,10 +171,10 @@ GameEngine::GameEngine() : dictionary(nullptr) { static_cast(SM64::ResourceType::SDialog), 0); loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, "Dictionary", static_cast(SM64::ResourceType::Dictionary), 0); - loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, - "Texture", static_cast(Fast::ResourceType::Texture), 0); - loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, - "Texture", static_cast(Fast::ResourceType::Texture), 1); +// loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, +// "Texture", static_cast(Fast::ResourceType::Texture), 0); +// loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, +// "Texture", static_cast(Fast::ResourceType::Texture), 1); loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, "Vertex", static_cast(Fast::ResourceType::Vertex), 0); loader->RegisterResourceFactory(std::make_shared(), diff --git a/src/port/game/GeoLayoutParser.cpp b/src/port/game/GeoLayoutParser.cpp index b32c22bf..624e4f55 100644 --- a/src/port/game/GeoLayoutParser.cpp +++ b/src/port/game/GeoLayoutParser.cpp @@ -243,7 +243,7 @@ void process_cmd_branch() { if (param == 1) { gGeoLayoutStack[gGeoLayoutStackIndex++] = reinterpret_cast(GeoLayoutParser::mReader); - SPDLOG_INFO("Branching and returning to {}:{}", name, gGeoLayoutStackIndex); + SPDLOG_TRACE("Branching and returning to {}:{}", name, gGeoLayoutStackIndex); } else { delete GeoLayoutParser::mReader; } @@ -254,7 +254,7 @@ void process_cmd_branch() { void process_cmd_return() { delete GeoLayoutParser::mReader; GeoLayoutParser::mReader = reinterpret_cast(gGeoLayoutStack[--gGeoLayoutStackIndex]); - SPDLOG_INFO("Returning to {}", gGeoLayoutStackIndex); + SPDLOG_TRACE("Returning to {}", gGeoLayoutStackIndex); } void process_cmd_open_node() { @@ -456,7 +456,7 @@ void process_cmd_node_translation() { if (params & 0x80) { displayList = ResourceGetDataByCrc(ReadSafeCrc()); drawingLayer = params & 0x0F; - SPDLOG_INFO("Current Offset {}", GeoLayoutParser::mReader->GetBaseAddress()); + SPDLOG_TRACE("Current Offset {}", GeoLayoutParser::mReader->GetBaseAddress()); } GraphNodeTranslation* graphNode = diff --git a/src/port/importer/AssetArrayFactory.cpp b/src/port/importer/AssetArrayFactory.cpp index 5af3a610..52b3571a 100644 --- a/src/port/importer/AssetArrayFactory.cpp +++ b/src/port/importer/AssetArrayFactory.cpp @@ -3,6 +3,7 @@ #include "./types/AssetArray.h" #include "spdlog/spdlog.h" #include "ResourceUtil.h" +#include namespace SM64 { std::shared_ptr @@ -12,14 +13,25 @@ ResourceFactoryBinaryAssetArrayV0::ReadResource(std::shared_ptr file return nullptr; } - auto asset = std::make_shared(initData); + auto array = std::make_shared(initData); auto reader = std::get>(file->Reader); auto count = reader->ReadUInt32(); for (size_t i = 0; i < count; i++) { - asset->mPtrs.push_back(reinterpret_cast(ResourceGetDataByCrc(reader->ReadUInt64()))); + auto asset = Ship::Context::GetInstance()->GetResourceManager()->LoadResource(reader->ReadUInt64()); + if(asset != nullptr) { + auto data = asset->GetInitData(); + if(data->Type == (uint32_t) Fast::ResourceType::Texture) { + array->mPaths.push_back("__OTR__"+data->Path); + array->mPtrs.push_back(reinterpret_cast(array->mPaths.back().c_str())); + } else { + array->mPtrs.push_back(reinterpret_cast(asset->GetRawPointer())); + } + } else { + array->mPtrs.push_back(0); + } } - return asset; + return array; } } // namespace SM64 diff --git a/src/port/importer/RawTextureFactory.cpp b/src/port/importer/RawTextureFactory.cpp new file mode 100644 index 00000000..0b848b09 --- /dev/null +++ b/src/port/importer/RawTextureFactory.cpp @@ -0,0 +1,91 @@ +#include "RawTextureFactory.h" +#include "fast/resource/type/Texture.h" +#include "spdlog/spdlog.h" +#include +#include +#include "ship/resource/archive/ArchiveManager.h" +#include "ship/resource/ResourceManager.h" + +namespace MK64 { + +std::shared_ptr loadPngTexture(std::shared_ptr filePng, std::shared_ptr initData) { + auto texture = std::make_shared(initData); + + int height, width = 0; + texture->ImageData = stbi_load_from_memory((const stbi_uc*)filePng->Buffer.get()->data(), + filePng->Buffer.get()->size(), &width, &height, nullptr, 4); + texture->Width = width; + texture->Height = height; + texture->Type = Fast::TextureType::RGBA32bpp; + texture->ImageDataSize = texture->Width * texture->Height * 4; + texture->Flags = TEX_FLAG_LOAD_AS_IMG; + texture->VPixelScale = 1.0f; + texture->HByteScale = 1.0f; + return texture; +} + +std::vector extension = {".png", ".PNG", ".jpg", ".JPG", ".jpeg", ".JPEG", ".bmp", ".BMP"}; + +std::shared_ptr +ResourceFactoryBinaryTextureV0::ReadResource(std::shared_ptr file, + std::shared_ptr initData) { + if (!FileHasValidFormatAndReader(file, initData)) { + return nullptr; + } + + for (const auto& ext : extension) { + auto filePng = Ship::Context::GetInstance()->GetResourceManager()->LoadFileProcess( + initData->Path + ext); + + if (filePng != nullptr) { + return loadPngTexture(filePng, initData); + } + } + + auto texture = std::make_shared(initData); + auto reader = std::get>(file->Reader); + + texture->Type = (Fast::TextureType)reader->ReadUInt32(); + texture->Width = reader->ReadUInt32(); + texture->Height = reader->ReadUInt32(); + texture->ImageDataSize = reader->ReadUInt32(); + texture->ImageData = new uint8_t[texture->ImageDataSize]; + + reader->Read((char*)texture->ImageData, texture->ImageDataSize); + + return texture; +} + +std::shared_ptr +ResourceFactoryBinaryTextureV1::ReadResource(std::shared_ptr file, + std::shared_ptr initData) { + if (!FileHasValidFormatAndReader(file, initData)) { + return nullptr; + } + + for (const auto& ext : extension) { + auto filePng = Ship::Context::GetInstance()->GetResourceManager()->LoadFileProcess( + initData->Path + ext); + + if (filePng != nullptr) { + return loadPngTexture(filePng, initData); + } + } + + auto texture = std::make_shared(initData); + auto reader = std::get>(file->Reader); + + texture->Type = (Fast::TextureType)reader->ReadUInt32(); + texture->Width = reader->ReadUInt32(); + texture->Height = reader->ReadUInt32(); + texture->Flags = reader->ReadUInt32(); + texture->HByteScale = reader->ReadFloat(); + texture->VPixelScale = reader->ReadFloat(); + texture->ImageDataSize = reader->ReadUInt32(); + texture->ImageData = new uint8_t[texture->ImageDataSize]; + + reader->Read((char*)texture->ImageData, texture->ImageDataSize); + + return texture; +} +} // namespace Fast \ No newline at end of file diff --git a/src/port/importer/RawTextureFactory.h b/src/port/importer/RawTextureFactory.h new file mode 100644 index 00000000..8138f9ee --- /dev/null +++ b/src/port/importer/RawTextureFactory.h @@ -0,0 +1,18 @@ +#pragma once + +#include "ship/resource/Resource.h" +#include "ship/resource/ResourceFactoryBinary.h" + +namespace MK64 { +class ResourceFactoryBinaryTextureV0 final : public Ship::ResourceFactoryBinary { + public: + std::shared_ptr ReadResource(std::shared_ptr file, + std::shared_ptr initData) override; +}; + +class ResourceFactoryBinaryTextureV1 final : public Ship::ResourceFactoryBinary { + public: + std::shared_ptr ReadResource(std::shared_ptr file, + std::shared_ptr initData) override; +}; +} // namespace MK64 \ No newline at end of file diff --git a/src/port/importer/types/AssetArray.h b/src/port/importer/types/AssetArray.h index e6584f94..be6986c5 100644 --- a/src/port/importer/types/AssetArray.h +++ b/src/port/importer/types/AssetArray.h @@ -13,5 +13,6 @@ namespace SM64 { size_t GetPointerSize(); std::vector mPtrs; + std::vector mPaths; }; } \ No newline at end of file