Create openIniFile function in FileUtil to avoid duplicates

+ correction
This commit is contained in:
LunaMoo 2016-06-22 05:55:45 +02:00
parent 434c95700e
commit 24fd336e60
4 changed files with 55 additions and 93 deletions

View file

@ -680,6 +680,55 @@ void CopyDir(const std::string &source_path, const std::string &dest_path)
#endif
}
void openIniFile(const std::string fileName) {
std::string iniFile;
#if defined(_WIN32)
iniFile = fileName;
// Can't rely on a .txt file extension to auto-open in the right editor,
// so let's find notepad
wchar_t notepad_path[MAX_PATH + 1];
GetSystemDirectory(notepad_path, MAX_PATH);
wcscat(notepad_path, L"\\notepad.exe");
wchar_t ini_path[MAX_PATH + 1] = { 0 };
wcsncpy(ini_path, ConvertUTF8ToWString(iniFile).c_str(), MAX_PATH);
// Flip any slashes...
for (size_t i = 0; i < wcslen(ini_path); i++) {
if (ini_path[i] == '/')
ini_path[i] = '\\';
}
// One for the space, one for the null.
wchar_t command_line[MAX_PATH * 2 + 1 + 1];
wsprintf(command_line, L"%s %s", notepad_path, ini_path);
STARTUPINFO si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
si.wShowWindow = SW_SHOW;
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof(pi));
UINT retval = CreateProcess(0, command_line, 0, 0, 0, 0, 0, 0, &si, &pi);
if (!retval) {
ERROR_LOG(COMMON, "Failed creating notepad process");
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
#elif !defined(MOBILE_DEVICE)
#if defined(__APPLE__)
iniFile = "open ";
#else
iniFile = "xdg-open ";
#endif
iniFile.append(fileName);
NOTICE_LOG(BOOT, "Launching %s", iniFile.c_str());
int retval = system(iniFile.c_str());
if (retval != 0) {
ERROR_LOG(COMMON, "Failed to launch ini file");
}
#endif
}
// Returns the current directory
std::string GetCurrentDir()
{

View file

@ -113,6 +113,9 @@ std::string GetCurrentDir();
// Create directory and copy contents (does not overwrite existing files)
void CopyDir(const std::string &source_path, const std::string &dest_path);
// Opens ini file (cheats, texture replacements etc.)
void openIniFile(const std::string fileName);
// Set the current directory to given directory
bool SetCurrentDir(const std::string &directory);

View file

@ -172,57 +172,12 @@ UI::EventReturn CwCheatScreen::OnAddCheat(UI::EventParams &params) {
}
UI::EventReturn CwCheatScreen::OnEditCheatFile(UI::EventParams &params) {
std::string cheatFile;
g_Config.bReloadCheats = true;
if (MIPSComp::jit) {
MIPSComp::jit->ClearCache();
}
screenManager()->finishDialog(this, DR_OK);
#ifdef _WIN32
cheatFile = activeCheatFile;
// Can't rely on a .txt file extension to auto-open in the right editor,
// so let's find notepad
wchar_t notepad_path[MAX_PATH + 1];
GetSystemDirectory(notepad_path, MAX_PATH);
wcscat(notepad_path, L"\\notepad.exe");
wchar_t cheat_path[MAX_PATH + 1] = {0};
wcsncpy(cheat_path, ConvertUTF8ToWString(cheatFile).c_str(), MAX_PATH);
// Flip any slashes...
for (size_t i = 0; i < wcslen(cheat_path); i++) {
if (cheat_path[i] == '/')
cheat_path[i] = '\\';
}
// One for the space, one for the null.
wchar_t command_line[MAX_PATH * 2 + 1 + 1];
wsprintf(command_line, L"%s %s", notepad_path, cheat_path);
STARTUPINFO si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
si.wShowWindow = SW_SHOW;
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof(pi));
UINT retval = CreateProcess(0, command_line, 0, 0, 0, 0, 0, 0, &si, &pi);
if (!retval) {
ERROR_LOG(COMMON, "Failed creating notepad process");
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
#elif !defined(MOBILE_DEVICE)
#if defined(__APPLE__)
cheatFile = "open ";
#else
cheatFile = "xdg-open ";
#endif
cheatFile.append(activeCheatFile);
NOTICE_LOG(BOOT, "Launching %s", cheatFile.c_str());
int retval = system(cheatFile.c_str());
if (retval != 0) {
ERROR_LOG(COMMON, "Failed to launch cheat file");
}
#endif
File::openIniFile(activeCheatFile);
return UI::EVENT_DONE;
}

View file

@ -1158,7 +1158,7 @@ UI::EventReturn DeveloperToolsScreen::OnOpenTexturesIniFile(UI::EventParams &e)
fclose(f);
// Let's also write some defaults
std::fstream fs;
File::OpenCPPFile(fs, texturesDirectory + "textures.ini", std::ios::out);
File::OpenCPPFile(fs, texturesDirectory + "textures.ini", std::ios::out | std::ios::ate);
fs << "# This file is optional\n";
fs << "# for syntax explanation check:\n";
fs << "# - https://github.com/hrydgard/ppsspp/pull/8715 \n";
@ -1176,52 +1176,7 @@ UI::EventReturn DeveloperToolsScreen::OnOpenTexturesIniFile(UI::EventParams &e)
enabled_ = File::Exists(texturesDirectory + "textures.ini");
}
if (enabled_) {
std::string texturesIniFile;
#if defined(_WIN32)
texturesIniFile = texturesDirectory + "textures.ini";
// Can't rely on a .txt file extension to auto-open in the right editor,
// so let's find notepad
wchar_t notepad_path[MAX_PATH + 1];
GetSystemDirectory(notepad_path, MAX_PATH);
wcscat(notepad_path, L"\\notepad.exe");
wchar_t ini_path[MAX_PATH + 1] = { 0 };
wcsncpy(ini_path, ConvertUTF8ToWString(texturesIniFile).c_str(), MAX_PATH);
// Flip any slashes...
for (size_t i = 0; i < wcslen(ini_path); i++) {
if (ini_path[i] == '/')
ini_path[i] = '\\';
}
// One for the space, one for the null.
wchar_t command_line[MAX_PATH * 2 + 1 + 1];
wsprintf(command_line, L"%s %s", notepad_path, ini_path);
STARTUPINFO si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
si.wShowWindow = SW_SHOW;
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof(pi));
UINT retval = CreateProcess(0, command_line, 0, 0, 0, 0, 0, 0, &si, &pi);
if (!retval) {
ERROR_LOG(COMMON, "Failed creating notepad process");
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
#elif !defined(MOBILE_DEVICE)
#if defined(__APPLE__)
texturesIniFile = "open ";
#else
texturesIniFile = "xdg-open ";
#endif
texturesIniFile.append(texturesDirectory + "textures.ini");
NOTICE_LOG(BOOT, "Launching %s", texturesIniFile.c_str());
int retval = system(texturesIniFile.c_str());
if (retval != 0) {
ERROR_LOG(COMMON, "Failed to launch textures.ini file");
}
#endif
File::openIniFile(texturesDirectory + "textures.ini");
}
return UI::EVENT_DONE;
}