1+ /*
2+ * This file is part of Sim2Editor-CPPCore
3+ * Copyright (C) 2020-2021 Sim2Team
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_SAV_UTILS_HPP
28+ #define _SIM2EDITOR_CPP_CORE_SAV_UTILS_HPP
29+
30+ #include " DataHelper.hpp"
31+ #include " ../gba/GBASav.hpp"
32+ #include " ../nds/NDSSav.hpp"
33+
34+
35+ namespace S2Editor {
36+ /*
37+ SavUtils for common things.
38+
39+ Used for SavType Detection and various other common things.
40+ */
41+ namespace SavUtils {
42+ extern SavType Sav; // Active SavType.
43+ extern std::string SavName;
44+
45+ SavType DetectType (const std::string &File);
46+ SavType DetectType (const std::unique_ptr<uint8_t []> &Data, const uint32_t Size);
47+ bool LoadSav (const std::string &File, const std::string &BasePath = " " , const bool DoBackup = false );
48+ bool LoadSav (std::unique_ptr<uint8_t []> &Data, const uint32_t Size, const std::string &BasePath = " " , const bool DoBackup = false );
49+ bool CreateBackup (const std::string &BasePath);
50+ void Finish ();
51+ bool ChangesMade ();
52+ };
53+
54+
55+ /* SavUtils for GBA. */
56+ namespace GBASavUtils {
57+ extern std::unique_ptr<GBASav> Sav;
58+
59+ /*
60+ Read from the SavBuffer.
61+
62+ const uint32_t Offs: The Offset from where to read.
63+ */
64+ template <typename T>
65+ T Read (const uint32_t Offs) {
66+ if (!GBASavUtils::Sav || !GBASavUtils::Sav->GetValid () || !GBASavUtils::Sav->GetData ()) return 0 ;
67+ return DataHelper::Read<T>(GBASavUtils::Sav->GetData (), Offs);
68+ };
69+
70+ /*
71+ Write to the SavBuffer.
72+
73+ const uint32_t Offs: The Offset where to write to.
74+ T Data: The data which to write.
75+ */
76+ template <typename T>
77+ void Write (const uint32_t Offs, T Data) {
78+ if (!GBASavUtils::Sav || !GBASavUtils::Sav->GetValid ()) return ;
79+
80+ if (DataHelper::Write<T>(GBASavUtils::Sav->GetData (), Offs, Data)) {
81+ if (!GBASavUtils::Sav->GetChangesMade ()) GBASavUtils::Sav->SetChangesMade (true );
82+ }
83+ };
84+
85+ /* BIT stuff. */
86+ const bool ReadBit (const uint32_t Offs, const uint8_t BitIndex);
87+ void WriteBit (const uint32_t Offs, const uint8_t BitIndex, const bool IsSet);
88+ const uint8_t ReadBits (const uint32_t Offs, const bool First = true );
89+ void WriteBits (const uint32_t Offs, const bool First = true , const uint8_t Data = 0x0 );
90+
91+ const std::string ReadString (const uint32_t Offs, const uint32_t Length);
92+ void WriteString (const uint32_t Offs, const uint32_t Length, const std::string &Str);
93+ };
94+
95+ /* SavUtils for NDS. */
96+ namespace NDSSavUtils {
97+ extern std::unique_ptr<NDSSav> Sav;
98+
99+ /*
100+ Read from the SavBuffer.
101+
102+ const uint32_t Offs: The Offset from where to read.
103+ */
104+ template <typename T>
105+ T Read (const uint32_t Offs) {
106+ if (!NDSSavUtils::Sav || !NDSSavUtils::Sav->GetValid () || !NDSSavUtils::Sav->GetData ()) return 0 ;
107+ return DataHelper::Read<T>(NDSSavUtils::Sav->GetData (), Offs);
108+ };
109+
110+ /*
111+ Write to the SavBuffer.
112+
113+ const uint32_t Offs: The Offset where to write to.
114+ T Data: The data which to write.
115+ */
116+ template <typename T>
117+ void Write (const uint32_t Offs, T Data) {
118+ if (!NDSSavUtils::Sav || !NDSSavUtils::Sav->GetValid ()) return ;
119+
120+ if (DataHelper::Write<T>(NDSSavUtils::Sav->GetData (), Offs, Data)) {
121+ if (!NDSSavUtils::Sav->GetChangesMade ()) NDSSavUtils::Sav->SetChangesMade (true );
122+ }
123+ };
124+
125+ /* BIT stuff. */
126+ const bool ReadBit (const uint32_t Offs, const uint8_t BitIndex);
127+ void WriteBit (const uint32_t Offs, const uint8_t BitIndex, const bool IsSet);
128+ const uint8_t ReadBits (const uint32_t Offs, const bool First = true );
129+ void WriteBits (const uint32_t Offs, const bool First = true , const uint8_t Data = 0x0 );
130+
131+ const std::string ReadString (const uint32_t Offs, const uint32_t Length);
132+ void WriteString (const uint32_t Offs, const uint32_t Length, const std::string &Str);
133+
134+ NDSSavRegion GetRegion ();
135+ };
136+ };
137+
138+ #endif
0 commit comments