Type parsing work
This commit is contained in:
@@ -1,9 +1,15 @@
|
|||||||
package io.anuke.mindustry.mod;
|
package io.anuke.mindustry.mod;
|
||||||
|
|
||||||
|
import io.anuke.arc.collection.*;
|
||||||
|
import io.anuke.arc.util.serialization.*;
|
||||||
import io.anuke.mindustry.game.*;
|
import io.anuke.mindustry.game.*;
|
||||||
import io.anuke.mindustry.type.*;
|
import io.anuke.mindustry.type.*;
|
||||||
|
|
||||||
public class ContentParser{
|
public class ContentParser{
|
||||||
|
private Json parser = new Json();
|
||||||
|
private ObjectMap<ContentType, TypeParser<?>> parsers = ObjectMap.of(
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses content from a json file.
|
* Parses content from a json file.
|
||||||
@@ -13,7 +19,11 @@ public class ContentParser{
|
|||||||
* @return the content that was parsed
|
* @return the content that was parsed
|
||||||
*/
|
*/
|
||||||
public Content parse(String name, String json, ContentType type) throws Exception{
|
public Content parse(String name, String json, ContentType type) throws Exception{
|
||||||
|
JsonValue value = parser.fromJson(null, json);
|
||||||
|
if(!parsers.containsKey(type)){
|
||||||
|
throw new SerializationException("No parsers for content type '" + type + "'");
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return parsers.get(type).parse(name, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import io.anuke.arc.graphics.g2d.*;
|
|||||||
import io.anuke.arc.util.*;
|
import io.anuke.arc.util.*;
|
||||||
import io.anuke.arc.util.io.*;
|
import io.anuke.arc.util.io.*;
|
||||||
import io.anuke.arc.util.serialization.*;
|
import io.anuke.arc.util.serialization.*;
|
||||||
|
import io.anuke.mindustry.game.*;
|
||||||
import io.anuke.mindustry.type.*;
|
import io.anuke.mindustry.type.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -75,7 +76,7 @@ public class Mods{
|
|||||||
try(InputStream stream = file.read()){
|
try(InputStream stream = file.read()){
|
||||||
byte[] bytes = Streams.copyStreamToByteArray(stream, Math.max((int)file.length(), 512));
|
byte[] bytes = Streams.copyStreamToByteArray(stream, Math.max((int)file.length(), 512));
|
||||||
Pixmap pixmap = new Pixmap(bytes, 0, bytes.length);
|
Pixmap pixmap = new Pixmap(bytes, 0, bytes.length);
|
||||||
packer.pack(mod.meta.name + ":" + file.nameWithoutExtension(), pixmap);
|
packer.pack(mod.name + ":" + file.nameWithoutExtension(), pixmap);
|
||||||
pixmap.dispose();
|
pixmap.dispose();
|
||||||
packed ++;
|
packed ++;
|
||||||
total ++;
|
total ++;
|
||||||
@@ -90,6 +91,8 @@ public class Mods{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//only pack if there's something to be packed
|
||||||
|
//TODO is disposing necessary/safe?
|
||||||
if(total > 0){
|
if(total > 0){
|
||||||
Core.app.post(() -> {
|
Core.app.post(() -> {
|
||||||
TextureFilter filter = Core.settings.getBool("linear") ? TextureFilter.Linear : TextureFilter.Nearest;
|
TextureFilter filter = Core.settings.getBool("linear") ? TextureFilter.Linear : TextureFilter.Nearest;
|
||||||
@@ -180,7 +183,8 @@ public class Mods{
|
|||||||
for(FileHandle file : folder.list()){
|
for(FileHandle file : folder.list()){
|
||||||
if(file.extension().equals("json")){
|
if(file.extension().equals("json")){
|
||||||
try{
|
try{
|
||||||
parser.parse(file.nameWithoutExtension(), file.readString(), type);
|
Content loaded = parser.parse(file.nameWithoutExtension(), file.readString(), type);
|
||||||
|
Log.info("[{0}] Loaded '{1}'", loaded, mod.meta.name);
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
throw new RuntimeException("Failed to parse content file '" + file + "' for mod '" + mod.meta.name + "'.", e);
|
throw new RuntimeException("Failed to parse content file '" + file + "' for mod '" + mod.meta.name + "'.", e);
|
||||||
}
|
}
|
||||||
@@ -243,8 +247,11 @@ public class Mods{
|
|||||||
public final FileHandle root;
|
public final FileHandle root;
|
||||||
/** The mod's main class; may be null. */
|
/** The mod's main class; may be null. */
|
||||||
public final @Nullable Mod mod;
|
public final @Nullable Mod mod;
|
||||||
|
/** Internal mod name. Used for textures. */
|
||||||
|
public final String name;
|
||||||
/** This mod's metadata. */
|
/** This mod's metadata. */
|
||||||
public final ModMeta meta;
|
public final ModMeta meta;
|
||||||
|
|
||||||
//TODO implement
|
//TODO implement
|
||||||
protected boolean enabled;
|
protected boolean enabled;
|
||||||
|
|
||||||
@@ -253,6 +260,7 @@ public class Mods{
|
|||||||
this.file = file;
|
this.file = file;
|
||||||
this.mod = mod;
|
this.mod = mod;
|
||||||
this.meta = meta;
|
this.meta = meta;
|
||||||
|
this.name = Strings.camelize(meta.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
8
core/src/io/anuke/mindustry/mod/TypeParser.java
Normal file
8
core/src/io/anuke/mindustry/mod/TypeParser.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package io.anuke.mindustry.mod;
|
||||||
|
|
||||||
|
import io.anuke.arc.util.serialization.*;
|
||||||
|
import io.anuke.mindustry.game.*;
|
||||||
|
|
||||||
|
public abstract class TypeParser<T extends Content>{
|
||||||
|
public abstract T parse(String name, JsonValue value);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user