Integrated arg name into InstallationFinder searching methods

Integrated arg name into InstallationFinder searching methods
Refactored tools to use InstallationFinder#defaultHomeDir
Added java.lang.Exception to throws clause of Tool#handleCliOptions
Uncaught exceptions thrown by Tool#handleCliOptions will be fatal
This commit is contained in:
Collin Smith 2021-09-16 12:25:34 -07:00
parent c5c98ce48c
commit 474ea5b44b
9 changed files with 20 additions and 74 deletions

View File

@ -72,19 +72,19 @@ public abstract class InstallationFinder {
public final FileHandle defaultHomeDir() throws DefaultNotFound {
try {
return defaultHomeDir(null);
return defaultHomeDir(null, null);
} catch (ArgNotFound t) {
throw new AssertionError("null arg should not throw " + ArgNotFound.class.getCanonicalName(), t);
}
}
public final FileHandle defaultHomeDir(String arg) throws ArgNotFound, DefaultNotFound {
public final FileHandle defaultHomeDir(String argName, String arg) throws ArgNotFound, DefaultNotFound {
if (arg != null) {
final FileHandle handle = new FileHandle(arg);
if (InstallationFinder.isD2Home(handle)) {
return handle;
} else {
throw new ArgNotFound(handle, "Unable to locate any D2 installation!");
throw new ArgNotFound(handle, "'" + argName + "' does not refer to a valid D2 installation: " + handle);
}
}
@ -100,19 +100,19 @@ public abstract class InstallationFinder {
public final FileHandle defaultTestDir() throws DefaultNotFound {
try {
return defaultTestDir(null);
return defaultTestDir(null, null);
} catch (ArgNotFound t) {
throw new AssertionError("null arg should not throw " + ArgNotFound.class.getCanonicalName(), t);
}
}
public final FileHandle defaultTestDir(String arg) throws ArgNotFound, DefaultNotFound {
public final FileHandle defaultTestDir(String argName, String arg) throws ArgNotFound, DefaultNotFound {
if (arg != null) {
final FileHandle handle = new FileHandle(arg);
if (InstallationFinder.isD2Home(handle)) {
return handle;
} else {
throw new ArgNotFound(handle, "Unable to locate any D2 test installation!");
throw new ArgNotFound(handle, "'" + argName + "' does not refer to a valid D2 test installation: " + handle);
}
}

View File

@ -12,7 +12,6 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.riiablo.Files;
import com.riiablo.Riiablo;
@ -64,30 +63,11 @@ public class D2SReaderTool extends Tool {
}
@Override
protected void handleCliOptions(String cmd, Options options, CommandLine cli) {
protected void handleCliOptions(String cmd, Options options, CommandLine cli) throws Exception {
super.handleCliOptions(cmd, options, cli);
final InstallationFinder finder = InstallationFinder.getInstance();
final FileHandle d2Home;
if (cli.hasOption("d2")) {
d2Home = new FileHandle(cli.getOptionValue("d2"));
if (!InstallationFinder.isD2Home(d2Home)) {
throw new GdxRuntimeException("'d2' does not refer to a valid D2 installation: " + d2Home);
}
} else {
log.trace("Locating D2 installations...");
Array<FileHandle> homeDirs = finder.getHomeDirs();
log.trace("D2 installations: {}", homeDirs);
if (homeDirs.size > 0) {
d2Home = homeDirs.first();
} else {
System.err.println("Unable to locate any D2 installation!");
printHelp(cmd, options);
System.exit(0);
return;
}
}
final FileHandle d2Home = finder.defaultHomeDir("d2", cli.getOptionValue("d2"));
log.debug("d2Home: {}", d2Home);
Riiablo.home = d2Home;

View File

@ -75,7 +75,7 @@ public class SerializerGeneratorTool extends Tool {
}
@Override
protected void handleCliOptions(String cmd, Options options, CommandLine cli) {
protected void handleCliOptions(String cmd, Options options, CommandLine cli) throws Exception {
super.handleCliOptions(cmd, options, cli);
String srcOptionValue = cli.getOptionValue("src");

View File

@ -27,7 +27,6 @@ import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.ReflectionException;
@ -91,12 +90,11 @@ public class FontMetricsTool extends Tool {
}
@Override
protected void handleCliOptions(String cmd, Options options, CommandLine cli) {
protected void handleCliOptions(String cmd, Options options, CommandLine cli) throws Exception {
super.handleCliOptions(cmd, options, cli);
InstallationFinder finder = InstallationFinder.getInstance();
Array<FileHandle> homeDirs = finder.getHomeDirs();
home = homeDirs.first();
home = finder.defaultHomeDir();
String fontOptionValue = cli.getOptionValue("font");
font = FilenameUtils.getBaseName(fontOptionValue);

View File

@ -14,8 +14,6 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.riiablo.camera.OrthographicCamera;
import com.riiablo.io.ByteInput;
@ -63,30 +61,11 @@ public class MapDebugger extends Tool {
}
@Override
protected void handleCliOptions(String cmd, Options options, CommandLine cli) {
protected void handleCliOptions(String cmd, Options options, CommandLine cli) throws Exception {
super.handleCliOptions(cmd, options, cli);
final InstallationFinder finder = InstallationFinder.getInstance();
final FileHandle d2Home;
if (cli.hasOption("d2")) {
d2Home = new FileHandle(cli.getOptionValue("d2"));
if (!InstallationFinder.isD2Home(d2Home)) {
throw new GdxRuntimeException("'d2' does not refer to a valid D2 installation: " + d2Home);
}
} else {
log.trace("Locating D2 installations...");
Array<FileHandle> homeDirs = finder.getHomeDirs();
log.trace("D2 installations: {}", homeDirs);
if (homeDirs.size > 0) {
d2Home = homeDirs.first();
} else {
System.err.println("Unable to locate any D2 installation!");
printHelp(cmd, options);
System.exit(0);
return;
}
}
final FileHandle d2Home = finder.defaultHomeDir("d2", cli.getOptionValue("d2"));
log.debug("d2Home: {}", d2Home);
Riiablo.home = d2Home;
}

View File

@ -30,7 +30,6 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.utils.UIUtils;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.riiablo.COFs;
@ -200,19 +199,11 @@ public class MapViewer extends Tool {
}
@Override
protected void handleCliOptions(String cmd, Options options, CommandLine cli) {
protected void handleCliOptions(String cmd, Options options, CommandLine cli) throws Exception {
super.handleCliOptions(cmd, options, cli);
if (cli.hasOption("d2")) {
home = new FileHandle(cli.getOptionValue("d2"));
if (!InstallationFinder.isD2Home(home)) {
throw new GdxRuntimeException("'d2' does not refer to a valid D2 installation: " + home);
}
} else {
InstallationFinder finder = InstallationFinder.getInstance();
Array<FileHandle> homeDirs = finder.getHomeDirs();
home = homeDirs.first();
}
InstallationFinder finder = InstallationFinder.getInstance();
home = finder.defaultHomeDir("d2", cli.getOptionValue("d2"));
if (cli.hasOption("random")) {
seed = -1;

View File

@ -260,7 +260,7 @@ public class MPQViewer {
}
@Override
protected void handleCliOptions(String cmd, Options options, CommandLine cli) {
protected void handleCliOptions(String cmd, Options options, CommandLine cli) throws Exception {
super.handleCliOptions(cmd, options, cli);
initialFile = cli.getOptionValue("file");
debugMode = cli.hasOption("debug");

View File

@ -41,7 +41,7 @@ public class Tool implements ApplicationListener {
System.exit(0);
}
protected void handleCliOptions(String cmd, Options options, CommandLine cli) {
protected void handleCliOptions(String cmd, Options options, CommandLine cli) throws Exception {
if (cli.hasOption("help")) {
printHelp(cmd, options);
System.exit(0);

View File

@ -12,7 +12,6 @@ import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.video.VideoPlayer;
import com.badlogic.gdx.video.VideoPlayerCreator;
@ -65,12 +64,11 @@ public class VideoPlayerTool extends Tool {
}
@Override
protected void handleCliOptions(String cmd, Options options, CommandLine cli) {
protected void handleCliOptions(String cmd, Options options, CommandLine cli) throws Exception {
super.handleCliOptions(cmd, options, cli);
InstallationFinder finder = InstallationFinder.getInstance();
Array<FileHandle> homeDirs = finder.getHomeDirs();
home = homeDirs.first();
home = finder.defaultHomeDir();
String fileOptionValue = cli.getOptionValue("file");
file = fileOptionValue;