Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
missablesdb.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 <QJsonValue>
23#include <QJsonArray>
24#include <QQmlEngine>
25#include <pse-common/utility.h>
26#include "./missablesdb.h"
27#include "./util/gamedata.h"
29
30MissablesDB* MissablesDB::inst()
31{
32 static MissablesDB* _inst = new MissablesDB;
33 return _inst;
34}
35
36const QVector<MissableDBEntry*> MissablesDB::getStore() const
37{
38 return store;
39}
40
41const QHash<QString, MissableDBEntry*> MissablesDB::getInd() const
42{
43 return ind;
44}
45
47{
48 return store.size();
49}
50
52{
53 if(ind < 0 || ind >= store.size())
54 return nullptr;
55
56 return store.at(ind);
57}
58
59MissableDBEntry* MissablesDB::getIndAt(const QString val) const
60{
61 return ind.value(val, nullptr);
62}
63
65{
66 static bool once = false;
67 if(once)
68 return;
69
70 // Grab Event Pokemon Data
71 auto jsonData = GameData::inst()->json("missables");
72
73 // Go through each event Pokemon
74 for(QJsonValue jsonEntry : jsonData.array())
75 {
76 // Create a new event Pokemon entry
77 auto entry = new MissableDBEntry(jsonEntry);
78
79 // Add to array
80 store.append(entry);
81 }
82
83 once = true;
84}
85
87{
88 static bool once = false;
89 if(once)
90 return;
91
92 for(auto entry : store)
93 {
94 // Index name and index
95 // Name is actually map + name, the name is the local name to the map
96 // and is often duplicated across maps
97 ind.insert(entry->map + " " + entry->name, entry);
98 ind.insert(QString::number(entry->ind), entry);
99 }
100
101 once = true;
102}
103
105{
106 static bool once = false;
107 if(once)
108 return;
109
110 for(auto entry : store)
111 {
112 entry->deepLink();
113 }
114
115 once = true;
116}
117
118void MissablesDB::qmlProtect(const QQmlEngine* const engine) const
119{
120 Utility::qmlProtectUtil(this, engine);
121 for(auto el : store)
122 el->qmlProtect(engine);
123}
124
125void MissablesDB::qmlRegister() const
126{
127 static bool once = false;
128 if(once)
129 return;
130
131 qmlRegisterUncreatableType<MissablesDB>("PSE.DB.MissablesDB", 1, 0, "MissablesDB", "Can't instantiate in QML");
132 once = true;
133}
134
135MissablesDB::MissablesDB()
136{
137 qmlRegister();
138 load();
139}
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
int getStoreSize() const
Missable count.
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
void index()
Build the name->entry index.
void deepLink()
Resolve each missable's cross-DB links.
const QHash< QString, MissableDBEntry * > getInd() const
Name->entry index.
MissableDBEntry * getIndAt(const QString val) const
Missable by name key (for QML).
const QVector< MissableDBEntry * > getStore() const
All missable definitions.
MissableDBEntry * getStoreAt(const int ind) const
Missable by store index (for QML).
static MissablesDB * inst()
< Number of missable definitions.
void load()
Load missables from JSON.
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 missable definition: a script/sprite that can be hidden or shown.