1+ using Server . Emulator . Tools ;
2+ using System ;
3+ using System . Collections . Generic ;
4+ using System . Linq ;
5+
6+ namespace Server . OsuManiaLoader ;
7+
8+
9+ public class Difficulty
10+ {
11+ public string HPDrainRate ;
12+ public string CircleSize ;
13+ public string OverallDifficulty ;
14+ public string ApproachRate ;
15+ public string SliderMultiplier ;
16+ public string SliderTickRate ;
17+ }
18+
19+ public class Editor
20+ {
21+ public string DistanceSpacing ;
22+ public string BeatDivisor ;
23+ public string GridSize ;
24+ public string TimelineZoom ;
25+ public string Bookmarks ;
26+ }
27+
28+ public class General
29+ {
30+ public string AudioFilename ;
31+ public string AudioLeadIn ;
32+ public string PreviewTime ;
33+ public string Countdown ;
34+ public string SampleSet ;
35+ public string StackLeniency ;
36+ public string Mode ;
37+ public string LetterboxInBreaks ;
38+ public string SpecialStyle ;
39+ public string WidescreenStoryboard ;
40+ }
41+
42+ public class Metadata
43+ {
44+ public string Title ;
45+ public string TitleUnicode ;
46+ public string Artist ;
47+ public string ArtistUnicode ;
48+ public string Creator ;
49+ public string Version ;
50+ public string Source ;
51+ public string Tags ;
52+ public string BeatmapID ;
53+ public string BeatmapSetID ;
54+ }
55+
56+ public class Beatmap
57+ {
58+ public int version ;
59+ public General General ;
60+ public Editor Editor ;
61+ public Metadata Metadata ;
62+ public Difficulty Difficulty ;
63+ public string [ ] Events ;
64+ public string [ ] TimingPoints ;
65+ public string [ ] HitObjects ;
66+ }
67+
68+
69+ public static class Base36
70+ {
71+ public static char [ ] alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" . ToArray ( ) ;
72+ public static string Encode ( int number )
73+ {
74+ var output = new List < char > ( ) ;
75+
76+ if ( 0 <= number && number < alphabet . Length )
77+ {
78+ output . Insert ( 0 , alphabet [ number ] ) ;
79+ }
80+ else
81+ {
82+ while ( number != 0 )
83+ {
84+ number = Math . DivRem ( number , alphabet . Length , out int i ) ;
85+ output . Insert ( 0 , alphabet [ i ] ) ;
86+ }
87+ }
88+
89+ return string . Join ( "" , output . Select ( ch => ch . ToString ( ) ) . ToArray ( ) ) . PadLeft ( 2 , '0' ) ;
90+ }
91+ }
92+
93+ public class BeatmapParser
94+ {
95+ public static Beatmap Parse ( string beatmapContent )
96+ {
97+ var lines = beatmapContent . Trim ( ) . Split ( '\n ' )
98+ . Select ( line => line . Trim ( ) )
99+ . Where ( line => line . Length > 0 && ! line . StartsWith ( "//" ) )
100+ . ToList ( ) ;
101+
102+ var version = Convert . ToInt32 ( lines . Pop ( 0 ) . Split ( 'v' ) . Last ( ) ) ;
103+ var sections = new Dictionary < string , List < string > > ( ) ;
104+ string curSection = null ;
105+
106+ foreach ( var line in lines )
107+ {
108+ if ( line . StartsWith ( "[" ) && line . EndsWith ( "]" ) )
109+ {
110+ curSection = line . Substring ( 1 , line . Length - 2 ) ;
111+ sections [ curSection ] = new List < string > ( ) ;
112+ }
113+ else
114+ {
115+ sections [ curSection ! ] . Add ( line . Trim ( ) ) ;
116+ }
117+ }
118+
119+ var parsedSections = new Dictionary < string , Dictionary < string , string > > ( ) ;
120+
121+ foreach ( var section in sections . Keys )
122+ {
123+ if ( new string [ ] { "HitObjects" , "TimingPoints" , "Events" } . Contains ( section ) ) continue ;
124+
125+ parsedSections [ section ] = sections [ section ] . ToDictionary ( line => line . Split ( ':' ) [ 0 ] . Trim ( ) , line => line . Split ( ':' ) [ 1 ] . Trim ( ) ) ;
126+ }
127+
128+ var beatmap = LitJson . JsonMapper . ToObject < Beatmap > ( LitJson . JsonMapper . ToJson ( parsedSections ) ) ;
129+ beatmap . version = version ;
130+ beatmap . TimingPoints = sections [ "TimingPoints" ] . ToArray ( ) ;
131+ beatmap . HitObjects = sections [ "HitObjects" ] . ToArray ( ) ;
132+ beatmap . Events = sections [ "Events" ] . ToArray ( ) ;
133+ return beatmap ;
134+ }
135+ }
0 commit comments