Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
types.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 <QQmlEngine>
24#include <pse-common/utility.h>
25
26#include "./types.h"
27#include "./util/gamedata.h"
28
30TypeDBEntry::TypeDBEntry(QJsonValue& data)
31{
32 name = data["name"].toString();
33 ind = static_cast<var8>(data["ind"].toDouble());
34 readable = data["readable"].toString();
35}
36
37TypesDB* TypesDB::inst()
38{
39 static TypesDB* _inst = new TypesDB;
40 return _inst;
41}
42
43const QVector<TypeDBEntry*> TypesDB::getStore() const { return store; }
44const QHash<QString, TypeDBEntry*> TypesDB::getInd() const { return ind; }
45int TypesDB::getStoreSize() const { return store.size(); }
46
48{
49 if (idx < 0 || idx >= store.size()) return nullptr;
50 return store.at(idx);
51}
52
53TypeDBEntry* TypesDB::getIndAt(const QString& key) const
54{
55 return ind.value(key, nullptr);
56}
57
59{
60 static bool once = false;
61 if (once) return;
62 auto jsonData = GameData::inst()->json("types");
63 for (QJsonValue entry : jsonData.array())
64 store.append(new TypeDBEntry(entry));
65 once = true;
66}
67
69{
70 static bool once = false;
71 if (once) return;
72 for (auto* entry : store) {
73 ind.insert(entry->name, entry);
74 ind.insert(QString::number(entry->ind), entry);
75 ind.insert(entry->readable, entry);
76 }
77 once = true;
78}
79
80void TypesDB::qmlProtect(const QQmlEngine* const engine) const
81{
82 Utility::qmlProtectUtil(this, engine);
83}
84
85void TypesDB::qmlRegister() const
86{
87 static bool once = false;
88 if (once) return;
89 qmlRegisterUncreatableType<TypesDB>("PSE.DB.TypesDB", 1, 0, "TypesDB", "Can't instantiate in QML");
90 once = true;
91}
92
93TypesDB::TypesDB()
94{
95 qmlRegister();
96}
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 types from JSON.
Definition types.cpp:58
TypeDBEntry * getIndAt(const QString &key) const
Type by name key (for QML).
Definition types.cpp:53
int getStoreSize() const
Type count.
Definition types.cpp:45
static TypesDB * inst()
< Number of types.
Definition types.cpp:37
const QVector< TypeDBEntry * > getStore() const
All types.
Definition types.cpp:43
const QHash< QString, TypeDBEntry * > getInd() const
Name->entry index.
Definition types.cpp:44
void index()
Build the name->entry index.
Definition types.cpp:68
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition types.cpp:80
TypeDBEntry * getStoreAt(int idx) const
Type by store index (for QML).
Definition types.cpp:47
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
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition types.h:124
One elemental type: its name plus the moves and Pokemon of that type.
Definition types.h:39
QString readable
Human-readable type name.
Definition types.h:45
var8 ind
Type index/id.
Definition types.h:44
TypeDBEntry()
Empty entry.
Definition types.cpp:29
QString name
Internal type name (key).
Definition types.h:43