add autocompletion for system names

This commit is contained in:
2021-07-09 14:40:27 +02:00
parent fca1e607ec
commit 14c14f7626
8 changed files with 175 additions and 71 deletions

59
EDDB/API.cs Normal file
View File

@@ -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) {
}
}
}

59
EDDB/PopulatedSystems.cs Normal file
View File

@@ -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<JObject>().Select(x => x.Value<string>("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;
}
}
}