Skip to content

Commit

Permalink
Large refactor to the app to fix URL configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Apr 30, 2024
1 parent bd6fea0 commit 81465a0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 41 deletions.
1 change: 1 addition & 0 deletions vita/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")
add_executable(Allefresher_vita
main.c
reader.c
clib.c
)

target_link_libraries(Allefresher_vita
Expand Down
27 changes: 27 additions & 0 deletions vita/clib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// This file provides implementations for some common libc functions we need, since we dont want to link against libc proper

char *strcpy(char *dest, const char *src)
{
char *ret = dest;

while (*src)
{
*dest++ = *src++;
}
*dest = '\0';

return ret;
}

int strlen(const char *s)
{
int len = 0;
while (*s++)
len++;
return len;
}

int isspace(int c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
}
44 changes: 21 additions & 23 deletions vita/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "reader.h"

char *GAME_URL;
char GAME_URL[256];

static SceUID https_hook;
static tai_hook_ref_t https_ref;
Expand All @@ -20,31 +20,32 @@ static tai_hook_ref_t http_ref;
static SceUID resource_hook;
static tai_hook_ref_t resource_ref;

int strlen(const char *s)
{
int len = 0;
while (*s++)
len++;
return len;
}

// This is the HTTPS url the game uses, its fine if its not actually HTTPS
char *getHttpsUrl(int arg1)
{
return GAME_URL;
}

// This is the HTTP url the game uses, its fine if its not actually HTTP
char *getHttpUrl(int arg1)
{
return GAME_URL;
}

// This returns the URL the game uses to fetch a specific resource
char *getResourceUrl(char *out, char *hash)
{
// http://%s/r/%s

// TODO: allow resource URL to be set to a different server

// Copy the base of the URL
sceClibMemcpy(out, GAME_URL, strlen(GAME_URL));
// Copy the resource subdir prefix
sceClibMemcpy(out + strlen(GAME_URL), "/r/", 3);
// Copy the hash to the end of the URL
sceClibMemcpy(out + strlen(GAME_URL) + 3, hash, strlen(hash));
// Null terminate the string
out[strlen(GAME_URL) + strlen(hash) + 3] = '\0';

return out;
Expand All @@ -53,15 +54,14 @@ char *getResourceUrl(char *out, char *hash)
void _start() __attribute__((weak, alias("module_start")));
int module_start(SceSize argc, const void *args)
{
sceClibPrintf("allefresher module start!\n");
sceClibPrintf("allefresher module start! looking for config...\n");

sceClibPrintf("reading config\n");
// Try to load the URL from the file, if it fails, just use the default URL
if (readFileFirstLine("ux0:/allefresher.txt", GAME_URL) == 0)
{
sceClibPrintf("Failed to read allefresher.txt, using default URL\n");

GAME_URL = "https://lbp.littlebigrefresh.com/lbp";
strcpy(GAME_URL, "https://lbp.littlebigrefresh.com/lbp");
}
else
{
Expand All @@ -70,11 +70,13 @@ int module_start(SceSize argc, const void *args)
{
GAME_URL[strlen(GAME_URL) - 1] = '\0';
}

sceClibPrintf("Loaded user provided URL %s\n", GAME_URL);
}

sceClibPrintf("game URL: %s\n", GAME_URL);
sceClibPrintf("Final base URL: %s\n", GAME_URL);

sceClibPrintf("hooking functions\n");
sceClibPrintf("Hooking functions...\n");

tai_module_info_t info;
info.size = sizeof(tai_module_info_t);
Expand All @@ -89,7 +91,7 @@ int module_start(SceSize argc, const void *args)
0x163a7e, // 0x81163a7e
1, // ARM/THUMB
getHttpsUrl);
sceClibPrintf("https_hook: %08x\n", https_hook);
sceClibPrintf("Hooked HTTPS: %08x\n", https_hook);

// Patch the game's get_http_url function to return our own URL
http_hook = taiHookFunctionOffset(
Expand All @@ -99,17 +101,17 @@ int module_start(SceSize argc, const void *args)
0x163994, // 0x81163994
1, // ARM/THUMB
getHttpUrl);
sceClibPrintf("http_hook: %08x\n", http_hook);
sceClibPrintf("Hooked HTTP: %08x\n", http_hook);

// Patch the game's get_resource_url function to return our own URL
// Patch the game's get_resource_url function to return our own formatted URLs
resource_hook = taiHookFunctionOffset(
&resource_ref,
info.modid,
0, // Segment index
0x163914, // 0x81163914
1, // ARM/THUMB
getResourceUrl);
sceClibPrintf("resource_hook: %08x\n", resource_hook);
sceClibPrintf("Hooked resource URL: %08x\n", resource_hook);

return SCE_KERNEL_START_SUCCESS;
}
Expand All @@ -119,10 +121,6 @@ int module_stop(SceSize argc, const void *args)
taiHookRelease(https_hook, https_ref);
taiHookRelease(http_hook, http_ref);
taiHookRelease(resource_hook, resource_ref);

return SCE_KERNEL_STOP_SUCCESS;
}

// int _free_vita_newlib()
// {
// return 0;
// }
18 changes: 0 additions & 18 deletions vita/reader.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
#include "reader.h"

int isspace(int c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
}

char *strcpy(char *dest, const char *src)
{
char *ret = dest;

while (*src)
{
*dest++ = *src++;
}
*dest = '\0';

return ret;
}

// Reads the file at `path` into `target`
int readFileFirstLine(char *path, char *target)
{
Expand Down
1 change: 1 addition & 0 deletions vita/reader.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <string.h>
#include <psp2/io/fcntl.h>
#include <psp2/kernel/clib.h>

Expand Down

0 comments on commit 81465a0

Please sign in to comment.