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