A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
KartParamFileManager.cc
1#include "KartParamFileManager.hh"
2
3namespace Kart {
4
6void KartParamFileManager::clear() {
7 m_kartParam.clear();
8 m_driverParam.clear();
9 m_bikeDispParam.clear();
10}
11
15 m_kartParam.load("kartParam.bin");
16 m_driverParam.load("driverParam.bin");
17 m_bikeDispParam.load("bikePartsDispParam.bin");
18 if (!validate()) {
19 PANIC("Parameter files could not be validated!");
20 }
21}
22
23EGG::RamStream KartParamFileManager::getDriverStream(Character character) const {
24 s32 idx = -1;
25 switch (character) {
26 case Character::Small_Mii_Outfit_A_Male:
27 case Character::Small_Mii_Outfit_A_Female:
28 case Character::Small_Mii_Outfit_B_Male:
29 case Character::Small_Mii_Outfit_B_Female:
30 case Character::Small_Mii_Outfit_C_Male:
31 case Character::Small_Mii_Outfit_C_Female:
32 case Character::Small_Mii:
33 idx = 23;
34 break;
35 case Character::Medium_Mii_Outfit_A_Male:
36 case Character::Medium_Mii_Outfit_A_Female:
37 case Character::Medium_Mii_Outfit_B_Male:
38 case Character::Medium_Mii_Outfit_B_Female:
39 case Character::Medium_Mii_Outfit_C_Male:
40 case Character::Medium_Mii_Outfit_C_Female:
41 case Character::Medium_Mii:
42 idx = 24;
43 break;
44 case Character::Large_Mii_Outfit_A_Male:
45 case Character::Large_Mii_Outfit_A_Female:
46 case Character::Large_Mii_Outfit_B_Male:
47 case Character::Large_Mii_Outfit_B_Female:
48 case Character::Large_Mii_Outfit_C_Male:
49 case Character::Large_Mii_Outfit_C_Female:
50 case Character::Large_Mii:
51 idx = 25;
52 break;
53 default:
54 if (character > Character::Rosalina) {
55 PANIC("Uh oh.");
56 }
57
58 idx = static_cast<s32>(character);
59 break;
60 }
61
62 auto *file = reinterpret_cast<ParamFile<KartParam::Stats> *>(m_driverParam.file);
63 ASSERT(file);
64 return EGG::RamStream(&file->params[idx], sizeof(KartParam::Stats));
65}
66
67EGG::RamStream KartParamFileManager::getVehicleStream(Vehicle vehicle) const {
68 if (vehicle >= Vehicle::Max) {
69 PANIC("Uh oh.");
70 }
71
72 s32 idx = static_cast<s32>(vehicle);
73 auto *file = reinterpret_cast<ParamFile<KartParam::Stats> *>(m_kartParam.file);
74 ASSERT(file);
75 return EGG::RamStream(&file->params[idx], sizeof(KartParam::Stats));
76}
77
78EGG::RamStream KartParamFileManager::getHitboxStream(Vehicle vehicle) const {
79 if (vehicle >= Vehicle::Max) {
80 PANIC("Uh oh.");
81 }
82
83 auto *resourceManager = System::ResourceManager::Instance();
84 size_t size;
85
86 auto *file = resourceManager->getBsp(vehicle, &size);
87 ASSERT(file);
88 ASSERT(size == sizeof(BSP));
89 return EGG::RamStream(file, size);
90}
91
92EGG::RamStream KartParamFileManager::getBikeDispParamsStream(Vehicle vehicle) const {
93 if (vehicle < Vehicle::Standard_Bike_S || vehicle >= Vehicle::Max) {
94 PANIC("Uh oh.");
95 }
96
97 // We need to index at the correct offset
98 constexpr u32 KART_MAX = 18;
99 s32 idx = static_cast<s32>(vehicle) - KART_MAX;
100
101 auto *file = reinterpret_cast<ParamFile<KartParam::BikeDisp> *>(m_bikeDispParam.file);
102 ASSERT(file);
103 return EGG::RamStream(&file->params[idx], sizeof(KartParam::BikeDisp));
104}
105
106KartParamFileManager *KartParamFileManager::CreateInstance() {
107 ASSERT(!s_instance);
108 s_instance = new KartParamFileManager;
109 return s_instance;
110}
111
112void KartParamFileManager::DestroyInstance() {
113 ASSERT(s_instance);
114 auto *instance = s_instance;
115 s_instance = nullptr;
116 delete instance;
117}
118
119KartParamFileManager::KartParamFileManager() {
120 init();
121}
122
123KartParamFileManager::~KartParamFileManager() {
124 if (s_instance) {
125 s_instance = nullptr;
126 WARN("KartParamFileManager instance not explicitly handled!");
127 }
128}
129
132 // Validate kartParam.bin
133 if (!m_kartParam.file || m_kartParam.size == 0) {
134 return false;
135 }
136
137 auto *kartFile = reinterpret_cast<ParamFile<KartParam::Stats> *>(m_kartParam.file);
138 if (m_kartParam.size != parse<u32>(kartFile->count) * sizeof(KartParam::Stats) + 4) {
139 return false;
140 }
141
142 // Validate driverParam.bin
143 if (!m_driverParam.file || m_driverParam.size == 0) {
144 return false;
145 }
146
147 auto *driverFile = reinterpret_cast<ParamFile<KartParam::Stats> *>(m_driverParam.file);
148 if (m_driverParam.size != parse<u32>(driverFile->count) * sizeof(KartParam::Stats) + 4) {
149 return false;
150 }
151
152 // Validate bikePartsDispParam.bin
153 if (!m_bikeDispParam.file || m_bikeDispParam.size == 0) {
154 return false;
155 }
156
157 auto *bikeDispFile = reinterpret_cast<ParamFile<KartParam::BikeDisp> *>(m_bikeDispParam.file);
158 if (m_bikeDispParam.size != parse<u32>(bikeDispFile->count) * sizeof(KartParam::BikeDisp) + 4) {
159 return false;
160 }
161
162 return true;
163}
164
165KartParamFileManager *KartParamFileManager::s_instance = nullptr;
166
167} // namespace Kart
A stream of data stored in memory.
Definition Stream.hh:64
Abstraction for the process of retrieving kart parameters from files.
void init()
Loads and validates the kart parameter files.
bool validate() const
Performs a few checks to make sure the files were loaded successfully.
Pertains to kart-related functionality.
Various character/vehicle-related handling and speed stats.
Definition KartParam.hh:67