Skip to content

Commit

Permalink
Merge pull request #45 from BoiHanny/Dev-Master
Browse files Browse the repository at this point in the history
Dev master
  • Loading branch information
BoiHanny authored Feb 23, 2024
2 parents 8579c16 + 639d620 commit 94f1e98
Show file tree
Hide file tree
Showing 33 changed files with 1,737 additions and 63 deletions.
7 changes: 7 additions & 0 deletions MagicChatboxV2/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Application
x:Class="MagicChatboxV2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MagicChatboxV2">
<Application.Resources />
</Application>
100 changes: 100 additions & 0 deletions MagicChatboxV2/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using Microsoft.Extensions.DependencyInjection;
using System.Windows;
using MagicChatboxV2.Services;
using MagicChatboxV2.Helpers;
using MagicChatboxV2.Extensions;
using System.Reflection;
using Serilog;
using System.Windows.Threading;
using MagicChatboxV2.UIVM.Windows;

namespace MagicChatboxV2
{
public partial class App : Application
{
private IServiceProvider serviceProvider;
public delegate PrimaryInterface PrimaryInterfaceFactory();


public App()

Check warning on line 19 in MagicChatboxV2/App.xaml.cs

View workflow job for this annotation

GitHub Actions / build-and-release

Non-nullable field 'serviceProvider' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
// Configure the logger
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File("logs/application.log", rollingInterval: RollingInterval.Day)
.CreateLogger();

// Set the shutdown mode to explicit
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

// Handle unhandled exceptions in the Dispatcher
DispatcherUnhandledException += App_DispatcherUnhandledException;

// Handle unhandled exceptions in the AppDomain
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}

// Handler for unhandled exceptions in the Dispatcher
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// Log the exception
Log.Error(e.Exception, "An unhandled Dispatcher exception occurred");

// Show a custom error dialog with the exception details
var errorDialog = new CustomErrorDialog(e.Exception.Message, e.Exception.StackTrace);

Check warning on line 45 in MagicChatboxV2/App.xaml.cs

View workflow job for this annotation

GitHub Actions / build-and-release

Possible null reference argument for parameter 'stackTrace' in 'CustomErrorDialog.CustomErrorDialog(string errorMessage, string stackTrace)'.
errorDialog.ShowDialog();

// Mark the exception as handled
e.Handled = true;
}

// Handler for unhandled exceptions in the AppDomain
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// Log the exception
Log.Error((Exception)e.ExceptionObject, "An unhandled Domain exception occurred");

// Show a custom error dialog with the exception details
var errorDialog = new CustomErrorDialog(((Exception)e.ExceptionObject).Message, ((Exception)e.ExceptionObject).StackTrace);

Check warning on line 59 in MagicChatboxV2/App.xaml.cs

View workflow job for this annotation

GitHub Actions / build-and-release

Possible null reference argument for parameter 'stackTrace' in 'CustomErrorDialog.CustomErrorDialog(string errorMessage, string stackTrace)'.
errorDialog.ShowDialog();
}

// Configure and build the service provider
private IServiceProvider ConfigureServices()
{
var services = new ServiceCollection();

// Register services and modules as before
services.AddSingleton<VRChatMonitorService>();
services.AddSingleton<SystemTrayService>();
services.AddSingleton<StartupHelper>();
services.AddModules(Assembly.GetExecutingAssembly());



// Register ModuleManagerService
services.AddSingleton<ModuleManagerService>();

services.AddSingleton<PrimaryInterface>();
services.AddSingleton<PrimaryInterfaceFactory>(serviceProvider => () => serviceProvider.GetRequiredService<PrimaryInterface>());


return services.BuildServiceProvider();
}


protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
serviceProvider = ConfigureServices();

// Get the StartupHelper service from the service provider
var startupHelper = serviceProvider.GetService<StartupHelper>();

// Start the application logic using the StartupHelper
startupHelper?.Start();
}

}
}
10 changes: 10 additions & 0 deletions MagicChatboxV2/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
23 changes: 23 additions & 0 deletions MagicChatboxV2/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace MagicChatboxV2.Extensions
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddModules(this IServiceCollection services, Assembly assembly)
{
// Find all types in the assembly that implement IModule and are class types
var moduleTypes = assembly.GetTypes()
.Where(t => typeof(UIVM.Models.IModule).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);

// Register each found module type with the services collection
foreach (var type in moduleTypes)
{
services.AddTransient(typeof(UIVM.Models.IModule), type);
}

return services;
}
}
}
117 changes: 117 additions & 0 deletions MagicChatboxV2/Helpers/StartupHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using MagicChatboxV2.Services;
using MagicChatboxV2.UIVM.Windows;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using static MagicChatboxV2.App;

namespace MagicChatboxV2.Helpers
{
public class StartupHelper
{
private readonly IServiceProvider serviceProvider;
private readonly PrimaryInterfaceFactory _mainWindowFactory;
private LoadingWindow loadingWindow;

public StartupHelper(IServiceProvider serviceProvider, PrimaryInterfaceFactory mainWindowFactory)

Check warning on line 21 in MagicChatboxV2/Helpers/StartupHelper.cs

View workflow job for this annotation

GitHub Actions / build-and-release

Non-nullable field 'loadingWindow' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
this.serviceProvider = serviceProvider;
this._mainWindowFactory = mainWindowFactory;
}

// Start method to initiate the startup process
public void Start()
{
try
{
ShowLoadingWindow();
InitializeServices();
}
catch (Exception ex)
{
Log.Error(ex, "Error during service initialization");
MessageBox.Show($"Failed to initialize services: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
CloseLoadingWindow();
Application.Current.Shutdown();
return;
}

CloseLoadingWindow();

try
{
var vrChatService = serviceProvider.GetService<VRChatMonitorService>();
vrChatService.OnVRChatStarted += OnVRChatStarted;

Check warning on line 49 in MagicChatboxV2/Helpers/StartupHelper.cs

View workflow job for this annotation

GitHub Actions / build-and-release

Dereference of a possibly null reference.
vrChatService.StartMonitoring();
}
catch (Exception ex)
{
Log.Error(ex, "Failed to start VRChat monitoring");
MessageBox.Show($"Failed to start VRChat monitoring: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

}
}

// Show the loading window
private void ShowLoadingWindow()
{
Application.Current.Dispatcher.Invoke(() =>
{
loadingWindow = new LoadingWindow();
loadingWindow.Show();
});
}

// Initialize the services
private void InitializeServices()
{
UpdateProgress(0, "Initializing System Tray...");
var trayService = serviceProvider.GetService<SystemTrayService>();
trayService.InitializeTrayIcon();

Check warning on line 75 in MagicChatboxV2/Helpers/StartupHelper.cs

View workflow job for this annotation

GitHub Actions / build-and-release

Dereference of a possibly null reference.
UpdateProgress(50, "System Tray Initialized.");

UpdateProgress(100, "Initialization Complete.");
}

// Update the progress of the loading window
private void UpdateProgress(double progress, string status)
{
Application.Current.Dispatcher.Invoke(() =>
{
loadingWindow.UpdateProgress(progress, status);
});
}

// Close the loading window
private void CloseLoadingWindow()
{
Application.Current.Dispatcher.Invoke(() =>
{
loadingWindow.Close();
});
}

// Event handler for when VRChat is started
private void OnVRChatStarted()
{
Application.Current.Dispatcher.Invoke(() =>
{
var mainWindow = _mainWindowFactory();
if (!mainWindow.IsVisible)
{
mainWindow.Show();
}
else
{
mainWindow.Activate();
}
});
}

}
}
33 changes: 33 additions & 0 deletions MagicChatboxV2/MagicChatboxV2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<None Remove="UIVM\Images\MagicOSC_icon.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<Resource Include="UIVM\Images\MagicOSC_icon.png" />
</ItemGroup>

<ItemGroup>
<Folder Include="UIVM\Pages\" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions MagicChatboxV2/PrimaryInterface.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Window
x:Class="MagicChatboxV2.PrimaryInterface"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MagicChatboxV2"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<StackPanel>
<TextBlock
x:Name="GetFormattedOutput"
Width="200"
Margin="10,10,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="TextBlock"
TextWrapping="Wrap" />
<Button Name="GET" Click="GET_Click" Content="GET" Margin="10,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
<Button Name="disposemodules" Click="disposemodules_Click" Content="Dispose Modules" Margin="10,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
<Button Name="startmodules" Click="startmodules_Click" Content="Dispose Modules" Margin="10,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
</StackPanel>
</Window>
58 changes: 58 additions & 0 deletions MagicChatboxV2/PrimaryInterface.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using MagicChatboxV2.Services;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MagicChatboxV2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class PrimaryInterface : Window
{
private readonly ModuleManagerService _moduleManagerService;

// Constructor injection of ModuleManagerService
public PrimaryInterface(ModuleManagerService moduleManagerService)
{
InitializeComponent();
_moduleManagerService = moduleManagerService;
}

private void GET_Click(object sender, RoutedEventArgs e)
{
// Retrieve formatted outputs from all active modules and display them
var formattedOutputs = _moduleManagerService.GetFormattedOutputs();
// Assuming you want to display these outputs in a TextBlock or similar control
// For demonstration, updating the Button content; adjust based on your UI design
GetFormattedOutput.Text = formattedOutputs;
}

protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true; // Cancel the closing
this.Hide(); // Hide the window instead of closing it
base.OnClosing(e);
}

private void disposemodules_Click(object sender, RoutedEventArgs e)
{
_moduleManagerService.DisposeModules();
}

private void startmodules_Click(object sender, RoutedEventArgs e)
{
_moduleManagerService.InitializeModules();
}
}

}
Loading

0 comments on commit 94f1e98

Please sign in to comment.