|
1 | 1 | package ore.models.project.io |
2 | 2 |
|
3 | 3 | import scala.language.higherKinds |
4 | | - |
5 | 4 | import java.io.BufferedReader |
6 | | -import com.google.gson.stream.JsonReader |
7 | 5 |
|
8 | 6 | import scala.collection.mutable.ArrayBuffer |
9 | 7 | import scala.jdk.CollectionConverters._ |
| 8 | +import scala.jdk.OptionConverters._ |
10 | 9 | import scala.util.control.NonFatal |
11 | | - |
12 | 10 | import ore.data.project.Dependency |
13 | 11 | import ore.db.{DbRef, Model, ModelService} |
14 | 12 | import ore.models.project.{TagColor, Version, VersionTag} |
15 | | - |
16 | 13 | import org.spongepowered.plugin.meta.McModInfo |
| 14 | +import org.spongepowered.plugin.metadata.builtin.MetadataParser |
| 15 | +import org.spongepowered.plugin.metadata.model.PluginDependency |
17 | 16 |
|
18 | 17 | /** |
19 | 18 | * The metadata within a [[PluginFile]] |
@@ -155,26 +154,26 @@ object McModInfoHandler extends FileTypeHandler("mcmod.info") { |
155 | 154 | else { |
156 | 155 | val metadata = info.head |
157 | 156 |
|
158 | | - if (metadata.getId != null) |
159 | | - dataValues += StringDataValue("id", metadata.getId) |
| 157 | + if (metadata.id != null) |
| 158 | + dataValues += StringDataValue("id", metadata.id) |
160 | 159 |
|
161 | | - if (metadata.getVersion != null) |
162 | | - dataValues += StringDataValue("version", metadata.getVersion) |
| 160 | + if (metadata.version != null) |
| 161 | + dataValues += StringDataValue("version", metadata.version) |
163 | 162 |
|
164 | | - if (metadata.getName != null) |
165 | | - dataValues += StringDataValue("name", metadata.getName) |
| 163 | + if (metadata.name != null) |
| 164 | + dataValues += StringDataValue("name", metadata.name) |
166 | 165 |
|
167 | | - if (metadata.getDescription != null) |
168 | | - dataValues += StringDataValue("description", metadata.getDescription) |
| 166 | + if (metadata.description != null) |
| 167 | + dataValues += StringDataValue("description", metadata.description) |
169 | 168 |
|
170 | | - if (metadata.getUrl != null) |
171 | | - dataValues += StringDataValue("url", metadata.getUrl) |
| 169 | + if (metadata.url != null) |
| 170 | + dataValues += StringDataValue("url", metadata.url) |
172 | 171 |
|
173 | | - if (metadata.getAuthors != null) |
174 | | - dataValues += StringListValue("authors", metadata.getAuthors.asScala.toSeq) |
| 172 | + if (metadata.authors != null) |
| 173 | + dataValues += StringListValue("authors", metadata.authors.asScala.toSeq) |
175 | 174 |
|
176 | | - if (metadata.getDependencies != null) { |
177 | | - val dependencies = metadata.getDependencies.asScala.map(p => Dependency(p.getId, Option(p.getVersion))).toSeq |
| 175 | + if (metadata.dependencies != null) { |
| 176 | + val dependencies = metadata.dependencies.asScala.map(p => Dependency(p.id, Option(p.version()))).toSeq |
178 | 177 | dataValues += DependencyDataValue("dependencies", dependencies) |
179 | 178 | } |
180 | 179 |
|
@@ -209,98 +208,35 @@ object ModTomlHandler extends FileTypeHandler("mod.toml") { |
209 | 208 | Nil |
210 | 209 | } |
211 | 210 |
|
212 | | -object SpongeJsonHandler extends FileTypeHandler("META-INF/plugins.json") { |
213 | | - |
214 | | - def readDependencies(in: JsonReader) = { |
215 | | - val deps = new ArrayBuffer[Dependency] |
216 | | - in.beginArray() |
217 | | - while (in.hasNext) { |
218 | | - in.beginObject() |
219 | | - var dep = Dependency(null, None) |
220 | | - while (in.hasNext) { |
221 | | - in.nextName() match { |
222 | | - case "id" => dep = dep.copy(pluginId = in.nextString()) |
223 | | - case "version" => dep = dep.copy(version = Option(in.nextString())) |
224 | | - case _ => in.skipValue() |
225 | | - } |
226 | | - } |
227 | | - deps += dep |
228 | | - in.endObject() |
229 | | - } |
230 | | - in.endArray() |
231 | | - deps.toSeq |
232 | | - } |
233 | | - |
234 | | - def readAuthors(in: JsonReader) = { |
235 | | - val authors = new ArrayBuffer[String] |
236 | | - in.beginArray() |
237 | | - while (in.hasNext) { |
238 | | - in.beginObject() |
239 | | - while (in.hasNext) { |
240 | | - in.nextName() match { |
241 | | - case "name" => authors += in.nextString() |
242 | | - case _ => in.skipValue() |
243 | | - } |
244 | | - } |
245 | | - in.endObject() |
246 | | - } |
247 | | - in.endArray() |
248 | | - authors.toSeq |
249 | | - } |
250 | | - |
251 | | - def readDataValue(dvs: ArrayBuffer[DataValue], in: JsonReader) = { |
252 | | - while (in.hasNext) { |
253 | | - in.nextName() match { |
254 | | - case "id" => dvs += StringDataValue("id", in.nextString()); |
255 | | - case "version" => dvs += StringDataValue("version", in.nextString()); |
256 | | - case "name" => dvs += StringDataValue("name", in.nextString()); |
257 | | - case "description" => dvs += StringDataValue("description", in.nextString()); |
258 | | - case "contributors" => dvs += StringListValue("authors", readAuthors(in)); |
259 | | - case "dependencies" => dvs += DependencyDataValue("dependencies", readDependencies(in)); |
260 | | - // case "links" => |
261 | | - // case "main-class" => |
262 | | - // case "loader" => |
263 | | - case _ => in.skipValue() // ignored |
264 | | - } |
265 | | - |
266 | | - } |
267 | | - } |
268 | | - |
269 | | - def readDataValues(dvs: ArrayBuffer[DataValue], in: JsonReader): Unit = { |
270 | | - var first = true; |
271 | | - in.beginArray() |
272 | | - while (in.hasNext) { |
273 | | - if (first) { |
274 | | - in.beginObject() |
275 | | - readDataValue(dvs, in) |
276 | | - first = false; |
277 | | - in.endObject() |
278 | | - } else { |
279 | | - in.skipValue() // cannot handle multiple plugins for now |
280 | | - } |
281 | | - } |
282 | | - in.endArray() |
283 | | - } |
| 211 | +object SpongeJsonHandler extends FileTypeHandler("META-INF/sponge_plugins.json") { |
284 | 212 |
|
285 | 213 | override def getData(bufferedReader: BufferedReader): Seq[DataValue] = { |
286 | | - val dataValues = new ArrayBuffer[DataValue] |
287 | 214 | try { |
288 | | - val reader = new JsonReader(bufferedReader) |
289 | | - reader.beginObject() |
290 | | - try { |
291 | | - if (reader.hasNext) { |
292 | | - if (reader.nextName().equals("plugins")) { |
293 | | - readDataValues(dataValues, reader) |
294 | | - } |
295 | | - } |
296 | | - } finally { |
297 | | - reader.endObject() |
298 | | - } |
299 | | - dataValues.toSeq |
| 215 | + val metadata = MetadataParser.read(bufferedReader) |
| 216 | + val firstPlugin = metadata.metadata.asScala.head |
| 217 | + Seq[DataValue]( |
| 218 | + StringDataValue("id", firstPlugin.id), |
| 219 | + StringDataValue("version", firstPlugin.version.toString), |
| 220 | + StringListValue("authors", firstPlugin.contributors.asScala.map(_.name).toSeq), |
| 221 | + DependencyDataValue("dependencies", readDependencies(firstPlugin.dependencies.asScala)) |
| 222 | + ) ++ Seq( |
| 223 | + firstPlugin.name.toScala.map(v => StringDataValue("name", v)), |
| 224 | + firstPlugin.description.toScala.map(v => StringDataValue("description", v)) |
| 225 | + ).flatten |
300 | 226 | } catch { |
301 | | - case NonFatal(e) => |
| 227 | + case NonFatal(e) => { |
302 | 228 | e.printStackTrace() |
303 | 229 | Nil |
| 230 | + } |
304 | 231 | } |
305 | 232 | } |
| 233 | + |
| 234 | + def readDependencies(in: Iterable[PluginDependency]): Seq[Dependency] = |
| 235 | + in.map { dep => |
| 236 | + Dependency( |
| 237 | + dep.id, |
| 238 | + Option.when(dep.version.hasRestrictions || dep.version.getRecommendedVersion != null)(dep.version.toString) |
| 239 | + ) |
| 240 | + }.toSeq |
| 241 | + |
306 | 242 | } |
0 commit comments