use of me.superckl.api.superscript.script.object.ScriptObject in project BiomeTweaker by superckl.
the class CommonProxy method setupScripts.
@Override
public void setupScripts(final ASMDataTable table) {
// Ensure BTParamterTypes registers its defaults
BTParameterTypes.BLOCKSTATE_BUILDER.getTypeClass();
LogHelper.debug("Discovering @AutoRegister script commands...");
final Map<Class<? extends ScriptObject>, Map<String, ScriptCommandListing>> listings = new HashMap<>();
final Set<ASMData> datas = table.getAll(AutoRegister.class.getCanonicalName());
final Set<ASMData> groupedDatas = table.getAll(AutoRegisters.class.getCanonicalName());
final Set<ASMData> allData = new HashSet<>(datas);
for (final ASMData data : groupedDatas) {
final List<Map<String, Object>> anns = WarningHelper.uncheckedCast(data.getAnnotationInfo().get("value"));
anns.forEach(l -> allData.add(new ASMData(data.getCandidate(), AutoRegister.class.getCanonicalName(), data.getClassName(), data.getObjectName(), l)));
}
final Set<String> examinedClasses = new HashSet<>();
for (final ASMData data : allData) try {
if (examinedClasses.contains(data.getClassName()))
continue;
final Class<?> asmClass = Class.forName(data.getClassName());
final Class<? extends ScriptCommand> scriptClass = asmClass.asSubclass(ScriptCommand.class);
final AutoRegister[] classAnns = scriptClass.getAnnotationsByType(AutoRegister.class);
final Constructor<?>[] cons = scriptClass.getConstructors();
for (final Constructor<?> con : cons) {
if (con.isAnnotationPresent(RegisterExempt.class))
continue;
final AutoRegister[] methodAnns = con.getAnnotationsByType(AutoRegister.class);
if (classAnns.length == 0 && methodAnns.length == 0)
continue;
AutoRegister[] annsToUse;
if (methodAnns.length != 0)
annsToUse = methodAnns;
else
annsToUse = classAnns;
final Class<?>[] cTypes = con.getParameterTypes();
final ParameterWrapper<?>[] pTypes = new ParameterWrapper[cTypes.length];
for (final ParameterOverride override : con.getAnnotationsByType(ParameterOverride.class)) pTypes[override.parameterIndex()] = ParameterTypes.getExceptionWrapper(override.exceptionKey());
for (int i = 0; i < cTypes.length; i++) {
if (pTypes[i] != null)
continue;
final ParameterType<?> type = ParameterTypes.getDefaultType(cTypes[i]);
if (type == null)
throw new IllegalStateException("No parameter type found for parameter " + cTypes[i].getCanonicalName());
pTypes[i] = type.getSimpleWrapper();
}
for (final AutoRegister ann : annsToUse) for (final Class<? extends ScriptObject> clazz : ann.classes()) {
if (!listings.containsKey(clazz))
listings.put(clazz, new HashMap<>());
final Map<String, ScriptCommandListing> map = listings.get(clazz);
if (!map.containsKey(ann.name()))
map.put(ann.name(), new ScriptCommandListing());
final ScriptCommandListing listing = map.get(ann.name());
listing.addEntry(Lists.newArrayList(pTypes), WarningHelper.uncheckedCast(con));
}
}
examinedClasses.add(data.getClassName());
} catch (final Exception e) {
LogHelper.error("Failed to auto-register a script command " + data.getAnnotationInfo().toString());
e.printStackTrace();
}
for (final Entry<Class<? extends ScriptObject>, Map<String, ScriptCommandListing>> entry : listings.entrySet()) {
LogHelper.debug("Registering " + entry.getValue().size() + " commands to " + entry.getKey().getSimpleName());
ScriptCommandRegistry.INSTANCE.registerClassListing(entry.getKey(), entry.getValue());
}
ScriptHandler.registerStaticObject("Tweaker", TweakerScriptObject.class);
try {
ConstructorListing<ScriptObject> listing = new ConstructorListing<>();
listing.addEntry(Lists.newArrayList(BTParameterTypes.BASIC_BIOMES_PACKAGE.getVarArgsWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("forBiomes", listing);
listing = new ConstructorListing<>();
listing.addEntry(Lists.newArrayList(BTParameterTypes.TYPE_BIOMES_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("forBiomesOfTypes", listing);
listing = new ConstructorListing<>();
listing.addEntry(Lists.newArrayList(BTParameterTypes.ALL_BIOMES_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("forAllBiomes", listing);
listing = new ConstructorListing<>();
listing.addEntry(Lists.newArrayList(BTParameterTypes.ALL_BUT_BIOMES_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("forAllBiomesExcept", listing);
listing = new ConstructorListing<>();
listing.addEntry(Lists.newArrayList(BTParameterTypes.INTERSECT_BIOMES_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("intersectionOf", listing);
listing = new ConstructorListing<>();
listing.addEntry(Lists.newArrayList(BTParameterTypes.SUBTRACT_BIOMES_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("subtractFrom", listing);
listing = new ConstructorListing<>();
listing.addEntry(Lists.newArrayList(BTParameterTypes.PROPERTY_RANGE_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("forBiomesWithPropertyRange", listing);
listing = new ConstructorListing<>();
listing.addEntry(new ArrayList<>(), OreDecorationScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("newOreDecoration", listing);
listing = new ConstructorListing<>();
listing.addEntry(new ArrayList<>(), TreesDecorationScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("newTreeDecoration", listing);
listing = new ConstructorListing<>();
listing.addEntry(new ArrayList<>(), ClusterDecorationScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("newClusterDecoration", listing);
listing = new ConstructorListing<>();
listing.addEntry(new ArrayList<>(), SplotchDecorationScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("newSplotchDecoration", listing);
listing = new ConstructorListing<>();
listing.addEntry(Lists.newArrayList(ParameterTypes.STRING.getSimpleWrapper()), BasicBlockStateScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("forBlock", listing);
listing = new ConstructorListing<>();
listing.addEntry(new ArrayList<>(), BlockReplacementScriptObject.class.getDeclaredConstructor());
ScriptParser.registerValidObjectInst("newBlockReplacement", listing);
} catch (final Exception e2) {
LogHelper.error("Failed to populate object listings! Some tweaks may not be applied.");
e2.printStackTrace();
}
}
use of me.superckl.api.superscript.script.object.ScriptObject in project BiomeTweaker by superckl.
the class ScriptParser method parseAssignment.
public static Map<String, Object> parseAssignment(final String script, final ScriptContext context, final ScriptHandler handler) throws Exception {
final String[] split = script.split("=");
if (split.length != 2) {
APIInfo.log.error("Failed to parse object assignment: " + script + " @ " + context + ". Reason: Incorrect '=' placement.");
return null;
}
final String var = split[0].trim();
final String assign = split[1].trim();
if (assign.startsWith("\"") && assign.endsWith("\"")) {
final String shortcut = ParameterTypes.STRING.tryParse(assign, handler);
return CollectionHelper.linkedMapWithEntry(var, (Object) shortcut);
} else {
String called;
try {
called = ScriptParser.getCommandCalled(assign);
} catch (final IllegalArgumentException e) {
APIInfo.log.error("Failed to parse object assignment: " + script + " @ " + context + ". Reason: " + e.getMessage());
return null;
}
if (ScriptParser.validObjects.containsKey(called)) {
final ConstructorListing<ScriptObject> listing = ScriptParser.validObjects.get(called);
String[] arguments = CollectionHelper.trimAll(ScriptParser.parseArguments(assign));
for (final Entry<List<ParameterWrapper<?>>, Constructor<? extends ScriptObject>> entry : listing.getConstructors().entrySet()) {
final List<Object> objs = new ArrayList<>();
final List<ParameterWrapper<?>> params = new ArrayList<>(entry.getKey());
final Iterator<ParameterWrapper<?>> it = params.iterator();
while (it.hasNext()) {
final ParameterWrapper<?> wrap = it.next();
final Pair<?, String[]> parsed = wrap.parseArgs(handler, arguments);
CollectionHelper.addAllFromArray(objs, parsed.getKey());
arguments = parsed.getValue();
it.remove();
}
if (!params.isEmpty() || (arguments.length != 0))
continue;
final Object[] args = new Object[objs.size()];
System.arraycopy(objs.toArray(), 0, args, 0, objs.size());
final ScriptObject obj = entry.getValue().getDeclaringClass().newInstance();
obj.readArgs(args);
return CollectionHelper.linkedMapWithEntry(var, (Object) obj);
}
}
APIInfo.log.error("Failed to find meaning in object assignment " + script + " @ " + context + ". Reason: No matching objects or registrations found.");
}
return null;
}
Aggregations