| 1 | package net.oni2.moddepot.model; | 
 
 
 
 
 | 2 |  | 
 
 
 
 
 | 3 | import java.util.HashSet; | 
 
 
 
 
 | 4 | import java.util.Vector; | 
 
 
 
 
 | 5 |  | 
 
 
 
 
 | 6 | import net.oni2.moddepot.DepotConfig; | 
 
 
 
 
 | 7 | import net.oni2.moddepot.DepotManager; | 
 
 
 
 
 | 8 | import net.oni2.moddepot.ECompatiblePlatform; | 
 
 
 
 
 | 9 |  | 
 
 
 
 
 | 10 | import org.json.JSONArray; | 
 
 
 
 
 | 11 | import org.json.JSONException; | 
 
 
 
 
 | 12 | import org.json.JSONObject; | 
 
 
 
 
 | 13 |  | 
 
 
 
 
 | 14 | /** | 
 
 
 
 
 | 15 | * @author Christian Illy | 
 
 
 
 
 | 16 | */ | 
 
 
 
 
 | 17 | public class NodeMod extends Node { | 
 
 
 
 
 | 18 | private Vector<File> uploads = new Vector<File>(); | 
 
 
 
 
 | 19 |  | 
 
 
 
 
 | 20 | private ECompatiblePlatform platform = null; | 
 
 
 
 
 | 21 | private String installType = null; | 
 
 
 
 
 | 22 | private HashSet<String> packageType = new HashSet<String>(); | 
 
 
 
 
 | 23 |  | 
 
 
 
 
 | 24 | private String creator = ""; | 
 
 
 
 
 | 25 | private String version = ""; | 
 
 
 
 
 | 26 | private int package_number = -1; | 
 
 
 
 
 | 27 |  | 
 
 
 
 
 | 28 | /** | 
 
 
 
 
 | 29 | * @param json | 
 
 
 
 
 | 30 | *            JSON object of Mod-node to parse | 
 
 
 
 
 | 31 | * @throws JSONException | 
 
 
 
 
 | 32 | *             On key not found / wrong type | 
 
 
 
 
 | 33 | */ | 
 
 
 
 
 | 34 | public NodeMod(JSONObject json) throws JSONException { | 
 
 
 
 
 | 35 | super(json); | 
 
 
 
 
 | 36 |  | 
 
 
 
 
 | 37 | Object uploadObj = json.get("upload"); | 
 
 
 
 
 | 38 | if (uploadObj instanceof JSONObject) { | 
 
 
 
 
 | 39 | JSONArray jUploads = ((JSONObject) uploadObj).getJSONArray("und"); | 
 
 
 
 
 | 40 | for (int i = 0; i < jUploads.length(); i++) { | 
 
 
 
 
 | 41 | NodeField_Upload up = new NodeField_Upload( | 
 
 
 
 
 | 42 | jUploads.getJSONObject(i)); | 
 
 
 
 
 | 43 | if (up.getDisplay() != 0) { | 
 
 
 
 
 | 44 | uploads.add(DepotManager.getInstance().getFile(up.getFid())); | 
 
 
 
 
 | 45 | } | 
 
 
 
 
 | 46 | } | 
 
 
 
 
 | 47 | } | 
 
 
 
 
 | 48 |  | 
 
 
 
 
 | 49 | for (Object key : json.keySet()) { | 
 
 
 
 
 | 50 | String keyS = ((String) key).toLowerCase(); | 
 
 
 
 
 | 51 | Object val = json.get(keyS); | 
 
 
 
 
 | 52 | if (keyS.startsWith("field_")) { | 
 
 
 
 
 | 53 | String fName = keyS.substring(keyS.indexOf("_") + 1); | 
 
 
 
 
 | 54 | String value = ""; | 
 
 
 
 
 | 55 | if (val instanceof JSONObject) { | 
 
 
 
 
 | 56 | value = ((JSONObject) val).getJSONArray("und") | 
 
 
 
 
 | 57 | .getJSONObject(0).getString("value"); | 
 
 
 
 
 | 58 | } | 
 
 
 
 
 | 59 | if (fName.equalsIgnoreCase("version")) | 
 
 
 
 
 | 60 | version = value; | 
 
 
 
 
 | 61 | else if (fName.equalsIgnoreCase("creator")) | 
 
 
 
 
 | 62 | creator = value; | 
 
 
 
 
 | 63 | else if (fName.equalsIgnoreCase("package_number")) { | 
 
 
 
 
 | 64 | try { | 
 
 
 
 
 | 65 | package_number = Integer.parseInt(value); | 
 
 
 
 
 | 66 | } catch (NumberFormatException e) { | 
 
 
 
 
 | 67 | } | 
 
 
 
 
 | 68 | } | 
 
 
 
 
 | 69 | } | 
 
 
 
 
 | 70 |  | 
 
 
 
 
 | 71 | if (keyS.startsWith("taxonomy_vocabulary_")) { | 
 
 
 
 
 | 72 | int vid = Integer | 
 
 
 
 
 | 73 | .parseInt(keyS.substring(keyS.lastIndexOf("_") + 1)); | 
 
 
 
 
 | 74 |  | 
 
 
 
 
 | 75 | if (val instanceof JSONObject) { | 
 
 
 
 
 | 76 | JSONArray ja = ((JSONObject) val).getJSONArray("und"); | 
 
 
 
 
 | 77 |  | 
 
 
 
 
 | 78 | if (vid == DepotManager.getInstance().vocabId_platform) { | 
 
 
 
 
 | 79 | int tid = ja.getJSONObject(0).getInt("tid"); | 
 
 
 
 
 | 80 | String validPlatform = DepotManager.getInstance() | 
 
 
 
 
 | 81 | .getTaxonomyTerm(tid).getName(); | 
 
 
 
 
 | 82 | if (validPlatform | 
 
 
 
 
 | 83 | .equalsIgnoreCase(DepotConfig.taxName_Platform_Both)) | 
 
 
 
 
 | 84 | platform = ECompatiblePlatform.BOTH; | 
 
 
 
 
 | 85 | else if (validPlatform | 
 
 
 
 
 | 86 | .equalsIgnoreCase(DepotConfig.taxName_Platform_Win)) | 
 
 
 
 
 | 87 | platform = ECompatiblePlatform.WIN; | 
 
 
 
 
 | 88 | else if (validPlatform | 
 
 
 
 
 | 89 | .equalsIgnoreCase(DepotConfig.taxName_Platform_MacOS)) | 
 
 
 
 
 | 90 | platform = ECompatiblePlatform.MACOS; | 
 
 
 
 
 | 91 | } else if (vid == DepotManager.getInstance().vocabId_type) { | 
 
 
 
 
 | 92 | for (int i = 0; i < ja.length(); i++) { | 
 
 
 
 
 | 93 | int tid = ja.getJSONObject(i).getInt("tid"); | 
 
 
 
 
 | 94 | packageType.add(DepotManager.getInstance() | 
 
 
 
 
 | 95 | .getTaxonomyTerm(tid).getName()); | 
 
 
 
 
 | 96 | } | 
 
 
 
 
 | 97 | } else if (vid == DepotManager.getInstance().vocabId_instmethod) { | 
 
 
 
 
 | 98 | int tid = ja.getJSONObject(0).getInt("tid"); | 
 
 
 
 
 | 99 | installType = DepotManager.getInstance() | 
 
 
 
 
 | 100 | .getTaxonomyTerm(tid).getName(); | 
 
 
 
 
 | 101 | } | 
 
 
 
 
 | 102 | } | 
 
 
 
 
 | 103 |  | 
 
 
 
 
 | 104 | } | 
 
 
 
 
 | 105 | } | 
 
 
 
 
 | 106 | } | 
 
 
 
 
 | 107 |  | 
 
 
 
 
 | 108 | /** | 
 
 
 
 
 | 109 | * @return the uploads | 
 
 
 
 
 | 110 | */ | 
 
 
 
 
 | 111 | public Vector<File> getUploads() { | 
 
 
 
 
 | 112 | return uploads; | 
 
 
 
 
 | 113 | } | 
 
 
 
 
 | 114 |  | 
 
 
 
 
 | 115 | /** | 
 
 
 
 
 | 116 | * @return Types | 
 
 
 
 
 | 117 | */ | 
 
 
 
 
 | 118 | public HashSet<String> getTypes() { | 
 
 
 
 
 | 119 | return packageType; | 
 
 
 
 
 | 120 | } | 
 
 
 
 
 | 121 |  | 
 
 
 
 
 | 122 | /** | 
 
 
 
 
 | 123 | * @return Install method | 
 
 
 
 
 | 124 | */ | 
 
 
 
 
 | 125 | public String getInstallMethod() { | 
 
 
 
 
 | 126 | return installType; | 
 
 
 
 
 | 127 | } | 
 
 
 
 
 | 128 |  | 
 
 
 
 
 | 129 | /** | 
 
 
 
 
 | 130 | * @return Compatible platform | 
 
 
 
 
 | 131 | */ | 
 
 
 
 
 | 132 | public ECompatiblePlatform getPlatform() { | 
 
 
 
 
 | 133 | return platform; | 
 
 
 
 
 | 134 | } | 
 
 
 
 
 | 135 |  | 
 
 
 
 
 | 136 | /** | 
 
 
 
 
 | 137 | * @return Creator of mod | 
 
 
 
 
 | 138 | */ | 
 
 
 
 
 | 139 | public String getCreator() { | 
 
 
 
 
 | 140 | return creator; | 
 
 
 
 
 | 141 | } | 
 
 
 
 
 | 142 |  | 
 
 
 
 
 | 143 | /** | 
 
 
 
 
 | 144 | * @return Version of mod | 
 
 
 
 
 | 145 | */ | 
 
 
 
 
 | 146 | public String getVersion() { | 
 
 
 
 
 | 147 | return version; | 
 
 
 
 
 | 148 | } | 
 
 
 
 
 | 149 |  | 
 
 
 
 
 | 150 | /** | 
 
 
 
 
 | 151 | * @return Package number | 
 
 
 
 
 | 152 | */ | 
 
 
 
 
 | 153 | public int getPackageNumber() { | 
 
 
 
 
 | 154 | return package_number; | 
 
 
 
 
 | 155 | } | 
 
 
 
 
 | 156 |  | 
 
 
 
 
 | 157 | /** | 
 
 
 
 
 | 158 | * @return Is this mod a tool? | 
 
 
 
 
 | 159 | */ | 
 
 
 
 
 | 160 | public boolean isTool() { | 
 
 
 
 
 | 161 | HashSet<String> types = getTypes(); | 
 
 
 
 
 | 162 | for (String s : DepotConfig.getTaxonomyName_ModType_Tool()) { | 
 
 
 
 
 | 163 | for (String type : types) | 
 
 
 
 
 | 164 | if (type.equalsIgnoreCase(s)) | 
 
 
 
 
 | 165 | return true; | 
 
 
 
 
 | 166 | } | 
 
 
 
 
 | 167 | return false; | 
 
 
 
 
 | 168 | } | 
 
 
 
 
 | 169 | } |