Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
utility.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 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 <QByteArray>
23#include <QStringList>
24#include <QQmlContext>
25#include <QQmlEngine>
26
27#include "./utility.h"
28#include "./random.h"
29
30// Meyers singleton: the static local is initialised once, on first call.
31Utility* Utility::inst()
32{
33 static Utility* _inst = new Utility;
34 return _inst;
35}
36
38{
39 return Random::inst();
40}
41
42// Thanks eyllanesc
43// https://stackoverflow.com/questions/45772951/converting-qstring-to-ascii-value-vice-versa-in-qt
44const QString Utility::encodeBeforeUrl(const QString beforeStr) const
45{
46 // Emit each character's Unicode code point as base-16, space separated.
47 QStringList numberString;
48 for(const auto character: beforeStr){
49 numberString << QString::number(character.unicode(), 16);
50 }
51
52 return numberString.join(" ");
53}
54
55// Thanks eyllanesc
56// https://stackoverflow.com/questions/45772951/converting-qstring-to-ascii-value-vice-versa-in-qt
57const QString Utility::decodeAfterUrl(QString beforeStr) const
58{
59 // Inverse of encodeBeforeUrl(): drop the spaces, read the hex back to bytes.
60 return QByteArray::fromHex(beforeStr.remove(" ").toLocal8Bit());
61}
62
63void Utility::qmlProtectUtil(const QObject* const obj, const QQmlEngine* const engine)
64{
65 // For some reason this demands it not be const
66 engine->setObjectOwnership(const_cast<QObject*>(obj), QQmlEngine::CppOwnership);
67}
68
69void Utility::qmlProtect(const QQmlEngine* const engine) const
70{
71 // Protect ourselves, then cascade to the Random we expose so neither is GC'd.
72 qmlProtectUtil(this, engine);
73 Random::inst()->qmlProtect(engine);
74}
75
76void Utility::qmlHook(QQmlContext* const context) const
77{
78 // For some reason this demands it not be const
79 context->setContextProperty("pseCommon", const_cast<Utility*>(this));
80}
81
82void Utility::qmlRegister() const
83{
84 // Idempotent: register the QML type at most once per process.
85 static bool registered = false;
86 if(registered)
87 return;
88
89 qmlRegisterUncreatableType<Utility>("PSE.Common.Utility", 1, 0, "Utility", "Can't instantiate in QML");
90 registered = true;
91}
92
93Utility::Utility()
94{
95 // Register our own QML type, then ensure Random exists (we expose it).
96 qmlRegister();
98}
Project-wide source of randomness, usable from both C++ and QML.
Definition random.h:50
void qmlProtect(const QQmlEngine *const engine) const
Pin this object to C++ ownership so QML's GC never deletes it.
Definition random.cpp:94
static Random * inst()
< Convenience 50% coin flip (integer path), readable from QML.
Definition random.cpp:31
const QString decodeAfterUrl(QString beforeStr) const
Decode the space-separated hex produced by encodeBeforeUrl() back to text.
Definition utility.cpp:57
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
static Utility * inst()
< The shared Random instance, reachable from QML as pseCommon.random.
Definition utility.cpp:31
void qmlProtect(const QQmlEngine *const engine) const
Protect this Utility (and the Random it owns) from QML GC.
Definition utility.cpp:69
Random * random()
Accessor for the shared Random singleton (also backs the random property).
Definition utility.cpp:37
void qmlHook(QQmlContext *const context) const
Install this object into a QML context as the pseCommon property.
Definition utility.cpp:76
const QString encodeBeforeUrl(const QString beforeStr) const
Encode a string into space-separated hex of each character's code point.
Definition utility.cpp:44