Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
flydb.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 <QVector>
23#include <QJsonArray>
24#include <QQmlEngine>
25#include <pse-common/utility.h>
26
27#ifdef QT_DEBUG
28#include <QtDebug>
29#endif
30
31#include "./flydb.h"
32#include "./util/gamedata.h"
33#include "./mapsdb.h"
35
37{
38 static FlyDB* _inst = new FlyDB;
39 return _inst;
40}
41
42const QVector<FlyDBEntry*> FlyDB::getStore() const
43{
44 return store;
45}
46
47const QHash<QString, FlyDBEntry*> FlyDB::getInd() const
48{
49 return ind;
50}
51
53{
54 return store.size();
55}
56
58{
59 if(ind < 0 || ind >= store.size())
60 return nullptr;
61
62 return store.at(ind);
63}
64
65FlyDBEntry* FlyDB::getIndAt(const QString val) const
66{
67 return ind.value(val, nullptr);
68}
69
71{
72 static bool once = false;
73 if(once)
74 return;
75
76 // Grab Event Pokemon Data
77 auto jsonData = GameData::inst()->json("fly");
78
79 // Go through each event Pokemon
80 for(QJsonValue jsonEntry : jsonData.array())
81 {
82 // Create a new event Pokemon entry
83 auto entry = new FlyDBEntry(jsonEntry);
84
85 // Add to array
86 store.append(entry);
87 }
88
89 once = true;
90}
91
93{
94 static bool once = false;
95 if(once)
96 return;
97
98 for(auto entry : store)
99 {
100 // Index name and index
101 ind.insert(entry->name, entry);
102 ind.insert(QString::number(entry->ind), entry);
103 }
104
105 once = true;
106}
107
109{
110 static bool once = false;
111 if(once)
112 return;
113
114 for(auto entry : store)
115 {
116 entry->deepLink();
117 }
118
119 once = true;
120}
121
122void FlyDB::qmlProtect(const QQmlEngine* const engine) const
123{
124 Utility::qmlProtectUtil(this, engine);
125 for(auto el : store)
126 el->qmlProtect(engine);
127}
128
129void FlyDB::qmlRegister() const
130{
131 static bool once = false;
132 if(once)
133 return;
134
135 qmlRegisterUncreatableType<FlyDB>("PSE.DB.FlyDB", 1, 0, "FlyDB", "Can't instantiate in QML");
136 once = true;
137}
138
140{
141 qmlRegister();
142 load();
143}
FlyDBEntry * getIndAt(const QString val) const
Destination by name key (for QML).
Definition flydb.cpp:65
void deepLink()
Resolve each destination's map link.
Definition flydb.cpp:108
void index()
Build the name->entry index.
Definition flydb.cpp:92
static FlyDB * inst()
< Number of fly destinations.
Definition flydb.cpp:36
FlyDB()
(Public here, but obtain the singleton via inst().)
Definition flydb.cpp:139
int getStoreSize() const
Destination count.
Definition flydb.cpp:52
QVector< FlyDBEntry * > store
The loaded destinations.
Definition flydb.h:67
FlyDBEntry * getStoreAt(const int ind) const
Destination by store index (for QML).
Definition flydb.cpp:57
void load()
Load destinations from JSON.
Definition flydb.cpp:70
QHash< QString, FlyDBEntry * > ind
Name->entry lookup.
Definition flydb.h:68
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition flydb.cpp:122
const QHash< QString, FlyDBEntry * > getInd() const
Name->entry index.
Definition flydb.cpp:47
const QVector< FlyDBEntry * > getStore() const
All fly destinations.
Definition flydb.cpp:42
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
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 fly destination: its name/index and the map it flies to.
Definition flydbentry.h:37