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