Skip to content

Commit

Permalink
bugfixes + autoload before manual load
Browse files Browse the repository at this point in the history
  • Loading branch information
blushiemagic committed Aug 7, 2015
1 parent 057c339 commit ffb3aff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
29 changes: 20 additions & 9 deletions Patcher/patches/tModLoader/Terraria.ModLoader/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static class ModLoader
public static readonly string ModSourcePath = Main.SavePath + Path.DirectorySeparatorChar + "Mod Sources";
public static readonly string DllPath = Main.SavePath + Path.DirectorySeparatorChar + "dllReferences";
private static bool referencesLoaded = false;
private static bool assemblyResolverAdded = false;
private static readonly IList<string> buildReferences = new List<string>();
internal const int earliestRelease = 149;
internal static string modToBuild;
Expand All @@ -44,6 +45,22 @@ private static void LoadReferences()
referencesLoaded = true;
}

private static void AddAssemblyResolver()
{
if (assemblyResolverAdded)
{
return;
}
AppDomain.CurrentDomain.AssemblyResolve += ResolveDllReference;
assemblyResolverAdded = true;
}

private static Assembly ResolveDllReference(object sender, ResolveEventArgs args)
{
Directory.CreateDirectory(DllPath);
return Assembly.LoadFrom(DllPath + Path.DirectorySeparatorChar + args.Name + ".dll");
}

internal static bool ModLoaded(string name)
{
return loadedMods.Contains(name);
Expand Down Expand Up @@ -76,8 +93,8 @@ private static void do_Load(object threadContext)
Interface.loadMods.SetProgressInit(mod.Name, num, mods.Count);
try
{
mod.Load();
mod.Autoload();
mod.Load();
}
catch (Exception e)
{
Expand Down Expand Up @@ -212,12 +229,6 @@ private static bool LoadMods()

private static void LoadMod(string modFile, BuildProperties properties)
{
Directory.CreateDirectory(DllPath);
foreach (string dllReference in properties.dllReferences)
{
string dllFile = DllPath + Path.DirectorySeparatorChar + dllReference + ".dll";
Assembly.Load(File.ReadAllBytes(dllFile));
}
Assembly modCode;
using (FileStream fileStream = File.OpenRead(modFile))
{
Expand Down Expand Up @@ -381,7 +392,7 @@ internal static void do_BuildAllMods(object threadContext)
}
num++;
}
Main.menuMode = flag ? Interface.errorMessageID : (reloadAfterBuild ? Interface.loadModsID : 0);
Main.menuMode = flag ? Interface.errorMessageID : (reloadAfterBuild ? Interface.reloadModsID : 0);
}

internal static void BuildMod()
Expand Down Expand Up @@ -443,7 +454,7 @@ internal static bool do_BuildMod(object threadContext)
EnableMod(file);
if (!buildAll)
{
Main.menuMode = reloadAfterBuild ? Interface.loadModsID : 0;
Main.menuMode = reloadAfterBuild ? Interface.reloadModsID : 0;
}
return true;
}
Expand Down
29 changes: 20 additions & 9 deletions Terraria.ModLoader/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class ModLoader
public static readonly string ModSourcePath = Main.SavePath + Path.DirectorySeparatorChar + "Mod Sources";
public static readonly string DllPath = Main.SavePath + Path.DirectorySeparatorChar + "dllReferences";
private static bool referencesLoaded = false;
private static bool assemblyResolverAdded = false;
private static readonly IList<string> buildReferences = new List<string>();
internal const int earliestRelease = 149;
internal static string modToBuild;
Expand All @@ -43,6 +44,22 @@ private static void LoadReferences()
referencesLoaded = true;
}

private static void AddAssemblyResolver()
{
if(assemblyResolverAdded)
{
return;
}
AppDomain.CurrentDomain.AssemblyResolve += ResolveDllReference;
assemblyResolverAdded = true;
}

private static Assembly ResolveDllReference(object sender, ResolveEventArgs args)
{
Directory.CreateDirectory(DllPath);
return Assembly.LoadFrom(DllPath + Path.DirectorySeparatorChar + args.Name + ".dll");
}

internal static bool ModLoaded(string name)
{
return loadedMods.Contains(name);
Expand Down Expand Up @@ -75,8 +92,8 @@ private static void do_Load(object threadContext)
Interface.loadMods.SetProgressInit(mod.Name, num, mods.Count);
try
{
mod.Load();
mod.Autoload();
mod.Load();
}
catch(Exception e)
{
Expand Down Expand Up @@ -211,12 +228,6 @@ private static bool LoadMods()

private static void LoadMod(string modFile, BuildProperties properties)
{
Directory.CreateDirectory(DllPath);
foreach(string dllReference in properties.dllReferences)
{
string dllFile = DllPath + Path.DirectorySeparatorChar + dllReference + ".dll";
Assembly.Load(File.ReadAllBytes(dllFile));
}
Assembly modCode;
using(FileStream fileStream = File.OpenRead(modFile))
{
Expand Down Expand Up @@ -380,7 +391,7 @@ internal static void do_BuildAllMods(object threadContext)
}
num++;
}
Main.menuMode = flag ? Interface.errorMessageID : ( reloadAfterBuild ? Interface.loadModsID : 0);
Main.menuMode = flag ? Interface.errorMessageID : ( reloadAfterBuild ? Interface.reloadModsID : 0);
}

internal static void BuildMod()
Expand Down Expand Up @@ -439,7 +450,7 @@ internal static bool do_BuildMod(object threadContext)
EnableMod(file);
if (!buildAll)
{
Main.menuMode = reloadAfterBuild ? Interface.loadModsID : 0;
Main.menuMode = reloadAfterBuild ? Interface.reloadModsID : 0;
}
return true;
}
Expand Down

0 comments on commit ffb3aff

Please sign in to comment.