SMG-Decomp
A decompilation of Super Mario Galaxy 1
Loading...
Searching...
No Matches
ResourceInfo.cpp
1#include "Game/System/ResourceInfo.hpp"
2#include "Game/Util.hpp"
3#include <cstring>
4#include <cstdio>
5#include <size_t.h>
6
7ResFileInfo::ResFileInfo() {
8 mName = 0;
9 mHashCode = 0;
10 mResource = 0;
11 _8 = 0;
12 _4 = 0;
13 _C = 0;
14}
15
16ResTable::ResTable()
17 : mFileInfoTable(0), mCount(0) {
18}
19
20void ResTable::newFileInfoTable(u32 count) {
21 mFileInfoTable = new ResFileInfo[count];
22}
23
24ResFileInfo* ResTable::add(const char *pName, void *pRes, bool stripExt) {
25 ResFileInfo* curInfo = &mFileInfoTable[mCount];
26 curInfo->mResource = pRes;
27 curInfo->setName(pName, stripExt);
28 mCount++;
29 return curInfo;
30}
31
32const char* ResTable::getResName(u32 idx) const {
33 return mFileInfoTable[idx].mName;
34}
35
36void* ResTable::getRes(u32 idx) const {
37 return mFileInfoTable[idx].mResource;
38}
39
40void* ResTable::getRes(const char *pName) const {
41 return findRes(pName);
42}
43
44ResFileInfo* ResTable::findFileInfo(const char *pName) const {
45 int resIdx = getResIndex(pName);
46 if (resIdx != -1) {
47 return &mFileInfoTable[resIdx];
48 }
49
50 return 0;
51}
52
53ResFileInfo* ResTable::getFileInfo(u32 idx) const {
54 return &mFileInfoTable[idx];
55}
56
57bool ResTable::isExistRes(const char *pRes) const {
58 return getResIndex(pRes) != -1;
59}
60
61void* ResTable::findRes(const char *pName) const {
62 int idx = getResIndex(pName);
63
64 if (idx != -1) {
65 return mFileInfoTable[idx].mResource;
66 }
67
68 return 0;
69}
70
71int ResTable::getResIndex(const char *pName) const {
72 u32 hash = MR::getHashCodeLower(pName);
73
74 for (int i = 0; i < mCount; i++) {
75 u32 curHash = mFileInfoTable[i].mHashCode;
76
77 if (curHash == hash) {
78 return i;
79 }
80 }
81
82 return -1;
83}
84
85const char* ResTable::findResName(const void *pRes) const {
86 for (int i = 0; i < mCount; i++) {
87 ResFileInfo* inf = &mFileInfoTable[i];
88 if (pRes == inf->mResource) {
89 return inf->mName;
90 }
91 }
92
93 return 0;
94}
95
96const char* ResTable::getResName(const void *pResource) const {
97 return findResName(pResource);
98}
99
100void ResFileInfo::setName(const char *pName, bool stripExt) {
101 size_t len = strlen(pName) + 1;
102 mName = new char[len];
103
104 snprintf(mName, len, "%s", pName);
105
106 if (stripExt) {
107 char* out = strrchr(mName, '.');
108
109 if (out) {
110 out[0] = 0;
111 }
112 }
113
114 mHashCode = MR::getHashCodeLower(mName);
115}