Remove key/val in favor of local variables + method call.

This commit is contained in:
smoogipooo
2017-05-30 20:59:46 +09:00
parent 5b5c2e4717
commit 223c75327f

View File

@ -62,34 +62,36 @@ namespace osu.Game.Beatmaps.Formats
Variables, 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; var metadata = beatmap.BeatmapInfo.Metadata;
switch (key) switch (pair.Key)
{ {
case @"AudioFilename": case @"AudioFilename":
metadata.AudioFile = val; metadata.AudioFile = pair.Value;
break; break;
case @"AudioLeadIn": case @"AudioLeadIn":
beatmap.BeatmapInfo.AudioLeadIn = int.Parse(val); beatmap.BeatmapInfo.AudioLeadIn = int.Parse(pair.Value);
break; break;
case @"PreviewTime": case @"PreviewTime":
metadata.PreviewTime = int.Parse(val); metadata.PreviewTime = int.Parse(pair.Value);
break; break;
case @"Countdown": case @"Countdown":
beatmap.BeatmapInfo.Countdown = int.Parse(val) == 1; beatmap.BeatmapInfo.Countdown = int.Parse(pair.Value) == 1;
break; break;
case @"SampleSet": case @"SampleSet":
defaultSampleBank = (LegacySampleBank)Enum.Parse(typeof(LegacySampleBank), val); defaultSampleBank = (LegacySampleBank)Enum.Parse(typeof(LegacySampleBank), pair.Value);
break; break;
case @"SampleVolume": case @"SampleVolume":
defaultSampleVolume = int.Parse(val); defaultSampleVolume = int.Parse(pair.Value);
break; break;
case @"StackLeniency": case @"StackLeniency":
beatmap.BeatmapInfo.StackLeniency = float.Parse(val, NumberFormatInfo.InvariantInfo); beatmap.BeatmapInfo.StackLeniency = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break; break;
case @"Mode": case @"Mode":
beatmap.BeatmapInfo.RulesetID = int.Parse(val); beatmap.BeatmapInfo.RulesetID = int.Parse(pair.Value);
switch (beatmap.BeatmapInfo.RulesetID) switch (beatmap.BeatmapInfo.RulesetID)
{ {
@ -108,100 +110,106 @@ namespace osu.Game.Beatmaps.Formats
} }
break; break;
case @"LetterboxInBreaks": case @"LetterboxInBreaks":
beatmap.BeatmapInfo.LetterboxInBreaks = int.Parse(val) == 1; beatmap.BeatmapInfo.LetterboxInBreaks = int.Parse(pair.Value) == 1;
break; break;
case @"SpecialStyle": case @"SpecialStyle":
beatmap.BeatmapInfo.SpecialStyle = int.Parse(val) == 1; beatmap.BeatmapInfo.SpecialStyle = int.Parse(pair.Value) == 1;
break; break;
case @"WidescreenStoryboard": case @"WidescreenStoryboard":
beatmap.BeatmapInfo.WidescreenStoryboard = int.Parse(val) == 1; beatmap.BeatmapInfo.WidescreenStoryboard = int.Parse(pair.Value) == 1;
break; 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": case @"Bookmarks":
beatmap.BeatmapInfo.StoredBookmarks = val; beatmap.BeatmapInfo.StoredBookmarks = pair.Value;
break; break;
case @"DistanceSpacing": case @"DistanceSpacing":
beatmap.BeatmapInfo.DistanceSpacing = double.Parse(val, NumberFormatInfo.InvariantInfo); beatmap.BeatmapInfo.DistanceSpacing = double.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break; break;
case @"BeatDivisor": case @"BeatDivisor":
beatmap.BeatmapInfo.BeatDivisor = int.Parse(val); beatmap.BeatmapInfo.BeatDivisor = int.Parse(pair.Value);
break; break;
case @"GridSize": case @"GridSize":
beatmap.BeatmapInfo.GridSize = int.Parse(val); beatmap.BeatmapInfo.GridSize = int.Parse(pair.Value);
break; break;
case @"TimelineZoom": case @"TimelineZoom":
beatmap.BeatmapInfo.TimelineZoom = double.Parse(val, NumberFormatInfo.InvariantInfo); beatmap.BeatmapInfo.TimelineZoom = double.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break; 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; var metadata = beatmap.BeatmapInfo.Metadata;
switch (key) switch (pair.Key)
{ {
case @"Title": case @"Title":
metadata.Title = val; metadata.Title = pair.Value;
break; break;
case @"TitleUnicode": case @"TitleUnicode":
metadata.TitleUnicode = val; metadata.TitleUnicode = pair.Value;
break; break;
case @"Artist": case @"Artist":
metadata.Artist = val; metadata.Artist = pair.Value;
break; break;
case @"ArtistUnicode": case @"ArtistUnicode":
metadata.ArtistUnicode = val; metadata.ArtistUnicode = pair.Value;
break; break;
case @"Creator": case @"Creator":
metadata.Author = val; metadata.Author = pair.Value;
break; break;
case @"Version": case @"Version":
beatmap.BeatmapInfo.Version = val; beatmap.BeatmapInfo.Version = pair.Value;
break; break;
case @"Source": case @"Source":
beatmap.BeatmapInfo.Metadata.Source = val; beatmap.BeatmapInfo.Metadata.Source = pair.Value;
break; break;
case @"Tags": case @"Tags":
beatmap.BeatmapInfo.Metadata.Tags = val; beatmap.BeatmapInfo.Metadata.Tags = pair.Value;
break; break;
case @"BeatmapID": case @"BeatmapID":
beatmap.BeatmapInfo.OnlineBeatmapID = int.Parse(val); beatmap.BeatmapInfo.OnlineBeatmapID = int.Parse(pair.Value);
break; break;
case @"BeatmapSetID": case @"BeatmapSetID":
beatmap.BeatmapInfo.OnlineBeatmapSetID = int.Parse(val); beatmap.BeatmapInfo.OnlineBeatmapSetID = int.Parse(pair.Value);
metadata.OnlineBeatmapSetID = int.Parse(val); metadata.OnlineBeatmapSetID = int.Parse(pair.Value);
break; 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; var difficulty = beatmap.BeatmapInfo.Difficulty;
switch (key) switch (pair.Key)
{ {
case @"HPDrainRate": case @"HPDrainRate":
difficulty.DrainRate = float.Parse(val, NumberFormatInfo.InvariantInfo); difficulty.DrainRate = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break; break;
case @"CircleSize": case @"CircleSize":
difficulty.CircleSize = float.Parse(val, NumberFormatInfo.InvariantInfo); difficulty.CircleSize = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break; break;
case @"OverallDifficulty": case @"OverallDifficulty":
difficulty.OverallDifficulty = float.Parse(val, NumberFormatInfo.InvariantInfo); difficulty.OverallDifficulty = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break; break;
case @"ApproachRate": case @"ApproachRate":
difficulty.ApproachRate = float.Parse(val, NumberFormatInfo.InvariantInfo); difficulty.ApproachRate = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break; break;
case @"SliderMultiplier": case @"SliderMultiplier":
difficulty.SliderMultiplier = float.Parse(val, NumberFormatInfo.InvariantInfo); difficulty.SliderMultiplier = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break; break;
case @"SliderTickRate": case @"SliderTickRate":
difficulty.SliderTickRate = float.Parse(val, NumberFormatInfo.InvariantInfo); difficulty.SliderTickRate = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break; 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) 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; byte r, g, b;
if (!byte.TryParse(split[0], out r) || !byte.TryParse(split[1], out g) || !byte.TryParse(split[2], out 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 // Note: the combo index specified in the beatmap is discarded
if (key.StartsWith(@"Combo")) if (pair.Key.StartsWith(@"Combo"))
{ {
beatmap.ComboColors.Add(new Color4 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) protected override Beatmap ParseFile(StreamReader stream)
{ {
return new LegacyBeatmap(base.ParseFile(stream)); return new LegacyBeatmap(base.ParseFile(stream));
@ -416,37 +432,19 @@ namespace osu.Game.Beatmaps.Formats
continue; continue;
} }
string key = null, val = null;
switch (section) switch (section)
{ {
case Section.General: case Section.General:
case Section.Editor: handleGeneral(beatmap, line);
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);
break; break;
case Section.Editor: case Section.Editor:
handleEditor(beatmap, key, val); handleEditor(beatmap, line);
break; break;
case Section.Metadata: case Section.Metadata:
handleMetadata(beatmap, key, val); handleMetadata(beatmap, line);
break; break;
case Section.Difficulty: case Section.Difficulty:
handleDifficulty(beatmap, key, val); handleDifficulty(beatmap, line);
break; break;
case Section.Events: case Section.Events:
handleEvents(beatmap, line); handleEvents(beatmap, line);
@ -455,7 +453,7 @@ namespace osu.Game.Beatmaps.Formats
handleTimingPoints(beatmap, line); handleTimingPoints(beatmap, line);
break; break;
case Section.Colours: case Section.Colours:
handleColours(beatmap, key, val, ref hasCustomColours); handleColours(beatmap, line, ref hasCustomColours);
break; break;
case Section.HitObjects: case Section.HitObjects:
var obj = parser.Parse(line); var obj = parser.Parse(line);
@ -465,7 +463,7 @@ namespace osu.Game.Beatmaps.Formats
break; break;
case Section.Variables: case Section.Variables:
variables[key] = val; handleVariables(line);
break; break;
} }
} }
@ -474,6 +472,15 @@ namespace osu.Game.Beatmaps.Formats
hitObject.ApplyDefaults(beatmap.ControlPointInfo, beatmap.BeatmapInfo.Difficulty); hitObject.ApplyDefaults(beatmap.ControlPointInfo, beatmap.BeatmapInfo.Difficulty);
} }
private KeyValuePair<string, string> splitKeyVal(string line, char separator)
{
return new KeyValuePair<string, string>
(
line.Remove(line.IndexOf(separator)).Trim(),
line.Substring(line.IndexOf(separator) + 1).Trim()
);
}
internal enum LegacySampleBank internal enum LegacySampleBank
{ {
None = 0, None = 0,