Skip to content

Commit 35eb9a0

Browse files
See desc for more.
- Add `DataHelper` namespace for all the Bit, Byte, String stuff. (Would be useful for later:tm:).
1 parent 30f229e commit 35eb9a0

9 files changed

Lines changed: 298 additions & 143 deletions

File tree

include/gba/GBAHouseItem.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
#include "CoreCommon.hpp"
3131

32-
33-
3432
/*
3533
NOTE:
3634
Items from your Inventory and the ones from your House have different structures, hence it's a separate class.

include/shared/CoreCommon.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#ifndef _SIM2EDITOR_CPP_CORE_COMMON_HPP
2828
#define _SIM2EDITOR_CPP_CORE_COMMON_HPP
2929

30+
#include <cstring>
3031
#include <math.h>
3132
#include <memory>
3233
#include <string>

include/shared/DataHelper.hpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* This file is part of Sim2Editor-CPPCore
3+
* Copyright (C) 2020-2021 SuperSaiyajinStackZ, Universal-Team
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
19+
* * Requiring preservation of specified reasonable legal notices or
20+
* author attributions in that material or in the Appropriate Legal
21+
* Notices displayed by works containing it.
22+
* * Prohibiting misrepresentation of the origin of that material,
23+
* or requiring that modified versions of such material be marked in
24+
* reasonable ways as different from the original version.
25+
*/
26+
27+
#ifndef _SIM2EDITOR_CPP_CORE_DATA_HELPER_HPP
28+
#define _SIM2EDITOR_CPP_CORE_DATA_HELPER_HPP
29+
30+
#include "CoreCommon.hpp"
31+
32+
namespace S2Editor {
33+
namespace DataHelper {
34+
/*
35+
Read from a Buffer.
36+
37+
const uint8_t *Buffer: The Buffer.
38+
const uint32_t Offs: The Offset from where to read.
39+
*/
40+
template <typename T>
41+
T Read(const uint8_t *Buffer, const uint32_t Offs) {
42+
if (!Buffer) return 0; // Return 0, if nullptr.
43+
44+
T Res = 0;
45+
memcpy(&Res, Buffer + Offs, sizeof(T));
46+
return Res;
47+
};
48+
49+
/*
50+
Write to a Buffer.
51+
52+
uint8_t *Buffer: The Buffer.
53+
const uint32_t Offs: The Offset where to write to.
54+
T Data: The data which to write.
55+
56+
Returns true if success, false if not.
57+
*/
58+
template <typename T>
59+
bool Write(uint8_t *Buffer, const uint32_t Offs, T Data) {
60+
if (!Buffer) return false;
61+
62+
for (size_t Idx = 0; Idx < sizeof(T); Idx++) {
63+
Buffer[Offs + Idx] = (uint8_t)Data;
64+
Data >>= 8; // Go to next byte.
65+
}
66+
67+
return true;
68+
};
69+
70+
/* BIT stuff. */
71+
const bool ReadBit(const uint8_t *Buffer, const uint32_t Offs, const uint8_t BitIndex);
72+
bool WriteBit(uint8_t *Buffer, const uint32_t Offs, const uint8_t BitIndex, const bool IsSet);
73+
const uint8_t ReadBits(const uint8_t *Buffer, const uint32_t Offs, const bool First = true);
74+
bool WriteBits(uint8_t *Buffer, const uint32_t Offs, const bool First = true, const uint8_t Data = 0x0);
75+
76+
/* String stuff. */
77+
const std::string ReadString(const uint8_t *Buffer, const uint32_t Offs, const uint32_t Length);
78+
bool WriteString(uint8_t *Buffer, const uint32_t Offs, const uint32_t Length, const std::string &Str);
79+
};
80+
};
81+
82+
#endif

include/shared/SAVUtils.hpp

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
#ifndef _SIM2EDITOR_CPP_CORE_SAV_UTILS_HPP
2828
#define _SIM2EDITOR_CPP_CORE_SAV_UTILS_HPP
2929

30+
#include "DataHelper.hpp"
3031
#include "../gba/GBASav.hpp"
3132
#include "../nds/NDSSav.hpp"
32-
#include <cstring>
3333

3434
namespace S2Editor {
3535
/*
@@ -46,15 +46,6 @@ namespace S2Editor {
4646
bool CreateBackup(const std::string &BasePath);
4747
void Finish();
4848
bool ChangesMade();
49-
50-
const std::string ReadString(const uint8_t *Buffer, const uint32_t Offset, const uint32_t Length);
51-
void WriteString(uint8_t *Buffer, const uint32_t Offset, const uint32_t Length, const std::string &Str);
52-
53-
/* BIT stuff. */
54-
const bool ReadBit(const uint8_t *Buffer, const uint32_t Offs, const uint8_t BitIndex);
55-
void WriteBit(uint8_t *Buffer, const uint32_t Offs, const uint8_t BitIndex, const bool IsSet);
56-
const uint8_t ReadBits(const uint8_t *Buffer, const uint32_t Offs, const bool First);
57-
void WriteBits(uint8_t *Buffer, const uint32_t Offs, const bool First, const uint8_t Data);
5849
};
5950

6051

@@ -69,11 +60,8 @@ namespace S2Editor {
6960
*/
7061
template <typename T>
7162
T Read(const uint32_t Offs) {
72-
if (!GBASAVUtils::SAV || !GBASAVUtils::SAV->GetValid()) return 0; // Return 0, if nullptr OR invalid.
73-
74-
T Res = 0;
75-
memcpy(&Res, GBASAVUtils::SAV->GetData() + Offs, sizeof(T));
76-
return Res;
63+
if (!GBASAVUtils::SAV || !GBASAVUtils::SAV->GetData()) return 0;
64+
return DataHelper::Read<T>(GBASAVUtils::SAV->GetData(), Offs);
7765
};
7866

7967
/*
@@ -86,19 +74,19 @@ namespace S2Editor {
8674
void Write(const uint32_t Offs, T Data) {
8775
if (!GBASAVUtils::SAV || !GBASAVUtils::SAV->GetValid()) return;
8876

89-
for (size_t Idx = 0; Idx < sizeof(T); Idx++) {
90-
GBASAVUtils::SAV->GetData()[Offs + Idx] = (uint8_t)Data;
91-
Data >>= 8; // Go to next byte.
77+
if (DataHelper::Write<T>(GBASAVUtils::SAV->GetData(), Offs, Data)) {
78+
if (!GBASAVUtils::SAV->GetChangesMade()) GBASAVUtils::SAV->SetChangesMade(true);
9279
}
93-
94-
if (!GBASAVUtils::SAV->GetChangesMade()) GBASAVUtils::SAV->SetChangesMade(true);
9580
};
9681

9782
/* BIT stuff. */
9883
const bool ReadBit(const uint32_t Offs, const uint8_t BitIndex);
9984
void WriteBit(const uint32_t Offs, const uint8_t BitIndex, const bool IsSet);
10085
const uint8_t ReadBits(const uint32_t Offs, const bool First = true);
10186
void WriteBits(const uint32_t Offs, const bool First = true, const uint8_t Data = 0x0);
87+
88+
const std::string ReadString(const uint32_t Offs, const uint32_t Length);
89+
void WriteString(const uint32_t Offs, const uint32_t Length, const std::string &Str);
10290
};
10391

10492
/* SAVUtils for NDS. */
@@ -112,11 +100,8 @@ namespace S2Editor {
112100
*/
113101
template <typename T>
114102
T Read(const uint32_t Offs) {
115-
if (!NDSSAVUtils::SAV || !NDSSAVUtils::SAV->GetValid()) return 0; // Return 0, if nullptr OR invalid.
116-
117-
T Res = 0;
118-
memcpy(&Res, NDSSAVUtils::SAV->GetData() + Offs, sizeof(T));
119-
return Res;
103+
if (!NDSSAVUtils::SAV || !NDSSAVUtils::SAV->GetData()) return 0;
104+
return DataHelper::Read<T>(NDSSAVUtils::SAV->GetData(), Offs);
120105
};
121106

122107
/*
@@ -129,19 +114,19 @@ namespace S2Editor {
129114
void Write(const uint32_t Offs, T Data) {
130115
if (!NDSSAVUtils::SAV || !NDSSAVUtils::SAV->GetValid()) return;
131116

132-
for (size_t Idx = 0; Idx < sizeof(T); Idx++) {
133-
NDSSAVUtils::SAV->GetData()[Offs + Idx] = (uint8_t)Data;
134-
Data >>= 8; // Go to next byte.
117+
if (DataHelper::Write<T>(NDSSAVUtils::SAV->GetData(), Offs, Data)) {
118+
if (!NDSSAVUtils::SAV->GetChangesMade()) NDSSAVUtils::SAV->SetChangesMade(true);
135119
}
136-
137-
if (!NDSSAVUtils::SAV->GetChangesMade()) NDSSAVUtils::SAV->SetChangesMade(true);
138120
};
139121

140122
/* BIT stuff. */
141123
const bool ReadBit(const uint32_t Offs, const uint8_t BitIndex);
142124
void WriteBit(const uint32_t Offs, const uint8_t BitIndex, const bool IsSet);
143125
const uint8_t ReadBits(const uint32_t Offs, const bool First = true);
144126
void WriteBits(const uint32_t Offs, const bool First = true, const uint8_t Data = 0x0);
127+
128+
const std::string ReadString(const uint32_t Offs, const uint32_t Length);
129+
void WriteString(const uint32_t Offs, const uint32_t Length, const std::string &Str);
145130
};
146131
};
147132

source/gba/GBAHouseItem.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
#include "GBAHouseItem.hpp"
2828
#include "../shared/SAVUtils.hpp"
29-
#include <cstring>
3029

3130
namespace S2Editor {
3231
/* Get and Set the Item Count. */

source/gba/GBASlot.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace S2Editor {
3636
const uint32_t DefaultOffs: The Default Offset, for things without an Item in your house.
3737
*/
3838
uint32_t GBASlot::Offset(const uint32_t DefaultOffs) const {
39-
return this->Offs + DefaultOffs + (GBASAVUtils::Read<uint8_t>(this->Offs + 0xD6) * 0x6);
39+
return (this->Offs + DefaultOffs) + (GBASAVUtils::Read<uint8_t>(this->Offs + 0xD6) * 0x6);
4040
};
4141

4242

@@ -53,8 +53,8 @@ namespace S2Editor {
5353
void GBASlot::Ratings(const uint16_t V) { GBASAVUtils::Write<uint16_t>(this->Offs + 0xA, std::min<uint16_t>(9999, V)); };
5454

5555
/* Get and Set Name. */
56-
std::string GBASlot::Name() const { return SAVUtils::ReadString(GBASAVUtils::SAV->GetData(), this->Offs + 0xD, 0x8); };
57-
void GBASlot::Name(const std::string &V) { SAVUtils::WriteString(GBASAVUtils::SAV->GetData(), this->Offs + 0xD, 0x8, V); };
56+
std::string GBASlot::Name() const { return GBASAVUtils::ReadString(this->Offs + 0xD, 0x8); };
57+
void GBASlot::Name(const std::string &V) { GBASAVUtils::WriteString(this->Offs + 0xD, 0x8, V); };
5858

5959
/* Get and Set Hairstyle. */
6060
uint8_t GBASlot::Hairstyle() const { return GBASAVUtils::ReadBits(this->Offs + 0x1D, false) / 2; };

source/nds/NDSSlot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ namespace S2Editor {
3434
void NDSSlot::Simoleons(uint32_t V) { NDSSAVUtils::Write<uint32_t>(this->Offs + 0x2C, (std::min<uint32_t>(999999, V))); };
3535

3636
/* Get and Set Name. */
37-
std::string NDSSlot::Name() const { return SAVUtils::ReadString(NDSSAVUtils::SAV->GetData(), this->Offs + 0x30, 0x7); };
38-
void NDSSlot::Name(const std::string &V) { SAVUtils::WriteString(NDSSAVUtils::SAV->GetData(), this->Offs + 0x30, 0x7, V); };
37+
std::string NDSSlot::Name() const { return NDSSAVUtils::ReadString(this->Offs + 0x30, 0x7); };
38+
void NDSSlot::Name(const std::string &V) { NDSSAVUtils::WriteString(this->Offs + 0x30, 0x7, V); };
3939

4040
/* Get and Set Nuclear Fuelrods. */
4141
uint8_t NDSSlot::Fuelrods() const { return NDSSAVUtils::Read<uint8_t>(this->Offs + 0xBC); };

source/shared/DataHelper.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* This file is part of Sim2Editor-CPPCore
3+
* Copyright (C) 2020-2021 SuperSaiyajinStackZ, Universal-Team
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
19+
* * Requiring preservation of specified reasonable legal notices or
20+
* author attributions in that material or in the Appropriate Legal
21+
* Notices displayed by works containing it.
22+
* * Prohibiting misrepresentation of the origin of that material,
23+
* or requiring that modified versions of such material be marked in
24+
* reasonable ways as different from the original version.
25+
*/
26+
27+
#include "DataHelper.hpp"
28+
29+
namespace S2Editor {
30+
/*
31+
Return a bit from a Buffer.
32+
33+
const uint8_t *Buffer: The Buffer.
34+
const uint32_t Offs: The Offset to read from.
35+
const uint8_t BitIndex: The Bit index ( 0 - 7 ).
36+
*/
37+
const bool DataHelper::ReadBit(const uint8_t *Buffer, const uint32_t Offs, const uint8_t BitIndex) {
38+
if (!Buffer || BitIndex > 7) return false;
39+
40+
return (Buffer[Offs] >> BitIndex & 1) != 0;
41+
};
42+
/*
43+
Set a bit to a Buffer.
44+
45+
uint8_t *Buffer: The Buffer.
46+
const uint32_t Offs: The Offset to write to.
47+
const uint8_t BitIndex: The Bit index ( 0 - 7 ).
48+
const bool IsSet: If it's set (1) or not (0).
49+
50+
Returns true if success, false if not.
51+
*/
52+
bool DataHelper::WriteBit(uint8_t *Buffer, const uint32_t Offs, const uint8_t BitIndex, const bool IsSet) {
53+
if (!Buffer || BitIndex > 7) return false;
54+
55+
Buffer[Offs] &= ~(1 << BitIndex);
56+
Buffer[Offs] |= (IsSet ? 1 : 0) << BitIndex;
57+
return true;
58+
};
59+
60+
/*
61+
Read Lower / Upper Bits.
62+
63+
const uint8_t *Buffer: The Buffer.
64+
const uint32_t Offs: The offset where to read from.
65+
const bool First: If Reading from the first four bits, or second.
66+
*/
67+
const uint8_t DataHelper::ReadBits(const uint8_t *Buffer, const uint32_t Offs, const bool First) {
68+
if (!Buffer) return 0x0;
69+
70+
if (First) return (Buffer[Offs] & 0xF); // Bit 0 - 3.
71+
else return (Buffer[Offs] >> 4); // Bit 4 - 7.
72+
};
73+
/*
74+
Write Lower / Upper Bits.
75+
76+
uint8_t *Buffer: The Buffer.
77+
const uint32_t Offs: The offset where to write to.
78+
const bool First: If Writing on the first four bits, or second.
79+
const uint8_t Data: The Data to write.
80+
81+
Returns true if success, false if not.
82+
*/
83+
bool DataHelper::WriteBits(uint8_t *Buffer, const uint32_t Offs, const bool First, const uint8_t Data) {
84+
if (Data > 0xF || !Buffer) return false;
85+
86+
if (First) Buffer[Offs] = (Buffer[Offs] & 0xF0) | (Data & 0xF); // Bit 0 - 3.
87+
else Buffer[Offs] = (Buffer[Offs] & 0x0F) | (Data << 4);// Bit 4 - 7.
88+
return true;
89+
};
90+
91+
/*
92+
Read a string from a Buffer.
93+
94+
const uint8_t *Buffer: The SAVBuffer.
95+
const uint32_t Offs: The Offset from where to read from.
96+
const uint32_t Length: The Length to read.
97+
*/
98+
const std::string DataHelper::ReadString(const uint8_t *Buffer, const uint32_t Offs, const uint32_t Length) {
99+
if (!Buffer) return "";
100+
101+
std::string Str;
102+
for (int Idx = 0; Idx < (int)Length; Idx++) {
103+
if (Buffer[Offs + Idx] == 0x0) break; // 0x0 -> End.
104+
105+
Str += Buffer[Offs + Idx];
106+
}
107+
108+
return Str;
109+
};
110+
/*
111+
Write a string to a Buffer.
112+
113+
uint8_t *Buffer: The SAVBuffer.
114+
const uint32_t Offs: The offset from where to write to.
115+
const uint32_t Length: The length to write.
116+
const std::string &Str: The string to write.
117+
118+
Returns true if success, false if not.
119+
*/
120+
bool DataHelper::WriteString(uint8_t *Buffer, const uint32_t Offs, const uint32_t Length, const std::string &Str) {
121+
if (!Buffer) return false;
122+
123+
for (int Idx = 0; Idx < (int)Length; Idx++) {
124+
if (Idx < (int)Str.size()) Buffer[Offs + Idx] = Str[Idx]; // The index is inside the string length, so write that.
125+
else Buffer[Offs + Idx] = 0; // Index outside the string length.. so write 0.
126+
}
127+
128+
return true;
129+
};
130+
};

0 commit comments

Comments
 (0)