Skip to content

Commit

Permalink
Update to newest libnx and new dmntcht (#98)
Browse files Browse the repository at this point in the history
* updated for new libnx, updated dmntcht

* contents instead of titles

* convert service names correctly

* update libnx
  • Loading branch information
HookedBehemoth authored and WerWolv committed Dec 8, 2019
1 parent 6970154 commit 44a30ce
Show file tree
Hide file tree
Showing 20 changed files with 426 additions and 798 deletions.
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"defines": [
"DEBUG",
"SWITCH",
"__SWITCH__"
"__SWITCH__",
"VERSION_STRING=\"3.1.0\""
],
"compilerPath": "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-g++",
"cStandard": "c11",
Expand Down
4 changes: 2 additions & 2 deletions include/guis/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Gui {
void drawTooltip(s16 x, s16 y, const char *text, color_t backgroundColor, color_t textColor, u8 alpha, bool flipped);

static bool requestKeyboardInput(std::string headerText, std::string subHeaderText, std::string initialText, SwkbdType type, char *out_text, size_t maxLength);
static u128 requestPlayerSelection();
static AccountUid requestPlayerSelection();
static void requestErrorMessage(Result result);

protected:
Expand All @@ -92,7 +92,7 @@ class Gui {
FT_Library m_fontLibrary;
FT_Face m_fontFaces[FONT_FACES_MAX];
FT_Face m_fontLastUsedFace;
size_t m_fontFacesTotal;
s32 m_fontFacesTotal;

std::unordered_map<size_t, std::pair<u16, u16>> m_stringDimensions;

Expand Down
2 changes: 1 addition & 1 deletion include/guis/gui_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GuiMain : public Gui {
s8 extraOption;

u64 titleId;
u128 userId;
AccountUid userId;
} m_selected = { 0, -1, 0, 0 };

static inline bool m_editableOnly = false;
Expand Down
27 changes: 21 additions & 6 deletions include/helpers/account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,33 @@
#include <vector>
#include <unordered_map>

namespace std {
template <>
struct hash<AccountUid> {
std::size_t operator()(const AccountUid& acc_uid) const {
using std::hash;
return ((hash<u64>()(acc_uid.uid[0])
^ (hash<u64>()(acc_uid.uid[1]) << 1)) >> 1);
}
};
}

inline bool operator==(const AccountUid& acc_1, const AccountUid& acc_2) {
return (acc_1.uid[0] == acc_2.uid[0] && acc_1.uid[1] == acc_2.uid[1]);
}

class Account {
public:
static inline Account *g_currAccount = nullptr;
static inline std::unordered_map<u128, Account*> g_accounts;
static inline std::unordered_map<AccountUid, Account*> g_accounts;

static inline u128 g_activeUser = 0;
static inline AccountUid g_activeUser = {0};

Account(u128 userID);
Account(AccountUid userID);
~Account();

std::string getUserName();
u128 getUserID();
AccountUid getUserID();
u8* getProfileImage();
bool isInitialized();

Expand All @@ -26,9 +41,9 @@ class Account {
AccountUserData m_userData;
AccountProfileBase m_profileBase;

u128 m_userID;
AccountUid m_userID;
std::vector<u8> m_profileImage;
size_t m_profileImageSize;
u32 m_profileImageSize;
std::string m_userName;
bool m_isInitialized;
};
168 changes: 92 additions & 76 deletions include/helpers/dmntcht.h
Original file line number Diff line number Diff line change
@@ -1,76 +1,92 @@
#pragma once

#include <edizon.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
u64 base;
u64 size;
} DmntMemoryRegionExtents;

typedef struct {
u64 process_id;
u64 title_id;
DmntMemoryRegionExtents main_nso_extents;
DmntMemoryRegionExtents heap_extents;
DmntMemoryRegionExtents alias_extents;
DmntMemoryRegionExtents address_space_extents;
u8 main_nso_build_id[0x20];
} DmntCheatProcessMetadata;

typedef struct {
char readable_name[0x40];
uint32_t num_opcodes;
uint32_t opcodes[0x100];
} DmntCheatDefinition;

typedef struct {
bool enabled;
uint32_t cheat_id;
DmntCheatDefinition definition;
} DmntCheatEntry;

typedef struct {
u64 value;
u8 width;
} DmntFrozenAddressValue;

typedef struct {
u64 address;
DmntFrozenAddressValue value;
} DmntFrozenAddressEntry;

Result dmntchtInitialize(void);
void dmntchtExit(void);
Service* dmntchtGetServiceSession(void);

Result dmntchtHasCheatProcess(bool *out);
Result dmntchtGetCheatProcessEvent(Event *event);
Result dmntchtGetCheatProcessMetadata(DmntCheatProcessMetadata *out_metadata);
Result dmntchtForceOpenCheatProcess(void);

Result dmntchtGetCheatProcessMappingCount(u64 *out_count);
Result dmntchtGetCheatProcessMappings(MemoryInfo *buffer, u64 max_count, u64 offset, u64 *out_count);
Result dmntchtReadCheatProcessMemory(u64 address, void *buffer, size_t size);
Result dmntchtWriteCheatProcessMemory(u64 address, const void *buffer, size_t size);
Result dmntchtQueryCheatProcessMemory(MemoryInfo *mem_info, u64 address);

Result dmntchtGetCheatCount(u64 *out_count);
Result dmntchtGetCheats(DmntCheatEntry *buffer, u64 max_count, u64 offset, u64 *out_count);
Result dmntchtGetCheatById(DmntCheatEntry *out_cheat, u32 cheat_id);
Result dmntchtToggleCheat(u32 cheat_id);
Result dmntchtAddCheat(DmntCheatDefinition *cheat, bool enabled, u32 *out_cheat_id);
Result dmntchtRemoveCheat(u32 cheat_id);

Result dmntchtGetFrozenAddressCount(u64 *out_count);
Result dmntchtGetFrozenAddresses(DmntFrozenAddressEntry *buffer, u64 max_count, u64 offset, u64 *out_count);
Result dmntchtGetFrozenAddress(DmntFrozenAddressEntry *out, u64 address);
Result dmntchtEnableFrozenAddress(u64 address, u64 width, u64 *out_value);
Result dmntchtDisableFrozenAddress(u64 address);

#ifdef __cplusplus
}
#endif
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <edizon.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
u64 base;
u64 size;
} DmntMemoryRegionExtents;

typedef struct {
u64 process_id;
u64 title_id;
DmntMemoryRegionExtents main_nso_extents;
DmntMemoryRegionExtents heap_extents;
DmntMemoryRegionExtents alias_extents;
DmntMemoryRegionExtents address_space_extents;
u8 main_nso_build_id[0x20];
} DmntCheatProcessMetadata;

typedef struct {
char readable_name[0x40];
uint32_t num_opcodes;
uint32_t opcodes[0x100];
} DmntCheatDefinition;

typedef struct {
bool enabled;
uint32_t cheat_id;
DmntCheatDefinition definition;
} DmntCheatEntry;

typedef struct {
u64 value;
u8 width;
} DmntFrozenAddressValue;

typedef struct {
u64 address;
DmntFrozenAddressValue value;
} DmntFrozenAddressEntry;

Result dmntchtInitialize(void);
void dmntchtExit(void);
Service* dmntchtGetServiceSession(void);

Result dmntchtHasCheatProcess(bool *out);
Result dmntchtGetCheatProcessEvent(Event *event);
Result dmntchtGetCheatProcessMetadata(DmntCheatProcessMetadata *out_metadata);
Result dmntchtForceOpenCheatProcess(void);

Result dmntchtGetCheatProcessMappingCount(u64 *out_count);
Result dmntchtGetCheatProcessMappings(MemoryInfo *buffer, u64 max_count, u64 offset, u64 *out_count);
Result dmntchtReadCheatProcessMemory(u64 address, void *buffer, size_t size);
Result dmntchtWriteCheatProcessMemory(u64 address, const void *buffer, size_t size);
Result dmntchtQueryCheatProcessMemory(MemoryInfo *mem_info, u64 address);

Result dmntchtGetCheatCount(u64 *out_count);
Result dmntchtGetCheats(DmntCheatEntry *buffer, u64 max_count, u64 offset, u64 *out_count);
Result dmntchtGetCheatById(DmntCheatEntry *out_cheat, u32 cheat_id);
Result dmntchtToggleCheat(u32 cheat_id);
Result dmntchtAddCheat(DmntCheatDefinition *cheat, bool enabled, u32 *out_cheat_id);
Result dmntchtRemoveCheat(u32 cheat_id);

Result dmntchtGetFrozenAddressCount(u64 *out_count);
Result dmntchtGetFrozenAddresses(DmntFrozenAddressEntry *buffer, u64 max_count, u64 offset, u64 *out_count);
Result dmntchtGetFrozenAddress(DmntFrozenAddressEntry *out, u64 address);
Result dmntchtEnableFrozenAddress(u64 address, u64 width, u64 *out_value);
Result dmntchtDisableFrozenAddress(u64 address);

#ifdef __cplusplus
}
#endif
12 changes: 6 additions & 6 deletions include/helpers/save.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
Result _getSaveList(std::vector<FsSaveDataInfo> &saveInfoList);

s32 deleteDirRecursively(const char *path, bool isSave);
void makeExInjDir(char ptr[0x100], u64 titleID, u128 userID, bool isInject);
Result mountSaveByTitleAccountIDs(const u64 titleID, const u128 userID, FsFileSystem& tmpfs);
void makeExInjDir(char ptr[0x100], u64 titleID, AccountUid userID, bool isInject);
Result mountSaveByTitleAccountIDs(const u64 titleID, const AccountUid userID, FsFileSystem& tmpfs);
s32 isDirectory(const char *path);
s32 cpFile(std::string srcPath, std::string dstPath);
s32 copyAllSave(const char * path, bool isInject, const char exInjDir[0x100]);

s32 backupSave(u64 titleID, u128 userID, bool isBatch, std::string backupName);
s32 restoreSave(u64 titleID, u128 userID, const char* path);
s32 backupSave(u64 titleID, AccountUid userID, bool isBatch, std::string backupName);
s32 restoreSave(u64 titleID, AccountUid userID, const char* path);

s32 loadSaveFile(std::vector<u8> *buffer, size_t *length, u64 titleID, u128 userID, const char *path);
s32 storeSaveFile(u8 *buffer, size_t length, u64 titleID, u128 userID, const char *path);
s32 loadSaveFile(std::vector<u8> *buffer, size_t *length, u64 titleID, AccountUid userID, const char *path);
s32 storeSaveFile(u8 *buffer, size_t length, u64 titleID, AccountUid userID, const char *path);
64 changes: 64 additions & 0 deletions include/helpers/service_guard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once
#include <switch/types.h>
#include <switch/result.h>
#include <switch/kernel/mutex.h>
#include <switch/sf/service.h>
#include <switch/services/sm.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct ServiceGuard {
Mutex mutex;
u32 refCount;
} ServiceGuard;

NX_INLINE bool serviceGuardBeginInit(ServiceGuard* g)
{
mutexLock(&g->mutex);
return (g->refCount++) == 0;
}

NX_INLINE Result serviceGuardEndInit(ServiceGuard* g, Result rc, void (*cleanupFunc)(void))
{
if (R_FAILED(rc)) {
cleanupFunc();
--g->refCount;
}
mutexUnlock(&g->mutex);
return rc;
}

NX_INLINE void serviceGuardExit(ServiceGuard* g, void (*cleanupFunc)(void))
{
mutexLock(&g->mutex);
if (g->refCount && (--g->refCount) == 0)
cleanupFunc();
mutexUnlock(&g->mutex);
}

#define NX_GENERATE_SERVICE_GUARD_PARAMS(name, _paramdecl, _parampass) \
\
static ServiceGuard g_##name##Guard; \
NX_INLINE Result _##name##Initialize _paramdecl; \
static void _##name##Cleanup(void); \
\
Result name##Initialize _paramdecl \
{ \
Result rc = 0; \
if (serviceGuardBeginInit(&g_##name##Guard)) \
rc = _##name##Initialize _parampass; \
return serviceGuardEndInit(&g_##name##Guard, rc, _##name##Cleanup); \
} \
\
void name##Exit(void) \
{ \
serviceGuardExit(&g_##name##Guard, _##name##Cleanup); \
}

#define NX_GENERATE_SERVICE_GUARD(name) NX_GENERATE_SERVICE_GUARD_PARAMS(name, (void), ())

#ifdef __cplusplus
}
#endif
6 changes: 3 additions & 3 deletions include/helpers/title.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Title {
std::string getTitleAuthor();
std::string getTitleVersion();
u8* getTitleIcon();
std::vector<u128> getUserIDs();
void addUserID(u128 userID);
std::vector<AccountUid> getUserIDs();
void addUserID(AccountUid userID);
u64 getTitleID();

private:
Expand All @@ -33,6 +33,6 @@ class Title {
std::string m_titleName;
std::string m_titleAuthor;
std::string m_titleVersion;
std::vector<u128> m_userIDs;
std::vector<AccountUid> m_userIDs;
u8 m_errorCode;
};
2 changes: 1 addition & 1 deletion libs/libnx
Submodule libnx updated 171 files
Loading

0 comments on commit 44a30ce

Please sign in to comment.