Skip to content

Commit

Permalink
add download URL settings (#114)
Browse files Browse the repository at this point in the history
* add URL settings

* use dynamic urls

* init IME
  • Loading branch information
bucanero authored Jun 10, 2024
1 parent 6245de9 commit 6fa1b42
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Package metadata.
TITLE := PS4 Cheats Manager
VERSION := 01.20
VERSION := 01.22
TITLE_ID := CHTM00777
CONTENT_ID := IV0000-CHTM00777_00-PS4CHEATSMANAGER

# Libraries linked into the ELF.
LIBS := -lc -lkernel -lSceAudioOut -lSceUserService -lScePigletv2VSH -lSceSysmodule -lSceFreeType \
-lScePad -lSceSystemService -lSceSaveData -lSceCommonDialog -lSceMsgDialog -lSceNet \
-lSceNetCtl -lSDL2 -lcjson -ldbglogger -lz -ljbc -lmxml -lcurl -lpolarssl -lSQLite
-lSceNetCtl -lSceImeDialog -lSDL2 -lcjson -ldbglogger -lz -ljbc -lmxml -lcurl -lpolarssl -lSQLite

# Additional compile flags.
EXTRAFLAGS := -fcolor-diagnostics -Wall -D__PS4__
Expand Down
1 change: 1 addition & 0 deletions include/cheats.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ int zip_directory(const char* basedir, const char* inputdir, const char* output_
int extract_zip_gh(const char* zip_file, const char* dest_path);

int show_dialog(int dialog_type, const char * format, ...);
int osk_dialog_get_text(const char* title, char* text, uint32_t size);
void init_progress_bar(const char* msg);
void update_progress_bar(uint64_t progress, const uint64_t total_size, const char* msg);
void end_progress_bar(void);
Expand Down
13 changes: 4 additions & 9 deletions include/settings.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define CHEATSMGR_VERSION "1.2.0" // PS4 Cheats Manager version (about menu)
#define CHEATSMGR_VERSION "1.2.2" // PS4 Cheats Manager version (about menu)

#define MENU_TITLE_OFF 45 // Offset of menu title text from menu mini icon
#define MENU_ICON_OFF 105 // X Offset to start printing menu mini icon
Expand Down Expand Up @@ -34,18 +34,13 @@ typedef struct
uint8_t update;
uint8_t overwrite;
uint32_t user_id;
char url_cheats[256];
char url_patches[256];
char url_plugins[256];
} app_config_t;

extern menu_option_t menu_options[];

extern app_config_t gcm_config;

void log_callback(int sel);
void music_callback(int sel);
void sort_callback(int sel);
void ani_callback(int sel);
void update_callback(int sel);
void overwrite_callback(int sel);
void clearcache_callback(int sel);
void clearpatch_callback(int sel);
void setpluginsperms_callback(int sel);
211 changes: 209 additions & 2 deletions source/dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>
#include "menu.h"

#include <orbis/CommonDialog.h>
#include <orbis/MsgDialog.h>
#include <orbis/ImeDialog.h>

#define MDIALOG_OK 0
#define MDIALOG_YESNO 1
#define SCE_IME_DIALOG_MAX_TEXT_LENGTH 512

static int ime_dialog_running = 0;
static uint16_t inputTextBuffer[SCE_IME_DIALOG_MAX_TEXT_LENGTH+1];
static uint16_t input_ime_title[SCE_IME_DIALOG_MAX_TEXT_LENGTH];


static inline void _orbisCommonDialogSetMagicNumber(uint32_t* magic, const OrbisCommonDialogBaseParam* param)
Expand Down Expand Up @@ -121,3 +126,205 @@ void stop_loading_screen(void)
{
end_progress_bar();
}

static int convert_to_utf16(const char* utf8, uint16_t* utf16, uint32_t available)
{
int count = 0;
while (*utf8)
{
uint8_t ch = (uint8_t)*utf8++;
uint32_t code;
uint32_t extra;

if (ch < 0x80)
{
code = ch;
extra = 0;
}
else if ((ch & 0xe0) == 0xc0)
{
code = ch & 31;
extra = 1;
}
else if ((ch & 0xf0) == 0xe0)
{
code = ch & 15;
extra = 2;
}
else
{
// TODO: this assumes there won't be invalid utf8 codepoints
code = ch & 7;
extra = 3;
}

for (uint32_t i=0; i<extra; i++)
{
uint8_t next = (uint8_t)*utf8++;
if (next == 0 || (next & 0xc0) != 0x80)
{
goto utf16_end;
}
code = (code << 6) | (next & 0x3f);
}

if (code < 0xd800 || code >= 0xe000)
{
if (available < 1) goto utf16_end;
utf16[count++] = (uint16_t)code;
available--;
}
else // surrogate pair
{
if (available < 2) goto utf16_end;
code -= 0x10000;
utf16[count++] = 0xd800 | (code >> 10);
utf16[count++] = 0xdc00 | (code & 0x3ff);
available -= 2;
}
}

utf16_end:
utf16[count]=0;
return count;
}

static int convert_from_utf16(const uint16_t* utf16, char* utf8, uint32_t size)
{
int count = 0;
while (*utf16)
{
uint32_t code;
uint16_t ch = *utf16++;
if (ch < 0xd800 || ch >= 0xe000)
{
code = ch;
}
else // surrogate pair
{
uint16_t ch2 = *utf16++;
if (ch < 0xdc00 || ch > 0xe000 || ch2 < 0xd800 || ch2 > 0xdc00)
{
goto utf8_end;
}
code = 0x10000 + ((ch & 0x03FF) << 10) + (ch2 & 0x03FF);
}

if (code < 0x80)
{
if (size < 1) goto utf8_end;
utf8[count++] = (char)code;
size--;
}
else if (code < 0x800)
{
if (size < 2) goto utf8_end;
utf8[count++] = (char)(0xc0 | (code >> 6));
utf8[count++] = (char)(0x80 | (code & 0x3f));
size -= 2;
}
else if (code < 0x10000)
{
if (size < 3) goto utf8_end;
utf8[count++] = (char)(0xe0 | (code >> 12));
utf8[count++] = (char)(0x80 | ((code >> 6) & 0x3f));
utf8[count++] = (char)(0x80 | (code & 0x3f));
size -= 3;
}
else
{
if (size < 4) goto utf8_end;
utf8[count++] = (char)(0xf0 | (code >> 18));
utf8[count++] = (char)(0x80 | ((code >> 12) & 0x3f));
utf8[count++] = (char)(0x80 | ((code >> 6) & 0x3f));
utf8[count++] = (char)(0x80 | (code & 0x3f));
size -= 4;
}
}

utf8_end:
utf8[count]=0;
return count;
}

static int initImeDialog(const char *usrTitle, const char *initialText, int maxTextLen, int type)
{
OrbisImeDialogSetting param;

if (ime_dialog_running)
return 0;

if ((strlen(initialText) > countof(inputTextBuffer)) || (strlen(usrTitle) > countof(input_ime_title)))
{
ime_dialog_running = 0;
return 0;
}

memset(&param, 0, sizeof(OrbisImeDialogSetting));
memset(inputTextBuffer, 0, sizeof(inputTextBuffer));
memset(input_ime_title, 0, sizeof(input_ime_title));

// converts the multibyte string src to a wide-character string starting at dest.
convert_to_utf16(initialText, inputTextBuffer, countof(inputTextBuffer));
convert_to_utf16(usrTitle, input_ime_title, countof(input_ime_title));

param.userId = gcm_config.user_id;
param.supportedLanguages = 0;
param.maxTextLength = maxTextLen;
param.inputTextBuffer = (wchar_t*) inputTextBuffer;
param.title = (wchar_t*) input_ime_title;
param.type = type ? ORBIS_TYPE_TYPE_URL : ORBIS_TYPE_DEFAULT;
param.posx = 1920 / 2;
param.posy = 1080 / 2;
param.horizontalAlignment = ORBIS_H_CENTER;
param.verticalAlignment = ORBIS_V_CENTER;
param.enterLabel = ORBIS_BUTTON_LABEL_DEFAULT;

if (sceImeDialogInit(&param, NULL) < 0)
return 0;

ime_dialog_running = 1;
return 1;
}

static int updateImeDialog(void)
{
OrbisDialogStatus status;
OrbisDialogResult result;

if (!ime_dialog_running)
return 0;

status = sceImeDialogGetStatus();
if (status == ORBIS_DIALOG_STATUS_STOPPED)
{
memset(&result, 0, sizeof(OrbisDialogResult));
sceImeDialogGetResult(&result);

sceImeDialogTerm();
ime_dialog_running = 0;

if (result.endstatus == ORBIS_DIALOG_OK)
return 1;

return (-1);
}

return 0;
}

int osk_dialog_get_text(const char* title, char* text, uint32_t size)
{
size = (size > SCE_IME_DIALOG_MAX_TEXT_LENGTH) ? SCE_IME_DIALOG_MAX_TEXT_LENGTH : (size-1);

if (!initImeDialog(title, text, size, 1))
return 0;

while (ime_dialog_running)
{
if (updateImeDialog() < 0)
return 0;
}

return (convert_from_utf16(inputTextBuffer, text, size));
}
12 changes: 6 additions & 6 deletions source/exec_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ static void togglePatch(const game_entry_t* game, const code_entry_t* code)

static void updNetCheats(void)
{
if (!http_download(GOLDCHEATS_URL, GOLDCHEATS_FILE, CHEATSMGR_LOCAL_CACHE LOCAL_TEMP_ZIP, 1))
if (!http_download(gcm_config.url_cheats, GOLDCHEATS_FILE, CHEATSMGR_LOCAL_CACHE LOCAL_TEMP_ZIP, 1))
{
show_message("No internet connection to " GOLDCHEATS_URL GOLDCHEATS_FILE " or server not available!");
show_message("No internet connection to %s%s or server not available!", gcm_config.url_cheats, GOLDCHEATS_FILE);
return;
}

Expand All @@ -74,9 +74,9 @@ static void updNetCheats(void)

static void updNetPatches(void)
{
if (!http_download(GOLDPATCH_URL, GOLDPATCH_FILE, CHEATSMGR_LOCAL_CACHE LOCAL_TEMP_ZIP, 1))
if (!http_download(gcm_config.url_patches, GOLDPATCH_FILE, CHEATSMGR_LOCAL_CACHE LOCAL_TEMP_ZIP, 1))
{
show_message("No internet connection to " GOLDPATCH_URL GOLDPATCH_FILE " or server not available!");
show_message("No internet connection to %s%s or server not available!", gcm_config.url_patches, GOLDPATCH_FILE);
return;
}

Expand All @@ -97,9 +97,9 @@ static void updNetPatches(void)

static void updNetPlugins(void)
{
if (!http_download(GOLDPLUGINS_UPDATE_URL, "", CHEATSMGR_LOCAL_CACHE "plugins.json", 0))
if (!http_download(gcm_config.url_plugins, "", CHEATSMGR_LOCAL_CACHE "plugins.json", 0))
{
show_message("No internet connection to " GOLDPLUGINS_UPDATE_URL " or server not available!");
show_message("No internet connection to %s or server not available!", gcm_config.url_plugins);
return;
}

Expand Down
20 changes: 9 additions & 11 deletions source/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,24 @@
#include "common.h"
#include "cheats.h"

#define HTTP_SUCCESS 1
#define HTTP_FAILED 0
#define HTTP_USER_AGENT "Mozilla/5.0 (PLAYSTATION 4; 1.00)"


int http_init(void)
{
if(sceSysmoduleLoadModuleInternal(ORBIS_SYSMODULE_INTERNAL_NET) < 0)
return HTTP_FAILED;
return false;

if(sceSysmoduleLoadModuleInternal(ORBIS_SYSMODULE_INTERNAL_NETCTL) < 0)
return HTTP_FAILED;
return false;

if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
return HTTP_FAILED;
return false;

if(sceNetCtlInit() < 0)
return HTTP_FAILED;
return false;

return HTTP_SUCCESS;
return true;
}

/* follow the CURLOPT_XFERINFOFUNCTION callback definition */
Expand All @@ -53,13 +51,13 @@ int http_download(const char* url, const char* filename, const char* local_dst,
if(!curl)
{
LOG("ERROR: CURL INIT");
return HTTP_FAILED;
return false;
}

fd = fopen(local_dst, "wb");
if (!fd) {
LOG("fopen Error: File path '%s'", local_dst);
return HTTP_FAILED;
return false;
}

snprintf(full_url, sizeof(full_url), "%s%s", url, filename);
Expand Down Expand Up @@ -128,10 +126,10 @@ int http_download(const char* url, const char* filename, const char* local_dst,
{
LOG("curl_easy_perform() failed: %s", curl_easy_strerror(res));
unlink_secure(local_dst);
return HTTP_FAILED;
return false;
}

return HTTP_SUCCESS;
return true;
}

void http_end(void)
Expand Down
6 changes: 5 additions & 1 deletion source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ app_config_t gcm_config = {
.update = 1,
.overwrite = 1,
.user_id = 0,
.url_cheats = GOLDCHEATS_URL,
.url_patches = GOLDPATCH_URL,
.url_plugins = GOLDPLUGINS_UPDATE_URL,
};

int close_app = 0;
Expand Down Expand Up @@ -427,7 +430,8 @@ s32 main(s32 argc, const char* argv[])
}

// Load MsgDialog
if (sceSysmoduleLoadModule(ORBIS_SYSMODULE_MESSAGE_DIALOG) < 0)
if (sceSysmoduleLoadModule(ORBIS_SYSMODULE_MESSAGE_DIALOG) < 0 ||
sceSysmoduleLoadModule(ORBIS_SYSMODULE_IME_DIALOG) < 0)
{
LOG("Failed to load dialog!");
return (-1);
Expand Down
Loading

0 comments on commit 6fa1b42

Please sign in to comment.