// Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using System.Collections.Generic; using System.IO; using osu.Game.Beatmaps.Legacy; using osu.Game.Storyboards; namespace osu.Game.Beatmaps.Formats { public abstract class LegacyDecoder : Decoder { public static void Register() { AddDecoder(@"osu file format v14"); AddDecoder(@"osu file format v13"); AddDecoder(@"osu file format v12"); AddDecoder(@"osu file format v11"); AddDecoder(@"osu file format v10"); AddDecoder(@"osu file format v9"); AddDecoder(@"osu file format v8"); AddDecoder(@"osu file format v7"); AddDecoder(@"osu file format v6"); AddDecoder(@"osu file format v5"); AddDecoder(@"osu file format v4"); AddDecoder(@"osu file format v3"); // TODO: differences between versions } protected int BeatmapVersion; protected readonly Dictionary Variables = new Dictionary(); public override Decoder GetStoryboardDecoder() => new LegacyStoryboardDecoder(BeatmapVersion); public override Beatmap DecodeBeatmap(StreamReader stream) => new LegacyBeatmap(base.DecodeBeatmap(stream)); protected override void ParseBeatmap(StreamReader stream, Beatmap beatmap) { throw new NotImplementedException(); } protected override void ParseStoryboard(StreamReader stream, Storyboard storyboard) { throw new NotImplementedException(); } protected void ParseContent(StreamReader stream) { Section section = Section.None; string line; while ((line = stream.ReadLine()) != null) { if (ShouldSkipLine(line)) continue; // It's already set in ParseBeatmap... why do it again? //if (line.StartsWith(@"osu file format v")) //{ // Beatmap.BeatmapInfo.BeatmapVersion = int.Parse(line.Substring(17)); // continue; //} if (line.StartsWith(@"[") && line.EndsWith(@"]")) { if (!Enum.TryParse(line.Substring(1, line.Length - 2), out section)) throw new InvalidDataException($@"Unknown osu section {line}"); continue; } ProcessSection(section, line); } } protected virtual bool ShouldSkipLine(string line) { if (string.IsNullOrWhiteSpace(line) || line.StartsWith("//")) return true; return false; } protected abstract void ProcessSection(Section section, string line); /// /// Decodes any beatmap variables present in a line into their real values. /// /// The line which may contains variables. protected void DecodeVariables(ref string line) { while (line.IndexOf('$') >= 0) { string origLine = line; string[] split = line.Split(','); for (int i = 0; i < split.Length; i++) { var item = split[i]; if (item.StartsWith("$") && Variables.ContainsKey(item)) split[i] = Variables[item]; } line = string.Join(",", split); if (line == origLine) break; } } protected enum Section { None, General, Editor, Metadata, Difficulty, Events, TimingPoints, Colours, HitObjects, Variables, } internal enum LegacySampleBank { None = 0, Normal = 1, Soft = 2, Drum = 3 } internal enum EventType { Background = 0, Video = 1, Break = 2, Colour = 3, Sprite = 4, Sample = 5, Animation = 6 } internal enum LegacyOrigins { TopLeft, Centre, CentreLeft, TopRight, BottomCentre, TopCentre, Custom, CentreRight, BottomLeft, BottomRight }; internal enum StoryLayer { Background = 0, Fail = 1, Pass = 2, Foreground = 3 } } }