Skip to content

Commit

Permalink
Switch to latticg maven, and add code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Mar 8, 2024
1 parent 406f308 commit f20575f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
24 changes: 21 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ archivesBaseName = project.archives_base_name
version = project.mod_version
group = project.maven_group

sourceSets {
codeGen
}

configurations {
includedLibrary
implementation.extendsFrom includedLibrary
Expand All @@ -21,6 +25,9 @@ repositories {
maven {
url = 'https://maven.seedfinding.com'
}
maven {
url = 'https://maven.latticg.com'
}
maven {
url = 'https://maven.xpple.dev/maven2'
}
Expand Down Expand Up @@ -55,7 +62,8 @@ dependencies {
includedLibrary "com.seedfinding:mc_core:${project.seedfinding_core_version}"
includedLibrary "com.seedfinding:mc_seed:${project.seedfinding_seed_version}"

includedLibrary files('libs/LattiCG-1.06-rt.jar')
includedLibrary "com.seedfinding:latticg:${project.latticg_version}:rt"
codeGenImplementation "com.seedfinding:latticg:${project.latticg_version}"

compileOnly 'com.demonwav.mcdev:annotations:2.0.0'

Expand All @@ -64,6 +72,8 @@ dependencies {
}

include api('net.fabricmc:mapping-io:0.5.1')

codeGenImplementation sourceSets.main.output
}

jar {
Expand All @@ -88,11 +98,12 @@ processResources {
}
}

tasks.withType(JavaCompile) {
tasks.withType(JavaCompile).configureEach {
options.encoding = "UTF-8"
}

task sourcesJar(type: Jar, dependsOn: classes) {
tasks.register('sourcesJar', Jar) {
dependsOn classes
archiveClassifier.set("sources")
from sourceSets.main.allSource
}
Expand All @@ -101,6 +112,13 @@ jar {
from "LICENSE"
}

tasks.register('codeGen', JavaExec) {
mainClass = 'net.earthcomputer.clientcommands.codegen.CodeGenerator'
classpath = sourceSets.codeGen.runtimeClasspath
args file('src/main/java').absolutePath
enableAssertions = true
}

// read the changelog from the changelog.txt file, make sure the changelog is recent to avoid accidentally using the previous version's changelog.
def changelogFile = file('changelog.txt')
def changelogText = changelogFile.exists() && System.currentTimeMillis() - changelogFile.lastModified() <= 1000 * 60 * 60 ?
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ org.gradle.jvmargs=-Xmx2G
seedfinding_biome_version=1.171.1
seedfinding_feature_version=1.171.1
seedfinding_seed_version=1.171.1
latticg_version=1.07
Binary file removed libs/LattiCG-1.06-rt.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.earthcomputer.clientcommands.codegen;

import com.seedfinding.latticg.generator.ClassGenerator;
import com.seedfinding.latticg.reversal.Program;
import com.seedfinding.latticg.reversal.ProgramBuilder;
import com.seedfinding.latticg.reversal.calltype.java.JavaCalls;
import com.seedfinding.latticg.util.LCG;
import net.earthcomputer.clientcommands.features.CCrackRng;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class CodeGenerator {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("clientcommands-codegen <destinationDir>");
return;
}

Path destDir = Path.of(args[0]);
genLattiCG(destDir);
}

private static void genLattiCG(Path destDir) throws IOException {
ProgramBuilder program = Program.builder(LCG.JAVA);
program.skip(-CCrackRng.NUM_THROWS * 4);
for (int i = 0; i < CCrackRng.NUM_THROWS; i++) {
program.skip(1);
program.add(JavaCalls.nextFloat().ranged(CCrackRng.MAX_ERROR * 2));
program.skip(2);
}

writeLattiCGClass(program.build(), "net.earthcomputer.clientcommands.features.CCrackRngGen", destDir);
}

private static void writeLattiCGClass(Program program, String fqName, Path destDir) throws IOException {
int dotIndex = fqName.lastIndexOf('.');
assert dotIndex >= 0;
String packageName = fqName.substring(0, dotIndex);
String className = fqName.substring(dotIndex + 1);

String generatedClass = new ClassGenerator(packageName, className, program).generate();
Files.writeString(destDir.resolve(fqName.replace('.', '/') + ".java"), generatedClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;

public class CCrackRng {
public static final int NUM_THROWS = 10;
// This value was computed by brute forcing all seeds
private static final float MAX_ERROR = 0.00883889f;
public static final float MAX_ERROR = 0.00883889f;

@FunctionalInterface
public interface OnCrack {
Expand All @@ -22,7 +23,7 @@ public interface OnCrack {


public static OnCrack callback;
public static float[] nextFloats = new float[10];
public static float[] nextFloats = new float[NUM_THROWS];
public static int expectedItems=0;
private static int attemptCount = 0;
private static final int MAX_ATTEMPTS = 5;
Expand All @@ -33,7 +34,7 @@ private static String throwItems() {
assert player != null;
player.moveTo(player.getX(), player.getY(), player.getZ(), player.getYRot(), 90);
player.connection.send(new ServerboundMovePlayerPacket.Rot(player.getYRot(), 90, true)); // point to correct location
ItemThrowTask task = new ItemThrowTask(10) {
ItemThrowTask task = new ItemThrowTask(NUM_THROWS) {
@Override
protected void onSuccess() {
CCrackRng.attemptCrack();
Expand All @@ -60,8 +61,7 @@ protected void onItemSpawn(ClientboundAddEntityPacket packet) {
}
}

public static void attemptCrack()
{
public static void attemptCrack() {
long[] seeds = CCrackRngGen.getSeeds(
Math.max(0, nextFloats[0] - MAX_ERROR), Math.min(1, nextFloats[0] + MAX_ERROR),
Math.max(0, nextFloats[1] - MAX_ERROR), Math.min(1, nextFloats[1] + MAX_ERROR),
Expand Down Expand Up @@ -102,7 +102,7 @@ private static void doCrack(OnCrack Callback){
ClientCommandHelper.addOverlayMessage(Component.translatable("commands.ccrackrng.retries", attemptCount, MAX_ATTEMPTS), 100);
currentTaskName = throwItems();
Configs.playerCrackState = PlayerRandCracker.CrackState.CRACKING;
expectedItems = 10;
expectedItems = NUM_THROWS;
if (attemptCount == 1) {
Component message = Component.translatable("commands.ccrackrng.starting")
.append(" ")
Expand All @@ -115,7 +115,7 @@ public static void onEntityCreation(ClientboundAddEntityPacket packet) {
if (Configs.playerCrackState == PlayerRandCracker.CrackState.CRACKING) {
if (CCrackRng.expectedItems > 0) {
float nextFloat = (float) Math.sqrt(packet.getXa() * packet.getXa() + packet.getZa() * packet.getZa()) * 50f;
CCrackRng.nextFloats[10 - CCrackRng.expectedItems] = nextFloat;
CCrackRng.nextFloats[NUM_THROWS - CCrackRng.expectedItems] = nextFloat;
CCrackRng.expectedItems--;
}
}
Expand Down

0 comments on commit f20575f

Please sign in to comment.