[ci skip] spotlessApply with the new settings

This commit is contained in:
Raven Szewczyk committed 2023-01-29 11:36:41 +00:00
1 parent 6b208b98bc
commit a309488cdf
214 files changed
+9283 -9265

No files matched your search

@@ -1,7 +1,6 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime;
@@ -13,14 +12,14 @@ import minetweaker.MineTweakerAPI;
* @author Stan
*/
public class GlobalFunctions {
private GlobalFunctions() {
}
public static void print(String message) {
MineTweakerAPI.logInfo(message);
}
public static boolean isNull(Object any){
private GlobalFunctions() {}
public static void print(String message) {
MineTweakerAPI.logInfo(message);
}
public static boolean isNull(Object any) {
return any == null;
}
}
@@ -1,11 +1,15 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime;
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.*;
import minetweaker.*;
import minetweaker.api.item.*;
import stanhebben.zenscript.*;
@@ -18,150 +22,146 @@ import stanhebben.zenscript.type.*;
import stanhebben.zenscript.type.natives.*;
import stanhebben.zenscript.util.*;
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.*;
/**
*
* @author Stan
*/
public class GlobalRegistry {
private static final Map<String, IZenSymbol> globals = new HashMap<String, IZenSymbol>();
private static final List<IRecipeRemover> removers = new ArrayList<IRecipeRemover>();
private static final List<IBracketHandler> bracketHandlers = new ArrayList<IBracketHandler>();
private static final TypeRegistry types = new TypeRegistry();
private static final SymbolPackage root = new SymbolPackage("<root>");
private static final IZenErrorLogger errors = new MyErrorLogger();
private static final IZenCompileEnvironment environment = new MyCompileEnvironment();
private static final Map<String, TypeExpansion> expansions = new HashMap<String, TypeExpansion>();
static {
registerGlobal("print", getStaticFunction(GlobalFunctions.class, "print", String.class));
private static final Map<String, IZenSymbol> globals = new HashMap<String, IZenSymbol>();
private static final List<IRecipeRemover> removers = new ArrayList<IRecipeRemover>();
private static final List<IBracketHandler> bracketHandlers = new ArrayList<IBracketHandler>();
private static final TypeRegistry types = new TypeRegistry();
private static final SymbolPackage root = new SymbolPackage("<root>");
private static final IZenErrorLogger errors = new MyErrorLogger();
private static final IZenCompileEnvironment environment = new MyCompileEnvironment();
private static final Map<String, TypeExpansion> expansions = new HashMap<String, TypeExpansion>();
static {
registerGlobal("print", getStaticFunction(GlobalFunctions.class, "print", String.class));
registerGlobal("isNull", getStaticFunction(GlobalFunctions.class, "isNull", Object.class));
registerGlobal("max", getStaticFunction(Math.class, "max", int.class, int.class));
registerGlobal("min", getStaticFunction(Math.class, "min", int.class, int.class));
registerGlobal("pow", getStaticFunction(Math.class, "pow", double.class, double.class));
}
}
public static IZenErrorLogger getErrors() {
return errors;
}
private GlobalRegistry() {
}
public static void registerGlobal(String name, IZenSymbol symbol) {
if (globals.containsKey(name)) {
throw new IllegalArgumentException("symbol already exists: " + name);
}
private GlobalRegistry() {}
globals.put(name, symbol);
}
public static void registerGlobal(String name, IZenSymbol symbol) {
if (globals.containsKey(name)) {
throw new IllegalArgumentException("symbol already exists: " + name);
}
public static void registerExpansion(Class<?> cls) {
try {
for (Annotation annotation : cls.getAnnotations()) {
if (annotation instanceof ZenExpansion) {
ZenExpansion eAnnotation = (ZenExpansion) annotation;
if (!expansions.containsKey(eAnnotation.value())) {
expansions.put(eAnnotation.value(), new TypeExpansion(eAnnotation.value()));
}
expansions.get(eAnnotation.value()).expand(cls, types);
}
}
} catch (Throwable ex) {
globals.put(name, symbol);
}
public static void registerExpansion(Class<?> cls) {
try {
for (Annotation annotation : cls.getAnnotations()) {
if (annotation instanceof ZenExpansion) {
ZenExpansion eAnnotation = (ZenExpansion) annotation;
if (!expansions.containsKey(eAnnotation.value())) {
expansions.put(eAnnotation.value(), new TypeExpansion(eAnnotation.value()));
}
expansions.get(eAnnotation.value()).expand(cls, types);
}
}
} catch (Throwable ex) {
errors.error("Error while applying expansion", ex);
ex.printStackTrace();
}
}
}
}
public static void registerRemover(IRecipeRemover remover) {
removers.add(remover);
}
public static void registerRemover(IRecipeRemover remover) {
removers.add(remover);
}
public static void registerBracketHandler(IBracketHandler handler) {
bracketHandlers.add(handler);
}
public static void registerBracketHandler(IBracketHandler handler) {
bracketHandlers.add(handler);
}
public static void registerNativeClass(Class<?> cls) {
//MineTweakerAPI.logInfo("Registering class: " + cls);
try {
ZenTypeNative type = new ZenTypeNative(cls);
type.complete(types);
public static void registerNativeClass(Class<?> cls) {
// MineTweakerAPI.logInfo("Registering class: " + cls);
try {
root.put(type.getName(), new SymbolType(type), errors);
} catch (Throwable ex) {
MineTweakerAPI.logError("Error for " + cls, ex);
ex.printStackTrace();
}
}
ZenTypeNative type = new ZenTypeNative(cls);
type.complete(types);
public static TypeRegistry getTypeRegistry() {
return types;
}
root.put(type.getName(), new SymbolType(type), errors);
} catch (Throwable ex) {
MineTweakerAPI.logError("Error for " + cls, ex);
ex.printStackTrace();
}
}
public static void remove(IIngredient ingredient) {
for (IRecipeRemover remover : removers) {
remover.remove(ingredient);
}
}
public static TypeRegistry getTypeRegistry() {
return types;
}
public static IZenSymbol resolveBracket(IEnvironmentGlobal environment, List<Token> tokens) {
for (IBracketHandler handler : bracketHandlers) {
IZenSymbol symbol = handler.resolve(environment, tokens);
if (symbol != null) {
return symbol;
}
}
public static void remove(IIngredient ingredient) {
for (IRecipeRemover remover : removers) {
remover.remove(ingredient);
}
}
return null;
}
public static IZenSymbol resolveBracket(IEnvironmentGlobal environment, List<Token> tokens) {
for (IBracketHandler handler : bracketHandlers) {
IZenSymbol symbol = handler.resolve(environment, tokens);
if (symbol != null) {
return symbol;
}
}
public static IZenSymbol getStaticFunction(Class cls, String name, Class... arguments) {
IJavaMethod method = JavaMethod.get(types, cls, name, arguments);
if (method == null)
return null;
return new SymbolJavaStaticMethod(method);
}
return null;
}
public static IZenSymbol getStaticField(Class cls, String name) {
try {
Field field = cls.getDeclaredField(name);
return new SymbolJavaStaticField(cls, field, types);
} catch (NoSuchFieldException ex) {
Logger.getLogger(GlobalRegistry.class.getName()).log(Level.SEVERE, null, ex);
return null;
} catch (SecurityException ex) {
Logger.getLogger(GlobalRegistry.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
public static IZenSymbol getStaticFunction(Class cls, String name, Class... arguments) {
IJavaMethod method = JavaMethod.get(types, cls, name, arguments);
if (method == null) return null;
return new SymbolJavaStaticMethod(method);
}
public static IEnvironmentGlobal makeGlobalEnvironment(Map<String, byte[]> classes) {
return new MyGlobalEnvironment(classes);
}
public static IZenSymbol getStaticField(Class cls, String name) {
try {
Field field = cls.getDeclaredField(name);
return new SymbolJavaStaticField(cls, field, types);
} catch (NoSuchFieldException ex) {
Logger.getLogger(GlobalRegistry.class.getName()).log(Level.SEVERE, null, ex);
return null;
} catch (SecurityException ex) {
Logger.getLogger(GlobalRegistry.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
private static class MyErrorLogger implements IZenErrorLogger {
@Override
public void error(ZenPosition position, String message) {
if (position == null) {
MineTweakerAPI.logError("system: " + message);
} else {
MineTweakerAPI.logError(position + ": " + message);
}
}
public static IEnvironmentGlobal makeGlobalEnvironment(Map<String, byte[]> classes) {
return new MyGlobalEnvironment(classes);
}
private static class MyErrorLogger implements IZenErrorLogger {
@Override
public void error(ZenPosition position, String message) {
if (position == null) {
MineTweakerAPI.logError("system: " + message);
} else {
MineTweakerAPI.logError(position + ": " + message);
}
}
@Override
public void warning(ZenPosition position, String message) {
if (position == null) {
MineTweakerAPI.logWarning("system: " + message);
} else {
MineTweakerAPI.logWarning(position + ": " + message);
}
}
@Override
public void warning(ZenPosition position, String message) {
if (position == null) {
MineTweakerAPI.logWarning("system: " + message);
} else {
MineTweakerAPI.logWarning(position + ": " + message);
}
}
@Override
public void info(ZenPosition position, String message) {
if (position == null) {
@@ -170,178 +170,180 @@ public class GlobalRegistry {
MineTweakerAPI.logInfo(position + ": " + message);
}
}
@Override
public void error(String message) {
error(null, message);
}
@Override
public void error(String message, Throwable e) {
error(null, message);
}
@Override
public void warning(String message) {
warning(null, message);
}
@Override
public void info(String message) {
info(null, message);
}
}
private static class MyCompileEnvironment implements IZenCompileEnvironment {
@Override
public IZenErrorLogger getErrorLogger() {
return errors;
}
private static class MyCompileEnvironment implements IZenCompileEnvironment {
@Override
public IZenSymbol getGlobal(String name) {
if (globals.containsKey(name)) {
return globals.get(name);
} else {
return root.get(name);
}
}
@Override
public IZenErrorLogger getErrorLogger() {
return errors;
}
@Override
public IZenSymbol getBracketed(IEnvironmentGlobal environment, List<Token> tokens) {
return resolveBracket(environment, tokens);
}
@Override
public IZenSymbol getGlobal(String name) {
if (globals.containsKey(name)) {
return globals.get(name);
} else {
return root.get(name);
}
}
@Override
public TypeRegistry getTypeRegistry() {
return types;
}
@Override
public IZenSymbol getBracketed(IEnvironmentGlobal environment, List<Token> tokens) {
return resolveBracket(environment, tokens);
}
@Override
public TypeRegistry getTypeRegistry() {
return types;
}
@Override
public TypeExpansion getExpansion(String type) {
return expansions.get(type);
}
@Override
public TypeExpansion getExpansion(String type) {
return expansions.get(type);
}
@Override
public void setRegistry(IZenRegistry registry) {
//NO-OP
// NO-OP
throw new UnsupportedOperationException("Registry is not local");
}
}
private static class MyGlobalEnvironment implements IEnvironmentGlobal {
private final Map<String, byte[]> classes;
private final Map<String, IZenSymbol> symbols;
private final ClassNameGenerator generator;
private static class MyGlobalEnvironment implements IEnvironmentGlobal {
public MyGlobalEnvironment(Map<String, byte[]> classes) {
this.classes = classes;
symbols = new HashMap<String, IZenSymbol>();
generator = new ClassNameGenerator();
}
private final Map<String, byte[]> classes;
private final Map<String, IZenSymbol> symbols;
private final ClassNameGenerator generator;
@Override
public IZenCompileEnvironment getEnvironment() {
return environment;
}
public MyGlobalEnvironment(Map<String, byte[]> classes) {
this.classes = classes;
symbols = new HashMap<String, IZenSymbol>();
generator = new ClassNameGenerator();
}
@Override
public TypeExpansion getExpansion(String name) {
return expansions.get(name);
}
@Override
public IZenCompileEnvironment getEnvironment() {
return environment;
}
@Override
public TypeExpansion getExpansion(String name) {
return expansions.get(name);
}
@Override
public String makeClassName() {
return generator.generate();
}
@Override
public String makeClassName() {
return generator.generate();
}
@Override
public String makeClassNameWithMiddleName(String middleName) {
return generator.generateWithMiddleName(middleName);
}
@Override
public boolean containsClass(String name) {
return classes.containsKey(name);
}
public boolean containsClass(String name) {
return classes.containsKey(name);
}
@Override
public void putClass(String name, byte[] data) {
classes.put(name, data);
}
@Override
public void putClass(String name, byte[] data) {
classes.put(name, data);
}
@Override
public IPartialExpression getValue(String name, ZenPosition position) {
if (symbols.containsKey(name)) {
return symbols.get(name).instance(position);
} else if (globals.containsKey(name)) {
return globals.get(name).instance(position);
} else {
IZenSymbol pkg = root.get(name);
if (pkg == null) {
return null;
} else {
return pkg.instance(position);
}
}
}
@Override
public IPartialExpression getValue(String name, ZenPosition position) {
if (symbols.containsKey(name)) {
return symbols.get(name).instance(position);
} else if (globals.containsKey(name)) {
return globals.get(name).instance(position);
} else {
IZenSymbol pkg = root.get(name);
if (pkg == null) {
return null;
} else {
return pkg.instance(position);
}
}
}
@Override
public void putValue(String name, IZenSymbol value, ZenPosition position) {
if (symbols.containsKey(name)) {
error(position, "Value already defined in this scope: " + name);
} else {
symbols.put(name, value);
}
}
@Override
public void putValue(String name, IZenSymbol value, ZenPosition position) {
if (symbols.containsKey(name)) {
error(position, "Value already defined in this scope: " + name);
} else {
symbols.put(name, value);
}
}
@Override
public ZenType getType(Type type) {
return types.getType(type);
}
@Override
public ZenType getType(Type type) {
return types.getType(type);
}
@Override
public void error(ZenPosition position, String message) {
MineTweakerAPI.logError(position.toString() + " > " + message);
}
@Override
public void error(ZenPosition position, String message) {
MineTweakerAPI.logError(position.toString() + " > " + message);
}
@Override
public void warning(ZenPosition position, String message) {
MineTweakerAPI.logWarning(position.toString() + " > " + message);
}
@Override
public void warning(ZenPosition position, String message) {
MineTweakerAPI.logWarning(position.toString() + " > " + message);
}
@Override
public void info(ZenPosition position, String message) {
MineTweakerAPI.logInfo(position.toString() + " > " + message);
}
public void error(String message) {
MineTweakerAPI.logError(message);
}
@Override
public void error(String message, Throwable e) {
MineTweakerAPI.logError(message, e);
}
@Override
public void warning(String message) {
MineTweakerAPI.logWarning(message);
}
@Override
public void info(String message) {
MineTweakerAPI.logInfo(message);
}
@Override
public Set<String> getClassNames() {
return classes.keySet();
}
@Override
public Set<String> getClassNames() {
return classes.keySet();
}
@Override
public byte[] getClass(String name) {
return classes.get(name);
}
}
@Override
public byte[] getClass(String name) {
return classes.get(name);
}
}
}
+13 -13
View File
@@ -1,7 +1,6 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime;
@@ -13,18 +12,19 @@ import stanhebben.zenscript.annotations.ZenMethod;
* @author Stan
*/
public interface ILogger {
@ZenMethod
public void logCommand(String message);
@ZenMethod
public void logInfo(String message);
@ZenMethod
public void logCommand(String message);
@ZenMethod
public void logWarning(String message);
@ZenMethod
public void logInfo(String message);
@ZenMethod
public void logError(String message);
@ZenMethod
public void logWarning(String message);
@ZenMethod
public void logError(String message, Throwable exception);
@ZenMethod
public void logError(String message);
@ZenMethod
public void logError(String message, Throwable exception);
}
@@ -1,7 +1,6 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime;
@@ -14,11 +13,12 @@ import java.io.InputStream;
* @author Stan
*/
public interface IScriptIterator {
public String getGroupName();
public boolean next();
public String getGroupName();
public String getName();
public boolean next();
public InputStream open() throws IOException;
public String getName();
public InputStream open() throws IOException;
}
@@ -1,7 +1,6 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime;
@@ -13,5 +12,6 @@ import java.util.Iterator;
* @author Stan
*/
public interface IScriptProvider {
public Iterator<IScriptIterator> getScripts();
public Iterator<IScriptIterator> getScripts();
}
+47 -48
View File
@@ -1,12 +1,12 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime;
import java.util.List;
import minetweaker.IUndoableAction;
import minetweaker.api.item.IIngredient;
import stanhebben.zenscript.annotations.ZenClass;
@@ -18,55 +18,54 @@ import stanhebben.zenscript.annotations.ZenMethod;
*/
@ZenClass("minetweaker.IMineTweaker")
public interface ITweaker {
/**
* Retrieves the script data to be loaded.
*
* @return
*/
public byte[] getStagedScriptData();
/**
* Executes a specified MineTweaker action. Will print a log message and
* adds the action to the undo list.
*
* @param action action to execute
*/
public void apply(IUndoableAction action);
/**
* Retrieves the script data to be loaded.
*
* @return
*/
public byte[] getStagedScriptData();
/**
* Removes a specific item from all known recipes handlers.
*
* @param items items to remove
*/
@ZenMethod
public void remove(IIngredient items);
/**
* Executes a specified MineTweaker action. Will print a log message and adds the action to the undo list.
*
* @param action action to execute
*/
public void apply(IUndoableAction action);
/**
* Rolls back all actions performed by MineTweaker. Returns the list of
* actions that could not be rolled back (the "stuck" ones that are not
* undoable).
*
* @return stuck action list
*/
public List<IUndoableAction> rollback();
/**
* Removes a specific item from all known recipes handlers.
*
* @param items items to remove
*/
@ZenMethod
public void remove(IIngredient items);
/**
* Sets the script provider.
*
* @param provider
* @return
*/
public void setScriptProvider(IScriptProvider provider);
/**
* Rolls back all actions performed by MineTweaker. Returns the list of actions that could not be rolled back (the
* "stuck" ones that are not undoable).
*
* @return stuck action list
*/
public List<IUndoableAction> rollback();
/**
* Executes all scripts provided by the script provider.
*/
public void load();
/**
* Sets the script provider.
*
* @param provider
* @return
*/
public void setScriptProvider(IScriptProvider provider);
/**
* Retrieves the data from the scripts that were loaded last.
*
* @return scripts data
*/
public byte[] getScriptData();
/**
* Executes all scripts provided by the script provider.
*/
public void load();
/**
* Retrieves the data from the scripts that were loaded last.
*
* @return scripts data
*/
public byte[] getScriptData();
}
+134 -132
View File
@@ -1,11 +1,20 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime;
import static stanhebben.zenscript.ZenModule.compileScripts;
import static stanhebben.zenscript.ZenModule.extractClassName;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.*;
import java.util.*;
import minetweaker.IUndoableAction;
import minetweaker.MineTweakerAPI;
import minetweaker.api.item.IIngredient;
@@ -16,169 +25,162 @@ import stanhebben.zenscript.ZenTokener;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.parser.ParseException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.*;
import java.util.*;
import static stanhebben.zenscript.ZenModule.compileScripts;
import static stanhebben.zenscript.ZenModule.extractClassName;
/**
*
*
* @author Stan Hebben
*/
public class MTTweaker implements ITweaker {
private static final boolean DEBUG = false;
private final List<IUndoableAction> actions = new ArrayList<IUndoableAction>();
private final Set<IUndoableAction> wereStuck = new LinkedHashSet<IUndoableAction>();
private final Map<Object, IUndoableAction> stuckOverridable = new HashMap<Object, IUndoableAction>();
private static final boolean DEBUG = false;
private IScriptProvider scriptProvider;
private byte[] scriptData;
private final List<IUndoableAction> actions = new ArrayList<IUndoableAction>();
private final Set<IUndoableAction> wereStuck = new LinkedHashSet<IUndoableAction>();
private final Map<Object, IUndoableAction> stuckOverridable = new HashMap<Object, IUndoableAction>();
@Override
public byte[] getStagedScriptData() {
return ScriptProviderMemory.collect(scriptProvider);
}
private IScriptProvider scriptProvider;
private byte[] scriptData;
@Override
public void apply(IUndoableAction action) {
MineTweakerAPI.logInfo(action.describe());
@Override
public byte[] getStagedScriptData() {
return ScriptProviderMemory.collect(scriptProvider);
}
Object overrideKey = action.getOverrideKey();
if (wereStuck.contains(action)) {
MineTweakerAPI.logInfo("WAS STUCK");
wereStuck.remove(action);
@Override
public void apply(IUndoableAction action) {
MineTweakerAPI.logInfo(action.describe());
if (overrideKey != null) {
stuckOverridable.remove(overrideKey);
}
} else {
if (overrideKey != null && stuckOverridable.containsKey(overrideKey)) {
wereStuck.remove(stuckOverridable.get(overrideKey));
stuckOverridable.remove(overrideKey);
}
Object overrideKey = action.getOverrideKey();
if (wereStuck.contains(action)) {
MineTweakerAPI.logInfo("WAS STUCK");
wereStuck.remove(action);
action.apply();
}
if (overrideKey != null) {
stuckOverridable.remove(overrideKey);
}
} else {
if (overrideKey != null && stuckOverridable.containsKey(overrideKey)) {
wereStuck.remove(stuckOverridable.get(overrideKey));
stuckOverridable.remove(overrideKey);
}
actions.add(action);
}
action.apply();
}
@Override
public void remove(IIngredient items) {
GlobalRegistry.remove(items);
}
actions.add(action);
}
@Override
public List<IUndoableAction> rollback() {
// Note: Requires a OnRollback event to be fired. If this is called from MineTweakerImplementationAPI it's handled there.
List<IUndoableAction> stuck = new ArrayList<>();
for (int i = actions.size() - 1; i >= 0; i--) {
IUndoableAction action = actions.get(i);
if (action.canUndo()) {
MineTweakerAPI.logInfo(action.describeUndo());
action.undo();
} else {
MineTweakerAPI.logInfo("[Stuck] 1 " + action.describe());
@Override
public void remove(IIngredient items) {
GlobalRegistry.remove(items);
}
@Override
public List<IUndoableAction> rollback() {
// Note: Requires a OnRollback event to be fired. If this is called from MineTweakerImplementationAPI it's
// handled there.
List<IUndoableAction> stuck = new ArrayList<>();
for (int i = actions.size() - 1; i >= 0; i--) {
IUndoableAction action = actions.get(i);
if (action.canUndo()) {
MineTweakerAPI.logInfo(action.describeUndo());
action.undo();
} else {
MineTweakerAPI.logInfo("[Stuck] 1 " + action.describe());
MineTweakerAPI.logInfo("[Stuck] 2 " + action.describeUndo());
MineTweakerAPI.logInfo("[Stuck] 3 " + action.toString());
stuck.add(0, action);
wereStuck.add(action);
wereStuck.add(action);
Object overrideKey = action.getOverrideKey();
if (overrideKey != null) {
stuckOverridable.put(overrideKey, action);
}
}
}
actions.clear();
return stuck;
}
Object overrideKey = action.getOverrideKey();
if (overrideKey != null) {
stuckOverridable.put(overrideKey, action);
}
}
}
actions.clear();
return stuck;
}
@Override
public void setScriptProvider(IScriptProvider provider) {
scriptProvider = provider;
}
@Override
public void setScriptProvider(IScriptProvider provider) {
scriptProvider = provider;
}
@Override
public void load() {
ZenModule.loadedClasses.clear();
System.out.println("Loading scripts");
@Override
public void load() {
ZenModule.loadedClasses.clear();
System.out.println("Loading scripts");
scriptData = ScriptProviderMemory.collect(scriptProvider);
Set<String> executed = new HashSet<>();
scriptData = ScriptProviderMemory.collect(scriptProvider);
Set<String> executed = new HashSet<>();
Iterator<IScriptIterator> scripts = scriptProvider.getScripts();
while (scripts.hasNext()) {
IScriptIterator script = scripts.next();
Iterator<IScriptIterator> scripts = scriptProvider.getScripts();
while (scripts.hasNext()) {
IScriptIterator script = scripts.next();
if (!executed.contains(script.getGroupName())) {
executed.add(script.getGroupName());
if (!executed.contains(script.getGroupName())) {
executed.add(script.getGroupName());
Map<String, byte[]> classes = new HashMap<>();
IEnvironmentGlobal environmentGlobal = GlobalRegistry.makeGlobalEnvironment(classes);
Map<String, byte[]> classes = new HashMap<>();
IEnvironmentGlobal environmentGlobal = GlobalRegistry.makeGlobalEnvironment(classes);
List<ZenParsedFile> files = new ArrayList<>();
List<ZenParsedFile> files = new ArrayList<>();
while (script.next()) {
Reader reader = null;
try {
reader = new InputStreamReader(new BufferedInputStream(script.open()), StandardCharsets.UTF_8);
while (script.next()) {
Reader reader = null;
try {
reader = new InputStreamReader(new BufferedInputStream(script.open()), StandardCharsets.UTF_8);
String filename = script.getName();
String className = extractClassName(filename);
String filename = script.getName();
String className = extractClassName(filename);
ZenTokener parser = new ZenTokener(reader, environmentGlobal.getEnvironment(), filename, false);
ZenParsedFile pfile = new ZenParsedFile(filename, className, parser, environmentGlobal);
files.add(pfile);
} catch (IOException ex) {
MineTweakerAPI.logError("Could not load script " + script.getName() + ": " + ex.getMessage());
} catch (ParseException ex) {
// ex.printStackTrace();
MineTweakerAPI.logError("Error parsing " + ex.getFile().getFileName() + ":" + ex.getLine() + " -- " + ex.getExplanation());
} catch (Exception ex) {
MineTweakerAPI.logError("Error loading " + script.getName() + ": " + ex.toString(), ex);
}
ZenTokener parser = new ZenTokener(reader, environmentGlobal.getEnvironment(), filename, false);
ZenParsedFile pfile = new ZenParsedFile(filename, className, parser, environmentGlobal);
files.add(pfile);
} catch (IOException ex) {
MineTweakerAPI.logError("Could not load script " + script.getName() + ": " + ex.getMessage());
} catch (ParseException ex) {
// ex.printStackTrace();
MineTweakerAPI.logError(
"Error parsing " + ex.getFile()
.getFileName() + ":" + ex.getLine() + " -- " + ex.getExplanation());
} catch (Exception ex) {
MineTweakerAPI.logError("Error loading " + script.getName() + ": " + ex.toString(), ex);
}
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {}
}
}
try {
String filename = script.getGroupName();
System.out.println("MineTweaker: Loading " + filename);
compileScripts(filename, files, environmentGlobal, DEBUG);
try {
String filename = script.getGroupName();
System.out.println("MineTweaker: Loading " + filename);
compileScripts(filename, files, environmentGlobal, DEBUG);
// execute scripts
ZenModule module = new ZenModule(classes, MineTweakerAPI.class.getClassLoader());
module.getMain().run();
} catch (Throwable ex) {
MineTweakerAPI.logError("Error executing " + script.getGroupName() + ": " + ex.getMessage(), ex);
}
}
}
// execute scripts
ZenModule module = new ZenModule(classes, MineTweakerAPI.class.getClassLoader());
module.getMain().run();
} catch (Throwable ex) {
MineTweakerAPI.logError("Error executing " + script.getGroupName() + ": " + ex.getMessage(), ex);
}
}
}
if (wereStuck.size() > 0) {
MineTweakerAPI.logWarning(Integer.toString(wereStuck.size()) + " modifications were stuck");
for (IUndoableAction action : wereStuck) {
MineTweakerAPI.logInfo("Stuck: " + action.describe());
}
}
}
if (wereStuck.size() > 0) {
MineTweakerAPI.logWarning(Integer.toString(wereStuck.size()) + " modifications were stuck");
for (IUndoableAction action : wereStuck) {
MineTweakerAPI.logInfo("Stuck: " + action.describe());
}
}
}
@Override
public byte[] getScriptData() {
return scriptData;
}
@Override
public byte[] getScriptData() {
return scriptData;
}
}
@@ -1,7 +1,6 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime.providers;
@@ -14,6 +13,7 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import minetweaker.runtime.IScriptIterator;
/**
@@ -21,53 +21,54 @@ import minetweaker.runtime.IScriptIterator;
* @author Stan
*/
public class ScriptIteratorDirectory implements IScriptIterator {
private final File directory;
private Iterator<File> contents;
private File current;
public ScriptIteratorDirectory(File directory) {
this.directory = directory;
private final File directory;
private Iterator<File> contents;
private File current;
File scriptDir = new File(directory, "scripts");
List<File> contentsList = new ArrayList<File>();
if (scriptDir.exists()) {
iterate(scriptDir, contentsList);
}
contents = contentsList.iterator();
}
public ScriptIteratorDirectory(File directory) {
this.directory = directory;
@Override
public String getGroupName() {
return directory.getName();
}
File scriptDir = new File(directory, "scripts");
List<File> contentsList = new ArrayList<File>();
if (scriptDir.exists()) {
iterate(scriptDir, contentsList);
}
contents = contentsList.iterator();
}
@Override
public boolean next() {
if (contents.hasNext()) {
current = contents.next();
return true;
} else {
return false;
}
}
@Override
public String getGroupName() {
return directory.getName();
}
@Override
public String getName() {
return current.getAbsolutePath().substring(directory.getAbsolutePath().length());
}
@Override
public boolean next() {
if (contents.hasNext()) {
current = contents.next();
return true;
} else {
return false;
}
}
@Override
public InputStream open() throws IOException {
return new BufferedInputStream(new FileInputStream(current));
}
@Override
public String getName() {
return current.getAbsolutePath().substring(directory.getAbsolutePath().length());
}
private static void iterate(File directory, List<File> contentsList) {
for (File file : directory.listFiles()) {
if (file.isDirectory()) {
iterate(file, contentsList);
} else if (file.isFile() && file.getName().endsWith(".zs")) {
contentsList.add(file);
}
}
}
@Override
public InputStream open() throws IOException {
return new BufferedInputStream(new FileInputStream(current));
}
private static void iterate(File directory, List<File> contentsList) {
for (File file : directory.listFiles()) {
if (file.isDirectory()) {
iterate(file, contentsList);
} else if (file.isFile() && file.getName().endsWith(".zs")) {
contentsList.add(file);
}
}
}
}
@@ -1,7 +1,6 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime.providers;
@@ -11,6 +10,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import minetweaker.runtime.IScriptIterator;
/**
@@ -18,35 +18,36 @@ import minetweaker.runtime.IScriptIterator;
* @author Stan
*/
public class ScriptIteratorSingle implements IScriptIterator {
private final File file;
private boolean first = true;
public ScriptIteratorSingle(File file) {
this.file = file;
}
private final File file;
private boolean first = true;
@Override
public String getGroupName() {
return file.getName();
}
public ScriptIteratorSingle(File file) {
this.file = file;
}
@Override
public boolean next() {
if (first) {
first = false;
return true;
} else {
return false;
}
}
@Override
public String getGroupName() {
return file.getName();
}
@Override
public String getName() {
return file.getName();
}
@Override
public boolean next() {
if (first) {
first = false;
return true;
} else {
return false;
}
}
@Override
public InputStream open() throws IOException {
return new BufferedInputStream(new FileInputStream(file));
}
@Override
public String getName() {
return file.getName();
}
@Override
public InputStream open() throws IOException {
return new BufferedInputStream(new FileInputStream(file));
}
}
@@ -1,7 +1,6 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime.providers;
@@ -15,6 +14,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import minetweaker.runtime.IScriptIterator;
/**
@@ -22,49 +22,50 @@ import minetweaker.runtime.IScriptIterator;
* @author Stan
*/
public class ScriptIteratorZip implements IScriptIterator {
private final File file;
private final ZipFile zipFile;
private final Iterator<ZipEntry> entries;
private ZipEntry current;
public ScriptIteratorZip(File file) throws IOException {
this.file = file;
private final File file;
private final ZipFile zipFile;
private final Iterator<ZipEntry> entries;
private ZipEntry current;
zipFile = new ZipFile(file);
List<ZipEntry> entriesList = new ArrayList<ZipEntry>();
Enumeration<? extends ZipEntry> original = zipFile.entries();
while (original.hasMoreElements()) {
ZipEntry entry = original.nextElement();
if (entry.getName().startsWith("scripts/") && entry.getName().endsWith(".zs")) {
entriesList.add(entry);
}
}
public ScriptIteratorZip(File file) throws IOException {
this.file = file;
entries = entriesList.iterator();
}
zipFile = new ZipFile(file);
List<ZipEntry> entriesList = new ArrayList<ZipEntry>();
Enumeration<? extends ZipEntry> original = zipFile.entries();
while (original.hasMoreElements()) {
ZipEntry entry = original.nextElement();
if (entry.getName().startsWith("scripts/") && entry.getName().endsWith(".zs")) {
entriesList.add(entry);
}
}
@Override
public String getGroupName() {
return file.getName().substring(0, file.getName().lastIndexOf('.'));
}
entries = entriesList.iterator();
}
@Override
public boolean next() {
if (entries.hasNext()) {
current = entries.next();
return true;
} else {
return false;
}
}
@Override
public String getGroupName() {
return file.getName().substring(0, file.getName().lastIndexOf('.'));
}
@Override
public String getName() {
return current.getName().substring("scripts/".length());
}
@Override
public boolean next() {
if (entries.hasNext()) {
current = entries.next();
return true;
} else {
return false;
}
}
@Override
public InputStream open() throws IOException {
return zipFile.getInputStream(current);
}
@Override
public String getName() {
return current.getName().substring("scripts/".length());
}
@Override
public InputStream open() throws IOException {
return zipFile.getInputStream(current);
}
}
@@ -1,7 +1,6 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime.providers;
@@ -9,6 +8,7 @@ package minetweaker.runtime.providers;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import minetweaker.runtime.IScriptIterator;
import minetweaker.runtime.IScriptProvider;
@@ -17,74 +17,74 @@ import minetweaker.runtime.IScriptProvider;
* @author Stan Hebben
*/
public class ScriptProviderCascade implements IScriptProvider {
private final IScriptProvider[] providers;
public ScriptProviderCascade(IScriptProvider... providers) {
this.providers = providers;
}
private final IScriptProvider[] providers;
@Override
public Iterator<IScriptIterator> getScripts() {
return new MyScripterator();
}
public ScriptProviderCascade(IScriptProvider... providers) {
this.providers = providers;
}
private class MyScripterator implements Iterator<IScriptIterator> {
private final Set<String> executed = new HashSet<String>();
@Override
public Iterator<IScriptIterator> getScripts() {
return new MyScripterator();
}
private int currentIndex = providers.length - 1;
private Iterator<IScriptIterator> current;
private IScriptIterator currentValue;
private class MyScripterator implements Iterator<IScriptIterator> {
public MyScripterator() {
currentIndex = providers.length - 1;
current = providers[currentIndex].getScripts();
private final Set<String> executed = new HashSet<String>();
advance();
}
private int currentIndex = providers.length - 1;
private Iterator<IScriptIterator> current;
private IScriptIterator currentValue;
@Override
public boolean hasNext() {
return currentIndex >= 0;
}
public MyScripterator() {
currentIndex = providers.length - 1;
current = providers[currentIndex].getScripts();
@Override
public IScriptIterator next() {
IScriptIterator result = currentValue;
executed.add(result.getGroupName());
advance();
}
advance();
@Override
public boolean hasNext() {
return currentIndex >= 0;
}
return result;
}
@Override
public IScriptIterator next() {
IScriptIterator result = currentValue;
executed.add(result.getGroupName());
private void advance() {
do {
while (!current.hasNext()) {
currentIndex--;
if (currentIndex < 0)
return;
advance();
current = providers[currentIndex].getScripts();
}
if (currentIndex < 0)
break;
return result;
}
currentValue = current.next();
} while (executed.contains(currentValue.getGroupName()) && hasNext());
}
private void advance() {
do {
while (!current.hasNext()) {
currentIndex--;
if (currentIndex < 0) return;
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet."); // To
// change
// body
// of
// generated
// methods,
// choose
// Tools
// |
// Templates.
}
}
current = providers[currentIndex].getScripts();
}
if (currentIndex < 0) break;
currentValue = current.next();
} while (executed.contains(currentValue.getGroupName()) && hasNext());
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet."); // To
// change
// body
// of
// generated
// methods,
// choose
// Tools
// |
// Templates.
}
}
}
@@ -1,14 +1,10 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime.providers;
import minetweaker.runtime.IScriptIterator;
import minetweaker.runtime.IScriptProvider;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -18,72 +14,78 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import minetweaker.runtime.IScriptIterator;
import minetweaker.runtime.IScriptProvider;
/**
*
* @author Stan
*/
public class ScriptProviderCustom implements IScriptProvider {
private static final Charset UTF8 = Charset.forName("UTF-8");
private final String moduleName;
private final List<CustomScript> scripts;
private static final Charset UTF8 = Charset.forName("UTF-8");
public ScriptProviderCustom(String moduleName) {
this.moduleName = moduleName;
scripts = new ArrayList<CustomScript>();
}
private final String moduleName;
private final List<CustomScript> scripts;
public void add(String name, byte[] content) {
scripts.add(new CustomScript(name, content));
}
public ScriptProviderCustom(String moduleName) {
this.moduleName = moduleName;
scripts = new ArrayList<CustomScript>();
}
public void add(String name, String content) {
add(name, content.getBytes(UTF8));
}
public void add(String name, byte[] content) {
scripts.add(new CustomScript(name, content));
}
@Override
public Iterator<IScriptIterator> getScripts() {
public void add(String name, String content) {
add(name, content.getBytes(UTF8));
}
return Collections.<IScriptIterator>singleton(new CustomScriptIterator()).iterator();
}
@Override
public Iterator<IScriptIterator> getScripts() {
private class CustomScript {
private final String name;
private final byte[] content;
return Collections.<IScriptIterator>singleton(new CustomScriptIterator()).iterator();
}
public CustomScript(String name, byte[] content) {
this.name = name;
this.content = content;
}
}
private class CustomScript {
private class CustomScriptIterator implements IScriptIterator {
private CustomScript current;
private final Iterator<CustomScript> iterator = scripts.iterator();
private final String name;
private final byte[] content;
@Override
public String getGroupName() {
return moduleName;
}
public CustomScript(String name, byte[] content) {
this.name = name;
this.content = content;
}
}
@Override
public boolean next() {
if (iterator.hasNext()) {
current = iterator.next();
return true;
} else {
return false;
}
}
private class CustomScriptIterator implements IScriptIterator {
@Override
public String getName() {
return current.name;
}
private CustomScript current;
private final Iterator<CustomScript> iterator = scripts.iterator();
@Override
public InputStream open() throws IOException {
return new ByteArrayInputStream(current.content);
}
}
@Override
public String getGroupName() {
return moduleName;
}
@Override
public boolean next() {
if (iterator.hasNext()) {
current = iterator.next();
return true;
} else {
return false;
}
}
@Override
public String getName() {
return current.name;
}
@Override
public InputStream open() throws IOException {
return new ByteArrayInputStream(current.content);
}
}
}
@@ -1,28 +1,27 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime.providers;
import minetweaker.MineTweakerAPI;
import minetweaker.runtime.IScriptIterator;
import minetweaker.runtime.IScriptProvider;
import java.io.File;
import java.io.IOException;
import java.util.*;
import minetweaker.MineTweakerAPI;
import minetweaker.runtime.IScriptIterator;
import minetweaker.runtime.IScriptProvider;
/**
* @author Stan
*/
public class ScriptProviderDirectory implements IScriptProvider {
private final File directory;
public ScriptProviderDirectory(File directory) {
if(directory == null)
throw new IllegalArgumentException("directory cannot be null");
if (directory == null) throw new IllegalArgumentException("directory cannot be null");
this.directory = directory;
}
@@ -30,31 +29,31 @@ public class ScriptProviderDirectory implements IScriptProvider {
@Override
public Iterator<IScriptIterator> getScripts() {
List<IScriptIterator> scripts = new ArrayList<IScriptIterator>();
if(directory.exists()) {
for(File file : directory.listFiles()) {
if(file.isDirectory()) {
if (directory.exists()) {
for (File file : directory.listFiles()) {
if (file.isDirectory()) {
scripts.add(new ScriptIteratorDirectory(file));
} else if(file.getName().endsWith(".zs")) {
} else if (file.getName().endsWith(".zs")) {
scripts.add(new ScriptIteratorSingle(file));
} else if(file.getName().endsWith(".zip")) {
} else if (file.getName().endsWith(".zip")) {
try {
scripts.add(new ScriptIteratorZip(file));
} catch(IOException ex) {
} catch (IOException ex) {
MineTweakerAPI.logError("Could not load " + file.getName() + ": " + ex.getMessage());
}
}
}
}
if(scripts.size() > 1)
scripts.sort(new Comparator<IScriptIterator>() {
@Override
public int compare(IScriptIterator sc, IScriptIterator sc1) {
if(sc == null || sc1 == null) {
return -1;
}
return sc.getName().compareTo(sc1.getName());
if (scripts.size() > 1) scripts.sort(new Comparator<IScriptIterator>() {
@Override
public int compare(IScriptIterator sc, IScriptIterator sc1) {
if (sc == null || sc1 == null) {
return -1;
}
});
return sc.getName().compareTo(sc1.getName());
}
});
return scripts.iterator();
}
}
@@ -1,7 +1,6 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* To change this license header, choose License Headers in Project Properties. To change this template file, choose
* Tools | Templates and open the template in the editor.
*/
package minetweaker.runtime.providers;
@@ -15,175 +14,180 @@ import java.io.InputStream;
import java.util.*;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import minetweaker.MineTweakerAPI;
import minetweaker.util.FileUtil;
import minetweaker.runtime.IScriptIterator;
import minetweaker.runtime.IScriptProvider;
import minetweaker.util.FileUtil;
/**
*
* @author Stan
*/
public class ScriptProviderMemory implements IScriptProvider {
public static byte[] collect(IScriptProvider provider) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
DeflaterOutputStream deflater = new DeflaterOutputStream(output);
DataOutputStream deflaterData = new DataOutputStream(deflater);
Set<String> executed = new HashSet<String>();
Iterator<IScriptIterator> scripts = provider.getScripts();
while (scripts.hasNext()) {
IScriptIterator script = scripts.next();
public static byte[] collect(IScriptProvider provider) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
DeflaterOutputStream deflater = new DeflaterOutputStream(output);
DataOutputStream deflaterData = new DataOutputStream(deflater);
Set<String> executed = new HashSet<String>();
if (!executed.contains(script.getGroupName())) {
executed.add(script.getGroupName());
Iterator<IScriptIterator> scripts = provider.getScripts();
while (scripts.hasNext()) {
IScriptIterator script = scripts.next();
deflaterData.writeUTF(script.getGroupName());
if (!executed.contains(script.getGroupName())) {
executed.add(script.getGroupName());
while (script.next()) {
String name = script.getName();
byte[] data = FileUtil.read(script.open());
if (data.length == 0)
continue; // skip empty files
deflaterData.writeUTF(script.getGroupName());
deflaterData.writeUTF(name);
deflaterData.writeInt(data.length);
deflaterData.write(data);
}
while (script.next()) {
String name = script.getName();
byte[] data = FileUtil.read(script.open());
if (data.length == 0) continue; // skip empty files
deflaterData.writeUTF("");
}
}
deflaterData.writeUTF(name);
deflaterData.writeInt(data.length);
deflaterData.write(data);
}
deflaterData.writeUTF("");
deflaterData.writeUTF("");
}
}
deflater.close();
} catch (IOException ex) {
MineTweakerAPI.logError("Could not collect scripts: " + ex.getMessage());
}
return output.toByteArray();
}
deflaterData.writeUTF("");
private final Map<String, MemoryModule> modules;
deflater.close();
} catch (IOException ex) {
MineTweakerAPI.logError("Could not collect scripts: " + ex.getMessage());
}
return output.toByteArray();
}
public ScriptProviderMemory(byte[] scripts) {
modules = new TreeMap<String, MemoryModule>();
private final Map<String, MemoryModule> modules;
try {
InflaterInputStream inflater = new InflaterInputStream(new ByteArrayInputStream(scripts));
DataInputStream inflaterData = new DataInputStream(inflater);
public ScriptProviderMemory(byte[] scripts) {
modules = new TreeMap<String, MemoryModule>();
String moduleName = inflaterData.readUTF();
while (moduleName.length() > 0) {
List<MemoryFile> files = new ArrayList<MemoryFile>();
try {
InflaterInputStream inflater = new InflaterInputStream(new ByteArrayInputStream(scripts));
DataInputStream inflaterData = new DataInputStream(inflater);
String fileName = inflaterData.readUTF();
while (fileName.length() > 0) {
byte[] data = new byte[inflaterData.readInt()];
inflaterData.readFully(data);
files.add(new MemoryFile(fileName, data));
String moduleName = inflaterData.readUTF();
while (moduleName.length() > 0) {
List<MemoryFile> files = new ArrayList<MemoryFile>();
fileName = inflaterData.readUTF();
}
modules.put(moduleName, new MemoryModule(moduleName, files));
String fileName = inflaterData.readUTF();
while (fileName.length() > 0) {
byte[] data = new byte[inflaterData.readInt()];
inflaterData.readFully(data);
files.add(new MemoryFile(fileName, data));
moduleName = inflaterData.readUTF();
}
fileName = inflaterData.readUTF();
}
modules.put(moduleName, new MemoryModule(moduleName, files));
inflaterData.close();
} catch (IOException ex) {
MineTweakerAPI.logError("Could not load transmitted scripts: " + ex.getMessage());
}
}
moduleName = inflaterData.readUTF();
}
@Override
public Iterator<IScriptIterator> getScripts() {
return new ProviderIterator();
}
inflaterData.close();
} catch (IOException ex) {
MineTweakerAPI.logError("Could not load transmitted scripts: " + ex.getMessage());
}
}
private class ProviderIterator implements Iterator<IScriptIterator> {
private final Iterator<MemoryModule> baseIterator = modules.values().iterator();
@Override
public Iterator<IScriptIterator> getScripts() {
return new ProviderIterator();
}
@Override
public boolean hasNext() {
return baseIterator.hasNext();
}
private class ProviderIterator implements Iterator<IScriptIterator> {
@Override
public IScriptIterator next() {
return new ScriptIterator(baseIterator.next());
}
private final Iterator<MemoryModule> baseIterator = modules.values().iterator();
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet."); // To
// change
// body
// of
// generated
// methods,
// choose
// Tools
// |
// Templates.
}
}
@Override
public boolean hasNext() {
return baseIterator.hasNext();
}
private class ScriptIterator implements IScriptIterator {
private final MemoryModule module;
private final Iterator<MemoryFile> files;
private MemoryFile current;
@Override
public IScriptIterator next() {
return new ScriptIterator(baseIterator.next());
}
public ScriptIterator(MemoryModule module) {
this.module = module;
files = module.data.iterator();
current = null;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet."); // To
// change
// body
// of
// generated
// methods,
// choose
// Tools
// |
// Templates.
}
}
@Override
public String getGroupName() {
return module.name;
}
private class ScriptIterator implements IScriptIterator {
@Override
public boolean next() {
if (files.hasNext()) {
current = files.next();
return true;
} else {
return false;
}
}
private final MemoryModule module;
private final Iterator<MemoryFile> files;
private MemoryFile current;
@Override
public String getName() {
return current.name;
}
public ScriptIterator(MemoryModule module) {
this.module = module;
files = module.data.iterator();
current = null;
}
@Override
public InputStream open() throws IOException {
return new ByteArrayInputStream(current.data);
}
}
@Override
public String getGroupName() {
return module.name;
}
private class MemoryModule {
private final String name;
private final List<MemoryFile> data;
@Override
public boolean next() {
if (files.hasNext()) {
current = files.next();
return true;
} else {
return false;
}
}
public MemoryModule(String name, List<MemoryFile> data) {
this.name = name;
this.data = data;
}
}
@Override
public String getName() {
return current.name;
}
private class MemoryFile {
private final String name;
private final byte[] data;
@Override
public InputStream open() throws IOException {
return new ByteArrayInputStream(current.data);
}
}
public MemoryFile(String name, byte[] data) {
this.name = name;
this.data = data;
}
}
private class MemoryModule {
private final String name;
private final List<MemoryFile> data;
public MemoryModule(String name, List<MemoryFile> data) {
this.name = name;
this.data = data;
}
}
private class MemoryFile {
private final String name;
private final byte[] data;
public MemoryFile(String name, byte[] data) {
this.name = name;
this.data = data;
}
}
}