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