Skip to content

Commit f347767

Browse files
committed
Add macOS support
Rendering: - Run the GL 4.6 renderer on macOS through Mesa (Zink over KosmicKrisp). SDL3_MesaEGL_Context renders into an EGL pbuffer at the engine's render size and presents each frame through SDL's Metal renderer. - Bundle libEGL, libgallium, KosmicKrisp and the Vulkan loader into Build/<config>/mesa/lib at build time. Requires `brew install mesa vulkan-loader`. - Scale mouse coordinates between window and render size so clicks land correctly when the two differ, as in fullscreen. - Implement HardwareConfig.CanDoSRGBReadFromRTs, which only the macOS shader path calls. Native libraries: - Move libsteam_api.dylib to runtimes/osx/native so arm64 finds it. - Use Homebrew's arm64 FreeImage on Apple Silicon, since the NuGet package only ships x86_64. - Implement GetSystemFontPath on macOS through CoreText. Cross-platform fixes: - Fix font fallback "NULL" entries being compared by reference, which looped forever when no fallback font could be loaded. - Use the invariant culture process-wide so data files parse correctly on locales with a comma decimal separator. - Resolve the mod directory, relative search paths and the platform directory against AppContext.BaseDirectory instead of the working directory, so launching from outside the build output works. - Find the Garry's Mod install through the macOS Steam library path. - Skip loading Chromium on macOS when the CEF framework is missing, instead of searching the working directory tree.
1 parent 6be510d commit f347767

16 files changed

Lines changed: 478 additions & 17 deletions

File tree

‎Game.Assets/Assets.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public static List<AssetMapping> GetOptionalAssets() {
8080
}
8181
}
8282
}
83+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
84+
string p = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library", "Application Support", "Steam");
85+
if (Directory.Exists(p))
86+
steamPath = p;
87+
}
8388

8489
if (string.IsNullOrEmpty(steamPath))
8590
return null;
File renamed without changes.

‎Game.UI/GameUI.cs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,7 @@ public bool SetShowProgressText(bool show) {
157157
private bool FindPlatformDirectory(out ReadOnlySpan<char> platformDir) {
158158
platformDir = string.Empty;
159159

160-
#if WIN32
161-
string? dir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
162-
#else
163-
string? dir = Directory.GetCurrentDirectory();
164-
#endif
160+
string? dir = AppContext.BaseDirectory;
165161
if (string.IsNullOrEmpty(dir))
166162
return false;
167163

‎HTML.CEF/CefSystem.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public bool Init(ReadOnlySpan<char> pBaseDir, IHtmlResourceHandler resourceHandl
2929
if (OperatingSystem.IsWindows() && !LowIntegrityLabel.Apply(rootCachePath))
3030
resourceHandler.Message($"Chromium: failed to set a low integrity label on {rootCachePath}, cookies and storage will not persist.\n");
3131

32+
if (OperatingSystem.IsMacOS() && !Directory.Exists(Path.Combine(chromiumDir, "Chromium Embedded Framework.framework"))) {
33+
resourceHandler.Message($"Chromium failed to load: Chromium Embedded Framework.framework is missing from {chromiumDir}\n");
34+
return false;
35+
}
36+
3237
try {
3338
if (OperatingSystem.IsWindows())
3439
CefRuntime.Load(chromiumDir);

‎Source.Common/Platform.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Source.Common;
22

33
using System.Diagnostics;
4+
using System.Globalization;
45
using System.Runtime.InteropServices;
56

67
namespace Source;
@@ -56,6 +57,10 @@ public static unsafe void DebugString(ReadOnlySpan<char> buf) {
5657
}
5758

5859
public static void Initialize() {
60+
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
61+
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
62+
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
63+
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
5964
ThreadUtils.SetMainThread();
6065
}
6166
}

‎Source.Engine/Common.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void InitFilesystem(ReadOnlySpan<char> fullModPath) {
103103
FileSystem FileSystem = providers.GetRequiredService<FileSystem>();
104104

105105
initInfo.FileSystem = engineAPI.GetRequiredService<IFileSystem>();
106-
initInfo.DirectoryName = new(fullModPath);
106+
initInfo.DirectoryName = fullModPath.IsEmpty ? "" : Path.GetFullPath(new string(fullModPath), AppContext.BaseDirectory);
107107
if (initInfo.DirectoryName == null)
108108
initInfo.DirectoryName = Host.GetCurrentGame();
109109

‎Source.Engine/Source.Engine.csproj‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
<PackageReference Include="FreeImage-dotnet-core" ExcludeAssets="native" />
2121
<None Include="$(NuGetPackageRoot)freeimage-dotnet-core\4.3.6\runtimes\win7-x64\native\FreeImage.dll" Link="FreeImage.dll" CopyToOutputDirectory="PreserveNewest" Visible="false" Condition="$([MSBuild]::IsOSPlatform('Windows')) And Exists('$(NuGetPackageRoot)freeimage-dotnet-core\4.3.6\runtimes\win7-x64\native\FreeImage.dll')" />
2222
<None Include="$(NuGetPackageRoot)freeimage-dotnet-core\4.3.6\runtimes\ubuntu.16.04-x64\native\FreeImage.so" Link="FreeImage.so" CopyToOutputDirectory="PreserveNewest" Visible="false" Condition="$([MSBuild]::IsOSPlatform('Linux')) And Exists('$(NuGetPackageRoot)freeimage-dotnet-core\4.3.6\runtimes\ubuntu.16.04-x64\native\FreeImage.so')" />
23-
<None Include="$(NuGetPackageRoot)freeimage-dotnet-core\4.3.6\runtimes\osx.10.10-x64\native\FreeImage.dylib" Link="FreeImage.dylib" CopyToOutputDirectory="PreserveNewest" Visible="false" Condition="$([MSBuild]::IsOSPlatform('OSX')) And Exists('$(NuGetPackageRoot)freeimage-dotnet-core\4.3.6\runtimes\osx.10.10-x64\native\FreeImage.dylib')" />
23+
<None Include="$(NuGetPackageRoot)freeimage-dotnet-core\4.3.6\runtimes\osx.10.10-x64\native\FreeImage.dylib" Link="FreeImage.dylib" CopyToOutputDirectory="PreserveNewest" Visible="false" Condition="$([MSBuild]::IsOSPlatform('OSX')) And '$(PlatformTarget)' != 'arm64' And Exists('$(NuGetPackageRoot)freeimage-dotnet-core\4.3.6\runtimes\osx.10.10-x64\native\FreeImage.dylib')" />
24+
<None Include="/opt/homebrew/opt/freeimage/lib/libfreeimage.dylib" Link="FreeImage.dylib" CopyToOutputDirectory="PreserveNewest" Visible="false" Condition="$([MSBuild]::IsOSPlatform('OSX')) And '$(PlatformTarget)' == 'arm64' And Exists('/opt/homebrew/opt/freeimage/lib/libfreeimage.dylib')" />
2425
</ItemGroup>
2526
<ItemGroup>
2627
<Folder Include="GarrysMod\" />

‎Source.Filesystem/BaseFileSystem.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private void AddMapPackFile(ReadOnlySpan<char> path, ReadOnlySpan<char> pathID,
101101
}
102102

103103
private void AddVPKFile(ReadOnlySpan<char> path, ReadOnlySpan<char> pathID, SearchPathAdd addType, PathGroupName groupName) {
104-
string newPath = Path.IsPathFullyQualified(path) ? new(path) : Path.GetFullPath(new(path));
104+
string newPath = Path.IsPathFullyQualified(path) ? new(path) : Path.GetFullPath(new(path), AppContext.BaseDirectory);
105105

106106
if (!SearchPaths.OpenOrCreateCollection(pathID, out SearchPathCollection collection)) {
107107
for (int i = 0, c = collection.Count; i < c; i++) {
@@ -136,7 +136,7 @@ private void AddSearchPathDiskInternal(ReadOnlySpan<char> path, ReadOnlySpan<cha
136136
case ".vpk": AddVPKFile(path, pathID, addType, groupName); return;
137137
}
138138

139-
string newPath = Path.IsPathFullyQualified(path) ? new(path) : Path.GetFullPath(new(path));
139+
string newPath = Path.IsPathFullyQualified(path) ? new(path) : Path.GetFullPath(new(path), AppContext.BaseDirectory);
140140
AddSeparatorAndFixPath(ref newPath);
141141

142142
if (!SearchPaths.OpenOrCreateCollection(pathID, out SearchPathCollection collection)) {

‎Source.Launcher/Source.Launcher.csproj‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@
5353
<Copy SourceFiles="@(CefRedistLinuxX64)" DestinationFiles="@(CefRedistLinuxX64->'$(OutDir)%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="true" />
5454
</Target>
5555

56+
<!-- macOS caps OpenGL at 4.1, so GL 4.6 runs on Mesa (Zink over KosmicKrisp). libEGL is repointed at the bundled libgallium so that Zink's @rpath lookup finds the bundled Vulkan loader. -->
57+
<PropertyGroup Condition="'$(IsOSX)'=='true'">
58+
<MesaPrefix Condition="'$(MesaPrefix)'==''">/opt/homebrew/opt/mesa</MesaPrefix>
59+
<VulkanLoaderPrefix Condition="'$(VulkanLoaderPrefix)'==''">/opt/homebrew/opt/vulkan-loader</VulkanLoaderPrefix>
60+
<MesaBundleDir>$(OutputPath)mesa/lib/</MesaBundleDir>
61+
</PropertyGroup>
62+
63+
<Target Name="SNWarnMissingMesa" AfterTargets="Build" Condition="'$(SNClientBuild)'=='true' And '$(IsOSX)'=='true' And (!Exists('$(MesaPrefix)/lib/libEGL.1.dylib') Or !Exists('$(VulkanLoaderPrefix)/lib/libvulkan.1.dylib'))">
64+
<Warning Text="Mesa or the Vulkan loader was not found, so the client cannot render on macOS. Run 'brew install mesa vulkan-loader' and rebuild." />
65+
</Target>
66+
67+
<ItemGroup Condition="'$(IsOSX)'=='true'">
68+
<MesaBundleFile Include="$(MesaPrefix)/lib/libEGL.1.dylib" />
69+
<MesaBundleFile Include="$(MesaPrefix)/lib/libgallium-*.dylib" />
70+
<MesaBundleFile Include="$(MesaPrefix)/lib/libvulkan_kosmickrisp.dylib" />
71+
<MesaBundleFile Include="$(VulkanLoaderPrefix)/lib/libvulkan.1.dylib" />
72+
</ItemGroup>
73+
74+
<Target Name="SNBundleMesa" AfterTargets="Build" Condition="'$(SNClientBuild)'=='true' And '$(IsOSX)'=='true' And Exists('$(MesaPrefix)/lib/libEGL.1.dylib') And Exists('$(VulkanLoaderPrefix)/lib/libvulkan.1.dylib')" Inputs="@(MesaBundleFile)" Outputs="$(MesaBundleDir)libEGL.1.dylib">
75+
<Copy SourceFiles="@(MesaBundleFile)" DestinationFolder="$(MesaBundleDir)" />
76+
<Exec WorkingDirectory="$(MesaBundleDir)" Command="old=`otool -L libEGL.1.dylib | awk '/libgallium/{print $1}'` &amp;&amp; install_name_tool -change &quot;$old&quot; &quot;@loader_path/`basename $old`&quot; libEGL.1.dylib 2&gt;/dev/null &amp;&amp; codesign -f -s - libEGL.1.dylib" />
77+
<WriteLinesToFile File="$(MesaBundleDir)kosmickrisp_icd.json" Lines="{ &quot;file_format_version&quot;: &quot;1.0.1&quot;, &quot;ICD&quot;: { &quot;library_path&quot;: &quot;./libvulkan_kosmickrisp.dylib&quot;, &quot;api_version&quot;: &quot;1.4.0&quot; } }" Overwrite="true" />
78+
</Target>
79+
5680
<!-- Copy LuaJIT (Game.Lua imports "lua51", which .NET resolves to liblua51.so on Linux) -->
5781
<ItemGroup>
5882
<None Include="$(MSBuildThisFileDirectory)..\Dependencies\LuaJIT\win64\lua51.dll" Link="lua51.dll" CopyToOutputDirectory="PreserveNewest" Visible="false" Condition="'$(IsWindows)'=='true'" />

‎Source.MaterialSystem/Surface/FontManager.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ internal void InitFontSettings() {
622622

623623
for (KeyValues? kv = fallbackFonts.GetFirstSubKey(); kv != null; kv = kv.GetNextKey()) {
624624
string? key = kv.Name == "NULL" ? null : kv.Name;
625-
string? value = kv.GetString() == "NULL" ? null : new(kv.GetString());
625+
string? value = kv.GetString().SequenceEqual("NULL") ? null : new(kv.GetString());
626626
FallbackFonts[ffidx++] = new(key, value);
627627
}
628628

0 commit comments

Comments
 (0)