Skip to content

Commit

Permalink
VXMusicSettingsSync needs to be run before Services are created to en…
Browse files Browse the repository at this point in the history
…sure they are created with the correct config. Deferr log messages to servicehandler once it is created.
  • Loading branch information
Soapwood committed Jul 20, 2024
1 parent cbcdaf0 commit c3a7122
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
7 changes: 5 additions & 2 deletions VXMusicDesktop/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public App() : base()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();

VXMusicSettingsSyncHandler.RunSettingsMigrationOnStartup();

var overlaySettings = new OverlaySettings()
{
Expand Down Expand Up @@ -136,6 +138,8 @@ public static void ConfigureServices()
*/
services.AddSingleton<App>();

services.AddSingleton<VXMusicSettingsSyncHandler>();

// Handle creating Recognition Clients based off of BYOAPI Configuration
services.AddSingleton<ShazamClient>(serviceProvider => VXMusicSession.RecognitionSettings!.ShazamSettings.IsByoApiEnabled
? new ShazamClient(serviceProvider, VXMusicSession.RecognitionSettings.ShazamSettings.ByoApiKey!)
Expand All @@ -151,9 +155,8 @@ public static void ConfigureServices()
services.AddSingleton<VrChatOscNotificationClient>();
services.AddSingleton<SteamVROverlayAppsInterface>();

services.AddSingleton<VXMusicSettingsSyncHandler>();
services.AddSingleton<VXMusicUpdateHandler>();

// services.AddSingleton<INotificationClient>(provider =>
// {
// var selectedClient = new XsOverlayNotificationClient();
Expand Down
39 changes: 26 additions & 13 deletions VXMusicDesktop/Update/VXMusicSettingsSyncHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,63 @@ namespace VXMusicDesktop.Update;

public class VXMusicSettingsSyncHandler
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<VXMusicSettingsSyncHandler> _logger;
private static List<string> _deferredLogMessages = new List<string>();

private static readonly string UserSettingsFileName = "user.config";
private static readonly string UrlFolderPattern = "VXMusicDesktop_Url_*";

public string CurrentUserSettingsFile;
public bool CurrentUserSettingsFileExists => File.Exists(CurrentUserSettingsFile);
public static string CurrentUserSettingsFile;
public static bool CurrentUserSettingsFileExists => File.Exists(CurrentUserSettingsFile);

public VXMusicSettingsSyncHandler(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_logger = serviceProvider.GetService(typeof(ILogger<VXMusicSettingsSyncHandler>))
as ILogger<VXMusicSettingsSyncHandler> ?? throw new ApplicationException("A logger must be created in service provider.");

_logger.LogTrace("Creating VXMusicSettingsSyncHandler.");


FlushDeferredLogMessages();
}

public static void RunSettingsMigrationOnStartup()
{
CurrentUserSettingsFile = GetCurrentConfigPath();

if (CurrentUserSettingsFileExists)
{
_logger.LogInformation($"Current User Settings File: {CurrentUserSettingsFile}");
_deferredLogMessages.Add($"Current User Settings File: {CurrentUserSettingsFile}");
}
else
{
_logger.LogTrace($"Current User Settings File: {CurrentUserSettingsFile} doesn't exist.");
_deferredLogMessages.Add($"Current User Settings File: {CurrentUserSettingsFile} doesn't exist.");
MigrateSettings();
}
}

public void MigrateSettings()
private static void MigrateSettings()
{
string latestConfigPath = GetLatestConfigPath();
if (latestConfigPath != null)
{
_logger.LogInformation($"Found previous user settings: {latestConfigPath}. Migrating to: {CurrentUserSettingsFile}");
_deferredLogMessages.Add($"Found previous user settings: {latestConfigPath}. Migrating to: {CurrentUserSettingsFile}");
EnsureAppDataUserSettingsFolderCreated();
File.Copy(latestConfigPath, CurrentUserSettingsFile, true);
}
else
{
_logger.LogInformation($"No Previous User Settings Found. Creating User Settings file in {CurrentUserSettingsFile}");
_deferredLogMessages.Add($"No Previous User Settings Found. Creating User Settings file in {CurrentUserSettingsFile}");
EnsureAppDataUserSettingsFolderCreated();
VXUserSettings.Settings.SetHasLaunched(true);
}
}

private string GetCurrentConfigPath()
private static string GetCurrentConfigPath()
{
return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
}

private string GetLatestConfigPath()
private static string GetLatestConfigPath()
{
string localAppDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"VirtualXtensions");
Expand Down Expand Up @@ -104,7 +108,7 @@ private string GetLatestConfigPath()
return null;
}

private void EnsureAppDataUserSettingsFolderCreated()
private static void EnsureAppDataUserSettingsFolderCreated()
{
var currentSettingsDirectoryPath = Path.GetDirectoryName(CurrentUserSettingsFile);

Expand All @@ -117,4 +121,13 @@ private static Version ParseVersion(string versionString)
Version version;
return Version.TryParse(versionString, out version) ? version : null;
}

private void FlushDeferredLogMessages()
{
foreach (var message in _deferredLogMessages)
{
_logger.LogInformation(message);
}
_deferredLogMessages.Clear();
}
}
2 changes: 1 addition & 1 deletion VXMusicDesktop/VXMusicDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageIcon>VXLogo.png</PackageIcon>
<ApplicationIcon>Images\VXLogoIcon.ico</ApplicationIcon>
<RunPostBuildEvent>Always</RunPostBuildEvent>
<Version>0.6.3.11</Version>
<Version>0.6.3.12</Version>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<Title>VXMusicDesktop</Title>
<Authors>VirtualXtensions</Authors>
Expand Down
4 changes: 2 additions & 2 deletions VXMusicDesktop/VXMusicSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class VXMusicSession
public VRChatLogParser VRChatLogParser;
public SteamVROverlayAppsInterface SteamVrOverlayApps;
public VXMusicUpdateHandler VxMusicUpdate;
public VXMusicSettingsSyncHandler VxMusicSettingsSync;
public VXMusicSettingsSyncHandler VXMusicSettingsSync;

public static event EventHandler LastFmLogin;
//public static event EventHandler SpotifyLogin;
Expand Down Expand Up @@ -82,8 +82,8 @@ public void Initialise()

SteamVrOverlayApps = App.ServiceProvider.GetRequiredService<SteamVROverlayAppsInterface>();

VxMusicSettingsSync = App.ServiceProvider.GetRequiredService<VXMusicSettingsSyncHandler>();
VxMusicUpdate = App.ServiceProvider.GetRequiredService<VXMusicUpdateHandler>();
VXMusicSettingsSync = App.ServiceProvider.GetRequiredService<VXMusicSettingsSyncHandler>();

ColourSchemeManager.SetTheme(VXUserSettings.Desktop.GetCurrentDesktopTheme());

Expand Down

0 comments on commit c3a7122

Please sign in to comment.