Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
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
29
TrainerDBEntry::TrainerDBEntry
() {}
30
TrainerDBEntry::TrainerDBEntry
(QJsonValue& data)
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
38
TrainersDB*
TrainersDB::inst
()
39
{
40
static
TrainersDB* _inst =
new
TrainersDB;
41
return
_inst;
42
}
43
44
const
QVector<TrainerDBEntry*>
TrainersDB::getStore
()
const
{
return
store; }
45
const
QHash<QString, TrainerDBEntry*>
TrainersDB::getInd
()
const
{
return
ind; }
46
int
TrainersDB::getStoreSize
()
const
{
return
store.size(); }
47
48
TrainerDBEntry
*
TrainersDB::getStoreAt
(
int
idx)
const
49
{
50
if
(idx < 0 || idx >= store.size())
return
nullptr
;
51
return
store.at(idx);
52
}
53
54
TrainerDBEntry
*
TrainersDB::getIndAt
(
const
QString& key)
const
55
{
56
return
ind.value(key,
nullptr
);
57
}
58
59
void
TrainersDB::load
()
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
69
void
TrainersDB::index
()
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
80
void
TrainersDB::qmlProtect
(
const
QQmlEngine*
const
engine)
const
81
{
82
Utility::qmlProtectUtil
(
this
, engine);
83
}
84
85
void
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
93
TrainersDB::TrainersDB()
94
{
95
qmlRegister();
96
}
GameData::json
const QJsonDocument json(const QString filename) const
Parsed document for filename.
Definition
gamedata.cpp:45
GameData::inst
static GameData * inst()
The process-wide GameData singleton.
Definition
gamedata.cpp:71
TrainersDB::getIndAt
TrainerDBEntry * getIndAt(const QString &key) const
Trainer by name key (for QML).
Definition
trainers.cpp:54
TrainersDB::getStore
const QVector< TrainerDBEntry * > getStore() const
All trainer classes.
Definition
trainers.cpp:44
TrainersDB::index
void index()
Build the name->entry index.
Definition
trainers.cpp:69
TrainersDB::inst
static TrainersDB * inst()
< Number of trainer classes.
Definition
trainers.cpp:38
TrainersDB::getStoreAt
TrainerDBEntry * getStoreAt(int idx) const
Trainer by store index (for QML).
Definition
trainers.cpp:48
TrainersDB::qmlProtect
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition
trainers.cpp:80
TrainersDB::getInd
const QHash< QString, TrainerDBEntry * > getInd() const
Name->entry index.
Definition
trainers.cpp:45
TrainersDB::load
void load()
Load trainers from JSON.
Definition
trainers.cpp:59
TrainersDB::getStoreSize
int getStoreSize() const
Trainer-class count.
Definition
trainers.cpp:46
Utility::qmlProtectUtil
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
var8
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition
types.h:124
gamedata.h
TrainerDBEntry
One trainer-class definition (its name/index and flags).
Definition
trainers.h:38
TrainerDBEntry::TrainerDBEntry
TrainerDBEntry()
Empty entry.
Definition
trainers.cpp:29
TrainerDBEntry::name
QString name
Trainer-class name (key).
Definition
trainers.h:42
TrainerDBEntry::unused
bool unused
Whether this slot is unused.
Definition
trainers.h:44
TrainerDBEntry::ind
var8 ind
Trainer-class index.
Definition
trainers.h:43
TrainerDBEntry::opp
bool opp
Whether it's an opponent class.
Definition
trainers.h:45
trainers.h
utility.h
projects
db
src
pse-db
trainers.cpp
Generated by
1.17.0