tp/libs/JSystem/JKernel/JKRArchivePri.cpp

214 lines
5.7 KiB
C++

#include "JSystem/JKernel/JKRArchive/JKRArchive.h"
#include "JSystem/JKernel/JKRHeap/JKRHeap.h"
#include "global.h"
#include "msl_c/string.h"
JKRArchive::JKRArchive(long entryNumber, JKRArchive::EMountMode mountMode) {
mIsMounted = false;
mMountMode = mountMode;
mMountCount = 1;
field_0x58 = 1;
mHeap = JKRHeap::findFromRoot(this);
if (mHeap == NULL) {
mHeap = JKRHeap::getCurrentHeap();
}
mEntryNum = entryNumber;
if (getCurrentVolume() == NULL) {
setCurrentVolume(this);
setCurrentDirID(0);
}
}
JKRArchive::~JKRArchive() {}
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;
}
SDirEntry* JKRArchive::findResType(u32 type) const {
SDirEntry* node = mNodes;
u32 count = 0;
while (count < mArcInfoBlock->num_nodes) {
if (node->type == type) {
return node;
}
node++;
count++;
}
return NULL;
}
SDirEntry* JKRArchive::findDirectory(const char* name, u32 directoryId) const {
if (name == NULL) {
return mNodes + directoryId;
}
CArcName arcName(&name, '/');
SDirEntry* dirEntry = mNodes + directoryId;
SDIFileEntry* fileEntry = mFiles + dirEntry->first_file_index;
for (int i = 0; i < dirEntry->num_entries; fileEntry++, i++) {
// regalloc doesn't like fileEntry->getNameHash()
if (isSameName(arcName, fileEntry->getNameOffset(), fileEntry->name_hash)) {
if (fileEntry->isDirectory()) {
return findDirectory(name, fileEntry->data_offset);
}
break;
}
}
return NULL;
}
SDIFileEntry* JKRArchive::findTypeResource(u32 type, const char* name) const {
if (type) {
CArcName arcName(name);
SDirEntry* dirEntry = findResType(type);
if (dirEntry) {
SDIFileEntry* fileEntry = mFiles + dirEntry->first_file_index;
for (int i = 0; i < dirEntry->num_entries; fileEntry++, i++) {
if (isSameName(arcName, fileEntry->getNameOffset(), fileEntry->getNameHash())) {
return fileEntry;
}
}
}
}
return NULL;
}
SDIFileEntry* JKRArchive::findFsResource(const char* name, u32 directoryId) const {
if (name) {
CArcName arcName(&name, '/');
SDirEntry* dirEntry = mNodes + directoryId;
SDIFileEntry* fileEntry = mFiles + dirEntry->first_file_index;
for (int i = 0; i < dirEntry->num_entries; fileEntry++, i++) {
// regalloc doesn't like fileEntry->getNameHash()
if (isSameName(arcName, fileEntry->getNameOffset(), fileEntry->name_hash)) {
if (fileEntry->isDirectory()) {
return findFsResource(name, fileEntry->data_offset);
}
if (name == NULL) {
return fileEntry;
}
return NULL;
}
}
}
return NULL;
}
SDIFileEntry* JKRArchive::findIdxResource(u32 fileIndex) const {
if (fileIndex < mArcInfoBlock->num_file_entries) {
return mFiles + fileIndex;
}
return NULL;
}
SDIFileEntry* JKRArchive::findNameResource(const char* name) const {
SDIFileEntry* fileEntry = mFiles;
CArcName arcName(name);
for (int i = 0; i < mArcInfoBlock->num_file_entries; fileEntry++, i++) {
if (isSameName(arcName, fileEntry->getNameOffset(), fileEntry->getNameHash())) {
return fileEntry;
}
}
return NULL;
}
SDIFileEntry* JKRArchive::findPtrResource(const void* resource) const {
SDIFileEntry* fileEntry = mFiles;
for (int i = 0; i < mArcInfoBlock->num_file_entries; fileEntry++, i++) {
if (fileEntry->data == resource) {
return fileEntry;
}
}
return NULL;
}
SDIFileEntry* JKRArchive::findIdResource(u16 id) const {
if (id != 0xFFFF) {
if (id < mArcInfoBlock->num_file_entries) {
SDIFileEntry* fileEntry = mFiles + id;
if (fileEntry->file_id == id && fileEntry->isUnknownFlag1()) {
return fileEntry;
}
}
SDIFileEntry* fileEntry = mFiles;
for (int i = 0; i < mArcInfoBlock->num_file_entries; fileEntry++, i++) {
if (fileEntry->file_id == id && fileEntry->isUnknownFlag1()) {
return 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 < (s32)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 < (s32)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];
}