|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Data.SqlTypes; |
| 4 | +using System.Diagnostics; |
3 | 5 | using System.IO;
|
4 | 6 | using Avalonia;
|
5 | 7 | using Avalonia.Controls;
|
6 | 8 | using Avalonia.Markup.Xaml;
|
7 | 9 | using Avalonia.Media.Imaging;
|
8 | 10 | using Avalonia.Platform;
|
9 | 11 | using JetBrains.Annotations;
|
| 12 | +using Microsoft.Win32; |
10 | 13 | using Serilog;
|
11 | 14 | using SS14.Launcher.Models.OverrideAssets;
|
| 15 | +using static System.Diagnostics.Process; |
| 16 | +using static System.Environment.SpecialFolder; |
12 | 17 |
|
13 | 18 | namespace SS14.Launcher;
|
14 | 19 |
|
@@ -44,6 +49,8 @@ public override void Initialize()
|
44 | 49 | IconsLoader.Load(this);
|
45 | 50 |
|
46 | 51 | _overrideAssets.AssetsChanged += OnAssetsChanged;
|
| 52 | + |
| 53 | + RegisterProtocol(); |
47 | 54 | }
|
48 | 55 |
|
49 | 56 | private void LoadBaseAssets()
|
@@ -93,6 +100,77 @@ private static object LoadAsset(AssetType type, Stream data)
|
93 | 100 | };
|
94 | 101 | }
|
95 | 102 |
|
| 103 | + private void RegisterProtocol() |
| 104 | + { |
| 105 | + List<string> protocols = new() { "ss14", "ss14s" }; |
| 106 | + |
| 107 | + #if WINDOWS |
| 108 | + Log.Information("Registering SS14 protocol handler for Windows"); |
| 109 | + foreach (var protocol in protocols) |
| 110 | + { |
| 111 | + var key = Registry.CurrentUser.CreateSubKey($@"SOFTWARE\Classes\{protocol}"); |
| 112 | + key.SetValue(string.Empty, $"URL: {protocol}"); |
| 113 | + key.SetValue("URL Protocol", string.Empty); |
| 114 | + |
| 115 | + key = key.CreateSubKey(@"shell\open\command"); |
| 116 | + key.SetValue(string.Empty, $"\"{Environment.ProcessPath}\" \"%1\""); |
| 117 | + key.Close(); |
| 118 | + } |
| 119 | + #elif MACOS |
| 120 | + Log.Information("Registration of SS14 protocol handler for MacOS isn't implemented, who uses that anyway?"); |
| 121 | + #elif LINUX |
| 122 | + Log.Information("Registering SS14 protocol handler for Linux"); |
| 123 | + foreach (var protocol in protocols) |
| 124 | + { |
| 125 | + try |
| 126 | + { |
| 127 | + // Put it in XDG_DATA_HOME/applications or ~/.local/share/applications |
| 128 | + var desktopFile = Path.Combine( |
| 129 | + Environment.GetEnvironmentVariable("XDG_DATA_HOME") |
| 130 | + ?? Path.Combine(Environment.GetFolderPath(UserProfile), ".local", "share"), |
| 131 | + "applications", $"ss14-{protocol}.desktop"); |
| 132 | + if (File.Exists(desktopFile)) |
| 133 | + #if DEBUG |
| 134 | + File.WriteAllText(desktopFile, string.Empty); |
| 135 | + #else |
| 136 | + Log.Information($"SS14 protocol handler desktop file for Linux already exists at {desktopFile}, skipping"); |
| 137 | + continue; |
| 138 | + #endif |
| 139 | + |
| 140 | + using var writer = new StreamWriter(File.OpenWrite(desktopFile)); |
| 141 | + writer.WriteLine("[Desktop Entry]"); |
| 142 | + writer.WriteLine("Type=Application"); |
| 143 | + writer.WriteLine($"Name=SS14 {protocol}"); |
| 144 | + writer.WriteLine($"Exec=\"{Environment.ProcessPath}\" %u"); |
| 145 | + writer.WriteLine("Terminal=false"); |
| 146 | + writer.WriteLine($"MimeType=x-scheme-handler/{protocol};"); |
| 147 | + writer.WriteLine("Categories=Network;"); |
| 148 | + writer.Close(); |
| 149 | + |
| 150 | + Log.Information($"Created SS14 protocol handler desktop file for Linux at {desktopFile}"); |
| 151 | + } |
| 152 | + catch (Exception e) |
| 153 | + { |
| 154 | + Log.Error(e, "Failed to create SS14 protocol handler desktop file for Linux"); |
| 155 | + } |
| 156 | + |
| 157 | + try |
| 158 | + { |
| 159 | + Start("xdg-mime", $"default ss14-{protocol}.desktop x-scheme-handler/{protocol}"); |
| 160 | + Start("update-desktop-database", "~/.local/share/applications"); |
| 161 | + |
| 162 | + Log.Information("Updated SS14 protocol handler registry for Linux"); |
| 163 | + } |
| 164 | + catch (Exception e) |
| 165 | + { |
| 166 | + Log.Error(e, "Failed to update SS14 protocol handler registry for Linux"); |
| 167 | + } |
| 168 | + } |
| 169 | + #else |
| 170 | + Log.Warning("Unknown OS, not registering SS14 protocol handler"); |
| 171 | + #endif |
| 172 | + } |
| 173 | + |
96 | 174 | private sealed record AssetDef(string DefaultPath, AssetType Type);
|
97 | 175 |
|
98 | 176 | private enum AssetType
|
|
0 commit comments