Skip to content

Commit

Permalink
Allow loading Qt resources from a file on macOS.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Oct 27, 2021
1 parent e7c6100 commit 60ec297
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions base/base_file_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "base/platform/base_platform_file_utilities.h"

#include <QtCore/QResource>

namespace base {

QString FileNameFromUserString(QString name) {
Expand Down Expand Up @@ -35,4 +37,13 @@ QString FileNameFromUserString(QString name) {
return Platform::FileNameFromUserString(std::move(name));
}

void RegisterBundledResources(const QString &name) {
const auto location = Platform::BundledResourcesPath();
const auto utf = location.toUtf8();
const auto cd = utf.constData();
if (!QResource::registerResource(location + '/' + name)) {
Unexpected("Packed resources not found.");
}
}

} // namespace base
2 changes: 2 additions & 0 deletions base/base_file_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ namespace base {

[[nodiscard]] QString FileNameFromUserString(QString name);

void RegisterBundledResources(const QString &name);

} // namespace base
1 change: 1 addition & 0 deletions base/platform/base_platform_file_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bool DeleteDirectory(QString path);
void RemoveQuarantine(const QString &path);

[[nodiscard]] QString CurrentExecutablePath(int argc, char *argv[]);
[[nodiscard]] QString BundledResourcesPath();

bool RenameWithOverwrite(const QString &from, const QString &to);
void FlushFileData(QFile &file);
Expand Down
4 changes: 4 additions & 0 deletions base/platform/linux/base_file_utilities_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ QString CurrentExecutablePath(int argc, char *argv[]) {
void RemoveQuarantine(const QString &path) {
}

QString BundledResourcesPath() {
Unexpected("BundledResourcesPath not implemented.");
}

// From http://stackoverflow.com/questions/2256945/removing-a-non-empty-directory-programmatically-in-c-or-c
bool DeleteDirectory(QString path) {
if (path.endsWith('/')) {
Expand Down
19 changes: 19 additions & 0 deletions base/platform/mac/base_file_utilities_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ void RemoveQuarantine(const QString &path) {
removexattr(local.data(), kQuarantineAttribute, 0);
}

QString BundledResourcesPath() {
@autoreleasepool {

NSString *path = @"";
@try {
path = [[NSBundle mainBundle] bundlePath];
if (!path) {
Unexpected("Could not get bundled path!");
}
path = [path stringByAppendingString:@"/Contents/Resources"];
return QFile::decodeName([path fileSystemRepresentation]);
}
@catch (NSException *exception) {
Unexpected("Exception in resource registering.");
}

}
}

bool DeleteDirectory(QString path) {
if (path.endsWith('/')) {
path.chop(1);
Expand Down
4 changes: 4 additions & 0 deletions base/platform/win/base_file_utilities_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ bool DeleteDirectory(QString path) {
void RemoveQuarantine(const QString &path) {
}

QString BundledResourcesPath() {
Unexpected("BundledResourcesPath not implemented.");
}

QString CurrentExecutablePath(int argc, char *argv[]) {
auto result = std::array<WCHAR, MAX_PATH + 1>{ 0 };
const auto count = GetModuleFileName(
Expand Down

0 comments on commit 60ec297

Please sign in to comment.