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