ModFile.java
4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.mumfrey.liteloader.core;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.mumfrey.liteloader.resources.ModResourcePack;
/**
* Wrapper for file which represents a mod file to load with associated version information and
* metadata. Retrieve this from litemod.xml at enumeration time. We also override comparable to
* provide our own custom sorting logic based on version info.
*
* @author Adam Mummery-Smith
*/
public class ModFile extends File
{
private static final long serialVersionUID = -7952147161905688459L;
/**
* Gson parser for JSON
*/
private static Gson gson = new Gson();
/**
* True if the metadata information is parsed successfully, the mod will be added
*/
private boolean valid = false;
/**
* True if parsed from JSON, false if fallback mode using legacy version.txt
*/
private boolean json = false;
/**
* Name of the mod specified in the JSON file, this can be any string but should be the same between mod versions
*/
private String modName;
/**
* Loader version
*/
private String version;
/**
* File time stamp, used as sorting criteria when no revision information is found
*/
private long timeStamp;
/**
* Revision number from the json file
*/
private float revision = 0.0F;
/**
* True if the revision number was successfully read, used as a semaphore so that we know when revision is a valid number
*/
private boolean hasRevision = false;
/**
* Resource pack we have registered with minecraft
*/
private ModResourcePack resourcePack = null;
/**
* ALL of the parsed metadata from the file, associated with the mod later on for retrieval via the loader
*/
private HashMap<String, String> metaData = new HashMap<String, String>();
/**
* @param file
* @param strVersion
*/
public ModFile(File file, String strVersion)
{
super(file.getAbsolutePath());
this.timeStamp = this.lastModified();
this.parseVersionFile(strVersion);
}
@SuppressWarnings("unchecked")
private void parseVersionFile(String strVersionData)
{
// Assume that it's json if the file starts with a brace
if (strVersionData.trim().startsWith("{"))
{
try
{
this.metaData = ModFile.gson.fromJson(strVersionData, HashMap.class);
}
catch (JsonSyntaxException jsx)
{
LiteLoader.getLogger().warning("Error reading litemod.json in " + this.getName() + ", JSON syntax exception: " + jsx.getMessage());
return;
}
this.modName = this.metaData.get("name");
this.version = this.metaData.get("mcversion");
if (this.version == null)
{
LiteLoader.getLogger().warning("Mod in " + this.getName() + " has no loader version number reading litemod.json");
return;
}
try
{
this.revision = Float.parseFloat(this.metaData.get("revision"));
this.hasRevision = true;
}
catch (Exception ex)
{
LiteLoader.getLogger().warning("Mod in " + this.getName() + " has an invalid revision number reading litemod.json");
}
this.valid = true;
this.json = true;
}
else
{
// Legacy version.txt file
this.version = strVersionData;
this.valid = true;
}
if (this.modName == null)
{
this.modName = this.getName().replaceAll("[^a-zA-Z]", "");
}
}
public String getModName()
{
return this.modName;
}
public boolean isValid()
{
return this.valid;
}
public boolean isJson()
{
return this.json;
}
public String getVersion()
{
return this.version;
}
public float getRevision()
{
return this.revision;
}
public String getMetaValue(String metaKey, String defaultValue)
{
return this.metaData.containsKey(metaKey) ? this.metaData.get(metaKey) : defaultValue;
}
public Map<String, String> getMetaData()
{
return this.metaData;
}
/**
* Registers this file as a minecraft resource pack
*
* @param name
* @return true if the pack was added
*/
public boolean registerAsResourcePack(String name)
{
if (this.resourcePack == null)
{
this.resourcePack = new ModResourcePack(name, this);
return LiteLoader.getInstance().registerModResourcePack(this.resourcePack);
}
return false;
}
@Override
public int compareTo(File other)
{
if (other == null || !(other instanceof ModFile)) return -1;
ModFile otherMod = (ModFile)other;
// If the other object has a revision, compare revisions
if (otherMod.hasRevision)
{
return this.hasRevision && this.revision - otherMod.revision > 0 ? -1 : 1;
}
// If we have a revision and the other object doesn't, then we are higher
if (this.hasRevision)
{
return -1;
}
// Give up and use timestamp
return (int)(otherMod.timeStamp - this.timeStamp);
}
}