Skip to content

Commit

Permalink
Fixed bug with finding game directory and wrong melonloader
Browse files Browse the repository at this point in the history
  • Loading branch information
gurrenm3 committed Mar 4, 2023
1 parent 7bf3bbd commit 05737e6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 15 deletions.
6 changes: 5 additions & 1 deletion BTD6 Mod Manager.Lib/Logger.cs
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;

namespace BTD6_Mod_Manager.Lib
{
Expand Down Expand Up @@ -63,7 +64,10 @@ public static void Log(string text, OutputType output = OutputType.Console)
{
LogEvents args = new LogEvents();
args.Output = output;
args.Message = ">> " + text + Environment.NewLine;

string message = $">> {text}\n";
args.Message = message;
Debug.WriteLine(message);
Instance.OnMessageLogged(args);
}
}
Expand Down
40 changes: 30 additions & 10 deletions BTD6 Mod Manager.Lib/Persistance/UserData.cs
Expand Up @@ -19,7 +19,19 @@ public class UserData
public static string MainSettingsDir;
public static string UserDataFilePath;

public static UserData Instance;

private static UserData _instance;

public static UserData Instance
{
get
{
if (_instance == null)
_instance = LoadUserData();
return _instance;
}
set { _instance = value; }
}


/// <summary>
Expand Down Expand Up @@ -73,7 +85,7 @@ public class UserData
/// </summary>
#region BTD6
private static GameInfo btd6 = GameInfo.GetGame(GameType.BTD6);
public string BTD6Dir { get; set; } = btd6.GameDir;
public string BTD6Dir { get; set; }// = btd6.GameDir;
public string BTD6Version { get; set; } = FileIO.GetFileVersion(btd6.GameDir + "\\" + btd6.EXEName);
public string BTD6BackupDir { get; set; } = Environment.CurrentDirectory + "\\Backups\\" + btd6.Type.ToString();
#endregion
Expand Down Expand Up @@ -125,6 +137,14 @@ public UserData()

if (PreviousProjects == null)
PreviousProjects = new List<string>();

UserDataLoaded += UserData_UserDataLoaded;
}

private void UserData_UserDataLoaded(object sender, UserDataEventArgs e)
{
if (string.IsNullOrEmpty(btd6.GameDir) && !string.IsNullOrEmpty(BTD6Dir))
btd6.GameDir = BTD6Dir;
}

#endregion
Expand All @@ -149,29 +169,29 @@ public static void OpenSettingsDir()
/// <returns>The loaded userdata</returns>
public static UserData LoadUserData()
{
if (Instance == null)
Instance = new UserData();
/*if (Instance == null)
Instance = new UserData();*/

var user = new UserData();

if (!File.Exists(UserDataFilePath))
{
user.OnUserDataLoaded(new UserDataEventArgs());
return Instance;
return user;
}

string json = File.ReadAllText(UserDataFilePath);
if (json == "null" || !Guard.IsJsonValid(json))
if (json == "null" || string.IsNullOrEmpty(json) || !Guard.IsJsonValid(json))
{
Logger.Log("Userdata has invalid json, generating a new one.");
Instance = new UserData();
user = new UserData();
user.OnUserDataLoaded(new UserDataEventArgs());
return Instance;
return user;
}

Instance = JsonConvert.DeserializeObject<UserData>(json);
user = JsonConvert.DeserializeObject<UserData>(json);
user.OnUserDataLoaded(new UserDataEventArgs());
return Instance;
return user;
}

/// <summary>
Expand Down
53 changes: 51 additions & 2 deletions BTD6 Mod Manager.wpf/MainWindow.xaml.cs
Expand Up @@ -4,6 +4,7 @@
using BTD6_Mod_Manager.Lib.MelonMods;
using BTD6_Mod_Manager.Lib.Persistance;
using BTD6_Mod_Manager.Persistance;
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
Expand Down Expand Up @@ -52,6 +53,7 @@ private void OnFinishedLoading()
UserData.MainProgramName = "BTD6 Mod Manager";
UserData.MainSettingsDir = tdloaderDir;
UserData.UserDataFilePath = tdloaderDir + "\\userdata.json";
var userData = UserData.Instance;

SessionData.loadedMods = Settings.LoadedSettings.LastUsedMods;

Expand Down Expand Up @@ -107,11 +109,58 @@ private void Main_Loaded(object sender, RoutedEventArgs e)
Lib.Updaters.ModManager_Updater update = new Lib.Updaters.ModManager_Updater();
DeleteOldUpdaterFiles();
var game = GameInfo.GetGame(SessionData.currentGame);
if (game == null)
{
Logger.Log("Failed to get game info!");
return;
}

// check if game directory is found
if (string.IsNullOrEmpty(game.GameDir) || !Directory.Exists(game.GameDir))
{
Logger.Log($"Game directory for {SessionData.currentGame.ToString()} was not found! Please browse for {game.EXEName}", OutputType.MsgBox);
string path = FileIO.BrowseForFile($"Browse for {SessionData.currentGame.ToString()} EXE", ".exe", "Exe Files (*.exe)|*.exe", Environment.CurrentDirectory);
if (string.IsNullOrEmpty(path) || !File.Exists(path) || !path.EndsWith(game.EXEName))
{
Logger.Log("The path that was selected was not valid! The program will now close", OutputType.MsgBox);
Environment.Exit(0);
}

string gameDir = new FileInfo(path).Directory.FullName;
game.GameDir = gameDir;

switch (SessionData.currentGame)
{
case GameType.BTD5:
UserData.Instance.BTD5Dir = gameDir;
break;
case GameType.BTDB:
UserData.Instance.BTDBDir = gameDir;
break;
case GameType.BMC:
UserData.Instance.BMCDir = gameDir;
break;
case GameType.BTD6:
UserData.Instance.BTD6Dir = gameDir;
break;
case GameType.BTDAT:
UserData.Instance.BTDATDir = gameDir;
break;
default:
break;
}

UserData.SaveUserData();


Logger.Log($"Game Directory was set to {gameDir}", OutputType.MsgBox);
}

BgThread.AddToQueue(() =>
{
update.HandleUpdates();
string gameD = game.GameDir + "\\MelonLoader\\MelonLoader.dll";
MelonMod_Handler.HandleUpdates(game.GameDir, gameD);
string melonLoaderDll = game.GameDir + "\\MelonLoader\\net6\\MelonLoader.dll";
MelonMod_Handler.HandleUpdates(game.GameDir, melonLoaderDll);
});

// check for mod helper and download if not exists
Expand Down
4 changes: 2 additions & 2 deletions BTD6 Mod Manager.wpf/Properties/AssemblyInfo.cs
Expand Up @@ -49,5 +49,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.2.7")]
[assembly: AssemblyFileVersion("2.2.7")]
[assembly: AssemblyVersion("2.2.8")]
[assembly: AssemblyFileVersion("2.2.8")]

0 comments on commit 05737e6

Please sign in to comment.