Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
gamedata.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#include "gamedata.h"
22#include "../db.h"
23
24#include <QFile>
25#include <QByteArray>
26#include <QQmlEngine>
27#include <pse-common/utility.h>
28
29const QByteArray GameData::jsonRaw(const QString filename) const
30{
31 // Prepare variables
32 QByteArray val;
33 QFile file;
34
35 // Read in file
36 file.setFileName(":/assets/data/" + filename + ".json");
37 if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
38 return val; // empty on failure
39 val = file.readAll();
40 file.close();
41
42 return val;
43}
44
45const QJsonDocument GameData::json(const QString filename) const
46{
47 // Convert to JSON Document
48 return QJsonDocument(QJsonDocument::fromJson(jsonRaw(filename)));
49}
50
51const QString GameData::jsonStr(const QString filename) const
52{
53 return jsonRaw(filename);
54}
55
56void GameData::qmlProtect(const QQmlEngine* const engine) const
57{
58 Utility::qmlProtectUtil(this, engine);
59}
60
61GameData::GameData()
62{
63 qmlRegister();
64}
65
66void GameData::qmlRegister() const
67{
68 qmlRegisterUncreatableType<GameData>("PSE.DB.GameData", 1, 0, "GameData", "Can't instantiate in QML");
69}
70
71GameData* GameData::inst()
72{
73 static GameData* _inst = new GameData;
74 return _inst;
75}
76
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
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition gamedata.cpp:56
const QString jsonStr(const QString filename) const
filename as a string (for QML).
Definition gamedata.cpp:51
const QByteArray jsonRaw(const QString filename) const
Raw bytes of filename (no .json extension).
Definition gamedata.cpp:29
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