Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
Electric1447 committed Jul 11, 2021
1 parent 163af5c commit db4d7ab
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 78 deletions.
31 changes: 16 additions & 15 deletions src/utils/filesystem.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include "filesystem.hpp"

#include <psp2/kernel/clib.h>

#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <string>

#include "psp2/kernel/clib.h"
#include "filesystem.hpp"

bool hasEndSlash(std::string str) {
std::string slash = "/";
bool hasEndSlash(string str) {
string slash = "/";

if (slash.size() > slash.size()) return false;
return std::equal(slash.rbegin(), slash.rend(), str.rbegin());
return equal(slash.rbegin(), slash.rend(), str.rbegin());
}

int doesDirExist(const char* path) {
Expand All @@ -23,14 +24,14 @@ int doesDirExist(const char* path) {
}
}

int Filesystem::mkDir(std::string path) {
int Filesystem::mkDir(string path) {
if (!doesDirExist(path.c_str()))
sceIoMkdir(path.c_str(), 0777);

return 0;
}

std::string Filesystem::readFile(std::string file) {
string Filesystem::readFile(string file) {
int fd = sceIoOpen(file.c_str(), SCE_O_RDONLY, 0777);

int fileSize = sceIoLseek ( fd, 0, SCE_SEEK_END );
Expand All @@ -39,15 +40,15 @@ std::string Filesystem::readFile(std::string file) {
if (fd < 0)
return "";

std::string readString(fileSize, '\0');
string readString(fileSize, '\0');
sceIoRead(fd, &readString[0], fileSize);

sceIoClose(fd);

return readString;
}

bool Filesystem::fileExists(std::string path) {
bool Filesystem::fileExists(string path) {
if (int fd = sceIoOpen(path.c_str(), SCE_O_RDONLY, 0777)) {
sceIoClose(fd);
return true;
Expand All @@ -56,7 +57,7 @@ bool Filesystem::fileExists(std::string path) {
}
}

int Filesystem::writeFile(std::string file, std::string buf) {
int Filesystem::writeFile(string file, string buf) {
int fd = sceIoOpen(file.c_str(), SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777);

if (fd < 0)
Expand All @@ -68,7 +69,7 @@ int Filesystem::writeFile(std::string file, std::string buf) {
return written;
}

int Filesystem::copyFile(std::string src, std::string dst) {
int Filesystem::copyFile(string src, string dst) {

// The destination is a subfolder of the source folder
SceUID fdsrc = sceIoOpen(src.c_str(), SCE_O_RDONLY, 0);
Expand Down Expand Up @@ -128,7 +129,7 @@ int Filesystem::copyFile(std::string src, std::string dst) {
return 1;
}

int Filesystem::copyPath(std::string src, std::string dst) {
int Filesystem::copyPath(string src, string dst) {

SceUID dfd = sceIoDopen(src.c_str());
if (dfd >= 0) {
Expand Down Expand Up @@ -186,15 +187,15 @@ int Filesystem::copyPath(std::string src, std::string dst) {
return 1;
}

int Filesystem::removePath(std::string path) {
int Filesystem::removePath(string path) {
SceUID dfd = sceIoDopen(path.c_str());
if (dfd < 0)
return dfd;

SceIoDirent dir;
memset(&dir, 0, sizeof(SceIoDirent));
while (sceIoDread(dfd, &dir) > 0) {
std::string newString = path + (hasEndSlash(path) ? "" : "/") + dir.d_name;
string newString = path + (hasEndSlash(path) ? "" : "/") + dir.d_name;

if (SCE_S_ISDIR(dir.d_stat.st_mode)) {
Filesystem::removePath(newString);
Expand Down
20 changes: 13 additions & 7 deletions src/utils/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
#include <psp2/io/stat.h>
#include <psp2/io/dirent.h>
#include <psp2/io/fcntl.h>
#include <string>


#define SCE_ERROR_ERRNO_EEXIST 0x80010011
#define MAX_PATH_LENGTH 1024

#define TRANSFER_SIZE (128 * 1024)


using namespace std;


class Filesystem {
public:
static std::string readFile(std::string file);
static int writeFile(std::string file, std::string buf);
static int copyFile(std::string src, std::string dst);
static int copyPath(std::string src, std::string dst);
static int mkDir(std::string path);
static int removePath(std::string path);
static bool fileExists(std::string path);
static string readFile(string file);
static int writeFile(string file, string buf);
static int copyFile(string src, string dst);
static int copyPath(string src, string dst);
static int mkDir(string path);
static int removePath(string path);
static bool fileExists(string path);
};

int doesDirExist(const char* path);
Expand Down
11 changes: 3 additions & 8 deletions src/utils/search.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#include <algorithm>
#include "search.hpp"

#include <psp2/sysmodule.h>
#include <psp2/ime_dialog.h>
#include <string>

#include "search.hpp"
#include "json.hpp"
#include <algorithm>

using json = nlohmann::json;
using namespace std;

static int ime_dialog_running = 0;

Expand Down Expand Up @@ -150,4 +145,4 @@ json moveToTopJson(string titleid, json original) {
string toLowercase(string strToConvert) {
transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(), ::tolower);
return strToConvert;
}
}
7 changes: 5 additions & 2 deletions src/utils/search.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#pragma once

#include <string>
#include <psp2/ime_dialog.h>

#include "json.hpp"


#define IME_DIALOG_RESULT_NONE 0
#define IME_DIALOG_RESULT_RUNNING 1
#define IME_DIALOG_RESULT_FINISHED 2
#define IME_DIALOG_RESULT_CANCELED 3

using json = nlohmann::json;

using namespace std;
using json = nlohmann::json;


int initImeDialog(char *title, char *initial_text, int max_text_length);
uint16_t *getImeDialogInputTextUTF16();
Expand Down
35 changes: 17 additions & 18 deletions src/utils/vitaPackage.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
* Originally created by devnoname120 (https://github.com/devnoname120)
*/

#include "vitaPackage.h"

#include <psp2/appmgr.h>
Expand Down Expand Up @@ -209,7 +208,7 @@ int makeHeadBin() {
return 0;
}

VitaPackage::VitaPackage(const std::string vpk) : vpk_(vpk) {
VitaPackage::VitaPackage(const string vpk) : vpk_(vpk) {
SceSysmoduleOpt opt;
opt.flags = 0;
opt.result = (int *)&opt.flags;
Expand All @@ -227,41 +226,41 @@ VitaPackage::~VitaPackage() {

void VitaPackage::Extract() {

Filesystem::removePath(std::string(PACKAGE_TEMP_FOLDER));
Filesystem::removePath(string(PACKAGE_TEMP_FOLDER));

sceIoMkdir(PACKAGE_TEMP_FOLDER, 0777);

Zipfile vpk_file = Zipfile(vpk_);

vpk_file.Unzip(std::string(PACKAGE_TEMP_FOLDER));
vpk_file.Unzip(string(PACKAGE_TEMP_FOLDER));
sceIoRemove(vpk_.c_str());
}

int VitaPackage::InstallExtracted() {

int ret = makeHeadBin();
if (ret < 0)
throw std::runtime_error("Error faking app signature");
throw runtime_error("Error faking app signature");

ret = scePromoterUtilityPromotePkg(PACKAGE_TEMP_FOLDER, 0);
if (ret < 0)
throw std::runtime_error("Error installing app");
throw runtime_error("Error installing app");

int state = 0;
do {
ret = scePromoterUtilityGetState(&state);
if (ret < 0)
throw std::runtime_error("Error while instaling");
throw runtime_error("Error while instaling");

sceKernelDelayThread(150 * 1000);
} while (state);

int result = 0;
ret = scePromoterUtilityGetResult(&result);
if (ret < 0)
throw std::runtime_error("Installation failed");
throw runtime_error("Installation failed");

Filesystem::removePath(std::string(PACKAGE_TEMP_FOLDER));
Filesystem::removePath(string(PACKAGE_TEMP_FOLDER));

return 1;
}
Expand All @@ -273,21 +272,21 @@ int VitaPackage::Install() {

int UpdaterPackage::InstallUpdater() {

Filesystem::removePath(std::string(PACKAGE_TEMP_FOLDER));
Filesystem::removePath(string(PACKAGE_TEMP_FOLDER));

Filesystem::mkDir(std::string(PACKAGE_TEMP_FOLDER));
Filesystem::mkDir(std::string(UPDATER_DST_SFO_DIR));
Filesystem::mkDir(string(PACKAGE_TEMP_FOLDER));
Filesystem::mkDir(string(UPDATER_DST_SFO_DIR));

Filesystem::copyFile(std::string(UPDATER_SRC_EBOOT_PATH), std::string(UPDATER_DST_EBOOT_PATH));
Filesystem::copyFile(std::string(UPDATER_SRC_SFO_PATH), std::string(UPDATER_DST_SFO_PATH));
Filesystem::copyFile(string(UPDATER_SRC_EBOOT_PATH), string(UPDATER_DST_EBOOT_PATH));
Filesystem::copyFile(string(UPDATER_SRC_SFO_PATH), string(UPDATER_DST_SFO_PATH));

return InstallExtracted();
}

void UpdatePackage::MakeHeadBin() {
int ret = makeHeadBin();
if (ret < 0)
throw std::runtime_error("Error faking app signature");
throw runtime_error("Error faking app signature");
}

bool InstalledVitaPackage::IsInstalled() {
Expand All @@ -304,7 +303,7 @@ int InstalledVitaPackage::Uninstall() {
return scePromoterUtilityDeletePkg(title_id.c_str());
}

bool isPackageInstalled(std::string titleid) {
bool isPackageInstalled(string titleid) {
// FIXME Don't reload the module every time

// ScePaf is required for PromoterUtil
Expand All @@ -318,7 +317,7 @@ bool isPackageInstalled(std::string titleid) {

int ret = scePromoterUtilityInit();
if (ret < 0)
throw std::runtime_error("scePromoterUtilityInit() = " + ret);
throw runtime_error("scePromoterUtilityInit() = " + ret);

int res;
int installed = scePromoterUtilityCheckExist(titleid.c_str(), &res);
Expand All @@ -328,7 +327,7 @@ bool isPackageInstalled(std::string titleid) {
return installed >= 0;
}

void openApp(std::string titleid) {
void openApp(string titleid) {
char uri[32];
snprintf(uri, sizeof(uri), "psgm:play?titleid=%s", titleid.c_str());

Expand Down
18 changes: 10 additions & 8 deletions src/utils/vitaPackage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

#include <stdexcept>

#include "filesystem.hpp"

#ifndef PACKAGE_TEMP_FOLDER
#define PACKAGE_TEMP_FOLDER "ux0:/temp/pkg/"
#endif


using namespace std;


class VitaPackage{
public:
explicit VitaPackage(std::string vpk);
explicit VitaPackage(string vpk);
~VitaPackage();

int Install();
void Extract();
int InstallExtracted();

private:
std::string vpk_;
string vpk_;
};

class UpdaterPackage : private VitaPackage {
Expand All @@ -31,23 +33,23 @@ class UpdaterPackage : private VitaPackage {

class UpdatePackage : private VitaPackage {
public:
explicit UpdatePackage(std::string vpk) : VitaPackage(vpk) {};
explicit UpdatePackage(string vpk) : VitaPackage(vpk) {};

void Extract() { VitaPackage::Extract(); }
void MakeHeadBin();
};

class InstalledVitaPackage : private VitaPackage {
public:
explicit InstalledVitaPackage(std::string title_id) : VitaPackage(""), title_id(std::move(title_id)) {}
explicit InstalledVitaPackage(string title_id) : VitaPackage(""), title_id(move(title_id)) {}

bool IsInstalled();

int Uninstall();

private:
std::string title_id;
string title_id;
};

bool isPackageInstalled(std::string titleid);
void openApp(std::string titleid);
bool isPackageInstalled(string titleid);
void openApp(string titleid);
Loading

0 comments on commit db4d7ab

Please sign in to comment.