diff --git a/InfectedRose.Triggers/Trigger.cs b/InfectedRose.Triggers/Trigger.cs index f150d0a..2ac1192 100644 --- a/InfectedRose.Triggers/Trigger.cs +++ b/InfectedRose.Triggers/Trigger.cs @@ -1,13 +1,16 @@ +using System.Collections.Generic; using System.Xml.Serialization; namespace InfectedRose.Triggers { public class Trigger { + public int FileId { get; set; } + [XmlAttribute("id")] public int Id { get; set; } [XmlAttribute("enabled")] public int Enabled { get; set; } - [XmlElement("event")] public TriggerEvent[] Events { get; set; } + [XmlElement("event")] public List Events { get; set; } } } \ No newline at end of file diff --git a/InfectedRose.Triggers/TriggerCollection.cs b/InfectedRose.Triggers/TriggerCollection.cs index 22803a5..87f5c2c 100644 --- a/InfectedRose.Triggers/TriggerCollection.cs +++ b/InfectedRose.Triggers/TriggerCollection.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Xml.Serialization; namespace InfectedRose.Triggers @@ -5,6 +6,6 @@ namespace InfectedRose.Triggers [XmlRoot("triggers")] public class TriggerCollection { - [XmlElement("trigger")] public Trigger[] Triggers { get; set; } + [XmlElement("trigger")] public List Triggers { get; set; } } } \ No newline at end of file diff --git a/InfectedRose.Triggers/TriggerDictionary.cs b/InfectedRose.Triggers/TriggerDictionary.cs new file mode 100644 index 0000000..5f8df35 --- /dev/null +++ b/InfectedRose.Triggers/TriggerDictionary.cs @@ -0,0 +1,76 @@ +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using System.Xml.Serialization; + +namespace InfectedRose.Triggers +{ + public class TriggerDictionary + { + public Trigger[] Triggers { get; } + + private TriggerDictionary(Trigger[] triggers) + { + Triggers = triggers; + } + + public static async Task FromDirectoryAsync(string path) + { + var entries = Directory.GetFiles(path, "*.lutriggers", SearchOption.TopDirectoryOnly); + + var collections = new TriggerCollection[entries.Length]; + + var tasks = new Task[entries.Length]; + + var serializer = new XmlSerializer(typeof(TriggerCollection)); + + for (var i = 0; i < entries.Length; i++) + { + var index = i; + + tasks[index] = Task.Run(async () => + { + var entry = entries[index]; + + var fileSegments = Path.GetFileNameWithoutExtension(entry).Split('_'); + + var fileId = 0; + + foreach (var filePath in fileSegments) + { + if (int.TryParse(filePath, out fileId)) break; + } + + if (fileId == default) return; + + await using var stream = File.OpenRead(entry); + + var triggerCollection = (TriggerCollection) serializer.Deserialize(stream); + + collections[index] = triggerCollection; + + foreach (var trigger in triggerCollection.Triggers) + { + trigger.FileId = fileId; + + Console.WriteLine($"TRIGGER: {trigger.Id}:{trigger.FileId}"); + } + }); + } + + await Task.WhenAll(tasks); + + var triggers = collections + .Where(c => c != default) + .SelectMany(c => c.Triggers) + .ToArray(); + + return new TriggerDictionary(triggers); + } + + public Trigger this[int fileId, int triggerId] => Triggers.FirstOrDefault( + t => t.FileId == fileId && t.Id == triggerId + ); + } +} \ No newline at end of file diff --git a/InfectedRose.Triggers/TriggerEvent.cs b/InfectedRose.Triggers/TriggerEvent.cs index 83f56fb..9668a50 100644 --- a/InfectedRose.Triggers/TriggerEvent.cs +++ b/InfectedRose.Triggers/TriggerEvent.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Xml.Serialization; namespace InfectedRose.Triggers @@ -6,6 +7,6 @@ public class TriggerEvent { [XmlAttribute("id")] public string Id { get; set; } - [XmlElement("command")] public TriggerCommand[] Commands { get; set; } + [XmlElement("command")] public List Commands { get; set; } } } \ No newline at end of file