Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
trainers.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 "./trainers.h"
27#include "./util/gamedata.h"
28
31{
32 name = data["name"].toString();
33 ind = static_cast<var8>(data["ind"].toDouble());
34 if (data["unused"].isBool()) unused = data["unused"].toBool();
35 if (data["opp"].isBool()) opp = data["opp"].toBool();
36}
37
38TrainersDB* TrainersDB::inst()
39{
40 static TrainersDB* _inst = new TrainersDB;
41 return _inst;
42}
43
44const QVector<TrainerDBEntry*> TrainersDB::getStore() const { return store; }
45const QHash<QString, TrainerDBEntry*> TrainersDB::getInd() const { return ind; }
46int TrainersDB::getStoreSize() const { return store.size(); }
47
49{
50 if (idx < 0 || idx >= store.size()) return nullptr;
51 return store.at(idx);
52}
53
54TrainerDBEntry* TrainersDB::getIndAt(const QString& key) const
55{
56 return ind.value(key, nullptr);
57}
58
60{
61 static bool once = false;
62 if (once) return;
63 auto jsonData = GameData::inst()->json("trainers");
64 for (QJsonValue entry : jsonData.array())
65 store.append(new TrainerDBEntry(entry));
66 once = true;
67}
68
70{
71 static bool once = false;
72 if (once) return;
73 for (auto* entry : store) {
74 ind.insert(entry->opp ? "Opp" + entry->name : entry->name, entry);
75 ind.insert(QString::number(entry->ind), entry);
76 }
77 once = true;
78}
79
80void TrainersDB::qmlProtect(const QQmlEngine* const engine) const
81{
82 Utility::qmlProtectUtil(this, engine);
83}
84
85void TrainersDB::qmlRegister() const
86{
87 static bool once = false;
88 if (once) return;
89 qmlRegisterUncreatableType<TrainersDB>("PSE.DB.TrainersDB", 1, 0, "TrainersDB", "Can't instantiate in QML");
90 once = true;
91}
92
93TrainersDB::TrainersDB()
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
TrainerDBEntry * getIndAt(const QString &key) const
Trainer by name key (for QML).
Definition trainers.cpp:54
const QVector< TrainerDBEntry * > getStore() const
All trainer classes.
Definition trainers.cpp:44
void index()
Build the name->entry index.
Definition trainers.cpp:69
static TrainersDB * inst()
< Number of trainer classes.
Definition trainers.cpp:38
TrainerDBEntry * getStoreAt(int idx) const
Trainer by store index (for QML).
Definition trainers.cpp:48
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition trainers.cpp:80
const QHash< QString, TrainerDBEntry * > getInd() const
Name->entry index.
Definition trainers.cpp:45
void load()
Load trainers from JSON.
Definition trainers.cpp:59
int getStoreSize() const
Trainer-class count.
Definition trainers.cpp:46
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 trainer-class definition (its name/index and flags).
Definition trainers.h:38
TrainerDBEntry()
Empty entry.
Definition trainers.cpp:29
QString name
Trainer-class name (key).
Definition trainers.h:42
bool unused
Whether this slot is unused.
Definition trainers.h:44
var8 ind
Trainer-class index.
Definition trainers.h:43
bool opp
Whether it's an opponent class.
Definition trainers.h:45