support for external mods

This commit is contained in:
minjaesong
2022-02-24 11:03:03 +09:00
parent 598d0514ff
commit b3940d96b8
3 changed files with 26 additions and 11 deletions

View File

@@ -59,7 +59,8 @@ object ModMgr {
val releaseDate: String,
val version: String,
val jar: String,
val dependencies: Array<String>
val dependencies: Array<String>,
val isInternal: Boolean
) {
override fun toString() =
"\tModule #$order -- $properName | $version | $author\n" +
@@ -131,10 +132,21 @@ object ModMgr {
try {
val modMetadata = Properties()
modMetadata.load(FileInputStream("$modDirInternal/$moduleName/$metaFilename"))
if (File("$modDirInternal/$moduleName/$defaultConfigFilename").exists()) {
val defaultConfig = JsonFetcher("$modDirInternal/$moduleName/$defaultConfigFilename")
val _internalFile = File("$modDirInternal/$moduleName/$metaFilename")
val _externalFile = File("$modDirExternal/$moduleName/$metaFilename")
// external mod has precedence over the internal
val isInternal = if (_externalFile.exists()) false else if (_internalFile.exists()) true else throw FileNotFoundException()
val file = if (isInternal) _internalFile else _externalFile
val modDir = if (isInternal) modDirInternal else modDirExternal
fun getGdxFile(path: String) = if (isInternal) Gdx.files.internal(path) else Gdx.files.absolute(path)
modMetadata.load(FileInputStream(file))
if (File("$modDir/$moduleName/$defaultConfigFilename").exists()) {
val defaultConfig = JsonFetcher("$modDir/$moduleName/$defaultConfigFilename")
// read config and store it to the game
// write to user's config file
@@ -151,7 +163,7 @@ object ModMgr {
val version = modMetadata.getProperty("version")
val jar = modMetadata.getProperty("jar")
val dependency = modMetadata.getProperty("dependency").split(Regex(""";[ ]*""")).filter { it.isNotEmpty() }.toTypedArray()
val isDir = FileSystems.getDefault().getPath("$modDirInternal/$moduleName").toFile().isDirectory
val isDir = FileSystems.getDefault().getPath("$modDir/$moduleName").toFile().isDirectory
val versionNumeral = version.split('.')
@@ -173,7 +185,7 @@ object ModMgr {
}
moduleInfo[moduleName] = ModuleMetadata(index, isDir, Gdx.files.internal("$modDirInternal/$moduleName/icon.png"), properName, description, author, packageName, entryPoint, releaseDate, version, jar, dependency)
moduleInfo[moduleName] = ModuleMetadata(index, isDir, getGdxFile("$modDir/$moduleName/icon.png"), properName, description, author, packageName, entryPoint, releaseDate, version, jar, dependency, isInternal)
printdbg(this, moduleInfo[moduleName])
@@ -192,7 +204,7 @@ object ModMgr {
val urls = arrayOf<URL>()
val cl = JarFileLoader(urls)
cl.addFile("${File(modDirInternal).absolutePath}/$moduleName/$jar")
cl.addFile("${File(modDir).absolutePath}/$moduleName/$jar")
moduleClassloader[moduleName] = cl
newClass = cl.loadClass(entryPoint)
}
@@ -325,14 +337,14 @@ object ModMgr {
/** Returning files are read-only */
fun getGdxFile(module: String, path: String): FileHandle {
checkExistence(module)
return if (true) // TODO if module is internal...
return if (moduleInfo[module]!!.isInternal)
Gdx.files.internal("$modDirInternal/$module/$path")
else
Gdx.files.absolute("$modDirExternal/$module/$path")
}
fun getFile(module: String, path: String): File {
checkExistence(module)
return if (true) // TODO if module is internal...
return if (moduleInfo[module]!!.isInternal)
FileSystems.getDefault().getPath("$modDirInternal/$module/$path").toFile()
else
FileSystems.getDefault().getPath("$modDirExternal/$module/$path").toFile()

View File

@@ -37,7 +37,7 @@ object CommandDict {
printdbg(this, ModMgr.loadOrder.reversed().map { ModMgr.moduleInfo[it]?.packageName })
((listOf("$" to "net.torvald.terrarum")) + ModMgr.loadOrder.reversed().map { it to ModMgr.moduleInfo[it]?.packageName }).forEach { (modName, packageRoot) ->
if (modName != "$" && ModMgr.hasFile(modName, "commands.csv")) {
if (modName == "$" || modName != "$" && ModMgr.hasFile(modName, "commands.csv")) {
val commandsList = if (modName == "$") engineCommandList else ModMgr.getFile(modName, "commands.csv").readLines()
val packageConsole = "$packageRoot.console"

View File

@@ -39,7 +39,10 @@ class UIItemSaving(parentUI: UICanvas, initialX: Int, initialY: Int) : UIItem(pa
if (index >= 0) {
val sx = index % circleSheet.horizontalCount
val sy = index / circleSheet.horizontalCount
batch.draw(circleSheet.get(sx, sy), (posX + (width - circleSheet.tileW) / 2).toFloat(), posY.toFloat())
// q&d fix for ArrayIndexOutOfBoundsException caused when saving huge world... wut?
if (sx in 0 until circleSheet.horizontalCount && sy in 0 until circleSheet.horizontalCount) {
batch.draw(circleSheet.get(sx, sy), (posX + (width - circleSheet.tileW) / 2).toFloat(), posY.toFloat())
}
}
}