Skip to content

Commit

Permalink
Added Data Files downloader feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinnegatamante committed Jul 1, 2020
1 parent 05b0ae3 commit f1c11bd
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 48 deletions.
1 change: 0 additions & 1 deletion Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ if (VITA_RELEASE)
SceAudio_stub
ScePower_stub
SceNet_stub
SceNetCtl_stub
SceAppMgr_stub
SceAppUtil_stub
c
Expand Down
54 changes: 40 additions & 14 deletions Source/SysVita/UI/DownloaderScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include "Utility/Timer.h"
#include "SysVita/UI/Menu.h"

#define MAX(a, b) a > b ? a : b

static char *sizes[] = {
"B",
"KB",
Expand Down Expand Up @@ -60,26 +58,25 @@ void DrawDownloaderScreen(int index, float downloaded_bytes, float total_bytes,
char msg[512];
sprintf(msg, "%s (%ld / %ld)", text, index, passes);
ImVec2 pos = ImGui::CalcTextSize(msg);
float win_size = pos.x > 400.0f ? pos.x + 10.0f : 400.0f;

ImGui::GetIO().MouseDrawCursor = false;
ImGui::SetNextWindowPos(ImVec2((SCR_WIDTH / 2) - 200 * UI_SCALE, (SCR_HEIGHT / 2) - 50 * UI_SCALE), ImGuiSetCond_Always);
ImGui::SetNextWindowSize(ImVec2(win_size * UI_SCALE, 100 * UI_SCALE), ImGuiSetCond_Always);
ImGui::SetNextWindowSize(ImVec2(400 * UI_SCALE, 100 * UI_SCALE), ImGuiSetCond_Always);
ImGui::Begin("", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus);

ImGui::SetCursorPos(ImVec2((win_size * UI_SCALE - pos.x) / 2, 20 * UI_SCALE));
ImGui::SetCursorPos(ImVec2((400 * UI_SCALE - pos.x) / 2, 20 * UI_SCALE));
ImGui::Text(msg);
if (total_bytes < 4000000000.0f) {
sprintf(msg, "%.2f %s / %.2f %s", format(downloaded_bytes), sizes[quota(downloaded_bytes)], format(total_bytes), sizes[quota(total_bytes)]);
pos = ImGui::CalcTextSize(msg);
ImGui::SetCursorPos(ImVec2((win_size * UI_SCALE - pos.x) / 2, 40 * UI_SCALE));
ImGui::SetCursorPos(ImVec2((400 * UI_SCALE - pos.x) / 2, 40 * UI_SCALE));
ImGui::Text(msg);
ImGui::SetCursorPos(ImVec2((win_size / 4) * UI_SCALE, 60 * UI_SCALE));
ImGui::SetCursorPos(ImVec2(100 * UI_SCALE, 60 * UI_SCALE));
ImGui::ProgressBar(downloaded_bytes / total_bytes, ImVec2(200 * UI_SCALE, 0));
} else {
sprintf(msg, "%.2f %s", format(downloaded_bytes), sizes[quota(downloaded_bytes)]);
pos = ImGui::CalcTextSize(msg);
ImGui::SetCursorPos(ImVec2((win_size * UI_SCALE - pos.x) / 2, 50 * UI_SCALE));
ImGui::SetCursorPos(ImVec2((400 * UI_SCALE - pos.x) / 2, 50 * UI_SCALE));
ImGui::Text(msg);
}

Expand All @@ -90,6 +87,36 @@ void DrawDownloaderScreen(int index, float downloaded_bytes, float total_bytes,
vglStopRendering();
}

void DrawDownloaderScreenCompat(float downloaded_bytes, float total_bytes, char *text) {
vglStartRendering();
ImGui_ImplVitaGL_NewFrame();

ImVec2 pos = ImGui::CalcTextSize(text);

ImGui::GetIO().MouseDrawCursor = false;
ImGui::SetNextWindowPos(ImVec2((SCR_WIDTH / 2) - 200 * UI_SCALE, (SCR_HEIGHT / 2) - 50 * UI_SCALE), ImGuiSetCond_Always);
ImGui::SetNextWindowSize(ImVec2(400 * UI_SCALE, 100 * UI_SCALE), ImGuiSetCond_Always);
ImGui::Begin("", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus);

ImGui::SetCursorPos(ImVec2((400 * UI_SCALE - pos.x) / 2, 20 * UI_SCALE));
ImGui::Text(text);

char msg[512];
sprintf(msg, "%.2f %s / %.2f %s", format(downloaded_bytes), sizes[quota(downloaded_bytes)], format(total_bytes), sizes[quota(total_bytes)]);
pos = ImGui::CalcTextSize(msg);
ImGui::SetCursorPos(ImVec2((400 * UI_SCALE - pos.x) / 2, 40 * UI_SCALE));
ImGui::Text(msg);
ImGui::SetCursorPos(ImVec2(100 * UI_SCALE, 60 * UI_SCALE));
ImGui::ProgressBar(downloaded_bytes / total_bytes, ImVec2(200 * UI_SCALE, 0));

ImGui::End();
glViewport(0, 0, static_cast<int>(ImGui::GetIO().DisplaySize.x), static_cast<int>(ImGui::GetIO().DisplaySize.y));
ImGui::Render();
ImGui_ImplVitaGL_RenderDrawData(ImGui::GetDrawData());
vglStopRendering();
sceKernelPowerTick(SCE_KERNEL_POWER_TICK_DEFAULT);
}

void DrawExtractorScreen(int index, float file_extracted_bytes, float extracted_bytes, float file_total_bytes, float total_bytes, char *filename, int num_files) {
vglStartRendering();
ImGui_ImplVitaGL_NewFrame();
Expand All @@ -99,23 +126,22 @@ void DrawExtractorScreen(int index, float file_extracted_bytes, float extracted_
sprintf(msg2, "%s (%.2f %s / %.2f %s)", filename, format(file_extracted_bytes), sizes[quota(file_extracted_bytes)], format(file_total_bytes), sizes[quota(file_total_bytes)]);
ImVec2 pos1 = ImGui::CalcTextSize(msg1);
ImVec2 pos2 = ImGui::CalcTextSize(msg2);
float win_size = MAX(pos1.x, pos2.x);
win_size = win_size > 400.0f ? win_size + 10.0f : 400.0f;

ImGui::GetIO().MouseDrawCursor = false;
ImGui::SetNextWindowPos(ImVec2((SCR_WIDTH / 2) - 200 * UI_SCALE, (SCR_HEIGHT / 2) - 50 * UI_SCALE), ImGuiSetCond_Always);
ImGui::SetNextWindowSize(ImVec2(win_size * UI_SCALE, 100 * UI_SCALE), ImGuiSetCond_Always);
ImGui::SetNextWindowSize(ImVec2(400 * UI_SCALE, 100 * UI_SCALE), ImGuiSetCond_Always);
ImGui::Begin("", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus);
ImGui::SetCursorPos(ImVec2((win_size * UI_SCALE - pos1.x) / 2, 20 * UI_SCALE));
ImGui::SetCursorPos(ImVec2((400 * UI_SCALE - pos1.x) / 2, 20 * UI_SCALE));
ImGui::Text(msg1);
ImGui::SetCursorPos(ImVec2((win_size * UI_SCALE - pos2.x) / 2, 40 * UI_SCALE));
ImGui::SetCursorPos(ImVec2((400 * UI_SCALE - pos2.x) / 2, 40 * UI_SCALE));
ImGui::Text(msg2);
ImGui::SetCursorPos(ImVec2((win_size / 4) * UI_SCALE, 60 * UI_SCALE));
ImGui::SetCursorPos(ImVec2(100 * UI_SCALE, 60 * UI_SCALE));
ImGui::ProgressBar(extracted_bytes / total_bytes, ImVec2(200 * UI_SCALE, 0));

ImGui::End();
glViewport(0, 0, static_cast<int>(ImGui::GetIO().DisplaySize.x), static_cast<int>(ImGui::GetIO().DisplaySize.y));
ImGui::Render();
ImGui_ImplVitaGL_RenderDrawData(ImGui::GetDrawData());
vglStopRendering();
sceKernelPowerTick(SCE_KERNEL_POWER_TICK_DEFAULT);
}
9 changes: 9 additions & 0 deletions Source/SysVita/UI/MainMenuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,15 @@ char *DrawRomSelector() {
DrawPendingDialog();
vglStopRendering();

if (pendingDownload) {
if (download_file("https://github.com/Rinnegatamante/DaedalusX64-vitaGL/releases/download/Nightly/DaedalusX64.zip", "ux0:data/temp.zip", lang_strings[STR_DLG_DOWNLOAD_DATA], 26 * 1024 * 1024) >= 0) {
extract_file("ux0:data/temp.zip", "ux0:data/");
sceIoRemove("ux0:data/temp.zip");
resetRomList();
}
pendingDownload = false;
}

if (selected) {
CheatCodes_Read( hovered->settings.GameName.c_str(), "Daedalus.cht", hovered->id.CountryID );
return selectedRom;
Expand Down
9 changes: 7 additions & 2 deletions Source/SysVita/UI/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ enum {
};

// Translation strings
#define LANG_STRINGS_NUM 145
#define LANG_STRINGS_NUM 147

#define FOREACH_STR(FUNC) \
FUNC(STR_DOWNLOADER_COMPAT_LIST) \
Expand Down Expand Up @@ -200,7 +200,9 @@ enum {
FUNC(STR_DLG_CUSTOM_PATH) \
FUNC(STR_SEARCH) \
FUNC(STR_DLG_SEARCH_ROM) \
FUNC(STR_EXTRACTING)
FUNC(STR_EXTRACTING) \
FUNC(STR_DOWNLOAD_DATA) \
FUNC(STR_DLG_DOWNLOAD_DATA)

#define GET_VALUE(x) x,
#define GET_STRING(x) #x,
Expand Down Expand Up @@ -268,13 +270,15 @@ extern char gCustomRomPath[256];

extern bool pendingDialog;
extern bool pendingAlert;
extern bool pendingDownload;
extern bool custom_path_str_dirty;

char *DrawRomSelector();
void DrawInGameMenu();
void DrawMenuBar();
void DrawInGameMenuBar();
void DrawDownloaderScreen(int index, float downloaded_bytes, float total_bytes, char *text, int passes);
void DrawDownloaderScreenCompat(float downloaded_bytes, float total_bytes, char *text);
void DrawExtractorScreen(int index, float file_extracted_bytes, float extracted_bytes, float file_total_bytes, float total_bytes, char *filename, int num_files);
void DrawPendingDialog();
void DrawPendingAlert();
Expand All @@ -293,5 +297,6 @@ void reloadFont();
void resetRomList();

void extract_file(char *file, char *dir);
int download_file(char *url, char *file, char *msg, float int_total_bytes);

void dummy_func();
6 changes: 6 additions & 0 deletions Source/SysVita/UI/MenuBarScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#define MAX_SAVESLOT 9

bool pendingDownload = false;

bool oldBigText = false;
int gLanguageIndex = SCE_SYSTEM_PARAM_LANG_ENGLISH_US;
int gUiTheme = DARK_THEME;
Expand Down Expand Up @@ -671,6 +673,10 @@ void DrawMenuBar() {
ImGui_ImplVitaGL_NewFrame();
if (ImGui::BeginMainMenuBar()){
if (ImGui::BeginMenu(lang_strings[STR_MENU_OPTIONS])) {
if (ImGui::MenuItem(lang_strings[STR_DOWNLOAD_DATA])) {
pendingDownload = true;
}
ImGui::Separator();
if (ImGui::MenuItem(custom_path_str)) {
showDialog(lang_strings[STR_DLG_CUSTOM_PATH], change_custom_rom_path, dummy_func, DIALOG_KEYBOARD);
}
Expand Down
2 changes: 2 additions & 0 deletions Source/SysVita/UI/TranslationStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,6 @@ char lang_strings[][256] = {
"Search: ", // STR_SEARCH
"Insert a Rom name filter", // STR_DLG_SEARCH_ROM
"Extracting archive", // STR_EXTRACTING
"Download Data Files", // STR_DOWNLOAD_DATA
"Downloading Data Files", // STR_DLG_DOWNLOAD_DATA
};
98 changes: 67 additions & 31 deletions Source/SysVita/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ char gCustomRomPath[256] = {0};

static char fname[512], ext_fname[512], read_buffer[8192];

static char *net_url = nullptr;

int console_language;

Dialog cur_dialog;
Expand Down Expand Up @@ -79,6 +81,8 @@ int gUseVSync = GL_TRUE;
bool pendingDialog = false;
bool pendingAlert = false;

void *net_memory = nullptr;

char boot_params[1024];

void recursive_mkdir(char *dir) {
Expand All @@ -100,7 +104,7 @@ void extract_file(char *file, char *dir) {
unzFile zipfile = unzOpen(file);
unzGetGlobalInfo(zipfile, &global_info);
unzGoToFirstFile(zipfile);
uint64_t total_extracted_bytes;
uint64_t total_extracted_bytes = 0;
uint64_t curr_extracted_bytes = 0;
uint64_t curr_file_bytes = 0;
int num_files = global_info.number_entry;
Expand Down Expand Up @@ -134,6 +138,7 @@ void extract_file(char *file, char *dir) {
if ((zip_idx + 1) < num_files) unzGoToNextFile(zipfile);
}
unzClose(zipfile);
ImGui::GetIO().MouseDrawCursor = true;
}

void log2file(const char *format, ...) {
Expand Down Expand Up @@ -212,7 +217,6 @@ static int compatListThread(unsigned int args, void* arg){
curl_handle = curl_easy_init();
for (int i = 1; i <= NUM_DB_CHUNKS; i++) {
downloader_pass = i;
sceKernelWaitSema(net_mutex, 1, NULL);
sprintf(dbname, "%sdb%ld.json", DAEDALUS_VITA_MAIN_PATH, i);
sprintf(url, "https://api.github.com/repos/Rinnegatamante/DaedalusX64-vitaGL-Compatibility/issues?state=open&page=%ld&per_page=100", i);
fh = fopen(TEMP_DOWNLOAD_NAME, "wb");
Expand All @@ -237,13 +241,27 @@ static int compatListThread(unsigned int args, void* arg){
return 0;
}

static int downloaderThread(unsigned int args, void* arg){
char url[512];
curl_handle = curl_easy_init();
sprintf(url, net_url);
fh = fopen(TEMP_DOWNLOAD_NAME, "wb");
downloaded_bytes = 0;
startDownload(url);
fclose(fh);
if (downloaded_bytes <= 12 * 1024)
sceIoRemove(TEMP_DOWNLOAD_NAME);
curl_easy_cleanup(curl_handle);
sceKernelExitDeleteThread(0);
return 0;
}

static int updaterThread(unsigned int args, void* arg){
uint8_t update_detected = 0;
char url[512];
curl_handle = curl_easy_init();
for (int i = UPDATER_CHECK_UPDATES; i < NUM_UPDATE_PASSES; i++) {
downloader_pass = i;
sceKernelWaitSema(net_mutex, 1, NULL);
if (i == UPDATER_CHECK_UPDATES) sprintf(url, "https://api.github.com/repos/Rinnegatamante/DaedalusX64-vitaGL/releases/latest");
else if (!update_detected) {
downloaded_bytes = total_bytes;
Expand Down Expand Up @@ -296,6 +314,48 @@ void reloadFont() {
ImGui::GetIO().Fonts->AddFontFromFileTTF("app0:/Roboto.ttf", 16.0f * UI_SCALE, NULL, ranges);
}

void start_net() {
sceSysmoduleLoadModule(SCE_SYSMODULE_NET);
int ret = sceNetShowNetstat();
SceNetInitParam initparam;
if (ret == SCE_NET_ERROR_ENOTINIT) {
net_memory = malloc(NET_INIT_SIZE);
initparam.memory = net_memory;
initparam.size = NET_INIT_SIZE;
initparam.flags = 0;
sceNetInit(&initparam);
}
}

int download_file(char *url, char *file, char *msg, float int_total_bytes) {
start_net();

SceKernelThreadInfo info;
info.size = sizeof(SceKernelThreadInfo);
int res = 0;
total_bytes = 0xFFFFFFFF;
downloaded_bytes = 0;
net_url = url;

SceUID thd = sceKernelCreateThread("Net Downloader", &downloaderThread, 0x10000100, 0x100000, 0, 0, NULL);
sceKernelStartThread(thd, 0, NULL);
do {
DrawDownloaderScreenCompat(downloaded_bytes, downloaded_bytes > int_total_bytes ? downloaded_bytes : int_total_bytes, msg);
res = sceKernelGetThreadInfo(thd, &info);
} while (info.status <= SCE_THREAD_STOPPED && res >= 0);

FILE *f = fopen(TEMP_DOWNLOAD_NAME, "r");
if (f) {
fclose(f);
sceIoRemove(file);
sceIoRename(TEMP_DOWNLOAD_NAME, file);
}else return -1;

ImGui::GetIO().MouseDrawCursor = true;

return 0;
}

static void Initialize()
{
sys_initialized = true;
Expand Down Expand Up @@ -327,21 +387,8 @@ static void Initialize()
uint8_t gSkipAutoUpdate = strstr(stringify(GIT_VERSION), "dirty") != nullptr;

// Initializing net
void *net_memory = nullptr;
if ((gAutoUpdate && !gSkipAutoUpdate) || !gSkipCompatListUpdate) {
net_mutex = sceKernelCreateSema("Net Mutex", 0, 0, 1, NULL);
sceSysmoduleLoadModule(SCE_SYSMODULE_NET);
int ret = sceNetShowNetstat();
SceNetInitParam initparam;
if (ret == SCE_NET_ERROR_ENOTINIT) {
net_memory = malloc(NET_INIT_SIZE);
initparam.memory = net_memory;
initparam.size = NET_INIT_SIZE;
initparam.flags = 0;
sceNetInit(&initparam);
}
sceNetCtlInit();
}
if ((gAutoUpdate && !gSkipAutoUpdate) || !gSkipCompatListUpdate)
start_net();

// Initializing vitaGL
vglInitExtended(0x100000, SCR_WIDTH, SCR_HEIGHT, 0x1800000, (SceGxmMultisampleMode)gAntiAliasing);
Expand All @@ -368,7 +415,6 @@ static void Initialize()
SceUID thd = sceKernelCreateThread("Auto Updater", &updaterThread, 0x10000100, 0x100000, 0, 0, NULL);
sceKernelStartThread(thd, 0, NULL);
do {
sceKernelSignalSema(net_mutex, 1);
if (downloader_pass == UPDATER_CHECK_UPDATES) DrawDownloaderScreen(downloader_pass + 1, downloaded_bytes, total_bytes, lang_strings[STR_DOWNLOADER_CHECK_UPDATE], NUM_UPDATE_PASSES);
else DrawDownloaderScreen(downloader_pass + 1, downloaded_bytes, total_bytes, lang_strings[STR_DOWNLOADER_UPDATE], NUM_UPDATE_PASSES);
res = sceKernelGetThreadInfo(thd, &info);
Expand All @@ -393,22 +439,12 @@ static void Initialize()
SceUID thd = sceKernelCreateThread("Compat List Updater", &compatListThread, 0x10000100, 0x100000, 0, 0, NULL);
sceKernelStartThread(thd, 0, NULL);
do {
sceKernelSignalSema(net_mutex, 1);
DrawDownloaderScreen(downloader_pass, downloaded_bytes, total_bytes, lang_strings[STR_DOWNLOADER_COMPAT_LIST], NUM_DB_CHUNKS);
res = sceKernelGetThreadInfo(thd, &info);
} while (info.status <= SCE_THREAD_STOPPED && res >= 0);
}

if ((gAutoUpdate && !gSkipAutoUpdate) || !gSkipCompatListUpdate) {
ImGui::GetIO().MouseDrawCursor = true;

// Closing net
sceNetCtlTerm();
sceNetTerm();
sceSysmoduleUnloadModule(SCE_SYSMODULE_NET);
free(net_memory);
sceKernelDeleteSema(net_mutex);
}

ImGui::GetIO().MouseDrawCursor = true;
}

void setCpuMode(int cpu_mode)
Expand Down

0 comments on commit f1c11bd

Please sign in to comment.