Skip to content

Commit

Permalink
- add openscreenshots opensaves and openconfig console commands…
Browse files Browse the repository at this point in the history
… on Windows and Linux and Mac
  • Loading branch information
madame-rachelle authored and Rachael Alexanderson committed Aug 10, 2022
1 parent 4710a40 commit 82d0376
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/common/console/c_enginecmds.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}

15 changes: 15 additions & 0 deletions src/common/platform/posix/cocoa/i_system.mm
Expand Up @@ -170,3 +170,18 @@ unsigned int I_MakeRNGSeed()
{
return static_cast<unsigned int>(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());
}
16 changes: 16 additions & 0 deletions src/common/platform/posix/sdl/i_system.cpp
Expand Up @@ -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());
}

2 changes: 1 addition & 1 deletion src/common/platform/win32/i_specialpaths.cpp
Expand Up @@ -283,7 +283,7 @@ FString M_GetScreenshotsPath()

if (!UseKnownFolders())
{
path << progdir << "/Screenshots/";
path << progdir << "Screenshots/";
}
else if (GetKnownFolder(-1, MyFOLDERID_Screenshots, true, path))
{
Expand Down
52 changes: 52 additions & 0 deletions src/common/platform/win32/i_system.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -49,6 +50,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdexcept>
#include <process.h>
#include <time.h>
#include <map>
Expand All @@ -62,6 +64,8 @@
#include <wincrypt.h>
#include <shlwapi.h>

#include <shellapi.h>

#include "hardware.h"
#include "printf.h"

Expand Down Expand Up @@ -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);
}

2 comments on commit 82d0376

@coelckers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add yet another UTF-8 conversion function? We already got one in zstring.h

@madame-rachelle
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 04a6fa3

Please sign in to comment.