Skip to content

Commit eb8cd16

Browse files
committed
Slight refactor, added VM-relevant code ahead of C;C support and distinguished library includes from our own
1 parent b169d4d commit eb8cd16

File tree

13 files changed

+284
-120
lines changed

13 files changed

+284
-120
lines changed

src/RegionalDialect/Config.cpp

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

22
#include <vector>
3+
#include <concepts>
34

4-
#include "log/logger_mgr.hpp"
5-
#include "skyline/utils/cpputils.hpp"
5+
#include <log/logger_mgr.hpp>
6+
#include <skyline/utils/cpputils.hpp>
67

7-
#include "RegionalDialect/Config.h"
8+
#include "Config.h"
89

910
namespace rd {
1011
namespace config {
@@ -42,6 +43,10 @@ template<> char* JsonWrapper::get<char*>() {
4243
return ::cJSON_GetStringValue(inner);
4344
}
4445

46+
template<> bool JsonWrapper::get<bool>() {
47+
return static_cast<bool>(::cJSON_IsTrue(inner));
48+
}
49+
4550
template<> std::vector<char*> JsonWrapper::get<std::vector<char*>>() {
4651
size_t size = (size_t)::cJSON_GetArraySize(inner);
4752
auto ret = std::vector<char*>();
@@ -79,22 +84,50 @@ void Init(std::string const& romMount) {
7984
free((void*)contents);
8085
return;
8186
}
87+
8288
bool result = ::cJSON_AddItemToObject(inner, "gamedef", parseResult);
8389
if (!result || inner == NULL) {
8490
Logging.Log("Failed to parse gamedef.json: %s\n", ::cJSON_GetErrorPtr());
91+
goto cleanup;
8592
} else {
8693
Logging.Log("Successfully parsed gamedef.json\n");
87-
config = JsonWrapper(inner, true);
88-
config.print();
89-
}
94+
}
95+
96+
free((void*)contents);
97+
98+
rc = skyline::utils::readEntireFile(romMount + "system/patchdef.json", (void**)(&contents), &contentsSize);
99+
100+
if (R_FAILED(rc)) {
101+
Logging.Log("Failed to load patchdef.json: 0x%x\n", rc);
102+
goto exit;
103+
}
104+
105+
Logging.Log("Successfully loaded patchdef.json: size(%d)\n", contentsSize);
106+
107+
parseResult = cJSON_ParseWithLength(contents, contentsSize);
108+
if (parseResult == NULL) {
109+
Logging.Log("Failed to parse patchdef.json: %s\n", ::cJSON_GetErrorPtr());
110+
goto exit;
111+
}
112+
113+
result = ::cJSON_AddItemToObject(inner, "patchdef", parseResult);
114+
if (!result || inner == NULL) {
115+
Logging.Log("Failed to parse patchdef.json: %s\n", ::cJSON_GetErrorPtr());
116+
} else {
117+
Logging.Log("Successfully parsed patchdef.json\n");
118+
}
119+
120+
exit:
121+
config = JsonWrapper(inner, true);
122+
cleanup:
90123
free((void*)contents);
91-
Logging.Log("Freed contents\n");
92124
}
93125

94126
template int JsonWrapper::get<int>();
95127
template size_t JsonWrapper::get<size_t>();
96128
template char *JsonWrapper::get<char*>();
97129
template std::vector<char*> JsonWrapper::get<std::vector<char*>>();
130+
template bool JsonWrapper::get<bool>();
98131

99132
} // namespace config
100133
} // namespace rd

src/RegionalDialect/Config.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
#include <string>
44

5-
#include "cJSON/cJSON.h"
6-
7-
#include "lib/log/logger_mgr.hpp"
5+
#include <cJSON/cJSON.h>
6+
#include <lib/log/logger_mgr.hpp>
87

98
namespace rd {
109
namespace config {

src/RegionalDialect/Hook.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
#include <string>
44
#include <sstream>
55

6-
#include "log/logger_mgr.hpp"
7-
#include "skyline/utils/cpputils.hpp"
6+
#include <log/logger_mgr.hpp>
7+
#include <skyline/utils/cpputils.hpp>
88

9-
#include "RegionalDialect/Utils.h"
10-
#include "RegionalDialect/Hook.h"
11-
#include "RegionalDialect/Config.h"
9+
#include "Mem.h"
10+
#include "Hook.h"
11+
#include "Config.h"
1212

1313
enum SigExprTokenType {
1414
Start,
@@ -99,7 +99,7 @@ class SigExprParser {
9999

100100
if (token.type == Comma && allowComma) {
101101
ptrdiff_t offset = expression(false);
102-
result = rd::utils::AssemblePointer(result, offset);
102+
result = rd::mem::AssemblePointer(result, offset);
103103
token = lexer.getToken();
104104
lexer.nextToken();
105105
}

src/RegionalDialect/Hook.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#pragma once
22

3+
#include <lib.hpp>
4+
#include <lib/hook/trampoline.hpp>
5+
36
#include "Config.h"
4-
#include "lib.hpp"
5-
#include "lib/hook/trampoline.hpp"
67

78
#define DECLARE_HOOK(name, ret, ...) \
89
HOOK_DEFINE_TRAMPOLINE(name) { static ret Callback(__VA_ARGS__); };
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
#include "log/logger_mgr.hpp"
1+
#include <log/logger_mgr.hpp>
22

3-
#include "RegionalDialect/Utils.h"
3+
#include "Mem.h"
44

55
extern uintptr_t codeCaves;
66

77
namespace rd {
8-
namespace utils {
8+
namespace mem {
99

1010
void Trampoline(uintptr_t address, uintptr_t target, reg::Register reg) {
1111
if ((address & 3) || (target & 3) || reg.Index() > 31) ::abort();
12+
13+
if (address == 0) { Logging.Log("Invalid address. Skipping."); return; }
14+
if (target == 0) { Logging.Log("Invalid target. Skipping."); return; }
15+
1216
Overwrite(address, inst::BranchLink(codeCaves - address).Value());
1317
Overwrite(codeCaves + 0, inst::LdrLiteral(reg, 0x8).Value());
1418
Overwrite(codeCaves + 4, inst::BranchRegister(reg).Value());
@@ -32,5 +36,5 @@ uintptr_t AssemblePointer(uintptr_t adrp_addr, ptrdiff_t ldr_offset) {
3236
return pageAddr + offsetFromPageStart;
3337
}
3438

35-
} // namespace utils
39+
} // namespace mem
3640
} // namespace rd
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#include <cstdint>
44
#include <cstring>
55

6-
#include "lib/armv8.hpp"
7-
#include "util/sys/rw_pages.hpp"
6+
#include <lib/armv8.hpp>
7+
#include <util/sys/rw_pages.hpp>
88

99
namespace inst = exl::armv8::inst;
1010
namespace reg = exl::armv8::reg;
1111

1212
namespace rd {
13-
namespace utils {
13+
namespace mem {
1414

1515
template <typename T>
1616
inline void Overwrite(uintptr_t address, const T &value) {
@@ -24,5 +24,5 @@ void Trampoline(uintptr_t address, uintptr_t target, reg::Register reg);
2424

2525
uintptr_t AssemblePointer(uintptr_t adrp_addr, ptrdiff_t ldr_offset);
2626

27-
} // namespace utils
27+
} // namespace mem
2828
} // namespace rd

src/RegionalDialect/System.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
#include "RegionalDialect/Utils.h"
2-
3-
#define SYSTEM_IMPLEMENTATION
4-
#include "RegionalDialect/System.h"
1+
#include "System.h"
2+
#include "Mem.h"
53

64
namespace rd {
75
namespace sys {
@@ -118,7 +116,9 @@ void OptionMain::Callback(void) {
118116
}
119117
}
120118

121-
119+
bool ChkViewDic::Callback(uint param_1, uint param_2) {
120+
return Orig(param_1, param_2);
121+
}
122122

123123
void Init() {
124124
ScrWork = (int32_t*)rd::hook::SigScan("game", "ScrWork");
@@ -140,35 +140,42 @@ void Init() {
140140
uintptr_t audioLoweringAddr = rd::hook::SigScan("game", "audioLoweringAddr");
141141
uint32_t nop = inst::Nop().Value();
142142

143-
rd::utils::Overwrite(audioLoweringAddr, nop);
144-
rd::utils::Overwrite(audioLoweringAddr + 4, nop);
143+
rd::mem::Overwrite(audioLoweringAddr, nop);
144+
rd::mem::Overwrite(audioLoweringAddr + 4, nop);
145145
}
146146

147-
if (rd::config::config["gamedef"]["signatures"]["game"].has("OPTmenuMaxCur")) {
148-
uint8_t *OPTmenuMaxCur = (uint8_t*)rd::hook::SigScan("game", "OPTmenuMaxCur");
149-
OPTmenuMaxCur[4] = 4;
150-
}
151147

152148
if (rd::config::config["gamedef"]["signatures"]["game"].has("SkipModeFix"))
153-
rd::utils::Overwrite(rd::hook::SigScan("game", "SkipModeFix"), inst::Branch(-284).Value());
149+
rd::mem::Overwrite(rd::hook::SigScan("game", "SkipModeFix"), inst::Branch(-284).Value());
154150

155151
if (rd::config::config["gamedef"]["signatures"]["game"].has("DoZSelection1"))
156-
rd::utils::Overwrite(rd::hook::SigScan("game", "DoZSelection1"), inst::Branch(-796).Value());
152+
rd::mem::Overwrite(rd::hook::SigScan("game", "DoZSelection1"), inst::Branch(-796).Value());
157153

158154
if (rd::config::config["gamedef"]["signatures"]["game"].has("DoZSelection2"))
159-
rd::utils::Overwrite(rd::hook::SigScan("game", "DoZSelection2"), inst::Branch(-320).Value());
155+
rd::mem::Overwrite(rd::hook::SigScan("game", "DoZSelection2"), inst::Branch(-320).Value());
160156

161157
if (rd::config::config["gamedef"]["signatures"]["game"].has("ShortcutMenuFix"))
162-
rd::utils::Overwrite(rd::hook::SigScan("game", "ShorcutMenuFix"), inst::Movz(reg::W0, 0x370).Value());
158+
rd::mem::Overwrite(rd::hook::SigScan("game", "ShorcutMenuFix"), inst::Movz(reg::W0, 0x370).Value());
163159

164160
HOOK_FUNC(game, GSLflatRectF);
165161
HOOK_FUNC(game, SetFlag);
166162
HOOK_FUNC(game, GetFlag);
167-
HOOK_FUNC(game, SpeakerDrawingFunction);
168-
HOOK_FUNC(game, OptionDispChip2);
163+
164+
if (rd::config::config["patchdef"]["base"]["addNametags"].get<bool>()) {
165+
166+
if (rd::config::config["gamedef"]["signatures"]["game"].has("OPTmenuMaxCur")) {
167+
uint8_t *OPTmenuMaxCur = (uint8_t*)rd::hook::SigScan("game", "OPTmenuMaxCur");
168+
OPTmenuMaxCur[4] = 4;
169+
}
170+
171+
HOOK_FUNC(game, SpeakerDrawingFunction);
172+
HOOK_FUNC(game, OptionDispChip2);
173+
HOOK_FUNC(game, OptionMain);
174+
}
175+
169176
HOOK_FUNC(game, SSEvolume);
170-
HOOK_FUNC(game, OptionMain);
171177
HOOK_FUNC(game, SSEplay);
178+
HOOK_FUNC(game, ChkViewDic);
172179
}
173180

174181
} // namespace sys

src/RegionalDialect/System.h

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,6 @@
55
namespace rd {
66
namespace sys {
77

8-
#pragma pack(4)
9-
struct ScriptThreadState {
10-
/* 0000 */ int accumulator;
11-
/* 0004 */ char gap4[16];
12-
/* 0014 */ unsigned int thread_group_id;
13-
/* 0018 */ unsigned int sleep_timeout;
14-
/* 001C */ char gap28[8];
15-
/* 0024 */ unsigned int loop_counter;
16-
/* 0028 */ unsigned int loop_target_label_id;
17-
/* 002C */ unsigned int call_stack_depth;
18-
/* 0030 */ unsigned int ret_address_ids[8];
19-
/* 0050 */ unsigned int ret_address_script_buffer_ids[8];
20-
/* 0070 */ int thread_id;
21-
/* 0074 */ int script_buffer_id;
22-
/* 0078 */ char gap120[68];
23-
/* 00BC */ int thread_local_variables[32];
24-
/* 013C */ int somePageNumber;
25-
/* 0140 */ ScriptThreadState *next_context;
26-
/* 0144 */ ScriptThreadState *prev_context;
27-
/* 0148 */ ScriptThreadState *next_free_context;
28-
/* 014C */ void *pc;
29-
};
30-
318
inline int32_t *ScrWork = nullptr;
329
inline uint32_t *OPTmenuModePtr = nullptr;
3310
inline uint8_t *OPTmenuCur = nullptr;
@@ -58,6 +35,8 @@ DECLARE_HOOK(SSEvolume, void, uint param_1);
5835

5936
DECLARE_HOOK(SSEplay, void, int param_1, int param_2);
6037

38+
DECLARE_HOOK(ChkViewDic, bool, uint param_1, uint param_2);
39+
6140
void Init();
6241

6342
} // namespace sys

0 commit comments

Comments
 (0)