Skip to content

Commit e2769a4

Browse files
committed
Fixing various utf-8 issues + path to binary lookup.
Should fix #1600.
1 parent d8a4a56 commit e2769a4

File tree

10 files changed

+184
-4
lines changed

10 files changed

+184
-4
lines changed

src/gui/gui.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ void PCSX::GUI::init(std::function<void()> applyArguments) {
545545
if (vg) {
546546
g_system->findResource(
547547
[vg](auto path) -> bool {
548-
int res = nvgCreateFont(vg, "noto-sans-regular", path.string().c_str());
548+
int res = nvgCreateFont(vg, "noto-sans-regular", (const char *)(path.u8string().c_str()));
549549
return res >= 0;
550550
},
551551
MAKEU8("NotoSans-Regular.ttf"), "fonts", std::filesystem::path("third_party") / "noto");

src/main/main.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "lua/luawrapper.h"
3838
#include "main/textui.h"
3939
#include "spu/interface.h"
40+
#include "support/binpath.h"
4041
#include "support/uvfile.h"
4142
#include "support/version.h"
4243
#include "tracy/Tracy.hpp"
@@ -194,7 +195,7 @@ int pcsxMain(int argc, char **argv) {
194195
const PCSX::u8string logfileArg = MAKEU8(logfileArgOpt.has_value() ? logfileArgOpt->c_str() : "");
195196
if (!logfileArg.empty()) system->useLogfile(logfileArg);
196197
PCSX::g_system = system;
197-
std::filesystem::path self = argv[0];
198+
std::filesystem::path self = PCSX::BinPath::getExecutablePath();
198199
std::filesystem::path binDir = std::filesystem::absolute(self).parent_path();
199200
system->setBinDir(binDir);
200201
system->loadAllLocales();

src/support/binpath-linux.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2024 PCSX-Redux authors
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
#include "binpath.h"
28+
29+
#ifdef __linux__
30+
31+
#include <unistd.h>
32+
33+
std::u8string PCSX::BinPath::getExecutablePath() {
34+
char8_t buffer[BUFSIZ];
35+
readlink("/proc/self/exe", (char*)buffer, BUFSIZ);
36+
return std::u8string(buffer);
37+
}
38+
39+
#endif

src/support/binpath-macos.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2024 PCSX-Redux authors
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
#include "binpath.h"
28+
29+
#if defined(__APPLE__) && defined(__MACH__)
30+
31+
#include <CoreFoundation/CoreFoundation.h>
32+
33+
#include <stdexcept>
34+
35+
std::u8string PCSX::BinPath::getExecutablePath() {
36+
CFBundleRef mainBundle = CFBundleGetMainBundle();
37+
CFURLRef execURL = CFBundleCopyExecutableURL(mainBundle);
38+
char8_t path[PATH_MAX];
39+
if (!CFURLGetFileSystemRepresentation(execURL, TRUE, (UInt8 *)path, PATH_MAX)) {
40+
throw std::runtime_error("Could not get executable path");
41+
}
42+
CFRelease(execURL);
43+
return std::u8string(path);
44+
}
45+
46+
#endif

src/support/binpath-windows.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2024 PCSX-Redux authors
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
// Needs to stay on top
28+
#include "windowswrapper.h"
29+
// Because MSVC is a special snowflake
30+
#include "binpath.h"
31+
32+
#ifdef _WIN32
33+
34+
std::u8string PCSX::BinPath::getExecutablePath() {
35+
wchar_t path[MAX_PATH];
36+
GetModuleFileNameW(nullptr, path, MAX_PATH);
37+
auto needed = WideCharToMultiByte(CP_UTF8, 0, path, -1, NULL, 0, NULL, NULL);
38+
std::u8string result(needed, 0);
39+
WideCharToMultiByte(CP_UTF8, 0, path, -1, (LPSTR)result.data(), needed, NULL, NULL);
40+
return result;
41+
}
42+
43+
#endif

src/support/binpath.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2024 PCSX-Redux authors
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
#pragma once
28+
29+
#include <string>
30+
31+
namespace PCSX::BinPath {
32+
33+
std::u8string getExecutablePath();
34+
35+
} // namespace PCSX::BinPath

src/support/file.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static FILE *openwrapper(const char *filename, const wchar_t *mode) {
171171
int needed = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
172172
if (needed <= 0) return nullptr;
173173
LPWSTR str = (LPWSTR)_malloca(needed * sizeof(wchar_t));
174-
MultiByteToWideChar(CP_UTF8, 0, filename, -1, str, needed * sizeof(wchar_t));
174+
MultiByteToWideChar(CP_UTF8, 0, filename, -1, str, needed);
175175
FILE *ret = _wfopen(str, mode);
176176
_freea(str);
177177
return ret;

third_party/zep

vsprojects/support/support.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
<ItemGroup>
152152
<ClInclude Include="..\..\src\mips\common\util\sjis-table.h" />
153153
<ClInclude Include="..\..\src\support\bezier.h" />
154+
<ClInclude Include="..\..\src\support\binpath.h" />
154155
<ClInclude Include="..\..\src\support\binstruct.h" />
155156
<ClInclude Include="..\..\src\support\circular.h" />
156157
<ClInclude Include="..\..\src\support\container-file.h" />
@@ -202,6 +203,9 @@
202203
<ClInclude Include="..\..\third_party\typestring.hh" />
203204
</ItemGroup>
204205
<ItemGroup>
206+
<ClCompile Include="..\..\src\support\binpath-linux.cc" />
207+
<ClCompile Include="..\..\src\support\binpath-macos.cc" />
208+
<ClCompile Include="..\..\src\support\binpath-windows.cc" />
205209
<ClCompile Include="..\..\src\support\container-file.cc" />
206210
<ClCompile Include="..\..\src\support\ffmpeg-audio-file.cc" />
207211
<ClCompile Include="..\..\src\support\file.cc" />

vsprojects/support/support.vcxproj.filters

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@
171171
<ClInclude Include="..\..\src\support\table-generator.h">
172172
<Filter>Header Files</Filter>
173173
</ClInclude>
174+
<ClInclude Include="..\..\src\support\binpath.h">
175+
<Filter>Header Files</Filter>
176+
</ClInclude>
174177
</ItemGroup>
175178
<ItemGroup>
176179
<ClCompile Include="..\..\src\support\file.cc">
@@ -224,6 +227,15 @@
224227
<ClCompile Include="..\..\src\support\sharedmem.cc">
225228
<Filter>Source Files</Filter>
226229
</ClCompile>
230+
<ClCompile Include="..\..\src\support\binpath-linux.cc">
231+
<Filter>Source Files</Filter>
232+
</ClCompile>
233+
<ClCompile Include="..\..\src\support\binpath-macos.cc">
234+
<Filter>Source Files</Filter>
235+
</ClCompile>
236+
<ClCompile Include="..\..\src\support\binpath-windows.cc">
237+
<Filter>Source Files</Filter>
238+
</ClCompile>
227239
</ItemGroup>
228240
<ItemGroup>
229241
<None Include="packages.config" />

0 commit comments

Comments
 (0)