Skip to content

Commit

Permalink
Merge pull request #218 from anno-mods/devel/self-updater
Browse files Browse the repository at this point in the history
add update checker, support sub mods
  • Loading branch information
jakobharder authored Apr 30, 2023
2 parents 1c38df1 + 4b2084a commit 02cfc9a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 42 deletions.
4 changes: 4 additions & 0 deletions ModManager/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using Anno.Utils;
using Imya.UI.Properties;
using Imya.UI.Utils;
using Imya.Utils;
Expand Down Expand Up @@ -47,6 +48,9 @@ public MainWindow()
if (result is true)
GameSetupManager.Instance.RemoveModloader();
}

// initialize self-updater
SelfUpdater.CheckForUpdate(GithubClientProvider.Client, "anno-mods", "iModYourAnno");
}

public void SetUpEmbeddedConsole()
Expand Down
60 changes: 60 additions & 0 deletions ModManager/Utils/SelfUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using Octokit;

namespace Anno.Utils
{
internal class SelfUpdater
{
public static void CheckForUpdate(GitHubClient client, string owner, string repo)
{
Task.Run(async () =>
{
var availableUpdate = await IsUpdateAvailableAsync(client, owner, repo);
if (availableUpdate is not null)
{
var answer = MessageBox.Show($"iModYourAnno {availableUpdate.TagName} is available.\n\nDo you want to download it now?",
"Update Available", MessageBoxButton.YesNo);
if (answer == MessageBoxResult.Yes)
{
OpenReleasePage(availableUpdate);
}
}
});
}

public async static Task<Release?> IsUpdateAvailableAsync(GitHubClient client, string owner, string repo)
{
var latest = await client.Repository.Release.GetLatest(owner, repo);
if (latest is null) return null;

Console.WriteLine(
"The latest release is tagged at {0} and is named {1}",
latest.TagName,
latest.Name);

var latestVersion = new Version(latest.TagName[1..]);
var currentVersion = GetCurrentVersion();

return (latestVersion > currentVersion) ? latest : null;
}

public static void OpenReleasePage(Release release)
{
var info = new ProcessStartInfo(release.HtmlUrl)
{
UseShellExecute = true,
};
Process.Start(info);
}

public static Version GetCurrentVersion()
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
return new Version((fvi.FileVersion ?? "v0.0")[1..].Split('-')[0]);
}
}
}
23 changes: 23 additions & 0 deletions ModManager_Classes/Models/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,25 @@ public Mod (string folderName, Modinfo? modinfo, string basePath)
// TODO move to separate async?
var info = new DirectoryInfo(FullModPath);
SizeInMB = (float)Math.Round((decimal)info.EnumerateFiles("*", SearchOption.AllDirectories).Sum(x => x.Length) / 1024 / 1024, 1);

string[] modinfos = Directory.GetFiles(Path.Combine(basePath, folderName), "modinfo.json", SearchOption.AllDirectories);
if (modinfos.Length > 1)
{
SubMods = new List<Mod>();
foreach (var submodinfo in modinfos)
{
if (submodinfo == Path.Combine(basePath, folderName, "modinfo.json"))
{
continue;
}

Mod? submod = TryFromFolder(Path.GetDirectoryName(submodinfo) ?? "");
if (submod is not null)
{
SubMods.Add(submod);
}
}
}
}

public void InitImageAsFilepath(String ImagePath)
Expand Down Expand Up @@ -310,6 +329,10 @@ public bool IsUpdateOf(Mod? target)
}
#endregion

#region Sub mods
public List<Mod>? SubMods { get; private set; }
#endregion

public ModStatus GetStatus()
{
return (Attributes.GetByType(AttributeType.ModStatus) as ModStatusAttribute)?.Status ?? ModStatus.Default;
Expand Down
2 changes: 1 addition & 1 deletion ModManager_Classes/Models/ModCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static void AutofixSubfolders(string modsPath)
{
foreach (var folder in Directory.EnumerateDirectories(modsPath))
{
if (Directory.Exists(Path.Combine(folder, "data")))
if (Directory.Exists(Path.Combine(folder, "data")) || File.Exists(Path.Combine(folder, "modinfo.json")))
continue;

var potentialMods = DirectoryEx.FindFolder(folder, "data").Select(x => Path.GetDirectoryName(x)!);
Expand Down
9 changes: 3 additions & 6 deletions ModManager_Classes/Utils/InstallationManager.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using Imya.Models;
using Imya.Models.Collections;
using Imya.Models.Installation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Downloader;
using Imya.Models.NotifyPropertyChanged;
Expand Down Expand Up @@ -266,7 +261,9 @@ private async Task MoveModsAsync(IUnpackableInstallation unpackable)
if (unpackable.CancellationToken.IsCancellationRequested)
return;
unpackable.Status = InstallationStatus.MovingFiles;
var newCollection = await ModCollectionLoader.LoadFrom(unpackable.UnpackTargetPath);

var newCollection = new ModCollection(unpackable.UnpackTargetPath, autofixSubfolder: true);
await newCollection.LoadModsAsync();
//async waiting
await Task.Run(() => _moveIntoSem.WaitOne(), unpackable.CancellationToken);
if (!unpackable.CancellationToken.IsCancellationRequested)
Expand Down
19 changes: 0 additions & 19 deletions ModManager_Classes/Utils/ModCollectionLoader.cs

This file was deleted.

4 changes: 3 additions & 1 deletion ModManager_Classes/Validation/ModCompatibilityValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ private static IEnumerable<string> GetUnresolvedDependencies(Modinfo modinfo, IR

foreach (var dep in modinfo.ModDependencies)
{
if (!collection.Any(x => x.Modinfo.ModID is not null && x.Modinfo.ModID.Equals(dep) && x.IsActiveAndValid))
if (!collection.Any(x => x.Modinfo.ModID is not null
&& (x.Modinfo.ModID.Equals(dep) || x.SubMods?.Find(submod => submod.ModID.Equals(dep)) is not null)
&& x.IsActiveAndValid))
yield return dep;
}
}
Expand Down
15 changes: 0 additions & 15 deletions ModManager_Classes/Validation/ModContentValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,11 @@ public void Validate(IEnumerable<Mod> changed, IReadOnlyCollection<Mod> all)

private static void ValidateSingle(Mod mod)
{
mod.Attributes.RemoveAttributesByType(AttributeType.ModContentInSubfolder);

if (mod.IsRemoved || !Directory.Exists(mod.FullModPath))
{
mod.IsRemoved = true;
return;
}

string dataPath = Path.Combine(mod.FullModPath, "data");
if (!Directory.Exists(dataPath))
{
// data/ doesn't exist, that's odd

var foundFolders = Directory.GetDirectories(mod.FullModPath, "data", SearchOption.AllDirectories);
if (foundFolders.Length > 0)
{
// there's a data/ somewhere deeper, probably a mistake then
mod.Attributes.AddAttribute(new GenericAttribute() { AttributeType = AttributeType.ModContentInSubfolder, Description = TextManager.Instance.GetText("ATTRIBUTE_MODCONTENTSUBFOLDER") });
}
}
}
}
}

0 comments on commit 02cfc9a

Please sign in to comment.