Got all the temporary folders deleting on exit
[disguisecraft:dc-injector.git] / src / pgDev / DisguiseCraft / Injector / Injector.java
1 package pgDev.DisguiseCraft.Injector;\r
2 \r
3 import java.io.BufferedInputStream;\r
4 import java.io.BufferedOutputStream;\r
5 import java.io.File;\r
6 import java.io.FileInputStream;\r
7 import java.io.FileOutputStream;\r
8 import java.io.IOException;\r
9 import java.net.HttpURLConnection;\r
10 import java.net.MalformedURLException;\r
11 import java.net.URL;\r
12 import java.nio.channels.FileChannel;\r
13 import java.util.LinkedList;\r
14 import java.util.jar.JarEntry;\r
15 import java.util.jar.JarOutputStream;\r
16 import java.util.zip.ZipEntry;\r
17 import java.util.zip.ZipInputStream;\r
18 \r
19 import org.apache.commons.io.FilenameUtils;\r
20 \r
21 public class Injector {\r
22         static String changeSource = "http://dev.pinoygamers.info/cbdc/changes.zip";\r
23         static String craftName = "craftbukkit-";\r
24         \r
25         static int BUFFER = 1024;\r
26         \r
27         public static void main(String[] args) {\r
28                 // Remote\r
29                 System.out.println("Downloading archive of changes...");\r
30                 try {\r
31                         saveUrl("changes.zip", changeSource);\r
32                         (new File("changes.zip")).deleteOnExit();\r
33                         System.out.println("Successfully downloaded.");\r
34                 } catch (Exception e) {\r
35                         System.out.println("Could not obtain changes from remote server!");\r
36                         e.printStackTrace();\r
37                         return;\r
38                 }\r
39                 \r
40                 // Local\r
41                 File[] files = (new File(".")).listFiles();\r
42                 \r
43                 File craftBukkit = null;\r
44                 boolean alreadyCreated = false;\r
45                 \r
46                 for (File file : files) {\r
47                         if (file.isFile()) {\r
48                                 String fileName = file.getName();\r
49                                 if (fileName.startsWith("craft")) { // A CB file\r
50                                         if (fileName.endsWith("-DC.jar")) { // A CD-DC file\r
51                                                 alreadyCreated = true;\r
52                                         } else { // A normal CD file\r
53                                                 craftBukkit = file;\r
54                                                 craftName = removeExtension(fileName);\r
55                                                 System.out.println("CraftBukkit jar to edit has been located.");\r
56                                         }\r
57                                 }\r
58                         }\r
59                 }\r
60                 \r
61                 if (alreadyCreated) {\r
62                         System.out.println("There is already a custom CraftBukkit-DC file in this directory. Aborting!");\r
63                 } else {\r
64                         if (craftBukkit == null) {\r
65                                 System.out.println("The CraftBukkit jar to edit could not be found. Aborting!");\r
66                         } else {\r
67                                 // Decompress the files\r
68                                 System.out.println("Decompressing CraftBukkit...");\r
69                                 (new File("./" + craftName + "/")).deleteOnExit();\r
70                                 if (!decompress(craftBukkit.getName(), true)) {\r
71                                         System.out.println("Could not decompress CraftBukkit. Aborting!");\r
72                                 } else {\r
73                                         System.out.println("Decompress complete.");\r
74                                         \r
75                                         System.out.println("Decompressing archive of changes...");\r
76                                         (new File("./changes/")).deleteOnExit();\r
77                                         if (!decompress("./changes.zip", true)) {\r
78                                                 System.out.println("Could not decompress changes. Aborting!");\r
79                                         } else {\r
80                                                 \r
81                                                 System.out.println("Decompress complete.");\r
82                                                 \r
83                                                 // Apply the changes\r
84                                                 LinkedList<File> changeFiles = getFiles(new File("./changes"));\r
85                                                 System.out.println("Copying over changes...");\r
86                                                 for (File change : changeFiles) {\r
87                                                         File file = toCB(change);\r
88                                                         if (file.exists()) {\r
89                                                                 file.delete();\r
90                                                         }\r
91                                                         try {\r
92                                                                 copyFile(change.getAbsoluteFile(), file.getAbsoluteFile());\r
93                                                                 System.out.println("Successfully copied: " + change.getName());\r
94                                                         } catch (IOException e) {\r
95                                                                 e.printStackTrace();\r
96                                                                 System.out.println("Could not copy over change: " + file.getName());\r
97                                                         }\r
98                                                         file.deleteOnExit();\r
99                                                 }\r
100                                                 System.out.println("Changes copied.");\r
101                                                 \r
102                                                 // Recompress\r
103                                                 System.out.println("Compressing new jar file...");\r
104                                                 if (!compressJar(craftName + "-DC.jar", new File("./" + craftName))) {\r
105                                                         System.out.println("Compression failed. Aborting!");\r
106                                                 } else {\r
107                                                         System.out.println("Compression complete.");\r
108                                                         System.out.println("Enjoy your newly modifed CraftBukkit jar file!");\r
109                                                 }\r
110                                         }\r
111                                 }\r
112                         }\r
113                 }\r
114                 System.out.println("Deleting files for exit...");\r
115         }\r
116         \r
117         public static void saveUrl(String filename, String urlString) throws MalformedURLException, IOException {\r
118                 HttpURLConnection httpCon;\r
119                 BufferedInputStream in = null;\r
120                 FileOutputStream fout = null;\r
121                 try {\r
122                         httpCon = (HttpURLConnection) (new URL (urlString)).openConnection();\r
123                         httpCon.addRequestProperty("User-Agent", "Mozilla/4.76"); // Needed to access some servers\r
124                         in = new BufferedInputStream(httpCon.getInputStream());\r
125                         fout = new FileOutputStream(filename);\r
126                         byte data[] = new byte[1024];\r
127                         int count;\r
128                         while ((count = in.read(data, 0, 1024)) != -1) {\r
129                                 fout.write(data, 0, count);\r
130                         }\r
131                 } finally {\r
132                         if (in != null) {\r
133                                 in.close();\r
134                         }\r
135                         if (fout != null) {\r
136                                 fout.close();\r
137                         }\r
138                 }\r
139         }\r
140         \r
141         public static boolean decompress(String zip, boolean temp) {\r
142                 try {\r
143                         BufferedOutputStream dest = null;\r
144                         FileInputStream fis = new FileInputStream(zip);\r
145                         ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));\r
146                         ZipEntry entry;\r
147                         \r
148                         File dir = new File(removeExtension(zip));\r
149                         if (!dir.exists()) {\r
150                                 dir.mkdir();\r
151                         }\r
152                         \r
153                         while((entry = zis.getNextEntry()) != null) {\r
154                                 if (entry.isDirectory()) {\r
155                                         File folder = new File(removeExtension(zip) + "/" + entry.getName());\r
156                                         if (!folder.exists()) {\r
157                                                 folder.mkdir();\r
158                                         }\r
159                                         \r
160                                         if (temp) {\r
161                                                 folder.deleteOnExit();\r
162                                         }\r
163                                 } else {\r
164                                         int count;\r
165                                         byte data[] = new byte[BUFFER];\r
166                                         String fileLoc = dir.getName() + "/" + entry.getName();\r
167                                         FileOutputStream fos = new FileOutputStream(fileLoc);\r
168                                         dest = new BufferedOutputStream(fos, BUFFER);\r
169                                         while ((count = zis.read(data, 0, BUFFER)) != -1) {\r
170                                                 dest.write(data, 0, count);\r
171                                         }\r
172                                         dest.flush();\r
173                                         dest.close();\r
174                                         \r
175                                         if (temp) {\r
176                                                 (new File(fileLoc)).deleteOnExit();\r
177                                         }\r
178                                 }\r
179                         }\r
180                         zis.close();\r
181                         return true;\r
182                 } catch (Exception e) {\r
183                         e.printStackTrace();\r
184                         return false;\r
185                 }\r
186         }\r
187         \r
188         public static boolean compressJar(String outFile, File dir) {\r
189                 try {\r
190                         BufferedInputStream origin = null;\r
191                         FileOutputStream dest = new FileOutputStream(outFile);\r
192                         JarOutputStream out = new JarOutputStream(new BufferedOutputStream(dest));\r
193                         byte data[] = new byte[BUFFER];\r
194                         String removePath = "./" + craftName + "/";\r
195                         for (File file : getFiles(dir)) {\r
196                                 FileInputStream fi = new FileInputStream(file);\r
197                                 origin = new BufferedInputStream(fi, BUFFER);\r
198                                 \r
199                                 // Entry positioning\r
200                                 String entryPath = file.getPath().substring(removePath.length());\r
201                                 if (file.isDirectory()) {\r
202                                         entryPath += "/";\r
203                                 }\r
204                                 \r
205                                 JarEntry entry = new JarEntry(entryPath.replace("\\", "/"));\r
206                                 out.putNextEntry(entry);\r
207                                 int count;\r
208                                 while((count = origin.read(data, 0, BUFFER)) != -1) {\r
209                                         out.write(data, 0, count);\r
210                                 }\r
211                                 origin.close();\r
212                         }\r
213                         out.close();\r
214                         return true;\r
215                 } catch (Exception e) {\r
216                         e.printStackTrace();\r
217                         return false;\r
218                 }\r
219         }\r
220         \r
221         public static LinkedList<File> getFiles(File dir) {\r
222                 LinkedList<File> output = new LinkedList<File>();\r
223                 for (File file : dir.listFiles()) {\r
224                         if (file.isFile()) {\r
225                                 output.add(file);\r
226                         } else if (file.isDirectory()) {\r
227                                 output.addAll(getFiles(file));\r
228                         }\r
229                 }\r
230                 return output;\r
231         }\r
232 \r
233         public static File toCB(File dc) {\r
234                 return new File(dc.getAbsolutePath().replaceFirst("changes", craftName));\r
235         }\r
236         \r
237         public static void copyFile(File sourceFile, File destFile) throws IOException {\r
238                 if (!destFile.exists()) {\r
239                         destFile.createNewFile();\r
240                 }\r
241 \r
242                 FileChannel source = null;\r
243                 FileChannel destination = null;\r
244                 try {\r
245                         source = new FileInputStream(sourceFile).getChannel();\r
246                         destination = new FileOutputStream(destFile).getChannel();\r
247 \r
248                         long count = 0;\r
249                         long size = source.size();                        \r
250                         while((count += destination.transferFrom(source, 0, size-count))<size);\r
251                 } finally {\r
252                         if (source != null) {\r
253                                 source.close();\r
254                         }\r
255                         if (destination != null) {\r
256                                 destination.close();\r
257                         }\r
258                 }\r
259         }\r
260         \r
261         public static String removeExtension(String file) {\r
262                 return FilenameUtils.removeExtension(file);\r
263         }\r
264 \r
265 }