Skip to content

Commit

Permalink
added update checker
Browse files Browse the repository at this point in the history
  • Loading branch information
iBowie committed Sep 30, 2020
1 parent a920bda commit af96559
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
56 changes: 56 additions & 0 deletions Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using BowieD.Unturned.AssetExpander.Models;
using Newtonsoft.Json.Linq;
using Rocket.Core.Plugins;
using SDG.Unturned;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;

namespace BowieD.Unturned.AssetExpander
Expand All @@ -23,6 +25,25 @@ protected override void Load()

Rocket.Core.Logging.Logger.Log("Plugin created by BowieD");
Rocket.Core.Logging.Logger.Log(@"https://github.com/iBowie/BowieD.Unturned.AssetExpander");

switch (checkForUpdates())
{
case true:
{
Rocket.Core.Logging.Logger.LogWarning("Update available. Head to GitHub page to download it.");
}
break;
case false:
{

}
break;
default:
{
Rocket.Core.Logging.Logger.LogWarning("Could not get new version.");
}
break;
}
}
protected override void Unload()
{
Expand Down Expand Up @@ -140,5 +161,40 @@ void addOrOverrideCustomData(Guid g, string name, string value)
else
dict.Add(name, value);
}

bool? checkForUpdates()
{
try
{
var v = Assembly.GetName().Version;

using (WebClient wc = new WebClient())
{
wc.Headers.Add(HttpRequestHeader.UserAgent, "Unturned");

var url = $"https://api.github.com/repos/iBowie/BowieD.Unturned.AssetExpander/releases/latest";

string data = wc.DownloadString(url);

JObject jobj = JObject.Parse(data);

if (jobj.TryGetValue("tag_name", out var tag_nameToken))
{
var nv = Version.Parse(tag_nameToken.Value<string>());

return nv > v ? true : false;
}
else
{
return null;
}
}
}
catch (Exception ex)
{
Rocket.Core.Logging.Logger.LogException(ex, "Could not check for new updates");
return null;
}
}
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.2.0")]
[assembly: AssemblyFileVersion("1.2.2.0")]
[assembly: AssemblyVersion("1.2.2.1")]
[assembly: AssemblyFileVersion("1.2.2.1")]

0 comments on commit af96559

Please sign in to comment.