aboutsummaryrefslogtreecommitdiff
path: root/src/org/happysanta/gd/Levels/Reader.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/happysanta/gd/Levels/Reader.java')
-rw-r--r--src/org/happysanta/gd/Levels/Reader.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/org/happysanta/gd/Levels/Reader.java b/src/org/happysanta/gd/Levels/Reader.java
new file mode 100644
index 0000000..f0ce9a6
--- /dev/null
+++ b/src/org/happysanta/gd/Levels/Reader.java
@@ -0,0 +1,52 @@
+package org.happysanta.gd.Levels;
+
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.happysanta.gd.Helpers.decodeCp1251;
+
+public class Reader {
+
+ private static final int MAX_VALID_TRACKS = 16384;
+
+ public static LevelHeader readHeader(InputStream in) throws IOException {
+ LevelHeader header = new LevelHeader();
+ DataInputStream din = new DataInputStream(in);
+ byte buf[] = new byte[40];
+ String tmp;
+ for (int i = 0; i < 3; i++) {
+ int tCount = din.readInt();
+ if (tCount > MAX_VALID_TRACKS) {
+ din.close();
+ throw new IOException("Level file is not valid");
+ }
+ header.setCount(i, tCount);
+
+ label0:
+ for (int j = 0; j < header.getCount(i); j++) {
+ int trackPointer = din.readInt();
+ header.setPointer(i, j, trackPointer);
+ int nameLen = 0;
+ do {
+ if (nameLen >= 40)
+ continue label0;
+
+ buf[nameLen] = din.readByte();
+ if (buf[nameLen] == 0) {
+ // tmp = (new String(buf, 0, nameLen, "CP-1251"));
+ tmp = decodeCp1251(buf);
+ header.setName(i, j, tmp.replace('_', ' '));
+ continue label0;
+ }
+ nameLen++;
+ } while (true);
+ }
+
+ }
+ din.close();
+
+ return header;
+ }
+
+}