Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.github.epicoweo.main;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
- import io.github.epicoweo.resources.References;
- public class Main {
- public static String input;
- static Class<?> loadedMethod = null;
- public static void main(String[] args) {
- runProgram();
- }
- private static void runProgram() {
- try {
- createVersionInfo();
- System.out.println("Running " + References.PROJNAME + " v" + References.VERSION +
- " released " + References.RELEASEDATE);
- System.out.println();
- userInput();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void userInput() throws IOException {
- System.out.println("Input command, use --help for command list");
- InputStreamReader cin = new InputStreamReader(System.in);
- BufferedReader reader = new BufferedReader(cin);
- String rawInput = reader.readLine();
- Pattern pattern = Pattern.compile("(--)(\\w+)", Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(rawInput);
- matcher.matches();
- input = matcher.group(2);
- checkCommand(input);
- }
- public static void help() throws IOException {
- long t1 = System.currentTimeMillis();
- System.out.println("\nCommands:\n");
- for(String command : References.COMMANDS) {
- System.out.println("--" + command);
- }
- userInput();
- }
- public static void zipProject() throws IOException {
- String sourceFile = "C:/Users/Braden/game/ProjTemplate";
- FileOutputStream fos = new FileOutputStream("E:/Desktop/" + References.PROJNAME + "-v" + References.VERSION + ".zip");
- ZipOutputStream zipOut = new ZipOutputStream(fos);
- File fileToZip = new File(sourceFile);
- zipFile(fileToZip, fileToZip.getName(), zipOut);
- zipOut.close();
- fos.close();
- userInput();
- }
- public static void toHex() throws IOException {
- System.out.println("Input: ");
- InputStreamReader cin = new InputStreamReader(System.in);
- BufferedReader reader = new BufferedReader(cin);
- String input = reader.readLine();
- //BINARY
- if(input.matches("(0[bB])([0-1])+")) {
- input = input.substring(2);
- int num = Integer.parseInt(input, 2);
- String hex = "0x" + Integer.toHexString(num);
- System.out.println(hex);
- //HEX
- } else if(input.matches("(0[xX])([0-9a-fA-F])+")) {
- System.out.println(input);
- //OCTAL
- } else if(input.matches("(0)([0-9])+")) {
- input = input.substring(1);
- int num = Integer.parseInt(input, 8);
- String hex = "0x" + Integer.toHexString(num);
- System.out.println(hex);
- //DECIMAL
- } else if(input.matches("[1-9][0-9]+")) {
- String hex = "0x" + Integer.toHexString(Integer.parseInt(input));
- System.out.println(hex);
- } else {
- System.out.println("Invalid number.");
- }
- userInput();
- }
- private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
- if(fileToZip.isHidden()) {
- return;
- }
- if(fileToZip.isDirectory()) {
- if(fileName.endsWith("/")) {
- zipOut.putNextEntry(new ZipEntry(fileName));
- zipOut.closeEntry();
- } else if (fileName.endsWith(".zip")){
- return;
- } else {
- zipOut.putNextEntry(new ZipEntry(fileName + "/"));
- zipOut.closeEntry();
- }
- File[] children = fileToZip.listFiles();
- for(File child : children) {
- zipFile(child, fileName + "/" + child.getName(), zipOut);
- }
- return;
- }
- FileInputStream fis = new FileInputStream(fileToZip);
- ZipEntry zipEntry = new ZipEntry(fileName);
- zipOut.putNextEntry(zipEntry);
- byte[] b = new byte[1024];
- int length;
- while((length = fis.read(b)) >= 0) {
- zipOut.write(b, 0, length);
- }
- fis.close();
- }
- private static void checkCommand(String input) {
- try {
- loadedMethod = Class.forName("io.github.epicoweo.main.Main");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- try {
- Method m = loadedMethod.getMethod(input, (Class[]) null);
- m.invoke((Object[]) null);
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- }
- catch (SecurityException e) {
- e.printStackTrace();
- }
- catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- catch (IllegalArgumentException e) {
- e.printStackTrace();
- }
- catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- return;
- }
- private static void createVersionInfo() throws IOException {
- File versionInfo = new File("versionInfo.txt");
- if(!versionInfo.exists())
- versionInfo.createNewFile();
- FileOutputStream fout = new FileOutputStream(versionInfo);
- //output to be written to file
- String s = "name: " + References.PROJNAME
- + "\nversion: " + References.VERSION
- + "\nreleaseDate: " + References.RELEASEDATE;
- byte b[] = s.getBytes(); //convert string to bytes
- fout.write(b); //write bytes
- fout.close(); //close file
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement