Skip to content

Commit 577bbc9

Browse files
committed
Make paths absolute and update the USB command
1 parent 265e5ed commit 577bbc9

6 files changed

Lines changed: 68 additions & 19 deletions

File tree

src/main/java/jaci/openrio/toast/core/ToastBootstrap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public static void main(String[] args) {
8383
}
8484

8585
if (isSimulation) {
86-
toastHome = new File("toast/");
87-
robotHome = new File("./");
86+
toastHome = new File("toast/").getAbsoluteFile();
87+
robotHome = new File("./").getAbsoluteFile();
8888
}
8989

9090
toastHome.mkdirs();

src/main/java/jaci/openrio/toast/core/ToastConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public static void init() {
2020

2121
public static enum Property {
2222
THREAD_POOL_SIZE("threading.pool_size", 2, "The amount of threads in the Thread Pool. The more threads, the more concurrent operations. This should be kept at 2 for most cases, but can be bumped up if required."),
23-
THREADED_LOADING("experimental.threaded_loading", false, "WARNING: EXPERIMENTAL CODE. DO NOT USE IN COMPETITION UNLESS YOU KNOW WHAT YOU ARE DOING! Should module loading be threaded? This has the advantage of speeding up loading times, but may be unstable for modules which aren't properly configured for concurrency."),
23+
THREADED_LOADING("experimental.threaded_loading", false, "Should module loading be threaded? This has the advantage of speeding up loading times, but may be unstable for modules which aren't properly configured for concurrency."),
2424
//THREADED_TICKING("experimental.threaded_ticking", false, "Should ticking by the StateTracker (autonomous, teleop, test, disabled, etc), be Threaded for each listener? This makes ticking run quickly and concurrently, but may be unstable for modules that aren't properly configured for concurrent operations."),
25+
JET_FUEL("jet.fuel", "steel beams", "JET FUEL CAN'T MELT STEEL BEAMS"),
2526
;
2627

2728
String key;
@@ -31,6 +32,8 @@ public static enum Property {
3132
Object value;
3233

3334
Property(String key, Object defaultValue, String comment) {
35+
if (key.startsWith("experimental."))
36+
comment = "WARNING: EXPERIMENTAL CODE. DO NOT USE IN COMPETITION UNLESS YOU KNOW WHAT YOU ARE DOING! " + comment;
3437
this.key = key;
3538
this.defaultValue = defaultValue;
3639
this.comments = MathHelper.splitStringByWords(comment, 15);

src/main/java/jaci/openrio/toast/core/command/cmd/CommandUSB.java

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import jaci.openrio.toast.core.io.usb.USBMassStorage;
88

99
import java.io.File;
10+
import java.io.FileInputStream;
11+
import java.io.FileOutputStream;
1012
import java.io.IOException;
13+
import java.nio.file.CopyOption;
1114
import java.nio.file.Files;
1215
import java.nio.file.Path;
1316
import java.nio.file.StandardCopyOption;
@@ -20,7 +23,9 @@ public String getCommandName() {
2023

2124
@Override
2225
public void invokeCommand(int argLength, String[] args, String command) {
23-
if (argLength == 1) {
26+
if (argLength == 1 || argLength == 2) {
27+
boolean specified = argLength == 2;
28+
2429
if (args[0].equals("generate")) {
2530
int count = 0;
2631
for (File file : USBMassStorage.invalidDrives) {
@@ -39,30 +44,62 @@ public void invokeCommand(int argLength, String[] args, String command) {
3944
} else if (args[0].equals("dump")) {
4045
for (MassStorageDevice drive : USBMassStorage.connectedDevices) {
4146
long time = System.currentTimeMillis();
42-
File root = new File(drive.toast_directory, "usb_dumps/dump_" + time);
47+
File root = new File(drive.dump_directory, "dump_" + time);
4348
try {
44-
copyDirectory(ToastBootstrap.toastHome, root);
45-
Toast.log().info("Data Dump Successful: dump_" + time + " on drive: " + drive.drive_name);
49+
if (!specified || drive.drive_name.equalsIgnoreCase(args[1])) {
50+
copyDirectory(ToastBootstrap.toastHome, root);
51+
Toast.log().info("Data Dump Successful: dump_" + time + " on drive: " + drive.drive_name);
52+
}
4653
} catch (IOException e) {
4754
Toast.log().error("Could not Dump Toast Files on drive: " + drive.drive_name);
4855
Toast.log().exception(e);
4956
}
5057
}
51-
}
58+
} else if (args[0].equals("load")) {
59+
for (MassStorageDevice drive : USBMassStorage.connectedDevices) {
60+
try {
61+
if (!specified || drive.drive_name.equalsIgnoreCase(args[1])) {
62+
copyDirectory(drive.toast_directory, ToastBootstrap.toastHome);
63+
Toast.log().info("Data Load Successful From Drive: " + drive.drive_name);
64+
}
65+
} catch (IOException e) {
66+
Toast.log().error("Could not Load Toast Files from drive: " + drive.drive_name);
67+
Toast.log().exception(e);
68+
}
69+
}
70+
} else
71+
usage();
5272
} else {
53-
Toast.log().info("Usage: usb <generate|dump>");
73+
usage();
5474
}
5575
}
5676

77+
private void usage() {
78+
Toast.log().warn("Usage: usb <generate|dump|load> [drive_name]");
79+
}
80+
5781
private void copyDirectory(File source, File dest) throws IOException {
5882
for (File f : source.listFiles()) {
59-
if (f.isDirectory())
60-
copyDirectory(f, dest);
61-
else {
62-
Path resolve = dest.toPath().resolve(f.toPath());
63-
resolve.toFile().getParentFile().mkdirs();
64-
Files.copy(f.getAbsoluteFile().toPath(), resolve);
65-
}
83+
File sourceFile = new File(source, f.getName());
84+
File destFile = new File(dest, f.getName());
85+
if (f.isDirectory()) {
86+
destFile.mkdirs();
87+
copyDirectory(sourceFile, destFile);
88+
} else
89+
copyFile(sourceFile, destFile);
90+
}
91+
}
92+
93+
private void copyFile(File source, File dest) throws IOException {
94+
FileInputStream inputStream = new FileInputStream(source);
95+
FileOutputStream outputStream = new FileOutputStream(dest);
96+
int lengthStream;
97+
byte[] buff = new byte[1024];
98+
while ((lengthStream = inputStream.read(buff)) > 0) {
99+
outputStream.write(buff, 0, lengthStream);
66100
}
101+
outputStream.close();
102+
inputStream.close();
67103
}
104+
68105
}

src/main/java/jaci/openrio/toast/core/io/usb/MassStorageDevice.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public class MassStorageDevice {
3232
* The directory that Toast files should be stored in on the drive
3333
*/
3434
public File toast_directory;
35+
36+
/**
37+
* The directory that USB Dumps are kept in
38+
*/
39+
public File dump_directory;
3540
public boolean override_modules;
3641
public boolean concurrent_modules;
3742

@@ -40,6 +45,7 @@ public MassStorageDevice(File path, GroovyPreferences autorun, String name) {
4045
this.preferences = autorun;
4146
this.drive_name = name;
4247
this.toast_directory = new File(path, autorun.getString("toast.directory"));
48+
this.dump_directory = new File(path, autorun.getString("toast.dumps"));
4349
this.toast_directory.mkdirs();
4450
this.override_modules = autorun.getBoolean("toast.override_modules");
4551
this.concurrent_modules = autorun.getBoolean("toast.concurrent_modules");

src/main/java/jaci/openrio/toast/core/io/usb/USBMassStorage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public static void crawlSym(File file) {
5555
}
5656

5757
GroovyPreferences pref = new GroovyPreferences(configuration);
58-
String drive_name = pref.getString("toast.device_name");
58+
String drive_name = pref.getString("toast.device_name").replace(" ", "_");
5959
MassStorageDevice device = new MassStorageDevice(canon, pref, drive_name);
6060
connectedDevices.add(device);
6161
if (device.override_modules && !device.concurrent_modules) override = true;
62-
logger.info("USB Mass Storage Device Detected -- Valid! " + canon);
62+
logger.info("USB Mass Storage Device Detected -- Valid! " + canon + " (" + device.drive_name + ")");
6363
} catch (Exception e) {
6464
e.printStackTrace();
6565
}

src/main/resources/assets/toast/autorun_default.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
toast {
22
// The name you wish to give to your USB Drive. This should be unique. Including your Team Number is good practise
3-
device_name = "Team #### USB Device"
3+
device_name = "Team_####_USB_Device"
44

55
// The directory you wish to use for your Toast files
66
directory = "toast"
77

8+
// The directory you wish for Toast Dumps to save on
9+
dumps = "toast_usb_dumps"
10+
811
// Should this USB drive override modules? If true, modules loaded in the robot's local files will be disabled.
912
// If false, modules on the robot's local files will be enabled, but modules on the USB device will be disabled.
1013
// Keep in mind, if one USB drive has this true, it loads from ALL usb devices.

0 commit comments

Comments
 (0)