Skip to content

Commit

Permalink
create first version of the installer
Browse files Browse the repository at this point in the history
  • Loading branch information
DasDarki committed Dec 18, 2020
1 parent 2e9f8bb commit 94daff1
Show file tree
Hide file tree
Showing 10 changed files with 831 additions and 0 deletions.
22 changes: 22 additions & 0 deletions GmodDotNet.Installer.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GmodDotNet.Installer", "src\GmodDotNet.Installer.csproj", "{9515FE74-33E9-44D6-9E31-D59FA80F6258}"
EndProject
Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {01D987C5-8BC8-4885-B6C1-2F47EBBD914E}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9515FE74-33E9-44D6-9E31-D59FA80F6258}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9515FE74-33E9-44D6-9E31-D59FA80F6258}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Release|Any CPU = Release|Any CPU
EndGlobalSection
EndGlobal
54 changes: 54 additions & 0 deletions src/GModPathFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace GmodDotNet.Installer
{
internal static class GModPathFinder
{
internal static string Find(OSPlatform platform)
{
if (platform == OSPlatform.Windows)
{
var steam32 = "SOFTWARE\\VALVE\\";
var steam64 = "SOFTWARE\\Wow6432Node\\Valve\\";
var key32 = Registry.LocalMachine.OpenSubKey(steam32);
var key64 = Registry.LocalMachine.OpenSubKey(steam64);
if (key32 != null && !string.IsNullOrEmpty(key32.ToString()))
{
foreach (var k32SubKey in key32.GetSubKeyNames())
{
using var subKey = key32.OpenSubKey(k32SubKey);
if (subKey != null)
{
string path = subKey.GetValue("InstallPath")?.ToString();
if(string.IsNullOrEmpty(path)) continue;
string gmodPath = Path.Combine(path, @"steamapps\common\GarrysMod");
if (Directory.Exists(Path.Combine(gmodPath, "garrysmod")))
return gmodPath;
}
}
}

if (key64 != null && !string.IsNullOrEmpty(key64.ToString()))
{
foreach (var k64SubKey in key64.GetSubKeyNames())
{
using var subKey = key64.OpenSubKey(k64SubKey);
if (subKey != null)
{
string path = subKey.GetValue("InstallPath")?.ToString();
if(string.IsNullOrEmpty(path)) continue;
string gmodPath = Path.Combine(path, @"steamapps\common\GarrysMod");
if (Directory.Exists(Path.Combine(gmodPath, "garrysmod")))
return gmodPath;
}
}
}
}

return null;
}
}
}
25 changes: 25 additions & 0 deletions src/GmodDotNet.Installer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8</LangVersion>
<Company>DasDarki on Github</Company>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<DefineConstants>_WINDOWS</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Reference Include="Microsoft.Win32.Registry, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\3.1.0\ref\netcoreapp3.1\Microsoft.Win32.Registry.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Octokit" Version="0.48.0" />
<PackageReference Include="SharpZipLib" Version="1.3.1" />
</ItemGroup>

</Project>
148 changes: 148 additions & 0 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using GmodDotNet.Installer.UI;
using Octokit;

namespace GmodDotNet.Installer
{
static class Program
{
internal static readonly string[] Types = {"Both", "Client", "Server"};

private static readonly string RepoUsername = "GmodNET";
private static readonly string RepoName = "GmodDotNet";
private static readonly GitHubClient Client = new GitHubClient(new ProductHeaderValue("GmodDotNet-Installer"));
private static IReadOnlyList<Release> _releases;

[STAThread]
static void Main(string[] args)
{
Console.Title = "GmodDotNet-Installer (c) 2020 github.com/DasDarki [unofficial]";
Console.CursorVisible = false;
try
{
Console.Write("Loading GmodDotNet-Repository... ");
using (ProgressBar bar = new ProgressBar())
_releases = Client.Repository.Release.GetAll(RepoUsername, RepoName).GetAwaiter().GetResult();
if (_releases == null || _releases.Count <= 0)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("The GmodDotNet-Repository could not be loaded! ");
Console.ResetColor();
AwaitAnyKey();
return;
}

string tag = _releases[0].TagName;
OSPlatform? platform = GetPlatform();
if (platform == null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("Your OS is unsupported! ");
Console.ResetColor();
AwaitAnyKey();
return;
}

SelectionBox typeSelection = new SelectionBox("Select Installation Type: ", Types);
int type = typeSelection.Await();
ValueBrowser tagBrowser = new ValueBrowser("release", "ENTER", "Select GmodDotNet-Release: ", tag,
newTag => !string.IsNullOrEmpty(newTag) && IsValidTag(newTag));
tag = tagBrowser.Await();
Release release = GetRelease(tag);
string defaultGmodPath = GModPathFinder.Find(platform.Value);
ValueBrowser pathBrowser = new ValueBrowser("path", "BROWSE", "Select GMod Path: ", defaultGmodPath,
newPath => !string.IsNullOrEmpty(newPath) && Directory.Exists(Path.Combine(newPath, "garrysmod")));
string path = pathBrowser.Await();
if (new Confirmation(platform.Value.ToString(), type, path, tag).Await())
{
Console.Clear();
WebInstaller installer = new WebInstaller(path, platform.Value.ToString(), tag, type, release);
string error = installer.Start();
Console.Clear();
if (string.IsNullOrEmpty(error))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("GmodDotNet successfully installed!");
}
else
{
Console.Write("Failed to install GmodDotNet: ");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(error);
}

Console.ResetColor();
AwaitAnyKey();
}
}
catch (Exception e)
{
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine("An error occurred!:");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e);
Console.ResetColor();
AwaitAnyKey();
}
}

private static Release GetRelease(string tag)
{
foreach (Release release in _releases)
{
if (release.TagName == tag)
{
return release;
}
}

return null;
}

private static bool IsValidTag(string tag)
{
foreach (Release release in _releases)
{
if (release.TagName == tag)
{
return true;
}
}

return false;
}

private static void AwaitAnyKey()
{
Console.WriteLine("Please press any key to exit...");
Console.ReadKey();
}

private static OSPlatform? GetPlatform()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return OSPlatform.Linux;
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return OSPlatform.Windows;
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return OSPlatform.OSX;
}

return null;
}
}
}
93 changes: 93 additions & 0 deletions src/UI/Confirmation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;

namespace GmodDotNet.Installer.UI
{
internal class Confirmation : Control<bool>
{
private readonly string _os;
private readonly int _type;
private readonly string _path;
private readonly string _tag;
private bool _isYesSelected;

public Confirmation(string os, int type, string path, string tag) : base("Please confirm the installation:")
{
_os = os;
_type = type;
_path = path;
_tag = tag;
}

protected override void OnDraw()
{
Console.Write("\tDetected OS: ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(_os);
Console.ResetColor();
Console.Write("\tSelected Release-Tag: ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(_tag);
Console.ResetColor();
Console.Write("\tSelected Type: ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(Program.Types[_type]);
Console.ResetColor();
Console.Write("\tSelected Path: ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(_path + "\n");
Console.ResetColor();

if (_isYesSelected)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" NO ");
Console.ResetColor();
Console.Write(" ");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.White;
Console.Write("<");
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" YES ");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.White;
Console.Write(">");

}
else
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write("<");
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" NO ");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.White;
Console.Write(">");
Console.ResetColor();
Console.Write(" ");
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" YES ");
}
Console.ResetColor();
}

protected override void OnKeyInput(ConsoleKey key)
{
switch (key)
{
case ConsoleKey.Enter:
Result = _isYesSelected;
SetFinished();
break;
case ConsoleKey.LeftArrow:
case ConsoleKey.RightArrow:
_isYesSelected = !_isYesSelected;
break;
}
}
}
}
42 changes: 42 additions & 0 deletions src/UI/Control.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;

namespace GmodDotNet.Installer.UI
{
internal abstract class Control<T>
{
private readonly string _text;
private bool _isFinished;
protected T Result;

protected Control(string text, T @default = default)
{
_text = text;
Result = @default;
}

protected void SetFinished()
{
_isFinished = true;
}

protected abstract void OnDraw();

protected virtual void OnKeyInput(ConsoleKey key) {}

public T Await()
{
while (!_isFinished)
{
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.WriteLine("\n" + _text + "\n");
OnDraw();
Console.ResetColor();
ConsoleKey key = Console.ReadKey().Key;
OnKeyInput(key);
}

return Result;
}
}
}
Loading

0 comments on commit 94daff1

Please sign in to comment.