Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
trades.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#ifdef QT_DEBUG
27#include <QtDebug>
28#endif
29
30#include "./trades.h"
31#include "./util/gamedata.h"
32#include "./pokemon.h"
33
36{
37 give = data["give"].toString();
38 get = data["get"].toString();
39 textId = static_cast<var8>(data["textId"].toDouble());
40 nickname = data["nickname"].toString();
41 if (data["unused"].isBool()) unused = data["unused"].toBool();
42}
43
45{
48#ifdef QT_DEBUG
49 if (!toGive) qCritical() << "Trade give:" << give << "could not be deep linked.";
50 if (!toGet) qCritical() << "Trade get:" << get << "could not be deep linked.";
51#endif
52 if (toGive) toGive->toTrades.append(this);
53 if (toGet) toGet->toTrades.append(this);
54}
55
56TradesDB* TradesDB::inst()
57{
58 static TradesDB* _inst = new TradesDB;
59 return _inst;
60}
61
62const QVector<TradeDBEntry*> TradesDB::getStore() const { return store; }
63int TradesDB::getStoreSize() const { return store.size(); }
64
66{
67 if (idx < 0 || idx >= store.size()) return nullptr;
68 return store.at(idx);
69}
70
72{
73 static bool once = false;
74 if (once) return;
75 auto jsonData = GameData::inst()->json("trades");
76 for (QJsonValue entry : jsonData.array())
77 store.append(new TradeDBEntry(entry));
78 once = true;
79}
80
82{
83 static bool once = false;
84 if (once) return;
85 for (auto* entry : store)
86 entry->deepLink();
87 once = true;
88}
89
90void TradesDB::qmlProtect(const QQmlEngine* const engine) const
91{
92 Utility::qmlProtectUtil(this, engine);
93}
94
95void TradesDB::qmlRegister() const
96{
97 static bool once = false;
98 if (once) return;
99 qmlRegisterUncreatableType<TradesDB>("PSE.DB.TradesDB", 1, 0, "TradesDB", "Can't instantiate in QML");
100 once = true;
101}
102
103TradesDB::TradesDB()
104{
105 qmlRegister();
106}
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 PokemonDB * inst()
< Number of species.
Definition pokemon.cpp:183
PokemonDBEntry * getIndAt(const QString &key) const
Species by name key (for QML).
Definition pokemon.cpp:199
void deepLink()
Resolve each trade's species links.
Definition trades.cpp:81
static TradesDB * inst()
< Number of trades.
Definition trades.cpp:56
int getStoreSize() const
Trade count.
Definition trades.cpp:63
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition trades.cpp:90
void load()
Load trades from JSON.
Definition trades.cpp:71
TradeDBEntry * getStoreAt(int idx) const
Trade by store index (for QML).
Definition trades.cpp:65
const QVector< TradeDBEntry * > getStore() const
All trades.
Definition trades.cpp:62
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 in-game (NPC) trade definition: what you give and get.
Definition trades.h:37
PokemonDBEntry * toGet
Resolved species you get (deepLink).
Definition trades.h:49
void deepLink()
Resolve the give/get species links.
Definition trades.cpp:44
var8 textId
Trade dialogue text id.
Definition trades.h:44
TradeDBEntry()
Empty entry.
Definition trades.cpp:34
bool unused
Whether this trade slot is unused.
Definition trades.h:46
QString nickname
Nickname the received mon comes with.
Definition trades.h:45
QString give
Species name you give (resolved to toGive).
Definition trades.h:42
QString get
Species name you get (resolved to toGet).
Definition trades.h:43
PokemonDBEntry * toGive
Resolved species you give (deepLink).
Definition trades.h:48