Advertisement
Guest User

main.java

a guest
Apr 2nd, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. package io.github.epicoweo.main;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.lang.reflect.InvocationTargetException;
  10. import java.lang.reflect.Method;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. import java.util.zip.ZipEntry;
  14. import java.util.zip.ZipOutputStream;
  15.  
  16. import io.github.epicoweo.resources.References;
  17.  
  18. public class Main {
  19.  
  20. public static String input;
  21. static Class<?> loadedMethod = null;
  22.  
  23. public static void main(String[] args) {
  24. runProgram();
  25. }
  26.  
  27. private static void runProgram() {
  28. try {
  29. createVersionInfo();
  30. System.out.println("Running " + References.PROJNAME + " v" + References.VERSION +
  31. " released " + References.RELEASEDATE);
  32. System.out.println();
  33. userInput();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38.  
  39. private static void userInput() throws IOException {
  40.  
  41. System.out.println("Input command, use --help for command list");
  42.  
  43. InputStreamReader cin = new InputStreamReader(System.in);
  44. BufferedReader reader = new BufferedReader(cin);
  45. String rawInput = reader.readLine();
  46.  
  47. Pattern pattern = Pattern.compile("(--)(\\w+)", Pattern.CASE_INSENSITIVE);
  48. Matcher matcher = pattern.matcher(rawInput);
  49. matcher.matches();
  50. input = matcher.group(2);
  51. checkCommand(input);
  52. }
  53.  
  54. public static void help() throws IOException {
  55. long t1 = System.currentTimeMillis();
  56. System.out.println("\nCommands:\n");
  57. for(String command : References.COMMANDS) {
  58. System.out.println("--" + command);
  59. }
  60.  
  61. userInput();
  62. }
  63.  
  64. public static void zipProject() throws IOException {
  65. String sourceFile = "C:/Users/Braden/game/ProjTemplate";
  66. FileOutputStream fos = new FileOutputStream("E:/Desktop/" + References.PROJNAME + "-v" + References.VERSION + ".zip");
  67. ZipOutputStream zipOut = new ZipOutputStream(fos);
  68. File fileToZip = new File(sourceFile);
  69. zipFile(fileToZip, fileToZip.getName(), zipOut);
  70.  
  71. zipOut.close();
  72. fos.close();
  73.  
  74. userInput();
  75. }
  76.  
  77. public static void toHex() throws IOException {
  78. System.out.println("Input: ");
  79.  
  80. InputStreamReader cin = new InputStreamReader(System.in);
  81. BufferedReader reader = new BufferedReader(cin);
  82. String input = reader.readLine();
  83. //BINARY
  84. if(input.matches("(0[bB])([0-1])+")) {
  85. input = input.substring(2);
  86. int num = Integer.parseInt(input, 2);
  87. String hex = "0x" + Integer.toHexString(num);
  88.  
  89. System.out.println(hex);
  90. //HEX
  91. } else if(input.matches("(0[xX])([0-9a-fA-F])+")) {
  92. System.out.println(input);
  93. //OCTAL
  94. } else if(input.matches("(0)([0-9])+")) {
  95. input = input.substring(1);
  96. int num = Integer.parseInt(input, 8);
  97. String hex = "0x" + Integer.toHexString(num);
  98.  
  99. System.out.println(hex);
  100. //DECIMAL
  101. } else if(input.matches("[1-9][0-9]+")) {
  102. String hex = "0x" + Integer.toHexString(Integer.parseInt(input));
  103.  
  104. System.out.println(hex);
  105. } else {
  106. System.out.println("Invalid number.");
  107. }
  108. userInput();
  109. }
  110.  
  111. private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
  112. if(fileToZip.isHidden()) {
  113. return;
  114. }
  115.  
  116. if(fileToZip.isDirectory()) {
  117. if(fileName.endsWith("/")) {
  118. zipOut.putNextEntry(new ZipEntry(fileName));
  119. zipOut.closeEntry();
  120. } else if (fileName.endsWith(".zip")){
  121. return;
  122. } else {
  123. zipOut.putNextEntry(new ZipEntry(fileName + "/"));
  124. zipOut.closeEntry();
  125. }
  126. File[] children = fileToZip.listFiles();
  127. for(File child : children) {
  128. zipFile(child, fileName + "/" + child.getName(), zipOut);
  129. }
  130. return;
  131.  
  132. }
  133. FileInputStream fis = new FileInputStream(fileToZip);
  134. ZipEntry zipEntry = new ZipEntry(fileName);
  135. zipOut.putNextEntry(zipEntry);
  136. byte[] b = new byte[1024];
  137. int length;
  138. while((length = fis.read(b)) >= 0) {
  139. zipOut.write(b, 0, length);
  140. }
  141. fis.close();
  142. }
  143.  
  144. private static void checkCommand(String input) {
  145. try {
  146. loadedMethod = Class.forName("io.github.epicoweo.main.Main");
  147. } catch (ClassNotFoundException e) {
  148. e.printStackTrace();
  149. }
  150.  
  151. try {
  152. Method m = loadedMethod.getMethod(input, (Class[]) null);
  153.  
  154. m.invoke((Object[]) null);
  155. } catch (NoSuchMethodException e) {
  156. e.printStackTrace();
  157. }
  158. catch (SecurityException e) {
  159. e.printStackTrace();
  160. }
  161. catch (IllegalAccessException e) {
  162. e.printStackTrace();
  163. }
  164. catch (IllegalArgumentException e) {
  165. e.printStackTrace();
  166. }
  167. catch (InvocationTargetException e) {
  168. e.printStackTrace();
  169. }
  170.  
  171. return;
  172. }
  173.  
  174. private static void createVersionInfo() throws IOException {
  175. File versionInfo = new File("versionInfo.txt");
  176. if(!versionInfo.exists())
  177. versionInfo.createNewFile();
  178.  
  179. FileOutputStream fout = new FileOutputStream(versionInfo);
  180.  
  181. //output to be written to file
  182. String s = "name: " + References.PROJNAME
  183. + "\nversion: " + References.VERSION
  184. + "\nreleaseDate: " + References.RELEASEDATE;
  185. byte b[] = s.getBytes(); //convert string to bytes
  186. fout.write(b); //write bytes
  187. fout.close(); //close file
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement