From 2e8cf81d97bdc9ea1c2c60a318b58e45bc39ff51 Mon Sep 17 00:00:00 2001 From: AnnoyingRains Date: Thu, 30 Nov 2023 00:10:27 +1100 Subject: [PATCH] Overhaul the difficultyData format We now store a dataVersion, which is enforced, and store the type of each note, allowing for special notes to exist --- CrankItUp.Game/src/Constants.cs | 2 + CrankItUp.Game/src/Elements/Beatmap.cs | 28 +++++-- .../src/Screens/DifficultySelect.cs | 77 ++++++++++++++++++- CrankItUp.Game/src/Screens/MappingScreen.cs | 11 ++- .../Beatmaps/Example/easy.json | 10 ++- 5 files changed, 113 insertions(+), 15 deletions(-) diff --git a/CrankItUp.Game/src/Constants.cs b/CrankItUp.Game/src/Constants.cs index bdf6024..e68cd80 100644 --- a/CrankItUp.Game/src/Constants.cs +++ b/CrankItUp.Game/src/Constants.cs @@ -11,5 +11,7 @@ public static class Constants public static readonly double CRANK_DEFAULT_LENGTH = 337; public static readonly double CRANK_DEFAULT_HEIGHT = 70; + + public static readonly int MAP_DATAVERSION = 1; } } diff --git a/CrankItUp.Game/src/Elements/Beatmap.cs b/CrankItUp.Game/src/Elements/Beatmap.cs index 23c6ee2..1002345 100644 --- a/CrankItUp.Game/src/Elements/Beatmap.cs +++ b/CrankItUp.Game/src/Elements/Beatmap.cs @@ -7,7 +7,6 @@ using osu.Framework.IO.Stores; using osu.Framework.Platform; using osu.Framework.Logging; -using osu.Framework.Extensions; namespace CrankItUp.Game { @@ -82,14 +81,27 @@ public Queue GetBaseNoteQueue() { Queue noteQueue = new Queue(); - foreach (JToken noteobject in beatmap.GetValue("BaseNoteQueue")) + foreach (JToken noteobject in beatmap.GetValue("noteQueue")) { - noteQueue.Enqueue( - new BaseNote( - noteobject.GetValue("position"), - noteobject.GetValue("spawnTime") - ) - ); + switch (noteobject.GetValue("noteType")) + { + case "Standard": + noteQueue.Enqueue( + new BaseNote( + noteobject.GetValue("position"), + noteobject.GetValue("spawnTime") + ) + ); + break; + case "Special": + Logger.Log( + "Map tried to spawn a special note, which is not yet implemented" + ); + break; + default: + Logger.Log("Unknown note type!"); + break; + } } return noteQueue; } diff --git a/CrankItUp.Game/src/Screens/DifficultySelect.cs b/CrankItUp.Game/src/Screens/DifficultySelect.cs index a2fe457..c1b374a 100644 --- a/CrankItUp.Game/src/Screens/DifficultySelect.cs +++ b/CrankItUp.Game/src/Screens/DifficultySelect.cs @@ -8,6 +8,10 @@ using osu.Framework.Graphics.Containers; using osu.Framework.Input.Events; using osu.Framework.Platform; +using Newtonsoft.Json.Linq; +using System; +using osu.Framework.Logging; +using NuGet.ProjectModel; namespace CrankItUp.Game { @@ -43,11 +47,64 @@ private void load(TextureStore textures, Storage storage) }; var difficulties = storage.GetFiles(Path.Combine("maps", map)); Vector2 position = new Vector2(0, 0); + Storage mapStorage = storage.GetStorageForDirectory("maps").GetStorageForDirectory(map); + int invalidDifficultyCount = 0; foreach (string difficulty in difficulties) { + JObject beatmap; if (difficulty.EndsWith(".json")) { - string difficultyname = difficulty[ + string difficultyname = difficulty[(difficulty.LastIndexOf("/") + 1)..]; + using ( + var sr = new StreamReader( + mapStorage.GetStream(difficultyname, mode: FileMode.Open) + ) + ) + { + // Read the stream as a string, and write the string to the console. + try + { + beatmap = JObject.Parse(sr.ReadToEnd()); + } + catch (Exception e) + { + Logger.Error( + e, + "Error while loading difficuly JSON! Skipping difficulty" + ); + invalidDifficultyCount += 1; + continue; + } + } + int dataVersion; + try + { + JToken meta = beatmap.GetValue("meta"); + dataVersion = meta.GetValue("dataVersion"); + } + catch (Exception e) + { + Logger.Error( + e, + "Error while checking difficulty dataVersion! Skipping difficulty" + ); + invalidDifficultyCount += 1; + continue; + } + if (dataVersion != Constants.MAP_DATAVERSION) + { + Logger.Log( + "Difficulty " + + difficultyname + + " has an invalid dataVersion for this version of the game!", + LoggingTarget.Runtime, + LogLevel.Important + ); + invalidDifficultyCount += 1; + continue; + } + + difficultyname = difficulty[ (difficulty.LastIndexOf("/") + 1)..(difficulty.Length - 5) ]; trackContainer.Add( @@ -68,6 +125,23 @@ private void load(TextureStore textures, Storage storage) position.X += 250; } } + + SpriteText invalidDifficultyText; + if (invalidDifficultyCount > 0) + { + invalidDifficultyText = new SpriteText + { + X = 10, + Y = 10, + Text = + "Warning: Map contains " + invalidDifficultyCount + " invalid difficulties!" + }; + } + else + { + // make an empty spritetext, as there is nothing to say + invalidDifficultyText = new SpriteText { }; + } InternalChildren = new Drawable[] { new DrawSizePreservingFillContainer @@ -82,6 +156,7 @@ private void load(TextureStore textures, Storage storage) }, backButton, trackContainer, + invalidDifficultyText, } }; } diff --git a/CrankItUp.Game/src/Screens/MappingScreen.cs b/CrankItUp.Game/src/Screens/MappingScreen.cs index 4760634..f8fa957 100644 --- a/CrankItUp.Game/src/Screens/MappingScreen.cs +++ b/CrankItUp.Game/src/Screens/MappingScreen.cs @@ -20,7 +20,7 @@ public partial class MappingScreen : Screen string difficulty; Track track; JObject beatmap = new JObject(); - JArray BaseNoteQueue = new JArray(); + JArray NoteQueue = new JArray(); Storage storage; public MappingScreen(string difficultyname) @@ -71,10 +71,11 @@ protected override bool OnKeyDown(KeyDownEvent e) { JObject note = new JObject { + { "noteType", "Standard" }, { "position", 0 }, { "spawnTime", track.CurrentTime } }; - BaseNoteQueue.Add(note); + NoteQueue.Add(note); } // if the user wishes to exit else if (e.Key == osuTK.Input.Key.Escape) @@ -82,14 +83,16 @@ protected override bool OnKeyDown(KeyDownEvent e) JObject meta = new JObject { // use default values for now + { "dataVersion", "1" }, { "radius", 50 }, { "approachRate", 100 }, // set the current time as the endTime - { "endTime", track.CurrentTime } + { "endTime", track.CurrentTime }, + { "startTime", 0 } }; // add everything to the beatmap beatmap.Add("meta", meta); - beatmap.Add("BaseNoteQueue", BaseNoteQueue); + beatmap.Add("noteQueue", NoteQueue); // save it to disk StreamWriter mapfile = new StreamWriter( storage.CreateFileSafely(Path.Combine("maps", "WIP", difficulty + ".json")) diff --git a/CrankItUp.Resources/Beatmaps/Example/easy.json b/CrankItUp.Resources/Beatmaps/Example/easy.json index 415d531..88b1f09 100644 --- a/CrankItUp.Resources/Beatmaps/Example/easy.json +++ b/CrankItUp.Resources/Beatmaps/Example/easy.json @@ -1,26 +1,32 @@ { "meta": { "radius": 50, - "approachRate": 100 + "approachRate": 100, + "dataVersion": 1 }, - "BaseNoteQueue": [ + "noteQueue": [ { + "noteType": "Standard", "position": 0, "spawnTime": 0 }, { + "noteType": "Standard", "position": 5, "spawnTime": 200 }, { + "noteType": "Standard", "position": 10, "spawnTime": 400 }, { + "noteType": "Standard", "position": 15, "spawnTime": 600 }, { + "noteType": "Standard", "position": 20, "spawnTime": 800 }