Better organization

This commit is contained in:
Anuken
2019-10-21 21:22:35 -04:00
parent bb593af463
commit d7ebbbf2b6
102 changed files with 206 additions and 170 deletions

View File

@@ -16,6 +16,7 @@ import io.anuke.arc.util.serialization.Json.*;
import io.anuke.mindustry.*;
import io.anuke.mindustry.content.*;
import io.anuke.mindustry.content.TechTree.*;
import io.anuke.mindustry.ctype.UnlockableContent;
import io.anuke.mindustry.entities.Effects.*;
import io.anuke.mindustry.entities.bullet.*;
import io.anuke.mindustry.entities.type.*;
@@ -72,7 +73,7 @@ public class ContentParser{
private Array<Runnable> reads = new Array<>();
private Array<Runnable> postreads = new Array<>();
private LoadedMod currentMod;
private Content currentContent;
private io.anuke.mindustry.ctype.Content currentContent;
private Json parser = new Json(){
@Override
@@ -92,7 +93,7 @@ public class ContentParser{
}
}
if(Content.class.isAssignableFrom(type)){
if(io.anuke.mindustry.ctype.Content.class.isAssignableFrom(type)){
ContentType ctype = contentTypes.getThrow(type, () -> new IllegalArgumentException("No content type for class: " + type.getSimpleName()));
String prefix = currentMod != null ? currentMod.name + "-" : "";
T one = (T)Vars.content.getByName(ctype, prefix + jsonData.asString());
@@ -212,14 +213,14 @@ public class ContentParser{
ContentType.zone, parser(ContentType.zone, Zone::new)
);
private <T extends Content> T find(ContentType type, String name){
Content c = Vars.content.getByName(type, name);
private <T extends io.anuke.mindustry.ctype.Content> T find(ContentType type, String name){
io.anuke.mindustry.ctype.Content c = Vars.content.getByName(type, name);
if(c == null) c = Vars.content.getByName(type, currentMod.name + "-" + name);
if(c == null) throw new IllegalArgumentException("No " + type + " found with name '" + name + "'");
return (T)c;
}
private <T extends Content> TypeParser<T> parser(ContentType type, Function<String, T> constructor){
private <T extends io.anuke.mindustry.ctype.Content> TypeParser<T> parser(ContentType type, Function<String, T> constructor){
return (mod, name, value) -> {
T item;
if(Vars.content.getByName(type, name) != null){
@@ -236,7 +237,7 @@ public class ContentParser{
}
private void readBundle(ContentType type, String name, JsonValue value){
UnlockableContent cont = Vars.content.getByName(type, name) instanceof UnlockableContent ?
io.anuke.mindustry.ctype.UnlockableContent cont = Vars.content.getByName(type, name) instanceof io.anuke.mindustry.ctype.UnlockableContent ?
Vars.content.getByName(type, name) : null;
String entryName = cont == null ? type + "." + currentMod.name + "-" + name + "." : type + "." + cont.name + ".";
@@ -258,7 +259,7 @@ public class ContentParser{
/** Call to read a content's extra info later.*/
private void read(Runnable run){
Content cont = currentContent;
io.anuke.mindustry.ctype.Content cont = currentContent;
LoadedMod mod = currentMod;
reads.add(() -> {
this.currentMod = mod;
@@ -269,11 +270,11 @@ public class ContentParser{
private void init(){
for(ContentType type : ContentType.all){
Array<Content> arr = Vars.content.getBy(type);
Array<io.anuke.mindustry.ctype.Content> arr = Vars.content.getBy(type);
if(!arr.isEmpty()){
Class<?> c = arr.first().getClass();
//get base content class, skipping intermediates
while(!(c.getSuperclass() == Content.class || c.getSuperclass() == UnlockableContent.class || Modifier.isAbstract(c.getSuperclass().getModifiers()))){
while(!(c.getSuperclass() == io.anuke.mindustry.ctype.Content.class || c.getSuperclass() == UnlockableContent.class || Modifier.isAbstract(c.getSuperclass().getModifiers()))){
c = c.getSuperclass();
}
@@ -301,7 +302,7 @@ public class ContentParser{
* @param file file that this content is being parsed from
* @return the content that was parsed
*/
public Content parse(LoadedMod mod, String name, String json, FileHandle file, ContentType type) throws Exception{
public io.anuke.mindustry.ctype.Content parse(LoadedMod mod, String name, String json, FileHandle file, ContentType type) throws Exception{
if(contentTypes.isEmpty()){
init();
}
@@ -313,7 +314,7 @@ public class ContentParser{
currentMod = mod;
boolean exists = Vars.content.getByName(type, name) != null;
Content c = parsers.get(type).parse(mod.name, name, value);
io.anuke.mindustry.ctype.Content c = parsers.get(type).parse(mod.name, name, value);
if(!exists){
c.sourceFile = file;
c.mod = mod;
@@ -453,7 +454,7 @@ public class ContentParser{
Object parse(Class<?> type, JsonValue value) throws Exception;
}
private interface TypeParser<T extends Content>{
private interface TypeParser<T extends io.anuke.mindustry.ctype.Content>{
T parse(String mod, String name, JsonValue value) throws Exception;
}

View File

@@ -14,7 +14,8 @@ import io.anuke.arc.util.ArcAnnotate.*;
import io.anuke.arc.util.*;
import io.anuke.arc.util.io.*;
import io.anuke.arc.util.serialization.*;
import io.anuke.mindustry.game.*;
import io.anuke.mindustry.core.Version;
import io.anuke.mindustry.ctype.UnlockableContent;
import io.anuke.mindustry.game.EventType.*;
import io.anuke.mindustry.gen.*;
import io.anuke.mindustry.plugin.*;
@@ -129,10 +130,10 @@ public class Mods implements Loadable{
packer.updateTextureAtlas(Core.atlas, filter, filter, false);
//generate new icons
for(Array<Content> arr : content.getContentMap()){
for(Array<io.anuke.mindustry.ctype.Content> arr : content.getContentMap()){
arr.each(c -> {
if(c instanceof UnlockableContent && c.mod != null){
UnlockableContent u = (UnlockableContent)c;
if(c instanceof io.anuke.mindustry.ctype.UnlockableContent && c.mod != null){
io.anuke.mindustry.ctype.UnlockableContent u = (io.anuke.mindustry.ctype.UnlockableContent)c;
u.createIcons(packer, editorPacker);
}
});
@@ -284,9 +285,9 @@ public class Mods implements Loadable{
if(file.extension().equals("json")){
try{
//this binds the content but does not load it entirely
Content loaded = parser.parse(mod, file.nameWithoutExtension(), file.readString("UTF-8"), file, type);
io.anuke.mindustry.ctype.Content loaded = parser.parse(mod, file.nameWithoutExtension(), file.readString("UTF-8"), file, type);
Log.info("[{0}] Loaded '{1}'.", mod.meta.name,
(loaded instanceof UnlockableContent ? ((UnlockableContent)loaded).localizedName : loaded));
(loaded instanceof io.anuke.mindustry.ctype.UnlockableContent ? ((UnlockableContent)loaded).localizedName : loaded));
}catch(Exception e){
throw new RuntimeException("Failed to parse content file '" + file + "' for mod '" + mod.meta.name + "'.", e);
}
@@ -361,7 +362,7 @@ public class Mods implements Loadable{
public void handleError(Throwable t, LoadedMod mod){
Array<Throwable> causes = Strings.getCauses(t);
Content content = null;
io.anuke.mindustry.ctype.Content content = null;
for(Throwable e : causes){
if(e instanceof ModLoadException && ((ModLoadException) e).content != null){
content = ((ModLoadException) e).content;
@@ -552,14 +553,14 @@ public class Mods implements Loadable{
/** Thrown when an error occurs while loading a mod.*/
public static class ModLoadException extends RuntimeException{
public Content content;
public io.anuke.mindustry.ctype.Content content;
public LoadedMod mod;
public ModLoadException(String message, Throwable cause){
super(message, cause);
}
public ModLoadException(String message, @Nullable Content content, Throwable cause){
public ModLoadException(String message, @Nullable io.anuke.mindustry.ctype.Content content, Throwable cause){
super(message, cause);
this.content = content;
if(content != null){