-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathApplicationVersionGuard.cs
More file actions
168 lines (147 loc) · 6.91 KB
/
Copy pathApplicationVersionGuard.cs
File metadata and controls
168 lines (147 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using Cysharp.Threading.Tasks;
using DCL.Browser;
using DCL.Diagnostics;
using DCL.Multiplayer.Connections.DecentralandUrls;
using DCL.Utility;
using DCL.WebRequests;
using SceneRuntime.Apis.Modules.SignedFetch.Messages;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using UnityEngine;
#if UNITY_EDITOR
// ReSharper disable once RedundantUsingDirective
using UnityEditor;
#endif
namespace DCL.ApplicationGuards
{
public class ApplicationVersionGuard
{
private const string LAUNCHER_EXECUTABLE_NAME = "Decentraland";
private const string LEGACY_LAUNCHER_EXECUTABLE_NAME = "Decentraland Launcher";
private const string LAUNCHER_EXECUTABLE_FILENAME = "dcl_launcher.exe";
private const string LAUNCHER_PATH_MAC = "/Applications/" + LAUNCHER_EXECUTABLE_NAME + ".app";
private const string LEGACY_LAUNCHER_PATH_MAC = "/Applications/" + LEGACY_LAUNCHER_EXECUTABLE_NAME + ".app";
private const string DECENTRALAND_LAUNCHER_WIN_X64_EXE = "Decentraland_installer.exe";
private const string DECENTRALAND_LAUNCHER_MAC_ARM_64_DMG = "Decentraland_installer.dmg";
//Aga: Rust version of launcher does not support intel macs, until fully deprecating it, we need to keep the old launcher for intel based macs
private const string DECENTRALAND_LEGACY_LAUNCHER_MAC_X_64_DMG = "Decentraland Outdated-mac-x64.dmg";
private readonly IWebRequestController webRequestController;
private readonly UnityAppWebBrowser webBrowser;
public ApplicationVersionGuard(IWebRequestController webRequestController, UnityAppWebBrowser webBrowser)
{
this.webRequestController = webRequestController;
this.webBrowser = webBrowser;
}
public async UniTask<string> GetLatestVersionAsync(CancellationToken ct)
{
FlatFetchResponse response = await webRequestController.GetAsync<FlatFetchResponse<GenericGetRequest>, FlatFetchResponse>(
IDecentralandUrlsSource.EXPLORER_LATEST_RELEASE_URL,
new FlatFetchResponse<GenericGetRequest>(),
ct,
ReportCategory.VERSION_CONTROL,
new WebRequestHeadersInfo());
ClientVersionInfo versionInfo = JsonUtility.FromJson<ClientVersionInfo>(response.body);
string latestVersion = versionInfo.version.TrimStart('v');
return latestVersion;
}
public async UniTask LaunchOrDownloadLauncherAsync(CancellationToken ct = default)
{
string? launcherPath = GetLauncherPath();
if (string.IsNullOrEmpty(launcherPath))
{
DownloadLauncher();
ExitUtils.Exit();
}
else
{
try
{
await UniTask.Delay(1000, cancellationToken: ct);
Utility.PlatformUtils.ShellExecute(launcherPath);
}
catch (Exception e)
{
if (e is not OperationCanceledException)
ReportHub.LogException(e, ReportCategory.VERSION_CONTROL);
}
finally
{
await UniTask.Delay(2000, cancellationToken: ct);
ExitUtils.Exit();
}
}
}
private void DownloadLauncher()
{
string assetName = GetLauncherAssetName();
string downloadUrl = $"{GetLauncherDownloadPath()}/{assetName}";
if (!string.IsNullOrEmpty(downloadUrl))
webBrowser.OpenUrlMainThreadOnly(downloadUrl);
else
ReportHub.LogError(ReportCategory.VERSION_CONTROL, "Failed to get launcher download URL.");
}
private static string GetLauncherAssetName()
{
return Application.platform switch
{
RuntimePlatform.WindowsEditor or RuntimePlatform.WindowsPlayer => DECENTRALAND_LAUNCHER_WIN_X64_EXE,
RuntimePlatform.OSXEditor or RuntimePlatform.OSXPlayer => isAppleSiliconMac ? DECENTRALAND_LAUNCHER_MAC_ARM_64_DMG : DECENTRALAND_LEGACY_LAUNCHER_MAC_X_64_DMG,
_ => throw new NotSupportedException("Unsupported platform for launcher download."),
};
}
private static string GetLauncherDownloadPath()
{
return Application.platform switch
{
RuntimePlatform.WindowsEditor or RuntimePlatform.WindowsPlayer => IDecentralandUrlsSource.LAUNCHER_DOWNLOAD_URL,
RuntimePlatform.OSXEditor or RuntimePlatform.OSXPlayer => isAppleSiliconMac ? IDecentralandUrlsSource.LAUNCHER_DOWNLOAD_URL : IDecentralandUrlsSource.LEGACY_LAUNCHER_DOWNLOAD_URL,
_ => throw new NotSupportedException("Unsupported platform for launcher download."),
};
}
private static string? GetLauncherPath()
{
string[] possiblePaths;
switch (Application.platform)
{
case RuntimePlatform.WindowsEditor:
case RuntimePlatform.WindowsPlayer:
possiblePaths = new[]
{
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), LAUNCHER_EXECUTABLE_NAME, LAUNCHER_EXECUTABLE_FILENAME),
};
break;
case RuntimePlatform.OSXEditor:
case RuntimePlatform.OSXPlayer:
possiblePaths = isAppleSiliconMac
? new[]
{
LAUNCHER_PATH_MAC,
$"{Environment.GetFolderPath(Environment.SpecialFolder.Personal)}{LAUNCHER_PATH_MAC}",
}
: new[]
{
LEGACY_LAUNCHER_PATH_MAC,
$"{Environment.GetFolderPath(Environment.SpecialFolder.Personal)}{LEGACY_LAUNCHER_PATH_MAC}",
};
break;
default:
ReportHub.LogError(ReportCategory.VERSION_CONTROL, "Unsupported platform for launching the application.");
return null;
}
return possiblePaths.FirstOrDefault(path =>
File.Exists(path) ||
(Directory.Exists(path) && (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer)));
}
private static bool isAppleSiliconMac =>
Application.platform is RuntimePlatform.OSXEditor or RuntimePlatform.OSXPlayer &&
SystemInfo.processorType.Contains("apple", StringComparison.OrdinalIgnoreCase);
[Serializable]
private struct ClientVersionInfo
{
public string version;
public string timestamp;
}
}
}