Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
mapsdb.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 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
21
22#include <QJsonArray>
23#include <pse-common/utility.h>
24#include <QQmlEngine>
25
26#include "./mapsdb.h"
27#include "./util/gamedata.h"
28#include "./util/mapsearch.h"
30
31#ifdef QT_DEBUG
32#include <QtDebug>
33#endif
34
35MapsDB* MapsDB::inst()
36{
37 static MapsDB* _inst = new MapsDB;
38 return _inst;
39}
40
41const QVector<MapDBEntry*> MapsDB::getStore() const
42{
43 return store;
44}
45
46const QHash<QString, MapDBEntry*> MapsDB::getInd() const
47{
48 return ind;
49}
50
52{
53 return store.size();
54}
55
57{
58 static bool once = false;
59 if(once)
60 return;
61
62 // Grab Map Data
63 auto jsonData = GameData::inst()->json("maps");
64
65 // Go through each map
66 for(QJsonValue jsonEntry : jsonData.array())
67 {
68 // Create a new map entry
69 auto entry = new MapDBEntry(jsonEntry);
70
71 // Add to array
72 store.append(entry);
73 }
74
75 once = true;
76}
77
79{
80 static bool once = false;
81 if(once)
82 return;
83
84 for(auto entry : store)
85 {
86 // Index name and index
87 ind.insert(entry->name, entry);
88 ind.insert(QString::number(entry->ind), entry);
89
90 // Also insert the modern name if present
91 if(entry->modernName != "")
92 ind.insert(entry->modernName, entry);
93 }
94
95 once = true;
96}
97
99{
100 static bool once = false;
101 if(once)
102 return;
103
104 for(auto entry : store)
105 {
106 entry->deepLink();
107 }
108
109 once = true;
110}
111
112void MapsDB::qmlProtect(const QQmlEngine* const engine) const
113{
114 Utility::qmlProtectUtil(this, engine);
115 for(auto el : store)
116 el->qmlProtect(engine);
117}
118
119void MapsDB::qmlRegister() const
120{
121 static bool once = false;
122 if(once)
123 return;
124
125 qmlRegisterUncreatableType<MapsDB>(
126 "PSE.DB.MapsDB", 1, 0, "MapsDB", "Can't instantiate in QML");
127 once = true;
128}
129
130MapsDB::MapsDB()
131{
132 qmlRegister();
133 load();
134}
135
137{
138 return new MapSearch();
139}
140
141QScopedPointer<MapSearch, QScopedPointerDeleteLater> MapsDB::search() const
142{
143 return QScopedPointer<MapSearch, QScopedPointerDeleteLater>(
144 new MapSearch());
145}
146
147MapDBEntry* MapsDB::getStoreAt(const int ind) const
148{
149 if(ind < 0 || ind >= store.size())
150 return nullptr;
151
152 return store.at(ind);
153}
154
155MapDBEntry* MapsDB::getIndAt(const QString val) const
156{
157 return ind.value(val, nullptr);
158}
class SAVEFILE_AUTOPORT MapDBEntry
Definition areageneral.h:52
const QJsonDocument json(const QString filename) const
Parsed document for filename.
Definition gamedata.cpp:45
static GameData * inst()
The process-wide GameData singleton.
Definition gamedata.cpp:71
A chainable filter ("finder") over the maps – the heart of map randomization.
Definition mapsearch.h:42
void deepLink()
Resolve every map's warps/sprites/connections/etc.
Definition mapsdb.cpp:98
static MapsDB * inst()
< Number of maps.
Definition mapsdb.cpp:35
QScopedPointer< MapSearch, QScopedPointerDeleteLater > search() const
C++-owned finder (smart pointer).
Definition mapsdb.cpp:141
MapDBEntry * getIndAt(const QString val) const
Map by name key (for QML).
Definition mapsdb.cpp:155
int getStoreSize() const
Map count.
Definition mapsdb.cpp:51
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition mapsdb.cpp:112
void index()
Build the name->map index.
Definition mapsdb.cpp:78
const QVector< MapDBEntry * > getStore() const
All maps.
Definition mapsdb.cpp:41
MapDBEntry * getStoreAt(const int ind) const
Map by store index (for QML).
Definition mapsdb.cpp:147
const QHash< QString, MapDBEntry * > getInd() const
Name->map index.
Definition mapsdb.cpp:46
MapSearch * searchRaw() const
Raw finder for QML (QML-managed ownership).
Definition mapsdb.cpp:136
void load()
Load maps from JSON.
Definition mapsdb.cpp:56
static void qmlProtectUtil(const QObject *const obj, const QQmlEngine *const engine)
Pin obj to C++ ownership so the QML engine never garbage-collects it.
Definition utility.cpp:63
One map's complete static definition – the root of the MapDBEntry family.
Definition mapdbentry.h:56