From 08245b9c4e207e42fe17e7a15bef46f799891760 Mon Sep 17 00:00:00 2001 From: Bohdan Date: Wed, 25 Oct 2023 21:10:29 +0300 Subject: [PATCH] decimal convertion fix && hurt note feature --- FNFDataAPI/FridayNightFunkin/FNFSong.cs | 9 +++ FNFDataAPI/FridayNightFunkin/Json/Note.cs | 73 ++++++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/FNFDataAPI/FridayNightFunkin/FNFSong.cs b/FNFDataAPI/FridayNightFunkin/FNFSong.cs index f0c9643..e1cb4a9 100644 --- a/FNFDataAPI/FridayNightFunkin/FNFSong.cs +++ b/FNFDataAPI/FridayNightFunkin/FNFSong.cs @@ -109,6 +109,7 @@ public void SaveSong(string savePath) public class FNFSection { + private const int ValidSectionCapacity = 3; public Note dataNote { get; } public List Notes { get; set; } public bool MustHitSection { get => dataNote.MustHitSection; set => dataNote.MustHitSection = value; } @@ -156,6 +157,14 @@ public FNFSection(Note prvDataNote) Notes = new List(); foreach (List n in dataNote.sectionNotes) { + // if some notes are missing + while (n.Count != ValidSectionCapacity) + { + if (n.Count < ValidSectionCapacity) + n.Add(0); + if (n.Count > ValidSectionCapacity) + n.RemoveAt(n.Count - 1); + } Notes.Add(new FNFNote(n[0],n[1],n[2])); } } diff --git a/FNFDataAPI/FridayNightFunkin/Json/Note.cs b/FNFDataAPI/FridayNightFunkin/Json/Note.cs index 92dd61b..3a52d2e 100644 --- a/FNFDataAPI/FridayNightFunkin/Json/Note.cs +++ b/FNFDataAPI/FridayNightFunkin/Json/Note.cs @@ -1,5 +1,8 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace FridayNightFunkin.Json { @@ -15,6 +18,7 @@ public class Note public long LengthInSteps { get; set; } [JsonProperty("sectionNotes")] + [JsonConverter(typeof(OnlyDecimalConverter))] public List> sectionNotes { get; set; } [JsonProperty("bpm", NullValueHandling = NullValueHandling.Ignore)] @@ -23,4 +27,71 @@ public class Note [JsonProperty("changeBPM", NullValueHandling = NullValueHandling.Ignore)] public bool? ChangeBpm { get; set; } } + + public class OnlyDecimalConverter : JsonConverter + { + public static bool DisableHurtNotes { get; set; } + + public override bool CanConvert(Type objectType) + { + return objectType == typeof(List>); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + JArray array = JArray.Load(reader); + List> onlyDecimals = new List>(); + List listOfDecimals = new List(); + + foreach (JToken token in array.Children()) + { + if (DisableHurtNotes) + foreach (var item in token.ToArray()) + { + if (item.Type == JTokenType.String) + { + token[1] = int.MaxValue; // set non existing NoteType if hurt note has been found + } + } + + foreach (var item in token) + { + if (item.Type == JTokenType.Float || item.Type == JTokenType.Integer) + listOfDecimals.Add(item.ToObject()); // filter all non compatible types + } + + onlyDecimals.Add(listOfDecimals.ToList()); + listOfDecimals.Clear(); + } + + return onlyDecimals; + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + if (value == null) + { + writer.WriteNull(); + return; + } + + var notes = (List>)value; + + writer.WriteStartArray(); + + foreach (var note in notes) + { + writer.WriteStartArray(); + + foreach (var n in note) + { + writer.WriteValue(n); + } + + writer.WriteEndArray(); + } + + writer.WriteEndArray(); + } + } } \ No newline at end of file