Skip to content

Commit

Permalink
replay tool
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonwatson committed Jul 8, 2024
1 parent 5459345 commit 184890a
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,5 @@ simgui-ds.json
simgui.json

!src/main/deploy/photon-configs/*.zip

/prev-log.txt
45 changes: 45 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,37 @@ task(checkAkitInstall, dependsOn: "classes", type: JavaExec) {
}
compileJava.finalizedBy checkAkitInstall

sourceSets {
tools {
compileClasspath += sourceSets.main.compileClasspath + sourceSets.main.output
runtimeClasspath += sourceSets.main.runtimeClasspath + sourceSets.main.output
}
}

import org.gradle.internal.os.OperatingSystem

project.ext.lwjglVersion = "3.3.3"

switch (OperatingSystem.current()) {
case OperatingSystem.LINUX:
project.ext.lwjglNatives = "natives-linux"
def osArch = System.getProperty("os.arch")
if (osArch.startsWith("arm") || osArch.startsWith("aarch64")) {
project.ext.lwjglNatives += osArch.contains("64") || osArch.startsWith("armv8") ? "-arm64" : "-arm32"
} else if (osArch.startsWith("ppc")) {
project.ext.lwjglNatives += "-ppc64le"
} else if (osArch.startsWith("riscv")) {
project.ext.lwjglNatives += "-riscv64"
}
break
case OperatingSystem.MAC_OS:
project.ext.lwjglNatives = "natives-macos"
break
case OperatingSystem.WINDOWS:
project.ext.lwjglNatives = "natives-windows"
break
}

dependencies {
def akitJson = new groovy.json.JsonSlurper().parseText(new File(projectDir.getAbsolutePath() + "/vendordeps/AdvantageKit.json").text)
annotationProcessor "org.littletonrobotics.akit.junction:junction-autolog:$akitJson.version"
Expand All @@ -104,6 +135,14 @@ dependencies {

implementation 'org.apache.httpcomponents:httpclient:4.5.14'
implementation 'org.apache.httpcomponents:httpmime:4.5.14'

toolsImplementation platform("org.lwjgl:lwjgl-bom:$lwjglVersion")
toolsImplementation "org.lwjgl:lwjgl"
toolsImplementation "org.lwjgl:lwjgl-nfd"
toolsRuntimeOnly "org.lwjgl:lwjgl::$lwjglNatives"
toolsRuntimeOnly "org.lwjgl:lwjgl-nfd::$lwjglNatives"
toolsImplementation wpi.java.deps.wpilibJniRelease(wpi.platforms.desktop)
toolsImplementation wpi.java.vendor.jniRelease(wpi.platforms.desktop)
}

// Simulation configuration (e.g. environment variables).
Expand Down Expand Up @@ -150,3 +189,9 @@ gversion {
timeZone = "America/New_York"
indent = " "
}

task(replay, dependsOn: "build", type: JavaExec) {
mainClass = "frc.robot.Replay"
classpath = sourceSets.tools.runtimeClasspath
systemProperties["java.library.path"] = new File(project.projectDir, "build/jni/release")
}
9 changes: 9 additions & 0 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ public void testInit() {
@Override
public void testPeriodic() {}

private static String logFile = null;

public static void setReplayFile(String logFile) {
Robot.logFile = logFile;
}


private static final String environmentVariable = "AKIT_LOG_PATH";
private static final String advantageScopeFileName = "akit-log-path.txt";
Expand All @@ -192,6 +198,9 @@ public void testPeriodic() {}
* available 3. The result of the prompt displayed to the user
*/
public static String findReplayLog() {
if (Robot.logFile != null) {
return Robot.logFile;
}
// Read environment variables
String envPath = System.getenv(environmentVariable);
if (envPath != null) {
Expand Down
77 changes: 77 additions & 0 deletions src/tools/java/frc/robot/Replay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package frc.robot;

import static org.lwjgl.system.MemoryStack.stackPush;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import org.littletonrobotics.junction.LogFileUtil;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.util.nfd.NFDFilterItem;
import org.lwjgl.util.nfd.NativeFileDialog;
import edu.wpi.first.wpilibj.Filesystem;
import edu.wpi.first.wpilibj.RobotBase;

/** */
public class Replay {

/** */
public static void main(String[] argv) {
File prevPath = new File(Filesystem.getLaunchDirectory(), "prev-log.txt");
NativeFileDialog.NFD_Init();
String logPath;
try (MemoryStack stack = stackPush()) {
NFDFilterItem.Buffer filters = NFDFilterItem.malloc(1);
filters.get(0).name(stack.UTF8("WPILog Files")).spec(stack.UTF8("wpilog"));

ByteBuffer defaultPath = null;
if (prevPath.exists()) {
String prev = new File(readToString(prevPath).trim()).getParent();
defaultPath = stack.UTF8(prev);
}

PointerBuffer pp = stack.mallocPointer(1);
int res = NativeFileDialog.NFD_OpenDialog(pp, filters, defaultPath);
if (res != NativeFileDialog.NFD_OKAY) {
return;
}
logPath = pp.getStringUTF8();
}
NativeFileDialog.NFD_Quit();
try {
try (PrintWriter out = new PrintWriter(prevPath)) {
out.print(logPath);
}
} catch (IOException e) {
e.printStackTrace();
}

Robot.setReplayFile(logPath);
RobotBase.startRobot(Robot::new);
}

private static String readToString(File f) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(f));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
// delete the last new line separator
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
reader.close();
return stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

}

0 comments on commit 184890a

Please sign in to comment.