diff --git a/EDDB/API.cs b/EDDB/API.cs new file mode 100644 index 0000000..76b75ea --- /dev/null +++ b/EDDB/API.cs @@ -0,0 +1,59 @@ +using System; +using System.IO; +using System.Net; + +namespace NonaBGS.EDDB { + public class API { + private static readonly string EDDB_SYSTEMS_ARCHIVE = "https://eddb.io/archive/v6/systems_populated.json"; + private string cache_folder = null; + private readonly 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 API(string cache_folder) { + Initialise(cache_folder); + } + + private void Initialise(string cache_folder) { + this.cache_folder = cache_folder; + systems_file = Path.Combine(this.cache_folder, "systems_populated.json"); + client.DownloadDataCompleted += Client_DownloadDataCompleted; + } + + private void DownloadFile(string url, string file) { + client.DownloadFileAsync(new Uri(url), file); + } + + public void Download(bool force) { + if (!HaveSystemsFile || force) { + DownloadFile(EDDB_SYSTEMS_ARCHIVE, systems_file); + } + } + + public void Download() { + Download(false); + } + + public bool HaveSystemsFile { + get { return systems_file != null && File.Exists(systems_file); } + } + + public PopulatedSystems MakePopulatedSystems() { + if (!HaveSystemsFile) { + throw new InvalidOperationException("no local systems file downloaded"); + } + + return PopulatedSystems.FromFile(SystemsFile); + } + + private void Client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) { + } + } +} diff --git a/EDDB/PopulatedSystems.cs b/EDDB/PopulatedSystems.cs new file mode 100644 index 0000000..7a3fa79 --- /dev/null +++ b/EDDB/PopulatedSystems.cs @@ -0,0 +1,59 @@ +using System.IO; +using System.Linq; +using System; +using System.Globalization; +using Newtonsoft.Json.Linq; + +namespace NonaBGS.EDDB { + public class PopulatedSystems { + private string json_file = null; + private JArray root = null; + private string[] system_names = null; + + public static PopulatedSystems FromFile(string file) { + PopulatedSystems pop = new PopulatedSystems(); + string content = File.ReadAllText(file); + + pop.json_file = file; + pop.root = JArray.Parse(content); + + return pop; + } + + private void MakeSystemNames() { + if (root == null) { + throw new InvalidDataException("no JSON loaded"); + } + + if (system_names != null && system_names.Length > 0) { + return; + } + + var names = root.Children().Select(x => x.Value("name")); + system_names = names.ToArray(); + } + + public string[] SystemNames { + get { + MakeSystemNames(); + return system_names; + } + } + + public string[] SystemNamesByFilter(string filter) { + MakeSystemNames(); + var culture = CultureInfo.InvariantCulture; + return system_names.Where(x => culture.CompareInfo.IndexOf(x, filter, CompareOptions.IgnoreCase) > -1) + .ToArray() + ; + } + + public string JSONFile { + get => json_file; + } + + public JArray Root { + get => root; + } + } +} diff --git a/MainWindow.xaml b/MainWindow.xaml index 4b15d7b..4cd1423 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -3,6 +3,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:abc="http://wpfcontrols.com/" xmlns:local="clr-namespace:NonaBGS" xmlns:BGS="clr-namespace:NonaBGS.BGS" xmlns:Util="clr-namespace:NonaBGS.Util" d:DataContext="{d:DesignInstance Type=Util:AppConfig}" x:Name="window" x:Class="NonaBGS.MainWindow" mc:Ignorable="d" @@ -29,11 +30,11 @@