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