mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-12 19:44:05 +09:00
console command dictionary is now automatically filled using reflection
This commit is contained in:
@@ -24,6 +24,7 @@ import org.apache.commons.codec.digest.DigestUtils
|
||||
*
|
||||
* Created by minjaesong on 2016-02-19.
|
||||
*/
|
||||
@ConsoleAlias("auth")
|
||||
internal object Authenticator : ConsoleCommand {
|
||||
|
||||
private var a = false
|
||||
|
||||
@@ -1,79 +1,73 @@
|
||||
package net.torvald.terrarum.console
|
||||
|
||||
import net.torvald.terrarum.AppLoader.printdbg
|
||||
import net.torvald.terrarum.AppLoader.printdbgerr
|
||||
import net.torvald.terrarum.ModMgr
|
||||
import net.torvald.terrarum.ModMgr.loadOrder
|
||||
import net.torvald.terrarum.modulebasegame.console.*
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import java.util.*
|
||||
import kotlin.streams.toList
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2016-01-15.
|
||||
*/
|
||||
object CommandDict {
|
||||
|
||||
// todo replace with reflection?
|
||||
internal val dict: HashMap<String, ConsoleCommand> = hashMapOf(
|
||||
"echo" to Echo,
|
||||
"error" to EchoError,
|
||||
"setav" to SetAV,
|
||||
"qqq" to QuitApp,
|
||||
"codex" to CodexEdictis,
|
||||
"export" to ExportMap,
|
||||
"gc" to ForceGC,
|
||||
"getav" to GetAV,
|
||||
"getlocale" to GetLocale,
|
||||
"togglenoclip" to ToggleNoClip,
|
||||
"nc" to ToggleNoClip,
|
||||
"setlocale" to SetLocale,
|
||||
"teleport" to Teleport,
|
||||
"tp" to Teleport,
|
||||
"cat" to CatStdout,
|
||||
"setgl" to SetGlobalLightOverride,
|
||||
"getfaction" to GetFactioning,
|
||||
"auth" to Authenticator,
|
||||
"batch" to Batch,
|
||||
"settime" to SetTime,
|
||||
"gettime" to GetTime,
|
||||
"settimedelta" to SetTimeDelta,
|
||||
"help" to Help,
|
||||
"version" to Version,
|
||||
"seed" to Seed,
|
||||
"println" to EchoConsole,
|
||||
"inventory" to Inventory,
|
||||
"avtracker" to AVTracker,
|
||||
"actorslist" to ActorsList,
|
||||
"setscale" to SetScale,
|
||||
"kill" to KillActor,
|
||||
"screenshot" to TakeScreenshot,
|
||||
"resize" to ResizeScreen,
|
||||
"possess" to Possess,
|
||||
internal val dict = hashMapOf<String, ConsoleCommand>()
|
||||
|
||||
// Test codes
|
||||
"money" to MoneyDisp,
|
||||
"bulletintest" to SetBulletin,
|
||||
"tips" to PrintRandomTips,
|
||||
"langtest" to LangTest,
|
||||
"spawnball" to SpawnPhysTestBall,
|
||||
"spawntorch" to SpawnTikiTorch,
|
||||
"musictest" to MusicTest,
|
||||
"spawntapestry" to SpawnTapestry,
|
||||
"imtest" to JavaIMTest,
|
||||
"cheatmotherfuckernootnoot" to CheatWarnTest,
|
||||
"spawnlunarlander" to SpawnPhysTestLunarLander,
|
||||
"savetest" to SavegameWriterTest,
|
||||
init {
|
||||
printdbg(this, ModMgr.loadOrder.reversed())
|
||||
printdbg(this, ModMgr.loadOrder.reversed().map { ModMgr.moduleInfo[it]?.packageName })
|
||||
|
||||
"exportav" to ExportAV,
|
||||
/* !! */"exportmeta" to ExportMeta,
|
||||
/* !! */"exportworld" to ExportWorld,
|
||||
/* !! */"exportactor" to ExportActor,
|
||||
/* !! */"importworld" to ImportWorld,
|
||||
/* !! */"importactor" to ImportActor,
|
||||
/* !! */"exportfborgb" to ExportRendererFboRGB,
|
||||
/* !! */"printworld" to PrintWorld
|
||||
)
|
||||
(listOf("net.torvald.terrarum") + ModMgr.loadOrder.reversed().map { ModMgr.moduleInfo[it]?.packageName }.filter { it != null }).forEach{ packageRoot ->
|
||||
printdbg(this, packageRoot)
|
||||
val packageConsole = "$packageRoot.console"
|
||||
val stream = ClassLoader.getSystemClassLoader().getResourceAsStream(packageConsole.replace('.','/'))
|
||||
val reader = BufferedReader(InputStreamReader(stream))
|
||||
|
||||
reader.lines()
|
||||
.filter{ it.endsWith(".class") && !it.contains('$') }
|
||||
.map { Class.forName("$packageConsole.${it.substring(0, it.lastIndexOf('.'))}") }
|
||||
.forEach {
|
||||
|
||||
printdbg(this, "> Trying to instantiate ${it.canonicalName}")
|
||||
|
||||
try {
|
||||
val instance = it.kotlin.objectInstance ?: it.kotlin.java.newInstance()
|
||||
|
||||
val aliases = instance.javaClass.getAnnotation(ConsoleAlias::class.java)?.aliasesCSV?.split(',')?.map { it.trim() }
|
||||
val noexport = instance.javaClass.getAnnotation(ConsoleNoExport::class.java)
|
||||
|
||||
if (noexport == null) {
|
||||
|
||||
dict[instance.javaClass.simpleName.lowercase()] = instance as ConsoleCommand
|
||||
aliases?.forEach {
|
||||
dict[it] = instance as ConsoleCommand
|
||||
}
|
||||
|
||||
printdbg(this, "Class instantiated: ${instance.javaClass.simpleName}")
|
||||
if (aliases != null)
|
||||
printdbg(this, " Annotations: $aliases")
|
||||
}
|
||||
}
|
||||
catch (e: ClassCastException) {
|
||||
printdbgerr(this, "${it.canonicalName} is not a ConsoleCommand")
|
||||
}
|
||||
catch (e: InstantiationException) {
|
||||
printdbgerr(this, "Could not instantiate ${it.canonicalName}")
|
||||
e.printStackTrace(System.err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
operator fun get(commandName: String): ConsoleCommand {
|
||||
return dict[commandName]!!
|
||||
}
|
||||
|
||||
fun add(name: String, obj: ConsoleCommand) {
|
||||
dict[name] = obj
|
||||
return dict[commandName.lowercase()]!!
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,14 +16,15 @@ internal object CommandInterpreter {
|
||||
private val commandsNoAuth = arrayOf(
|
||||
"auth",
|
||||
"qqq",
|
||||
"zoom",
|
||||
"setlocale",
|
||||
"getlocale",
|
||||
"help",
|
||||
"version",
|
||||
"tips",
|
||||
"screenshot",
|
||||
"resize"
|
||||
"resize",
|
||||
"echo",
|
||||
"error"
|
||||
)
|
||||
|
||||
internal fun execute(command: String) {
|
||||
@@ -38,7 +39,7 @@ internal object CommandInterpreter {
|
||||
var commandObj: ConsoleCommand? = null
|
||||
try {
|
||||
if (single_command.name.toLowerCase().startsWith("qqq")) {
|
||||
commandObj = CommandDict["qqq"]
|
||||
commandObj = CommandDict["QuitApp"]
|
||||
}
|
||||
else if (commandsNoAuth.contains(single_command.name.toLowerCase())) {
|
||||
commandObj = CommandDict[single_command.name.toLowerCase()]
|
||||
|
||||
@@ -15,4 +15,7 @@ interface ConsoleCommand {
|
||||
|
||||
fun printUsage()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
annotation class ConsoleAlias(val aliasesCSV: String)
|
||||
annotation class ConsoleNoExport()
|
||||
@@ -3,6 +3,7 @@ package net.torvald.terrarum.console
|
||||
/**
|
||||
* Created by minjaesong on 2016-09-07.
|
||||
*/
|
||||
@ConsoleAlias("println")
|
||||
internal object EchoConsole : ConsoleCommand {
|
||||
/**
|
||||
* Args 0: command given
|
||||
|
||||
@@ -7,6 +7,7 @@ import net.torvald.terrarum.ui.ConsoleWindow
|
||||
/**
|
||||
* Created by minjaesong on 2016-04-25.
|
||||
*/
|
||||
@ConsoleAlias("error")
|
||||
internal object EchoError : ConsoleCommand {
|
||||
override fun execute(args: Array<String>) {
|
||||
val argsWoHeader = Array(args.size - 1) { args[it + 1] }
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.badlogic.gdx.Gdx
|
||||
/**
|
||||
* Created by minjaesong on 2016-01-15.
|
||||
*/
|
||||
@ConsoleAlias("qqq")
|
||||
internal object QuitApp : ConsoleCommand {
|
||||
|
||||
override fun execute(args: Array<String>) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.torvald.terrarum.console
|
||||
import net.torvald.terrarum.AppLoader
|
||||
import net.torvald.terrarum.TerrarumScreenSize
|
||||
|
||||
@ConsoleAlias("resize")
|
||||
object ResizeScreen: ConsoleCommand {
|
||||
override fun execute(args: Array<String>) {
|
||||
if (args.size == 3) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import net.torvald.terrarum.weather.WeatherMixer
|
||||
/**
|
||||
* Created by minjaesong on 2016-02-17.
|
||||
*/
|
||||
@ConsoleAlias("setgl")
|
||||
internal object SetGlobalLightOverride : ConsoleCommand {
|
||||
|
||||
override fun execute(args: Array<String>) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.torvald.terrarum.console
|
||||
|
||||
import net.torvald.terrarum.AppLoader
|
||||
|
||||
@ConsoleNoExport
|
||||
object TakeScreenshot: ConsoleCommand {
|
||||
override fun execute(args: Array<String>) {
|
||||
AppLoader.requestScreenshot()
|
||||
|
||||
Reference in New Issue
Block a user