Skip to content

Commit 841a243

Browse files
Implement ss14 & ss14s Protocols (#20)
1 parent 0d2ad51 commit 841a243

File tree

5 files changed

+138
-3
lines changed

5 files changed

+138
-3
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="12.0">
2+
<!-- Adds to the DefineConstants to provide things such as platform-specific defines. -->
3+
<PropertyGroup>
4+
<OS Condition="'$(OS)' == ''">Windows_NT</OS>
5+
</PropertyGroup>
6+
<Choose>
7+
<When Condition="'$(OS)' != 'Unix'">
8+
<PropertyGroup>
9+
<ActualOS>Windows</ActualOS>
10+
</PropertyGroup>
11+
</When>
12+
<!-- Folders that *probably* only exist on MacOS and not Linux. -->
13+
<When Condition="Exists('/Volumes') And Exists('/System') And Exists('/Library')" >
14+
<PropertyGroup>
15+
<ActualOS>MacOS</ActualOS>
16+
</PropertyGroup>
17+
</When>
18+
<When Condition="$([MSBuild]::IsOSPlatform('FreeBSD'))">
19+
<PropertyGroup>
20+
<ActualOS>FreeBSD</ActualOS>
21+
</PropertyGroup>
22+
</When>
23+
<Otherwise>
24+
<PropertyGroup>
25+
<ActualOS>Linux</ActualOS>
26+
</PropertyGroup>
27+
</Otherwise>
28+
</Choose>
29+
<PropertyGroup>
30+
<TargetOS Condition="'$(TargetOS)' == ''">$(ActualOS)</TargetOS>
31+
</PropertyGroup>
32+
<Choose>
33+
<When Condition="'$(TargetOS)' == 'Windows'">
34+
<PropertyGroup>
35+
<DefineConstants>$(DefineConstants);WINDOWS</DefineConstants>
36+
</PropertyGroup>
37+
</When>
38+
<When Condition="'$(TargetOS)' == 'MacOS'" >
39+
<PropertyGroup>
40+
<DefineConstants>$(DefineConstants);MACOS;UNIX</DefineConstants>
41+
</PropertyGroup>
42+
</When>
43+
<Otherwise>
44+
<PropertyGroup>
45+
<DefineConstants>$(DefineConstants);LINUX;UNIX</DefineConstants>
46+
</PropertyGroup>
47+
</Otherwise>
48+
</Choose>
49+
<PropertyGroup Condition="'$(FullRelease)' == 'True'">
50+
<DefineConstants>$(DefineConstants);FULL_RELEASE</DefineConstants>
51+
</PropertyGroup>
52+
<PropertyGroup Condition="'$(FullRelease)' != 'True'">
53+
<DefineConstants>$(DefineConstants);DEVELOPMENT</DefineConstants>
54+
</PropertyGroup>
55+
</Project>

SS14.Launcher.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OS/@EntryIndexedValue">OS</s:String>
44
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=VM/@EntryIndexedValue">VM</s:String>
55
<s:Boolean x:Key="/Default/UserDictionary/Words/=Avalonia/@EntryIndexedValue">True</s:Boolean>
6+
<s:Boolean x:Key="/Default/UserDictionary/Words/=prerelease/@EntryIndexedValue">True</s:Boolean>
67
<s:Boolean x:Key="/Default/UserDictionary/Words/=ROWID/@EntryIndexedValue">True</s:Boolean>
78
<s:Boolean x:Key="/Default/UserDictionary/Words/=zeroblob/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

SS14.Launcher/App.xaml.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data.SqlTypes;
4+
using System.Diagnostics;
35
using System.IO;
46
using Avalonia;
57
using Avalonia.Controls;
68
using Avalonia.Markup.Xaml;
79
using Avalonia.Media.Imaging;
810
using Avalonia.Platform;
911
using JetBrains.Annotations;
12+
using Microsoft.Win32;
1013
using Serilog;
1114
using SS14.Launcher.Models.OverrideAssets;
15+
using static System.Diagnostics.Process;
16+
using static System.Environment.SpecialFolder;
1217

1318
namespace SS14.Launcher;
1419

@@ -44,6 +49,8 @@ public override void Initialize()
4449
IconsLoader.Load(this);
4550

4651
_overrideAssets.AssetsChanged += OnAssetsChanged;
52+
53+
RegisterProtocol();
4754
}
4855

4956
private void LoadBaseAssets()
@@ -93,6 +100,77 @@ private static object LoadAsset(AssetType type, Stream data)
93100
};
94101
}
95102

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+
96174
private sealed record AssetDef(string DefaultPath, AssetType Type);
97175

98176
private enum AssetType

SS14.Launcher/SS14.Launcher.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,5 @@
9393
</Content>
9494
</ItemGroup>
9595
<Import Project="..\MSBuild\Robust.Trimming.targets" />
96+
<Import Project="..\MSBuild\Launcher.DefineConstants.targets" />
9697
</Project>

publish.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
cd "$(dirname "$0")"
44

5-
./publish_linux.sh
6-
./publish_osx.sh
7-
./publish_windows.sh
5+
TargetOS=Linux ./publish_linux.sh
6+
TargetOS=MacOS ./publish_osx.sh
7+
TargetOS=Windows ./publish_windows.sh

0 commit comments

Comments
 (0)