Skip to content

Commit

Permalink
Fixed bugs. Changed how btd6 api mods are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
gurrenm3 committed Dec 23, 2020
1 parent 13f30e2 commit 8e7df5d
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 239 deletions.
6 changes: 3 additions & 3 deletions BTD6 Mod Manager Updater/BTD6 Mod Manager Updater/App.config
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
</configuration>
Expand Up @@ -11,6 +11,7 @@
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -21,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -30,11 +32,13 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Resources\wrench.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
Expand Down
3 changes: 2 additions & 1 deletion BTD6 Mod Manager Updater/BTD6 Mod Manager Updater/Program.cs
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace BTD6_Mod_Manager_Updater
{
Expand Down Expand Up @@ -74,7 +75,7 @@ public static void CloseWindow(string windowMainTitle)
{
while (IsProgramRunning(windowMainTitle))
{
var openWindowProcesses = System.Diagnostics.Process.GetProcesses()
var openWindowProcesses = Process.GetProcesses()
.Where(p => p.MainWindowHandle != IntPtr.Zero && p.ProcessName != "explorer");

foreach (var a in openWindowProcesses)
Expand Down
25 changes: 0 additions & 25 deletions BTD6 Mod Manager.Lib/MelonMods/MelonMod_Handler.cs
Expand Up @@ -16,34 +16,9 @@ public static void HandleUpdates(string downloadDir, string melonModHandler_path
{
var updater = new MelonLoader_Updater(downloadDir, melonModHandler_path);
updater.HandleUpdates();
/*UpdateHandler update = new UpdateHandler()
{
GitApiReleasesURL = melonLoaderApiURL,
ProjectExePath = melonModHandler_path,
InstallDirectory = downloadDir,
ProjectName = "MelonLoader",
UpdatedZipName = "MelonLoader.zip",
GitFileIndexsToDownload = new List<int>() { 1 }
};
update.DeleteUpdater();
update.HandleUpdates(false, false, true);*/

}

/*private string GetGitText()
{
WebReader reader = new WebReader();
return reader.ReadText_FromURL(melonLoaderApiURL);
}

private bool IsUpdate(string melonModHandler_path, string aquiredGitText)
{
if (!File.Exists(melonModHandler_path))
return true;
return UpdateHandler.IsUpdate(melonModHandler_path, "MelonLoader", aquiredGitText);
}*/

/// <summary>
/// Get the Assembly of the mod that is calling NKHook6 code
/// </summary>
Expand Down
26 changes: 8 additions & 18 deletions BTD6 Mod Manager.Lib/Updaters/MelonLoader_Updater.cs
Expand Up @@ -47,7 +47,7 @@ public void HandleUpdates()
bool isUpdate = IsUpdate(releaseConfig);
if (!File.Exists(MelonHandlerDllPath))
isUpdate = true;

if (!isUpdate)
{
Logger.Log("MelonLoader is up to date!");
Expand All @@ -64,9 +64,12 @@ public void HandleUpdates()
return;
}
}
else if (isUpdate && !File.Exists(MelonHandlerDllPath))
Logger.Log("MelonLoader is not installed. Installing MelonLoader");

DownloadUpdates();
ExtractUpdater();

if (File.Exists(MelonHandlerDllPath))
Logger.Log("Successfully installed MelonLoader", OutputType.Both);
else
Expand Down Expand Up @@ -154,6 +157,7 @@ private void DownloadUpdates()
FileDownloader downloader = new FileDownloader();
downloader.DownloadFile(file, DownloadDir);
}

}

private List<string> GetDownloadURLs()
Expand All @@ -175,11 +179,11 @@ private List<string> GetDownloadURLs()

private void ExtractUpdater()
{
var files = new DirectoryInfo(DownloadDir).GetFiles();
var files = new DirectoryInfo(DownloadDir).GetFiles("*.zip");
foreach (var file in files)
{
string filename = file.Name;
if (!filename.EndsWith(".zip") && !filename.EndsWith(".rar") && !filename.EndsWith(".7z"))
if (!filename.ToLower().Contains("melonloader"))
continue;

using (ZipArchive archive = ZipFile.OpenRead(file.FullName))
Expand All @@ -200,19 +204,5 @@ private void ExtractUpdater()
}
}
}


private void LaunchUpdater()
{
var updaterPath = DownloadDir + "\\Updater.exe";
if (!File.Exists(updaterPath))
{
Logger.Log("ERROR! Unable to find updater. You will need to close BTD6 Mod Manager" +
$" and manually extract the updater from: {updaterPath}");
return;
}

Process.Start(updaterPath, "launched_from_BTD6 Mod Manager");
}
}
}
}
101 changes: 2 additions & 99 deletions BTD6 Mod Manager.wpf/Classes/Launcher.cs
Expand Up @@ -14,111 +14,14 @@ class Launcher
{
public static void Launch()
{
var launcher = new Launcher();

TempSettings.Instance.SaveSettings();

bool inject = false;
foreach (var item in TempSettings.Instance.LastUsedMods)
{
if (item.ToLower().EndsWith(".btd6mod"))
{
inject = true;
break;
}
}

if (inject)
BgThread.AddToQueue(() => launcher.LaunchBTD6());
else
{
var gameInfo = GameInfo.GetGame(SessionData.CurrentGame);

if (!Utility.IsProgramRunning(gameInfo.ProcName, out var btd6Proc))
Process.Start("steam://rungameid/" + gameInfo.SteamID);
}
}

public void LaunchBTD6()
{
BTD6_CrashHandler handler = new BTD6_CrashHandler();
handler.EnableCrashLog();

int injectWaitTime = WaitForBTD6(out Process btd6Proc);

if (SessionData.LoadedMods == null)
return;

HandleInjectionTimer(injectWaitTime);

handler.CheckForCrashes();

InjectMods(btd6Proc);
}

private int WaitForBTD6(out Process btd6Proc)
{
int injectWaitTime = 15000;
var gameInfo = GameInfo.GetGame(SessionData.CurrentGame);

if (!Utility.IsProgramRunning(gameInfo.ProcName, out btd6Proc))
if (!Utility.IsProgramRunning(gameInfo.ProcName, out var btd6Proc))
Process.Start("steam://rungameid/" + gameInfo.SteamID);
else
injectWaitTime = 0;

while (!Utility.IsProgramRunning(gameInfo.ProcName, out btd6Proc))
Thread.Sleep(1000);

return injectWaitTime;
}

private void HandleInjectionTimer(int injectWaitTime)
{
while (injectWaitTime > 0)
{
MainWindow.instance.Timer_TextBlock.Dispatcher.BeginInvoke((System.Action)(() =>
{
MainWindow.instance.Timer_TextBlock.Visibility = Visibility.Visible;
var timeLeft = injectWaitTime / 1000;
if (timeLeft == 1)
MainWindow.instance.Timer_TextBlock.Text = "Time to inject: " + timeLeft + " second";
else
MainWindow.instance.Timer_TextBlock.Text = "Time to inject: " + timeLeft + " seconds";
}));

Thread.Sleep(1000);
injectWaitTime -= 1000;

if (injectWaitTime <= 0)
{
MainWindow.instance.Timer_TextBlock.Dispatcher.BeginInvoke((System.Action)(() =>
{
MainWindow.instance.Timer_TextBlock.Visibility = Visibility.Collapsed;
}));
}
}
}

private void InjectMods(Process btd6Proc)
{
bool modsInjected = false;
Logger.Log("Injecting mods...");
foreach (var modPath in SessionData.LoadedMods)
{
if (!File.Exists(modPath))
{
Logger.Log("The BTD6 mod \"" + modPath + "\" could not be found. Failed to inject it");
continue;
}
Logger.Log("Error! Injector has been disabled. If you are seeing this message please contact devs", OutputType.Both);
return;
//BTD_Backend.Natives.Injector.InjectDll(modPath, btd6Proc);
modsInjected = true;
Logger.Log("Please close BTD6 to continue...", OutputType.Both);
}

if (modsInjected)
Logger.Log("Mods Injected...");
}
}
}
1 change: 1 addition & 0 deletions BTD6 Mod Manager.wpf/Classes/TempSettings.cs
Expand Up @@ -35,6 +35,7 @@ public static TempSettings Instance
public bool IsNewUser { get; set; } = true;
public bool ConsoleFlash { get; set; } = true;
public bool LoadedFirstMod { get; set; } = false;
public bool ShownBtdApiInjectorMessage { get; set; } = false;
public GameType LastGame { get; set; } = GameType.BTD6;
public List<string> LastUsedMods { get; set; } = new List<string>();
public string BTD6_ModsDir { get; set; }
Expand Down

0 comments on commit 8e7df5d

Please sign in to comment.