Skip to content

Commit f2d31c5

Browse files
committed
Add everest: URI handler for GameBanana
1 parent 06683e7 commit f2d31c5

6 files changed

Lines changed: 152 additions & 55 deletions

File tree

Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434
// You can specify all the values or you can default the Build and Revision Numbers
3535
// by using the '*' as shown below:
3636
// [assembly: AssemblyVersion("1.0.*")]
37-
[assembly: AssemblyVersion("18.05.0.*")]
37+
[assembly: AssemblyVersion("18.05.1.*")]
3838
[assembly: AssemblyFileVersion("1.0.0.0")]

src/GameModder.cs

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Microsoft.Win32;
2+
using System;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Globalization;
@@ -44,10 +45,30 @@ public void Install() {
4445
Console.WriteLine();
4546

4647
// Sneaky step: Copy the installer.
48+
string installerPath = null;
4749
try {
48-
string installerPath = Assembly.GetEntryAssembly().Location;
49-
File.Copy(installerPath, Path.Combine(Info.CurrentGamePath, Path.GetFileName(installerPath)), true);
50+
string installerTmpPath = Assembly.GetEntryAssembly().Location;
51+
installerPath = Path.Combine(Info.CurrentGamePath, Path.GetFileName(installerTmpPath));
52+
File.Copy(installerTmpPath, installerPath, true);
5053
} catch {
54+
installerPath = null;
55+
}
56+
// Sneaky step: Set up URI handler.
57+
if (!string.IsNullOrEmpty(Info.ModURIProtocol) &&
58+
!string.IsNullOrEmpty(Info.ModsDir)) {
59+
try {
60+
RegistryKey regClasses = Registry
61+
.CurrentUser
62+
?.OpenSubKey("Software", true)
63+
?.OpenSubKey("Classes", true);
64+
RegistryKey regProtocol = regClasses?.CreateSubKey(Info.ModURIProtocol);
65+
if (regProtocol != null) {
66+
regProtocol.SetValue("", $"URL:{Info.ModURIProtocol}");
67+
regProtocol.SetValue("URL Protocol", "");
68+
regProtocol.CreateSubKey(@"shell\open\command").SetValue("", $"\"{installerPath}\" --uri %1");
69+
}
70+
} catch {
71+
}
5172
}
5273

5374
_Install();
@@ -72,6 +93,19 @@ public void Uninstall() {
7293
OnStart?.Invoke();
7394
try {
7495

96+
// Sneaky step: Remove URI handler.
97+
if (!string.IsNullOrEmpty(Info.ModURIProtocol) &&
98+
!string.IsNullOrEmpty(Info.ModsDir)) {
99+
try {
100+
RegistryKey regClasses = Registry
101+
.CurrentUser
102+
?.OpenSubKey("Software", true)
103+
?.OpenSubKey("Classes", true);
104+
regClasses?.DeleteSubKey(Info.ModURIProtocol, false);
105+
} catch {
106+
}
107+
}
108+
75109
_Restore();
76110

77111
} catch (Exception e) {
@@ -86,6 +120,45 @@ public void Uninstall() {
86120
OnFinish?.Invoke();
87121
}
88122

123+
public void DownloadMod(string url) {
124+
Console.WriteLine("Starting download");
125+
126+
Uri uri = new Uri(url);
127+
128+
string modRoot = Info.ModsDir;
129+
if (!Directory.Exists(modRoot))
130+
modRoot = Path.Combine(Info.CurrentGamePath, modRoot);
131+
if (!Directory.Exists(modRoot))
132+
Directory.CreateDirectory(modRoot);
133+
134+
string modPath = Path.Combine(modRoot, Path.GetFileName(uri.AbsolutePath));
135+
if (File.Exists(modPath))
136+
File.Delete(modPath);
137+
138+
Console.WriteLine($"Downloading: {url}");
139+
140+
OnStart?.Invoke();
141+
try {
142+
143+
byte[] zipData = _Download(url);
144+
145+
Console.WriteLine("Writing data to file");
146+
File.WriteAllBytes(modPath, zipData);
147+
148+
} catch (Exception e) {
149+
Console.WriteLine($"Failed downloading {url}");
150+
Console.WriteLine(e);
151+
Console.WriteLine("Error! Please check installer-log.txt");
152+
OnError?.Invoke(e);
153+
if (Debugger.IsAttached)
154+
throw;
155+
return;
156+
}
157+
Console.WriteLine($"{Path.GetFileName(modPath)} downloaded!");
158+
Console.WriteLine();
159+
OnFinish?.Invoke();
160+
}
161+
89162
private void _DownloadAndUnpack() {
90163
string root = Info.CurrentGamePath;
91164
Console.WriteLine($"STEP: DOWNLOAD & UNPACK");

src/Infos/EverestInfo.cs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,13 @@ public partial class EverestInfo : GameModInfo {
1717

1818
public readonly static Random RNG = new Random();
1919

20-
public override string GameName {
21-
get {
22-
return "Celeste";
23-
}
24-
}
25-
26-
public override string ModName {
27-
get {
28-
return "Everest";
29-
}
30-
}
31-
32-
public override string ModInstallerName {
33-
get {
34-
return "Everest.Installer";
35-
}
36-
}
20+
public override string GameName => "Celeste";
21+
public override string ModName => "Everest";
22+
public override string ModURIProtocol => "Everest";
23+
public override string ModInstallerName => "Everest.Installer";
3724

38-
public override Image HeaderImage {
39-
get {
40-
return Properties.Resources.header;
41-
}
42-
}
43-
44-
public override Image BackgroundImage {
45-
get {
46-
return Properties.Resources.background;
47-
}
48-
}
25+
public override Image HeaderImage => Properties.Resources.header;
26+
public override Image BackgroundImage => Properties.Resources.background;
4927

5028
public override string ExecutableDir {
5129
get {

src/Infos/GameModInfo.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ public virtual ModBackup[] Backups {
2727
}
2828
public abstract Dictionary<string, string> GameIDs { get; }
2929

30-
public virtual string CacheDir {
31-
get {
32-
return "ModInstallerCache";
33-
}
34-
}
30+
public virtual string ModURIProtocol => "";
31+
public virtual string ModsDir => "Mods";
3532

3633
public abstract List<ModVersion> ModVersions { get; }
3734

src/MainForm.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ protected override CreateParams CreateParams {
2929
}
3030
}
3131

32-
public readonly AnimationManager AnimationManager;
32+
public readonly string VersionString;
3333

34-
public readonly static Random RNG = new Random();
34+
public readonly AnimationManager AnimationManager;
3535

3636
private Image BackgroundNoise;
3737

@@ -46,9 +46,10 @@ protected override CreateParams CreateParams {
4646
public readonly GameModInfo Info;
4747
public readonly GameModder Modder;
4848
private Thread _ModderThread;
49-
5049
public InstallerStatus Status { get; private set; } = InstallerStatus.Prepare;
5150

51+
public string AutoDownloadMod;
52+
5253
public bool ShowFPS =
5354
#if DEBUG
5455
true;
@@ -69,8 +70,6 @@ protected override CreateParams CreateParams {
6970
private OpenFileDialog _ExeBrowseDialog;
7071
private OpenFileDialog _ModBrowseDialog;
7172

72-
public readonly string VersionString;
73-
7473
private Thread _ModVersionsThread;
7574

7675
public MainForm(GameModInfo info) {
@@ -288,15 +287,21 @@ private void MainForm_Load(object _s, EventArgs _e) {
288287
_PreviousModVersionIndex = MainVersionList.SelectedIndex;
289288
};
290289

290+
if (!string.IsNullOrEmpty(AutoDownloadMod)) {
291+
if (Status != InstallerStatus.Prepare)
292+
return;
293+
Status = InstallerStatus.Progress;
294+
MainPanel.SlideOut();
295+
ProgressPanel.SlideIn();
296+
297+
_ModderThread = new Thread(() => Modder.DownloadMod(AutoDownloadMod));
298+
_ModderThread.Start();
299+
return;
300+
}
301+
291302
_ModVersionsThread = new Thread(_DownloadModVersions);
292303
_ModVersionsThread.IsBackground = true;
293304
_ModVersionsThread.Start();
294-
295-
/*
296-
this.SetProgressState(TaskbarExt.TBPF.TBPF_NORMAL);
297-
this.SetProgressValue(5, 10);
298-
this.SetOverlayIcon(Icon);
299-
*/
300305
}
301306

302307

src/Program.cs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Linq;
6+
using System.Net;
67
using System.Reflection;
78
using System.Text;
89
using System.Windows.Forms;
@@ -14,14 +15,29 @@ static class Program {
1415
/// </summary>
1516
[STAThread]
1617
static void Main(string[] args) {
17-
string log = Path.GetFullPath("installer-log.txt");
18-
if (args.Length >= 2 && args[0] == "--log") {
19-
StringBuilder logBuilt = new StringBuilder();
20-
for (int i = 1; i < args.Length; i++)
21-
logBuilt.Append(args[i]).Append(" ");
22-
log = logBuilt.ToString().Trim();
18+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
19+
20+
const string logDefaultName = "installer-log.txt";
21+
string log = Path.GetFullPath(logDefaultName);
22+
23+
Queue<string> argsQueue = new Queue<string>(args);
24+
Queue<string> argsLateQueue = new Queue<string>();
25+
while (argsQueue.Count > 0) {
26+
string arg = argsQueue.Dequeue();
27+
28+
if (arg == "--log" && argsQueue.Count >= 1) {
29+
StringBuilder logBuilt = new StringBuilder();
30+
while (argsQueue.Count > 0)
31+
logBuilt.Append(argsQueue.Dequeue()).Append(" ");
32+
log = logBuilt.ToString().Trim();
33+
34+
} else {
35+
argsLateQueue.Enqueue(arg);
36+
}
2337
}
38+
argsQueue = argsLateQueue;
2439

40+
// If the assembly name doesn't match, re-run the installer from a temporary location.
2541
string asmLoc = Assembly.GetEntryAssembly().Location;
2642
string asmNameWanted = Assembly.GetExecutingAssembly().GetName().Name + ".exe";
2743
if (Path.GetFileName(asmLoc) != asmNameWanted) {
@@ -31,6 +47,14 @@ static void Main(string[] args) {
3147
return;
3248
}
3349

50+
// Check if log is writable, otherwise write log to user dir.
51+
try {
52+
using (Stream tmpStream = File.OpenWrite(log)) {
53+
}
54+
} catch {
55+
log = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), logDefaultName);
56+
}
57+
3458
if (File.Exists(log))
3559
File.Delete(log);
3660
using (Stream fileStream = File.OpenWrite(log))
@@ -42,12 +66,32 @@ static void Main(string[] args) {
4266
Console.SetOut(logWriter);
4367

4468
GameModInfo info = new Everest.EverestInfo();
69+
4570
Console.WriteLine($"{info.ModInstallerName} v{Assembly.GetEntryAssembly().GetName().Version}");
4671

4772
try {
4873
Application.EnableVisualStyles();
4974
Application.SetCompatibleTextRenderingDefault(false);
50-
Application.Run(new MainForm(info));
75+
76+
MainForm form = new MainForm(info);
77+
78+
while (argsQueue.Count > 0) {
79+
string arg = argsQueue.Dequeue();
80+
81+
if (arg == "--uri" && argsQueue.Count >= 1) {
82+
arg = argsQueue.Dequeue();
83+
84+
if (arg.ToLowerInvariant().StartsWith(info.ModURIProtocol.ToLowerInvariant() + ":"))
85+
arg = arg.Substring(info.ModURIProtocol.Length + 1);
86+
87+
if (arg.StartsWith("http://") || arg.StartsWith("https://")) {
88+
// Automatic mod .zip download.
89+
form.AutoDownloadMod = arg.Split(',')[0];
90+
}
91+
}
92+
}
93+
94+
Application.Run(form);
5195
} catch (Exception e) {
5296
Console.WriteLine(e);
5397
if (Debugger.IsAttached) {

0 commit comments

Comments
 (0)