move EliteBGS GUI app to .NET 7

This commit is contained in:
2022-11-24 19:38:19 +01:00
parent 42f305c7ac
commit 411b3606f9
69 changed files with 13257 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
using System.ComponentModel;
namespace EliteBGS.Util {
public class AppConfig : INotifyPropertyChanged {
private static readonly string default_journal_location = "%UserProfile%\\Saved Games\\Frontier Developments\\Elite Dangerous";
private string journal_location = default_journal_location;
private string lastdiscordlog;
public string DefaultJournalLocation => default_journal_location;
public string LastUsedDiscordTemplate {
get => lastdiscordlog;
set {
lastdiscordlog = value;
FirePropertyChanged("LastUsedDiscordTemplate");
}
}
public string JournalLocation {
get {
if (journal_location == null) {
return DefaultJournalLocation;
}
return journal_location;
}
set {
journal_location = value;
FirePropertyChanged("JournalLocation");
}
}
private void FirePropertyChanged(string property) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
public event PropertyChangedEventHandler PropertyChanged;
}
}

66
EliteBGS/Util/Config.cs Normal file
View File

@@ -0,0 +1,66 @@
using System;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace EliteBGS.Util {
public class Config {
private string config_folder = null;
private string config_file = null;
private AppConfig global_config = new AppConfig();
public Config() {
DetermineConfigFolder();
global_config.PropertyChanged += Global_config_PropertyChanged;
}
private void Global_config_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
try {
SaveGlobal();
} catch (Exception) {
/* ignored */
}
}
public string ConfigPath => config_folder;
public AppConfig Global => global_config;
private void DetermineConfigFolder() {
string folder = Environment.ExpandEnvironmentVariables("%appdata%\\EliteBGS");
if (!Directory.Exists(folder)) {
Directory.CreateDirectory(folder);
}
config_folder = folder;
config_file = Path.Combine(config_folder, "config.json");
}
public void SaveGlobal() {
var serializer = JsonSerializer.CreateDefault();
using (FileStream filestream = File.OpenWrite(config_file)) {
filestream.SetLength(0);
filestream.Flush();
using (StreamWriter file = new StreamWriter(filestream, Encoding.UTF8)) {
var stream = new JsonTextWriter(file);
serializer.Serialize(stream, global_config);
}
}
}
public void LoadGlobal() {
var serializer = JsonSerializer.CreateDefault();
using (var file = new StreamReader(File.OpenRead(config_file), Encoding.UTF8)) {
var stream = new JsonTextReader(file);
var app = serializer.Deserialize<AppConfig>(stream);
if (app != null) {
this.global_config = app;
global_config.PropertyChanged += Global_config_PropertyChanged;
}
}
}
}
}