Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
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
"
33
#include "
./entries/mapdbentry.h
"
34
35
ScriptDBEntry::ScriptDBEntry
() {}
36
ScriptDBEntry::ScriptDBEntry
(QJsonValue& data)
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
53
void
ScriptDBEntry::deepLink
()
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
67
ScriptsDB*
ScriptsDB::inst
()
68
{
69
static
ScriptsDB* _inst =
new
ScriptsDB;
70
return
_inst;
71
}
72
73
const
QVector<ScriptDBEntry*>
ScriptsDB::getStore
()
const
{
return
store; }
74
const
QHash<QString, ScriptDBEntry*>
ScriptsDB::getInd
()
const
{
return
ind; }
75
int
ScriptsDB::getStoreSize
()
const
{
return
store.size(); }
76
77
ScriptDBEntry
*
ScriptsDB::getStoreAt
(
int
idx)
const
78
{
79
if
(idx < 0 || idx >= store.size())
return
nullptr
;
80
return
store.at(idx);
81
}
82
83
ScriptDBEntry
*
ScriptsDB::getIndAt
(
const
QString& key)
const
84
{
85
return
ind.value(key,
nullptr
);
86
}
87
88
void
ScriptsDB::load
()
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
98
void
ScriptsDB::index
()
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
109
void
ScriptsDB::deepLink
()
110
{
111
static
bool
once =
false
;
112
if
(once)
return
;
113
for
(
auto
* entry : store)
114
entry->deepLink();
115
once =
true
;
116
}
117
118
void
ScriptsDB::qmlProtect
(
const
QQmlEngine*
const
engine)
const
119
{
120
Utility::qmlProtectUtil
(
this
, engine);
121
}
122
123
void
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
131
ScriptsDB::ScriptsDB()
132
{
133
qmlRegister();
134
}
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
MapsDB::inst
static MapsDB * inst()
< Number of maps.
Definition
mapsdb.cpp:35
MapsDB::getIndAt
MapDBEntry * getIndAt(const QString val) const
Map by name key (for QML).
Definition
mapsdb.cpp:155
ScriptsDB::getStoreSize
int getStoreSize() const
Script count.
Definition
scripts.cpp:75
ScriptsDB::deepLink
void deepLink()
Resolve each script's map links.
Definition
scripts.cpp:109
ScriptsDB::getInd
const QHash< QString, ScriptDBEntry * > getInd() const
Name->entry index.
Definition
scripts.cpp:74
ScriptsDB::load
void load()
Load scripts from JSON.
Definition
scripts.cpp:88
ScriptsDB::qmlProtect
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition
scripts.cpp:118
ScriptsDB::inst
static ScriptsDB * inst()
< Number of scripts.
Definition
scripts.cpp:67
ScriptsDB::getIndAt
ScriptDBEntry * getIndAt(const QString &key) const
Script by name key (for QML).
Definition
scripts.cpp:83
ScriptsDB::getStore
const QVector< ScriptDBEntry * > getStore() const
All scripts.
Definition
scripts.cpp:73
ScriptsDB::index
void index()
Build the name->entry index.
Definition
scripts.cpp:98
ScriptsDB::getStoreAt
ScriptDBEntry * getStoreAt(int idx) const
Script by store index (for QML).
Definition
scripts.cpp:77
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
mapdbentry.h
mapsdb.h
scripts.h
ScriptDBEntry
One map-script definition: its id/size and which maps use it.
Definition
scripts.h:40
ScriptDBEntry::size
var8 size
Script size.
Definition
scripts.h:47
ScriptDBEntry::toMaps
QVector< MapDBEntry * > toMaps
Resolved map entries (deepLink).
Definition
scripts.h:52
ScriptDBEntry::deepLink
void deepLink()
Resolve the maps names to entries.
Definition
scripts.cpp:53
ScriptDBEntry::skip
std::optional< var8 > skip
Optional skip value.
Definition
scripts.h:50
ScriptDBEntry::ind
var8 ind
Script index.
Definition
scripts.h:46
ScriptDBEntry::maps
QVector< QString > maps
Map names using this script.
Definition
scripts.h:49
ScriptDBEntry::ScriptDBEntry
ScriptDBEntry()
Empty entry.
Definition
scripts.cpp:35
ScriptDBEntry::name
QString name
Script name (key).
Definition
scripts.h:45
utility.h
projects
db
src
pse-db
scripts.cpp
Generated by
1.17.0