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