[598] | 1 | package net.oni2.aeinstaller.backend.mods;
|
---|
| 2 |
|
---|
[600] | 3 | import java.io.BufferedReader;
|
---|
[598] | 4 | import java.io.File;
|
---|
[600] | 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;
|
---|
[637] | 10 | import java.net.URI;
|
---|
| 11 | import java.net.URISyntaxException;
|
---|
[600] | 12 | import java.util.HashSet;
|
---|
[598] | 13 |
|
---|
[600] | 14 | import net.oni2.aeinstaller.backend.Paths;
|
---|
| 15 | import net.oni2.aeinstaller.backend.Settings;
|
---|
| 16 | import net.oni2.aeinstaller.backend.Settings.Platform;
|
---|
[604] | 17 | import net.oni2.aeinstaller.backend.depot.DepotConfig;
|
---|
[600] | 18 | import net.oni2.aeinstaller.backend.depot.DepotManager;
|
---|
| 19 | import net.oni2.aeinstaller.backend.depot.model.NodeMod;
|
---|
| 20 | import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
|
---|
| 21 |
|
---|
[598] | 22 | /**
|
---|
| 23 | * @author Christian Illy
|
---|
| 24 | */
|
---|
| 25 | public class Mod implements Comparable<Mod> {
|
---|
[600] | 26 | private String name = "";
|
---|
| 27 | private int packageNumber = 0;
|
---|
[598] | 28 |
|
---|
[600] | 29 | private HashSet<Type> types = new HashSet<Type>();
|
---|
[623] | 30 | private boolean tool = false;
|
---|
[600] | 31 | private ECompatiblePlatform platform = null;
|
---|
| 32 | private String version = "";
|
---|
[639] | 33 | private String submitter = "";
|
---|
[600] | 34 | private String creator = "";
|
---|
[608] | 35 | private EBSLInstallType bslInstallType = EBSLInstallType.NORMAL;
|
---|
[600] | 36 | private String description = "";
|
---|
| 37 | private double aeVersion = 0;
|
---|
| 38 | private int zipSize = 0;
|
---|
| 39 | private NodeMod node = null;
|
---|
| 40 | private net.oni2.aeinstaller.backend.depot.model.File file = null;
|
---|
| 41 |
|
---|
[623] | 42 | private File exeFile = null;
|
---|
| 43 | private File iconFile = null;
|
---|
| 44 | private String workingDir = "Base";
|
---|
| 45 |
|
---|
[608] | 46 | private HashSet<Integer> incompatibilities = new HashSet<Integer>();
|
---|
[602] | 47 | private HashSet<Integer> dependencies = new HashSet<Integer>();
|
---|
[623] | 48 | private HashSet<Integer> unlockLevel = new HashSet<Integer>();
|
---|
[602] | 49 |
|
---|
[600] | 50 | private long localTimestamp = 0;
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Create a new Mod entry from a given Mod-Node
|
---|
| 54 | *
|
---|
| 55 | * @param nm
|
---|
| 56 | * Mod-Node
|
---|
| 57 | */
|
---|
| 58 | public Mod(NodeMod nm) {
|
---|
| 59 | node = nm;
|
---|
| 60 | name = nm.getTitle();
|
---|
| 61 | packageNumber = nm.getPackageNumber();
|
---|
[608] | 62 | platform = nm.getPlatform();
|
---|
[623] | 63 | tool = nm.isTool();
|
---|
[600] | 64 | for (TaxonomyTerm tt : nm.getTypes()) {
|
---|
| 65 | Type t = ModManager.getInstance().getTypeByName(tt.getName());
|
---|
| 66 | types.add(t);
|
---|
[623] | 67 | if (!tool && !isMandatoryMod() && isValidOnPlatform())
|
---|
[602] | 68 | t.addEntry(this);
|
---|
[600] | 69 | }
|
---|
| 70 | version = nm.getVersion();
|
---|
[639] | 71 | submitter = nm.getName();
|
---|
[600] | 72 | creator = nm.getCreator();
|
---|
| 73 | if (nm.getBody() != null)
|
---|
| 74 | description = nm.getBody().getSafe_value();
|
---|
| 75 | file = DepotManager.getInstance().getFile(
|
---|
| 76 | nm.getUploads().firstElement().getFid());
|
---|
| 77 | zipSize = file.getFilesize();
|
---|
| 78 |
|
---|
| 79 | if (isLocalAvailable())
|
---|
| 80 | updateLocalData();
|
---|
[598] | 81 | }
|
---|
| 82 |
|
---|
[600] | 83 | /**
|
---|
| 84 | * Update information for local package existence
|
---|
| 85 | */
|
---|
| 86 | public void updateLocalData() {
|
---|
| 87 | File config = new File(getLocalPath(), "Mod_Info.cfg");
|
---|
[604] | 88 | File aeicfg = new File(getLocalPath(), "aei.cfg");
|
---|
[623] | 89 | File plain = new File(getLocalPath(), "plain");
|
---|
[600] | 90 | if (config.exists()) {
|
---|
[636] | 91 | Mod_Info mi = new Mod_Info(config, packageNumber);
|
---|
| 92 |
|
---|
| 93 | aeVersion = mi.getAeVersion();
|
---|
| 94 | bslInstallType = mi.getBslInstallType();
|
---|
| 95 | if (node == null) {
|
---|
| 96 | name = mi.getName();
|
---|
| 97 | creator = mi.getCreator();
|
---|
| 98 | version = mi.getVersion();
|
---|
| 99 | description = mi.getDescription();
|
---|
[600] | 100 | }
|
---|
[636] | 101 |
|
---|
| 102 | dependencies = mi.getDependencies();
|
---|
| 103 | incompatibilities = mi.getIncompatibilities();
|
---|
| 104 | unlockLevel = mi.getUnlockLevel();
|
---|
| 105 |
|
---|
| 106 | exeFile = mi.getExeFile();
|
---|
| 107 | workingDir = mi.getWorkingDir();
|
---|
| 108 | iconFile = mi.getIconFile();
|
---|
[600] | 109 | } else {
|
---|
| 110 | System.err.println("No config found for mod folder: "
|
---|
| 111 | + getLocalPath().getPath());
|
---|
| 112 | }
|
---|
[604] | 113 | if (aeicfg.exists()) {
|
---|
[600] | 114 | try {
|
---|
[604] | 115 | FileInputStream fstream = new FileInputStream(aeicfg);
|
---|
[600] | 116 | InputStreamReader isr = new InputStreamReader(fstream);
|
---|
| 117 | BufferedReader br = new BufferedReader(isr);
|
---|
[604] | 118 | String strLine;
|
---|
| 119 | while ((strLine = br.readLine()) != null) {
|
---|
| 120 | if (strLine.indexOf("->") < 1)
|
---|
| 121 | continue;
|
---|
| 122 | if (strLine.indexOf("//") >= 0)
|
---|
| 123 | strLine = strLine.substring(0, strLine.indexOf("//"));
|
---|
| 124 | String[] split = strLine.split("->", 2);
|
---|
| 125 | String sName = split[0].trim();
|
---|
| 126 | String sVal = split[1].trim();
|
---|
| 127 | if (sName.equalsIgnoreCase("Timestamp")) {
|
---|
| 128 | localTimestamp = Long.parseLong(sVal);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
[600] | 131 | isr.close();
|
---|
| 132 | } catch (FileNotFoundException e) {
|
---|
| 133 | } catch (IOException e) {
|
---|
| 134 | e.printStackTrace();
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
[623] | 137 | if (node == null)
|
---|
| 138 | tool = plain.exists();
|
---|
[600] | 139 | }
|
---|
| 140 |
|
---|
| 141 | /**
|
---|
| 142 | * Create a new Mod entry from the given local mod folder
|
---|
| 143 | *
|
---|
| 144 | * @param folder
|
---|
| 145 | * Mod folder with Mod_Info.cfg
|
---|
| 146 | */
|
---|
| 147 | public Mod(File folder) {
|
---|
| 148 | packageNumber = Integer.parseInt(folder.getName().substring(0, 5));
|
---|
| 149 | updateLocalData();
|
---|
| 150 |
|
---|
| 151 | platform = ECompatiblePlatform.BOTH;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * @return has separate paths for win/mac/common or not
|
---|
| 156 | */
|
---|
[598] | 157 | public boolean hasSeparatePlatformDirs() {
|
---|
| 158 | return aeVersion >= 2;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[631] | 161 | private String getSanitizedPathName() {
|
---|
| 162 | return name.replaceAll("[^a-zA-Z0-9_.-]", "_");
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[600] | 165 | /**
|
---|
| 166 | * @return Path to local mod folder
|
---|
| 167 | */
|
---|
[598] | 168 | public File getLocalPath() {
|
---|
[600] | 169 | final String folderStart = String.format("%05d", packageNumber);
|
---|
| 170 |
|
---|
| 171 | if (Paths.getModsPath().exists()) {
|
---|
| 172 | for (File f : Paths.getModsPath().listFiles(new FilenameFilter() {
|
---|
| 173 | @Override
|
---|
| 174 | public boolean accept(File d, String fn) {
|
---|
| 175 | return fn.startsWith(folderStart);
|
---|
| 176 | }
|
---|
| 177 | })) {
|
---|
| 178 | return f;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[631] | 182 | return new File(Paths.getModsPath(), folderStart
|
---|
| 183 | + getSanitizedPathName());
|
---|
[598] | 184 | }
|
---|
[600] | 185 |
|
---|
| 186 | /**
|
---|
| 187 | * @return Is there a newer version on the depot?
|
---|
| 188 | */
|
---|
| 189 | public boolean isNewerAvailable() {
|
---|
[603] | 190 | if (file != null)
|
---|
| 191 | return file.getTimestamp() > localTimestamp;
|
---|
[600] | 192 | else
|
---|
| 193 | return false;
|
---|
[598] | 194 | }
|
---|
[600] | 195 |
|
---|
| 196 | /**
|
---|
| 197 | * @return Mod exists within mods folder
|
---|
| 198 | */
|
---|
| 199 | public boolean isLocalAvailable() {
|
---|
| 200 | return getLocalPath().exists();
|
---|
[598] | 201 | }
|
---|
[600] | 202 |
|
---|
| 203 | /**
|
---|
[631] | 204 | * @return Is mod installed?
|
---|
| 205 | */
|
---|
| 206 | public boolean isInstalled() {
|
---|
| 207 | return ModManager.getInstance().isModInstalled(this);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /**
|
---|
[600] | 211 | * @return Name of mod
|
---|
| 212 | */
|
---|
| 213 | public String getName() {
|
---|
| 214 | return name;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | /**
|
---|
| 218 | * @return the package number
|
---|
| 219 | */
|
---|
[598] | 220 | public int getPackageNumber() {
|
---|
| 221 | return packageNumber;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[600] | 224 | /**
|
---|
[604] | 225 | * @return the package number as 5 digit string
|
---|
| 226 | */
|
---|
| 227 | public String getPackageNumberString() {
|
---|
| 228 | return String.format("%05d", packageNumber);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
[600] | 232 | * @return Types of mod
|
---|
| 233 | */
|
---|
| 234 | public HashSet<Type> getTypes() {
|
---|
| 235 | return types;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | /**
|
---|
[623] | 239 | * @return Is this mod actually a tool?
|
---|
| 240 | */
|
---|
| 241 | public boolean isTool() {
|
---|
| 242 | return tool;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /**
|
---|
[600] | 246 | * @return Compatible platforms
|
---|
| 247 | */
|
---|
| 248 | public ECompatiblePlatform getPlatform() {
|
---|
| 249 | return platform;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | /**
|
---|
| 253 | * @return Version of mod
|
---|
| 254 | */
|
---|
| 255 | public String getVersion() {
|
---|
| 256 | return version;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | /**
|
---|
[639] | 260 | * @return Submitter of mod
|
---|
| 261 | */
|
---|
| 262 | public String getSubmitter() {
|
---|
| 263 | return submitter;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /**
|
---|
[600] | 267 | * @return Creator of mod
|
---|
| 268 | */
|
---|
| 269 | public String getCreator() {
|
---|
| 270 | return creator;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /**
|
---|
| 274 | * @return Installation type of BSL files
|
---|
| 275 | */
|
---|
| 276 | public EBSLInstallType getBSLInstallType() {
|
---|
| 277 | return bslInstallType;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /**
|
---|
| 281 | * @return Description of mod
|
---|
| 282 | */
|
---|
| 283 | public String getDescription() {
|
---|
| 284 | return description;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | /**
|
---|
| 288 | * @return Size of Zip file on Depot
|
---|
| 289 | */
|
---|
| 290 | public int getZipSize() {
|
---|
| 291 | return zipSize;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | /**
|
---|
| 295 | * @return Is a mod that is always installed?
|
---|
| 296 | */
|
---|
[602] | 297 | public boolean isMandatoryMod() {
|
---|
[604] | 298 | return packageNumber < DepotConfig.getMandatoryLimit();
|
---|
[600] | 299 | }
|
---|
| 300 |
|
---|
| 301 | /**
|
---|
| 302 | * @return Get the depot file entry
|
---|
| 303 | */
|
---|
| 304 | public net.oni2.aeinstaller.backend.depot.model.File getFile() {
|
---|
| 305 | return file;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[637] | 308 | /**
|
---|
| 309 | * @return Depot page URI
|
---|
| 310 | */
|
---|
| 311 | public URI getUrl() {
|
---|
| 312 | if (node == null)
|
---|
| 313 | return null;
|
---|
| 314 | if (node.getPath() == null)
|
---|
| 315 | return null;
|
---|
| 316 | URI res = null;
|
---|
| 317 | try {
|
---|
| 318 | res = new URI(node.getPath());
|
---|
| 319 | } catch (URISyntaxException e) {
|
---|
| 320 | e.printStackTrace();
|
---|
| 321 | }
|
---|
| 322 | return res;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[598] | 325 | @Override
|
---|
[600] | 326 | public String toString() {
|
---|
| 327 | return name;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | /**
|
---|
[608] | 331 | * @return the incompabitilities
|
---|
[602] | 332 | */
|
---|
[608] | 333 | public HashSet<Integer> getIncompabitilities() {
|
---|
| 334 | return incompatibilities;
|
---|
[602] | 335 | }
|
---|
| 336 |
|
---|
| 337 | /**
|
---|
| 338 | * @return the dependencies
|
---|
| 339 | */
|
---|
| 340 | public HashSet<Integer> getDependencies() {
|
---|
| 341 | return dependencies;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | /**
|
---|
[623] | 345 | * @return the levels this mod will unlock
|
---|
| 346 | */
|
---|
| 347 | public HashSet<Integer> getUnlockLevels() {
|
---|
| 348 | return unlockLevel;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | /**
|
---|
| 352 | * @return Executable name of this tool
|
---|
| 353 | */
|
---|
| 354 | public File getExeFile() {
|
---|
| 355 | return exeFile;
|
---|
| 356 | }
|
---|
[631] | 357 |
|
---|
[623] | 358 | /**
|
---|
| 359 | * @return Icon file of this tool
|
---|
| 360 | */
|
---|
| 361 | public File getIconFile() {
|
---|
| 362 | return iconFile;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | /**
|
---|
| 366 | * @return Working directory of this tool
|
---|
| 367 | */
|
---|
| 368 | public File getWorkingDir() {
|
---|
| 369 | if (workingDir.equalsIgnoreCase("Exe")) {
|
---|
| 370 | if (exeFile != null)
|
---|
| 371 | return exeFile.getParentFile();
|
---|
| 372 | else
|
---|
| 373 | return Paths.getEditionGDF();
|
---|
| 374 | } else if (workingDir.equalsIgnoreCase("GDF"))
|
---|
| 375 | return Paths.getEditionGDF();
|
---|
| 376 | else
|
---|
| 377 | return Paths.getEditionBasePath();
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | /**
|
---|
[600] | 381 | * @return Is this mod valid on the running platform?
|
---|
| 382 | */
|
---|
[608] | 383 | public boolean isValidOnPlatform() {
|
---|
| 384 | switch (platform) {
|
---|
[600] | 385 | case BOTH:
|
---|
| 386 | return true;
|
---|
| 387 | case MACOS:
|
---|
| 388 | return (Settings.getPlatform() == Platform.MACOS);
|
---|
| 389 | case WIN:
|
---|
| 390 | return (Settings.getPlatform() == Platform.WIN)
|
---|
| 391 | || (Settings.getPlatform() == Platform.LINUX);
|
---|
| 392 | }
|
---|
| 393 | return false;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | @Override
|
---|
[598] | 397 | public int compareTo(Mod o) {
|
---|
| 398 | return getPackageNumber() - o.getPackageNumber();
|
---|
| 399 | }
|
---|
[639] | 400 |
|
---|
[598] | 401 | }
|
---|