Skip to content
This repository has been archived by the owner on Dec 17, 2022. It is now read-only.

Commit

Permalink
Informs user of new Updater versions. Code Optimisations.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirMangler committed May 24, 2019
1 parent 0efd47b commit 41d9f1f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 37 deletions.
1 change: 1 addition & 0 deletions PrimeHack Updater/PrimeHack Updater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<ItemGroup>
<Compile Include="Updater.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VersionCheck.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
55 changes: 18 additions & 37 deletions PrimeHack Updater/Updater.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using PrimeHack_Updater;
using System;
using System.Diagnostics;
using System.IO;
Expand All @@ -10,24 +11,29 @@ namespace PrimeHack_Updator
{
class Updater
{
static string sysversion = "1.0.2";

static void Main(string[] args)
{
Console.WriteLine("Checking for latest updates.");

string html = getVersionJSON();
if (html == null || html.Length == 0)
string html = VersionCheck.getJSONInfo(@"https://api.github.com/repos/SirMangler/PrimeHack-Updater/releases/latest");
string remoteversion = VersionCheck.getVersion(html);

if (remoteversion.Equals(sysversion))
{
Console.WriteLine("Press any key to launch.");
Console.WriteLine("PrimeHack Updater has an available update. It is highly recommended you update in order for PrimeHack Updater to support the latest features!");
Console.WriteLine("Update Link: https://github.com/SirMangler/PrimeHack-Updater/releases/");
Console.WriteLine("Press Any Key To Continue");
Console.ReadKey();
runPrimeHack();
}

dynamic j = JObject.Parse(html);
html = VersionCheck.getJSONInfo(@"https://api.github.com/repos/shiiion/Ishiiruka/releases/latest");
remoteversion = VersionCheck.getVersion(html);

string version = (string)j.tag_name;
string currentversion = getVersion().Replace("\r\n", "");

if (version.Equals(currentversion))
if (currentversion.Equals(currentversion))
{
runPrimeHack();
}
Expand All @@ -48,12 +54,13 @@ static void Main(string[] args)
}
}

dynamic j = JObject.Parse(html);
JArray ja = j.assets;
dynamic assets = ja[0];
string url = assets.browser_download_url;

downloadLatest(url);
System.IO.File.WriteAllLines(".\\version.txt", new string[] { version });
System.IO.File.WriteAllLines(".\\version.txt", new string[] { remoteversion });

Console.WriteLine("Moving profiles into Documents.");
cutProfiles();
Expand Down Expand Up @@ -128,6 +135,9 @@ public static void downloadLatest(string url)

if (File.Exists(completeFileName))
{
if (completeFileName.EndsWith("hack_config.ini"))
continue;

long ziptime = file.LastWriteTime.ToFileTime();
long oldtime = File.GetLastWriteTime(completeFileName).ToFileTime();

Expand All @@ -147,35 +157,6 @@ public static void downloadLatest(string url)
File.Delete(Path.GetTempPath() + "\\PrimeHackRelease.zip");
}

public static string getVersionJSON()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;

string html = string.Empty;
string url = @"https://api.github.com/repos/shiiion/Ishiiruka/releases/latest";

try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
request.UserAgent = "PrimeHackUpdater";

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
}
catch (Exception e)
{
Console.WriteLine("Failed to retrieve version info: " + e.Message);
}

return html;
}
}
}
60 changes: 60 additions & 0 deletions PrimeHack Updater/VersionCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace PrimeHack_Updater
{
class VersionCheck
{
public static string getVersion(string html)
{
if (html == null || html.Length == 0)
{
Console.WriteLine("Couldn't access JSON info. Press any key to launch.");
Console.ReadKey();

return "-1";
}

dynamic j = JObject.Parse(html);
string version = (string)j.tag_name;

return version;
}

public static string getJSONInfo(string url)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;

string html = string.Empty;

try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
request.UserAgent = "PrimeHackUpdater";

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
}
catch (Exception e)
{
Console.WriteLine("Failed to retrieve version info: " + e.Message);
}

return html;
}
}
}

0 comments on commit 41d9f1f

Please sign in to comment.