-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_patch.cpp
167 lines (137 loc) · 4.29 KB
/
main_patch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#if defined(PORTAL) && defined(PORTAL2)
#error You cannot define PORTAL and PORTAL2 at the same time!
#endif
#if !defined(PORTAL) && !defined(PORTAL2)
#error You must define if you are using PORTAL or PORTAL2!
#endif
#include "skyline/inlinehook/And64InlineHook.hpp"
#include "skyline/utils/cpputils.hpp"
#include "skyline/inlinehook/memcpy_controlled.hpp"
#include "nn/fs.h"
#include "nn/nifm.h"
#include <sys/stat.h>
uintptr_t TextRegionOffset = 0;
ptrdiff_t returnInstructionOffset(uintptr_t LR) {
return LR - TextRegionOffset;
}
extern "C" {
int stat_nx(const char* pathname, struct stat* statbuf);
FILE* fopen_nx ( const char * filename, const char * mode );
#ifdef PORTAL2
void* _ZN2nn3mem17StandardAllocator8AllocateEm(void* _this, size_t size);
#endif
}
namespace nn {
namespace nifm {
uint64_t (*Initialize_original)();
uint64_t Initialize_hook() {
return 0x27C;
}
}
}
char* strtolower(char* dest, const char* src, size_t len)
{
if (!len)
return 0;
else {
char* d = dest;
while (*src && len-- > 0)
*d++ = tolower(*src++);
*d = 0;
return dest;
}
}
void formatPath (const char* path, char* filepath, bool NXCONTENT) {
char temp[256] = "";
if (!NXCONTENT) {
if (!strncmp(path, "/", 1))
sprintf(&temp[0], "rom:%s", path);
else
sprintf(&temp[0], "rom:/%s", path);
strtolower(filepath, &temp[0], strlen(&temp[0]));
}
else {
if (!strncmp(path, "/", 1))
sprintf(&temp[0], "rom:/nxcontent%s", path);
else
sprintf(&temp[0], "rom:/nxcontent/%s", path);
strtolower(filepath, &temp[0], strlen(&temp[0]));
}
}
int (*stat_nx_original)(const char* pathname, struct stat* statbuf);
int stat_nx_hook(const char* pathname, struct stat* statbuf) {
int ret = stat_nx_original(pathname, statbuf);
if (!ret)
return ret;
if (!strncmp(pathname, "/;", 2))
return ret;
char filepath[256] = "";
formatPath(pathname, &filepath[0], true);
struct stat temp_statbuf;
int ret2 = stat(&filepath[0], &temp_statbuf);
if (!ret2)
return stat(&filepath[0], statbuf);
formatPath(pathname, &filepath[0], false);
ret2 = stat(&filepath[0], &temp_statbuf);
if (!ret2)
return stat(&filepath[0], statbuf);
return ret;
}
struct fopen2Struct {
uint64_t buffer_size;
void* buffer;
};
void (*fopen2_original)(fopen2Struct* _struct, void* x1, const char* path);
void fopen2_hook(fopen2Struct* _struct, void* x1, const char* path){
char filepath[256] = "";
formatPath(path, &filepath[0], false);
FILE* file = fopen(filepath, "r");
if(!file)
return fopen2_original(_struct, x1, path);
int32_t size = 0;
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
#ifdef PORTAL
void* new_buffer = malloc(size);
#endif
#ifdef PORTAL2
void* Allocator = (void*)(TextRegionOffset+0x860078);
void* new_buffer = _ZN2nn3mem17StandardAllocator8AllocateEm(Allocator, size);
#endif
fread(new_buffer, size, 1, file);
fclose(file);
_struct->buffer_size = size;
_struct->buffer = new_buffer;
}
char filepath[256] = "";
FILE* (*fopen_nx_original)(const char* path, const char* mode);
FILE* fopen_nx_hook(const char* path, const char* mode) {
nn::fs::FileHandle filehandle;
formatPath(path, &filepath[0], true);
if(R_FAILED(nn::fs::OpenFile(&filehandle, &filepath[0], nn::fs::OpenMode_Read))) {
formatPath(path, &filepath[0], false);
if(R_FAILED(nn::fs::OpenFile(&filehandle, &filepath[0], nn::fs::OpenMode_Read)))
return fopen_nx_original(path, mode);
}
nn::fs::CloseFile(filehandle);
return fopen(&filepath[0], mode);
}
void Portal_main()
{
TextRegionOffset = (uintptr_t)skyline::utils::getRegionAddress(skyline::utils::region::Text);
#ifdef PORTAL
A64HookFunction((void*)(TextRegionOffset + 0x7D978), reinterpret_cast<void*>(fopen2_hook), (void**)&fopen2_original);
#endif
#ifdef PORTAL2
A64HookFunction((void*)(TextRegionOffset + 0x182F50), reinterpret_cast<void*>(fopen2_hook), (void**)&fopen2_original);
#endif
A64HookFunction((void**)&fopen_nx, reinterpret_cast<void*>(fopen_nx_hook), (void**)&fopen_nx_original);
A64HookFunction((void**)&stat_nx, reinterpret_cast<void*>(stat_nx_hook), (void**)&stat_nx_original);
#ifdef PORTAL2
#ifdef PDEBUG
//Hook needed to run game with connected internet and blocked Nintendo servers
A64HookFunction((void**)&nn::nifm::Initialize, reinterpret_cast<void*>(nn::nifm::Initialize_hook), (void**)&nn::nifm::Initialize_original);
#endif
#endif
}