2021-07-09 14:40:27 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2021-07-29 21:07:53 +02:00
|
|
|
|
using System.Collections.Generic;
|
2021-07-09 14:40:27 +02:00
|
|
|
|
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;
|
2021-07-29 21:07:53 +02:00
|
|
|
|
private Dictionary<string, int> to_id;
|
2021-07-09 14:40:27 +02:00
|
|
|
|
|
|
|
|
|
public static PopulatedSystems FromFile(string file) {
|
|
|
|
|
PopulatedSystems pop = new PopulatedSystems();
|
|
|
|
|
string content = File.ReadAllText(file);
|
|
|
|
|
|
|
|
|
|
pop.json_file = file;
|
|
|
|
|
pop.root = JArray.Parse(content);
|
2021-07-29 21:07:53 +02:00
|
|
|
|
pop.Initialise();
|
2021-07-09 14:40:27 +02:00
|
|
|
|
|
|
|
|
|
return pop;
|
|
|
|
|
}
|
2021-07-29 21:07:53 +02:00
|
|
|
|
|
|
|
|
|
private void Initialise() {
|
|
|
|
|
MakeSystemNames();
|
|
|
|
|
|
|
|
|
|
to_id = root.ToDictionary(x => x.Value<string>("name"), x => x.Value<int>("id"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int ToId(string name) {
|
|
|
|
|
return to_id.First(x => string.Compare(x.Key, name, true) == 0).Value;
|
|
|
|
|
}
|
2021-07-09 14:40:27 +02:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|