Skip to content

Commit

Permalink
CleanUP
Browse files Browse the repository at this point in the history
  • Loading branch information
netquick committed Jul 15, 2024
1 parent 748df13 commit a72b4d3
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 126 deletions.
18 changes: 9 additions & 9 deletions DeFRaG_Helper/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Initialize logging
SimpleLogger.Log("Application starting");
MessageHelper.Log("Application starting");
LoadConfigurationAndStartAsync().ContinueWith(_ =>
{
// This ensures the continuation runs on the UI thread
Dispatcher.Invoke(() =>
{
// Now that configuration and resources are loaded, show the main window
SimpleLogger.Log("Main window created");
MessageHelper.Log("Main window created");
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
});
Expand All @@ -46,32 +46,32 @@ private async void StartDelayedTasks()
private async Task LoadConfigurationAndStartAsync()
{
// Ensure the configuration is loaded before proceeding
await SimpleLogger.LogAsync("Loading configuration");
await MessageHelper.LogAsync("Loading configuration");

await AppConfig.LoadConfigurationAsync();
await SimpleLogger.LogAsync($"Configuration loaded: {AppConfig.GameDirectoryPath}");
await MessageHelper.LogAsync($"Configuration loaded: {AppConfig.GameDirectoryPath}");

ApplyThemeColor();
// Create an instance of MapHistoryManager
SimpleLogger.Log("Creating MapHistoryManager");
MessageHelper.Log("Creating MapHistoryManager");
var mapHistoryManager = MapHistoryManager.GetInstance("DeFRaG_Helper");
SimpleLogger.Log("MapHistoryManager created");
MessageHelper.Log("MapHistoryManager created");

await AppConfig.EnsureDatabaseExistsAsync();
SimpleLogger.Log("Database exists");
MessageHelper.Log("Database exists");
// The main window creation and showing is moved to the continuation of this method in OnStartup
}
private void ApplyThemeColor()
{
if (!string.IsNullOrEmpty(AppConfig.SelectedColor))
{
SimpleLogger.Log($"Applying theme color: {AppConfig.SelectedColor}");
MessageHelper.Log($"Applying theme color: {AppConfig.SelectedColor}");
// Assuming AppConfig.SelectedColor is a string like "Red", "Yellow", etc.
var color = (Color)ColorConverter.ConvertFromString(AppConfig.SelectedColor);
var brush = new SolidColorBrush(color);
Current.Resources["ThemeColor"] = brush;
}
else { SimpleLogger.Log("No theme color selected");
else { MessageHelper.Log("No theme color selected");
}
}
}
Expand Down
42 changes: 21 additions & 21 deletions DeFRaG_Helper/Config/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static AppConfig()
if (!Directory.Exists(appFolder))
{
Directory.CreateDirectory(appFolder);
SimpleLogger.Log($"Created directory: {appFolder}");
MessageHelper.Log($"Created directory: {appFolder}");
}
// Default values for new properties
DatabasePath = Path.Combine(appFolder, "MapData.db");
Expand All @@ -42,7 +42,7 @@ public static async Task LoadConfigurationAsync()
{
if (File.Exists(configFilePath))
{
await SimpleLogger.LogAsync($"Found config {configFilePath}");
await MessageHelper.LogAsync($"Found config {configFilePath}");

string json = await File.ReadAllTextAsync(configFilePath);
var config = JsonSerializer.Deserialize<Configuration>(json);
Expand All @@ -54,25 +54,25 @@ public static async Task LoadConfigurationAsync()
DatabaseUrl = config?.DatabaseUrl ?? DatabaseUrl;
MenuState = config?.MenuState ?? MenuState; // Use default if not set
ConnectionString = config?.ConnectionString ?? ConnectionString; // Use default if not set
await SimpleLogger.LogAsync($"GameDirectoryPath: {GameDirectoryPath}");
await SimpleLogger.LogAsync($"SelectedColor: {SelectedColor}");
await SimpleLogger.LogAsync($"ButtonState: {ButtonState}");
await SimpleLogger.LogAsync($"PhysicsSetting: {PhysicsSetting}");
await SimpleLogger.LogAsync($"DatabasePath: {DatabasePath}");
await SimpleLogger.LogAsync($"DatabaseUrl: {DatabaseUrl}");
await MessageHelper.LogAsync($"GameDirectoryPath: {GameDirectoryPath}");
await MessageHelper.LogAsync($"SelectedColor: {SelectedColor}");
await MessageHelper.LogAsync($"ButtonState: {ButtonState}");
await MessageHelper.LogAsync($"PhysicsSetting: {PhysicsSetting}");
await MessageHelper.LogAsync($"DatabasePath: {DatabasePath}");
await MessageHelper.LogAsync($"DatabaseUrl: {DatabaseUrl}");
}
else
{
//create the file if it doesn't exist

await SimpleLogger.LogAsync($"{configFilePath} not found");
await MessageHelper.LogAsync($"{configFilePath} not found");
//await SaveConfigurationAsync();

}
}
catch (Exception ex)
{
SimpleLogger.Log(ex.Message);
MessageHelper.Log(ex.Message);
throw;
}

Expand Down Expand Up @@ -100,39 +100,39 @@ public static async Task SaveConfigurationAsync()
ConnectionString = ConnectionString

};
SimpleLogger.Log($"GameDirectoryPath: {GameDirectoryPath}");
SimpleLogger.Log($"SelectedColor: {SelectedColor}");
SimpleLogger.Log($"ButtonState: {ButtonState}");
SimpleLogger.Log($"PhysicsSetting: {PhysicsSetting}");
SimpleLogger.Log($"DatabasePath: {DatabasePath}");
SimpleLogger.Log($"DatabaseUrl: {DatabaseUrl}");
MessageHelper.Log($"GameDirectoryPath: {GameDirectoryPath}");
MessageHelper.Log($"SelectedColor: {SelectedColor}");
MessageHelper.Log($"ButtonState: {ButtonState}");
MessageHelper.Log($"PhysicsSetting: {PhysicsSetting}");
MessageHelper.Log($"DatabasePath: {DatabasePath}");
MessageHelper.Log($"DatabaseUrl: {DatabaseUrl}");

var options = new JsonSerializerOptions { WriteIndented = true };
string json = JsonSerializer.Serialize(config, options);
try
{
await File.WriteAllTextAsync(configFilePath, json);
SimpleLogger.Log("Configuration saved");
MessageHelper.Log("Configuration saved");
}
catch (Exception ex)
{
SimpleLogger.Log(ex.Message);
MessageHelper.Log(ex.Message);
throw;
}
}
public static async Task EnsureDatabaseExistsAsync()
{
if (!File.Exists(DatabasePath))
{
await SimpleLogger.LogAsync("Database not found, downloading...");
await MessageHelper.LogAsync("Database not found, downloading...");
// Use Downloader to download the database
// Assuming Downloader has a static method DownloadFileAsync for this purpose
await Downloader.DownloadFileAsync(DatabaseUrl, DatabasePath, null);
await SimpleLogger.LogAsync("Database downloaded");
await MessageHelper.LogAsync("Database downloaded");
}
else
{
await SimpleLogger.LogAsync($"Database found at {DatabasePath}");
await MessageHelper.LogAsync($"Database found at {DatabasePath}");
}
}

Expand Down
50 changes: 25 additions & 25 deletions DeFRaG_Helper/Config/CheckGameInstall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CheckGameInstall
public async static void StartChecks()
{
// Check if the game directory path is set in the AppConfig class
SimpleLogger.Log("Checking game install");
MessageHelper.Log("Checking game install");
SetGameDirectoryPath();

// Await the fully initialized instance of MapViewModel
Expand All @@ -33,33 +33,33 @@ public async static void StartChecks()
public static bool CheckInstall(string path)
{
string[] dirs = System.IO.Directory.GetDirectories(path);
SimpleLogger.Log($"Checking for defrag folder in {path}");
MessageHelper.Log($"Checking for defrag folder in {path}");
foreach (string dir in dirs)
{
if (dir.Contains("defrag"))
{
SimpleLogger.Log("Defrag folder found");
MessageHelper.Log("Defrag folder found");
return true;
}
}
SimpleLogger.Log("Defrag folder not found");
MessageHelper.Log("Defrag folder not found");
return false;
}
//if the folder is found, we check if there is a file called "oDFe.x64.exe" or "oDFe.exe" in the "defrag" folder
public static bool CheckExe(string path)
{
//string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string[] files = System.IO.Directory.GetFiles(path);
SimpleLogger.Log($"Checking for oDFe.x64.exe or oDFe.exe in {path}");
MessageHelper.Log($"Checking for oDFe.x64.exe or oDFe.exe in {path}");
foreach (string file in files)
{
if (file.Contains("oDFe.x64.exe") || file.Contains("oDFe.exe"))
{
SimpleLogger.Log("oDFe.x64.exe or oDFe.exe found");
MessageHelper.Log("oDFe.x64.exe or oDFe.exe found");
return true;
}
}
SimpleLogger.Log("oDFe.x64.exe or oDFe.exe not found");
MessageHelper.Log("oDFe.x64.exe or oDFe.exe not found");
return false;
}

Expand All @@ -71,10 +71,10 @@ public async static void SetGameDirectoryPath()
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
if (string.IsNullOrEmpty(AppConfig.GameDirectoryPath))
{
SimpleLogger.Log("Game directory path not set");
MessageHelper.Log("Game directory path not set");
if (CheckInstall(path) && CheckExe(path))
{
SimpleLogger.Log($"Game found in {path}");
MessageHelper.Log($"Game found in {path}");
AppConfig.GameDirectoryPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//save the game directory path in the app.config file
await AppConfig.SaveConfigurationAsync();
Expand All @@ -84,32 +84,32 @@ public async static void SetGameDirectoryPath()
}
else
{
SimpleLogger.Log($"Game not found in {path}");
MessageHelper.Log($"Game not found in {path}");
//prompt user to set game directory path in a folder browser dialog
//App.Current.Dispatcher.Invoke(() => MainWindow.Instance.ShowMessage("Game not found"));
//open file browser dialog from wpf (as forms not working) to set game directory path
var tempDir = BrowseFolder();
if (tempDir != null)
{
AppConfig.GameDirectoryPath = tempDir;
SimpleLogger.Log($"Game directory path set to {tempDir}");
MessageHelper.Log($"Game directory path set to {tempDir}");
//save the game directory path in the app.config file
await AppConfig.SaveConfigurationAsync();
SimpleLogger.Log("Game directory path saved");
MessageHelper.Log("Game directory path saved");
//we need check again, if the folder and the file are found now, if yes we set the game directory path in the AppConfig class to the actual application path, if no, we install the game in method InstallGame()
path = tempDir;
SimpleLogger.Log($"Checking game install in {path}");
MessageHelper.Log($"Checking game install in {path}");

if (CheckInstall(path) && CheckExe(path))
{
SimpleLogger.Log($"Game found in {path}");
MessageHelper.Log($"Game found in {path}");
AppConfig.GameDirectoryPath = path;
App.Current.Dispatcher.Invoke(() => MainWindow.Instance.ShowMessage("Game found"));
await AppConfig.SaveConfigurationAsync();
}
else
{
SimpleLogger.Log($"Game not found in {path}");
MessageHelper.Log($"Game not found in {path}");
InstallGame();
}

Expand All @@ -127,18 +127,18 @@ public async static void SetGameDirectoryPath()
}
else
{
SimpleLogger.Log($"Game directory path set to {AppConfig.GameDirectoryPath}");
MessageHelper.Log($"Game directory path set to {AppConfig.GameDirectoryPath}");
}
}

//method to install the game
private async static Task InstallGame()
{
App.Current.Dispatcher.Invoke(() => MainWindow.Instance.ShowMessage("Game data will be downloaded"));
SimpleLogger.Log("Game data will be downloaded");
MessageHelper.Log("Game data will be downloaded");
// Create a folder called "defrag" in the GameDirectoryPath
string path = AppConfig.GameDirectoryPath;
SimpleLogger.Log($"Game directory path set to {path}");
MessageHelper.Log($"Game directory path set to {path}");
System.IO.Directory.CreateDirectory(path + "\\defrag");

// Prepare the progress handler to update the UI
Expand All @@ -149,26 +149,26 @@ private async static Task InstallGame()
IProgress<double> progress = progressHandler;

// Download the game from the server
SimpleLogger.Log("Downloading game data");
MessageHelper.Log("Downloading game data");
await Downloader.DownloadFileAsync("https://github.com/JBustos22/oDFe/releases/download/latest/oDFe-windows-x86_64.zip", path + "\\oDFe-windows-x86_64.zip", progress);
// Extract the engine from the zip file
SimpleLogger.Log($"Extracting game data in {path}");
MessageHelper.Log($"Extracting game data in {path}");
await Downloader.UnpackFile(path + "\\oDFe-windows-x86_64.zip", path, progress);

//check if the defrag folder already contains autoexec.cfg. if not we need install the gamedata
if (!System.IO.File.Exists(path + "\\defrag\\autoexec.cfg"))
{
SimpleLogger.Log("Game data will be installed");
MessageHelper.Log("Game data will be installed");
//Download the game data from
await Downloader.DownloadFileAsync("https://dl.defrag.racing/downloads/game-bundles/DeFRaG%20Bundle%20all-in-one%20Windows%2064bit.7z", path + "\\DeFRaG Bundle all-in-one Windows 64bit.7z", progress);
//Extract the game data from the 7z file
SimpleLogger.Log("Extracting game data");
MessageHelper.Log("Extracting game data");
await Downloader.UnpackFile(path + "\\DeFRaG Bundle all-in-one Windows 64bit.7z", path, progress);
//move the contents of the extracted folder (DeFRaG Bundle all-in-one Windows 64bit) to the root folder
SimpleLogger.Log($"Moving game data in {path}");
MessageHelper.Log($"Moving game data in {path}");
await Downloader.MoveFolderContents(path + "\\DeFRaG Bundle all-in-one Windows 64bit", path, progress);
//create "archive" folder in the root folder
SimpleLogger.Log("Creating archive folder");
MessageHelper.Log("Creating archive folder");
System.IO.Directory.CreateDirectory(path + "\\archive");

}
Expand All @@ -191,7 +191,7 @@ public static string BrowseFolder()
// Set the filter to only show directories
openFileDialog.Filter = "Folder|*.none";
openFileDialog.FileName = "Select Folder";
SimpleLogger.Log("Opening folder browser dialog");
MessageHelper.Log("Opening folder browser dialog");
if (openFileDialog.ShowDialog() == true)
{
// Return the directory path
Expand Down
10 changes: 5 additions & 5 deletions DeFRaG_Helper/Helpers/CreateAndUpdateDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static async Task UpdateDB()
catch (Exception ex)
{
// Handle any errors that might occur during the update process
SimpleLogger.Log($"An error occurred during the database update: {ex.Message}");
MessageHelper.Log($"An error occurred during the database update: {ex.Message}");
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ private static async Task ParsePagesAsync(int pageCount)
else
{
consecutiveExistingMaps = 0; // Reset the counter if a new map is found
SimpleLogger.Log($"Parsing map: {fullDetailsPageUrl}");
MessageHelper.Log($"Parsing map: {fullDetailsPageUrl}");
}

// Assuming the third cell contains the filename in an <a> tag's href attribute
Expand All @@ -227,12 +227,12 @@ private static async Task ParsePagesAsync(int pageCount)
}
}
}
SimpleLogger.Log($"Page {i + 1} of {pageCount} processed.");
MessageHelper.Log($"Page {i + 1} of {pageCount} processed.");

}
} catch (Exception ex)
{
SimpleLogger.Log($"An error occurred during the database update: {ex.Message}");
MessageHelper.Log($"An error occurred during the database update: {ex.Message}");
throw;
}
}
Expand Down Expand Up @@ -675,7 +675,7 @@ public static List<string> ExtractImageUrls(string html)
var slideMatch = slideRegex.Match(dataAttributes);
if (slideMatch.Success)
{
//SimpleLogger.Log($"Found slide {i}: {slideMatch.Groups[1].Value}");
//MessageHelper.Log($"Found slide {i}: {slideMatch.Groups[1].Value}");
imageUrls.Add(slideMatch.Groups[1].Value);
}
}
Expand Down
Loading

0 comments on commit a72b4d3

Please sign in to comment.