From 0b770d122595547b9a766cef2b4864f926ae28d6 Mon Sep 17 00:00:00 2001 From: ColdVolcano Date: Fri, 26 May 2017 23:55:34 -0500 Subject: [PATCH 1/8] Allow for variables in storyboards --- osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs index cb1d9d2fcd..78ef60748f 100644 --- a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs +++ b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs @@ -2,6 +2,7 @@ // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; +using System.Collections.Generic; using System.Globalization; using System.IO; using OpenTK.Graphics; @@ -31,6 +32,8 @@ namespace osu.Game.Beatmaps.Formats private ConvertHitObjectParser parser; + private Dictionary variables = new Dictionary(); + private LegacySampleBank defaultSampleBank; private int defaultSampleVolume = 100; @@ -56,6 +59,7 @@ namespace osu.Game.Beatmaps.Formats TimingPoints, Colours, HitObjects, + Variables, } private void handleGeneral(Beatmap beatmap, string key, string val) @@ -391,7 +395,7 @@ namespace osu.Game.Beatmaps.Formats } string val = line, key = null; - if (section != Section.Events && section != Section.TimingPoints && section != Section.HitObjects) + if (section != Section.Events && section != Section.TimingPoints && section != Section.HitObjects && section != Section.Variables) { key = val.Remove(val.IndexOf(':')).Trim(); val = val.Substring(val.IndexOf(':') + 1).Trim(); @@ -411,6 +415,13 @@ namespace osu.Game.Beatmaps.Formats handleDifficulty(beatmap, key, val); break; case Section.Events: + string[] valSplit = val.Split(','); + for (int i = 0; i < valSplit.Length; i++) + { + if (valSplit[i][0] == '$' && variables.ContainsKey(valSplit[i])) + valSplit[i] = variables[valSplit[i]]; + } + val = string.Join(",", valSplit); handleEvents(beatmap, val); break; case Section.TimingPoints: @@ -426,6 +437,11 @@ namespace osu.Game.Beatmaps.Formats beatmap.HitObjects.Add(obj); break; + case Section.Variables: + key = val.Remove(val.IndexOf('=')).Trim(); + val = val.Substring(val.IndexOf('=') + 1).Trim(); + variables[key] = val; + break; } } } From a523dfc3884c920ee3ea1bfba5496eabb17c6b6d Mon Sep 17 00:00:00 2001 From: ColdVolcano Date: Sat, 27 May 2017 00:13:46 -0500 Subject: [PATCH 2/8] Allow variables of variables Some storyboards like to do tricky stuff --- osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs index 78ef60748f..6716af371c 100644 --- a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs +++ b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs @@ -32,7 +32,7 @@ namespace osu.Game.Beatmaps.Formats private ConvertHitObjectParser parser; - private Dictionary variables = new Dictionary(); + private readonly Dictionary variables = new Dictionary(); private LegacySampleBank defaultSampleBank; private int defaultSampleVolume = 100; @@ -415,13 +415,16 @@ namespace osu.Game.Beatmaps.Formats handleDifficulty(beatmap, key, val); break; case Section.Events: - string[] valSplit = val.Split(','); - for (int i = 0; i < valSplit.Length; i++) + do { - if (valSplit[i][0] == '$' && variables.ContainsKey(valSplit[i])) - valSplit[i] = variables[valSplit[i]]; - } - val = string.Join(",", valSplit); + string[] valSplit = val.Split(','); + for (int i = 0; i < valSplit.Length; i++) + { + if (valSplit[i][0] == '$' && variables.ContainsKey(valSplit[i])) + valSplit[i] = variables[valSplit[i]]; + } + val = string.Join(",", valSplit); + } while (val.IndexOf('$') != -1); handleEvents(beatmap, val); break; case Section.TimingPoints: From e46e1d96a7b382df862869a60bc997f27b90ecaf Mon Sep 17 00:00:00 2001 From: ColdVolcano Date: Sat, 27 May 2017 00:20:19 -0500 Subject: [PATCH 3/8] Move logic to handleEvent --- osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs index 6716af371c..2045293a58 100644 --- a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs +++ b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs @@ -208,6 +208,17 @@ namespace osu.Game.Beatmaps.Formats private void handleEvents(Beatmap beatmap, string val) { + do + { + string[] valSplit = val.Split(','); + for (int i = 0; i < valSplit.Length; i++) + { + if (valSplit[i][0] == '$' && variables.ContainsKey(valSplit[i])) + valSplit[i] = variables[valSplit[i]]; + } + val = string.Join(",", valSplit); + } while (val.IndexOf('$') != -1); + string[] split = val.Split(','); EventType type; @@ -415,16 +426,6 @@ namespace osu.Game.Beatmaps.Formats handleDifficulty(beatmap, key, val); break; case Section.Events: - do - { - string[] valSplit = val.Split(','); - for (int i = 0; i < valSplit.Length; i++) - { - if (valSplit[i][0] == '$' && variables.ContainsKey(valSplit[i])) - valSplit[i] = variables[valSplit[i]]; - } - val = string.Join(",", valSplit); - } while (val.IndexOf('$') != -1); handleEvents(beatmap, val); break; case Section.TimingPoints: From 0175b9192793f02a0f77bee202488af20f2f1b37 Mon Sep 17 00:00:00 2001 From: smoogipooo Date: Tue, 30 May 2017 19:52:21 +0900 Subject: [PATCH 4/8] Cleanups. --- osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs | 76 ++++++++++++------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs index e2884dc5e2..8856d58d37 100644 --- a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs +++ b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs @@ -10,6 +10,7 @@ using osu.Game.Beatmaps.Timing; using osu.Game.Beatmaps.Legacy; using osu.Game.Rulesets.Objects.Legacy; using osu.Game.Beatmaps.ControlPoints; +using System.Linq; namespace osu.Game.Beatmaps.Formats { @@ -206,18 +207,29 @@ namespace osu.Game.Beatmaps.Formats } } + /// + /// Decodes any beatmap variables present in a string value. + /// + /// The value which contains variables. + private void decodeVariables(ref string val) + { + while (val.IndexOf('$') >= 0) + { + string[] split = val.Split(','); + for (int i = 0; i < split.Length; i++) + { + var item = split[i]; + if (item.StartsWith("$") && variables.ContainsKey(item)) + item = variables[item]; + } + + val = string.Join(",", split); + } + } + private void handleEvents(Beatmap beatmap, string val) { - do - { - string[] valSplit = val.Split(','); - for (int i = 0; i < valSplit.Length; i++) - { - if (valSplit[i][0] == '$' && variables.ContainsKey(valSplit[i])) - valSplit[i] = variables[valSplit[i]]; - } - val = string.Join(",", valSplit); - } while (val.IndexOf('$') != -1); + decodeVariables(ref val); string[] split = val.Split(','); @@ -405,46 +417,56 @@ namespace osu.Game.Beatmaps.Formats continue; } - string val = line, key = null; - if (section != Section.Events && section != Section.TimingPoints && section != Section.HitObjects && section != Section.Variables) - { - key = val.Remove(val.IndexOf(':')).Trim(); - val = val.Substring(val.IndexOf(':') + 1).Trim(); - } + string key = null, value = null; + switch (section) { case Section.General: - handleGeneral(beatmap, key, val); + case Section.Editor: + case Section.Metadata: + case Section.Difficulty: + case Section.Colours: + key = line.Remove(line.IndexOf(':')).Trim(); + value = line.Substring(line.IndexOf(':') + 1).Trim(); + break; + case Section.Variables: + key = line.Remove(line.IndexOf('=')).Trim(); + value = line.Substring(line.IndexOf('=') + 1).Trim(); + break; + } + + switch (section) + { + case Section.General: + handleGeneral(beatmap, key, value); break; case Section.Editor: - handleEditor(beatmap, key, val); + handleEditor(beatmap, key, value); break; case Section.Metadata: - handleMetadata(beatmap, key, val); + handleMetadata(beatmap, key, value); break; case Section.Difficulty: - handleDifficulty(beatmap, key, val); + handleDifficulty(beatmap, key, value); break; case Section.Events: - handleEvents(beatmap, val); + handleEvents(beatmap, line); break; case Section.TimingPoints: - handleTimingPoints(beatmap, val); + handleTimingPoints(beatmap, line); break; case Section.Colours: - handleColours(beatmap, key, val, ref hasCustomColours); + handleColours(beatmap, key, value, ref hasCustomColours); break; case Section.HitObjects: - var obj = parser.Parse(val); + var obj = parser.Parse(line); if (obj != null) beatmap.HitObjects.Add(obj); break; case Section.Variables: - key = val.Remove(val.IndexOf('=')).Trim(); - val = val.Substring(val.IndexOf('=') + 1).Trim(); - variables[key] = val; + variables[key] = value; break; } } From 93a33be596d59561def6e1318a861c335b928c54 Mon Sep 17 00:00:00 2001 From: smoogipooo Date: Tue, 30 May 2017 19:52:43 +0900 Subject: [PATCH 5/8] val -> value, val -> line (in cases where a key is not used). --- osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs | 102 +++++++++--------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs index 8856d58d37..600dc1cfea 100644 --- a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs +++ b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs @@ -63,34 +63,34 @@ namespace osu.Game.Beatmaps.Formats Variables, } - private void handleGeneral(Beatmap beatmap, string key, string val) + private void handleGeneral(Beatmap beatmap, string key, string value) { var metadata = beatmap.BeatmapInfo.Metadata; switch (key) { case @"AudioFilename": - metadata.AudioFile = val; + metadata.AudioFile = value; break; case @"AudioLeadIn": - beatmap.BeatmapInfo.AudioLeadIn = int.Parse(val); + beatmap.BeatmapInfo.AudioLeadIn = int.Parse(value); break; case @"PreviewTime": - metadata.PreviewTime = int.Parse(val); + metadata.PreviewTime = int.Parse(value); break; case @"Countdown": - beatmap.BeatmapInfo.Countdown = int.Parse(val) == 1; + beatmap.BeatmapInfo.Countdown = int.Parse(value) == 1; break; case @"SampleSet": - defaultSampleBank = (LegacySampleBank)Enum.Parse(typeof(LegacySampleBank), val); + defaultSampleBank = (LegacySampleBank)Enum.Parse(typeof(LegacySampleBank), value); break; case @"SampleVolume": - defaultSampleVolume = int.Parse(val); + defaultSampleVolume = int.Parse(value); break; case @"StackLeniency": - beatmap.BeatmapInfo.StackLeniency = float.Parse(val, NumberFormatInfo.InvariantInfo); + beatmap.BeatmapInfo.StackLeniency = float.Parse(value, NumberFormatInfo.InvariantInfo); break; case @"Mode": - beatmap.BeatmapInfo.RulesetID = int.Parse(val); + beatmap.BeatmapInfo.RulesetID = int.Parse(value); switch (beatmap.BeatmapInfo.RulesetID) { @@ -109,113 +109,113 @@ namespace osu.Game.Beatmaps.Formats } break; case @"LetterboxInBreaks": - beatmap.BeatmapInfo.LetterboxInBreaks = int.Parse(val) == 1; + beatmap.BeatmapInfo.LetterboxInBreaks = int.Parse(value) == 1; break; case @"SpecialStyle": - beatmap.BeatmapInfo.SpecialStyle = int.Parse(val) == 1; + beatmap.BeatmapInfo.SpecialStyle = int.Parse(value) == 1; break; case @"WidescreenStoryboard": - beatmap.BeatmapInfo.WidescreenStoryboard = int.Parse(val) == 1; + beatmap.BeatmapInfo.WidescreenStoryboard = int.Parse(value) == 1; break; } } - private void handleEditor(Beatmap beatmap, string key, string val) + private void handleEditor(Beatmap beatmap, string key, string value) { switch (key) { case @"Bookmarks": - beatmap.BeatmapInfo.StoredBookmarks = val; + beatmap.BeatmapInfo.StoredBookmarks = value; break; case @"DistanceSpacing": - beatmap.BeatmapInfo.DistanceSpacing = double.Parse(val, NumberFormatInfo.InvariantInfo); + beatmap.BeatmapInfo.DistanceSpacing = double.Parse(value, NumberFormatInfo.InvariantInfo); break; case @"BeatDivisor": - beatmap.BeatmapInfo.BeatDivisor = int.Parse(val); + beatmap.BeatmapInfo.BeatDivisor = int.Parse(value); break; case @"GridSize": - beatmap.BeatmapInfo.GridSize = int.Parse(val); + beatmap.BeatmapInfo.GridSize = int.Parse(value); break; case @"TimelineZoom": - beatmap.BeatmapInfo.TimelineZoom = double.Parse(val, NumberFormatInfo.InvariantInfo); + beatmap.BeatmapInfo.TimelineZoom = double.Parse(value, NumberFormatInfo.InvariantInfo); break; } } - private void handleMetadata(Beatmap beatmap, string key, string val) + private void handleMetadata(Beatmap beatmap, string key, string value) { var metadata = beatmap.BeatmapInfo.Metadata; switch (key) { case @"Title": - metadata.Title = val; + metadata.Title = value; break; case @"TitleUnicode": - metadata.TitleUnicode = val; + metadata.TitleUnicode = value; break; case @"Artist": - metadata.Artist = val; + metadata.Artist = value; break; case @"ArtistUnicode": - metadata.ArtistUnicode = val; + metadata.ArtistUnicode = value; break; case @"Creator": - metadata.Author = val; + metadata.Author = value; break; case @"Version": - beatmap.BeatmapInfo.Version = val; + beatmap.BeatmapInfo.Version = value; break; case @"Source": - beatmap.BeatmapInfo.Metadata.Source = val; + beatmap.BeatmapInfo.Metadata.Source = value; break; case @"Tags": - beatmap.BeatmapInfo.Metadata.Tags = val; + beatmap.BeatmapInfo.Metadata.Tags = value; break; case @"BeatmapID": - beatmap.BeatmapInfo.OnlineBeatmapID = int.Parse(val); + beatmap.BeatmapInfo.OnlineBeatmapID = int.Parse(value); break; case @"BeatmapSetID": - beatmap.BeatmapInfo.OnlineBeatmapSetID = int.Parse(val); - metadata.OnlineBeatmapSetID = int.Parse(val); + beatmap.BeatmapInfo.OnlineBeatmapSetID = int.Parse(value); + metadata.OnlineBeatmapSetID = int.Parse(value); break; } } - private void handleDifficulty(Beatmap beatmap, string key, string val) + private void handleDifficulty(Beatmap beatmap, string key, string value) { var difficulty = beatmap.BeatmapInfo.Difficulty; switch (key) { case @"HPDrainRate": - difficulty.DrainRate = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.DrainRate = float.Parse(value, NumberFormatInfo.InvariantInfo); break; case @"CircleSize": - difficulty.CircleSize = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.CircleSize = float.Parse(value, NumberFormatInfo.InvariantInfo); break; case @"OverallDifficulty": - difficulty.OverallDifficulty = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.OverallDifficulty = float.Parse(value, NumberFormatInfo.InvariantInfo); break; case @"ApproachRate": - difficulty.ApproachRate = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.ApproachRate = float.Parse(value, NumberFormatInfo.InvariantInfo); break; case @"SliderMultiplier": - difficulty.SliderMultiplier = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.SliderMultiplier = float.Parse(value, NumberFormatInfo.InvariantInfo); break; case @"SliderTickRate": - difficulty.SliderTickRate = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.SliderTickRate = float.Parse(value, NumberFormatInfo.InvariantInfo); break; } } /// - /// Decodes any beatmap variables present in a string value. + /// Decodes any beatmap variables present in a line into their real values. /// - /// The value which contains variables. - private void decodeVariables(ref string val) + /// The line which may contains variables. + private void decodeVariables(ref string line) { - while (val.IndexOf('$') >= 0) + while (line.IndexOf('$') >= 0) { - string[] split = val.Split(','); + string[] split = line.Split(','); for (int i = 0; i < split.Length; i++) { var item = split[i]; @@ -223,15 +223,15 @@ namespace osu.Game.Beatmaps.Formats item = variables[item]; } - val = string.Join(",", split); + line = string.Join(",", split); } } - private void handleEvents(Beatmap beatmap, string val) + private void handleEvents(Beatmap beatmap, string line) { - decodeVariables(ref val); + decodeVariables(ref line); - string[] split = val.Split(','); + string[] split = line.Split(','); EventType type; if (!Enum.TryParse(split[0], out type)) @@ -263,9 +263,9 @@ namespace osu.Game.Beatmaps.Formats } } - private void handleTimingPoints(Beatmap beatmap, string val) + private void handleTimingPoints(Beatmap beatmap, string line) { - string[] split = val.Split(','); + string[] split = line.Split(','); double time = double.Parse(split[0].Trim(), NumberFormatInfo.InvariantInfo); double beatLength = double.Parse(split[1].Trim(), NumberFormatInfo.InvariantInfo); @@ -348,12 +348,12 @@ namespace osu.Game.Beatmaps.Formats } } - private void handleColours(Beatmap beatmap, string key, string val, ref bool hasCustomColours) + private void handleColours(Beatmap beatmap, string key, string value, ref bool hasCustomColours) { - string[] split = val.Split(','); + string[] split = value.Split(','); if (split.Length != 3) - throw new InvalidOperationException($@"Color specified in incorrect format (should be R,G,B): {val}"); + throw new InvalidOperationException($@"Color specified in incorrect format (should be R,G,B): {value}"); byte r, g, b; if (!byte.TryParse(split[0], out r) || !byte.TryParse(split[1], out g) || !byte.TryParse(split[2], out b)) From 0728aea6a4f70acdc23774f1167a22eb934652be Mon Sep 17 00:00:00 2001 From: smoogipooo Date: Tue, 30 May 2017 20:26:39 +0900 Subject: [PATCH 6/8] Fixes + cleanup. --- osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs index 600dc1cfea..e9f6c66630 100644 --- a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs +++ b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs @@ -10,7 +10,6 @@ using osu.Game.Beatmaps.Timing; using osu.Game.Beatmaps.Legacy; using osu.Game.Rulesets.Objects.Legacy; using osu.Game.Beatmaps.ControlPoints; -using System.Linq; namespace osu.Game.Beatmaps.Formats { @@ -220,7 +219,7 @@ namespace osu.Game.Beatmaps.Formats { var item = split[i]; if (item.StartsWith("$") && variables.ContainsKey(item)) - item = variables[item]; + split[i] = variables[item]; } line = string.Join(",", split); From 5b5c2e47178606980ade4ba4f5a69aad96ce6d2c Mon Sep 17 00:00:00 2001 From: smoogipooo Date: Tue, 30 May 2017 20:37:30 +0900 Subject: [PATCH 7/8] Back to using val... --- osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs | 98 +++++++++---------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs index e9f6c66630..23f6c869ea 100644 --- a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs +++ b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs @@ -62,34 +62,34 @@ namespace osu.Game.Beatmaps.Formats Variables, } - private void handleGeneral(Beatmap beatmap, string key, string value) + private void handleGeneral(Beatmap beatmap, string key, string val) { var metadata = beatmap.BeatmapInfo.Metadata; switch (key) { case @"AudioFilename": - metadata.AudioFile = value; + metadata.AudioFile = val; break; case @"AudioLeadIn": - beatmap.BeatmapInfo.AudioLeadIn = int.Parse(value); + beatmap.BeatmapInfo.AudioLeadIn = int.Parse(val); break; case @"PreviewTime": - metadata.PreviewTime = int.Parse(value); + metadata.PreviewTime = int.Parse(val); break; case @"Countdown": - beatmap.BeatmapInfo.Countdown = int.Parse(value) == 1; + beatmap.BeatmapInfo.Countdown = int.Parse(val) == 1; break; case @"SampleSet": - defaultSampleBank = (LegacySampleBank)Enum.Parse(typeof(LegacySampleBank), value); + defaultSampleBank = (LegacySampleBank)Enum.Parse(typeof(LegacySampleBank), val); break; case @"SampleVolume": - defaultSampleVolume = int.Parse(value); + defaultSampleVolume = int.Parse(val); break; case @"StackLeniency": - beatmap.BeatmapInfo.StackLeniency = float.Parse(value, NumberFormatInfo.InvariantInfo); + beatmap.BeatmapInfo.StackLeniency = float.Parse(val, NumberFormatInfo.InvariantInfo); break; case @"Mode": - beatmap.BeatmapInfo.RulesetID = int.Parse(value); + beatmap.BeatmapInfo.RulesetID = int.Parse(val); switch (beatmap.BeatmapInfo.RulesetID) { @@ -108,100 +108,100 @@ namespace osu.Game.Beatmaps.Formats } break; case @"LetterboxInBreaks": - beatmap.BeatmapInfo.LetterboxInBreaks = int.Parse(value) == 1; + beatmap.BeatmapInfo.LetterboxInBreaks = int.Parse(val) == 1; break; case @"SpecialStyle": - beatmap.BeatmapInfo.SpecialStyle = int.Parse(value) == 1; + beatmap.BeatmapInfo.SpecialStyle = int.Parse(val) == 1; break; case @"WidescreenStoryboard": - beatmap.BeatmapInfo.WidescreenStoryboard = int.Parse(value) == 1; + beatmap.BeatmapInfo.WidescreenStoryboard = int.Parse(val) == 1; break; } } - private void handleEditor(Beatmap beatmap, string key, string value) + private void handleEditor(Beatmap beatmap, string key, string val) { switch (key) { case @"Bookmarks": - beatmap.BeatmapInfo.StoredBookmarks = value; + beatmap.BeatmapInfo.StoredBookmarks = val; break; case @"DistanceSpacing": - beatmap.BeatmapInfo.DistanceSpacing = double.Parse(value, NumberFormatInfo.InvariantInfo); + beatmap.BeatmapInfo.DistanceSpacing = double.Parse(val, NumberFormatInfo.InvariantInfo); break; case @"BeatDivisor": - beatmap.BeatmapInfo.BeatDivisor = int.Parse(value); + beatmap.BeatmapInfo.BeatDivisor = int.Parse(val); break; case @"GridSize": - beatmap.BeatmapInfo.GridSize = int.Parse(value); + beatmap.BeatmapInfo.GridSize = int.Parse(val); break; case @"TimelineZoom": - beatmap.BeatmapInfo.TimelineZoom = double.Parse(value, NumberFormatInfo.InvariantInfo); + beatmap.BeatmapInfo.TimelineZoom = double.Parse(val, NumberFormatInfo.InvariantInfo); break; } } - private void handleMetadata(Beatmap beatmap, string key, string value) + private void handleMetadata(Beatmap beatmap, string key, string val) { var metadata = beatmap.BeatmapInfo.Metadata; switch (key) { case @"Title": - metadata.Title = value; + metadata.Title = val; break; case @"TitleUnicode": - metadata.TitleUnicode = value; + metadata.TitleUnicode = val; break; case @"Artist": - metadata.Artist = value; + metadata.Artist = val; break; case @"ArtistUnicode": - metadata.ArtistUnicode = value; + metadata.ArtistUnicode = val; break; case @"Creator": - metadata.Author = value; + metadata.Author = val; break; case @"Version": - beatmap.BeatmapInfo.Version = value; + beatmap.BeatmapInfo.Version = val; break; case @"Source": - beatmap.BeatmapInfo.Metadata.Source = value; + beatmap.BeatmapInfo.Metadata.Source = val; break; case @"Tags": - beatmap.BeatmapInfo.Metadata.Tags = value; + beatmap.BeatmapInfo.Metadata.Tags = val; break; case @"BeatmapID": - beatmap.BeatmapInfo.OnlineBeatmapID = int.Parse(value); + beatmap.BeatmapInfo.OnlineBeatmapID = int.Parse(val); break; case @"BeatmapSetID": - beatmap.BeatmapInfo.OnlineBeatmapSetID = int.Parse(value); - metadata.OnlineBeatmapSetID = int.Parse(value); + beatmap.BeatmapInfo.OnlineBeatmapSetID = int.Parse(val); + metadata.OnlineBeatmapSetID = int.Parse(val); break; } } - private void handleDifficulty(Beatmap beatmap, string key, string value) + private void handleDifficulty(Beatmap beatmap, string key, string val) { var difficulty = beatmap.BeatmapInfo.Difficulty; switch (key) { case @"HPDrainRate": - difficulty.DrainRate = float.Parse(value, NumberFormatInfo.InvariantInfo); + difficulty.DrainRate = float.Parse(val, NumberFormatInfo.InvariantInfo); break; case @"CircleSize": - difficulty.CircleSize = float.Parse(value, NumberFormatInfo.InvariantInfo); + difficulty.CircleSize = float.Parse(val, NumberFormatInfo.InvariantInfo); break; case @"OverallDifficulty": - difficulty.OverallDifficulty = float.Parse(value, NumberFormatInfo.InvariantInfo); + difficulty.OverallDifficulty = float.Parse(val, NumberFormatInfo.InvariantInfo); break; case @"ApproachRate": - difficulty.ApproachRate = float.Parse(value, NumberFormatInfo.InvariantInfo); + difficulty.ApproachRate = float.Parse(val, NumberFormatInfo.InvariantInfo); break; case @"SliderMultiplier": - difficulty.SliderMultiplier = float.Parse(value, NumberFormatInfo.InvariantInfo); + difficulty.SliderMultiplier = float.Parse(val, NumberFormatInfo.InvariantInfo); break; case @"SliderTickRate": - difficulty.SliderTickRate = float.Parse(value, NumberFormatInfo.InvariantInfo); + difficulty.SliderTickRate = float.Parse(val, NumberFormatInfo.InvariantInfo); break; } } @@ -347,12 +347,12 @@ namespace osu.Game.Beatmaps.Formats } } - private void handleColours(Beatmap beatmap, string key, string value, ref bool hasCustomColours) + private void handleColours(Beatmap beatmap, string key, string val, ref bool hasCustomColours) { - string[] split = value.Split(','); + string[] split = val.Split(','); if (split.Length != 3) - throw new InvalidOperationException($@"Color specified in incorrect format (should be R,G,B): {value}"); + throw new InvalidOperationException($@"Color specified in incorrect format (should be R,G,B): {val}"); byte r, g, b; if (!byte.TryParse(split[0], out r) || !byte.TryParse(split[1], out g) || !byte.TryParse(split[2], out b)) @@ -416,7 +416,7 @@ namespace osu.Game.Beatmaps.Formats continue; } - string key = null, value = null; + string key = null, val = null; switch (section) { @@ -426,27 +426,27 @@ namespace osu.Game.Beatmaps.Formats case Section.Difficulty: case Section.Colours: key = line.Remove(line.IndexOf(':')).Trim(); - value = line.Substring(line.IndexOf(':') + 1).Trim(); + val = line.Substring(line.IndexOf(':') + 1).Trim(); break; case Section.Variables: key = line.Remove(line.IndexOf('=')).Trim(); - value = line.Substring(line.IndexOf('=') + 1).Trim(); + val = line.Substring(line.IndexOf('=') + 1).Trim(); break; } switch (section) { case Section.General: - handleGeneral(beatmap, key, value); + handleGeneral(beatmap, key, val); break; case Section.Editor: - handleEditor(beatmap, key, value); + handleEditor(beatmap, key, val); break; case Section.Metadata: - handleMetadata(beatmap, key, value); + handleMetadata(beatmap, key, val); break; case Section.Difficulty: - handleDifficulty(beatmap, key, value); + handleDifficulty(beatmap, key, val); break; case Section.Events: handleEvents(beatmap, line); @@ -455,7 +455,7 @@ namespace osu.Game.Beatmaps.Formats handleTimingPoints(beatmap, line); break; case Section.Colours: - handleColours(beatmap, key, value, ref hasCustomColours); + handleColours(beatmap, key, val, ref hasCustomColours); break; case Section.HitObjects: var obj = parser.Parse(line); @@ -465,7 +465,7 @@ namespace osu.Game.Beatmaps.Formats break; case Section.Variables: - variables[key] = value; + variables[key] = val; break; } } From 223c75327ffa75ca678eb6f6e656bc1b9280cb65 Mon Sep 17 00:00:00 2001 From: smoogipooo Date: Tue, 30 May 2017 20:59:46 +0900 Subject: [PATCH 8/8] Remove key/val in favor of local variables + method call. --- osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs | 145 +++++++++--------- 1 file changed, 76 insertions(+), 69 deletions(-) diff --git a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs index 23f6c869ea..4c540fa8cf 100644 --- a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs +++ b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs @@ -62,34 +62,36 @@ namespace osu.Game.Beatmaps.Formats Variables, } - private void handleGeneral(Beatmap beatmap, string key, string val) + private void handleGeneral(Beatmap beatmap, string line) { + var pair = splitKeyVal(line, ':'); + var metadata = beatmap.BeatmapInfo.Metadata; - switch (key) + switch (pair.Key) { case @"AudioFilename": - metadata.AudioFile = val; + metadata.AudioFile = pair.Value; break; case @"AudioLeadIn": - beatmap.BeatmapInfo.AudioLeadIn = int.Parse(val); + beatmap.BeatmapInfo.AudioLeadIn = int.Parse(pair.Value); break; case @"PreviewTime": - metadata.PreviewTime = int.Parse(val); + metadata.PreviewTime = int.Parse(pair.Value); break; case @"Countdown": - beatmap.BeatmapInfo.Countdown = int.Parse(val) == 1; + beatmap.BeatmapInfo.Countdown = int.Parse(pair.Value) == 1; break; case @"SampleSet": - defaultSampleBank = (LegacySampleBank)Enum.Parse(typeof(LegacySampleBank), val); + defaultSampleBank = (LegacySampleBank)Enum.Parse(typeof(LegacySampleBank), pair.Value); break; case @"SampleVolume": - defaultSampleVolume = int.Parse(val); + defaultSampleVolume = int.Parse(pair.Value); break; case @"StackLeniency": - beatmap.BeatmapInfo.StackLeniency = float.Parse(val, NumberFormatInfo.InvariantInfo); + beatmap.BeatmapInfo.StackLeniency = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo); break; case @"Mode": - beatmap.BeatmapInfo.RulesetID = int.Parse(val); + beatmap.BeatmapInfo.RulesetID = int.Parse(pair.Value); switch (beatmap.BeatmapInfo.RulesetID) { @@ -108,100 +110,106 @@ namespace osu.Game.Beatmaps.Formats } break; case @"LetterboxInBreaks": - beatmap.BeatmapInfo.LetterboxInBreaks = int.Parse(val) == 1; + beatmap.BeatmapInfo.LetterboxInBreaks = int.Parse(pair.Value) == 1; break; case @"SpecialStyle": - beatmap.BeatmapInfo.SpecialStyle = int.Parse(val) == 1; + beatmap.BeatmapInfo.SpecialStyle = int.Parse(pair.Value) == 1; break; case @"WidescreenStoryboard": - beatmap.BeatmapInfo.WidescreenStoryboard = int.Parse(val) == 1; + beatmap.BeatmapInfo.WidescreenStoryboard = int.Parse(pair.Value) == 1; break; } } - private void handleEditor(Beatmap beatmap, string key, string val) + private void handleEditor(Beatmap beatmap, string line) { - switch (key) + var pair = splitKeyVal(line, ':'); + + switch (pair.Key) { case @"Bookmarks": - beatmap.BeatmapInfo.StoredBookmarks = val; + beatmap.BeatmapInfo.StoredBookmarks = pair.Value; break; case @"DistanceSpacing": - beatmap.BeatmapInfo.DistanceSpacing = double.Parse(val, NumberFormatInfo.InvariantInfo); + beatmap.BeatmapInfo.DistanceSpacing = double.Parse(pair.Value, NumberFormatInfo.InvariantInfo); break; case @"BeatDivisor": - beatmap.BeatmapInfo.BeatDivisor = int.Parse(val); + beatmap.BeatmapInfo.BeatDivisor = int.Parse(pair.Value); break; case @"GridSize": - beatmap.BeatmapInfo.GridSize = int.Parse(val); + beatmap.BeatmapInfo.GridSize = int.Parse(pair.Value); break; case @"TimelineZoom": - beatmap.BeatmapInfo.TimelineZoom = double.Parse(val, NumberFormatInfo.InvariantInfo); + beatmap.BeatmapInfo.TimelineZoom = double.Parse(pair.Value, NumberFormatInfo.InvariantInfo); break; } } - private void handleMetadata(Beatmap beatmap, string key, string val) + private void handleMetadata(Beatmap beatmap, string line) { + var pair = splitKeyVal(line, ':'); + var metadata = beatmap.BeatmapInfo.Metadata; - switch (key) + switch (pair.Key) { case @"Title": - metadata.Title = val; + metadata.Title = pair.Value; break; case @"TitleUnicode": - metadata.TitleUnicode = val; + metadata.TitleUnicode = pair.Value; break; case @"Artist": - metadata.Artist = val; + metadata.Artist = pair.Value; break; case @"ArtistUnicode": - metadata.ArtistUnicode = val; + metadata.ArtistUnicode = pair.Value; break; case @"Creator": - metadata.Author = val; + metadata.Author = pair.Value; break; case @"Version": - beatmap.BeatmapInfo.Version = val; + beatmap.BeatmapInfo.Version = pair.Value; break; case @"Source": - beatmap.BeatmapInfo.Metadata.Source = val; + beatmap.BeatmapInfo.Metadata.Source = pair.Value; break; case @"Tags": - beatmap.BeatmapInfo.Metadata.Tags = val; + beatmap.BeatmapInfo.Metadata.Tags = pair.Value; break; case @"BeatmapID": - beatmap.BeatmapInfo.OnlineBeatmapID = int.Parse(val); + beatmap.BeatmapInfo.OnlineBeatmapID = int.Parse(pair.Value); break; case @"BeatmapSetID": - beatmap.BeatmapInfo.OnlineBeatmapSetID = int.Parse(val); - metadata.OnlineBeatmapSetID = int.Parse(val); + beatmap.BeatmapInfo.OnlineBeatmapSetID = int.Parse(pair.Value); + metadata.OnlineBeatmapSetID = int.Parse(pair.Value); break; } } - private void handleDifficulty(Beatmap beatmap, string key, string val) + private void handleDifficulty(Beatmap beatmap, string line) { + var pair = splitKeyVal(line, ':'); + var difficulty = beatmap.BeatmapInfo.Difficulty; - switch (key) + switch (pair.Key) { case @"HPDrainRate": - difficulty.DrainRate = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.DrainRate = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo); break; case @"CircleSize": - difficulty.CircleSize = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.CircleSize = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo); break; case @"OverallDifficulty": - difficulty.OverallDifficulty = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.OverallDifficulty = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo); break; case @"ApproachRate": - difficulty.ApproachRate = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.ApproachRate = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo); break; case @"SliderMultiplier": - difficulty.SliderMultiplier = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.SliderMultiplier = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo); break; case @"SliderTickRate": - difficulty.SliderTickRate = float.Parse(val, NumberFormatInfo.InvariantInfo); + difficulty.SliderTickRate = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo); break; } } @@ -347,12 +355,14 @@ namespace osu.Game.Beatmaps.Formats } } - private void handleColours(Beatmap beatmap, string key, string val, ref bool hasCustomColours) + private void handleColours(Beatmap beatmap, string line, ref bool hasCustomColours) { - string[] split = val.Split(','); + var pair = splitKeyVal(line, ':'); + + string[] split = pair.Value.Split(','); if (split.Length != 3) - throw new InvalidOperationException($@"Color specified in incorrect format (should be R,G,B): {val}"); + throw new InvalidOperationException($@"Color specified in incorrect format (should be R,G,B): {pair.Value}"); byte r, g, b; if (!byte.TryParse(split[0], out r) || !byte.TryParse(split[1], out g) || !byte.TryParse(split[2], out b)) @@ -365,7 +375,7 @@ namespace osu.Game.Beatmaps.Formats } // Note: the combo index specified in the beatmap is discarded - if (key.StartsWith(@"Combo")) + if (pair.Key.StartsWith(@"Combo")) { beatmap.ComboColors.Add(new Color4 { @@ -377,6 +387,12 @@ namespace osu.Game.Beatmaps.Formats } } + private void handleVariables(string line) + { + var pair = splitKeyVal(line, '='); + variables[pair.Key] = pair.Value; + } + protected override Beatmap ParseFile(StreamReader stream) { return new LegacyBeatmap(base.ParseFile(stream)); @@ -416,37 +432,19 @@ namespace osu.Game.Beatmaps.Formats continue; } - string key = null, val = null; - switch (section) { case Section.General: - case Section.Editor: - case Section.Metadata: - case Section.Difficulty: - case Section.Colours: - key = line.Remove(line.IndexOf(':')).Trim(); - val = line.Substring(line.IndexOf(':') + 1).Trim(); - break; - case Section.Variables: - key = line.Remove(line.IndexOf('=')).Trim(); - val = line.Substring(line.IndexOf('=') + 1).Trim(); - break; - } - - switch (section) - { - case Section.General: - handleGeneral(beatmap, key, val); + handleGeneral(beatmap, line); break; case Section.Editor: - handleEditor(beatmap, key, val); + handleEditor(beatmap, line); break; case Section.Metadata: - handleMetadata(beatmap, key, val); + handleMetadata(beatmap, line); break; case Section.Difficulty: - handleDifficulty(beatmap, key, val); + handleDifficulty(beatmap, line); break; case Section.Events: handleEvents(beatmap, line); @@ -455,7 +453,7 @@ namespace osu.Game.Beatmaps.Formats handleTimingPoints(beatmap, line); break; case Section.Colours: - handleColours(beatmap, key, val, ref hasCustomColours); + handleColours(beatmap, line, ref hasCustomColours); break; case Section.HitObjects: var obj = parser.Parse(line); @@ -465,7 +463,7 @@ namespace osu.Game.Beatmaps.Formats break; case Section.Variables: - variables[key] = val; + handleVariables(line); break; } } @@ -474,6 +472,15 @@ namespace osu.Game.Beatmaps.Formats hitObject.ApplyDefaults(beatmap.ControlPointInfo, beatmap.BeatmapInfo.Difficulty); } + private KeyValuePair splitKeyVal(string line, char separator) + { + return new KeyValuePair + ( + line.Remove(line.IndexOf(separator)).Trim(), + line.Substring(line.IndexOf(separator) + 1).Trim() + ); + } + internal enum LegacySampleBank { None = 0,