Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
scripts.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 "./scripts.h"
31#include "./util/gamedata.h"
32#include "./mapsdb.h"
34
37{
38 name = data["name"].toString();
39 ind = static_cast<var8>(data["ind"].toDouble());
40 size = static_cast<var8>(data["size"].toDouble());
41
42 if (data["skip"].isDouble())
43 skip = static_cast<var8>(data["skip"].toDouble());
44
45 if (data["maps"].isArray()) {
46 for (const auto& mapEntry : data["maps"].toArray())
47 maps.append(mapEntry.toString());
48 } else {
49 maps.append(name);
50 }
51}
52
54{
55 for (const auto& mapName : maps) {
56 auto* map = MapsDB::inst()->getIndAt(mapName);
57 toMaps.append(map);
58#ifdef QT_DEBUG
59 if (!map)
60 qCritical() << "Script Entry:" << name << "could not deep-link to map" << mapName;
61#endif
62 if (map)
63 map->toScript = this;
64 }
65}
66
67ScriptsDB* ScriptsDB::inst()
68{
69 static ScriptsDB* _inst = new ScriptsDB;
70 return _inst;
71}
72
73const QVector<ScriptDBEntry*> ScriptsDB::getStore() const { return store; }
74const QHash<QString, ScriptDBEntry*> ScriptsDB::getInd() const { return ind; }
75int ScriptsDB::getStoreSize() const { return store.size(); }
76
78{
79 if (idx < 0 || idx >= store.size()) return nullptr;
80 return store.at(idx);
81}
82
83ScriptDBEntry* ScriptsDB::getIndAt(const QString& key) const
84{
85 return ind.value(key, nullptr);
86}
87
89{
90 static bool once = false;
91 if (once) return;
92 auto jsonData = GameData::inst()->json("scripts");
93 for (QJsonValue entry : jsonData.array())
94 store.append(new ScriptDBEntry(entry));
95 once = true;
96}
97
99{
100 static bool once = false;
101 if (once) return;
102 for (auto* entry : store) {
103 ind.insert(entry->name, entry);
104 ind.insert(QString::number(entry->ind), entry);
105 }
106 once = true;
107}
108
110{
111 static bool once = false;
112 if (once) return;
113 for (auto* entry : store)
114 entry->deepLink();
115 once = true;
116}
117
118void ScriptsDB::qmlProtect(const QQmlEngine* const engine) const
119{
120 Utility::qmlProtectUtil(this, engine);
121}
122
123void ScriptsDB::qmlRegister() const
124{
125 static bool once = false;
126 if (once) return;
127 qmlRegisterUncreatableType<ScriptsDB>("PSE.DB.ScriptsDB", 1, 0, "ScriptsDB", "Can't instantiate in QML");
128 once = true;
129}
130
131ScriptsDB::ScriptsDB()
132{
133 qmlRegister();
134}
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 MapsDB * inst()
< Number of maps.
Definition mapsdb.cpp:35
MapDBEntry * getIndAt(const QString val) const
Map by name key (for QML).
Definition mapsdb.cpp:155
int getStoreSize() const
Script count.
Definition scripts.cpp:75
void deepLink()
Resolve each script's map links.
Definition scripts.cpp:109
const QHash< QString, ScriptDBEntry * > getInd() const
Name->entry index.
Definition scripts.cpp:74
void load()
Load scripts from JSON.
Definition scripts.cpp:88
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition scripts.cpp:118
static ScriptsDB * inst()
< Number of scripts.
Definition scripts.cpp:67
ScriptDBEntry * getIndAt(const QString &key) const
Script by name key (for QML).
Definition scripts.cpp:83
const QVector< ScriptDBEntry * > getStore() const
All scripts.
Definition scripts.cpp:73
void index()
Build the name->entry index.
Definition scripts.cpp:98
ScriptDBEntry * getStoreAt(int idx) const
Script by store index (for QML).
Definition scripts.cpp:77
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 map-script definition: its id/size and which maps use it.
Definition scripts.h:40
var8 size
Script size.
Definition scripts.h:47
QVector< MapDBEntry * > toMaps
Resolved map entries (deepLink).
Definition scripts.h:52
void deepLink()
Resolve the maps names to entries.
Definition scripts.cpp:53
std::optional< var8 > skip
Optional skip value.
Definition scripts.h:50
var8 ind
Script index.
Definition scripts.h:46
QVector< QString > maps
Map names using this script.
Definition scripts.h:49
ScriptDBEntry()
Empty entry.
Definition scripts.cpp:35
QString name
Script name (key).
Definition scripts.h:45