Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New features and fixes #19

Merged
merged 8 commits into from
Jun 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.strongjoshua</groupId>
<artifactId>libgdx-inGameConsole</artifactId>
<version>0.4.0</version>
<version>0.5.0</version>
<name>LibGdx In-Game Console</name>
<description>This is a LibGdx library that allows a developer to add a console (similar to how it is featured in Source games) to their game.</description>

Expand Down
120 changes: 94 additions & 26 deletions src/com/strongjoshua/console/AbstractConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public abstract class AbstractConsole implements Console, Disposable {
protected boolean logToSystem;

protected boolean disabled;

protected boolean executeHiddenCommands = true;
protected boolean displayHiddenCommands = false;

public AbstractConsole() {
log = new Log();
Expand Down Expand Up @@ -112,6 +115,8 @@ public void setCommandExecutor (CommandExecutor commandExec) {
*/
@Override
public void execCommand(String command) {
if(disabled) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, does this fix some usecase?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This enforces the disabled flag, whether it is a GUIConsole, HeadlessConsole, or even a custom Console extended from AbstractConsole, it is disabled from executing commands.


log(command, LogLevel.COMMAND);

String[] parts = command.split(" ");
Expand All @@ -124,48 +129,62 @@ public void execCommand(String command) {
}
}

// attempt to convert arguments to numbers. If the conversion does not work, keep the argument as a string.
Object[] args = null;
if (sArgs != null) {
args = new Object[sArgs.length];
for (int i = 0; i < sArgs.length; i++) {
String s = sArgs[i];
try {
int j = Integer.parseInt(s);
args[i] = j;
} catch (NumberFormatException e) {
try {
float f = Float.parseFloat(s);
args[i] = f;
} catch (NumberFormatException e2) {
args[i] = s;
}
}
}
}

Class<? extends CommandExecutor> clazz = exec.getClass();
Method[] methods = ClassReflection.getMethods(clazz);
Array<Integer> possible = new Array<Integer>();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equalsIgnoreCase(methodName)) {
Method method = methods[i];
if (method.getName().equalsIgnoreCase(methodName) && ConsoleUtils.canExecuteCommand(this, method)) {
possible.add(i);
}
}

if (possible.size <= 0) {
log("No such method found.", LogLevel.ERROR);
return;
}

int size = possible.size;
int numArgs;
numArgs = args == null ? 0 : args.length;
int numArgs = sArgs == null ? 0 : sArgs.length;
for (int i = 0; i < size; i++) {
Method m = methods[possible.get(i)];
Class<?>[] params = m.getParameterTypes();
if (numArgs != params.length) {
continue;
} else {
if (numArgs == params.length) {
try {
Object[] args = null;

try {
if(sArgs != null) {
args = new Object[numArgs];

for(int j=0; j<params.length; j++) {
Class<?> param = params[j];
final String value = sArgs[j];

if(param.equals(String.class)) {
args[j] = value;
} else if(param.equals(Boolean.class) || param.equals(boolean.class)) {
args[j] = Boolean.parseBoolean(value);
} else if(param.equals(Byte.class) || param.equals(byte.class)) {
args[j] = Byte.parseByte(value);
} else if(param.equals(Short.class) || param.equals(short.class)) {
args[j] = Short.parseShort(value);
} else if(param.equals(Integer.class) || param.equals(int.class)) {
args[j] = Integer.parseInt(value);
} else if(param.equals(Long.class) || param.equals(long.class)) {
args[j] = Long.parseLong(value);
} else if(param.equals(Float.class) || param.equals(float.class)) {
args[j] = Float.parseFloat(value);
} else if(param.equals(Double.class) || param.equals(double.class)) {
args[j] = Double.parseDouble(value);
}
}
}
} catch(Exception e) {
// Error occurred trying to parse parameter, continue to next function
continue;
}

m.setAccessible(true);
m.invoke(exec, args);
return;
Expand All @@ -180,7 +199,56 @@ public void execCommand(String command) {
}
}
}

log("Bad parameters. Check your code.", LogLevel.ERROR);
}

@Override
public void printCommands() {
Method[] methods = ClassReflection.getDeclaredMethods(exec.getClass());
for(int j = 0; j < methods.length; j++) {
Method m = methods[j];
if(m.isPublic() && ConsoleUtils.canDisplayCommand(this, m)) {
String s = "";
s += m.getName();
s += " : ";

Class<?>[] params = m.getParameterTypes();
for(int i = 0; i < params.length; i++) {
s += params[i].getSimpleName();
if(i < params.length - 1) {
s += ", ";
}
}

log(s);
}
}
}

/* (non-Javadoc)
* @see com.strongjoshua.console.Console#setExecuteHiddenCommands(boolean)
*/
@Override
public void setExecuteHiddenCommands(boolean enabled) {
executeHiddenCommands = enabled;
}

@Override
public boolean isExecuteHiddenCommandsEnabled() {
return executeHiddenCommands;
}

/* (non-Javadoc)
* @see com.strongjoshua.console.Console#setDisplayHiddenCommands(boolean)
*/
@Override
public void setDisplayHiddenCommands(boolean enabled) {
displayHiddenCommands = enabled;
}

@Override
public boolean isDisplayHiddenCommandsEnabled() {
return displayHiddenCommands;
}
}
2 changes: 1 addition & 1 deletion src/com/strongjoshua/console/CommandCompleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void set(CommandExecutor ce, String s) {
Array<Method> methods = getAllMethods(ce);
for(Method m : methods) {
String name = m.getName();
if(name.toLowerCase().startsWith(setString)) {
if(name.toLowerCase().startsWith(setString) && ConsoleUtils.canDisplayCommand(ce.console, m)) {
possibleCommands.add(name);
}
}
Expand Down
20 changes: 1 addition & 19 deletions src/com/strongjoshua/console/CommandExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,6 @@ public final void exitApp() {
* Shows all available methods, and their parameter types, in the console.
*/
public final void help() {
Method[] methods = ClassReflection.getDeclaredMethods(this.getClass());
for(int j = 0; j < methods.length; j++) {
Method m = methods[j];
if(m.isPublic()) {
String s = "";
s += m.getName();
s += " : ";

Class<?>[] params = m.getParameterTypes();
for(int i = 0; i < params.length; i++) {
s += params[i].getSimpleName();
if(i < params.length - 1) {
s += ", ";
}
}

console.log(s);
}
}
console.printCommands();
}
}
28 changes: 27 additions & 1 deletion src/com/strongjoshua/console/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public interface Console {
* @param fh The {@link FileHandle} that links to the file to be written to. Note that <code>classpath</code> and
* <code>internal</code> FileHandles cannot be written to. */
public void printLogToFile(FileHandle fh);

/** Prints all commands */
public void printCommands();

/** @return If the console is disabled.
* @see Console#setDisabled(boolean) */
Expand Down Expand Up @@ -128,5 +131,28 @@ public interface Console {

/** @return If console is hidden */
public boolean isHidden();


/** Sets the executeHiddenCommands field
*
* @param enabled - if true, commands annotated with {@link HiddenCommand} can be executed
*/
public void setExecuteHiddenCommands(boolean enabled);

/** Sets the executeHiddenCommands field
*
* @param enabled - if true, commands annotated with {@link HiddenCommand} show when printCommands (help) is executed
*/
public void setDisplayHiddenCommands(boolean enabled);

/**
*
* @return If hidden commands can be executed
*/
public boolean isExecuteHiddenCommandsEnabled();

/**
*
* @return If hidden commands can be displayed
*/
public boolean isDisplayHiddenCommandsEnabled();
}
30 changes: 30 additions & 0 deletions src/com/strongjoshua/console/ConsoleUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
*
*/
package com.strongjoshua.console;

import com.badlogic.gdx.utils.reflect.Method;

/**
* @author Eric
*
*/
public final class ConsoleUtils {

public static boolean canExecuteCommand(Console console, Method method) {
if(!console.isExecuteHiddenCommandsEnabled() && method.isAnnotationPresent(HiddenCommand.class)) {
return false;
}

return true;
}

public static boolean canDisplayCommand(Console console, Method method) {
if(!console.isDisplayHiddenCommandsEnabled() && method.isAnnotationPresent(HiddenCommand.class)) {
return false;
}

return true;
}

}
23 changes: 23 additions & 0 deletions src/com/strongjoshua/console/HiddenCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
*
*/
package com.strongjoshua.console;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Eric Burns (ThaBalla1148)
*
*/
@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface HiddenCommand {

}
Loading