Skip to content

Commit

Permalink
Proxy items now work properly.
Browse files Browse the repository at this point in the history
Started working on triggers.
  • Loading branch information
Wincent01 committed Feb 15, 2020
1 parent 863112b commit 0a93e1e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
5 changes: 4 additions & 1 deletion InfectedRose.Triggers/Trigger.cs
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; }
}
}
3 changes: 2 additions & 1 deletion InfectedRose.Triggers/TriggerCollection.cs
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; }
}
}
76 changes: 76 additions & 0 deletions InfectedRose.Triggers/TriggerDictionary.cs
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
);
}
}
3 changes: 2 additions & 1 deletion InfectedRose.Triggers/TriggerEvent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Xml.Serialization;

namespace InfectedRose.Triggers
Expand All @@ -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<TriggerCommand> Commands { get; set; }
}
}

0 comments on commit 0a93e1e

Please sign in to comment.