From 82d03765206774314a2c61af6bb4331cb6a01fda Mon Sep 17 00:00:00 2001 From: Rachael Alexanderson Date: Wed, 10 Aug 2022 12:06:03 -0400 Subject: [PATCH 1/4] - add `openscreenshots` `opensaves` and `openconfig` console commands on Windows and Linux and Mac --- src/common/console/c_enginecmds.cpp | 20 ++++++++ src/common/platform/posix/cocoa/i_system.mm | 15 ++++++ src/common/platform/posix/sdl/i_system.cpp | 16 ++++++ src/common/platform/win32/i_specialpaths.cpp | 2 +- src/common/platform/win32/i_system.cpp | 52 ++++++++++++++++++++ 5 files changed, 104 insertions(+), 1 deletion(-) diff --git a/src/common/console/c_enginecmds.cpp b/src/common/console/c_enginecmds.cpp index 0af6f3b56a5..4ce05c8169a 100644 --- a/src/common/console/c_enginecmds.cpp +++ b/src/common/console/c_enginecmds.cpp @@ -52,6 +52,10 @@ #include "version.h" #include "findfile.h" #include "md5.h" +#include "i_specialpaths.h" + +void I_OpenShellFolder(const char*); +void I_OpenShellFile(const char*); extern FILE* Logfile; @@ -336,3 +340,19 @@ CCMD(printlocalized) } } + +UNSAFE_CCMD(opensaves) +{ + I_OpenShellFolder(M_GetSavegamesPath().GetChars()); +} + +UNSAFE_CCMD(openscreenshots) +{ + I_OpenShellFolder(M_GetScreenshotsPath().GetChars()); +} + +UNSAFE_CCMD(openconfig) +{ + I_OpenShellFile(M_GetConfigPath(false).GetChars()); +} + diff --git a/src/common/platform/posix/cocoa/i_system.mm b/src/common/platform/posix/cocoa/i_system.mm index 76a38cbcf73..02cad154ab3 100644 --- a/src/common/platform/posix/cocoa/i_system.mm +++ b/src/common/platform/posix/cocoa/i_system.mm @@ -170,3 +170,18 @@ unsigned int I_MakeRNGSeed() { return static_cast(arc4random()); } + +void I_OpenShellFolder(const char* folder) +{ + std::string x = (std::string)"open " + (std::string)folder; + Printf("Opening folder: %s\n", folder); + std::system(x.c_str()); +} + +void I_OpenShellFile(const char* file) +{ + std::string x = (std::string)"open " + (std::string)file; + x.erase(x.find_last_of('/'), std::string::npos); + Printf("Opening folder to file: %s\n", file); + std::system(x.c_str()); +} diff --git a/src/common/platform/posix/sdl/i_system.cpp b/src/common/platform/posix/sdl/i_system.cpp index a63b15a9f2a..3dd43e98d80 100644 --- a/src/common/platform/posix/sdl/i_system.cpp +++ b/src/common/platform/posix/sdl/i_system.cpp @@ -431,3 +431,19 @@ unsigned int I_MakeRNGSeed() } return seed; } + +void I_OpenShellFolder(const char* folder) +{ + std::string x = (std::string)"xdg-open " + (std::string)folder; + Printf("Opening folder: %s\n", folder); + std::system(x.c_str()); +} + +void I_OpenShellFile(const char* file) +{ + std::string x = (std::string)"xdg-open " + (std::string)file; + x.erase(x.find_last_of('/'), std::string::npos); + Printf("Opening folder to file: %s\n", file); + std::system(x.c_str()); +} + diff --git a/src/common/platform/win32/i_specialpaths.cpp b/src/common/platform/win32/i_specialpaths.cpp index bbd3c4976ba..777d4fc63ac 100644 --- a/src/common/platform/win32/i_specialpaths.cpp +++ b/src/common/platform/win32/i_specialpaths.cpp @@ -283,7 +283,7 @@ FString M_GetScreenshotsPath() if (!UseKnownFolders()) { - path << progdir << "/Screenshots/"; + path << progdir << "Screenshots/"; } else if (GetKnownFolder(-1, MyFOLDERID_Screenshots, true, path)) { diff --git a/src/common/platform/win32/i_system.cpp b/src/common/platform/win32/i_system.cpp index 5459b7458c7..554bdddbcaa 100644 --- a/src/common/platform/win32/i_system.cpp +++ b/src/common/platform/win32/i_system.cpp @@ -6,6 +6,7 @@ ** Copyright 1998-2009 Randy Heit ** Copyright (C) 2007-2012 Skulltag Development Team ** Copyright (C) 2007-2016 Zandronum Development Team +** Copyright (C) 2017-2022 GZDoom Development Team ** All rights reserved. ** ** Redistribution and use in source and binary forms, with or without @@ -49,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -62,6 +64,8 @@ #include #include +#include + #include "hardware.h" #include "printf.h" @@ -955,3 +959,51 @@ void I_SetThreadNumaNode(std::thread &thread, int numaNode) SetThreadAffinityMask(handle, (DWORD_PTR)numaNodes[numaNode].affinityMask); } } + +std::wstring ToUtf16(const std::string& str) +{ + if (str.empty()) return {}; + int needed = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0); + if (needed == 0) + throw std::runtime_error("MultiByteToWideChar failed"); + std::wstring result; + result.resize(needed); + needed = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &result[0], (int)result.size()); + if (needed == 0) + throw std::runtime_error("MultiByteToWideChar failed"); + return result; +} + +/* // not currently used, but here for reference if it is needed +std::string FromUtf16(const std::wstring& str) +{ + if (str.empty()) return {}; + int needed = WideCharToMultiByte(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0, nullptr, nullptr); + if (needed == 0) + throw std::runtime_error("WideCharToMultiByte failed"); + std::string result; + result.resize(needed); + needed = WideCharToMultiByte(CP_UTF8, 0, str.data(), (int)str.size(), &result[0], (int)result.size(), nullptr, nullptr); + if (needed == 0) + throw std::runtime_error("WideCharToMultiByte failed"); + return result; +} +*/ + +void I_OpenShellFolder(const char* folder) +{ + std::string x = folder; + for (auto& c : x) { if (c == '/') c = '\\'; } + Printf("Opening folder: %s\n", x.c_str()); + ShellExecuteW(NULL, L"open", L"explorer.exe", ToUtf16(x).c_str(), NULL, SW_SHOWNORMAL); +} + +void I_OpenShellFile(const char* file) +{ + std::string x = (std::string)"/select," + (std::string)file; + for (auto& c : x) { if (c == '/') c = '\\'; } + x[0] = '/'; + Printf("Opening folder to file: %s\n", x.c_str()); + ShellExecuteW(NULL, L"open", L"explorer.exe", ToUtf16(x).c_str(), NULL, SW_SHOWNORMAL); +} + From f2df781b763031f864b18c03e2ec25ab248fd81b Mon Sep 17 00:00:00 2001 From: Rachael Alexanderson Date: Wed, 10 Aug 2022 13:10:13 -0400 Subject: [PATCH 2/4] - add menu entries --- src/common/console/c_enginecmds.cpp | 6 +++--- wadsrc/static/menudef.txt | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/common/console/c_enginecmds.cpp b/src/common/console/c_enginecmds.cpp index 4ce05c8169a..0845ac82273 100644 --- a/src/common/console/c_enginecmds.cpp +++ b/src/common/console/c_enginecmds.cpp @@ -341,17 +341,17 @@ CCMD(printlocalized) } -UNSAFE_CCMD(opensaves) +CCMD(opensaves) { I_OpenShellFolder(M_GetSavegamesPath().GetChars()); } -UNSAFE_CCMD(openscreenshots) +CCMD(openscreenshots) { I_OpenShellFolder(M_GetScreenshotsPath().GetChars()); } -UNSAFE_CCMD(openconfig) +CCMD(openconfig) { I_OpenShellFile(M_GetConfigPath(false).GetChars()); } diff --git a/wadsrc/static/menudef.txt b/wadsrc/static/menudef.txt index a6944789457..d8024a6465a 100644 --- a/wadsrc/static/menudef.txt +++ b/wadsrc/static/menudef.txt @@ -380,6 +380,9 @@ OptionMenu "OptionsMenu" protected SafeCommand "$OPTMNU_WRITEINI", "writeini" Command "$OPTMNU_CONSOLE", "menuconsole" + Command "$OPTMNU_OPENCONFIG", "openconfig" + Command "$OPTMNU_OPENSCREENSHOTS", "openscreenshots" + Command "$OPTMNU_OPENSAVES", "opensaves" StaticText " " } From 04a6fa321b270ae193e86ad343489775727c1565 Mon Sep 17 00:00:00 2001 From: Rachael Alexanderson Date: Wed, 10 Aug 2022 18:13:49 -0400 Subject: [PATCH 3/4] - use FString instead of std::string in the Windows shell functions --- src/common/platform/win32/i_system.cpp | 48 +++++--------------------- 1 file changed, 9 insertions(+), 39 deletions(-) diff --git a/src/common/platform/win32/i_system.cpp b/src/common/platform/win32/i_system.cpp index 554bdddbcaa..d9dcd221d5e 100644 --- a/src/common/platform/win32/i_system.cpp +++ b/src/common/platform/win32/i_system.cpp @@ -960,50 +960,20 @@ void I_SetThreadNumaNode(std::thread &thread, int numaNode) } } -std::wstring ToUtf16(const std::string& str) -{ - if (str.empty()) return {}; - int needed = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0); - if (needed == 0) - throw std::runtime_error("MultiByteToWideChar failed"); - std::wstring result; - result.resize(needed); - needed = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &result[0], (int)result.size()); - if (needed == 0) - throw std::runtime_error("MultiByteToWideChar failed"); - return result; -} - -/* // not currently used, but here for reference if it is needed -std::string FromUtf16(const std::wstring& str) -{ - if (str.empty()) return {}; - int needed = WideCharToMultiByte(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0, nullptr, nullptr); - if (needed == 0) - throw std::runtime_error("WideCharToMultiByte failed"); - std::string result; - result.resize(needed); - needed = WideCharToMultiByte(CP_UTF8, 0, str.data(), (int)str.size(), &result[0], (int)result.size(), nullptr, nullptr); - if (needed == 0) - throw std::runtime_error("WideCharToMultiByte failed"); - return result; -} -*/ - void I_OpenShellFolder(const char* folder) { - std::string x = folder; - for (auto& c : x) { if (c == '/') c = '\\'; } - Printf("Opening folder: %s\n", x.c_str()); - ShellExecuteW(NULL, L"open", L"explorer.exe", ToUtf16(x).c_str(), NULL, SW_SHOWNORMAL); + FString proc = folder; + proc.ReplaceChars('/', '\\'); + Printf("Opening folder: %s\n", proc.GetChars()); + ShellExecuteW(NULL, L"open", L"explorer.exe", proc.WideString().c_str(), NULL, SW_SHOWNORMAL); } void I_OpenShellFile(const char* file) { - std::string x = (std::string)"/select," + (std::string)file; - for (auto& c : x) { if (c == '/') c = '\\'; } - x[0] = '/'; - Printf("Opening folder to file: %s\n", x.c_str()); - ShellExecuteW(NULL, L"open", L"explorer.exe", ToUtf16(x).c_str(), NULL, SW_SHOWNORMAL); + FString proc = file; + proc.ReplaceChars('/', '\\'); + Printf("Opening folder to file: %s\n", proc.GetChars()); + proc.Format("/select,%s", proc.GetChars()); + ShellExecuteW(NULL, L"open", L"explorer.exe", proc.WideString().c_str(), NULL, SW_SHOWNORMAL); } From 8e50c59301b73ce2cabb668d53559fd533a58169 Mon Sep 17 00:00:00 2001 From: Rachael Alexanderson Date: Thu, 11 Aug 2022 15:43:22 -0400 Subject: [PATCH 4/4] - remove latch flag from sv_cheats --- src/console/c_cmds.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/console/c_cmds.cpp b/src/console/c_cmds.cpp index a45bc1ccae9..4037f818967 100644 --- a/src/console/c_cmds.cpp +++ b/src/console/c_cmds.cpp @@ -73,7 +73,7 @@ extern FILE *Logfile; extern bool insave; -CVAR (Bool, sv_cheats, false, CVAR_SERVERINFO | CVAR_LATCH) +CVAR (Bool, sv_cheats, false, CVAR_SERVERINFO) CVAR (Bool, sv_unlimited_pickup, false, CVAR_SERVERINFO) CVAR (Int, cl_blockcheats, 0, 0)