Skip to content

Commit

Permalink
Updated dependencies. Updated to btd6 version 25.0. Added updater. Pr…
Browse files Browse the repository at this point in the history
…epared for first release
  • Loading branch information
gurrenm3 committed Apr 18, 2021
1 parent 8267a87 commit e4b1c49
Show file tree
Hide file tree
Showing 60 changed files with 630 additions and 42 deletions.
18 changes: 18 additions & 0 deletions BTD Mod Helper Core/Api/Helpers/ActionHelper.cs
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(); });
}
}
}
2 changes: 2 additions & 0 deletions BTD Mod Helper Core/Api/InGame Mod Options/CheckboxOption.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using UnityEngine;
using UnityEngine.UI;
using BTD_Mod_Helper.Extensions;
using System;

namespace BTD_Mod_Helper.Api.InGame_Mod_Options
{
Expand Down
1 change: 1 addition & 0 deletions BTD Mod Helper Core/Api/InGame Mod Options/SliderOption.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UnityEngine;
using UnityEngine.UI;
using BTD_Mod_Helper.Extensions;

namespace BTD_Mod_Helper.Api.InGame_Mod_Options
{
Expand Down
188 changes: 188 additions & 0 deletions BTD Mod Helper Core/Api/Updater/GithubReleaseSchema.cs
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 }
},
};
}
}
103 changes: 103 additions & 0 deletions BTD Mod Helper Core/Api/Updater/UpdateChecker.cs
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" : "";
}
}
}
}
16 changes: 14 additions & 2 deletions BTD Mod Helper Core/BTD Mod Helper Core.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Api\Guard.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\Helpers\ActionHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\InGame Mod Options\ButtonOption.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\InGame Mod Options\CheckboxOption.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\InGame Mod Options\InputOption.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\InGame Mod Options\ModOptionsMenu.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\InGame Mod Options\SliderOption.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\InGameMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\JsonSerializer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\SessionData.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\Updater\GithubReleaseSchema.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\Updater\UpdateChecker.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SessionData.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\SpriteRegister.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\BehaviorExtensions\AttackModelBehaviorExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\BehaviorExtensions\BloonModelBehaviorExt.cs" />
Expand All @@ -27,6 +30,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\BehaviorExtensions\WeaponModelBehaviorExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\CollectionExtensions\ArrayExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\CollectionExtensions\Il2CppGenericIEnumerableExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Function.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\GameExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\LINQExtensions\Il2CppGenericIEnumerable.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\CollectionExtensions\IEnumerableExt.cs" />
Expand All @@ -50,16 +54,24 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SimulationExtensions\BloonExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SimulationExtensions\BloonToSimulationExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SimulationExtensions\EntityExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SimulationExtensions\MapExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SimulationExtensions\SpawnerExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SimulationExtensions\TowerExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SimulationExtensions\TowerToSimulationExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SystemExtensions\ActionExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\ButtonClickedEventExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\Events\ButtonClickedEventExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\ButtonExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\ComponentExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\Events\InputFieldOnValueChanged.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\Events\SliderEventExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\GameObjectExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\ImageExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\InputFieldExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\Events\InputFieldSubmitEvent.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\SpriteExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\Texture2DExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\Events\ToggleEventExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\ToggleExt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\UnityExtensions\Vector3Ext.cs" />
</ItemGroup>
</Project>
Loading

0 comments on commit e4b1c49

Please sign in to comment.