-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started working on triggers.
- Loading branch information
Showing
4 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TriggerEvent> Events { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
using System.Collections.Generic; | ||
using System.Xml.Serialization; | ||
|
||
namespace InfectedRose.Triggers | ||
{ | ||
[XmlRoot("triggers")] | ||
public class TriggerCollection | ||
{ | ||
[XmlElement("trigger")] public Trigger[] Triggers { get; set; } | ||
[XmlElement("trigger")] public List<Trigger> Triggers { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TriggerDictionary> 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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters