Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions scripts/util/Platform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.IO;
using Godot;

namespace util;

public static class Platform
{
/// <summary>
/// Path to the running .app bundle on macOS, the executable lives at Rhythia.app/Contents/MacOS/Rhythia
/// </summary>
public static string MacAppBundlePath => Path.GetFullPath(Path.Combine(OS.GetExecutablePath(), "../../.."));

/// <summary>
/// Wraps a value in single quotes so it is passed to /bin/sh as one literal argument
/// </summary>
public static string ShellQuote(string value) => $"'{value.Replace("'", "'\\''")}'";

/// <summary>
/// Shell snippet that blocks until the current process has exited
/// </summary>
public static string WaitForExitScript => $"while kill -0 {OS.GetProcessId()} 2>/dev/null; do sleep 0.1; done";
}
1 change: 1 addition & 0 deletions scripts/util/Platform.cs.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://d0b88m1ycctvg
31 changes: 31 additions & 0 deletions scripts/util/Releases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Godot;
using Octokit;
using Updatum;
using util;

public class Releases
{
Expand Down Expand Up @@ -50,6 +51,36 @@ public static async Task<bool> InstallUpdateAsync(UpdatumDownloadedAsset asset)
return false;
}

if (OS.GetName() == "macOS")
{
return installMacUpdate(asset);
}

return await MANAGER.InstallUpdateAsync(asset);
}

// Updatum finds the .app through AppContext.BaseDirectory, which Godot points at Contents/Resources,
// so it would sync the update inside the running bundle instead of replacing it
private static bool installMacUpdate(UpdatumDownloadedAsset asset)
{
if (asset == null || !asset.FileExists)
{
return false;
}

string app = Platform.ShellQuote(Platform.MacAppBundlePath);
string zip = Platform.ShellQuote(asset.FilePath);

string script =
$"{Platform.WaitForExitScript}; "
+ $"tmp=$(mktemp -d) && ditto -x -k {zip} \"$tmp\" "
+ "&& new=$(find \"$tmp\" -maxdepth 1 -name '*.app' | head -n 1) && [ -n \"$new\" ] "
+ $"&& mv {app} \"$tmp/old.app\" && {{ ditto \"$new\" {app} || mv \"$tmp/old.app\" {app}; }}; "
+ $"xattr -dr com.apple.quarantine {app}; rm -rf \"$tmp\"; open {app}";

OS.CreateProcess("/bin/sh", ["-c", script]);
((SceneTree)Engine.GetMainLoop()).Quit();

return true;
}
}