Skip to content

Commit

Permalink
get other patches
Browse files Browse the repository at this point in the history
  • Loading branch information
01010100b committed Oct 15, 2023
1 parent 15838d1 commit 74432e4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
7 changes: 3 additions & 4 deletions AuroraPatch/AuroraPatchForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public partial class AuroraPatchForm : Form
private readonly Loader Loader;
private readonly List<Patch> Patches;

internal AuroraPatchForm(Loader loader) : base()
internal AuroraPatchForm() : base()
{
InitializeComponent();

Loader = loader;
Loader = Program.Loader;
try
{
Patches = Loader.FindPatches();
Expand Down Expand Up @@ -49,8 +49,7 @@ private void ButtonStart_Click(object sender, EventArgs e)
}
catch (Exception ex)
{
Program.Logger.LogCritical($"Failed to start Aurora. {ex}");
MessageBox.Show("Failed to start Aurora.");
Program.Logger.LogCritical($"Failed to start Aurora. {ex}", true);
}
}

Expand Down
10 changes: 9 additions & 1 deletion AuroraPatch/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ internal Loader(string exe, string checksum)

internal List<Patch> FindPatches()
{
var patches = new List<Patch>();
string patchesDirectory = Path.Combine(Path.GetDirectoryName(AuroraExecutablePath), "Patches");
Directory.CreateDirectory(patchesDirectory);

Expand All @@ -53,6 +52,9 @@ internal List<Patch> FindPatches()
}
}

var patches = new List<Patch>();
var names = new HashSet<string>();

foreach (var assembly in assemblies)
{
Program.Logger.LogInfo($"Trying to retrieve types from assembly {Path.GetFileName(assembly.Location)}");
Expand All @@ -61,12 +63,18 @@ internal List<Patch> FindPatches()
{
foreach (var type in assembly.GetTypes())
{
if (names.Contains(type.Name))
{
continue;
}

if (typeof(Patch).IsAssignableFrom(type))
{
Program.Logger.LogInfo($"Found patch {type.Name}");
var patch = (Patch)Activator.CreateInstance(type);
patch.Loader = this;
patches.Add(patch);
names.Add(type.Name);
}
}
}
Expand Down
29 changes: 22 additions & 7 deletions AuroraPatch/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,34 @@ public abstract class Patch
internal Loader Loader { get; set; }

/// <summary>
/// Get one of your dependencies.
/// Get one of your dependencies. Deprecated, use TryGetPatch instead.
/// </summary>
public T GetDependency<T>(string name) where T : Patch
public T GetDependency<T>(string name = null) where T : Patch
{
try
if (TryGetPatch<T>(out var patch))
{
return (T)Loader.LoadedPatches.Single(p => p.Name == name);
return patch;
}
catch (Exception e)
else
{
LogError($"Failed to get dependency {name}. {e}");
return default;
}
}

return null;
/// <summary>
/// Get another loaded patch.
/// </summary>
public bool TryGetPatch<T>(out T patch) where T : Patch
{
patch = (T)Loader.LoadedPatches.SingleOrDefault(x => x.Name == typeof(T).Name);

if (patch != default)
{
return true;
}
else
{
return false;
}
}

Expand Down
5 changes: 3 additions & 2 deletions AuroraPatch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace AuroraPatch
internal static class Program
{
internal static readonly Logger Logger = new Logger();
internal static Loader Loader { get; private set; }

/// <summary>
/// The main entry point for the application.
Expand Down Expand Up @@ -39,7 +40,7 @@ static void Main(string[] args)
Logger.LogInfo($"Found Aurora at {exe}");

var checksum = GetChecksum(File.ReadAllBytes(exe));
var loader = new Loader(exe, checksum);
Loader = new Loader(exe, checksum);

AppDomain.CurrentDomain.AssemblyResolve += (sender, a) =>
{
Expand All @@ -54,7 +55,7 @@ static void Main(string[] args)
return null;
};

var form = new AuroraPatchForm(loader);
var form = new AuroraPatchForm();
form.Show();

Application.Run();
Expand Down

0 comments on commit 74432e4

Please sign in to comment.