Skip to content

Commit

Permalink
Use hbmenu as a homebrew chooser dialog, more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
XorTroll committed Jul 9, 2023
1 parent 5ff1a0e commit 8b0791b
Show file tree
Hide file tree
Showing 35 changed files with 311 additions and 154 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DEFAULT_HB_NAME := Unnamed homebrew
DEFAULT_HB_AUTHOR := Unknown author
DEFAULT_HB_VERSION := Unknown version

export CPP_DEFS := -DUL_VERSION=\"$(VERSION)\"
export UL_DEFS := -DUL_VERSION=\"$(VERSION)\"

.PHONY: all fresh clean default_hb usystem uloader umenu

Expand Down
2 changes: 1 addition & 1 deletion libs/uCommon/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ OUT_LIB := lib
#---------------------------------------------------------------------------------
ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIC -ftls-model=local-exec

CFLAGS := -g -Wall -Werror -O2 -ffunction-sections -fdata-sections $(ARCH) $(INCLUDE) -D__SWITCH__
CFLAGS := -g -Wall -Werror -O2 -ffunction-sections -fdata-sections $(ARCH) $(INCLUDE) -D__SWITCH__ $(UL_DEFS)

CXXFLAGS := $(CFLAGS) -fno-rtti -fexceptions -std=gnu++20

Expand Down
2 changes: 1 addition & 1 deletion libs/uCommon/include/ul/cfg/cfg_Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#pragma once
#include <ul/ul_Include.hpp>
#include <ul/fs/fs_Stdio.hpp>
#include <ul/loader/loader_TargetInput.hpp>
#include <ul/loader/loader_TargetTypes.hpp>
#include <ul/util/util_String.hpp>
#include <ul/util/util_Json.hpp>

Expand Down
33 changes: 0 additions & 33 deletions libs/uCommon/include/ul/loader/loader_TargetInput.hpp

This file was deleted.

69 changes: 69 additions & 0 deletions libs/uCommon/include/ul/loader/loader_TargetTypes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

#pragma once
#include <ul/util/util_String.hpp>

namespace ul::loader {

constexpr size_t NroPathSize = 512;
constexpr size_t NroArgvSize = 2048;
constexpr size_t MenuCaptionSize = 1024;
constexpr char MenuCaptionHeader[] = "Loaded by uLoader v" UL_VERSION " - uLaunch's custom hbloader replacement!";

struct TargetInput {
static constexpr u32 Magic = 0x49444C55; // "ULDI"

u32 magic;
char nro_path[NroPathSize];
char nro_argv[NroArgvSize];
char menu_caption[MenuCaptionSize];
bool target_once;

template<typename S1, typename S2, typename S3>
static inline TargetInput Create(const S1 &nro_path, const S2 &nro_argv, const bool target_once, const S3 &menu_caption) {
TargetInput target_ipt = {
.magic = Magic,
.target_once = target_once
};


util::CopyToStringBuffer(target_ipt.nro_path, nro_path);
util::CopyToStringBuffer(target_ipt.nro_argv, nro_argv);

std::string menu_full_caption = menu_caption;
if(!menu_full_caption.empty()) {
menu_full_caption = "\n" + menu_full_caption;
}
menu_full_caption = MenuCaptionHeader + menu_full_caption;
util::CopyToStringBuffer(target_ipt.menu_caption, menu_full_caption);
return target_ipt;
}

inline bool IsValid() {
return this->magic == Magic;
}
};

struct TargetOutput {
static constexpr u32 Magic = 0x4F444C55; // "ULDO"

u32 magic;
char nro_path[NroPathSize];
char nro_argv[NroArgvSize];

template<typename S1, typename S2>
static inline TargetOutput Create(const S1 &nro_path, const S2 &nro_argv) {
TargetOutput target_opt = {
.magic = Magic
};

util::CopyToStringBuffer(target_opt.nro_path, nro_path);
util::CopyToStringBuffer(target_opt.nro_argv, nro_argv);
return target_opt;
}

inline bool IsValid() {
return this->magic == Magic;
}
};

}
13 changes: 9 additions & 4 deletions libs/uCommon/include/ul/smi/smi_Protocol.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#pragma once
#include <ul/loader/loader_TargetInput.hpp>
#include <ul/loader/loader_TargetTypes.hpp>
#include <ul/smi/smi_Results.hpp>
#include <functional>

Expand All @@ -10,15 +10,16 @@ namespace ul::smi {
Invalid,
StartupScreen,
Menu,
MenuApplicationSuspended,
MenuLaunchFailure
MenuApplicationSuspended
};

enum class MenuMessage : u32 {
Invalid,
HomeRequest,
SdCardEjected,
GameCardMountFailure
GameCardMountFailure,
PreviousLaunchFailure,
ChosenHomebrew
};

struct MenuMessageContext {
Expand All @@ -28,6 +29,9 @@ namespace ul::smi {
struct {
Result mount_rc;
} gc_mount_failure;
struct {
char nro_path[FS_MAX_PATH];
} chosen_hb;
};
};

Expand All @@ -39,6 +43,7 @@ namespace ul::smi {
TerminateApplication,
LaunchHomebrewLibraryApplet,
LaunchHomebrewApplication,
ChooseHomebrew,
OpenWebPage,
OpenAlbum,
RestartMenu,
Expand Down
35 changes: 25 additions & 10 deletions libs/uCommon/include/ul/ul_Include.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,61 @@ namespace ul {

constexpr const char RootHomebrewPath[] = "sdmc:/switch";

class Lock {
class Mutex {
private:
::Mutex mutex;

public:
constexpr Lock() : mutex() {}
constexpr Mutex() : mutex() {}

inline void lock() {
inline void Lock() {
mutexLock(&this->mutex);
}

inline void unlock() {
inline void Unlock() {
mutexUnlock(&this->mutex);
}

inline bool try_lock() {
inline bool TryLock() {
return mutexTryLock(&this->mutex);
}
};

class RecursiveLock {
class RecursiveMutex {
private:
::RMutex mutex;

public:
constexpr RecursiveLock() : mutex() {}
constexpr RecursiveMutex() : mutex() {}

inline void lock() {
inline void Lock() {
rmutexLock(&this->mutex);
}

inline void unlock() {
inline void Unlock() {
rmutexUnlock(&this->mutex);
}

inline bool try_lock() {
inline bool TryLock() {
return rmutexTryLock(&this->mutex);
}
};

template<typename T>
class ScopedLock {
private:
T &lock;

public:
ScopedLock(T &lock) : lock(lock) {
lock.Lock();
}

~ScopedLock() {
this->lock.Unlock();
}
};

inline std::string JoinPath(const std::string &a, const std::string &b) {
return a + "/" + b;
}
Expand Down
2 changes: 1 addition & 1 deletion libs/uCommon/include/ul/ul_Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace ul {
template<typename ...Args>
inline void NORETURN OnAssertionFailed(const Result rc, const char *log_fmt, Args &&...args) {
// TODONEW: unique log file for each assertion fatal?
auto log_f = fopen(AssertionLogFile, "wb");
auto log_f = fopen(AssertionLogFile, "ab+");
if(log_f) {
fprintf(log_f, log_fmt, args...);
fclose(log_f);
Expand Down
14 changes: 0 additions & 14 deletions libs/uCommon/include/ul/util/util_Scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,4 @@ namespace ul::util {

#define UL_ON_SCOPE_EXIT(...) ::ul::util::OnScopeExit UL_UNIQUE_VAR_NAME(on_scope_exit) ([&]() { __VA_ARGS__ })

class ScopedLock {
private:
Mutex &lock;

public:
ScopedLock(Mutex &lock) : lock(lock) {
mutexLock(std::addressof(lock));
}

~ScopedLock() {
mutexUnlock(std::addressof(this->lock));
}
};

}
15 changes: 15 additions & 0 deletions libs/uCommon/include/ul/util/util_String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once
#include <switch.h>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>

Expand All @@ -24,6 +25,20 @@ namespace ul::util {
return strtoull(val.c_str(), nullptr, 16);
}

template<size_t S>
inline void CopyToStringBuffer(char (&dst)[S], const std::string &src) {
const auto copy_size = std::min(S - 1, src.length());
memcpy(dst, src.c_str(), copy_size);
dst[copy_size] = '\0';
}

template<size_t S1, size_t S2>
inline void CopyToStringBuffer(char (&dst)[S1], const char (&src)[S2]) {
constexpr auto copy_size = std::min(S1 - 1, S2 - 1);
memcpy(dst, src, copy_size);
dst[copy_size] = '\0';
}

std::string FormatAccount(const AccountUid value);
std::string FormatResultDisplay(const Result rc);
std::string FormatResultHex(const Result rc);
Expand Down
15 changes: 8 additions & 7 deletions libs/uCommon/source/ul/cfg/cfg_Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ul::cfg {

std::string GetHomebrewCachePath(const std::string &nro_path, const std::string &ext) {
char path_copy[FS_MAX_PATH] = {};
strcpy(path_copy, nro_path.c_str());
util::CopyToStringBuffer(path_copy, nro_path);
u8 hash[SHA256_HASH_SIZE] = {};
sha256CalculateHash(hash, path_copy, FS_MAX_PATH);

Expand Down Expand Up @@ -176,10 +176,11 @@ namespace ul::cfg {
NroHeader header;
if(fread(&header, sizeof(header), 1, f) == 1) {
if((header.magic == NROHEADER_MAGIC) && (f_size >= header.size)) {
TitleRecord rec = {};
rec.title_type = TitleType::Homebrew;
rec.hb_info = {};
strcpy(rec.hb_info.nro_target.nro_path, path.c_str());
TitleRecord rec = {
.title_type = TitleType::Homebrew,
.hb_info = {}
};
util::CopyToStringBuffer(rec.hb_info.nro_target.nro_path, path);
nros.push_back(rec);
}
}
Expand Down Expand Up @@ -639,9 +640,9 @@ namespace ul::cfg {
rec.control.custom_icon_path = !rec.control.icon_path.empty();

const std::string argv = entry.value("nro_argv", "");
strcpy(rec.hb_info.nro_target.nro_path, nro_path.c_str());
util::CopyToStringBuffer(rec.hb_info.nro_target.nro_path, nro_path);
if(!argv.empty()) {
strcpy(rec.hb_info.nro_target.nro_argv, argv.c_str());
util::CopyToStringBuffer(rec.hb_info.nro_target.nro_argv, argv);
}

if(folder.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion projects/uLoader/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE
CFLAGS := -g -Wall -Werror -O2 -ffunction-sections \
$(ARCH) $(DEFINES)

CFLAGS += $(INCLUDE) -D__SWITCH__ $(CPP_DEFS)
CFLAGS += $(INCLUDE) -D__SWITCH__ $(UL_DEFS)

CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++20

Expand Down
5 changes: 3 additions & 2 deletions projects/uLoader/include/ul/loader/loader_Input.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

#pragma once
#include <ul/loader/loader_TargetInput.hpp>
#include <ul/loader/loader_TargetTypes.hpp>

namespace ul::loader {

Result ReadTargetInput(TargetInput &out_ipt);
Result ReadTargetInput(TargetInput &out_target_ipt);
Result WriteTargetOutput(const TargetOutput &target_opt);

}
3 changes: 2 additions & 1 deletion projects/uLoader/include/ul/loader/loader_Target.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

#pragma once
#include <ul/loader/loader_TargetInput.hpp>
#include <ul/loader/loader_TargetTypes.hpp>

namespace ul::loader {

Result Target(const TargetInput &target_ipt, const bool is_auto_gameplay_recording, const u64 applet_heap_size, const u64 applet_heap_reservation_size);
void LoadTargetOutput(TargetOutput &out_target_opt);

}
Loading

0 comments on commit 8b0791b

Please sign in to comment.