mirror of
https://github.com/openeggbert/sprite-utils.git
synced 2025-03-25 15:37:50 +01:00
Added the support for the command line options
This commit is contained in:
parent
30efc3fe86
commit
2f6218c754
22
pom.xml
22
pom.xml
@ -110,22 +110,7 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<dependencies>
|
<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 -->
|
<!-- Other dependencies -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -176,11 +161,6 @@
|
|||||||
<artifactId>log4j-slf4j-impl</artifactId>
|
<artifactId>log4j-slf4j-impl</artifactId>
|
||||||
<version>${log4j.version}</version>
|
<version>${log4j.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>dev.mccue</groupId>
|
|
||||||
<artifactId>guava-io</artifactId>
|
|
||||||
<version>0.0.3</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jclarion</groupId>
|
<groupId>org.jclarion</groupId>
|
||||||
<artifactId>image4j</artifactId>
|
<artifactId>image4j</artifactId>
|
||||||
|
@ -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"
|
|
@ -22,9 +22,6 @@ module spriteutils {
|
|||||||
requires org.apache.commons.io;
|
requires org.apache.commons.io;
|
||||||
requires lombok;
|
requires lombok;
|
||||||
requires org.apache.logging.log4j;
|
requires org.apache.logging.log4j;
|
||||||
requires powerframework.time;
|
|
||||||
requires powerframework.collections;
|
|
||||||
requires dev.mccue.guava.io;
|
|
||||||
requires java.desktop;
|
requires java.desktop;
|
||||||
requires image4j;
|
requires image4j;
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,7 @@ import org.nanoboot.spriteutils.core.SpriteSheet;
|
|||||||
import org.nanoboot.spriteutils.core.SpriteSheetRow;
|
import org.nanoboot.spriteutils.core.SpriteSheetRow;
|
||||||
import org.nanoboot.spriteutils.core.SpriteUtilsArgs;
|
import org.nanoboot.spriteutils.core.SpriteUtilsArgs;
|
||||||
import org.nanoboot.spriteutils.core.SpriteUtilsException;
|
import org.nanoboot.spriteutils.core.SpriteUtilsException;
|
||||||
|
import org.nanoboot.spriteutils.core.SpriteUtilsOptions;
|
||||||
import org.nanoboot.spriteutils.core.Utils;
|
import org.nanoboot.spriteutils.core.Utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -138,54 +139,69 @@ public class DrawCommand implements Command {
|
|||||||
LOG.error("SpriteUtilsArgs cannot be null.");
|
LOG.error("SpriteUtilsArgs cannot be null.");
|
||||||
throw new IllegalArgumentException("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 workingDirectory = new File(spriteUtilsOptions.getWorkingDirectory());
|
||||||
File testFile = new File(workingDirectory, "BLUPI000.BLP");
|
System.out.println("Going to process images in directory: " + workingDirectory);
|
||||||
File backupFile = new File(testFile.getAbsolutePath() + ".backup");
|
for (File imageFile : workingDirectory.listFiles()) {
|
||||||
try {
|
if (spriteUtilsOptions.getFileName().isPresent() && !spriteUtilsOptions.getFileName().get().equals(imageFile.getName())) {
|
||||||
if (backupFile.exists()) {
|
continue;
|
||||||
testFile.delete();
|
}
|
||||||
Utils.copyFile(backupFile, testFile);
|
|
||||||
} else {
|
File backupFile = new File(imageFile.getAbsolutePath() + ".backup");
|
||||||
Utils.copyFile(testFile, backupFile);
|
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 "";
|
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 startX = row.getX();
|
||||||
int endX = startX + row.getWidth() - 1;
|
int endX = startX + row.getWidth() - 1;
|
||||||
int startY = row.getY();
|
int startY = row.getY();
|
||||||
|
@ -29,7 +29,7 @@ public class Main {
|
|||||||
System.out.println("Sprite Utils - tool used to work with sprites\n");
|
System.out.println("Sprite Utils - tool used to work with sprites\n");
|
||||||
|
|
||||||
SpriteUtils spriteUtils = new SpriteUtils();
|
SpriteUtils spriteUtils = new SpriteUtils();
|
||||||
args = new String[]{"draw"};
|
|
||||||
spriteUtils.run(args);
|
spriteUtils.run(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ package org.nanoboot.spriteutils.core;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -56,15 +57,16 @@ public class SpriteUtilsArgs {
|
|||||||
|
|
||||||
if (args.length > 1) {
|
if (args.length > 1) {
|
||||||
|
|
||||||
for (String arg : args) {
|
for (int i = 1;i< args.length;i++) {
|
||||||
if (arg == null) {
|
|
||||||
continue;
|
String key = args[i];
|
||||||
|
if(args.length < (i+2)) {
|
||||||
|
throw new SpriteUtilsException("Missing value for option: " + key);
|
||||||
}
|
}
|
||||||
if (args[0].equals(arg)) {
|
++i;
|
||||||
continue;
|
String value = args[i];
|
||||||
}
|
|
||||||
String[] keyValue = arg.split("=", 2);
|
internalMap.put(key, value);
|
||||||
internalMap.put(keyValue[0], keyValue.length > 1 ? keyValue[1] : null);
|
|
||||||
}
|
}
|
||||||
for (String key : internalMap.keySet()) {
|
for (String key : internalMap.keySet()) {
|
||||||
System.out.println("Found argument: " + key + "(=)" + internalMap.get(key));
|
System.out.println("Found argument: " + key + "(=)" + internalMap.get(key));
|
||||||
@ -85,6 +87,14 @@ public class SpriteUtilsArgs {
|
|||||||
return internalMap.get(arg);
|
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() {
|
public boolean isVerboseLoggingEnabled() {
|
||||||
return hasArgument("verbose") && getArgument("verbose").equals("true");
|
return hasArgument("verbose") && getArgument("verbose").equals("true");
|
||||||
}
|
}
|
||||||
|
@ -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()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -19,8 +19,6 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
package org.nanoboot.spriteutils.core;
|
package org.nanoboot.spriteutils.core;
|
||||||
|
|
||||||
import dev.mccue.guava.hash.Hashing;
|
|
||||||
import dev.mccue.guava.io.Files;
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
@ -124,27 +122,6 @@ public class Utils {
|
|||||||
return resultStringBuilder.toString();
|
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) {
|
public static String encodeBase64(String s) {
|
||||||
return Base64.getEncoder().encodeToString(s.getBytes());
|
return Base64.getEncoder().encodeToString(s.getBytes());
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user