Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
music.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 "./music.h"
27#include "./util/gamedata.h"
28
31{
32 name = data["name"].toString();
33 bank = static_cast<var8>(data["bank"].toDouble());
34 id = static_cast<var8>(data["id"].toDouble());
35}
36
37MusicDB* MusicDB::inst()
38{
39 static MusicDB* _inst = new MusicDB;
40 return _inst;
41}
42
43const QVector<MusicDBEntry*> MusicDB::getStore() const { return store; }
44const QHash<QString, MusicDBEntry*> MusicDB::getInd() const { return ind; }
45int MusicDB::getStoreSize() const { return store.size(); }
46
48{
49 if (idx < 0 || idx >= store.size()) return nullptr;
50 return store.at(idx);
51}
52
53MusicDBEntry* MusicDB::getIndAt(const QString& key) const
54{
55 return ind.value(key, nullptr);
56}
57
59{
60 static bool once = false;
61 if (once) return;
62 auto jsonData = GameData::inst()->json("music");
63 for (QJsonValue entry : jsonData.array())
64 store.append(new MusicDBEntry(entry));
65 once = true;
66}
67
69{
70 static bool once = false;
71 if (once) return;
72 for (auto* entry : store) {
73 ind.insert(entry->name, entry);
74 ind.insert(QString::number(entry->bank) + "_" + QString::number(entry->id), entry);
75 }
76 once = true;
77}
78
79void MusicDB::qmlProtect(const QQmlEngine* const engine) const
80{
81 Utility::qmlProtectUtil(this, engine);
82}
83
84void MusicDB::qmlRegister() const
85{
86 static bool once = false;
87 if (once) return;
88 qmlRegisterUncreatableType<MusicDB>("PSE.DB.MusicDB", 1, 0, "MusicDB", "Can't instantiate in QML");
89 once = true;
90}
91
92MusicDB::MusicDB()
93{
94 qmlRegister();
95}
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
const QVector< MusicDBEntry * > getStore() const
All tracks.
Definition music.cpp:43
MusicDBEntry * getStoreAt(int idx) const
Track by store index (for QML).
Definition music.cpp:47
const QHash< QString, MusicDBEntry * > getInd() const
Name->entry index.
Definition music.cpp:44
void load()
Load tracks from JSON.
Definition music.cpp:58
int getStoreSize() const
Track count.
Definition music.cpp:45
void index()
Build the name->entry index.
Definition music.cpp:68
MusicDBEntry * getIndAt(const QString &key) const
Track by name key (for QML).
Definition music.cpp:53
static MusicDB * inst()
< Number of tracks.
Definition music.cpp:37
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition music.cpp:79
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 music track: its name and bank/id, plus the maps that use it.
Definition music.h:38
var8 bank
Audio bank.
Definition music.h:43
MusicDBEntry()
Empty entry.
Definition music.cpp:29
QString name
Track name (key).
Definition music.h:42