mirror of
https://github.com/muskit/MercuryConverter.git
synced 2026-06-02 20:24:26 -07:00
add settings wrt data path
This commit is contained in:
@@ -19,8 +19,10 @@
|
||||
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
|
||||
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||
<PackageReference Include="Deadpikle.AvaloniaProgressRing" Version="0.10.10" />
|
||||
<PackageReference Include="DialogHost.Avalonia" Version="0.9.3" />
|
||||
<PackageReference Include="ini-parser" Version="2.5.2" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
+5
-5
@@ -1,11 +1,12 @@
|
||||
namespace MercuryConverter;
|
||||
|
||||
using Avalonia;
|
||||
using System;
|
||||
|
||||
using MercuryConverter.UI;
|
||||
using Avalonia;
|
||||
using Avalonia.Logging;
|
||||
|
||||
using MercuryConverter.UI;
|
||||
|
||||
class Program
|
||||
{
|
||||
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
||||
@@ -14,16 +15,15 @@ class Program
|
||||
[STAThread]
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
// BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
|
||||
new Settings();
|
||||
|
||||
try
|
||||
{
|
||||
// prepare and run your App here
|
||||
BuildAvaloniaApp()
|
||||
.StartWithClassicDesktopLifetime(args);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// here we can work with the exception, for example add it to our log file
|
||||
Console.WriteLine($"App exception!!\b{e}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using IniParser;
|
||||
using IniParser.Model;
|
||||
|
||||
namespace MercuryConverter;
|
||||
|
||||
public enum Theme
|
||||
{
|
||||
Light, Dark, System
|
||||
}
|
||||
|
||||
public partial class Settings : ObservableObject
|
||||
{
|
||||
public static Settings? I;
|
||||
|
||||
private string iniPath;
|
||||
|
||||
public string AppDataPath =>
|
||||
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "muskit", "MercuryConverter");
|
||||
|
||||
[ObservableProperty]
|
||||
private string dataPath = "";
|
||||
[ObservableProperty]
|
||||
private Theme theme = Theme.System;
|
||||
|
||||
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
|
||||
{
|
||||
Console.Write($"Setting {e.PropertyName} changed to ");
|
||||
switch (e.PropertyName)
|
||||
{
|
||||
case nameof(DataPath):
|
||||
Console.WriteLine(DataPath);
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("unknown variable");
|
||||
break;
|
||||
}
|
||||
SaveToIni();
|
||||
|
||||
base.OnPropertyChanged(e);
|
||||
}
|
||||
|
||||
public Settings()
|
||||
{
|
||||
I = this;
|
||||
|
||||
Console.WriteLine($"Settings path: {AppDataPath}");
|
||||
iniPath = Path.Combine(AppDataPath, "settings.ini");
|
||||
|
||||
// Attempt to read settings; try to create new if unable to
|
||||
try
|
||||
{
|
||||
LoadFromIni();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Couldn't read {iniPath}!\n{e.Message}");
|
||||
Console.WriteLine("Attempting to create new settings file.");
|
||||
SaveToIni();
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveToIni()
|
||||
{
|
||||
var data = new IniData();
|
||||
data["paths"]["data"] = DataPath;
|
||||
data["ui"]["theme"] = Theme.ToString();
|
||||
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(iniPath)!);
|
||||
FileIniDataParser parser = new();
|
||||
parser.WriteFile(iniPath, data);
|
||||
Console.WriteLine($"Settings saved to {iniPath}.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Could not save settings to {iniPath}!\n{e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadFromIni()
|
||||
{
|
||||
FileIniDataParser parser = new();
|
||||
var iniData = parser.ReadFile(iniPath);
|
||||
|
||||
DataPath = iniData["paths"]["data"];
|
||||
if (Enum.TryParse(iniData["ui"]["theme"], out Theme loadedTheme))
|
||||
Theme = loadedTheme;
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@
|
||||
Margin="0,15,0,0"/>
|
||||
<StackPanel Margin="0 12 0 0" Name="ButtonGroup" Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Right" IsVisible="false">
|
||||
<Button Name="BtnClose" Margin="6 0 0 0" Content="Close" Click="CloseHandler"/>
|
||||
<Button Name="BtnSelectFolder" Margin="6 0 0 0" Content="Open Data Folder" Click="OpenDataHandler"/>
|
||||
<Button Name="BtnSelectFolder" Margin="6 0 0 0" Content="Open Data Folder" Click="OpenFolderHandler"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@@ -12,12 +13,12 @@ namespace MercuryConverter.UI.Dialogs;
|
||||
|
||||
public partial class DataScanning : UserControl
|
||||
{
|
||||
public static DataScanning? Instance { get; private set; }
|
||||
private bool requiresUser;
|
||||
|
||||
|
||||
public DataScanning()
|
||||
public DataScanning(bool requiresUser = true)
|
||||
{
|
||||
Instance = this;
|
||||
this.requiresUser = requiresUser;
|
||||
Console.WriteLine($"DataScan should stay open: {requiresUser}");
|
||||
InitializeComponent();
|
||||
|
||||
if (!Design.IsDesignMode)
|
||||
@@ -28,34 +29,42 @@ public partial class DataScanning : UserControl
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
var path = ""; // TODO: set to current/saved data path (move to config?)
|
||||
var selectedPath = await BeginDirSelection();
|
||||
|
||||
if (Settings.I!.DataPath == "" || requiresUser) // no data path saved
|
||||
{
|
||||
var selectedPath = await BeginDirSelection(Settings.I!.DataPath);
|
||||
if (selectedPath == "") // cancelled opening folder
|
||||
{
|
||||
// TODO:
|
||||
// return and go to completed mode if scan already completed
|
||||
// continue if no scan has been completed
|
||||
// break if we already have a path but somehow not scanned
|
||||
Dispatcher.UIThread.Post(() => ScanPath.Text = "");
|
||||
UISetError("No data folder provided.");
|
||||
return;
|
||||
}
|
||||
if (!Directory.Exists(selectedPath))
|
||||
Settings.I.DataPath = selectedPath;
|
||||
}
|
||||
|
||||
Dispatcher.UIThread.Post(() => ScanPath.Text = Settings.I.DataPath);
|
||||
if (!Directory.Exists(Settings.I.DataPath))
|
||||
{
|
||||
UISetError("Folder does not exist.");
|
||||
return;
|
||||
}
|
||||
if (!(File.Exists(Path.Combine(selectedPath, "MusicParameterTable.uasset")) && File.Exists(Path.Combine(selectedPath, "MusicParameterTable.uexp"))))
|
||||
if (!(File.Exists(Path.Combine(Settings.I.DataPath, "MusicParameterTable.uasset"))
|
||||
&& File.Exists(Path.Combine(Settings.I.DataPath, "MusicParameterTable.uexp"))))
|
||||
{
|
||||
UISetError("Missing MusicParameterTable asset files.\nPlease ensure you've set up your data folder properly!");
|
||||
return;
|
||||
}
|
||||
|
||||
path = selectedPath;
|
||||
UIScanningMode(Settings.I.DataPath);
|
||||
Database.SetupNew(Settings.I.DataPath);
|
||||
|
||||
UIScanningMode(path);
|
||||
Database.SetupNew(path);
|
||||
if (requiresUser) // TODO: or if there are warnings
|
||||
UIScanCompletedMode();
|
||||
else
|
||||
Dispatcher.UIThread.Post(() => MainWindow.Instance!.Dialog.IsOpen = false);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -65,7 +74,7 @@ public partial class DataScanning : UserControl
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
ScanStatus.Text = "select your data folder...";
|
||||
ScanPath.IsVisible = true;
|
||||
ScanPath.IsVisible = false;
|
||||
ScanInfo.IsVisible = false;
|
||||
ButtonGroup.IsVisible = false;
|
||||
ProgressAnimation.IsVisible = true;
|
||||
@@ -105,6 +114,7 @@ public partial class DataScanning : UserControl
|
||||
/// <param name="error"></param>
|
||||
private void UISetError(string? error = null)
|
||||
{
|
||||
requiresUser = true;
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
if (error == null)
|
||||
@@ -115,6 +125,8 @@ public partial class DataScanning : UserControl
|
||||
|
||||
ScanError.IsVisible = true;
|
||||
ErrorText.Text = error;
|
||||
ScanPath.IsVisible = ScanPath.Text == "" ? false : true;
|
||||
ScanInfo.IsVisible = false;
|
||||
ProgressAnimation.IsVisible = false;
|
||||
ButtonGroup.IsVisible = true;
|
||||
ScanStatus.Text = "an error has occurred";
|
||||
@@ -159,8 +171,9 @@ public partial class DataScanning : UserControl
|
||||
});
|
||||
}
|
||||
|
||||
private void OpenDataHandler(object sender, RoutedEventArgs args)
|
||||
private void OpenFolderHandler(object sender, RoutedEventArgs args)
|
||||
{
|
||||
requiresUser = true;
|
||||
RunFlow();
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,6 @@ public partial class Welcome : Window
|
||||
|
||||
private void ClickHandler(object sender, RoutedEventArgs args)
|
||||
{
|
||||
MainWindow.Instance!.Dialog.DialogContent = new DataScanning().Content;
|
||||
MainWindow.Instance!.Dialog.DialogContent = new DataScanning(true).Content;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@
|
||||
xmlns:local="clr-namespace:MercuryConverter.UI"
|
||||
x:Class="MercuryConverter.UI.MainWindow"
|
||||
|
||||
xmlns:views="clr-namespace:MercuryConverter.UI.Views"
|
||||
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
Title="MercuryConverter"
|
||||
Width="1024" Height="800"
|
||||
@@ -40,7 +42,7 @@
|
||||
|
||||
<TabControl>
|
||||
<TabItem Name="TabSelection" Header="selection">
|
||||
<UserControl Name="SelectionControl"/>
|
||||
<views:Selection />
|
||||
</TabItem>
|
||||
|
||||
<TabItem Header="export">
|
||||
|
||||
@@ -27,9 +27,6 @@ public partial class MainWindow : Window
|
||||
RequestedThemeVariant = ThemeVariant.Dark;
|
||||
}
|
||||
|
||||
// Setup tab views
|
||||
SelectionControl.Content = new Selection();
|
||||
|
||||
// Show dialog on startup
|
||||
Activated += OnActivated;
|
||||
|
||||
@@ -42,8 +39,12 @@ public partial class MainWindow : Window
|
||||
if (initialShown) return;
|
||||
initialShown = true;
|
||||
|
||||
Dialog.DialogContent = new Welcome().Content;
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
Dialog.IsOpen = true;
|
||||
Dialog.DialogContent = Settings.I!.DataPath == "" ?
|
||||
new Welcome().Content : new DataScanning(false).Content;
|
||||
});
|
||||
}
|
||||
|
||||
private void OnDbSelChanged(object? sender, NotifyCollectionChangedEventArgs e)
|
||||
@@ -57,7 +58,7 @@ public partial class MainWindow : Window
|
||||
public void OpenDataHandler()
|
||||
{
|
||||
Dialog.IsOpen = true;
|
||||
Dialog.DialogContent = new DataScanning().Content;
|
||||
Dialog.DialogContent = new DataScanning(true).Content;
|
||||
}
|
||||
|
||||
public void OpenDataHOWTO()
|
||||
|
||||
Reference in New Issue
Block a user