Shipwright/ZAPDTR/ZAPD/ZRom.cpp

233 lines
6.3 KiB
C++
Raw Normal View History

#include "ZRom.h"
#include "Utils/BitConverter.h"
#include "Utils/File.h"
#include "Utils/Directory.h"
#include "yaz0/yaz0.h"
Add Support for macOS (#441) * Fixed soh filters * add more makefile changes * almost ready * more updates * update * update * Update Makefiles to handle both platforms * Allow for overriding the CXX and CC executables * Restore original structure while supporting custom CXX flags * Remove some platform specific libs * Dynamic target name * Make X11 paths package-agnostic * Remove changes to `gfx_opengl.cpp` * Use OpenGL2 on MacOS instead of OpenGL3 * make it actually render something * render at least the first texture, still need to figure out the second one * Let’s use OpenGL 3 again * maybe this works to get the right texture? link's eyes still look off a bit * did this work? * set the platform to macos * actual numbers are right, but logic is ugly XXX/TODO, i know * add zlib to ldflags for ZAPDUtils * A bit of cleanup * Revert unneeded changes * Remove GL_CHECK * Fix issues with z64 branch * use an std::map instead of a giant array * three point filter fix (#2) * Fix mac compilation * fix audio for 64 bit * revert audio heap size, keep bigger pools * Add more Apple specific checks to our modifications * Add building instructions for macOS * Remove unecessary step from building instructions * Add missing SDL2 & GLEW to Linux LDLIBS * Update BUILDING.md Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com> * Update soh/.gitignore to include other arch binaries Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com> * Use right platform name for debugging window Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com> * Fix stormlib on macos (arm64) * Simplify some of the ifdef checks * Revert an older no longer necessary fix * Remove remaining unecessary deviations * Update building instructions after StormLib changes * Feature: Use OpenGL 4.1 (#1) * Further tweak the BUILDING * Tidy up * reword -j message * Add Jenkins CI Support (#2) * Fix type issues * add target <appbundle> and <filledappbundle> add makefile targets to create an .app `filledappbundle` creates the target with the .otr included this should perhaps be moved to Application Support though * pull gcc's rpath from otool output * move make target to the end so it's not default * Add Jenkins and make exe in par with other platforms * Actually save build artefacts * Fix artefact path * Remove x11 mentions and linking (not used) * Update building instructions for generating app * use appsupport directory * Add new app icon * Update target to match macOS types * Update more audio types * fix null deref in Audio_PlayFanfare * Remove old import from z64 * address final nit with apple ifdefs Co-authored-by: KiritoDev <36680385+KiritoDv@users.noreply.github.com> Co-authored-by: Jeffrey Crowell <github@crowell.biz> Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com>
2022-06-22 14:59:21 -04:00
#ifdef __linux__
#include <byteswap.h>
#endif
#include <Globals.h>
namespace fs = std::filesystem;
#define DMA_ENTRY_SIZE 16
#if defined(_MSC_VER)
#define __bswap_32 _byteswap_ulong
#define bswap_32 _byteswap_ulong
#endif
Add Support for macOS (#441) * Fixed soh filters * add more makefile changes * almost ready * more updates * update * update * Update Makefiles to handle both platforms * Allow for overriding the CXX and CC executables * Restore original structure while supporting custom CXX flags * Remove some platform specific libs * Dynamic target name * Make X11 paths package-agnostic * Remove changes to `gfx_opengl.cpp` * Use OpenGL2 on MacOS instead of OpenGL3 * make it actually render something * render at least the first texture, still need to figure out the second one * Let’s use OpenGL 3 again * maybe this works to get the right texture? link's eyes still look off a bit * did this work? * set the platform to macos * actual numbers are right, but logic is ugly XXX/TODO, i know * add zlib to ldflags for ZAPDUtils * A bit of cleanup * Revert unneeded changes * Remove GL_CHECK * Fix issues with z64 branch * use an std::map instead of a giant array * three point filter fix (#2) * Fix mac compilation * fix audio for 64 bit * revert audio heap size, keep bigger pools * Add more Apple specific checks to our modifications * Add building instructions for macOS * Remove unecessary step from building instructions * Add missing SDL2 & GLEW to Linux LDLIBS * Update BUILDING.md Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com> * Update soh/.gitignore to include other arch binaries Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com> * Use right platform name for debugging window Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com> * Fix stormlib on macos (arm64) * Simplify some of the ifdef checks * Revert an older no longer necessary fix * Remove remaining unecessary deviations * Update building instructions after StormLib changes * Feature: Use OpenGL 4.1 (#1) * Further tweak the BUILDING * Tidy up * reword -j message * Add Jenkins CI Support (#2) * Fix type issues * add target <appbundle> and <filledappbundle> add makefile targets to create an .app `filledappbundle` creates the target with the .otr included this should perhaps be moved to Application Support though * pull gcc's rpath from otool output * move make target to the end so it's not default * Add Jenkins and make exe in par with other platforms * Actually save build artefacts * Fix artefact path * Remove x11 mentions and linking (not used) * Update building instructions for generating app * use appsupport directory * Add new app icon * Update target to match macOS types * Update more audio types * fix null deref in Audio_PlayFanfare * Remove old import from z64 * address final nit with apple ifdefs Co-authored-by: KiritoDev <36680385+KiritoDv@users.noreply.github.com> Co-authored-by: Jeffrey Crowell <github@crowell.biz> Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com>
2022-06-22 14:59:21 -04:00
#if defined __APPLE__
#define __bswap32 __builtin_bswap32
#define bswap32 __builtin_bswap32
#endif
// ROM DMA Table Start
#define OOT_OFF_NTSC_10_RC 0x7430
#define OOT_OFF_NTSC_10 0x7430
#define OOT_OFF_NTSC_11 0x7430
#define OOT_OFF_PAL_10 0x7950
#define OOT_OFF_NTSC_12 0x7960
#define OOT_OFF_PAL_11 0x7950
#define OOT_OFF_JP_GC 0x7170
#define OOT_OFF_JP_MQ 0x7170
#define OOT_OFF_US_GC 0x7170
#define OOT_OFF_US_MQ 0x7170
#define OOT_OFF_PAL_GC_DBG1 0x12F70
#define OOT_OFF_PAL_MQ_DBG 0x12F70
#define OOT_OFF_PAL_GC_DBG2 0x12F70
#define OOT_OFF_PAL_GC 0x7170
#define OOT_OFF_PAL_MQ 0x7170
#define OOT_OFF_JP_GC_CE 007170
#define OOT_OFF_CN_IQUE 0xB7A0
#define OOT_OFF_TW_IQUE 0xB240
#define MM_OFF_US_10 0x1A500
#define MM_OFF_JP_10 0x1C110
#define MM_OFF_JP_11 0x1C050
#define MM_OFF_DBG 0x24F60
#define OOT_NTSC_10 0xEC7011B7
#define OOT_NTSC_11 0xD43DA81F
#define OOT_NTSC_12 0x693BA2AE
#define OOT_PAL_10 0xB044B569
#define OOT_PAL_11 0xB2055FBD
#define OOT_NTSC_JP_GC_CE 0xF7F52DB8
#define OOT_NTSC_JP_GC 0xF611F4BA
#define OOT_NTSC_US_GC 0xF3DD35BA
#define OOT_PAL_GC 0x09465AC3
#define OOT_NTSC_JP_MQ 0xF43B45BA
#define OOT_NTSC_US_MQ 0xF034001A
#define OOT_PAL_MQ 0x1D4136F3
#define OOT_PAL_GC_DBG1 0x871E1C92 // 03-21-2002 build
#define OOT_PAL_GC_DBG2 0x87121EFE // 03-13-2002 build
#define OOT_PAL_GC_MQ_DBG 0x917D18F6
#define OOT_IQUE_TW 0x3D81FB3E
#define OOT_IQUE_CN 0xB1E1E07B
Dual OTR MQ and Vanilla Support (#1694) * Changes OTR Extraction to have specific mq and nonmq paths. Also updates the game to load resources according to whether or not Master Quest or Vanilla is loaded. * Removes unneeded code from the last commit. * Fixes some weird formatting in ZRom.c * Loads oot-mq.otr and patches oot.otr on top, if both are present. If only one or the other are present, it becomes the only and main OTR. * Adds ImGui Logic for whether or an MQ Checkbox. Checkbox checked only specifies whether new saves should be MQ or not. Checkbox is disabled or force-enabled according to which OTRs are loaded. Also as a necessity includes tracking what game versions have been loaded from the OTRs. * Adds MQ settings logic for Randomizer's ImGui menu. * Writes Master Quest dungeons to the spoiler log * Loads MQ Dungeons from spoiler, persists in save, and loads when appropriate. * Adds logic to prevent loading or creating incompatible rando saves. * Fixdes some linux build issues and new rando save issues * Makes appimage create both vanilla and mq otrs If either rom is present, it makes the corresponding OTR. If both are present, it will make both. If one OTR is present but both roms are present, it will create the missing OTR. * Makes it so a randomized save file will not be marked as MQ. * Refactors to load all OTRs from MainPath or a specific list. Also adds the ability to take a std::unordered_set of hashes to validate each OTR's version file against. * Fixes a syntax error * Makes ExtractAssets output Vanilla and MQ OTRs if both roms are present * Fixes asset generation bug. * Partially working fix for dual OTR extract_assets Currently the cmake ExtractAssets target will return with a 1 if you only end up exporting one type of OTR isntead of both. Haven't found a great way to only attempt to copy a file if it exists from within cmake. It does actually correctly copy the OTR that is generated, despite the error from copying the other one. Pushing as is for now but will keep investigating. * Adds oot-mq.otr to the gitignore. * Makes ExtractAssets not fail on only one rom/OTR. * Removes PatchesPath from the constructors requiring OTRFiles vector. * Renames OOT_UNKNOWN to just UNKNOWN to remove OOT specific reference. * Removes randomizing MQ Dungeons and re-disables MQ rando. Doing this so the PR can get merged quicker with just the Dual OTR support and won't need to wait on rando logic to be updated. That will happen in another PR directly after the merge. * Update mac startup script for dual otr * Update soh/macosx/soh-macos.sh * Update soh/macosx/soh-macos.sh * Update soh/macosx/soh-macos.sh * Implements new BinaryReader to fix Linux build issue. BinaryReader itself comes from https://github.com/Moneyl/BinaryTools I added a wrapper to adapt it to the ABI from ZAPD's Binary Reader and add Endianness checking. I also had to copy a handful of other bits and pieces from ZAPD to make it all function as expected. * A few edits to the updatream BinaryReader to compile it on Linux. * Adds the Endianness to the first byte of the version file. * Fixes Jenkins * Addresses some of Kenix's comments * Renames `ReadNullTerminatedString` to `ReadCString` * Refactors Archive::LoadFile into a private method with more arguments. * Removes BitConverter and extends existing endianness.h instead. * Fixes an endianness issue with the version file. Co-authored-by: Garrett Cox <garrettjcox@gmail.com>
2022-10-16 23:07:35 -04:00
#define UNKNOWN 0xFFFFFFFF
bool ZRom::IsMQ() {
int crc = BitConverter::ToInt32BE(romData, 0x10);
switch (crc) {
case OOT_NTSC_10:
case OOT_NTSC_11:
case OOT_NTSC_12:
case OOT_PAL_10:
case OOT_PAL_11:
case OOT_NTSC_JP_GC:
case OOT_NTSC_JP_GC_CE:
case OOT_NTSC_US_GC:
case OOT_PAL_GC:
case OOT_PAL_GC_DBG1:
case OOT_PAL_GC_DBG2:
case OOT_IQUE_CN:
case OOT_IQUE_TW:
default:
return false;
case OOT_NTSC_JP_MQ:
case OOT_NTSC_US_MQ:
case OOT_PAL_MQ:
case OOT_PAL_GC_MQ_DBG:
return true;
}
}
ZRom::ZRom(std::string romPath)
{
RomVersion version;
romData = File::ReadAllBytes(romPath);
version.crc = BitConverter::ToInt32BE(romData, 0x10);
switch (version.crc)
{
case OOT_NTSC_10:
version.version = "N64 NTSC 1.0";
version.listPath = "ntsc_oot.txt";
version.offset = OOT_OFF_NTSC_10;
break;
case OOT_NTSC_11:
version.version = "N64 NTSC 1.1";
version.listPath = "ntsc_oot.txt";
version.offset = OOT_OFF_NTSC_11;
break;
case OOT_NTSC_12:
version.version = "N64 NTSC 1.2";
version.listPath = "ntsc_oot.txt";
version.offset = OOT_OFF_NTSC_12;
break;
case OOT_PAL_10:
version.version = "N64 PAL 1.0";
version.listPath = "pal_oot.txt";
version.offset = OOT_OFF_PAL_10;
break;
case OOT_PAL_11:
version.version = "N64 PAL 1.1";
version.listPath = "pal_oot.txt";
version.offset = OOT_OFF_PAL_11;
break;
case OOT_NTSC_JP_GC:
version.version = "JP GameCube (MQ Disk)";
version.listPath = "gamecube.txt";
version.offset = OOT_OFF_JP_GC;
break;
case OOT_NTSC_JP_GC_CE:
version.version = "GameCube (Collectors Edition Disk)";
version.listPath = "gamecube.txt";
version.offset = OOT_OFF_JP_GC_CE;
break;
case OOT_NTSC_JP_MQ:
version.version = "JP Master Quest";
version.listPath = "gamecube.txt";
version.offset = OOT_OFF_JP_MQ;
break;
case OOT_NTSC_US_MQ:
version.version = "NTSC Master Quest";
version.listPath = "gamecube.txt";
version.offset = OOT_OFF_JP_MQ;
break;
case OOT_NTSC_US_GC:
version.version = "NTSC GameCube";
version.listPath = "gamecube.txt";
version.offset = OOT_OFF_US_MQ;
break;
case OOT_PAL_GC:
version.version = "PAL GameCube";
version.listPath = "gamecube_pal.txt";
version.offset = OOT_OFF_PAL_GC;
break;
case OOT_PAL_MQ:
version.version = "PAL Master Quest";
version.listPath = "gamecube_pal.txt";
version.offset = OOT_OFF_PAL_MQ;
break;
case OOT_PAL_GC_DBG1:
version.version = "GameCube Debug 1.0";
version.listPath = "dbg.txt";
version.offset = OOT_OFF_PAL_GC_DBG1;
break;
case OOT_PAL_GC_DBG2:
version.version = "GameCube Debug 2.0";
version.listPath = "dbg.txt";
version.offset = OOT_OFF_PAL_GC_DBG2;
break;
case OOT_PAL_GC_MQ_DBG:
version.version = "GameCube MQ-Debug";
version.listPath = "dbg.txt";
version.offset = OOT_OFF_PAL_MQ_DBG;
break;
case OOT_IQUE_CN:
version.version = "OoT IQue";
version.listPath = "ique.txt";
version.offset = OOT_OFF_CN_IQUE;
break;
case OOT_IQUE_TW:
version.version = "TW IQue";
version.listPath = "ique.txt";
version.offset = OOT_OFF_TW_IQUE;
break;
}
auto path = StringHelper::Sprintf("%s/%s", Globals::Instance->fileListPath.string().c_str(), version.listPath.c_str());
auto txt = File::ReadAllText(path);
std::vector<std::string> lines = StringHelper::Split(txt, "\n");
std::vector<uint8_t> decompressedData(1);
for (int i = 0; i < lines.size(); i++)
{
lines[i] = StringHelper::Strip(lines[i], "\r");
const int romOffset = version.offset + (DMA_ENTRY_SIZE * i);
const int virtStart = BitConverter::ToInt32BE(romData, romOffset + 0);
const int virtEnd = BitConverter::ToInt32BE(romData, romOffset + 4);
const int physStart = BitConverter::ToInt32BE(romData, romOffset + 8);
const int physEnd = BitConverter::ToInt32BE(romData, romOffset + 12);
const bool compressed = physEnd != 0;
int size = compressed ? physEnd - physStart : virtEnd - virtStart;
auto outData = std::vector<uint8_t>();
outData.resize(size);
memcpy(outData.data(), romData.data() + physStart, size);
if (compressed)
{
int decSize = virtEnd - virtStart;
decompressedData = std::vector<uint8_t>();
decompressedData.resize(decSize);
yaz0_decode(outData.data(), decompressedData.data(), decSize);
files[lines[i]] = decompressedData;
}
else
files[lines[i]] = outData;
2022-06-14 12:32:11 -04:00
//File::WriteAllBytes(StringHelper::Sprintf("baserom/%s", lines[i].c_str()), files[lines[i]]);
}
}
std::vector<uint8_t> ZRom::GetFile(std::string fileName)
{
return files[fileName];
}