Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
savefile.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Twilight
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15*/
16
22#include "savefile.h"
24#include "./savefileiterator.h"
25#include "./savefiletoolset.h"
26
27SaveFile::SaveFile(QObject* parent)
28 : QObject(parent)
29{
30 // One-Time Init Data in order of dependencies
31 // Since the sav file is a primitive array we have to clear it first to zeroes
32 data = new var8[SAV_DATA_SIZE];
33 memset(data, 0, SAV_DATA_SIZE);
34
35 // Create toolset 2nd and then dataExpanded
36 // dataExpanded will perform an initial load from sav data which is why
37 // we had to clear it first up there, dataExpanded also depends on toolset
38 toolset = new SaveFileToolset(this);
40
41 // Perform post-init stuff including notifying other code of data changes
42 resetData();
43}
44
46{
47 // Erase backwards from creation order
48 dataExpanded->deleteLater();
49 delete toolset;
50 delete[] data;
51}
52
57
58void SaveFile::resetData(bool silent)
59{
60 memset(data, 0, SAV_DATA_SIZE);
61 dataChanged(data);
62
63 if(!silent) {
64 expandData();
66 }
67}
68
69void SaveFile::setData(var8* data, bool silent)
70{
71 // Never copy from a null source. setData's callers obtain their buffer from
72 // FileManagement::readSaveData(), which returns nullptr when a file can't be
73 // opened or is too short to be a valid save. Dereferencing that here is a hard
74 // crash (memcpy from address 0). Treat a null buffer as "no change" -- the
75 // current in-memory save is left exactly as it was, and the caller surfaces the
76 // failure to the user. (Belt-and-braces: callers also guard before calling.)
77 if(data == nullptr)
78 return;
79
80 memcpy(this->data, data, SAV_DATA_SIZE);
81 dataChanged(data);
82
83 if(!silent) {
84 expandData();
86 }
87}
88
90{
91 dataExpanded->save(this);
92}
93
95{
96 dataExpanded->load(this);
97}
98
100{
101 dataExpanded->reset();
102}
103
105{
106 dataExpanded->randomize();
107}
Root of the editable object tree – the friendly mirror of a raw save.
A moving cursor over a SaveFile, layering auto-advancing reads/writes on top of SaveFileToolset.
Low-level read/write primitives over a SaveFile's raw 32 KB buffer.
void randomizeExpansion()
Fully randomizes the expansion data, doesn't change save file data.
Definition savefile.cpp:104
SaveFileToolset * toolset
Tools to operate directly on the raw sav file data.
Definition savefile.h:117
void setData(var8 *data, bool silent=false)
Change-out the save file.
Definition savefile.cpp:69
SaveFileIterator * iterator()
Returns a unique iterator that's setup to iterate over the raw sav file data.
Definition savefile.cpp:53
void expandData()
Replace expansion with new expansion of current sav file.
Definition savefile.cpp:94
void flattenData()
Flatten expansion back to the save file, overwriting it's current contents with only data that's stri...
Definition savefile.cpp:89
void resetData(bool silent=false)
Empty this save file to zero's.
Definition savefile.cpp:58
void dataExpandedChanged(SaveFileExpanded *expanded)
SAV file has changed but the old expansion has not been replaced with exxpansion of new data.
void eraseExpansion()
Erase expansion data, this makes expansion data act like a new file but save file contents are preser...
Definition savefile.cpp:99
var8 * data
Actual SAV Data, a raw internal binary copy of the file.
Definition savefile.h:111
SaveFile(QObject *parent=nullptr)
The expanded, editable object tree. QML traverses in from here.
Definition savefile.cpp:27
virtual ~SaveFile()
Definition savefile.cpp:45
SaveFileExpanded * dataExpanded
Expanded SAV data to be readable and more usable.
Definition savefile.h:114
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition types.h:124
constexpr var16 SAV_DATA_SIZE
Size of a Gen 1 save in bytes (32 KB).
Definition savefile.h:29