Skip to content

decimal convertion fix && hurt note feature #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions FNFDataAPI/FridayNightFunkin/FNFSong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void SaveSong(string savePath)

public class FNFSection
{
private const int ValidSectionCapacity = 3;
public Note dataNote { get; }
public List<FNFNote> Notes { get; set; }
public bool MustHitSection { get => dataNote.MustHitSection; set => dataNote.MustHitSection = value; }
Expand Down Expand Up @@ -156,6 +157,14 @@ public FNFSection(Note prvDataNote)
Notes = new List<FNFNote>();
foreach (List<decimal> 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]));
}
}
Expand Down
73 changes: 72 additions & 1 deletion FNFDataAPI/FridayNightFunkin/Json/Note.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -15,6 +18,7 @@ public class Note
public long LengthInSteps { get; set; }

[JsonProperty("sectionNotes")]
[JsonConverter(typeof(OnlyDecimalConverter))]
public List<List<decimal>> sectionNotes { get; set; }

[JsonProperty("bpm", NullValueHandling = NullValueHandling.Ignore)]
Expand All @@ -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<List<decimal>>);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JArray array = JArray.Load(reader);
List<List<decimal>> onlyDecimals = new List<List<decimal>>();
List<decimal> listOfDecimals = new List<decimal>();

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<decimal>()); // 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<List<decimal>>)value;

writer.WriteStartArray();

foreach (var note in notes)
{
writer.WriteStartArray();

foreach (var n in note)
{
writer.WriteValue(n);
}

writer.WriteEndArray();
}

writer.WriteEndArray();
}
}
}