|
| 1 | +import java.io.FileNotFoundException; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonSyntaxException; |
| 5 | + |
| 6 | +import bundles.BundleList; |
| 7 | +import bundles.Mod; |
| 8 | +import config.Configurations; |
| 9 | +import errorHandling.exceptions.ConfigException; |
| 10 | +import errorHandling.exceptions.ItemFileException; |
| 11 | +import errorHandling.exceptions.PrettyException; |
| 12 | +import items.ItemDatabase; |
| 13 | +import util.InputReading; |
| 14 | +import util.ReadableJsonFile; |
| 15 | +import util.WriteableJsonFile; |
| 16 | + |
| 17 | +public class Interface { |
| 18 | + |
| 19 | + private ItemDatabase itemDb; |
| 20 | + |
| 21 | + public Interface() throws ItemFileException, ConfigException { |
| 22 | + Configurations configs = new Configurations(); |
| 23 | + configs.configure(); |
| 24 | + this.itemDb = new ItemDatabase(); |
| 25 | + } |
| 26 | + |
| 27 | + public void start() throws PrettyException { |
| 28 | + System.out.println("Welcome to Stardew Valley Bundle Maker!"); |
| 29 | + System.out.println(); |
| 30 | + while (true) { |
| 31 | + System.out.println("Enter a command (e.g. help)."); |
| 32 | + while (true) { |
| 33 | + String input = InputReading.getString().toLowerCase().trim(); |
| 34 | + switch (input) { |
| 35 | + case "sbmm": |
| 36 | + makeSbmmFormatFile(); |
| 37 | + break; |
| 38 | + case "sv": |
| 39 | + makeSvFormatFile(); |
| 40 | + break; |
| 41 | + case "m": |
| 42 | + makeMod(); |
| 43 | + break; |
| 44 | + case "q": |
| 45 | + System.exit(0); |
| 46 | + break; |
| 47 | + case "help": |
| 48 | + System.out.println("Command | Description"); |
| 49 | + System.out.println("--------+-----------------------------------------------------------------------"); |
| 50 | + System.out.println(" sbmm | Convert a JSON bundles file from Stardew Valley format to SBMM format."); |
| 51 | + System.out.println(" sv | Convert a JSON bundles file from SBMM format to Stardew Valley format."); |
| 52 | + System.out.println(" m | Make a mod."); |
| 53 | + System.out.println(" q | Terminate the program."); |
| 54 | + break; |
| 55 | + default: |
| 56 | + System.out.println("Unrecognised command. Please type 'help' for a list of valid commands."); |
| 57 | + continue; |
| 58 | + } |
| 59 | + System.out.println(); |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void makeSbmmFormatFile() throws PrettyException { |
| 66 | + // Get input bundles json |
| 67 | + ReadableJsonFile inputFile; |
| 68 | + JsonObject inputJson; |
| 69 | + while (true) { |
| 70 | + inputFile = new ReadableJsonFile("Bundles/SvFormat", null); |
| 71 | + try { |
| 72 | + inputJson = inputFile.getJson(); |
| 73 | + break; |
| 74 | + } catch (JsonSyntaxException e) { |
| 75 | + System.out.println("Your file does not contain valid JSON.\n"); |
| 76 | + continue; |
| 77 | + } catch (FileNotFoundException e) { |
| 78 | + System.out.println("File not found.\n"); |
| 79 | + continue; |
| 80 | + } |
| 81 | + } |
| 82 | + // Make bundle objects |
| 83 | + BundleList bundles = new BundleList(this.itemDb, false, inputFile, inputJson); |
| 84 | + // Make the output file |
| 85 | + WriteableJsonFile outputFile = new WriteableJsonFile("Bundles/SbmmFormat", inputFile.getBaseName()); |
| 86 | + outputFile.write(bundles.getSbmmFormatBundles()); |
| 87 | + // Inform user of success |
| 88 | + System.out.println("Your file is ready! You can find it at " + outputFile.getDirectoryPath() + "\n"); |
| 89 | + } |
| 90 | + |
| 91 | + private void makeSvFormatFile() throws PrettyException { |
| 92 | + // Get input bundles json |
| 93 | + ReadableJsonFile inputFile; |
| 94 | + JsonObject inputJson; |
| 95 | + while (true) { |
| 96 | + inputFile = new ReadableJsonFile("Bundles/SbmmFormat", null); |
| 97 | + try { |
| 98 | + inputJson = inputFile.getJson(); |
| 99 | + break; |
| 100 | + } catch (JsonSyntaxException e) { |
| 101 | + System.out.println("Your file does not contain valid JSON.\n"); |
| 102 | + continue; |
| 103 | + } catch (FileNotFoundException e) { |
| 104 | + System.out.println("File not found.\n"); |
| 105 | + continue; |
| 106 | + } |
| 107 | + } |
| 108 | + // Make bundle objects |
| 109 | + BundleList bundles = new BundleList(this.itemDb, true, inputFile, inputJson); |
| 110 | + // Make the output file |
| 111 | + WriteableJsonFile outputFile = new WriteableJsonFile("Bundles/SvFormat", inputFile.getBaseName()); |
| 112 | + outputFile.write(bundles.getSvFormatBundles()); |
| 113 | + // Inform user of success |
| 114 | + System.out.println("Your file is ready! You can find it at " + outputFile.getDirectoryPath() + "\n"); |
| 115 | + } |
| 116 | + |
| 117 | + private void makeMod() throws PrettyException { |
| 118 | + ReadableJsonFile inputFile; |
| 119 | + boolean isSbmmFormat; |
| 120 | + while (true) { |
| 121 | + // Get input file name |
| 122 | + ReadableJsonFile sbmmFile = new ReadableJsonFile("Bundles/SbmmFormat", null); |
| 123 | + ReadableJsonFile svFile = new ReadableJsonFile("Bundles/SvFormat", sbmmFile.getBaseName()); |
| 124 | + boolean sbmmFileExists = sbmmFile.exists(); |
| 125 | + boolean svFileExists = svFile.exists(); |
| 126 | + if (sbmmFileExists ^ svFileExists) { |
| 127 | + // One exists |
| 128 | + if (sbmmFileExists) { |
| 129 | + inputFile = sbmmFile; |
| 130 | + isSbmmFormat = true; |
| 131 | + } else { |
| 132 | + inputFile = svFile; |
| 133 | + isSbmmFormat = false; |
| 134 | + } |
| 135 | + } else if (sbmmFileExists) { |
| 136 | + // Both exist |
| 137 | + while (true) { |
| 138 | + // Determine which file the user wants to use |
| 139 | + System.out.println("Is your file in SBMM or SV format?"); |
| 140 | + String format = InputReading.getString().toLowerCase(); |
| 141 | + switch (format) { |
| 142 | + case "sbmm": |
| 143 | + inputFile = sbmmFile; |
| 144 | + isSbmmFormat = true; |
| 145 | + break; |
| 146 | + case "sv": |
| 147 | + inputFile = svFile; |
| 148 | + isSbmmFormat = false; |
| 149 | + break; |
| 150 | + default: |
| 151 | + System.out.println("Unrecognised response. Please enter 'sbmm' or 'sv'.\n"); |
| 152 | + continue; |
| 153 | + } |
| 154 | + break; |
| 155 | + } |
| 156 | + } else { |
| 157 | + // Neither exist |
| 158 | + System.out.println("File not found.\n"); |
| 159 | + continue; |
| 160 | + } |
| 161 | + break; |
| 162 | + } |
| 163 | + try { |
| 164 | + // Make the mod |
| 165 | + String modName = inputFile.getBaseName(); |
| 166 | + Mod mod = new Mod("StardewBundleModMaker", modName, this.itemDb, isSbmmFormat, inputFile, inputFile.getJson()); |
| 167 | + // Create output files |
| 168 | + mod.write(); |
| 169 | + // Inform user of success |
| 170 | + System.out.println("Your mod is ready! You can find it at Mods/" + modName + "\n"); |
| 171 | + } catch (FileNotFoundException e1) { |
| 172 | + throw new RuntimeException("File '" + inputFile + "' not found after positive check."); |
| 173 | + } catch (JsonSyntaxException e2) { |
| 174 | + System.out.println("That file does not contain valid JSON.\n"); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + public static void main(String[] args) { |
| 179 | + try { |
| 180 | + Interface i = new Interface(); |
| 181 | + i.start(); |
| 182 | + } catch (PrettyException e) { |
| 183 | + // Allow the user to read the error message before the program terminates |
| 184 | + System.out.println("Press enter to terminate the program."); |
| 185 | + InputReading.getVoid(); |
| 186 | + // Terminate |
| 187 | + System.exit(0); |
| 188 | + } catch (RuntimeException programmerError) { |
| 189 | + System.err.println("PROGRAMMER ERROR: " + programmerError.getMessage()); |
| 190 | + programmerError.printStackTrace(); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments