generated from Zelaux/ZelauxMindustryModTemplate
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into pressure-rework-3
- Loading branch information
Showing
36 changed files
with
778 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="Omaloon [debug jar]" type="GradleRunConfiguration" factoryName="Gradle"> | ||
<ExternalSystemSettings> | ||
<option name="env"> | ||
<map> | ||
<entry key="lombok.plugin.debug_sleep" value="3600" /> | ||
</map> | ||
</option> | ||
<option name="executionName" /> | ||
<option name="externalProjectPath" value="$PROJECT_DIR$" /> | ||
<option name="externalSystemIdString" value="GRADLE" /> | ||
<option name="scriptParameters" value="--stacktrace" /> | ||
<option name="taskDescriptions"> | ||
<list /> | ||
</option> | ||
<option name="taskNames"> | ||
<list> | ||
<option value="jar" /> | ||
</list> | ||
</option> | ||
<option name="vmOptions" /> | ||
</ExternalSystemSettings> | ||
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess> | ||
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess> | ||
<DebugAllEnabled>false</DebugAllEnabled> | ||
<RunAsTest>false</RunAsTest> | ||
<method v="2" /> | ||
</configuration> | ||
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
println("Hello from gradle") | ||
apply plugin: "java" | ||
|
||
tasks.withType(JavaCompile).tap{ | ||
configureEach{ | ||
options.fork = true | ||
options.compilerArgs += [ | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED', | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.code.Symbol=ALL-UNNAMED', | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.code.Type=ALL-UNNAMED', | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED', | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED', | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED', | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED', | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED', | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED', | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.tree.JCTree=ALL-UNNAMED', | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED', | ||
'--add-exports', 'jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED', | ||
] | ||
} | ||
} | ||
repositories{ | ||
// Necessary Maven repositories to pull dependencies from. | ||
mavenCentral() | ||
|
||
maven{url "https://oss.sonatype.org/content/repositories/snapshots/"} | ||
maven{url "https://oss.sonatype.org/content/repositories/releases/"} | ||
maven{url "https://raw.githubusercontent.com/GlennFolker/EntityAnnoMaven/main"} | ||
|
||
// Use Zelaux's non-buggy repository for release Mindustry and Arc builds. | ||
maven{url "https://raw.githubusercontent.com/Zelaux/MindustryRepo/master/repository"} | ||
maven{url "https://raw.githubusercontent.com/Zelaux/Repo/master/repository" } | ||
} | ||
|
||
dependencies{ | ||
println(asmLib.invoke(":annotations:lombok-plugin-starter")) | ||
println(asmLib) | ||
implementation asmLib.invoke(":annotations:lombok-plugin-starter") | ||
|
||
|
||
compileOnly "org.jetbrains:annotations:24.0.1" | ||
|
||
implementation "com.github.javaparser:javaparser-symbol-solver-core:$jparcerVersion" | ||
|
||
compileOnly "org.projectlombok:lombok:$lombokVersion" | ||
annotationProcessor "org.projectlombok:lombok:$lombokVersion" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package omaloon.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.SOURCE) | ||
public @interface Load { | ||
/** | ||
* The region name to load. Variables can be used: | ||
* "@" -> block name | ||
* "@size" -> block size | ||
* "#" "#1" "#2" -> index number, for arrays | ||
*/ | ||
String value(); | ||
|
||
/** | ||
* 1D Array length, if applicable. | ||
*/ | ||
int length() default 1; | ||
|
||
/** | ||
* 2D array lengths. | ||
*/ | ||
int[] lengths() default {}; | ||
|
||
/** | ||
* Fallback strings used to replace "@" (the block name) if the region isn't found. | ||
*/ | ||
String[] fallback() default {}; | ||
} |
55 changes: 55 additions & 0 deletions
55
annotations/src/main/java/omaloon/annotations/Starter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package omaloon.annotations; | ||
|
||
import asmlib.annotations.LombokPluginStarter; | ||
import asmlib.annotations.Permit; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodHandles.Lookup; | ||
|
||
import com.sun.tools.javac.code.*; | ||
import com.sun.tools.javac.code.Source.*; | ||
|
||
import javax.annotation.processing.*; | ||
import javax.lang.model.*; | ||
import javax.lang.model.element.*; | ||
import java.lang.invoke.*; | ||
import java.lang.invoke.MethodHandles.*; | ||
import java.util.*; | ||
|
||
import static com.sun.tools.javac.code.Source.Feature.*; | ||
|
||
public class Starter extends LombokPluginStarter { | ||
static{ | ||
initSelf(omaloon.annotations.Starter.class); | ||
} | ||
static{ | ||
try{ | ||
// Get the trusted private lookup. | ||
Lookup lookup = (Lookup) Permit.getField(Lookup.class, "IMPL_LOOKUP").get(null); | ||
// Get the minimum level setter, to force certain features to qualify as a Java 8 feature. | ||
MethodHandle set = lookup.findSetter(Feature.class, "minLevel", Source.class); | ||
|
||
// Downgrade most Java 8-compatible features. | ||
for(Feature feature : new Feature[]{ | ||
EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES, | ||
PRIVATE_SAFE_VARARGS, | ||
DIAMOND_WITH_ANONYMOUS_CLASS_CREATION, | ||
LOCAL_VARIABLE_TYPE_INFERENCE, | ||
VAR_SYNTAX_IMPLICIT_LAMBDAS, | ||
SWITCH_MULTIPLE_CASE_LABELS, | ||
SWITCH_RULE, | ||
SWITCH_EXPRESSION, | ||
TEXT_BLOCKS, | ||
PATTERN_MATCHING_IN_INSTANCEOF, | ||
REIFIABLE_TYPES_INSTANCEOF | ||
}) set.invokeExact(feature, Source.JDK8); | ||
}catch(Throwable t){ | ||
throw new RuntimeException(t); | ||
} | ||
} | ||
@Override | ||
public String dontForgetToInitSelfInStatic(){ | ||
return "ok"; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
annotations/src/main/java/omaloon/annotations/lombok/FieldDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package omaloon.annotations.lombok; | ||
|
||
import com.github.javaparser.StaticJavaParser; | ||
import com.github.javaparser.ast.stmt.BlockStmt; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.experimental.FieldDefaults; | ||
import lombok.javac.JavacNode; | ||
import omaloon.annotations.Load; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Arrays; | ||
import java.util.StringJoiner; | ||
|
||
@FieldDefaults(makeFinal = true, level = AccessLevel.PUBLIC) | ||
@AllArgsConstructor | ||
public class FieldDescriptor { | ||
public static final String ARC_CORE_ATLAS_FIND = "arc.Core.atlas.find"; | ||
public static final String INDECIES__MARKER = "<<<INDECIES>>"; | ||
JavacNode field; | ||
Load an; | ||
|
||
public void addMe(BlockStmt block) { | ||
// JCTree.JCVariableDecl variableDecl = (JCTree.JCVariableDecl) field.get(); | ||
// variableDecl.getType() | ||
RegionName expression = new RegionName(); | ||
expression.string("this.%s%s = %s(".formatted(field.getName(), INDECIES__MARKER, ARC_CORE_ATLAS_FIND)); | ||
expression.join(RegionName.build('"' + an.value() + '"')); | ||
for (String fallback : an.fallback()) { | ||
expression.string(", " + ARC_CORE_ATLAS_FIND + "(\""); | ||
expression.join(RegionName.build(fallback)); | ||
expression.string("\""); | ||
} | ||
{ | ||
char[] close = new char[an.fallback().length + 1]; | ||
Arrays.fill(close, ')'); | ||
expression.string(new String(close)); | ||
} | ||
|
||
//TODO dimension check | ||
int dimension = (int) (expression.dimensions + Math.random() * 0); | ||
if (expression.dimensions != dimension) { | ||
field.addError("@Load Dimension mismatch"); | ||
return; | ||
} | ||
int[] lengths = lengths(); | ||
if (expression.dimensions > lengths.length) { | ||
field.addError("@Load expected %d lengths but found %d".formatted(expression.dimensions, lengths.length)); | ||
return; | ||
} | ||
|
||
if (expression.dimensions < lengths.length && an.lengths().length > 0) { | ||
field.addWarning("@Load the extra size for the array is indicated"); | ||
} | ||
|
||
|
||
//TODO check type | ||
if (false) { | ||
field.addError("@Load Expected TextureRegion as field type"); | ||
return; | ||
} | ||
|
||
|
||
int totalRounds = 1; | ||
for (int length : lengths) totalRounds *= length; | ||
|
||
if (totalRounds == 0) { | ||
field.addError("@Load one of given lengths are 0"); | ||
return; | ||
} | ||
|
||
if(expression.getItems().get(0) instanceof Reference.StringReference ref){ | ||
StringJoiner joiner=new StringJoiner("][","[","]"); | ||
RegionName indecies = new RegionName(); | ||
int markerIndex = ref.value.indexOf(INDECIES__MARKER); | ||
indecies.string(ref.value.substring(0, markerIndex)); | ||
for (int i = 0; i < expression.dimensions; i++) { | ||
indecies.string("["); | ||
indecies.index(i); | ||
indecies.string("]"); | ||
} | ||
indecies.string(ref.value.substring(markerIndex+INDECIES__MARKER.length())); | ||
for (int i = 1; i < expression.getItems().size(); i++) { | ||
indecies.add(expression.getItems().get(i)); | ||
} | ||
expression.getItems().clear(); | ||
expression.join(indecies); | ||
} | ||
int[] indecies = new int[expression.dimensions]; | ||
for (int __i = 0; __i < totalRounds; __i++) { | ||
String calculatedExpression = expression.calculate(indecies); | ||
|
||
block.addStatement( | ||
StaticJavaParser.parseStatement( | ||
calculatedExpression + ";" | ||
) | ||
); | ||
increase(indecies, lengths, 0); | ||
} | ||
|
||
} | ||
|
||
private void increase(int[] indecies, int[] lengths, int i) { | ||
if (i >= indecies.length) return; | ||
indecies[i]++; | ||
if (indecies[i] <= lengths[i]) return; | ||
indecies[i] = 0; | ||
increase(indecies, lengths, i + 1); | ||
} | ||
|
||
@NotNull | ||
private int[] lengths() { | ||
int[] lengths = an.lengths(); | ||
if (lengths.length == 0) lengths = new int[]{an.length()}; | ||
return lengths; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
annotations/src/main/java/omaloon/annotations/lombok/LoadAnnotationASTVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package omaloon.annotations.lombok; | ||
|
||
import asmlib.lombok.javaparser.CompileBodyVisitor; | ||
import com.github.javaparser.StaticJavaParser; | ||
import com.github.javaparser.ast.Modifier; | ||
import com.github.javaparser.ast.body.MethodDeclaration; | ||
import com.github.javaparser.ast.stmt.BlockStmt; | ||
import com.sun.tools.javac.tree.JCTree; | ||
import com.sun.tools.javac.util.List; | ||
import lombok.core.AST; | ||
import lombok.core.AnnotationValues; | ||
import lombok.javac.JavacASTAdapter; | ||
import lombok.javac.JavacNode; | ||
import lombok.javac.handlers.JavacHandlerUtil; | ||
import omaloon.annotations.Load; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
//TODO try handle something like @Load(name+"-hello") | ||
//TODO support for delegate function | ||
public class LoadAnnotationASTVisitor extends JavacASTAdapter { | ||
@Override | ||
public void visitType(JavacNode typeNode, JCTree.JCClassDecl type) { | ||
super.visitType(typeNode, type); | ||
ArrayList<FieldDescriptor> loadFields = new ArrayList<>(); | ||
JavacNode loadMethod = null; | ||
for (JavacNode field : typeNode.down()) { | ||
if (field.getKind() == AST.Kind.METHOD) { | ||
//TODO hierarchy check | ||
JCTree.JCMethodDecl methodDecl = (JCTree.JCMethodDecl) field.get(); | ||
if (field.getName().equals("load") && methodDecl.getParameters().isEmpty()) { | ||
loadMethod = field; | ||
} | ||
continue; | ||
} | ||
if (field.getKind() != AST.Kind.FIELD) { | ||
continue; | ||
} | ||
AnnotationValues<Load> annotation = field.findAnnotation(Load.class); | ||
if (annotation == null) continue; | ||
loadFields.add(new FieldDescriptor(field, annotation.getInstance())); | ||
} | ||
if (loadFields.isEmpty()) return; | ||
CompileBodyVisitor transformer = transformer(typeNode); | ||
if (loadMethod == null) { | ||
BlockStmt body = new BlockStmt(); | ||
MethodDeclaration declaration = new MethodDeclaration() | ||
.setName("load") | ||
.setType(void.class) | ||
.addAnnotation(Override.class) | ||
.setModifiers(Modifier.Keyword.PUBLIC) | ||
.setBody(body) | ||
; | ||
|
||
body.addStatement(StaticJavaParser.parseStatement("super.load();")); | ||
|
||
addFields(loadFields, body); | ||
|
||
JCTree.JCMethodDecl methodDecl = transformer.visit(declaration, null); | ||
JavacHandlerUtil.injectMethod(typeNode, methodDecl); | ||
} else { | ||
JCTree.JCMethodDecl decl = (JCTree.JCMethodDecl) loadMethod.get(); | ||
List<JCTree.JCStatement> oldStats = decl.body.stats; | ||
|
||
JCTree.JCBlock newBlock = transformer.visit(addFields(loadFields, new BlockStmt()), null); | ||
List<JCTree.JCStatement> stats = newBlock.stats; | ||
decl.body.stats= stats.appendList(oldStats); | ||
loadMethod.getAst().setChanged(); | ||
} | ||
|
||
} | ||
|
||
private static BlockStmt addFields(java.util.List<FieldDescriptor> loadFields, BlockStmt block) { | ||
for (FieldDescriptor field : loadFields) | ||
field.addMe(block); | ||
return block; | ||
} | ||
|
||
@NotNull | ||
private static CompileBodyVisitor transformer(JavacNode typeNode) { | ||
return new CompileBodyVisitor( | ||
typeNode.getTreeMaker(), | ||
typeNode.getAst(), | ||
typeNode.getContext() | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.