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