-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
66 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
namespace App.Interfaces | ||
using System; | ||
|
||
namespace App.Interfaces | ||
{ | ||
public interface IUpdateModel | ||
{ | ||
bool IsUpdateAvaliable(); | ||
bool UpdateIsAvailable { get; } | ||
bool Error { get; } | ||
string newVersion { get; } | ||
string newVersionLink { get; } | ||
string currentVersion { get; } | ||
void CheckIfUpdateIsAvaliable(); | ||
Version OnlineVersion { get; } | ||
string NewVersionLink { get; } | ||
Version CurrentVersion { get; } | ||
bool CheckForUpdates(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,64 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
using App.Interfaces; | ||
using CollectionManagerExtensionsDll.Utils; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace App | ||
{ | ||
public class UpdateChecker : IUpdateModel | ||
{ | ||
private const string UpdateUrl = "http://osustats.ppy.sh/api/ce/version"; | ||
private const string baseGithubUrl = "https://api.github.com/repos/Piotrekol/CollectionManager"; | ||
private const string githubUpdateUrl = baseGithubUrl + "/releases/latest"; | ||
|
||
public bool Error { get; private set; } | ||
public string newVersion { get; private set; } | ||
public string newVersionLink { get; private set; } | ||
public string currentVersion { get; set; } = "???"; | ||
|
||
public bool IsUpdateAvaliable() | ||
{ | ||
return CheckForUpdates(); | ||
} | ||
public void CheckIfUpdateIsAvaliable() | ||
public UpdateChecker() | ||
{ | ||
UpdateVersion(); | ||
var version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location); | ||
CurrentVersion = new Version(version.ProductVersion); | ||
} | ||
|
||
private bool CheckForUpdates() | ||
public bool Error { get; private set; } | ||
public Version OnlineVersion { get; private set; } | ||
public string NewVersionLink { get; private set; } | ||
public Version CurrentVersion { get; } | ||
|
||
public bool UpdateIsAvailable => OnlineVersion != null && OnlineVersion > CurrentVersion; | ||
|
||
public bool CheckForUpdates() | ||
{ | ||
UpdateVersion(); | ||
if (string.IsNullOrWhiteSpace(newVersion)) | ||
var data = GetStringData(githubUpdateUrl); | ||
if (string.IsNullOrEmpty(data)) | ||
{ | ||
Error = true; | ||
return false; | ||
} | ||
Version verLocal, verOnline; | ||
|
||
JObject json; | ||
try | ||
{ | ||
verLocal = new Version(currentVersion); | ||
verOnline = new Version(newVersion); | ||
json = JObject.Parse(data); | ||
} | ||
catch | ||
catch (JsonReaderException) | ||
{ | ||
return true; | ||
return false; | ||
} | ||
|
||
var newestReleaseVersion = json["tag_name"].ToString(); | ||
OnlineVersion = new Version(newestReleaseVersion); | ||
NewVersionLink = json["html_url"].ToString(); | ||
|
||
return verLocal.CompareTo(verOnline) < 0; | ||
return UpdateIsAvailable; | ||
} | ||
private void UpdateVersion() | ||
{ | ||
try | ||
{ | ||
string contents; | ||
using (var wc = new System.Net.WebClient()) | ||
contents = wc.DownloadString(UpdateUrl); | ||
if (contents.Contains("<html>") || contents.Contains("<head>") || contents.Contains("html>")) | ||
return; | ||
var splited = contents.Split(new[] { ',' }, 2); | ||
|
||
newVersionLink = splited[1]; | ||
newVersion = splited[0]; | ||
|
||
} | ||
catch (Exception) | ||
private string GetStringData(string url) | ||
{ | ||
using (var wc = new ImpatientWebClient()) | ||
{ | ||
wc.Headers.Add("user-agent", $"CollectionManager_Updater_{CurrentVersion}"); | ||
return wc.DownloadString(url); | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters