Skip to content

Commit

Permalink
Added map metadata file, with many options
Browse files Browse the repository at this point in the history
Some options are unused for now
  • Loading branch information
AnnoyingRain5 committed Nov 30, 2023
1 parent 2e8cf81 commit 70a223a
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 29 deletions.
1 change: 1 addition & 0 deletions CrankItUp.Game/src/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public static class Constants
public static readonly double CRANK_DEFAULT_HEIGHT = 70;

public static readonly int MAP_DATAVERSION = 1;
public static readonly int METADATA_DATAVERSION = 1;
}
}
12 changes: 7 additions & 5 deletions CrankItUp.Game/src/Elements/Beatmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Beatmap
public readonly double endTime;
public readonly double startTime;
private readonly Storage mapStorage;
private readonly TrackMetadata trackmeta;

/// <summary>Creates a new instance of the Beatmap class.</summary>
/// <param name="mapname">The name of the map.</param>
Expand Down Expand Up @@ -55,12 +56,13 @@ public Beatmap(string mapname, string difficulty, AudioManager audio, Storage st
ITrackStore trackStore = audio.GetTrackStore(
new StorageBackedResourceStore(mapStorage)
);
track = trackStore.Get("music.mp3");
trackmeta = new TrackMetadata(mapStorage.GetStream("metadata.json"));
track = trackStore.Get(trackmeta.trackFilename);

JToken meta = beatmap.GetValue("meta");
noteRadius = meta.GetValue<double>("radius");
approachRate = meta.GetValue<double>("approachRate");
endTime = meta.GetValue<double>("endTime");
JToken meta = beatmap.GetValueOrFail("meta");
noteRadius = meta.GetValueOrFail<double>("radius");
approachRate = meta.GetValueOrFail<double>("approachRate");
endTime = meta.GetValueOrFail<double>("endTime");
// for some reason, GetValue<double> returns 0 if the value is not found
if (endTime == 0)
{
Expand Down
34 changes: 34 additions & 0 deletions CrankItUp.Game/src/JobjectGetValueOrFail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Newtonsoft.Json.Linq;

namespace CrankItUp.Game
{
public static class JobjectGetValueOrFail
{
public static JToken GetValueOrFail(this JToken token, string name)
{
if (
token
.ToObject<JObject>()
.TryGetValue(name, StringComparison.CurrentCulture, out JToken value) == false
)
{
throw new ArgumentException("Value not found!");
}
return value;
}

public static T GetValueOrFail<T>(this JToken token, string name)
{
if (
token
.ToObject<JObject>()
.TryGetValue(name, StringComparison.CurrentCulture, out JToken value) == false
)
{
throw new ArgumentException("Value not found!");
}
return value.Value<T>();
}
}
}
33 changes: 30 additions & 3 deletions CrankItUp.Game/src/Screens/DifficultySelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,28 @@
using System;
using osu.Framework.Logging;
using NuGet.ProjectModel;
using osu.Framework.Audio;
using osu.Framework.IO.Stores;
using osu.Framework.Audio.Track;

namespace CrankItUp.Game
{
public partial class DifficultySelect : Screen
{
Container trackContainer;
CIUButton backButton;
Track track;
string map;
TrackMetadata trackmeta;

public DifficultySelect(string mapname)
public DifficultySelect(string mapname, TrackMetadata trackmeta)
{
map = mapname;
this.trackmeta = trackmeta;
}

[BackgroundDependencyLoader]
private void load(TextureStore textures, Storage storage)
private void load(AudioManager audio, TextureStore textures, Storage storage)
{
backButton = new CIUButton(textures)
{
Expand All @@ -45,10 +51,19 @@ private void load(TextureStore textures, Storage storage)
Origin = Anchor.BottomRight,
Position = new Vector2(0, 100)
};

ITrackStore trackStore = audio.GetTrackStore(
new StorageBackedResourceStore(
storage.GetStorageForDirectory(Path.Combine("maps", map))
)
);
track = trackStore.Get(trackmeta.trackFilename);

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;
Expand All @@ -61,7 +76,6 @@ private void load(TextureStore textures, Storage storage)
)
)
{
// Read the stream as a string, and write the string to the console.
try
{
beatmap = JObject.Parse(sr.ReadToEnd());
Expand Down Expand Up @@ -127,6 +141,7 @@ private void load(TextureStore textures, Storage storage)
}

SpriteText invalidDifficultyText;
invalidDifficultyCount -= 1; // the metadata file will always be an invalid difficulty
if (invalidDifficultyCount > 0)
{
invalidDifficultyText = new SpriteText
Expand Down Expand Up @@ -161,6 +176,12 @@ private void load(TextureStore textures, Storage storage)
};
}

protected override void LoadComplete()
{
track.Start();
track.Seek(trackmeta.trackPreviewStart);
}

private void pushMenu()
{
this.Exit();
Expand Down Expand Up @@ -189,5 +210,11 @@ public override void OnResuming(ScreenTransitionEvent e)
{
this.FadeInFromZero(500, Easing.OutQuint);
}

public override bool OnExiting(ScreenExitEvent e)
{
track.Stop();
return base.OnExiting(e);
}
}
}
15 changes: 12 additions & 3 deletions CrankItUp.Game/src/Screens/InitialSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,23 @@ void PushMenu(AudioManager audio, Storage storage)
Stream exampleMap = CrankItUpResources.ResourceAssembly.GetManifestResourceStream(
"CrankItUp.Resources.Beatmaps.Example.easy.json"
);

Logger.Log(exampleMap.ToString());

exampleMap.CopyTo(exampleMapFileStream);

exampleMapFileStream.Dispose();
exampleMap.Dispose();

Stream exampleMetadataFileStream = storage.CreateFileSafely(
"maps/Example/metadata.json"
);
// there is no *store class for text files, so we have to pull it from the ResourceAssembly manually
Stream exampleMetadata = CrankItUpResources.ResourceAssembly.GetManifestResourceStream(
"CrankItUp.Resources.Beatmaps.Example.metadata.json"
);
exampleMetadata.CopyTo(exampleMetadataFileStream);

exampleMetadataFileStream.Dispose();
exampleMetadata.Dispose();

Stream exampleSong = storage.CreateFileSafely("maps/Example/music.mp3");
audio.GetTrackStore().GetStream("Tera I_O.mp3").CopyTo(exampleSong);
exampleSong.Dispose();
Expand Down
8 changes: 5 additions & 3 deletions CrankItUp.Game/src/Screens/MappingScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ namespace CrankItUp.Game
public partial class MappingScreen : Screen
{
string difficulty;
private string trackFilename;
Track track;
JObject beatmap = new JObject();
JArray NoteQueue = new JArray();
Storage storage;

public MappingScreen(string difficultyname)
public MappingScreen(string difficulty, string trackFilename)
{
difficulty = difficultyname;
this.difficulty = difficulty;
this.trackFilename = trackFilename;
}

[BackgroundDependencyLoader]
Expand All @@ -52,7 +54,7 @@ private void load(AudioManager audio, Storage store)
}
};

track = trackStore.Get("music.mp3");
track = trackStore.Get(trackFilename);
}

protected override void LoadComplete()
Expand Down
8 changes: 7 additions & 1 deletion CrankItUp.Game/src/Screens/MappingSetupDifficulty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public partial class MappingSetupDifficulty : Screen
CIUButton backButton;
CIUButton confirmButton;
BasicTextBox difficultySelector;
string trackFilename;

public MappingSetupDifficulty(string trackFilename)
{
this.trackFilename = trackFilename;
}

[BackgroundDependencyLoader]
private void load(TextureStore textures)
Expand Down Expand Up @@ -85,7 +91,7 @@ public override void OnEntering(ScreenTransitionEvent e)

void PushMappingScreen()
{
this.Push(new MappingScreen(difficultySelector.Text));
this.Push(new MappingScreen(difficultySelector.Text, trackFilename));
}

void PushMenu()
Expand Down
88 changes: 78 additions & 10 deletions CrankItUp.Game/src/Screens/MappingSetupTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
using osuTK;
using osu.Framework.Graphics.Textures;
using osu.Framework.Input.Events;
using System.Diagnostics;
using System.IO;
using osu.Framework.Platform;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface;
using Newtonsoft.Json.Linq;
using System.IO;
using NuGet.Protocol;

namespace CrankItUp.Game
{
Expand All @@ -17,6 +19,11 @@ public partial class MappingSetupTrack : Screen
CIUButton backButton;
CIUButton continueButton;
CIUButton mapsFolderButton;
BasicTextBox trackFileNameBox;
private BasicTextBox trackNameBox;
private BasicTextBox mapperNameBox;
private BasicTextBox artistNameBox;
private BasicTextBox trackPreviewStartBox;

[BackgroundDependencyLoader]
private void load(TextureStore textures, Storage storage)
Expand All @@ -28,7 +35,7 @@ private void load(TextureStore textures, Storage storage)
Text = "Back to menu",
Size = new Vector2(200, 40),
Margin = new MarginPadding(10),
Position = new Vector2(0, 110),
Position = new Vector2(0, 170),
Action = () => PushMenu(),
};
continueButton = new CIUButton(textures)
Expand All @@ -38,8 +45,8 @@ private void load(TextureStore textures, Storage storage)
Text = "Next",
Size = new Vector2(200, 40),
Margin = new MarginPadding(10),
Position = new Vector2(0, 60),
Action = () => PushSetupDifficulty(),
Position = new Vector2(0, 110),
Action = () => PushSetupDifficulty(storage),
};
mapsFolderButton = new CIUButton(textures)
{
Expand All @@ -48,9 +55,51 @@ private void load(TextureStore textures, Storage storage)
Text = "Open maps folder",
Size = new Vector2(200, 40),
Margin = new MarginPadding(10),
Position = new Vector2(0, 10),
Position = new Vector2(0, 60),
Action = () => storage.GetStorageForDirectory("maps").PresentExternally(),
};

trackFileNameBox = new BasicTextBox()
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Position = new Vector2(-420, 10),
Size = new Vector2(200, 40),
PlaceholderText = "Song filename"
};
trackNameBox = new BasicTextBox()
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Position = new Vector2(-210, 10),
Size = new Vector2(200, 40),
PlaceholderText = "Track name"
};
mapperNameBox = new BasicTextBox()
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Position = new Vector2(0, 10),
Size = new Vector2(200, 40),
PlaceholderText = "Mapper name (you)"
};
artistNameBox = new BasicTextBox()
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Position = new Vector2(210, 10),
Size = new Vector2(200, 40),
PlaceholderText = "Artist Name"
};
trackPreviewStartBox = new BasicTextBox()
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Position = new Vector2(445, 10),
Size = new Vector2(250, 40),
PlaceholderText = "Preview start time (ms)"
};

InternalChildren = new Drawable[]
{
new DrawSizePreservingFillContainer
Expand All @@ -63,6 +112,11 @@ private void load(TextureStore textures, Storage storage)
Origin = Anchor.TopCentre,
Font = FontUsage.Default.With(size: 40)
},
trackFileNameBox,
trackNameBox,
mapperNameBox,
artistNameBox,
trackPreviewStartBox,
mapsFolderButton,
continueButton,
backButton,
Expand All @@ -76,8 +130,8 @@ private void load(TextureStore textures, Storage storage)
"1: open the 'maps' folder using the button below",
"2: create a folder called 'WIP'",
"3: Put the mp3 of the song wish to map inside this folder",
"4: rename the mp3 file to `music` (keep the file extension intact)",
"5: return here and select the map you wish to add a difficulty for!",
"4: Put the exact name of the file in the box below, case sensitive, include the file extension.",
"5: Return here and select the map you wish to add a difficulty for!",
"6: When you are finished mapping in the editor, rename the folder to the name of the map",
"7: You can then edit the positions of these objects by editing the json file in a text editor"
};
Expand Down Expand Up @@ -115,9 +169,23 @@ void PushMenu()
this.Exit();
}

void PushSetupDifficulty()
void PushSetupDifficulty(Storage storage)
{
this.Push(new MappingSetupDifficulty());
JObject meta = new JObject
{
{ "dataVersion", Constants.METADATA_DATAVERSION },
{ "name", trackNameBox.Text },
{ "mapperName", mapperNameBox.Text },
{ "trackArtistName", artistNameBox.Text },
{ "trackFilename", trackFileNameBox.Text },
{ "trackPreviewStart", trackPreviewStartBox.Text }
};
StreamWriter metafile = new StreamWriter(
storage.CreateFileSafely("maps/WIP/metadata.json")
);
metafile.Write(meta.ToJson(Newtonsoft.Json.Formatting.Indented));
metafile.Dispose();
this.Push(new MappingSetupDifficulty(trackFileNameBox.Text));
}
}
}
Loading

0 comments on commit 70a223a

Please sign in to comment.