Archived
1
0

Add project files.

This commit is contained in:
2021-07-09 11:03:30 +02:00
parent ac7029146f
commit 5d3af78a69
39 changed files with 2092 additions and 0 deletions

43
Util/AppConfig.cs Normal file
View File

@@ -0,0 +1,43 @@
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NonaBGS.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 bool useeddb = false;
public string DefaultJournalLocation => default_journal_location;
public bool UseEDDB {
get => useeddb;
set {
useeddb = value;
FirePropertyChanged("UseEDDB");
}
}
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;
}
}

85
Util/Config.cs Normal file
View File

@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using NonaBGS.BGS;
namespace NonaBGS.Util {
public class Config {
private string config_folder = null;
private string objectives_file = 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%\\NonaBGS");
if (!Directory.Exists(folder)) {
Directory.CreateDirectory(folder);
}
config_folder = folder;
objectives_file = Path.Combine(config_folder, "objectives.json");
config_file = Path.Combine(config_folder, "config.json");
}
public void SaveGlobal() {
var serializer = JsonSerializer.CreateDefault();
using (var file = new StreamWriter(File.OpenWrite(config_file), 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;
}
}
}
public void SaveObjectives(Report report) {
var serializer = JsonSerializer.CreateDefault();
using (var file = new StreamWriter(File.OpenWrite(objectives_file), Encoding.UTF8)) {
var stream = new JsonTextWriter(file);
serializer.Serialize(stream, report.Objectives);
}
}
public void LoadObjectives(Report report) {
var serializer = JsonSerializer.CreateDefault();
using (var file = new StreamReader(File.OpenRead(objectives_file), Encoding.UTF8)) {
var stream = new JsonTextReader(file);
var objectives = serializer.Deserialize<List<Objective>>(stream);
report.Objectives = objectives;
}
}
}
}

48
Util/EDDB.cs Normal file
View File

@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Net;
namespace NonaBGS.Util {
public class EDDB {
private static readonly string EDDB_SYSTEMS_ARCHIVE = "https://eddb.io/archive/v6/systems_populated.json";
private string cache_folder = null;
private WebClient client = new WebClient();
private string systems_file = null;
public string SystemsFile => systems_file;
public string Cache {
get => cache_folder;
set => cache_folder = value;
}
public EDDB(string cache_folder) {
this.cache_folder = cache_folder;
Initialise();
}
public void Initialise() {
client.DownloadDataCompleted += Client_DownloadDataCompleted;
}
private void DownloadFile(string url) {
Uri uri = new Uri(url);
string name = Path.GetFileName(uri.AbsolutePath);
systems_file = Path.Combine(this.cache_folder, name);
client.DownloadFileAsync(new Uri(EDDB_SYSTEMS_ARCHIVE), systems_file);
}
public void Download() {
DownloadFile(EDDB_SYSTEMS_ARCHIVE);
}
private void Client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) {
}
}
}