Skip to content

Commit

Permalink
Finally fixed startup crashes. It was a threading issue
Browse files Browse the repository at this point in the history
  • Loading branch information
gurrenm3 committed Dec 23, 2020
1 parent bca1279 commit 2566dea
Show file tree
Hide file tree
Showing 19 changed files with 334 additions and 225 deletions.
Expand Up @@ -23,6 +23,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -33,6 +34,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Resources\wrench.ico</ApplicationIcon>
Expand Down
9 changes: 9 additions & 0 deletions BTD6 Mod Manager.Lib/BTD6 Mod Manager.Lib.csproj
Expand Up @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,12 +30,19 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="MelonLoader.ModHandler, Version=0.2.7.4, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\BTD6 Mod Manager.wpf\bin\Debug\MelonLoader.ModHandler.dll</HintPath>
</Reference>
<Reference Include="Microsoft.WindowsAPICodePack, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\BTD6 Mod Manager.wpf\packages\WindowsAPICodePack-Core.1.1.1\lib\Microsoft.WindowsAPICodePack.dll</HintPath>
</Reference>
<Reference Include="Microsoft.WindowsAPICodePack.Shell, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\BTD6 Mod Manager.wpf\packages\WindowsAPICodePack-Shell.1.1.1\lib\Microsoft.WindowsAPICodePack.Shell.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\BTD6 Mod Manager.wpf\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -69,6 +77,7 @@
<Compile Include="Natives\Win32.cs" />
<Compile Include="Persistance\UserData.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Serializer.cs" />
<Compile Include="Updaters\MelonLoader_Updater.cs" />
<Compile Include="Updaters\ModManager_Updater.cs" />
<Compile Include="Updaters\Universal_Updater.cs" />
Expand Down
12 changes: 12 additions & 0 deletions BTD6 Mod Manager.Lib/IO/FileIO.cs
@@ -1,4 +1,5 @@
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.Diagnostics;
using System.IO;

Expand Down Expand Up @@ -30,6 +31,17 @@ public static string BrowseForFile(string title, string defaultExt, string filte
return fileDiag.FileName;
}

public static string BrowseForDirectory(string title, string startDir)
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
dialog.Title = title;
dialog.Multiselect = false;
dialog.InitialDirectory = startDir;
return (dialog.ShowDialog() == CommonFileDialogResult.Ok) ? dialog.FileName : string.Empty;
}


/// <summary>
/// Copy a directory and all of its contents, while preserving file structure
/// </summary>
Expand Down
51 changes: 51 additions & 0 deletions BTD6 Mod Manager.Lib/Serializer.cs
@@ -0,0 +1,51 @@
using Newtonsoft.Json;
using System;
using System.IO;

namespace BTD6_Mod_Manager.Lib
{
public class Serializer
{
public static T LoadFromFile<T>(string filePath) where T : class
{
if (!IsPathValid(filePath))
return null;

string json = File.ReadAllText(filePath);
if (String.IsNullOrEmpty(json))
return null;

return JsonConvert.DeserializeObject<T>(json);
}

private static bool IsPathValid(string filePath)
{
Guard.ThrowIfStringIsNull(filePath, "Can't load file, path is null");
if (!File.Exists(filePath))
return false;

return true;
}

/// <summary>
/// Save any class to file. This is not done yet
/// </summary>
public static void SaveToFile<T>(T jsonObject, string savePath, bool overwriteExisting = true) where T : class
{
Guard.ThrowIfStringIsNull(savePath, "Can't save file, save path is null");
CreateDirIfNotFound(savePath);
string json = JsonConvert.SerializeObject(jsonObject, Formatting.Indented);

bool keepOriginal = !overwriteExisting;
StreamWriter serialize = new StreamWriter(savePath, keepOriginal);
serialize.Write(json);
serialize.Close();
}

private static void CreateDirIfNotFound(string dir)
{
FileInfo f = new FileInfo(dir);
Directory.CreateDirectory(f.Directory.FullName);
}
}
}
4 changes: 2 additions & 2 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 @@ -69,7 +69,7 @@ public void HandleUpdates()

DownloadUpdates();
ExtractUpdater();

if (File.Exists(MelonHandlerDllPath))
Logger.Log("Successfully installed MelonLoader", OutputType.Both);
else
Expand Down
2 changes: 2 additions & 0 deletions BTD6 Mod Manager.Lib/packages.config
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net472" />
<package id="WindowsAPICodePack-Core" version="1.1.1" targetFramework="net472" />
<package id="WindowsAPICodePack-Shell" version="1.1.1" targetFramework="net472" />
</packages>
9 changes: 6 additions & 3 deletions BTD6 Mod Manager.wpf/BTD6 Mod Manager.csproj
Expand Up @@ -40,6 +40,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
Expand All @@ -50,7 +51,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Resources\wrench.ico</ApplicationIcon>
Expand Down Expand Up @@ -105,10 +106,12 @@
</ApplicationDefinition>
<Compile Include="Classes\BTD6-CrashHandler.cs" />
<Compile Include="Classes\FileIO.cs" />
<Compile Include="Classes\Launcher.cs" />
<Compile Include="Classes\SessionData.cs" />
<Compile Include="Launcher.cs" />
<Compile Include="Classes\TempGuard.cs" />
<Compile Include="Classes\TempSettings.cs" />
<Compile Include="IO\FileIO.cs" />
<Compile Include="Persistance\Settings.cs" />
<Compile Include="SessionData.cs" />
<Compile Include="UserControls\ModItem_UserControl.xaml.cs">
<DependentUpon>ModItem_UserControl.xaml</DependentUpon>
</Compile>
Expand Down
1 change: 1 addition & 0 deletions BTD6 Mod Manager.wpf/Classes/FileIO.cs
Expand Up @@ -58,6 +58,7 @@ public static string BrowseForDirectory(string title, string startDir)
else
return null;
}

public static void CopyDirsAndContents(string source, string destination)
{
string[] split = source.Split('\\');
Expand Down
12 changes: 0 additions & 12 deletions BTD6 Mod Manager.wpf/Classes/SessionData.cs

This file was deleted.

34 changes: 22 additions & 12 deletions BTD6 Mod Manager.wpf/Classes/TempSettings.cs
Expand Up @@ -4,14 +4,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace BTD6_Mod_Manager.Classes
{
class TempSettings
/*class TempSettings
{
private static TempSettings instance;
Expand All @@ -38,12 +34,14 @@ public static TempSettings Instance
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; }
[JsonProperty]
private string BTD6_ModsDir { get; set; }
public TempSettings()
{
/*settingsFileName = "settings.json";*/
/*MainSettingsDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\TD Loader";*/
*//*settingsFileName = "settings.json";*/
/*MainSettingsDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\TD Loader";*//*
}
public TempSettings LoadSettings()
Expand Down Expand Up @@ -90,16 +88,28 @@ public void SaveSettings()
public void CreateNewSettings()
{
BTD6_ModsDir = GameInfo.GetGame(GameType.BTD6).GameDir + "\\Mods";
BTD6_ModsDir = SteamUtils.GetGameDir(GameType.BTD6) + "\\Mods";
Logger.Log(BTD6_ModsDir, OutputType.Both);
SaveSettings();
}
public string GetModsDir(GameType game) => BTD6_ModsDir;
public string GetModsDir() => GetModsDir(SessionData.CurrentGame);
public string GetModsDir(GameType game)
{
string modsDir = BTD6_ModsDir;
if (String.IsNullOrEmpty(modsDir))
Logger.Log("Mods Directory is null. Please create a Mods folder in your BTD6 folder", OutputType.Both);
Directory.CreateDirectory(modsDir);
return modsDir;
}
public void SetModsDir(string path) => SetModsDir(SessionData.CurrentGame, path);
public void SetModsDir(GameType game, string path)
{
BTD6_ModsDir = path;
SaveSettings();
}
}
}
}*/
}
23 changes: 23 additions & 0 deletions BTD6 Mod Manager.wpf/IO/FileIO.cs
@@ -0,0 +1,23 @@
using BTD6_Mod_Manager.Lib.Game;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BTD6_Mod_Manager.IO
{
public class FileIO : Lib.IO.FileIO
{
public static string BrowseForGameDir(string windowTitle = "Browse for BloonsTD6 directory")
{
return BrowseForDirectory(windowTitle, Environment.CurrentDirectory);
}

public static string AutoDetectGameDir() => AutoDetectGameDir(SessionData.currentGame);
public static string AutoDetectGameDir(GameType gameType)
{
return SteamUtils.GetGameDir(gameType);
}
}
}
Expand Up @@ -8,13 +8,13 @@
using System.Windows.Controls;
using System.Windows.Media.Media3D;

namespace BTD6_Mod_Manager.Classes
namespace BTD6_Mod_Manager
{
class Launcher
{
public static void Launch()
{
var gameInfo = GameInfo.GetGame(SessionData.CurrentGame);
var gameInfo = GameInfo.GetGame(SessionData.currentGame);

if (!Utility.IsProgramRunning(gameInfo.ProcName, out var btd6Proc))
Process.Start("steam://rungameid/" + gameInfo.SteamID);
Expand Down
5 changes: 3 additions & 2 deletions BTD6 Mod Manager.wpf/MainWindow.xaml
Expand Up @@ -35,8 +35,7 @@
</ItemsPanelTemplate>
</Menu.ItemsPanel>

<MenuItem x:Name="Settings_Button" Header="Settings" FontSize="16" Foreground="White" Background="#FF191919" BorderThickness="0" Click="Settings_Button_Click"/>
<MenuItem x:Name="OpenSettingsDir_Button" Header="Open Settings Dir" FontSize="16" Foreground="White" Background="#FF191919" BorderThickness="0" Click="OpenSettingsDir_Button_Click"/>
<MenuItem x:Name="BrowseModDir_Button" Header="Set Mods Dir" FontSize="16" Foreground="White" Background="#FF191919" BorderThickness="0" Click="BrowseModDir_Button_Click"/>
</Menu>
</xctk:DropDownButton.DropDownContent>
</xctk:DropDownButton>
Expand All @@ -50,6 +49,8 @@
</ItemsPanelTemplate>
</Menu.ItemsPanel>

<MenuItem x:Name="Settings_Button" Header="Settings" FontSize="16" Foreground="White" Background="#FF191919" BorderThickness="0" Click="Settings_Button_Click"/>
<MenuItem x:Name="OpenSettingsDir_Button" Header="Open Settings Dir" FontSize="16" Foreground="White" Background="#FF191919" BorderThickness="0" Click="OpenSettingsDir_Button_Click"/>
<MenuItem x:Name="OpenBTD6_ModDir_Button" Header="BTD6 Mods Directory" FontSize="16" Foreground="White" Background="#FF191919" BorderThickness="0" Click="OpenBTD6_ModDir_Button_Click"/>
</Menu>
</xctk:DropDownButton.DropDownContent>
Expand Down

0 comments on commit 2566dea

Please sign in to comment.