]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/NexuizDemoRecorder/main/src/main/java/com/nexuiz/demorecorder/application/democutter/DemoCutter.java
Merge branch 'BuddyFriendGuy/fixVolumeReset' into 'master'
[xonotic/xonotic.git] / misc / tools / NexuizDemoRecorder / main / src / main / java / com / nexuiz / demorecorder / application / democutter / DemoCutter.java
1 package com.nexuiz.demorecorder.application.democutter;
2 import java.io.DataInputStream;
3 import java.io.DataOutputStream;
4 import java.io.EOFException;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.io.UnsupportedEncodingException;
11
12 public class DemoCutter {
13
14         private static final byte CDTRACK_SEPARATOR = 0x0A;
15
16         private DataInputStream inStream;
17         private DataOutputStream outStream;
18         private File inFile;
19         private File outFile;
20
21         /**
22          * Calls the cutDemo method with reasonable default values for the second and first fast-forward stage.
23          * @param inFile @see other cutDemo method
24          * @param outFile @see other cutDemo method
25          * @param startTime @see other cutDemo method
26          * @param endTime @see other cutDemo method
27          * @param injectAtStart @see other cutDemo method
28          * @param injectBeforeCap @see other cutDemo method
29          * @param injectAfterCap @see other cutDemo method
30          */
31         public void cutDemo(File inFile, File outFile, float startTime, float endTime, String injectAtStart, String injectBeforeCap, String injectAfterCap) {
32                 this.cutDemo(inFile, outFile, startTime, endTime, injectAtStart, injectBeforeCap, injectAfterCap, 100, 10);
33         }
34         
35         /**
36          * Cuts the demo by injecting a 2-phase fast forward command until startTime is reached, then injects the cl_capturevideo 1 command
37          * and once endTime is reached the cl_capturevideo 0 command is injected.
38          * @param inFile the original demo file
39          * @param outFile the new cut demo file
40          * @param startTime when to start capturing (use the gametime in seconds)
41          * @param endTime when to stop capturing
42          * @param injectAtStart a String that will be injected right at the beginning of the demo
43          *                                              can be anything that would make sense and can be parsed by DP's console
44          * @param injectBeforeCap a String that will be injected 5 seconds before capturing starts
45          * @param injectAfterCap a String that will be injected shortly after capturing ended
46          * @param ffwSpeedFirstStage fast-forward speed at first stage, when the startTime is still about a minute away (use high values, e.g. 100)
47          * @param ffwSpeedSecondStage fast-forward speed when coming a few seconds close to startTime, use lower values e.g. 5 or 10
48          */
49         public void cutDemo(File inFile, File outFile, float startTime, float endTime, String injectAtStart, String injectBeforeCap, String injectAfterCap, int ffwSpeedFirstStage, int ffwSpeedSecondStage) {
50                 this.inFile = inFile;
51                 this.outFile = outFile;
52                 this.prepareStreams();
53                 this.readCDTrack();
54                 injectAfterCap = this.checkInjectString(injectAfterCap);
55                 injectAtStart = this.checkInjectString(injectAtStart);
56                 injectBeforeCap = this.checkInjectString(injectBeforeCap);
57
58                 byte[] data;
59                 float svctime = -1;
60                 boolean firstLoop = true;
61                 String injectBuffer = "";
62                 int demoStarted = 0;
63                 boolean endIsReached = false;
64                 boolean finalInjectionDone = false;
65                 boolean disconnectIssued = false;
66                 float firstSvcTime = -1;
67                 float lastSvcTime = -1;
68                 
69                 try {
70                         while (true) {
71                                 DemoPacket demoPacket = new DemoPacket(this.inStream);
72                                 if (demoPacket.isEndOfFile()) {
73                                         break;
74                                 }
75                                 
76                                 if (demoPacket.isClientToServerPacket()) {
77                                         try {
78                                                 this.outStream.write(demoPacket.getOriginalLengthAsByte());
79                                                 this.outStream.write(demoPacket.getAngles());
80                                                 this.outStream.write(demoPacket.getOriginalData());
81                                         } catch (IOException e) {
82                                                 throw new DemoCutterException("Unexpected I/O Exception occurred when writing to the cut demo", e);
83                                         }
84                                         
85                                         continue;
86                                 }
87
88                                 if (demoPacket.getSvcTime() != -1) {
89                                         svctime = demoPacket.getSvcTime();
90                                 }
91
92                                 if (svctime != -1) {
93                                         if (firstSvcTime == -1) {
94                                                 firstSvcTime = svctime;
95                                         }
96                                         lastSvcTime = svctime;
97                                         
98                                         if (demoStarted < 1 && svctime > (startTime - 50)) {
99                                                 injectBuffer = "slowmo " + ffwSpeedSecondStage;
100                                                 demoStarted = 1;
101                                         }
102                                         if (demoStarted < 2 && svctime > (startTime - 5)) {
103                                                 injectBuffer = "slowmo 1;" + injectBeforeCap;
104                                                 demoStarted = 2;
105                                         }
106                                         if (demoStarted < 3 && svctime > startTime) {
107                                                 injectBuffer = "cl_capturevideo 1";
108                                                 demoStarted = 3;
109                                         }
110                                         if (!endIsReached && svctime > endTime) {
111                                                 injectBuffer = "cl_capturevideo 0";
112                                                 endIsReached = true;
113                                         }
114                                         if (endIsReached && !finalInjectionDone && svctime > (endTime + 1)) {
115                                                 injectBuffer = injectAfterCap;
116                                                 finalInjectionDone = true;
117                                         }
118                                         if (finalInjectionDone && !disconnectIssued && svctime > (endTime + 2)) {
119                                                 injectBuffer = "disconnect";
120                                                 disconnectIssued = true;
121                                         }
122                                         // ensure injectAtStart runs exactly once, before everything else
123                                         if (firstLoop) {
124                                                 injectBuffer = injectAtStart + ";slowmo " + ffwSpeedFirstStage + ";" + injectBuffer;
125                                                 firstLoop = false;
126                                         }
127                                         // add Buffer head and tail
128                                         if (injectAtStart.length() > 0) {
129                                                 injectBuffer = "\011\n" + checkInjectString(injectBuffer) + "\n\000";
130                                         }
131                                 }
132
133                                 byte[] injectBufferAsBytes = null;
134                                 try {
135                                         injectBufferAsBytes = injectBuffer.getBytes("US-ASCII");
136                                 } catch (UnsupportedEncodingException e) {
137                                         throw new DemoCutterException("Could not convert String to bytes using US-ASCII charset!", e);
138                                 }
139
140                                 data = demoPacket.getOriginalData();
141                                 if ((injectBufferAsBytes.length + data.length) < 65536) {
142                                         data = DemoCutterUtils.mergeByteArrays(injectBufferAsBytes, data);
143                                         injectBuffer = "";
144                                 }
145                                 
146                                 byte[] newLengthLittleEndian = DemoCutterUtils.convertLittleEndian(data.length);
147                                 try {
148                                         this.outStream.write(newLengthLittleEndian);
149                                         this.outStream.write(demoPacket.getAngles());
150                                         this.outStream.write(data);
151                                 } catch (IOException e) {
152                                         throw new DemoCutterException("Unexpected I/O Exception occurred when writing to the cut demo", e);
153                                 }
154
155                         }
156                         
157                         if (startTime < firstSvcTime) {
158                                 throw new DemoCutterException("Start time for the demo is " + startTime + ", but demo doesn't start before " + firstSvcTime);
159                         }
160                         if (endTime > lastSvcTime) {
161                                 throw new DemoCutterException("End time for the demo is " + endTime + ", but demo already stops at " + lastSvcTime);
162                         }
163                 } catch (DemoCutterException e) {
164                         throw e;
165                 } catch (Throwable e) {
166                         throw new DemoCutterException("Internal error in demo cutter sub-route (invalid demo file?)", e);
167                 } finally {
168                         try {
169                                 this.outStream.close();
170                                 this.inStream.close();
171                         } catch (IOException e) {}
172                 }
173         }
174
175         
176
177         /**
178          * Seeks forward in the inStream until CDTRACK_SEPARATOR byte was reached.
179          * All the content is copied to the outStream.
180          */
181         private void readCDTrack() {
182                 byte lastByte;
183                 try {
184                         while ((lastByte = inStream.readByte()) != CDTRACK_SEPARATOR) {
185                                 this.outStream.write(lastByte);
186                         }
187                         this.outStream.write(CDTRACK_SEPARATOR);
188                 } catch (EOFException e) {
189                         throw new DemoCutterException("Unexpected EOF occurred when reading CD track of demo " + inFile.getPath(), e);
190                 }
191                 catch (IOException e) {
192                         throw new DemoCutterException("Unexpected I/O Exception occurred when reading CD track of demo " + inFile.getPath(), e);
193                 }
194         }
195
196         private void prepareStreams() {
197                 try {
198                         this.inStream = new DataInputStream(new FileInputStream(this.inFile));
199                 } catch (FileNotFoundException e) {
200                         throw new DemoCutterException("Could not open demo file " + inFile.getPath(), e);
201                 }
202                 
203                 try {
204                         this.outStream = new DataOutputStream(new FileOutputStream(this.outFile));
205                 } catch (FileNotFoundException e) {
206                         throw new DemoCutterException("Could not open demo file " + outFile.getPath(), e);
207                 }
208         }
209         
210         private String checkInjectString(String injectionString) {
211                 while (injectionString.endsWith(";") || injectionString.endsWith("\n")) {
212                         injectionString = injectionString.substring(0, injectionString.length()-1);
213                 }
214                 return injectionString;
215         }
216 }