feat: allow lists of replacement text
This commit is contained in:
parent
feb08ab877
commit
c63514d9ad
@ -5,7 +5,7 @@ using YamlDotNet.Serialization;
|
||||
namespace TimePasses.Model;
|
||||
|
||||
public interface IWhen {
|
||||
string Text { get; }
|
||||
ReplacementText Text { get; }
|
||||
bool Slowly { get; }
|
||||
|
||||
bool IsValid(Plugin plugin);
|
||||
|
@ -1,9 +1,11 @@
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace TimePasses.Model;
|
||||
|
||||
[Serializable]
|
||||
internal class Replacement {
|
||||
public uint Id { get; init; }
|
||||
public string? Text { get; init; }
|
||||
public ReplacementText? Text { get; init; }
|
||||
public bool Slowly { get; init; }
|
||||
public IWhen[] When { get; init; } = [];
|
||||
}
|
||||
|
66
Model/ReplacementText.cs
Normal file
66
Model/ReplacementText.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using System.Collections;
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace TimePasses.Model;
|
||||
|
||||
[Serializable]
|
||||
public class ReplacementText : IReadOnlyList<string> {
|
||||
private IReadOnlyList<string> List { get; }
|
||||
|
||||
internal ReplacementText(IReadOnlyList<string> list) {
|
||||
this.List = list;
|
||||
}
|
||||
|
||||
public string this[int index] => this.List[index];
|
||||
|
||||
public int Count => this.List.Count;
|
||||
|
||||
public IEnumerator<string> GetEnumerator() {
|
||||
return this.List.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() {
|
||||
return this.List.GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
internal class ReplacementTextConverter : IYamlTypeConverter {
|
||||
internal static readonly ReplacementTextConverter Instance = new();
|
||||
|
||||
public bool Accepts(Type type) {
|
||||
return type == typeof(ReplacementText);
|
||||
}
|
||||
|
||||
public object? ReadYaml(IParser parser, Type type) {
|
||||
var list = new List<string>();
|
||||
if (parser.Current is Scalar) {
|
||||
AddItem();
|
||||
} else if (parser.Current is SequenceStart) {
|
||||
parser.Consume<SequenceStart>();
|
||||
while (parser.Current is not SequenceEnd) {
|
||||
AddItem();
|
||||
}
|
||||
|
||||
parser.Consume<SequenceEnd>();
|
||||
} else {
|
||||
throw new YamlException("expected string or list of strings");
|
||||
}
|
||||
|
||||
return new ReplacementText(list);
|
||||
|
||||
void AddItem() {
|
||||
var scalar = parser.Consume<Scalar>();
|
||||
if (scalar.IsKey) {
|
||||
throw new YamlException("invalid key found: expected string or list of strings");
|
||||
}
|
||||
|
||||
list.Add(scalar.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ namespace TimePasses.Model;
|
||||
public class WhenLevel : IWhen {
|
||||
public uint Level { get; init; }
|
||||
public int CompareResult { get; init; }
|
||||
public string Text { get; init; }
|
||||
public ReplacementText Text { get; init; }
|
||||
public bool Slowly { get; init; }
|
||||
|
||||
public bool IsValid(Plugin plugin) {
|
||||
@ -34,7 +34,7 @@ public class WhenLevelConverter : IYamlTypeConverter {
|
||||
public object ReadYaml(IParser parser, Type type) {
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
string? text = null;
|
||||
ReplacementText? text = null;
|
||||
int? comparerResult = null;
|
||||
uint? level = null;
|
||||
var slowly = false;
|
||||
@ -44,6 +44,11 @@ public class WhenLevelConverter : IYamlTypeConverter {
|
||||
throw new YamlException("expected key");
|
||||
}
|
||||
|
||||
if (key.Value == "text") {
|
||||
text = (ReplacementText?) ReplacementTextConverter.Instance.ReadYaml(parser, type);
|
||||
continue;
|
||||
}
|
||||
|
||||
var value = parser.Consume<Scalar>();
|
||||
|
||||
switch (key.Value) {
|
||||
@ -59,10 +64,6 @@ public class WhenLevelConverter : IYamlTypeConverter {
|
||||
ParseOperation(-1);
|
||||
break;
|
||||
}
|
||||
case "text": {
|
||||
text = value.Value;
|
||||
break;
|
||||
}
|
||||
case "slowly": {
|
||||
if (!bool.TryParse(value.Value, out slowly)) {
|
||||
throw new YamlException("invalid whenlevel: slowly was not a boolean");
|
||||
|
@ -6,7 +6,7 @@ namespace TimePasses.Model;
|
||||
public class WhenQuest : IWhen {
|
||||
public uint Id { get; init; }
|
||||
public QuestStatus Status { get; init; }
|
||||
public string Text { get; init; }
|
||||
public ReplacementText Text { get; init; }
|
||||
public bool Slowly { get; init; }
|
||||
|
||||
public unsafe bool IsValid(Plugin plugin) {
|
||||
|
@ -33,6 +33,7 @@ public sealed class Plugin : IDalamudPlugin {
|
||||
private static IDeserializer Deserializer { get; } = new DeserializerBuilder()
|
||||
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
||||
.WithTypeConverter(new WhenLevelConverter())
|
||||
.WithTypeConverter(ReplacementTextConverter.Instance)
|
||||
.WithNodeDeserializer(new WhenNodeDeserialiser())
|
||||
.IgnoreUnmatchedProperties()
|
||||
.Build();
|
||||
@ -111,11 +112,13 @@ public sealed class Plugin : IDalamudPlugin {
|
||||
|
||||
var when = rep.When.FirstOrDefault(when => when.IsValid(this));
|
||||
if (when != null) {
|
||||
return this.GetOrCreateReplacementPointer(when.Text, when.Slowly);
|
||||
var idx = Random.Shared.Next(when.Text.Count);
|
||||
return this.GetOrCreateReplacementPointer(when.Text[idx], when.Slowly);
|
||||
}
|
||||
|
||||
if (rep.Text != null) {
|
||||
return this.GetOrCreateReplacementPointer(rep.Text, rep.Slowly);
|
||||
var idx = Random.Shared.Next(rep.Text.Count);
|
||||
return this.GetOrCreateReplacementPointer(rep.Text[idx], rep.Slowly);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
Loading…
Reference in New Issue
Block a user