-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNBSDecoder.java
More file actions
115 lines (104 loc) · 3.73 KB
/
NBSDecoder.java
File metadata and controls
115 lines (104 loc) · 3.73 KB
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
package cn.nukkitmot.exampleplugin.nbs;
import cn.nukkit.plugin.PluginLogger;
import java.io.*;
import java.util.HashMap;
public class NBSDecoder {
private static PluginLogger logger;
public static void setLogger(PluginLogger log) {
logger = log;
}
public static Song parse(File file) {
try {
return parse(new FileInputStream(file), file);
} catch (FileNotFoundException e) {
if (logger != null) {
logger.error("NBS file not found: " + file.getName(), e);
}
}
return null;
}
private static Song parse(InputStream inputStream, File file) {
HashMap<Integer, Layer> layerHashMap = new HashMap<>();
try {
DataInputStream dis = new DataInputStream(new BufferedInputStream(inputStream));
short length = readShort(dis);
short songHeight = readShort(dis);
String title = readString(dis);
String author = readString(dis);
readString(dis);
String description = readString(dis);
float speed = readShort(dis) / 100f;
dis.readBoolean();
dis.readByte();
dis.readByte();
readInt(dis);
readInt(dis);
readInt(dis);
readInt(dis);
readInt(dis);
readString(dis);
short tick = -1;
while (true) {
short jumpTicks = readShort(dis);
if (jumpTicks == 0) {
break;
}
tick += jumpTicks;
short layer = -1;
while (true) {
short jumpLayers = readShort(dis);
if (jumpLayers == 0) {
break;
}
layer += jumpLayers;
setNote(layer, tick, dis.readByte(), dis.readByte(), layerHashMap);
}
}
for (int i = 0; i < songHeight; i++) {
Layer l = layerHashMap.get(i);
if (l != null) {
l.setName(readString(dis));
l.setVolume(dis.readByte());
}
}
return new Song(speed, layerHashMap, songHeight, length, title, author, description, file);
} catch (IOException e) {
if (logger != null) {
logger.error("Failed to parse NBS file: " + file.getName(), e);
}
}
return null;
}
private static void setNote(int layer, int tick, byte instrument, byte key, HashMap<Integer, Layer> layerHashMap) {
Layer l = layerHashMap.get(layer);
if (l == null) {
l = new Layer();
layerHashMap.put(layer, l);
}
l.setNote(tick, new Note(instrument, key));
}
private static short readShort(DataInputStream dis) throws IOException {
int byte1 = dis.readUnsignedByte();
int byte2 = dis.readUnsignedByte();
return (short) (byte1 + (byte2 << 8));
}
private static int readInt(DataInputStream dis) throws IOException {
int byte1 = dis.readUnsignedByte();
int byte2 = dis.readUnsignedByte();
int byte3 = dis.readUnsignedByte();
int byte4 = dis.readUnsignedByte();
return (byte1 + (byte2 << 8) + (byte3 << 16) + (byte4 << 24));
}
private static String readString(DataInputStream dis) throws IOException {
int length = readInt(dis);
StringBuilder sb = new StringBuilder(length);
for (; length > 0; --length) {
char c = (char) dis.readByte();
if (c == (char) 0x0D) {
c = ' ';
}
sb.append(c);
}
return sb.toString();
}
}