Skip to content

Commit ee74c1a

Browse files
authored
Add Custom Script Directory Support (#41)
- Scripts can now be placed in subdirectories under /scripts (subdirectory names can be anything). - These subdirectories are ignored by Git (so your custom scripts won’t get committed by accident). - Scripts stored this way are still fully functional and discoverable by the system, just like scripts in the top-level /scripts folder. - `PointSelector` now has a second function `getRandomPointByColour() to accept `ColourObj`s directly, allowing users to store colours inline.
1 parent b73b3e5 commit ee74c1a

File tree

3 files changed

+27
-43
lines changed

3 files changed

+27
-43
lines changed

src/main/java/com/chromascape/utils/actions/PointSelector.java

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,41 @@
1515
import org.apache.logging.log4j.Logger;
1616

1717
/**
18-
* The {@code PointSelector} class provides utility methods for selecting random
19-
* points within
20-
* graphical zones. These methods are designed to reduce code duplication and
21-
* streamline common
22-
* actions when automating interactions with graphical objects like colours or
23-
* images.
18+
* The {@code PointSelector} class provides utility methods for selecting random points within
19+
* graphical zones. These methods are designed to reduce code duplication and streamline common
20+
* actions when automating interactions with graphical objects like colours or images.
2421
*
25-
* <p>
26-
* <b>Features:</b>
22+
* <p><b>Features:</b>
2723
*
2824
* <ul>
29-
* <li>Finds a random point within the bounding box of a detected image
30-
* template.
31-
* <li>Finds a random point inside the contour of the first detected object of a
32-
* specified color.
25+
* <li>Finds a random point within the bounding box of a detected image template.
26+
* <li>Finds a random point inside the contour of the first detected object of a specified colour.
3327
* </ul>
3428
*
35-
* <p>
36-
* These utilities are commonly reused across scripts written for the
37-
* ChromaScape automation
38-
* framework. The class does not perform any input actions (such as clicking),
39-
* but provides the
29+
* <p>These utilities are commonly reused across scripts written for the ChromaScape automation
30+
* framework. The class does not perform any input actions (such as clicking), but provides the
4031
* coordinates needed for such actions.
4132
*
42-
* <p>
43-
* <b>Typical Usage:</b>
33+
* <p><b>Typical Usage:</b>
4434
*
4535
* <pre>
4636
* Point pointInImage = PointSelector.getRandomPointInImage(templatePath, gameView, threshold);
4737
* Point pointInColour = PointSelector.getRandomPointInColour(gameView, "Purple", maxAttempts);
4838
* </pre>
4939
*
50-
* <p>
51-
* All methods are static and thread-safe.
40+
* <p>All methods are static and thread-safe.
5241
*/
5342
public class PointSelector {
5443

5544
private static final Logger logger = LogManager.getLogger(PointSelector.class);
5645

5746
/**
58-
* Searches for the provided image template within the current game view, then
59-
* returns a random
60-
* point within the detected bounding box if the match exceeds the defined
61-
* threshold.
47+
* Searches for the provided image template within the current game view, then returns a random
48+
* point within the detected bounding box if the match exceeds the defined threshold.
6249
*
63-
* @param templatePath the BufferedImage template to locate and click within the
64-
* larger image view
65-
* @param image the larger image, what you're searching in
66-
* @param threshold the openCV threshold to decide if a match exists
50+
* @param templatePath the BufferedImage template to locate and click within the larger image view
51+
* @param image the larger image, what you're searching in
52+
* @param threshold the openCV threshold to decide if a match exists
6753
* @return The point to click
6854
*/
6955
public static Point getRandomPointInImage(
@@ -86,22 +72,19 @@ public static Point getRandomPointInImage(
8672
}
8773

8874
/**
89-
* Attempts to find a random point inside the contour of the first object of the
90-
* specified color.
75+
* Attempts to find a random point inside the contour of the first object of the specified colour.
9176
*
92-
* @param image the image to search in (e.g. game view from controller)
93-
* @param colourName the name of the color (must match ColourInstances key,
94-
* e.g. "Purple")
95-
* @param maxAttempts maximum number of attempts to find a point inside the
96-
* contour
77+
* @param image the image to search in (e.g. game view from controller)
78+
* @param colourName the name of the colour (must match ColourInstances key, e.g. "Purple")
79+
* @param maxAttempts maximum number of attempts to find a point inside the contour
9780
* @return a random Point inside the contour, or null if not found/error
9881
*/
9982
public static Point getRandomPointInColour(
10083
BufferedImage image, String colourName, int maxAttempts) {
101-
return getRandomPointByColor(image, ColourInstances.getByName(colourName), maxAttempts);
84+
return getRandomPointByColour(image, ColourInstances.getByName(colourName), maxAttempts);
10285
}
10386

104-
public static Point getRandomPointByColor(
87+
public static Point getRandomPointByColour(
10588
BufferedImage image, ColourObj colourName, int maxAttempts) {
10689
List<ChromaObj> objs;
10790
try {

src/main/java/com/chromascape/web/SendScripts.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@ public class SendScripts {
2727
/**
2828
* Returns a list of script file names located in the {@code scripts} directory.
2929
*
30-
* <p>This endpoint scans only the top-level entries (non-recursive).
30+
* <p>This endpoint scans the directory recursively so nested folders are included in the results.
3131
*
3232
* @return a list of script file names relative to {@code SCRIPTS_DIR}
3333
* @throws IOException if an I/O error occurs while reading the directory
3434
*/
3535
@GetMapping("/scripts")
3636
public List<String> getScripts() throws IOException {
37-
try (Stream<Path> stream = Files.walk(SCRIPTS_DIR, 1)) {
37+
try (Stream<Path> stream = Files.walk(SCRIPTS_DIR)) {
3838
return stream
39-
.filter(path -> !path.equals(SCRIPTS_DIR))
39+
.filter(Files::isRegularFile)
4040
.map(SCRIPTS_DIR::relativize)
41-
.map(Path::toString)
41+
.map(path -> path.toString().replace("\\", "/"))
42+
.sorted()
4243
.collect(Collectors.toList());
4344
}
4445
}

src/main/java/com/chromascape/web/instance/ScriptInstance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public ScriptInstance(RunConfig config, WebSocketStateHandler stateHandler)
3838
this.stateHandler = stateHandler;
3939

4040
String fileName = config.getScript();
41-
String className = fileName.replace(".java", "");
41+
String className = fileName.replace(".java", "").replace("/", ".");
4242

4343
Class<?> script = Class.forName("com.chromascape.scripts." + className);
4444
Constructor<?> constructor = script.getDeclaredConstructor(boolean.class);

0 commit comments

Comments
 (0)