]> de.git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/NexuizDemoRecorder/main/src/main/java/com/nexuiz/demorecorder/application/democutter/DemoPacket.java
fix lots of CRLFs
[xonotic/xonotic.git] / misc / tools / NexuizDemoRecorder / main / src / main / java / com / nexuiz / demorecorder / application / democutter / DemoPacket.java
1 package com.nexuiz.demorecorder.application.democutter;
2 import java.io.DataInputStream;
3 import java.io.EOFException;
4 import java.io.IOException;
5 import java.nio.ByteBuffer;
6
7
8 public class DemoPacket {
9         
10         private static final int DEMOMSG_CLIENT_TO_SERVER = 0x80000000;
11         
12         private DataInputStream inStream = null;
13         private boolean isEndOfFile = false;
14         private byte[] buffer = new byte[4]; //contains packet length
15         private byte[] angles = new byte[12];
16         private byte[] data;
17         private int packetLength;
18         private boolean isClientToServer = false;
19         private float svcTime = -1;
20
21         public DemoPacket(DataInputStream inStream) {
22                 this.inStream = inStream;
23                 
24                 try {
25                         inStream.readFully(buffer);
26                 } catch (EOFException e) {
27                         this.isEndOfFile = true;
28                         return;
29                 } catch (IOException e) {
30                         throw new DemoCutterException("Unexpected I/O Exception occurred when processing demo");
31                 }
32                 
33                 packetLength = DemoCutterUtils.convertLittleEndian(buffer);
34                 if ((packetLength & DEMOMSG_CLIENT_TO_SERVER) != 0) {
35                         packetLength = packetLength & ~DEMOMSG_CLIENT_TO_SERVER;
36
37                         this.isClientToServer = true;
38                         this.readAnglesAndData();
39                         return;
40                 }
41                 
42                 this.readAnglesAndData();
43                 
44                 // extract svc_time
45                 this.readSvcTime();
46                 
47         }
48         
49         public boolean isEndOfFile() {
50                 return this.isEndOfFile;
51         }
52         
53         public boolean isClientToServerPacket() {
54                 return this.isClientToServer;
55         }
56         
57         public byte[] getOriginalLengthAsByte() {
58                 return this.buffer;
59         }
60         
61         public byte[] getAngles() {
62                 return this.angles;
63         }
64         
65         public byte[] getOriginalData() {
66                 return this.data;
67         }
68         
69         public float getSvcTime() {
70                 return this.svcTime;
71         }
72         
73         private void readAnglesAndData() {
74                 // read angles
75                 try {
76                         inStream.readFully(angles);
77                 } catch (EOFException e) {
78                         throw new DemoCutterException("Invalid Demo Packet");
79                 } catch (IOException e) {
80                         throw new DemoCutterException("Unexpected I/O Exception occurred when processing demo");
81                 }
82
83                 // read data
84                 data = new byte[packetLength];
85                 try {
86                         inStream.readFully(data);
87                 } catch (EOFException e) {
88                         throw new DemoCutterException("Invalid Demo Packet");
89                 } catch (IOException e) {
90                         throw new DemoCutterException("Unexpected I/O Exception occurred when processing demo");
91                 }
92         }
93         
94         private void readSvcTime() {
95                 if (data[0] == 0x07) {
96                         ByteBuffer bb = ByteBuffer.allocate(4);
97                         bb.put(data, 1, 4);
98                         byte[] array = bb.array();
99                         this.svcTime = DemoCutterUtils.byteArrayToFloat(array);
100                 }
101         }
102 }