external classloader from modmgr

This commit is contained in:
minjaesong
2025-03-30 17:00:39 +09:00
parent 852c0b7dcc
commit 0c35faa548
24 changed files with 200 additions and 43 deletions

View File

@@ -0,0 +1,50 @@
package net.torvald.terrarum.modulecomputers.gameactors
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.CommonResourcePool
import net.torvald.terrarum.ModMgr
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZEF
import net.torvald.terrarum.gameactors.drawBodyInGoodPosition
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.modulebasegame.gameactors.BlockBox
import net.torvald.terrarum.modulebasegame.gameactors.Electric
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2025-03-30.
*/
class FixtureComputerConsole : Electric {
@Transient override val spawnNeedsFloor = true
@Transient override val spawnNeedsWall = false
constructor() : super(
BlockBox(BlockBox.ALLOW_MOVE_DOWN, 2, 2),
nameFun = { Lang["ITEM_COMPUTER_CONSOLE"] }
)
@Transient lateinit var itemImageSheet: TextureRegionPack
init {
itemImageSheet = CommonResourcePool.getOrPut("spritesheet:dwarventech/sprites/fixtures/computers.tga") {
TextureRegionPack(ModMgr.getGdxFile("dwarventech", "sprites/fixtures/computers.tga"), TILE_SIZE, TILE_SIZE)
} as TextureRegionPack
}
override fun drawBody(frameDelta: Float, batch: SpriteBatch) {
super.drawBody(frameDelta, batch)
val sx = hitbox.startX.toFloat()
val sy = hitbox.startY.toFloat()
drawBodyInGoodPosition(sx, sy) { x, y ->
batch.draw(itemImageSheet.get(0, 1), x, y)
batch.draw(itemImageSheet.get(1, 1), x + TILE_SIZEF, y)
batch.draw(itemImageSheet.get(0, 2), x, y + TILE_SIZEF)
batch.draw(itemImageSheet.get(1, 2), x + TILE_SIZEF, y + TILE_SIZEF)
}
}
}

View File

@@ -0,0 +1,23 @@
package net.torvald.terrarum.modulecomputers.gameitems
import net.torvald.terrarum.TerrarumAppConfiguration
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE
import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.modulebasegame.gameitems.FixtureItemBase
/**
* Created by minjaesong on 2025-03-30.
*/
class ItemComputerConsole(originalID: ItemID) : FixtureItemBase(originalID, "net.torvald.terrarum.modulecomputers.gameactors.FixtureComputerConsole") {
override var dynamicID: ItemID = originalID
override var baseMass = 80.0
override val canBeDynamic = false
override val materialId = ""
init {
// itemImage = FixtureItemBase.getItemImageFromSheet("dwarventech", "sprites/fixtures/desktop_computer.tga", TILE_SIZE, TILE_SIZE)
}
override var baseToolSize: Double? = baseMass
override var originalName = "ITEM_COMPUTER_CONSOLE"
}

View File

@@ -7,7 +7,6 @@ id;drop;name;renderclass;accept;inputcount;inputtype;outputtype;javaclass;invent
1;1;WIRE_POWER_LOW;power;power_low;3;N/A;N/A;net.torvald.terrarum.modulebasegame.gameitems.WirePieceSignalWire;basegame.items,5,4;1;"POWERWIRE_LOW"
2;2;WIRE_POWER_HIGH;power;power_high;3;N/A;N/A;net.torvald.terrarum.modulebasegame.gameitems.WirePieceSignalWire;basegame.items,6,4;1;"POWERWIRE_HIGH"
16;16;WIRE_ETHERNET;network;10base2;3;N/A;N/A;net.torvald.terrarum.modulebasegame.gameitems.WirePieceSignalWire;basegame.items,7,4;1;"ETHERNETWIRE"
256;256;AXLE;axle;axle;1;N/A;N/A;net.torvald.terrarum.modulebasegame.gameitems.WirePieceAxle;basegame.items,1,5;0;"AXLE"
1 id drop name renderclass accept inputcount inputtype outputtype javaclass inventoryimg branching tags
7 1 1 WIRE_POWER_LOW power power_low 3 N/A N/A net.torvald.terrarum.modulebasegame.gameitems.WirePieceSignalWire basegame.items,5,4 1 POWERWIRE_LOW
8 2 2 WIRE_POWER_HIGH power power_high 3 N/A N/A net.torvald.terrarum.modulebasegame.gameitems.WirePieceSignalWire basegame.items,6,4 1 POWERWIRE_HIGH
9 16 256 16 256 WIRE_ETHERNET AXLE network axle 10base2 axle 3 1 N/A N/A net.torvald.terrarum.modulebasegame.gameitems.WirePieceSignalWire net.torvald.terrarum.modulebasegame.gameitems.WirePieceAxle basegame.items,7,4 basegame.items,1,5 1 0 ETHERNETWIRE AXLE
256 256 AXLE axle axle 1 N/A N/A net.torvald.terrarum.modulebasegame.gameitems.WirePieceAxle basegame.items,1,5 0 AXLE
10 # accept: which wiretype (defined elsewhere) the wires acceps. Use comma to separate multiple. N/A for electronic components (aka not wires)
11 # inputcount: how many sides are input (outputcount is deduced from the inputcount). N/A for wires
12 # inputtype: which wiretype it accepts. N/A for wires

View File

@@ -1,3 +1,4 @@
id;classname
1;net.torvald.terrarum.modulecomputers.gameitems.ItemWearableWorldRadar
2;net.torvald.terrarum.modulecomputers.gameitems.ItemHomeComputer
id;classname;tags
#1;net.torvald.terrarum.modulecomputers.gameitems.ItemWearableWorldRadar;
#2;net.torvald.terrarum.modulecomputers.gameitems.ItemHomeComputer;
16;net.torvald.terrarum.modulecomputers.gameitems.ItemComputerConsole;TAGS
1 id classname tags
2 1 #1 net.torvald.terrarum.modulecomputers.gameitems.ItemWearableWorldRadar
3 2 #2 net.torvald.terrarum.modulecomputers.gameitems.ItemHomeComputer
4 16 net.torvald.terrarum.modulecomputers.gameitems.ItemComputerConsole TAGS

View File

@@ -4,7 +4,7 @@ author=CuriousTo\uA75Bvald
package=net.torvald.terrarum.modulecomputers
entrypoint=net.torvald.terrarum.modulecomputers.EntryPoint
releasedate=2021-12-03
version=0.3.0
version=0.5.2
jar=ModuleComputers.jar
jarhash=384E6650960E9D044055C5BAFE4C941398B688C3809FA6733B06DDB92A746CFA
dependency=basegame 0.3.0
dependency=basegame 0.5.2

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,15 @@
id;drop;name;renderclass;accept;inputcount;inputtype;outputtype;javaclass;inventoryimg;branching;tags
1;1;WIRE_THINNET;network;netframe;1;N/A;N/A;net.torvald.terrarum.modulecomputers.gameitems.WirePieceRingBus;dwarventech.items,7,4;0;""
2;2;WIRE_IO_BUS;io_bus;io_bus;1;N/A;N/A;net.torvald.terrarum.modulecomputers.gameitems.WirePieceIOBus;dwarventech.items,7,4;0;""
# accept: which wiretype (defined elsewhere) the wires acceps. Use comma to separate multiple. N/A for electronic components (aka not wires)
# inputcount: how many sides are input (outputcount is deduced from the inputcount). N/A for wires
# inputtype: which wiretype it accepts. N/A for wires
# outputtype: which wiretype it emits. N/A for wires
# branching: if this wire can have branches. 0: unable, 1: tee-only, 2: cross-only, 3: tee and cross.
# Something like a thicknet can't have branches
#
# comments
# digital_3bits must come right after three wires it bundles
# what's the point of WIRE_BUNDLE when you can overlap as many wires as you want? -- Torvald, 2021-08-09
1 id drop name renderclass accept inputcount inputtype outputtype javaclass inventoryimg branching tags
2 1 1 WIRE_THINNET network netframe 1 N/A N/A net.torvald.terrarum.modulecomputers.gameitems.WirePieceRingBus dwarventech.items,7,4 0
3 2 2 WIRE_IO_BUS io_bus io_bus 1 N/A N/A net.torvald.terrarum.modulecomputers.gameitems.WirePieceIOBus dwarventech.items,7,4 0
4 # accept: which wiretype (defined elsewhere) the wires acceps. Use comma to separate multiple. N/A for electronic components (aka not wires)
5 # inputcount: how many sides are input (outputcount is deduced from the inputcount). N/A for wires
6 # inputtype: which wiretype it accepts. N/A for wires
7 # outputtype: which wiretype it emits. N/A for wires
8 # branching: if this wire can have branches. 0: unable, 1: tee-only, 2: cross-only, 3: tee and cross.
9 # Something like a thicknet can't have branches
10 #
11 # comments
12 # digital_3bits must come right after three wires it bundles
13 # what's the point of WIRE_BUNDLE when you can overlap as many wires as you want? -- Torvald, 2021-08-09

View File

@@ -519,6 +519,38 @@ object ModMgr {
return filesList.toList()
}
fun <T> getJavaClass(module: String, className: String, constructorTypes: Array<Class<*>>, initArgs: Array<Any>): T {
checkExistence(module)
moduleClassloader[module].let {
if (it == null) {
val loadedClass = Class.forName(className)
val loadedClassConstructor = loadedClass.getConstructor(*constructorTypes)
return loadedClassConstructor.newInstance(*initArgs) as T
}
else {
val loadedClass = it.loadClass(className)
val loadedClassConstructor = loadedClass.getConstructor(*constructorTypes)
return loadedClassConstructor.newInstance(*initArgs) as T
}
}
}
fun <T> getJavaClass(module: String, className: String): T {
checkExistence(module)
moduleClassloader[module].let {
if (it == null) {
val loadedClass = Class.forName(className)
val loadedClassConstructor = loadedClass.getConstructor()
return loadedClassConstructor.newInstance() as T
}
else {
val loadedClass = it.loadClass(className)
val loadedClassConstructor = loadedClass.getConstructor()
return loadedClassConstructor.newInstance() as T
}
}
}
fun disposeMods() {
entryPointClasses.forEach { it.dispose() }
}
@@ -668,26 +700,14 @@ object ModMgr {
csv.forEach {
val className: String = it["classname"].toString()
val internalID: Int = it["id"].toInt()
val itemName: String = "item@$module:$internalID"
val itemName: ItemID = "item@$module:$internalID"
val tags = it["tags"].split(',').map { it.trim().toUpperCase() }.toHashSet()
printdbg(this, "Reading item ${itemName} <<- internal #$internalID with className $className")
moduleClassloader[module].let {
if (it == null) {
val loadedClass = Class.forName(className)
val loadedClassConstructor = loadedClass.getConstructor(ItemID::class.java)
val loadedClassInstance = loadedClassConstructor.newInstance(itemName)
ItemCodex[itemName] = loadedClassInstance as GameItem
ItemCodex[itemName]!!.tags.addAll(tags)
}
else {
val loadedClass = it.loadClass(className)
val loadedClassConstructor = loadedClass.getConstructor(ItemID::class.java)
val loadedClassInstance = loadedClassConstructor.newInstance(itemName)
ItemCodex[itemName] = loadedClassInstance as GameItem
ItemCodex[itemName]!!.tags.addAll(tags)
}
ModMgr.getJavaClass<GameItem>(module, className, arrayOf(ItemID::class.java), arrayOf(itemName)).let {
ItemCodex[itemName] = it
ItemCodex[itemName]!!.tags.addAll(tags)
}
}
}

View File

@@ -21,6 +21,9 @@ import kotlin.math.min
typealias ItemID = String
fun ItemID.getModuleName(): String = this.substringBefore(':').substringAfter('@')
/**
* Instances of the GameItem (e.g. net.torvald.terrarum.modulebasegame.gameitems.PickaxeCopper) are preferably referenced
* from the ```<module>/items/itemid.csv``` file only, and not from the actual game code.

View File

@@ -172,8 +172,17 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
val SIZE_NORMAL = Point2i(CHUNK_W*100, CHUNK_H*60)
val SIZE_LARGE = Point2i(CHUNK_W*150, CHUNK_H*60)
val SIZE_HUGE = Point2i(CHUNK_W*250, CHUNK_H*60)
val NEW_WORLD_SIZE = arrayOf(SIZE_SMALL, SIZE_NORMAL, SIZE_LARGE, SIZE_HUGE)
val WORLDPORTAL_NEW_WORLD_SIZE = arrayOf(SIZE_SMALL, SIZE_NORMAL, SIZE_LARGE, SIZE_HUGE)
val SIZE_TEST = Point2i(CHUNK_W*11, CHUNK_H*7)
val NEW_WORLD_SIZE =
if (App.IS_DEVELOPMENT_BUILD)
arrayOf(SIZE_SMALL, SIZE_NORMAL, SIZE_LARGE, SIZE_HUGE, SIZE_TEST)
else
arrayOf(SIZE_SMALL, SIZE_NORMAL, SIZE_LARGE, SIZE_HUGE)
val WORLDPORTAL_NEW_WORLD_SIZE =
if (App.IS_DEVELOPMENT_BUILD)
arrayOf(SIZE_SMALL, SIZE_NORMAL, SIZE_LARGE, SIZE_HUGE, SIZE_TEST)
else
arrayOf(SIZE_SMALL, SIZE_NORMAL, SIZE_LARGE, SIZE_HUGE)
val worldgenThreadExecutor = ThreadExecutor()
}

View File

@@ -144,7 +144,7 @@ class ActorConveyors : ActorWithBody {
override fun updateImpl(delta: Float) {
super.updateImpl(delta)
turn += delta / 1
turn += delta * 8
while (turn >= 1.0) turn -= 1.0
// turn = 0.0

View File

@@ -6,6 +6,7 @@ import net.torvald.terrarum.*
import net.torvald.terrarum.gameactors.ActorWithBody
import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.gameitems.getModuleName
import net.torvald.terrarum.gameitems.mouseInInteractableRange
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.gameactors.FixtureBase
@@ -22,8 +23,8 @@ open class FixtureItemBase(originalID: ItemID, val fixtureClassName: String) : G
// @Transient private val hash = RandomWordsName(4)
@Transient protected open val makeFixture: () -> FixtureBase = {
Class.forName(fixtureClassName).getDeclaredConstructor().newInstance() as FixtureBase
@Transient protected open val makeFixture: (String) -> FixtureBase = { moduleName: String ->
ModMgr.getJavaClass<FixtureBase>(moduleName, fixtureClassName)
}
init {
@@ -62,7 +63,7 @@ open class FixtureItemBase(originalID: ItemID, val fixtureClassName: String) : G
override fun effectWhileEquipped(actor: ActorWithBody, delta: Float) {
// println("ghost: ${ghostItem}; ghostInit = $ghostInit; instance: $hash")
if (!ghostInit.compareAndExchangeAcquire(false, true)) {
ghostItem.set(makeFixture())
ghostItem.set(makeFixture(originalID.getModuleName()))
// printdbg(this, "ghost item initialised: $ghostItem")
}
@@ -98,7 +99,7 @@ open class FixtureItemBase(originalID: ItemID, val fixtureClassName: String) : G
}
override fun startPrimaryUse(actor: ActorWithBody, delta: Float) = mouseInInteractableRange(actor) { _, _, mx, my ->
val item = ghostItem.getAndSet(makeFixture()) // renew the "ghost" otherwise you'll be spawning exactly the same fixture again; old ghost will be returned
val item = ghostItem.getAndSet(makeFixture(originalID.getModuleName())) // renew the "ghost" otherwise you'll be spawning exactly the same fixture again; old ghost will be returned
if (item.spawn(mx, my, if (actor is IngamePlayer) actor.uuid else null)) 1 else -1
// return true when placed, false when cannot be placed

View File

@@ -26,7 +26,7 @@ class ItemTapestry(originalID: ItemID) : FixtureItemBase(originalID, "net.torval
override var baseToolSize: Double? = baseMass
override var originalName = "ITEM_TAPESTRY"
@Transient override val makeFixture: () -> FixtureBase = {
@Transient override val makeFixture: (String) -> FixtureBase = { moduleName: String ->
FixtureTapestry(
// TODO use extra["fileRef"] (string) and extra["framingMaterial"] (string)
Gdx.files.internal("assets/monkey_island").readBytes(),

View File

@@ -35,7 +35,7 @@ class ItemTextSignCopper(originalID: ItemID) : FixtureItemBase(originalID, "net.
stackable = false
}
@Transient override val makeFixture: () -> FixtureBase = {
@Transient override val makeFixture: (String) -> FixtureBase = { moduleName: String ->
FixtureTextSignCopper(
extra.getAsString("signContent") ?: "",
extra.getAsInt("signPanelCount") ?: 2

View File

@@ -53,7 +53,7 @@ class UINewWorld(val remoCon: UIRemoCon) : UICanvas() {
private val normalTex = TextureRegion(Texture(ModMgr.getGdxFile("basegame", "gui/normal.png")))
private val smallTex = TextureRegion(Texture(ModMgr.getGdxFile("basegame", "gui/small.png")))
private val tex = arrayOf(smallTex, normalTex, largeTex, hugeTex)
private val tex = arrayOf(smallTex, normalTex, largeTex, hugeTex, smallTex)
override var width = 480
override var height = 480
@@ -72,12 +72,23 @@ class UINewWorld(val remoCon: UIRemoCon) : UICanvas() {
private val sizeSelector = UIItemInlineRadioButtons(this,
drawX + radioX, drawY + sizeSelY, radioCellWidth,
listOf(
{ Lang["CONTEXT_DESCRIPTION_TINY"] },
{ Lang["CONTEXT_DESCRIPTION_SMALL"] },
{ Lang["CONTEXT_DESCRIPTION_BIG"] },
{ Lang["CONTEXT_DESCRIPTION_HUGE"] }
)
if (App.IS_DEVELOPMENT_BUILD) {
listOf(
{ Lang["CONTEXT_DESCRIPTION_TINY"] },
{ Lang["CONTEXT_DESCRIPTION_SMALL"] },
{ Lang["CONTEXT_DESCRIPTION_BIG"] },
{ Lang["CONTEXT_DESCRIPTION_HUGE"] },
{ "Testification" }
)
}
else {
listOf(
{ Lang["CONTEXT_DESCRIPTION_TINY"] },
{ Lang["CONTEXT_DESCRIPTION_SMALL"] },
{ Lang["CONTEXT_DESCRIPTION_BIG"] },
{ Lang["CONTEXT_DESCRIPTION_HUGE"] }
)
}
)
private val rng = HQRNG()
@@ -290,8 +301,12 @@ class UINewWorld(val remoCon: UIRemoCon) : UICanvas() {
)
// name/seed input labels
App.fontGame.draw(batch, Lang["MENU_NAME"], drawX - 4, drawY + sizeSelY + inputLineY1)
App.fontGame.draw(batch, Lang["CONTEXT_GENERATOR_SEED"], drawX - 4, drawY + sizeSelY + inputLineY2)
App.fontGame.draw(batch,
if (sizeSelector.selection == 4) "Test Program" else Lang["MENU_NAME"],
drawX - 4, drawY + sizeSelY + inputLineY1)
App.fontGame.draw(batch,
if (sizeSelector.selection == 4) "Parameters" else Lang["CONTEXT_GENERATOR_SEED"],
drawX - 4, drawY + sizeSelY + inputLineY2)
val (wx, wy) = TerrarumIngame.NEW_WORLD_SIZE[sizeSelector.selection]
val etaSec = Worldgen.getEstimationSec(wx, wy)

Binary file not shown.

Binary file not shown.