/
OJNEncoder.cs
165 lines (140 loc) · 6.09 KB
/
OJNEncoder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;
using Cgen;
using Cgen.IO;
using CXO2;
using CXO2.Charting;
using CXO2.Charting.O2Jam;
namespace CXO2.Processors.O2Jam
{
public static class OJNEncoder
{
public static bool Check(string extension)
{
return extension == "ojn" || extension == "new.ojn";
}
public static byte[] Encode(OJN ojn)
{
byte[] header = EncodeHeader(ojn);
using (var stream = new BufferStream(header))
{
foreach (var difficulty in Enum.GetValues(typeof(OJN.Difficulty)) as OJN.Difficulty[])
{
if (difficulty == OJN.Difficulty.MX)
continue;
var events = new List<Event>();
long offset = ojn.BlockOffset[difficulty];
int count = ojn.BlockCount[difficulty];
stream.Seek(offset, SeekOrigin.Begin);
var blocks = ojn.Events[difficulty].GroupBy((ev) => new { ev.Measure, ev.Tempo, ev.LaneIndex });
foreach (var block in blocks)
{
stream.Write(block.Key.Measure);
stream.Write((short)block.Key.LaneIndex);
stream.Write((short)block.Key.Tempo);
for (int i = 0; i < block.Key.Tempo; i++)
{
var ev = block.FirstOrDefault((e) => e.Cell == i);
if (ev != null)
{
if (ev.Channel == Event.ChannelType.Measurement ||
ev.Channel == Event.ChannelType.BPM)
{
var time = ev as Event.Time;
stream.Write(time.Value);
}
else
{
var sound = ev as Event.Sound;
int id = sound.Id;
int vol = sound.Volume == 100 ? 0 : (int)((sound.Volume / 100f) * 16f);
int pan = (int)sound.Pan;
id = (id >= 1000 ? id - 1000 : id) + 1;
pan = pan == 0 ? pan : ((pan / 100) * 8) + 8;
stream.Write((short)id);
stream.Write((byte)((vol << 8) | pan & 0x00FF));
stream.Write((byte)((int)sound.Signature));
}
}
else
{
stream.Write(0);
}
}
}
}
stream.Seek(ojn.CoverArtOffset, SeekOrigin.Begin);
stream.Write(ojn.CoverArt);
stream.Write(ojn.Thumbnail);
return stream.ToArray();
}
}
public static byte[] EncodeHeader(OJN ojn)
{
using (var stream = new BufferStream())
{
stream.Seek(0, SeekOrigin.Begin);
stream.Write(ojn.Id);
stream.Write(ojn.Sign.PadNull(4));
stream.Write(ojn.EncodingVersion);
stream.Write((int)ojn.Genre);
stream.Write(ojn.BPM);
foreach (var diff in Enum.GetValues(typeof(OJN.Difficulty)) as OJN.Difficulty[])
{
if (diff != OJN.Difficulty.MX)
stream.Write(ojn.Level[diff]);
else
stream.Write((short)0);
}
foreach (var diff in Enum.GetValues(typeof(OJN.Difficulty)) as OJN.Difficulty[])
{
if (diff != OJN.Difficulty.MX)
stream.Write(ojn.EventCount[diff]);
}
foreach (var diff in Enum.GetValues(typeof(OJN.Difficulty)) as OJN.Difficulty[])
{
if (diff != OJN.Difficulty.MX)
stream.Write(ojn.NoteCount[diff]);
}
foreach (var diff in Enum.GetValues(typeof(OJN.Difficulty)) as OJN.Difficulty[])
{
if (diff != OJN.Difficulty.MX)
stream.Write(ojn.MeasureCount[diff]);
}
foreach (var diff in Enum.GetValues(typeof(OJN.Difficulty)) as OJN.Difficulty[])
{
if (diff != OJN.Difficulty.MX)
stream.Write(ojn.BlockCount[diff]);
}
stream.Write(ojn.OldEncodingVersion);
stream.Write(ojn.OldId);
stream.Write(ojn.OldGenre.PadNull(20));
stream.Write(ojn.ThumbnailSize);
stream.Write(ojn.Version);
stream.Write(ojn.Title.PadNull(64));
stream.Write(ojn.Artist.PadNull(32));
stream.Write(ojn.Pattern.PadNull(32));
stream.Write(ojn.OJMFileName.PadNull(32));
stream.Write(ojn.CoverArtSize);
foreach (var diff in Enum.GetValues(typeof(OJN.Difficulty)) as OJN.Difficulty[])
{
if (diff != OJN.Difficulty.MX)
stream.Write(ojn.Duration[diff]);
}
foreach (var diff in Enum.GetValues(typeof(OJN.Difficulty)) as OJN.Difficulty[])
{
if (diff != OJN.Difficulty.MX)
stream.Write(ojn.BlockOffset[diff]);
}
stream.Write(ojn.CoverArtOffset);
//stream.Seek(ojn.CoverArtOffset, SeekOrigin.Begin);
//stream.Write(ojn.CoverArt);
//stream.Write(ojn.Thumbnail);
return stream.ToArray();
}
}
}
}