mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-17 22:14:05 +09:00
console command dictionary is now automatically filled using reflection
This commit is contained in:
@@ -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()]!!
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user