Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.53 KB | None | 0 0
  1. package Joehot200;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.EventQueue;
  5. import java.awt.Toolkit;
  6. import java.io.BufferedReader;
  7. import java.io.EOFException;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.FileReader;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.io.PrintWriter;
  15. import java.net.HttpURLConnection;
  16. import java.net.ProtocolException;
  17. import java.net.URL;
  18. import java.net.URLConnection;
  19. import java.nio.charset.StandardCharsets;
  20. import java.nio.file.Files;
  21. import java.nio.file.Paths;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24.  
  25. import javax.swing.JFrame;
  26. import javax.swing.JProgressBar;
  27.  
  28. public class Main extends JFrame{
  29.  
  30. String version = "0.0";
  31. boolean needDownload = false;
  32. static JFrame fr;
  33. public static void main(String[] args){
  34. fr = new Main();
  35. }
  36. public void needDownload(){
  37. {
  38. File f = new File(System.getProperty("user.dir") + "/ver.txt");
  39. try {
  40. List<String> lines = Files.readAllLines(Paths.get(f.getAbsolutePath()), StandardCharsets.UTF_8);
  41. for (String str : lines){
  42. version = str;
  43. System.out.println("Version: " + str);
  44. }
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. progressBar.setValue(25);
  50. {
  51. StringBuilder page = new StringBuilder();
  52. URL google;
  53. try {
  54. google = new URL("http://endcraft.net/ver");
  55.  
  56. //URL google = new URL("www.endcraft.net/Post");
  57. URLConnection yc = google.openConnection();
  58. BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
  59. String inputLine;
  60. progressBar.setValue(50);
  61. while ((inputLine = in.readLine()) != null) {
  62. System.out.println(inputLine + " || " + version);
  63. if (!inputLine.contains(version)){
  64. needDownload = true;
  65. System.out.println("Downloading!");
  66. File f = new File(System.getProperty("user.dir") + "/ver.txt");
  67. if (f.exists()) f.delete();
  68. f.createNewFile();
  69. PrintWriter writer = new PrintWriter(f.getAbsoluteFile(), "UTF-8");
  70. writer.println(inputLine);
  71. System.out.println("Updated to version " + inputLine + " from version " + version);
  72. writer.close();
  73. }else{
  74. System.out.println("No downloaded needed!");
  75. }
  76. }
  77. progressBar.setValue(100);
  78. } catch (Exception e) {
  79. // TODO Auto-generated catch block
  80. e.printStackTrace();
  81. }
  82. }
  83. //RES TODO
  84. {
  85. /*StringBuilder page = new StringBuilder();
  86. URL google;
  87. try {
  88. google = new URL("http://endcraft.net/verres");
  89.  
  90. //URL google = new URL("www.endcraft.net/Post");
  91. URLConnection yc = google.openConnection();
  92. BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
  93. String inputLine;
  94. while ((inputLine = in.readLine()) != null) {
  95. System.out.println(inputLine + " || " + versionRes);
  96. if (!inputLine.contains(versionRes)){
  97. downloadRes = true;
  98. System.out.println("Downloading res!");
  99. }else{
  100. System.out.println("No res downloaded needed!");
  101. }
  102. }
  103. } catch (Exception e) {
  104. // TODO Auto-generated catch block
  105. e.printStackTrace();
  106. }*/
  107. }
  108. }
  109. //boolean downloadRes = false;
  110. JProgressBar progressBar;
  111. JProgressBar oprogressBar;
  112. int i = 0;
  113. List<Thread> threads = new ArrayList<Thread>();
  114. long lastTime = 0;
  115. public Main() {
  116. fr = this;
  117. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  118. int width = (int) screenSize.getWidth();
  119. int height = (int) screenSize.getHeight();
  120. fr.setTitle("Downloading game...");
  121. fr.setBounds(width / 2 - 200, height / 2 - 50, 400, 100);
  122. fr.setVisible(true);
  123. progressBar = new JProgressBar(0);
  124. oprogressBar = new JProgressBar(0);
  125. progressBar.setBounds(0, 0, 400, 50);
  126. oprogressBar.setBounds(0, 50, 400, 50);
  127. fr.add(progressBar);
  128. fr.add(oprogressBar);
  129. progressBar.setValue(0);
  130. oprogressBar.setValue(0);
  131. needDownload();
  132. //if (downloadRes){
  133.  
  134. {
  135. {
  136. File f = new File("res/");
  137. if (!f.exists()){
  138. if (!f.mkdir()){
  139. System.out.println("Unable to create dir!!");
  140. System.exit(1);
  141. }else{
  142. System.out.println("Created dir!");
  143. }
  144. }else{
  145. System.out.println("Dir already exists");
  146. }
  147. }
  148. {
  149. File f = new File("res/natives-win");
  150.  
  151. if (!f.exists()){
  152. if (!f.mkdir()){
  153. System.out.println("Unable to create dir!!");
  154. System.exit(1);
  155. }else{
  156. System.out.println("Created dir!");
  157. }
  158. }else{
  159. System.out.println("Dir already exists");
  160. }
  161. }
  162. progressBar.setValue(10);
  163. try{
  164. {
  165. URL url = new URL("http://www.endcraft.net/webstart/res/download.txt");
  166. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  167. conn.setRequestMethod("HEAD");
  168. File fi = new File("res/download.txt");
  169. System.out.println((fi.length() != conn.getContentLength()) + " " + fi.length() + " || " + conn.getContentLength());
  170. if (!fi.exists() || fi.length() != conn.getContentLength()){
  171. System.out.println("Downloading resource!");
  172. download("http://www.endcraft.net/webstart/res/download.txt", "res/download.txt", conn.getContentLength());
  173. }else{
  174. System.out.println("No need to download resource!");
  175. }
  176.  
  177. }
  178. progressBar.setValue(15);
  179. ArrayList<String> download = readTextFile("res/download.txt");
  180.  
  181.  
  182. for (final String str : download){
  183.  
  184.  
  185. URL url = new URL("http://www.endcraft.net/webstart/res/" + str);
  186. final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  187. progressBar.setValue(30);
  188. conn.setRequestMethod("HEAD");
  189. File fi = new File("res/" + str);
  190. if (!fi.getParentFile().exists()){
  191. fi.getParentFile().mkdirs();
  192. }
  193. System.out.println((fi.length() != conn.getContentLength()) + " " + fi.length() + " || " + conn.getContentLength());
  194. if (!fi.exists() || fi.length() != conn.getContentLength()){
  195. System.out.println("Downloading resource!");
  196. progressBar.setValue(50);
  197. Thread t = new Thread() {
  198. @Override public void run() {
  199. download("http://www.endcraft.net/webstart/res/" + str, "res/" + str, conn.getContentLength());
  200. }
  201. };
  202. t.start();
  203. threads.add(t);
  204.  
  205.  
  206. }else{
  207. System.out.println("No need to download resource!");
  208. progressBar.setValue(100);
  209. }
  210.  
  211. }
  212. i++;
  213. oprogressBar.setValue((int) ((i * 100.0f) / download.size() - threads.size()));
  214.  
  215.  
  216. }catch (Exception e){
  217.  
  218. }
  219. }
  220. progressBar.setValue(50);
  221. File f = new File("privateers.jar");
  222. System.out.println("Exists: " + f.exists());
  223. try{
  224. URL url = new URL("http://www.endcraft.net/webstart/privateers.jar");
  225. final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  226. conn.setRequestMethod("HEAD");
  227. if (needDownload || !f.exists() || f.length() != conn.getContentLength()){
  228.  
  229.  
  230. Thread t = new Thread() {
  231. @Override public void run() {
  232. download("http://www.endcraft.net/webstart/privateers.jar", "privateers.jar", conn.getContentLength());
  233. }
  234. };
  235. t.start();
  236. threads.add(t);
  237.  
  238. }
  239. } catch (Exception e) {
  240. e.printStackTrace();
  241. }
  242.  
  243. for (Thread t : threads) {
  244. try {
  245. t.join();
  246. } catch (InterruptedException e) {
  247. e.printStackTrace();
  248. }
  249.  
  250. }
  251.  
  252. progressBar.setValue(95);
  253. try {
  254. File dir = new File("settings");
  255. if (!dir.exists()){
  256. dir.mkdirs();
  257. }
  258. String exec = (System.getProperty("user.dir") + java.io.File.separator + "privateers.jar");
  259. String[] command = {"java", "-jar", exec};
  260. ProcessBuilder pb =
  261. new ProcessBuilder(command[0], command[1], command[2]);
  262. progressBar.setValue(100);
  263. System.out.println(pb.command());
  264. pb.start();
  265. //final Process process = Runtime.getRuntime().exec(command);
  266. System.out.println("Running " + exec);
  267. //while (true){
  268. //System.out.println(process.waitFor() + " || " + process.getOutputStream());
  269. System.exit(0);
  270. //}
  271.  
  272. } catch (IOException e1) {
  273. e1.printStackTrace();
  274. }
  275.  
  276. }
  277. void download(String source, String destination, int size) {
  278.  
  279. // ten percent of the total download size
  280. File ofile = new File(System.getProperty("user.dir") + "", destination);
  281. System.out.printf("\nDownloading\n\t%s\nTo\n\t%s\n", source, destination);
  282. try {
  283. if (ofile.exists()) ofile.delete();
  284. if (!ofile.createNewFile()) {
  285. throw new IOException("Can't create " + ofile.getAbsolutePath());
  286. }
  287.  
  288. int inChar = 0;
  289. URL url = new URL(source);
  290. InputStream input = url.openStream();
  291. FileOutputStream fos = new FileOutputStream(ofile);
  292. for (int i = 0; i < size && inChar != -1; i++) {
  293. if (System.nanoTime() > lastTime + 2000000000){
  294. lastTime = System.nanoTime();
  295. int percentage = (int) ((i * 100.0f) / size);
  296.  
  297. progressBar.setValue(((int) ((percentage * 100.0f) / 100)));
  298. fr.setTitle(ofile.getName() + ": " + progressBar.getValue() + "%" + " Total: " + oprogressBar.getValue() + "%");
  299. }
  300. inChar = input.read();
  301. fos.write(inChar);
  302. }
  303. i++;
  304. oprogressBar.setValue((int) ((i * 100.0f) / threads.size()));
  305. input.close();
  306. fos.close();
  307. System.out.println("Downloaded " + ofile.getAbsolutePath());
  308. } catch (EOFException e) {
  309. e.printStackTrace();
  310. } catch (IOException e) {
  311. e.printStackTrace();
  312. }
  313. }
  314.  
  315. public static ArrayList<String> readTextFile(String fileName) {
  316.  
  317. ArrayList<String> values = new ArrayList<String>();
  318. FileReader file = null;
  319.  
  320. try {
  321.  
  322. file = new FileReader(fileName);
  323. BufferedReader reader = new BufferedReader(file);
  324. String line = "";
  325. while ((line = reader.readLine()) != null) {
  326. //////System.out.println(line);
  327.  
  328. //line = line.replace("~", "");
  329. //if (!line.endsWith("~")) line = line + "~";
  330. //if (line == "" || line.startsWith("~") || line.length() <= 1){
  331. // line = ChatColor.GREEN + " ~";
  332. //}
  333. values.add(line);
  334. //////System.out.println(line);
  335.  
  336. }
  337. } catch (Exception e) {
  338. e.printStackTrace();
  339. throw new RuntimeException(e);
  340. } finally {
  341. if (file != null) {
  342. try {
  343. file.close();
  344. } catch (IOException e) {
  345. // Ignore issues during closing
  346. }
  347. }
  348. }
  349. //String[] returnValue = new String[values.size()];
  350. //for ()
  351. return values;
  352. }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement