mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-07-04 23:38:28 +07:00
Introduced AbstractTool
Introduced AbstractTool to take over some of the tool backend boilerplate code
This commit is contained in:
@ -44,11 +44,11 @@ import com.riiablo.loader.PaletteLoader;
|
||||
import com.riiablo.logger.LogManager;
|
||||
import com.riiablo.logger.Logger;
|
||||
import com.riiablo.mpq.MPQFileHandleResolver;
|
||||
import com.riiablo.tool.BaseTool;
|
||||
import com.riiablo.tool.LwjglTool;
|
||||
import com.riiablo.tool.Tool;
|
||||
import com.riiablo.util.InstallationFinder;
|
||||
|
||||
public class FontMetricsTool extends BaseTool {
|
||||
public class FontMetricsTool extends Tool {
|
||||
private static final Logger log = LogManager.getLogger(FontMetricsTool.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -96,12 +96,12 @@ import com.riiablo.logger.Logger;
|
||||
import com.riiablo.map.DT1.Tile;
|
||||
import com.riiablo.map.pfa.GraphPath;
|
||||
import com.riiablo.mpq.MPQFileHandleResolver;
|
||||
import com.riiablo.tool.BaseTool;
|
||||
import com.riiablo.tool.LwjglTool;
|
||||
import com.riiablo.tool.Tool;
|
||||
import com.riiablo.util.DebugUtils;
|
||||
import com.riiablo.util.InstallationFinder;
|
||||
|
||||
public class MapViewer extends BaseTool {
|
||||
public class MapViewer extends Tool {
|
||||
private static final Logger log = LogManager.getLogger(MapViewer.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
67
tools/src/main/java/com/riiablo/tool/AbstractTool.java
Normal file
67
tools/src/main/java/com/riiablo/tool/AbstractTool.java
Normal file
@ -0,0 +1,67 @@
|
||||
package com.riiablo.tool;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
|
||||
import com.badlogic.gdx.Application;
|
||||
|
||||
public abstract class AbstractTool<A extends Application> {
|
||||
public abstract static class ToolBuilder<
|
||||
A extends Application,
|
||||
T extends Tool,
|
||||
C,
|
||||
B extends ToolBuilder<A, T, C, B>
|
||||
> {
|
||||
protected final Class<T> toolClass;
|
||||
protected final String cmd;
|
||||
protected final String[] args;
|
||||
protected final C config;
|
||||
|
||||
protected ToolBuilder(Class<T> toolClass, String cmd, String[] args, C config) {
|
||||
this.toolClass = toolClass;
|
||||
this.cmd = cmd;
|
||||
this.args = args;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public B defaults() {
|
||||
return (B) this;
|
||||
}
|
||||
|
||||
public B config(ToolConfigurator<C> configurator) {
|
||||
configurator.config(config);
|
||||
return (B) this;
|
||||
}
|
||||
|
||||
public abstract A newInstance(T toolInstance, C config);
|
||||
|
||||
public A start() {
|
||||
try {
|
||||
Constructor<T> defaultConstructor = toolClass.getDeclaredConstructor();
|
||||
defaultConstructor.setAccessible(true);
|
||||
defaultConstructor.newInstance((Object[]) null);
|
||||
T toolInstance = defaultConstructor.newInstance((Object[]) null);
|
||||
|
||||
Options options = new Options();
|
||||
toolInstance.createCliOptions(options);
|
||||
try {
|
||||
CommandLine cli = toolInstance.parseCliOptions(options, args);
|
||||
toolInstance.handleCliOptions(cmd, options, cli);
|
||||
} catch (Throwable t) {
|
||||
toolInstance.handleCliError(cmd, options, t);
|
||||
return ExceptionUtils.wrapAndThrow(t);
|
||||
}
|
||||
|
||||
return newInstance(toolInstance, config);
|
||||
} catch (Throwable t) {
|
||||
return ExceptionUtils.wrapAndThrow(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface ToolConfigurator<C> {
|
||||
void config(C config);
|
||||
}
|
||||
}
|
@ -1,50 +1,56 @@
|
||||
package com.riiablo.tool;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
|
||||
import com.badlogic.gdx.Files;
|
||||
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
|
||||
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
|
||||
|
||||
public class LwjglTool extends LwjglApplication {
|
||||
LwjglTool(BaseTool tool, LwjglApplicationConfiguration config) {
|
||||
super(tool, config);
|
||||
}
|
||||
|
||||
public static LwjglToolBuilder create(
|
||||
Class<? extends BaseTool> toolClass,
|
||||
public class LwjglTool extends AbstractTool<LwjglApplication> {
|
||||
public static <T extends Tool>
|
||||
LwjglToolBuilder<T> create(
|
||||
Class<T> toolClass,
|
||||
String cmd,
|
||||
String[] args
|
||||
) {
|
||||
return create(toolClass, cmd, args, new LwjglApplicationConfiguration()).defaults();
|
||||
}
|
||||
|
||||
public static LwjglToolBuilder create(
|
||||
Class<? extends BaseTool> toolClass,
|
||||
public static <T extends Tool>
|
||||
LwjglToolBuilder<T> create(
|
||||
Class<T> toolClass,
|
||||
String cmd,
|
||||
String[] args,
|
||||
LwjglApplicationConfiguration config
|
||||
) {
|
||||
return new LwjglToolBuilder(toolClass, cmd, args, config);
|
||||
return new LwjglToolBuilder<>(toolClass, cmd, args, config);
|
||||
}
|
||||
|
||||
public static final class LwjglToolBuilder {
|
||||
final Class<? extends BaseTool> toolClass;
|
||||
final String cmd;
|
||||
final String[] args;
|
||||
final LwjglApplicationConfiguration config;
|
||||
|
||||
LwjglToolBuilder(Class<? extends BaseTool> toolClass, String cmd, String[] args, LwjglApplicationConfiguration config) {
|
||||
this.toolClass = toolClass;
|
||||
this.cmd = cmd;
|
||||
this.args = args;
|
||||
this.config = config;
|
||||
public static final class LwjglToolBuilder<T extends Tool>
|
||||
extends ToolBuilder<
|
||||
LwjglApplication,
|
||||
T,
|
||||
LwjglApplicationConfiguration,
|
||||
LwjglToolBuilder<T>
|
||||
>
|
||||
{
|
||||
LwjglToolBuilder(
|
||||
Class<T> toolClass,
|
||||
String cmd,
|
||||
String[] args,
|
||||
LwjglApplicationConfiguration config
|
||||
) {
|
||||
super(toolClass, cmd, args, config);
|
||||
}
|
||||
|
||||
public LwjglToolBuilder defaults() {
|
||||
@Override
|
||||
public LwjglApplication newInstance(
|
||||
T toolInstance,
|
||||
LwjglApplicationConfiguration config
|
||||
) {
|
||||
return new LwjglApplication(toolInstance, config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LwjglToolBuilder<T> defaults() {
|
||||
config.title = toolClass.getSimpleName();
|
||||
config.width = 800;
|
||||
config.height = 600;
|
||||
@ -57,47 +63,22 @@ public class LwjglTool extends LwjglApplication {
|
||||
return this;
|
||||
}
|
||||
|
||||
public LwjglToolBuilder config(LwjglToolConfigurator configurator) {
|
||||
configurator.config(config);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LwjglToolBuilder title(String title) {
|
||||
public LwjglToolBuilder<T> title(String title) {
|
||||
config.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LwjglToolBuilder size(int width, int height) {
|
||||
public LwjglToolBuilder<T> size(int width, int height) {
|
||||
config.width = width;
|
||||
config.height = height;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LwjglTool start() {
|
||||
try {
|
||||
Constructor<? extends BaseTool> defaultConstructor = toolClass.getDeclaredConstructor();
|
||||
defaultConstructor.setAccessible(true);
|
||||
defaultConstructor.newInstance((Object[]) null);
|
||||
BaseTool toolInstance = defaultConstructor.newInstance((Object[]) null);
|
||||
|
||||
Options options = new Options();
|
||||
toolInstance.createCliOptions(options);
|
||||
try {
|
||||
CommandLine cli = toolInstance.parseCliOptions(options, args);
|
||||
toolInstance.handleCliOptions(cmd, options, cli);
|
||||
} catch (Throwable t) {
|
||||
toolInstance.handleCliError(cmd, options, t);
|
||||
return ExceptionUtils.wrapAndThrow(t);
|
||||
}
|
||||
|
||||
return new LwjglTool(toolInstance, config);
|
||||
} catch (Throwable t) {
|
||||
return ExceptionUtils.wrapAndThrow(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface LwjglToolConfigurator {
|
||||
public interface LwjglToolConfigurator
|
||||
extends ToolConfigurator<LwjglApplicationConfiguration>
|
||||
{
|
||||
@Override
|
||||
void config(LwjglApplicationConfiguration config);
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import org.apache.commons.cli.ParseException;
|
||||
|
||||
import com.badlogic.gdx.ApplicationListener;
|
||||
|
||||
public class BaseTool implements ApplicationListener {
|
||||
public class Tool implements ApplicationListener {
|
||||
protected void createCliOptions(Options options) {
|
||||
options.addOption(Option.builder("h")
|
||||
.longOpt("help")
|
Reference in New Issue
Block a user