1
0
mirror of https://github.com/openeggbert/sprite-utils.git synced 2025-03-14 23:33:28 +01:00

Added the support for the command line options

This commit is contained in:
Robert Vokac 2024-07-30 20:55:06 +02:00
parent 30efc3fe86
commit 2f6218c754
No known key found for this signature in database
GPG Key ID: C459E1E4B4A986BB
8 changed files with 150 additions and 99 deletions

22
pom.xml
View File

@ -110,22 +110,7 @@
</build>
<dependencies>
<!-- Power dependencies -->
<dependency>
<groupId>org.nanoboot.powerframework</groupId>
<artifactId>power-time</artifactId>
<version>${power.version}</version>
</dependency>
<dependency>
<groupId>org.nanoboot.powerframework</groupId>
<artifactId>power-random</artifactId>
<version>${power.version}</version>
</dependency>
<dependency>
<groupId>org.nanoboot.powerframework</groupId>
<artifactId>power-collections</artifactId>
<version>${power.version}</version>
</dependency>
<!-- Other dependencies -->
<dependency>
@ -176,11 +161,6 @@
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>dev.mccue</groupId>
<artifactId>guava-io</artifactId>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>org.jclarion</groupId>
<artifactId>image4j</artifactId>

View File

@ -1,3 +0,0 @@
set_last_mod_time_the_same_as_this_file="$1"
set_last_mod_time_for_this_file="$2"
touch -r "$set_last_mod_time_the_same_as_this_file" "$set_last_mod_time_for_this_file"

View File

@ -22,9 +22,6 @@ module spriteutils {
requires org.apache.commons.io;
requires lombok;
requires org.apache.logging.log4j;
requires powerframework.time;
requires powerframework.collections;
requires dev.mccue.guava.io;
requires java.desktop;
requires image4j;
}

View File

@ -42,6 +42,7 @@ import org.nanoboot.spriteutils.core.SpriteSheet;
import org.nanoboot.spriteutils.core.SpriteSheetRow;
import org.nanoboot.spriteutils.core.SpriteUtilsArgs;
import org.nanoboot.spriteutils.core.SpriteUtilsException;
import org.nanoboot.spriteutils.core.SpriteUtilsOptions;
import org.nanoboot.spriteutils.core.Utils;
/**
@ -138,54 +139,69 @@ public class DrawCommand implements Command {
LOG.error("SpriteUtilsArgs cannot be null.");
throw new IllegalArgumentException("SpriteUtilsArgs cannot be null.");
}
SpriteUtilsOptions spriteUtilsOptions = new SpriteUtilsOptions(args);
File workingDirectory = new File("/rv/data/desktop/code/code.nanoboot.org/nanoboot/open-eggbert-data/Speedy_Eggbert_1/Game/IMAGE08");
File testFile = new File(workingDirectory, "BLUPI000.BLP");
File backupFile = new File(testFile.getAbsolutePath() + ".backup");
try {
if (backupFile.exists()) {
testFile.delete();
Utils.copyFile(backupFile, testFile);
} else {
Utils.copyFile(testFile, backupFile);
File workingDirectory = new File(spriteUtilsOptions.getWorkingDirectory());
System.out.println("Going to process images in directory: " + workingDirectory);
for (File imageFile : workingDirectory.listFiles()) {
if (spriteUtilsOptions.getFileName().isPresent() && !spriteUtilsOptions.getFileName().get().equals(imageFile.getName())) {
continue;
}
File backupFile = new File(imageFile.getAbsolutePath() + ".backup");
try {
if (backupFile.exists()) {
imageFile.delete();
Utils.copyFile(backupFile, imageFile);
} else {
Utils.copyFile(imageFile, backupFile);
}
} catch (SpriteUtilsException e) {
LOG.error("Error managing backup files", e);
throw new SpriteUtilsException("Error managing backup files", e);
}
BMPImage image = null;
try {
image = BMPDecoder.readExt(imageFile);
} catch (IOException e) {
LOG.error("Reading image failed: {}", imageFile.getAbsolutePath(), e);
throw new SpriteUtilsException("Reading image failed", e);
}
BufferedImage bi = image.getImage();
Graphics2D g = bi.createGraphics();
Supplier<Integer> randomByte = () -> random.apply(0, 255);
Stroke dashedStroke = configureGraphics(g);
SpriteSheet spriteSheet = new SpriteSheet(new File(spriteUtilsOptions.getSpriteSheetPath()));
spriteSheet
.getSpriteSheets(imageFile.getName().toLowerCase())
.stream()
.filter(s -> spriteUtilsOptions.getRow().isEmpty() ? true : (spriteUtilsOptions.getRow().get() == s.getRow()))
.forEach(row -> {
drawSpriteSheetRow(row, g, dashedStroke, spriteUtilsOptions);
});
g.dispose();
try {
BMPEncoder.write(bi, imageFile);
} catch (IOException e) {
LOG.error("Writing image failed: {}", imageFile.getAbsolutePath(), e);
throw new SpriteUtilsException("Writing image failed", e);
}
LOG.info("Image details - Colour Count: {}, Colour Depth: {}, Height: {}, Width: {}, Indexed: {}",
image.getColourCount(), image.getColourDepth(), image.getHeight(), image.getWidth(), image.isIndexed());
if (spriteUtilsOptions.getFileName().isPresent() && spriteUtilsOptions.getFileName().equals(imageFile.getName())) {
break;
}
} catch (SpriteUtilsException e) {
LOG.error("Error managing backup files", e);
throw new SpriteUtilsException("Error managing backup files", e);
}
BMPImage image = null;
try {
image = BMPDecoder.readExt(testFile);
} catch (IOException e) {
LOG.error("Reading image failed: {}", testFile.getAbsolutePath(), e);
throw new SpriteUtilsException("Reading image failed", e);
}
BufferedImage bi = image.getImage();
Graphics2D g = bi.createGraphics();
Supplier<Integer> randomByte = () -> random.apply(0, 255);
Stroke dashedStroke = configureGraphics(g);
SpriteSheet spriteSheet = new SpriteSheet(new File(workingDirectory, "spritesheet.csv"));
spriteSheet.getSpriteSheets(testFile.getName().toLowerCase()).forEach(row -> {
drawSpriteSheetRow(row, g, dashedStroke);
});
g.dispose();
try {
BMPEncoder.write(bi, testFile);
} catch (IOException e) {
LOG.error("Writing image failed: {}", testFile.getAbsolutePath(), e);
throw new SpriteUtilsException("Writing image failed", e);
}
LOG.info("Image details - Colour Count: {}, Colour Depth: {}, Height: {}, Width: {}, Indexed: {}",
image.getColourCount(), image.getColourDepth(), image.getHeight(), image.getWidth(), image.isIndexed());
return "";
}
private void drawSpriteSheetRow(SpriteSheetRow row, Graphics2D g, Stroke dashedStroke) {
private void drawSpriteSheetRow(SpriteSheetRow row, Graphics2D g, Stroke dashedStroke, SpriteUtilsOptions spriteUtilsOptions) {
int startX = row.getX();
int endX = startX + row.getWidth() - 1;
int startY = row.getY();

View File

@ -29,7 +29,7 @@ public class Main {
System.out.println("Sprite Utils - tool used to work with sprites\n");
SpriteUtils spriteUtils = new SpriteUtils();
args = new String[]{"draw"};
spriteUtils.run(args);
}

View File

@ -21,6 +21,7 @@ package org.nanoboot.spriteutils.core;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import lombok.Getter;
/**
@ -56,15 +57,16 @@ public class SpriteUtilsArgs {
if (args.length > 1) {
for (String arg : args) {
if (arg == null) {
continue;
for (int i = 1;i< args.length;i++) {
String key = args[i];
if(args.length < (i+2)) {
throw new SpriteUtilsException("Missing value for option: " + key);
}
if (args[0].equals(arg)) {
continue;
}
String[] keyValue = arg.split("=", 2);
internalMap.put(keyValue[0], keyValue.length > 1 ? keyValue[1] : null);
++i;
String value = args[i];
internalMap.put(key, value);
}
for (String key : internalMap.keySet()) {
System.out.println("Found argument: " + key + "(=)" + internalMap.get(key));
@ -85,6 +87,14 @@ public class SpriteUtilsArgs {
return internalMap.get(arg);
}
public Optional<String> getArgumentOptional(String arg) {
return Optional.ofNullable(hasArgument(arg) ? internalMap.get(arg) : null);
}
public Boolean getBooleanArgument(String arg) {
return hasArgument(arg) ? internalMap.get(arg).equals("true") : false;
}
public boolean isVerboseLoggingEnabled() {
return hasArgument("verbose") && getArgument("verbose").equals("true");
}

View File

@ -0,0 +1,74 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// sprite-utils: Tool used to work with sprites
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package org.nanoboot.spriteutils.core;
import java.awt.Color;
import java.util.Optional;
/**
*
* @author robertvokac
*/
public class SpriteUtilsOptions {
private final SpriteUtilsArgs spriteUtilsArgs;
public SpriteUtilsOptions(SpriteUtilsArgs spriteUtilsArgs) {
this.spriteUtilsArgs = spriteUtilsArgs;
}
public String getWorkingDirectory() {
return spriteUtilsArgs.getArgumentOptional("--dir").orElse(".");
}
public boolean isDrawNumberEnabled() {
return spriteUtilsArgs.getBooleanArgument("--draw-number");
}
public Color getRectangleColor() {
Optional<String> arg = spriteUtilsArgs.getArgumentOptional("--rectangle-color");
if (arg.isEmpty()) {
return Color.RED;
}
String[] array = arg.get().split(",");
if (array.length != 3) {
throw new SpriteUtilsException("Invalid format of rectangle-color option: " + arg.get());
}
return new Color(
Integer.parseInt(array[0]),
Integer.parseInt(array[1]),
Integer.parseInt(array[2])
);
}
public String getSpriteSheetPath() {
return spriteUtilsArgs.getArgumentOptional("--sprite-sheet-path").orElse(getWorkingDirectory() + "/spritesheet.csv");
}
public Optional<String> getFileName() {
return spriteUtilsArgs.getArgumentOptional("--file-name");
}
public Optional<Integer> getRow() {
Optional<String> arg = spriteUtilsArgs.getArgumentOptional("--row");
return arg.isEmpty() ? Optional.empty() : Optional.of(Integer.valueOf(arg.get()));
}
}

View File

@ -19,8 +19,6 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package org.nanoboot.spriteutils.core;
import dev.mccue.guava.hash.Hashing;
import dev.mccue.guava.io.Files;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
@ -124,27 +122,6 @@ public class Utils {
return resultStringBuilder.toString();
}
public static String calculateSHA512Hash(File file) {
try {
return Files.hash(file, Hashing.sha512()).toString();
} catch (IOException ex) {
System.err.println(ex.getMessage());
throw new SpriteUtilsException(ex);
}
}
public static String calculateSHA256Hash(File file) {
if (file.isDirectory()) {
return "";
}
try {
return Files.hash(file, Hashing.sha256()).toString();
} catch (IOException ex) {
System.err.println(ex.getMessage());
throw new SpriteUtilsException(ex);
}
}
public static String encodeBase64(String s) {
return Base64.getEncoder().encodeToString(s.getBytes());