Files
dusklight/libs/JSystem/src/JKernel/JKRArchivePri.cpp
T
qwertyquerty 767ba3bb14 Ongoing gameplay dev (#49)
* launch.json cwd

* bodge to load gci for testing

* stub card stat

* gameplay bodges

* viewport, ub fixes

* add release with debug info cmake variant

* be fixes, sound stub

* viewport h

* d_msg_flow BE

* be fopAcM_createItemFromEnemyID

* update launch configuration to use iso

* more audio stubs

* Attempt to set viewport and get messages for brightness check

* skip opening scene again, fixed JMessage::TResourceContainer::TCResource::Do_destroy

* add guards for viewport changes

* moar endian swapping to get Link sitting in PROC_OPENING_SCENE and for dialogues

* BE d_msg_class i_data

* stub bgm start

* fix div by 0 error (for now)

* TEMP_BROKEN in d_menu_ring

* REQUIRES_GX_LINES

* properly stub renderingAmap::draw with REQUIRES_GX_LINES

* better stubbing outside of stubs

* fix event data getting swapped multiple times

* evil draw vp fix

* Stub log imgui

This redirects all spammy logs to an imgui window that is cleared per frame.

This fixes the serious performance dip of the logging, and makes the regular log readable.

* Oops move those optimization changes I accidentally committed behind a flag

DUSK_SELECTED_OPT

* gx_line macro in map

* fix audio stubbing

* switch to CARD API aurora impl

* remove kabufuda from link libs

* refactor imgui stuff and add input viewer

* merge stub log with refactor

* accidentally committed a metaforce header shh

* basic map loader

* ImGuiConsole: Add missing <thread> include

* you may now play as luigi (you may now load stages with bridges)

* bloom fix

* bloom leak fix

* cloud shadow fix

* add soft reset button to imgui menu

* if it broke dont not fix it

* i swear i committed this

* BE swap indMtx in JPAResource::setPTev

* wnark ct fix

* frsqrte implementation from kinoko

* Fix Clang compile error in JAISeq::prepare_getSeqData_

* Add endian conversions to dMsgFlow_c::getInitNodeIndex

This fixes a freeze when Fado tries to stop you from leaving the
starting area.

* Add RAII GXTexObj wrapper; fix almost all leaks

* Update aurora for indirect texturing

* Update aurora for CARD fix

* Fix Clang build

* More d_msg_flow endian fixes

Fixes softlock when trying to talk to Fado and possibly other NPCs.

* no frame limiter

* get pause menu working

* proper frame limiting

* particle pointer size fix

* improve map loader a bit

---------

Co-authored-by: Jasper St. Pierre <jstpierre@mecheye.net>
Co-authored-by: TakaRikka <takarikka@outlook.com>
Co-authored-by: CraftyBoss <talibabdulmaalik@gmail.com>
Co-authored-by: Luke Street <luke@street.dev>
Co-authored-by: Lurs <2795933+Lurs@users.noreply.github.com>
Co-authored-by: PJB3005 <pieterjan.briers+git@gmail.com>
Co-authored-by: tgsm <doodrabbit@hotmail.com>
Co-authored-by: Max Roncace <me@caseif.net>
Co-authored-by: Phillip Stephens <antidote.crk@gmail.com>
2026-03-12 04:01:03 -07:00

274 lines
7.1 KiB
C++

#include "JSystem/JSystem.h" // IWYU pragma: keep
#include "JSystem/JKernel/JKRArchive.h"
#include "JSystem/JKernel/JKRHeap.h"
#include <cctype>
#include <cstring>
#if TARGET_PC
#include <cassert>
#endif
u32 JKRArchive::sCurrentDirID;
JKRArchive::JKRArchive() {
mIsMounted = false;
mMountDirection = MOUNT_DIRECTION_HEAD;
#if TARGET_PC
mFileData = nullptr;
#endif
}
JKRArchive::JKRArchive(s32 entryNumber, JKRArchive::EMountMode mountMode) {
mIsMounted = false;
mMountMode = mountMode;
mMountCount = 1;
field_0x58 = 1;
#if TARGET_PC
mFileData = nullptr;
#endif
mHeap = JKRHeap::findFromRoot(this);
if (mHeap == NULL) {
mHeap = JKRHeap::getCurrentHeap();
}
mEntryNum = entryNumber;
if (sCurrentVolume == NULL) {
sCurrentVolume = this;
sCurrentDirID = 0;
}
}
JKRArchive::~JKRArchive() {
#if TARGET_PC
if (mFileData != nullptr) {
JKRHeap::getSystemHeap()->free(mFileData);
mFileData = nullptr;
}
#endif
}
bool JKRArchive::isSameName(JKRArchive::CArcName& name, u32 nameOffset, u16 nameHash) const {
u16 hash = name.getHash();
if (hash != nameHash)
return false;
return strcmp(mStringTable + nameOffset, name.getString()) == 0;
}
JKRArchive::SDIDirEntry* JKRArchive::findResType(u32 type) const {
SDIDirEntry* node = mNodes;
for (u32 count = 0; count < mArcInfoBlock->num_nodes; count++) {
if (node->type == type) {
return node;
}
node++;
}
return NULL;
}
JKRArchive::SDIDirEntry* JKRArchive::findDirectory(const char* name, u32 directoryId) const {
if (name == NULL) {
return mNodes + directoryId;
}
CArcName arcName(&name, '/');
SDIDirEntry* dirEntry = mNodes + directoryId;
SDIFileEntry* fileEntry = mFiles + dirEntry->first_file_index;
for (int i = 0; i < dirEntry->num_entries; i++) {
if (isSameName(arcName, fileEntry->type_flags_and_name_offset & 0xFFFFFF, fileEntry->name_hash)) {
if ((fileEntry->type_flags_and_name_offset >> 24) & 2) {
return findDirectory(name, fileEntry->data_offset);
}
break;
}
fileEntry++;
}
return NULL;
}
JKRArchive::SDIFileEntry* JKRArchive::findTypeResource(u32 type, const char* name) const {
if (type) {
CArcName arcName(name);
SDIDirEntry* dirEntry = findResType(type);
if (dirEntry) {
SDIFileEntry* fileEntry = mFiles + dirEntry->first_file_index;
for (int i = 0; i < dirEntry->num_entries; i++) {
if (isSameName(arcName, fileEntry->type_flags_and_name_offset & 0xFFFFFF, fileEntry->name_hash)) {
return fileEntry;
}
fileEntry++;
}
}
}
return NULL;
}
JKRArchive::SDIFileEntry* JKRArchive::findFsResource(const char* name, u32 directoryId) const {
if (name) {
CArcName arcName(&name, '/');
SDIDirEntry* dirEntry = mNodes + directoryId;
SDIFileEntry* fileEntry = mFiles + dirEntry->first_file_index;
for (int i = 0; i < dirEntry->num_entries; i++) {
if (isSameName(arcName, fileEntry->type_flags_and_name_offset & 0xFFFFFF, fileEntry->name_hash)) {
if ((fileEntry->type_flags_and_name_offset >> 24) & 2) {
return findFsResource(name, fileEntry->data_offset);
}
if (name == NULL) {
return fileEntry;
}
return NULL;
}
fileEntry++;
}
}
return NULL;
}
JKRArchive::SDIFileEntry* JKRArchive::findIdxResource(u32 fileIndex) const {
if (fileIndex < mArcInfoBlock->num_file_entries) {
return mFiles + fileIndex;
}
return NULL;
}
JKRArchive::SDIFileEntry* JKRArchive::findNameResource(const char* name) const {
SDIFileEntry* fileEntry = mFiles;
CArcName arcName(name);
for (int i = 0; i < mArcInfoBlock->num_file_entries; i++) {
if (isSameName(arcName, fileEntry->type_flags_and_name_offset & 0xFFFFFF, fileEntry->name_hash)) {
return fileEntry;
}
fileEntry++;
}
return NULL;
}
JKRArchive::SDIFileEntry* JKRArchive::findPtrResource(const void* resource) const {
SDIFileEntry* fileEntry = mFiles;
for (int i = 0; i < mArcInfoBlock->num_file_entries; i++) {
if (JKAR_DATA(fileEntry) == resource) {
return fileEntry;
}
fileEntry++;
}
return NULL;
}
JKRArchive::SDIFileEntry* JKRArchive::findIdResource(u16 id) const {
if (id != 0xFFFF) {
SDIFileEntry* fileEntry;
if (id < mArcInfoBlock->num_file_entries) {
fileEntry = mFiles + id;
if (fileEntry->file_id == id && ((fileEntry->type_flags_and_name_offset >> 24) & 1)) {
return fileEntry;
}
}
fileEntry = mFiles;
for (int i = 0; i < mArcInfoBlock->num_file_entries; i++) {
if (fileEntry->file_id == id && ((fileEntry->type_flags_and_name_offset >> 24) & 1)) {
return fileEntry;
}
fileEntry++;
}
}
return NULL;
}
void JKRArchive::CArcName::store(const char* name) {
mHash = 0;
s32 length = 0;
while (*name) {
s32 ch = tolower(*name);
mHash = ch + mHash * 3;
if (length < ARRAY_SIZE(mData)) {
mData[length++] = ch;
}
name++;
}
mLength = (u16)length;
mData[length] = 0;
}
const char* JKRArchive::CArcName::store(const char* name, char endChar) {
mHash = 0;
s32 length = 0;
while (*name && *name != endChar) {
s32 lch = tolower((int)*name);
mHash = lch + mHash * 3;
if (length < ARRAY_SIZE(mData)) {
mData[length++] = lch;
}
name++;
}
mLength = (u16)length;
mData[length] = 0;
if (*name == 0)
return NULL;
return name + 1;
}
void JKRArchive::setExpandSize(SDIFileEntry* fileEntry, u32 expandSize) {
int index = fileEntry - mFiles;
if (!mExpandedSize || index >= mArcInfoBlock->num_file_entries)
return;
mExpandedSize[index] = expandSize;
}
u32 JKRArchive::getExpandSize(SDIFileEntry* fileEntry) const {
int index = fileEntry - mFiles;
if (!mExpandedSize || index >= mArcInfoBlock->num_file_entries)
return 0;
return mExpandedSize[index];
}
#if TARGET_PC
void*& JKRArchive::getFileDataPointer(int idx) const {
assert(mArcInfoBlock);
assert(idx < mArcInfoBlock->num_file_entries);
assert(mFileData);
return mFileData[idx];
}
void JKRArchive::initFileDataPointers() {
assert(mArcInfoBlock);
assert(mFiles);
if (mFileData != nullptr) {
JKRHeap::getSystemHeap()->free(mFileData);
mFileData = nullptr;
}
mFileData = static_cast<void**>(
JKRHeap::getSystemHeap()->alloc(mArcInfoBlock->num_file_entries * sizeof(void*), alignof(void*)));
memset(mFileData, 0, mArcInfoBlock->num_file_entries * sizeof(void*));
for (u32 i = 0; i < mArcInfoBlock->num_file_entries; i++) {
mFiles[i].index = i;
}
}
#endif