Cleanup and improvements

This commit is contained in:
XorTroll 2020-06-13 20:45:44 +02:00
parent 7853802391
commit ae77e3225e
9 changed files with 136 additions and 127 deletions

View file

@ -3,6 +3,7 @@
#include <ul_Include.hpp> #include <ul_Include.hpp>
#include <hb/hb_Target.hpp> #include <hb/hb_Target.hpp>
#include <fs/fs_Stdio.hpp> #include <fs/fs_Stdio.hpp>
#include <util/util_Convert.hpp>
namespace cfg { namespace cfg {
@ -130,7 +131,10 @@ namespace cfg {
void RenameFolder(TitleList &list, const std::string &old_name, const std::string &new_name); void RenameFolder(TitleList &list, const std::string &old_name, const std::string &new_name);
bool ExistsRecord(TitleList &list, TitleRecord record); bool ExistsRecord(TitleList &list, TitleRecord record);
std::string GetTitleCacheIconPath(u64 app_id); inline std::string GetTitleCacheIconPath(u64 app_id) {
return UL_BASE_SD_DIR "/title/" + util::FormatApplicationId(app_id) + ".jpg";
}
std::string GetNROCacheIconPath(const std::string &path); std::string GetNROCacheIconPath(const std::string &path);
} }

View file

@ -25,7 +25,7 @@ namespace os {
}; };
std::string GetLanguageName(u32 idx); std::string GetLanguageName(u32 idx);
std::vector<std::string> &GetLanguageNameList(); const std::vector<std::string> &GetLanguageNameList();
u32 GetBatteryLevel(); u32 GetBatteryLevel();
bool IsConsoleCharging(); bool IsConsoleCharging();

View file

@ -1,33 +1,38 @@
#include <am/am_Application.hpp> #include <am/am_Application.hpp>
namespace am namespace am {
{
extern bool g_home_has_focus; namespace {
static AppletApplication g_application_holder;
static u64 g_last_app_id; AppletApplication g_ApplicationHolder;
u64 g_LastApplicationId;
}
extern bool g_DaemonHasFocus;
bool ApplicationIsActive() { bool ApplicationIsActive() {
if(g_application_holder.StateChangedEvent.revent == INVALID_HANDLE) { if(g_ApplicationHolder.StateChangedEvent.revent == INVALID_HANDLE) {
return false; return false;
} }
if(!serviceIsActive(&g_application_holder.s)) { if(!serviceIsActive(&g_ApplicationHolder.s)) {
return false; return false;
} }
return !appletApplicationCheckFinished(&g_application_holder); return !appletApplicationCheckFinished(&g_ApplicationHolder);
} }
void ApplicationTerminate() { void ApplicationTerminate() {
appletApplicationRequestExit(&g_application_holder); appletApplicationRequestExit(&g_ApplicationHolder);
g_home_has_focus = true; g_DaemonHasFocus = true;
} }
Result ApplicationStart(u64 app_id, bool system, AccountUid user_id, void *data, size_t size) { Result ApplicationStart(u64 app_id, bool system, AccountUid user_id, void *data, size_t size) {
appletApplicationClose(&g_application_holder); appletApplicationClose(&g_ApplicationHolder);
if(system) { if(system) {
R_TRY(appletCreateSystemApplication(&g_application_holder, app_id)); R_TRY(appletCreateSystemApplication(&g_ApplicationHolder, app_id));
} }
else { else {
R_TRY(appletCreateApplication(&g_application_holder, app_id)); R_TRY(appletCreateApplication(&g_ApplicationHolder, app_id));
} }
if(accountUidIsValid(&user_id)) { if(accountUidIsValid(&user_id)) {
@ -40,20 +45,20 @@ namespace am
} }
R_TRY(appletUnlockForeground()); R_TRY(appletUnlockForeground());
R_TRY(appletApplicationStart(&g_application_holder)); R_TRY(appletApplicationStart(&g_ApplicationHolder));
R_TRY(ApplicationSetForeground()); R_TRY(ApplicationSetForeground());
g_last_app_id = app_id; g_LastApplicationId = app_id;
return 0; return 0;
} }
bool ApplicationHasForeground() { bool ApplicationHasForeground() {
return !g_home_has_focus; return !g_DaemonHasFocus;
} }
Result ApplicationSetForeground() { Result ApplicationSetForeground() {
R_TRY(appletApplicationRequestForApplicationToGetForeground(&g_application_holder)); R_TRY(appletApplicationRequestForApplicationToGetForeground(&g_ApplicationHolder));
g_home_has_focus = false; g_DaemonHasFocus = false;
return ResultSuccess; return ResultSuccess;
} }
@ -64,12 +69,12 @@ namespace am
appletStorageClose(&st); appletStorageClose(&st);
}); });
R_TRY(appletStorageWrite(&st, 0, data, size)); R_TRY(appletStorageWrite(&st, 0, data, size));
R_TRY(appletApplicationPushLaunchParameter(&g_application_holder, kind, &st)); R_TRY(appletApplicationPushLaunchParameter(&g_ApplicationHolder, kind, &st));
return ResultSuccess; return ResultSuccess;
} }
u64 ApplicationGetId() { u64 ApplicationGetId() {
return g_last_app_id; return g_LastApplicationId;
} }
bool ApplicationNeedsUser(u64 app_id) { bool ApplicationNeedsUser(u64 app_id) {

View file

@ -3,16 +3,15 @@
namespace am { namespace am {
bool g_home_has_focus = true; bool g_DaemonHasFocus = true;
extern AppletHolder g_applet_holder;
bool HomeMenuHasForeground() { bool HomeMenuHasForeground() {
return g_home_has_focus; return g_DaemonHasFocus;
} }
Result HomeMenuSetForeground() { Result HomeMenuSetForeground() {
R_TRY(appletRequestToGetForeground()); R_TRY(appletRequestToGetForeground());
g_home_has_focus = true; g_DaemonHasFocus = true;
return ResultSuccess; return ResultSuccess;
} }

View file

@ -1,95 +1,98 @@
#include <am/am_LibraryApplet.hpp> #include <am/am_LibraryApplet.hpp>
#include <map>
namespace am namespace am {
{
static AppletHolder g_applet_holder;
static AppletId g_menu_applet_id = InvalidAppletId;
static AppletId g_last_applet_id = InvalidAppletId;
static std::map<u64, AppletId> g_applet_id_table = { namespace {
{ 0x0100000000001001, AppletId_auth },
{ 0x0100000000001002, AppletId_cabinet }, AppletHolder g_AppletHolder;
{ 0x0100000000001003, AppletId_controller }, AppletId g_MenuAppletId = InvalidAppletId;
{ 0x0100000000001004, AppletId_dataErase }, AppletId g_LastAppletId = InvalidAppletId;
{ 0x0100000000001005, AppletId_error },
{ 0x0100000000001006, AppletId_netConnect }, const std::map<u64, AppletId> g_AppletIdTable = {
{ 0x0100000000001007, AppletId_playerSelect }, { 0x0100000000001001, AppletId_auth },
{ 0x0100000000001008, AppletId_swkbd }, { 0x0100000000001002, AppletId_cabinet },
{ 0x0100000000001009, AppletId_miiEdit }, { 0x0100000000001003, AppletId_controller },
{ 0x010000000000100A, AppletId_web }, { 0x0100000000001004, AppletId_dataErase },
{ 0x010000000000100B, AppletId_shop }, { 0x0100000000001005, AppletId_error },
{ 0x010000000000100D, AppletId_photoViewer }, { 0x0100000000001006, AppletId_netConnect },
{ 0x010000000000100E, AppletId_set }, { 0x0100000000001007, AppletId_playerSelect },
{ 0x010000000000100F, AppletId_offlineWeb }, { 0x0100000000001008, AppletId_swkbd },
{ 0x0100000000001010, AppletId_loginShare }, { 0x0100000000001009, AppletId_miiEdit },
{ 0x0100000000001011, AppletId_wifiWebAuth }, { 0x010000000000100A, AppletId_web },
{ 0x0100000000001013, AppletId_myPage } { 0x010000000000100B, AppletId_shop },
}; { 0x010000000000100D, AppletId_photoViewer },
{ 0x010000000000100E, AppletId_set },
{ 0x010000000000100F, AppletId_offlineWeb },
{ 0x0100000000001010, AppletId_loginShare },
{ 0x0100000000001011, AppletId_wifiWebAuth },
{ 0x0100000000001013, AppletId_myPage }
};
}
bool LibraryAppletIsActive() { bool LibraryAppletIsActive() {
if(g_applet_holder.StateChangedEvent.revent == INVALID_HANDLE) { if(g_AppletHolder.StateChangedEvent.revent == INVALID_HANDLE) {
return false; return false;
} }
if(!serviceIsActive(&g_applet_holder.s)) { if(!serviceIsActive(&g_AppletHolder.s)) {
return false; return false;
} }
return !appletHolderCheckFinished(&g_applet_holder); return !appletHolderCheckFinished(&g_AppletHolder);
} }
void LibraryAppletSetMenuAppletId(AppletId id) { void LibraryAppletSetMenuAppletId(AppletId id) {
g_menu_applet_id = id; g_MenuAppletId = id;
} }
AppletId LibraryAppletGetMenuAppletId() { AppletId LibraryAppletGetMenuAppletId() {
return g_menu_applet_id; return g_MenuAppletId;
} }
bool LibraryAppletIsMenu() { bool LibraryAppletIsMenu() {
return LibraryAppletIsActive() && (g_menu_applet_id != InvalidAppletId) && (LibraryAppletGetId() == g_menu_applet_id); return LibraryAppletIsActive() && (g_MenuAppletId != InvalidAppletId) && (LibraryAppletGetId() == g_MenuAppletId);
} }
void LibraryAppletTerminate() { void LibraryAppletTerminate() {
// Give it 15 seconds // Give it 15 seconds
appletHolderRequestExitOrTerminate(&g_applet_holder, 15'000'000'000ul); appletHolderRequestExitOrTerminate(&g_AppletHolder, 15'000'000'000ul);
} }
Result LibraryAppletStart(AppletId id, u32 la_version, void *in_data, size_t in_size) { Result LibraryAppletStart(AppletId id, u32 la_version, void *in_data, size_t in_size) {
if(LibraryAppletIsActive()) { if(LibraryAppletIsActive()) {
LibraryAppletTerminate(); LibraryAppletTerminate();
} }
appletHolderClose(&g_applet_holder); appletHolderClose(&g_AppletHolder);
LibAppletArgs largs; LibAppletArgs largs;
libappletArgsCreate(&largs, la_version); libappletArgsCreate(&largs, la_version);
R_TRY(appletCreateLibraryApplet(&g_applet_holder, id, LibAppletMode_AllForeground)); R_TRY(appletCreateLibraryApplet(&g_AppletHolder, id, LibAppletMode_AllForeground));
R_TRY(libappletArgsPush(&largs, &g_applet_holder)); R_TRY(libappletArgsPush(&largs, &g_AppletHolder));
if(in_size > 0) { if(in_size > 0) {
R_TRY(LibraryAppletSend(in_data, in_size)); R_TRY(LibraryAppletSend(in_data, in_size));
} }
R_TRY(appletHolderStart(&g_applet_holder)); R_TRY(appletHolderStart(&g_AppletHolder));
g_last_applet_id = id; g_LastAppletId = id;
return ResultSuccess; return ResultSuccess;
} }
Result LibraryAppletSend(void *data, size_t size) { Result LibraryAppletSend(void *data, size_t size) {
return libappletPushInData(&g_applet_holder, data, size); return libappletPushInData(&g_AppletHolder, data, size);
} }
Result LibraryAppletRead(void *data, size_t size) { Result LibraryAppletRead(void *data, size_t size) {
return libappletPopOutData(&g_applet_holder, data, size, nullptr); return libappletPopOutData(&g_AppletHolder, data, size, nullptr);
} }
Result LibraryAppletDaemonLaunchWith(AppletId id, u32 la_version, std::function<void(AppletHolder*)> on_prepare, std::function<void(AppletHolder*)> on_finish, std::function<bool()> on_wait) { Result LibraryAppletDaemonLaunchWith(AppletId id, u32 la_version, std::function<void(AppletHolder*)> on_prepare, std::function<void(AppletHolder*)> on_finish, std::function<bool()> on_wait) {
if(LibraryAppletIsActive()) { if(LibraryAppletIsActive()) {
LibraryAppletTerminate(); LibraryAppletTerminate();
} }
appletHolderClose(&g_applet_holder); appletHolderClose(&g_AppletHolder);
LibAppletArgs largs; LibAppletArgs largs;
libappletArgsCreate(&largs, la_version); libappletArgsCreate(&largs, la_version);
R_TRY(appletCreateLibraryApplet(&g_applet_holder, id, LibAppletMode_AllForeground)); R_TRY(appletCreateLibraryApplet(&g_AppletHolder, id, LibAppletMode_AllForeground));
R_TRY(libappletArgsPush(&largs, &g_applet_holder)); R_TRY(libappletArgsPush(&largs, &g_AppletHolder));
on_prepare(&g_applet_holder); on_prepare(&g_AppletHolder);
R_TRY(appletHolderStart(&g_applet_holder)); R_TRY(appletHolderStart(&g_AppletHolder));
while(true) { while(true) {
if(!LibraryAppletIsActive()) { if(!LibraryAppletIsActive()) {
break; break;
@ -98,15 +101,15 @@ namespace am
LibraryAppletTerminate(); LibraryAppletTerminate();
break; break;
} }
svcSleepThread(10'000'000l); svcSleepThread(10'000'000ul);
} }
on_finish(&g_applet_holder); on_finish(&g_AppletHolder);
appletHolderClose(&g_applet_holder); appletHolderClose(&g_AppletHolder);
return ResultSuccess; return ResultSuccess;
} }
u64 LibraryAppletGetProgramIdForAppletId(AppletId id) { u64 LibraryAppletGetProgramIdForAppletId(AppletId id) {
for(auto &[program_id, applet_id] : g_applet_id_table) { for(auto &[program_id, applet_id] : g_AppletIdTable) {
if(applet_id == id) { if(applet_id == id) {
return program_id; return program_id;
} }
@ -115,17 +118,17 @@ namespace am
} }
AppletId LibraryAppletGetAppletIdForProgramId(u64 id) { AppletId LibraryAppletGetAppletIdForProgramId(u64 id) {
auto it = g_applet_id_table.find(id); auto it = g_AppletIdTable.find(id);
if(it != g_applet_id_table.end()) { if(it != g_AppletIdTable.end()) {
return it->second; return it->second;
} }
return InvalidAppletId; return InvalidAppletId;
} }
AppletId LibraryAppletGetId() { AppletId LibraryAppletGetId() {
auto idcopy = g_last_applet_id; auto idcopy = g_LastAppletId;
if(!LibraryAppletIsActive()) { if(!LibraryAppletIsActive()) {
g_last_applet_id = InvalidAppletId; g_LastAppletId = InvalidAppletId;
} }
return idcopy; return idcopy;
} }

View file

@ -2,9 +2,7 @@
#include <os/os_Titles.hpp> #include <os/os_Titles.hpp>
#include <util/util_Misc.hpp> #include <util/util_Misc.hpp>
#include <util/util_String.hpp> #include <util/util_String.hpp>
#include <fs/fs_Stdio.hpp>
#include <db/db_Save.hpp> #include <db/db_Save.hpp>
#include <util/util_Convert.hpp>
namespace cfg { namespace cfg {
@ -218,8 +216,7 @@ namespace cfg {
return cfg; return cfg;
} }
Config LoadConfig() Config LoadConfig() {
{
// Default constructor sets everything // Default constructor sets everything
Config cfg = {}; Config cfg = {};
JSON cfgjson; JSON cfgjson;
@ -248,9 +245,9 @@ namespace cfg {
return CreateNewAndLoadConfig(); return CreateNewAndLoadConfig();
} }
void SaveConfig(const Config &cfg) void SaveConfig(const Config &cfg) {
{
fs::DeleteFile(CFG_CONFIG_JSON); fs::DeleteFile(CFG_CONFIG_JSON);
auto json = JSON::object(); auto json = JSON::object();
json["theme_name"] = cfg.theme_name; json["theme_name"] = cfg.theme_name;
json["viewer_usb_enabled"] = cfg.viewer_usb_enabled; json["viewer_usb_enabled"] = cfg.viewer_usb_enabled;
@ -258,13 +255,13 @@ namespace cfg {
json["menu_program_id"] = util::FormatApplicationId(cfg.menu_program_id); json["menu_program_id"] = util::FormatApplicationId(cfg.menu_program_id);
json["homebrew_applet_program_id"] = util::FormatApplicationId(cfg.homebrew_applet_program_id); json["homebrew_applet_program_id"] = util::FormatApplicationId(cfg.homebrew_applet_program_id);
json["homebrew_title_application_id"] = util::FormatApplicationId(cfg.homebrew_title_application_id); json["homebrew_title_application_id"] = util::FormatApplicationId(cfg.homebrew_title_application_id);
std::ofstream ofs(CFG_CONFIG_JSON); std::ofstream ofs(CFG_CONFIG_JSON);
ofs << std::setw(4) << json; ofs << std::setw(4) << json;
ofs.close(); ofs.close();
} }
void SaveRecord(TitleRecord &record) void SaveRecord(TitleRecord &record) {
{
auto entry = JSON::object(); auto entry = JSON::object();
entry["type"] = record.title_type; entry["type"] = record.title_type;
entry["folder"] = record.sub_folder; entry["folder"] = record.sub_folder;
@ -507,8 +504,7 @@ namespace cfg {
return title_found; return title_found;
} }
TitleFolder &FindFolderByName(TitleList &list, const std::string &name) TitleFolder &FindFolderByName(TitleList &list, const std::string &name) {
{
if(!name.empty()) { if(!name.empty()) {
auto f = STL_FIND_IF(list.folders, fld, (fld.name == name)); auto f = STL_FIND_IF(list.folders, fld, (fld.name == name));
if(STL_FOUND(list.folders, f)) { if(STL_FOUND(list.folders, f)) {
@ -529,9 +525,8 @@ namespace cfg {
} }
} }
bool ExistsRecord(TitleList &list, TitleRecord record) bool ExistsRecord(TitleList &list, TitleRecord record) {
{ auto title_found = false;
bool title_found = false;
TitleRecord record_copy = {}; TitleRecord record_copy = {};
std::string recjson; std::string recjson;
const auto type = static_cast<TitleType>(record.title_type); const auto type = static_cast<TitleType>(record.title_type);
@ -670,11 +665,6 @@ namespace cfg {
return list; return list;
} }
std::string GetTitleCacheIconPath(u64 app_id) {
auto strappid = util::FormatApplicationId(app_id);
return UL_BASE_SD_DIR "/title/" + strappid + ".jpg";
}
std::string GetNROCacheIconPath(const std::string &path) { std::string GetNROCacheIconPath(const std::string &path) {
char pathcopy[FS_MAX_PATH] = {0}; char pathcopy[FS_MAX_PATH] = {0};
strcpy(pathcopy, path.c_str()); strcpy(pathcopy, path.c_str());
@ -683,10 +673,12 @@ namespace cfg {
std::string out = UL_BASE_SD_DIR "/nro/"; std::string out = UL_BASE_SD_DIR "/nro/";
std::stringstream strm; std::stringstream strm;
strm << out; strm << out;
for(u32 i = 0; i < 0x10; i++) { // Use the first half of the hash, like N does with NCAs.
for(u32 i = 0; i < sizeof(hash) / 2; i++) {
strm << std::setw(2) << std::setfill('0') << std::hex << std::nouppercase << (u32)hash[i]; strm << std::setw(2) << std::setfill('0') << std::hex << std::nouppercase << (u32)hash[i];
} }
strm << ".jpg"; strm << ".jpg";
return strm.str(); return strm.str();
} }
} }

View file

@ -48,7 +48,7 @@ namespace net {
std::string GetConsoleIPAddress() { std::string GetConsoleIPAddress() {
char ipaddr[0x20] = {0}; char ipaddr[0x20] = {0};
auto ip = gethostid(); auto ip = gethostid();
sprintf(ipaddr, "%lu.%lu.%lu.%lu", (ip & 0xff000000) >> 24, (ip & 0x00ff0000) >> 16, (ip & 0x0000ff00) >> 8, (ip & 0x000000ff)); sprintf(ipaddr, "%lu.%lu.%lu.%lu", (ip & 0x000000FF), (ip & 0x0000FF00) >> 8, (ip & 0x00FF0000) >> 16, (ip & 0xFF000000) >> 24);
return ipaddr; return ipaddr;
} }

View file

@ -15,10 +15,11 @@ namespace os {
s32 acc_count = 0; s32 acc_count = 0;
R_TRY(accountListAllUsers(uids, ACC_USER_LIST_SIZE, &acc_count)); R_TRY(accountListAllUsers(uids, ACC_USER_LIST_SIZE, &acc_count));
for(s32 i = 0; i < acc_count; i++) { for(s32 i = 0; i < acc_count; i++) {
out_accounts.push_back(uids[i]); auto uid = uids[i];
out_accounts.push_back(uid);
if(dump_icon) { if(dump_icon) {
AccountProfile prof; AccountProfile prof;
auto rc = accountGetProfile(&prof, uids[i]); auto rc = accountGetProfile(&prof, uid);
if(R_SUCCEEDED(rc)) { if(R_SUCCEEDED(rc)) {
u32 imgsz = 0; u32 imgsz = 0;
rc = accountProfileGetImageSize(&prof, &imgsz); rc = accountProfileGetImageSize(&prof, &imgsz);
@ -27,7 +28,7 @@ namespace os {
u32 tmpsz; u32 tmpsz;
rc = accountProfileLoadImage(&prof, imgbuf, imgsz, &tmpsz); rc = accountProfileLoadImage(&prof, imgbuf, imgsz, &tmpsz);
if(R_SUCCEEDED(rc)) { if(R_SUCCEEDED(rc)) {
auto iconcache = GetIconCacheImagePath(uids[i]); auto iconcache = GetIconCacheImagePath(uid);
fs::WriteFile(iconcache, imgbuf, imgsz, true); fs::WriteFile(iconcache, imgbuf, imgsz, true);
} }
delete[] imgbuf; delete[] imgbuf;

View file

@ -3,35 +3,39 @@
namespace os { namespace os {
static std::vector<std::string> g_lang_names = { namespace {
"Japanese",
"American English", const std::vector<std::string> g_LanguageNameList = {
"Français", "Japanese",
"Deutsch", "American English",
"Italiano", "Français",
"Español", "Deutsch",
"Chinese", "Italiano",
"Korean", "Español",
"Nederlands", "Chinese",
"Português", "Korean",
"Русский", "Nederlands",
"Taiwanese", "Português",
"British English", "Русский",
"Français canadien", "Taiwanese",
"Español latino", "British English",
"Chinese (simplified)", "Français canadien",
"Chinese (traditional)" "Español latino",
}; "Chinese (simplified)",
"Chinese (traditional)"
};
std::string GetLanguageName(u32 idx) {
if(idx >= g_lang_names.size()) {
return "";
}
return g_lang_names[idx];
} }
std::vector<std::string> &GetLanguageNameList() { std::string GetLanguageName(u32 idx) {
return g_lang_names; if(idx >= g_LanguageNameList.size()) {
return "";
}
return g_LanguageNameList[idx];
}
const std::vector<std::string> &GetLanguageNameList() {
return g_LanguageNameList;
} }
u32 GetBatteryLevel() { u32 GetBatteryLevel() {
@ -53,6 +57,7 @@ namespace os {
} }
// Thanks Goldleaf // Thanks Goldleaf
std::string GetCurrentTime() { std::string GetCurrentTime() {
auto time_val = time(nullptr); auto time_val = time(nullptr);
auto local_time = localtime(&time_val); auto local_time = localtime(&time_val);