Skip to content

Commit

Permalink
Mod CI step 1 - allow running Unciv as a cli tool for linting etc
Browse files Browse the repository at this point in the history
  • Loading branch information
yairm210 committed Jul 14, 2024
1 parent 767759f commit 41bd131
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8 deletions.
4 changes: 2 additions & 2 deletions core/src/com/unciv/models/ruleset/Ruleset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Ruleset {

val tileRemovals by lazy { tileImprovements.values.filter { it.name.startsWith(Constants.remove) } }
val nonRoadTileRemovals by lazy { tileRemovals.filter { rulesetImprovement ->
RoadStatus.values().none { it.removeAction == rulesetImprovement.name } } }
RoadStatus.entries.toTypedArray().none { it.removeAction == rulesetImprovement.name } } }

/** Contains all happiness levels that moving *from* them, to one *below* them, can change uniques that apply */
val allHappinessLevelsThatAffectUniques by lazy {
Expand Down Expand Up @@ -244,7 +244,7 @@ class Ruleset {
fun allICivilopediaText(): Sequence<ICivilopediaText> =
allRulesetObjects() + events.values + events.values.flatMap { it.choices }

internal fun load(folderHandle: FileHandle) {
fun load(folderHandle: FileHandle) {
// Note: Most files are loaded using createHashmap, which sets originRuleset automatically.
// For other files containing IRulesetObject's we'll have to remember to do so manually - e.g. Tech.
val modOptionsFile = folderHandle.child("ModOptions.json")
Expand Down
10 changes: 7 additions & 3 deletions desktop/src/com/unciv/app/desktop/ConsoleLauncher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.unciv.models.ruleset.nation.Nation
import com.unciv.models.skins.SkinCache
import com.unciv.models.tilesets.TileSetCache
import com.unciv.utils.Log
import java.util.LinkedHashSet
import kotlin.time.ExperimentalTime

internal object ConsoleLauncher {
Expand All @@ -39,7 +38,12 @@ internal object ConsoleLauncher {
TileSetCache.loadTileSetConfigs(true)
SkinCache.loadSkinConfigs(true)

val ruleset = RulesetCache[BaseRuleset.Civ_V_GnK.fullName]!!
runSimulation()
}

@ExperimentalTime
private fun runSimulation() {
val ruleset = RulesetCache[BaseRuleset.Civ_V_GnK.fullName]!!

ruleset.nations[simulationCiv1] = Nation().apply { name = simulationCiv1 }
ruleset.nations[simulationCiv2] = Nation().apply { name = simulationCiv2 }
Expand All @@ -52,7 +56,7 @@ internal object ConsoleLauncher {
UncivGame.Current.gameInfo = newGame


val simulation = Simulation(newGame,10,4)
val simulation = Simulation(newGame, 10, 4)

simulation.start()
}
Expand Down
20 changes: 20 additions & 0 deletions desktop/src/com/unciv/app/desktop/DesktopGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package com.unciv.app.desktop
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration
import com.unciv.UncivGame
import com.unciv.models.ruleset.Ruleset
import com.unciv.models.ruleset.validation.RulesetErrorSeverity
import com.unciv.models.ruleset.validation.RulesetValidator
import kotlin.system.exitProcess

class DesktopGame(config: Lwjgl3ApplicationConfiguration) : UncivGame() {

private var discordUpdater = DiscordUpdater()
private val windowListener = UncivWindowListener()
var isModCi = false

init {
config.setWindowListener(windowListener)
Expand All @@ -33,6 +38,21 @@ class DesktopGame(config: Lwjgl3ApplicationConfiguration) : UncivGame() {
discordUpdater.startUpdates()
}

override fun create() {
// The uniques checker requires the file system to be seet up, which happens after lwjgw initializes it
if (isModCi) {
ImagePacker.packImagesPerMod(".", ".")
val ruleset = Ruleset()
ruleset.folderLocation = Gdx.files.local("jsons")
ruleset.load(ruleset.folderLocation!!)
val errors = RulesetValidator(ruleset).getErrorList(true)
println(errors.getErrorText(true))
exitProcess(if (errors.any { it.errorSeverityToReport == RulesetErrorSeverity.Error }) 1 else 0)
}

super.create()
}

override fun installAudioHooks() {
(Gdx.app as HardenGdxAudio).installHooks(
musicController.getAudioLoopCallback(),
Expand Down
7 changes: 5 additions & 2 deletions desktop/src/com/unciv/app/desktop/DesktopLauncher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal object DesktopLauncher {
// For more info see https://github.com/yairm210/Unciv/pull/3202 and https://github.com/LWJGL/lwjgl/issues/119
System.setProperty("org.lwjgl.opengl.Display.allowSoftwareOpenGL", "true")

val isRunFromJAR = DesktopLauncher.javaClass.`package`.specificationVersion != null
val isRunFromJAR = true//DesktopLauncher.javaClass.`package`.specificationVersion != null
ImagePacker.packImages(isRunFromJAR)

val config = Lwjgl3ApplicationConfiguration()
Expand Down Expand Up @@ -68,8 +68,11 @@ internal object DesktopLauncher {
UiElementDocsWriter().write()
}

val desktopGame = DesktopGame(config)
if (arg.isNotEmpty() && arg[0] == "mod-ci") desktopGame.isModCi = true

// HardenGdxAudio extends Lwjgl3Application, and the Lwjgl3Application constructor runs as long as the game runs
HardenGdxAudio(DesktopGame(config), config)
HardenGdxAudio(desktopGame, config)
exitProcess(0)
}
}
2 changes: 1 addition & 1 deletion desktop/src/com/unciv/app/desktop/ImagePacker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ internal object ImagePacker {
}

// Scan multiple image folders and generate an atlas for each - if outdated
private fun packImagesPerMod(input: String, output: String, defaultSettings: TexturePacker.Settings) {
fun packImagesPerMod(input: String, output: String, defaultSettings: TexturePacker.Settings = getDefaultSettings()) {
val baseDir = File(input)
if (!File(baseDir, imagesPathBase).exists() && !File(baseDir, existCheck2).exists()) return // So we don't run this from within a fat JAR
val atlasList = mutableListOf<String>()
Expand Down
1 change: 1 addition & 0 deletions docs/Modders/.pages
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ nav:
- uniques.md
- Type-checking.md
- Scenarios.md
- CI-automation.md
7 changes: 7 additions & 0 deletions docs/Modders/CI-automation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# AI automation

Unciv contains built-in capabilities for packing images and autoupdating uniques.
You can use Unciv as a command-line tool by running it with the command

`java -jar <folder location>/Unciv.jar mod-ci`

0 comments on commit 41bd131

Please sign in to comment.