-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated dependencies. Updated to btd6 version 25.0. Added updater. Pr…
…epared for first release
- Loading branch information
Showing
60 changed files
with
630 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using BTD_Mod_Helper.Extensions; | ||
|
||
namespace BTD_Mod_Helper.Api.Helpers | ||
{ | ||
public static class ActionHelper | ||
{ | ||
public static Action CreateFromOptionalFunction(Function funcToExecute) | ||
{ | ||
return (funcToExecute is null) ? new Action(() => { }) : new Action(() => { funcToExecute(); }); | ||
} | ||
|
||
public static Action<T> CreateFromOptionalFunction<T>(Function<T> funcToExecute) | ||
{ | ||
return (funcToExecute is null) ? new Action<T>((T t) => { }) : new Action<T>((T t) => { funcToExecute(); }); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,188 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Text; | ||
|
||
namespace BTD_Mod_Helper_Core.Api.Updater | ||
{ | ||
public partial class GithubReleaseInfo | ||
{ | ||
[JsonProperty("url")] | ||
public Uri Url { get; set; } | ||
|
||
[JsonProperty("assets_url")] | ||
public Uri AssetsUrl { get; set; } | ||
|
||
[JsonProperty("upload_url")] | ||
public string UploadUrl { get; set; } | ||
|
||
[JsonProperty("html_url")] | ||
public Uri HtmlUrl { get; set; } | ||
|
||
[JsonProperty("id")] | ||
public long Id { get; set; } | ||
|
||
[JsonProperty("author")] | ||
public Author Author { get; set; } | ||
|
||
[JsonProperty("node_id")] | ||
public string NodeId { get; set; } | ||
|
||
[JsonProperty("tag_name")] | ||
public string TagName { get; set; } | ||
|
||
[JsonProperty("target_commitish")] | ||
public string TargetCommitish { get; set; } | ||
|
||
[JsonProperty("name")] | ||
public string Name { get; set; } | ||
|
||
[JsonProperty("draft")] | ||
public bool Draft { get; set; } | ||
|
||
[JsonProperty("prerelease")] | ||
public bool Prerelease { get; set; } | ||
|
||
[JsonProperty("created_at")] | ||
public DateTimeOffset CreatedAt { get; set; } | ||
|
||
[JsonProperty("published_at")] | ||
public DateTimeOffset PublishedAt { get; set; } | ||
|
||
[JsonProperty("assets")] | ||
public List<Asset> Assets { get; set; } | ||
|
||
[JsonProperty("tarball_url")] | ||
public Uri TarballUrl { get; set; } | ||
|
||
[JsonProperty("zipball_url")] | ||
public Uri ZipballUrl { get; set; } | ||
|
||
[JsonProperty("body")] | ||
public string Body { get; set; } | ||
} | ||
|
||
public partial class Asset | ||
{ | ||
[JsonProperty("url")] | ||
public Uri Url { get; set; } | ||
|
||
[JsonProperty("id")] | ||
public long Id { get; set; } | ||
|
||
[JsonProperty("node_id")] | ||
public string NodeId { get; set; } | ||
|
||
[JsonProperty("name")] | ||
public string Name { get; set; } | ||
|
||
[JsonProperty("label")] | ||
public object Label { get; set; } | ||
|
||
[JsonProperty("uploader")] | ||
public Author Uploader { get; set; } | ||
|
||
[JsonProperty("content_type")] | ||
public string ContentType { get; set; } | ||
|
||
[JsonProperty("state")] | ||
public string State { get; set; } | ||
|
||
[JsonProperty("size")] | ||
public long Size { get; set; } | ||
|
||
[JsonProperty("download_count")] | ||
public long DownloadCount { get; set; } | ||
|
||
[JsonProperty("created_at")] | ||
public DateTimeOffset CreatedAt { get; set; } | ||
|
||
[JsonProperty("updated_at")] | ||
public DateTimeOffset UpdatedAt { get; set; } | ||
|
||
[JsonProperty("browser_download_url")] | ||
public Uri BrowserDownloadUrl { get; set; } | ||
} | ||
|
||
public partial class Author | ||
{ | ||
[JsonProperty("login")] | ||
public string Login { get; set; } | ||
|
||
[JsonProperty("id")] | ||
public long Id { get; set; } | ||
|
||
[JsonProperty("node_id")] | ||
public string NodeId { get; set; } | ||
|
||
[JsonProperty("avatar_url")] | ||
public Uri AvatarUrl { get; set; } | ||
|
||
[JsonProperty("gravatar_id")] | ||
public string GravatarId { get; set; } | ||
|
||
[JsonProperty("url")] | ||
public Uri Url { get; set; } | ||
|
||
[JsonProperty("html_url")] | ||
public Uri HtmlUrl { get; set; } | ||
|
||
[JsonProperty("followers_url")] | ||
public Uri FollowersUrl { get; set; } | ||
|
||
[JsonProperty("following_url")] | ||
public string FollowingUrl { get; set; } | ||
|
||
[JsonProperty("gists_url")] | ||
public string GistsUrl { get; set; } | ||
|
||
[JsonProperty("starred_url")] | ||
public string StarredUrl { get; set; } | ||
|
||
[JsonProperty("subscriptions_url")] | ||
public Uri SubscriptionsUrl { get; set; } | ||
|
||
[JsonProperty("organizations_url")] | ||
public Uri OrganizationsUrl { get; set; } | ||
|
||
[JsonProperty("repos_url")] | ||
public Uri ReposUrl { get; set; } | ||
|
||
[JsonProperty("events_url")] | ||
public string EventsUrl { get; set; } | ||
|
||
[JsonProperty("received_events_url")] | ||
public Uri ReceivedEventsUrl { get; set; } | ||
|
||
[JsonProperty("type")] | ||
public string Type { get; set; } | ||
|
||
[JsonProperty("site_admin")] | ||
public bool SiteAdmin { get; set; } | ||
} | ||
|
||
public partial class GithubReleaseInfo | ||
{ | ||
public static List<GithubReleaseInfo> FromJson(string json) => JsonConvert.DeserializeObject<List<GithubReleaseInfo>>(json, Converter.Settings); | ||
} | ||
|
||
public static class Serialize | ||
{ | ||
public static string ToJson(this List<GithubReleaseInfo> self) => JsonConvert.SerializeObject(self, Converter.Settings); | ||
} | ||
|
||
internal static class Converter | ||
{ | ||
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | ||
{ | ||
MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | ||
DateParseHandling = DateParseHandling.None, | ||
Converters = | ||
{ | ||
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | ||
}, | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace BTD_Mod_Helper_Core.Api.Updater | ||
{ | ||
public class UpdateChecker | ||
{ | ||
private static HttpClient client = null; | ||
public string ReleaseURL { get; set; } | ||
|
||
public UpdateChecker() | ||
{ | ||
if (client is null) | ||
client = CreateHttpClient(); | ||
} | ||
|
||
public UpdateChecker(string url) : this() | ||
{ | ||
ReleaseURL = url; | ||
} | ||
|
||
|
||
private HttpClient CreateHttpClient() | ||
{ | ||
client = new HttpClient(); | ||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; | ||
client.DefaultRequestHeaders.Add("user-agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"); | ||
return client; | ||
} | ||
|
||
|
||
public async Task<List<GithubReleaseInfo>> GetReleaseInfoAsync() => GetReleaseInfoAsync(ReleaseURL)?.Result; | ||
|
||
public async Task<List<GithubReleaseInfo>> GetReleaseInfoAsync(string url) | ||
{ | ||
var releaseJson = await client.GetStringAsync(url); | ||
return GithubReleaseInfo.FromJson(releaseJson); | ||
} | ||
|
||
|
||
public async Task<GithubReleaseInfo> GetLatestReleaseAsync() => GetLatestReleaseAsync(ReleaseURL).Result; | ||
|
||
public async Task<GithubReleaseInfo> GetLatestReleaseAsync(string url) | ||
{ | ||
return GetReleaseInfoAsync(url).Result[0]; | ||
} | ||
|
||
|
||
public bool IsUpdate(string currentVersion, GithubReleaseInfo latestReleaseInfo) | ||
{ | ||
return IsUpdate(currentVersion, latestReleaseInfo?.TagName); | ||
} | ||
|
||
public bool IsUpdate(string currentVersion, string latestVersion) | ||
{ | ||
if (string.IsNullOrEmpty(currentVersion) || string.IsNullOrEmpty(latestVersion)) | ||
throw new ArgumentNullException(); | ||
|
||
CleanVersionStrings(ref currentVersion, ref latestVersion); | ||
|
||
Int32.TryParse(currentVersion, out int currentVersionNum); | ||
Int32.TryParse(latestVersion, out int latestVersionNum); | ||
|
||
return latestVersionNum > currentVersionNum; | ||
} | ||
|
||
|
||
private void CleanVersionStrings(ref string string1, ref string string2) | ||
{ | ||
RemoveAllNonNumeric(ref string1); | ||
RemoveAllNonNumeric(ref string2); | ||
MakeLengthEven(ref string1, ref string2); | ||
} | ||
|
||
private void RemoveAllNonNumeric(ref string str) | ||
{ | ||
string cleanedStr = ""; | ||
for (int i = 0; i < str.Length; i++) | ||
{ | ||
var currentLetter = str[i].ToString(); | ||
bool isNumber = Int32.TryParse(currentLetter, out int num); | ||
if (isNumber) | ||
cleanedStr += currentLetter; | ||
} | ||
|
||
str = cleanedStr; | ||
} | ||
|
||
private void MakeLengthEven(ref string string1, ref string string2) | ||
{ | ||
while (string1.Length != string2.Length) | ||
{ | ||
bool isString1Bigger = string1.Length > string2.Length; | ||
|
||
string1 += isString1Bigger ? "" : "0"; | ||
string2 += isString1Bigger ? "0" : ""; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.