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