| 1 | package net.oni2.aeinstaller.backend.mods;
 | 
|---|
| 2 | 
 | 
|---|
| 3 | import java.io.BufferedReader;
 | 
|---|
| 4 | import java.io.File;
 | 
|---|
| 5 | import java.io.FileInputStream;
 | 
|---|
| 6 | import java.io.FileNotFoundException;
 | 
|---|
| 7 | import java.io.FilenameFilter;
 | 
|---|
| 8 | import java.io.IOException;
 | 
|---|
| 9 | import java.io.InputStreamReader;
 | 
|---|
| 10 | import java.util.HashSet;
 | 
|---|
| 11 | 
 | 
|---|
| 12 | import net.oni2.aeinstaller.backend.Paths;
 | 
|---|
| 13 | import net.oni2.aeinstaller.backend.Settings;
 | 
|---|
| 14 | import net.oni2.aeinstaller.backend.Settings.Platform;
 | 
|---|
| 15 | import net.oni2.aeinstaller.backend.depot.DepotManager;
 | 
|---|
| 16 | import net.oni2.aeinstaller.backend.depot.model.NodeMod;
 | 
|---|
| 17 | import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
 | 
|---|
| 18 | 
 | 
|---|
| 19 | /**
 | 
|---|
| 20 |  * @author Christian Illy
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | public class Mod implements Comparable<Mod> {
 | 
|---|
| 23 |         // TODO: Dependencies/Conflicts
 | 
|---|
| 24 | 
 | 
|---|
| 25 |         private String name = "";
 | 
|---|
| 26 |         private int packageNumber = 0;
 | 
|---|
| 27 | 
 | 
|---|
| 28 |         private HashSet<Type> types = new HashSet<Type>();
 | 
|---|
| 29 |         private ECompatiblePlatform platform = null;
 | 
|---|
| 30 |         private String version = "";
 | 
|---|
| 31 |         private String creator = "";
 | 
|---|
| 32 |         private EBSLInstallType bslInstallType = null;
 | 
|---|
| 33 |         private String description = "";
 | 
|---|
| 34 |         private double aeVersion = 0;
 | 
|---|
| 35 |         private int zipSize = 0;
 | 
|---|
| 36 |         private NodeMod node = null;
 | 
|---|
| 37 |         private net.oni2.aeinstaller.backend.depot.model.File file = null;
 | 
|---|
| 38 | 
 | 
|---|
| 39 |         private HashSet<Integer> conflicts = new HashSet<Integer>();
 | 
|---|
| 40 |         private HashSet<Integer> dependencies = new HashSet<Integer>();
 | 
|---|
| 41 | 
 | 
|---|
| 42 |         private long localTimestamp = 0;
 | 
|---|
| 43 | 
 | 
|---|
| 44 |         /**
 | 
|---|
| 45 |          * Create a new Mod entry from a given Mod-Node
 | 
|---|
| 46 |          * 
 | 
|---|
| 47 |          * @param nm
 | 
|---|
| 48 |          *            Mod-Node
 | 
|---|
| 49 |          */
 | 
|---|
| 50 |         public Mod(NodeMod nm) {
 | 
|---|
| 51 |                 node = nm;
 | 
|---|
| 52 |                 name = nm.getTitle();
 | 
|---|
| 53 |                 packageNumber = nm.getPackageNumber();
 | 
|---|
| 54 |                 for (TaxonomyTerm tt : nm.getTypes()) {
 | 
|---|
| 55 |                         Type t = ModManager.getInstance().getTypeByName(tt.getName());
 | 
|---|
| 56 |                         types.add(t);
 | 
|---|
| 57 |                         if (!nm.isTool())
 | 
|---|
| 58 |                                 t.addEntry(this);
 | 
|---|
| 59 |                 }
 | 
|---|
| 60 |                 platform = nm.getPlatform();
 | 
|---|
| 61 |                 version = nm.getVersion();
 | 
|---|
| 62 |                 creator = nm.getCreator();
 | 
|---|
| 63 |                 if (nm.getBody() != null)
 | 
|---|
| 64 |                         description = nm.getBody().getSafe_value();
 | 
|---|
| 65 |                 file = DepotManager.getInstance().getFile(
 | 
|---|
| 66 |                                 nm.getUploads().firstElement().getFid());
 | 
|---|
| 67 |                 zipSize = file.getFilesize();
 | 
|---|
| 68 | 
 | 
|---|
| 69 |                 if (isLocalAvailable())
 | 
|---|
| 70 |                         updateLocalData();
 | 
|---|
| 71 |         }
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         /**
 | 
|---|
| 74 |          * Update information for local package existence
 | 
|---|
| 75 |          */
 | 
|---|
| 76 |         public void updateLocalData() {
 | 
|---|
| 77 |                 File config = new File(getLocalPath(), "Mod_Info.cfg");
 | 
|---|
| 78 |                 File timestamp = new File(getLocalPath(), "aei.cfg");
 | 
|---|
| 79 |                 if (config.exists()) {
 | 
|---|
| 80 |                         try {
 | 
|---|
| 81 |                                 FileInputStream fstream = new FileInputStream(config);
 | 
|---|
| 82 |                                 InputStreamReader isr = new InputStreamReader(fstream);
 | 
|---|
| 83 |                                 BufferedReader br = new BufferedReader(isr);
 | 
|---|
| 84 |                                 String strLine;
 | 
|---|
| 85 |                                 while ((strLine = br.readLine()) != null) {
 | 
|---|
| 86 |                                         if (strLine.indexOf("->") < 1)
 | 
|---|
| 87 |                                                 continue;
 | 
|---|
| 88 |                                         if (strLine.indexOf("//") >= 0)
 | 
|---|
| 89 |                                                 strLine = strLine.substring(0, strLine.indexOf("//"));
 | 
|---|
| 90 |                                         String[] split = strLine.split("->", 2);
 | 
|---|
| 91 |                                         String sName = split[0].trim();
 | 
|---|
| 92 |                                         String sVal = split[1].trim();
 | 
|---|
| 93 |                                         if (sName.equalsIgnoreCase("AEInstallVersion")) {
 | 
|---|
| 94 |                                                 aeVersion = Double.parseDouble(sVal);
 | 
|---|
| 95 |                                         } else if (sName.equalsIgnoreCase("NameOfMod")) {
 | 
|---|
| 96 |                                                 if (node == null)
 | 
|---|
| 97 |                                                         name = sVal;
 | 
|---|
| 98 |                                         } else if (sName.equalsIgnoreCase("Creator")) {
 | 
|---|
| 99 |                                                 if (node == null)
 | 
|---|
| 100 |                                                         creator = sVal;
 | 
|---|
| 101 |                                         } else if (sName.equalsIgnoreCase("HasBsl")) {
 | 
|---|
| 102 |                                                 if (sVal.equalsIgnoreCase("addon"))
 | 
|---|
| 103 |                                                         bslInstallType = EBSLInstallType.ADDON;
 | 
|---|
| 104 |                                                 else if (sVal.equalsIgnoreCase("yes"))
 | 
|---|
| 105 |                                                         bslInstallType = EBSLInstallType.NORMAL;
 | 
|---|
| 106 |                                                 else
 | 
|---|
| 107 |                                                         bslInstallType = EBSLInstallType.NONE;
 | 
|---|
| 108 |                                         } else if (sName.equalsIgnoreCase("ModVersion")) {
 | 
|---|
| 109 |                                                 if (node == null)
 | 
|---|
| 110 |                                                         version = sVal;
 | 
|---|
| 111 |                                         } else if (sName.equalsIgnoreCase("Readme")) {
 | 
|---|
| 112 |                                                 if (node == null)
 | 
|---|
| 113 |                                                         description = sVal.replaceAll("\\\\n", "<br>");
 | 
|---|
| 114 |                                         }
 | 
|---|
| 115 |                                 }
 | 
|---|
| 116 |                                 isr.close();
 | 
|---|
| 117 |                         } catch (FileNotFoundException e) {
 | 
|---|
| 118 |                         } catch (IOException e) {
 | 
|---|
| 119 |                                 e.printStackTrace();
 | 
|---|
| 120 |                         }
 | 
|---|
| 121 |                 } else {
 | 
|---|
| 122 |                         System.err.println("No config found for mod folder: "
 | 
|---|
| 123 |                                         + getLocalPath().getPath());
 | 
|---|
| 124 |                 }
 | 
|---|
| 125 |                 if (timestamp.exists()) {
 | 
|---|
| 126 |                         try {
 | 
|---|
| 127 |                                 FileInputStream fstream = new FileInputStream(timestamp);
 | 
|---|
| 128 |                                 InputStreamReader isr = new InputStreamReader(fstream);
 | 
|---|
| 129 |                                 BufferedReader br = new BufferedReader(isr);
 | 
|---|
| 130 |                                 String ts = br.readLine();
 | 
|---|
| 131 |                                 localTimestamp = Long.parseLong(ts);
 | 
|---|
| 132 |                                 isr.close();
 | 
|---|
| 133 |                         } catch (FileNotFoundException e) {
 | 
|---|
| 134 |                         } catch (IOException e) {
 | 
|---|
| 135 |                                 e.printStackTrace();
 | 
|---|
| 136 |                         }
 | 
|---|
| 137 |                 }
 | 
|---|
| 138 |         }
 | 
|---|
| 139 | 
 | 
|---|
| 140 |         /**
 | 
|---|
| 141 |          * Create a new Mod entry from the given local mod folder
 | 
|---|
| 142 |          * 
 | 
|---|
| 143 |          * @param folder
 | 
|---|
| 144 |          *            Mod folder with Mod_Info.cfg
 | 
|---|
| 145 |          */
 | 
|---|
| 146 |         public Mod(File folder) {
 | 
|---|
| 147 |                 packageNumber = Integer.parseInt(folder.getName().substring(0, 5));
 | 
|---|
| 148 |                 updateLocalData();
 | 
|---|
| 149 | 
 | 
|---|
| 150 |                 Type t = ModManager.getInstance().getTypeByName("-Local-");
 | 
|---|
| 151 |                 types.add(t);
 | 
|---|
| 152 |                 t.addEntry(this);
 | 
|---|
| 153 | 
 | 
|---|
| 154 |                 platform = ECompatiblePlatform.BOTH;
 | 
|---|
| 155 |         }
 | 
|---|
| 156 | 
 | 
|---|
| 157 |         /**
 | 
|---|
| 158 |          * @return has separate paths for win/mac/common or not
 | 
|---|
| 159 |          */
 | 
|---|
| 160 |         public boolean hasSeparatePlatformDirs() {
 | 
|---|
| 161 |                 return aeVersion >= 2;
 | 
|---|
| 162 |         }
 | 
|---|
| 163 | 
 | 
|---|
| 164 |         /**
 | 
|---|
| 165 |          * @return Path to local mod folder
 | 
|---|
| 166 |          */
 | 
|---|
| 167 |         public File getLocalPath() {
 | 
|---|
| 168 |                 final String folderStart = String.format("%05d", packageNumber);
 | 
|---|
| 169 | 
 | 
|---|
| 170 |                 if (Paths.getModsPath().exists()) {
 | 
|---|
| 171 |                         for (File f : Paths.getModsPath().listFiles(new FilenameFilter() {
 | 
|---|
| 172 |                                 @Override
 | 
|---|
| 173 |                                 public boolean accept(File d, String fn) {
 | 
|---|
| 174 |                                         return fn.startsWith(folderStart);
 | 
|---|
| 175 |                                 }
 | 
|---|
| 176 |                         })) {
 | 
|---|
| 177 |                                 return f;
 | 
|---|
| 178 |                         }
 | 
|---|
| 179 |                 }
 | 
|---|
| 180 | 
 | 
|---|
| 181 |                 return new File(Paths.getModsPath(), folderStart);
 | 
|---|
| 182 |         }
 | 
|---|
| 183 | 
 | 
|---|
| 184 |         /**
 | 
|---|
| 185 |          * @return Is there a newer version on the depot?
 | 
|---|
| 186 |          */
 | 
|---|
| 187 |         public boolean isNewerAvailable() {
 | 
|---|
| 188 |                 if (file != null)
 | 
|---|
| 189 |                         return file.getTimestamp() > localTimestamp;
 | 
|---|
| 190 |                 else
 | 
|---|
| 191 |                         return false;
 | 
|---|
| 192 |         }
 | 
|---|
| 193 | 
 | 
|---|
| 194 |         /**
 | 
|---|
| 195 |          * @return Mod exists within mods folder
 | 
|---|
| 196 |          */
 | 
|---|
| 197 |         public boolean isLocalAvailable() {
 | 
|---|
| 198 |                 return getLocalPath().exists();
 | 
|---|
| 199 |         }
 | 
|---|
| 200 | 
 | 
|---|
| 201 |         /**
 | 
|---|
| 202 |          * @return Name of mod
 | 
|---|
| 203 |          */
 | 
|---|
| 204 |         public String getName() {
 | 
|---|
| 205 |                 return name;
 | 
|---|
| 206 |         }
 | 
|---|
| 207 | 
 | 
|---|
| 208 |         /**
 | 
|---|
| 209 |          * @return the package number
 | 
|---|
| 210 |          */
 | 
|---|
| 211 |         public int getPackageNumber() {
 | 
|---|
| 212 |                 return packageNumber;
 | 
|---|
| 213 |         }
 | 
|---|
| 214 | 
 | 
|---|
| 215 |         /**
 | 
|---|
| 216 |          * @return Types of mod
 | 
|---|
| 217 |          */
 | 
|---|
| 218 |         public HashSet<Type> getTypes() {
 | 
|---|
| 219 |                 return types;
 | 
|---|
| 220 |         }
 | 
|---|
| 221 | 
 | 
|---|
| 222 |         /**
 | 
|---|
| 223 |          * @return Compatible platforms
 | 
|---|
| 224 |          */
 | 
|---|
| 225 |         public ECompatiblePlatform getPlatform() {
 | 
|---|
| 226 |                 return platform;
 | 
|---|
| 227 |         }
 | 
|---|
| 228 | 
 | 
|---|
| 229 |         /**
 | 
|---|
| 230 |          * @return Version of mod
 | 
|---|
| 231 |          */
 | 
|---|
| 232 |         public String getVersion() {
 | 
|---|
| 233 |                 return version;
 | 
|---|
| 234 |         }
 | 
|---|
| 235 | 
 | 
|---|
| 236 |         /**
 | 
|---|
| 237 |          * @return Creator of mod
 | 
|---|
| 238 |          */
 | 
|---|
| 239 |         public String getCreator() {
 | 
|---|
| 240 |                 return creator;
 | 
|---|
| 241 |         }
 | 
|---|
| 242 | 
 | 
|---|
| 243 |         /**
 | 
|---|
| 244 |          * @return Installation type of BSL files
 | 
|---|
| 245 |          */
 | 
|---|
| 246 |         public EBSLInstallType getBSLInstallType() {
 | 
|---|
| 247 |                 return bslInstallType;
 | 
|---|
| 248 |         }
 | 
|---|
| 249 | 
 | 
|---|
| 250 |         /**
 | 
|---|
| 251 |          * @return Description of mod
 | 
|---|
| 252 |          */
 | 
|---|
| 253 |         public String getDescription() {
 | 
|---|
| 254 |                 return description;
 | 
|---|
| 255 |         }
 | 
|---|
| 256 | 
 | 
|---|
| 257 |         /**
 | 
|---|
| 258 |          * @return Size of Zip file on Depot
 | 
|---|
| 259 |          */
 | 
|---|
| 260 |         public int getZipSize() {
 | 
|---|
| 261 |                 return zipSize;
 | 
|---|
| 262 |         }
 | 
|---|
| 263 | 
 | 
|---|
| 264 |         /**
 | 
|---|
| 265 |          * @return Is a mod that is always installed?
 | 
|---|
| 266 |          */
 | 
|---|
| 267 |         public boolean isMandatoryMod() {
 | 
|---|
| 268 |                 return packageNumber < 10000;
 | 
|---|
| 269 |         }
 | 
|---|
| 270 | 
 | 
|---|
| 271 |         /**
 | 
|---|
| 272 |          * @return Get the depot file entry
 | 
|---|
| 273 |          */
 | 
|---|
| 274 |         public net.oni2.aeinstaller.backend.depot.model.File getFile() {
 | 
|---|
| 275 |                 return file;
 | 
|---|
| 276 |         }
 | 
|---|
| 277 | 
 | 
|---|
| 278 |         @Override
 | 
|---|
| 279 |         public String toString() {
 | 
|---|
| 280 |                 return name;
 | 
|---|
| 281 |         }
 | 
|---|
| 282 | 
 | 
|---|
| 283 |         /**
 | 
|---|
| 284 |          * @return the conflicts
 | 
|---|
| 285 |          */
 | 
|---|
| 286 |         public HashSet<Integer> getConflicts() {
 | 
|---|
| 287 |                 return conflicts;
 | 
|---|
| 288 |         }
 | 
|---|
| 289 | 
 | 
|---|
| 290 |         /**
 | 
|---|
| 291 |          * @return the dependencies
 | 
|---|
| 292 |          */
 | 
|---|
| 293 |         public HashSet<Integer> getDependencies() {
 | 
|---|
| 294 |                 return dependencies;
 | 
|---|
| 295 |         }
 | 
|---|
| 296 | 
 | 
|---|
| 297 |         /**
 | 
|---|
| 298 |          * @return Is this mod valid on the running platform?
 | 
|---|
| 299 |          */
 | 
|---|
| 300 |         public boolean validOnPlatform() {
 | 
|---|
| 301 |                 ECompatiblePlatform plat = platform;
 | 
|---|
| 302 |                 switch (plat) {
 | 
|---|
| 303 |                         case BOTH:
 | 
|---|
| 304 |                                 return true;
 | 
|---|
| 305 |                         case MACOS:
 | 
|---|
| 306 |                                 return (Settings.getPlatform() == Platform.MACOS);
 | 
|---|
| 307 |                         case WIN:
 | 
|---|
| 308 |                                 return (Settings.getPlatform() == Platform.WIN)
 | 
|---|
| 309 |                                                 || (Settings.getPlatform() == Platform.LINUX);
 | 
|---|
| 310 |                 }
 | 
|---|
| 311 |                 return false;
 | 
|---|
| 312 |         }
 | 
|---|
| 313 | 
 | 
|---|
| 314 |         @Override
 | 
|---|
| 315 |         public int compareTo(Mod o) {
 | 
|---|
| 316 |                 return getPackageNumber() - o.getPackageNumber();
 | 
|---|
| 317 |         }
 | 
|---|
| 318 | }
 | 
|---|