Skip to content

Commit

Permalink
improv: Move firstRun to wrapper and improve internal version-type ch…
Browse files Browse the repository at this point in the history
…ecks
  • Loading branch information
Griefed committed Jan 4, 2025
1 parent a8c31ef commit 25b8f10
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,7 @@ class ApiProperties(propertiesFile: File = File("serverpackcreator.properties"))
@Suppress("MemberVisibilityCanBePrivate")
val trueFalseRegex = "^(true|false)$".toRegex()

@Suppress("MemberVisibilityCanBePrivate")
val alphaBetaRegex = "^(.*alpha.*|.*beta.*)$".toRegex()
val preReleaseRegex = ".*(alpha|beta|dev).*".toRegex()

@Suppress("MemberVisibilityCanBePrivate")
val serverPacksRegex = "^(?:\\./)?server-packs$".toRegex()
Expand Down Expand Up @@ -760,7 +759,7 @@ class ApiProperties(propertiesFile: File = File("serverpackcreator.properties"))

val preRelease: Boolean
get() {
return apiVersion.matches(alphaBetaRegex)
return apiVersion.matches(preReleaseRegex)
}

val configVersion: String = if (preRelease || devBuild) {
Expand All @@ -769,14 +768,6 @@ class ApiProperties(propertiesFile: File = File("serverpackcreator.properties"))
"4"
}

/**
* Only the first call to this property will return true if this is the first time ServerPackCreator is being run
* on a given host. Any subsequent call will return false. Handle with care!
*
* @author Griefed
*/
val firstRun: Boolean

var logLevel = "INFO"
get() {
field = acquireProperty(pLogLevel, "INFO").uppercase()
Expand Down Expand Up @@ -934,8 +925,8 @@ class ApiProperties(propertiesFile: File = File("serverpackcreator.properties"))
}
}

fun getPreference(pref: String) : Optional<String> {
return Optional.ofNullable(spcPreferences.get(pref, null))
fun getPreference(pref: String, def: String? = null) : Optional<String> {
return Optional.ofNullable(spcPreferences.get(pref, def))
}

fun storePreference(pref: String, value: String) {
Expand Down Expand Up @@ -2613,7 +2604,7 @@ class ApiProperties(propertiesFile: File = File("serverpackcreator.properties"))
}
}
if (fallbackUpdated) {
saveProperties(File(homeDirectory, serverPackCreatorProperties).absoluteFile)
saveProperties(serverPackCreatorPropertiesFile)
}
return fallbackUpdated
}
Expand Down Expand Up @@ -2770,8 +2761,6 @@ class ApiProperties(propertiesFile: File = File("serverpackcreator.properties"))
setLoggingLevel(logLevel)
}

firstRun = spcPreferences.getBoolean("de.griefed.serverpackcreator.firstrun",true)
spcPreferences.putBoolean("de.griefed.serverpackcreator.firstrun",false)
logsDirectory.create(createFileOrDir = true, asDirectory = true)
serverFilesDirectory.create(createFileOrDir = true, asDirectory = true)
propertiesDirectory.create(createFileOrDir = true, asDirectory = true)
Expand All @@ -2787,7 +2776,7 @@ class ApiProperties(propertiesFile: File = File("serverpackcreator.properties"))
minecraftServerManifestsDirectory.create(createFileOrDir = true, asDirectory = true)
installerCacheDirectory.create(createFileOrDir = true, asDirectory = true)
printSettings()
saveProperties(File(homeDirectory, serverPackCreatorProperties).absoluteFile)
saveProperties(serverPackCreatorPropertiesFile)
}

private fun setLoggingLevel(level: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class ApiWrapper private constructor(
val properties: File = File("serverpackcreator.properties"),
runSetup: Boolean = true
) {
val versionsRegex = ".*(alpha|beta|dev).*".toRegex()
val xmlJsonRegex = ".*\\.(xml|json)".toRegex()
var setupWasRun: Boolean = false

private val log by lazy { cachedLoggerOf(this.javaClass) }

/**
Expand All @@ -64,6 +64,18 @@ class ApiWrapper private constructor(
ApiProperties(properties)
}

/**
* Will return true if this is the first time ServerPackCreator is being run
* on a given host.
*/
@Volatile
var firstRun: Boolean = apiProperties.getPreference("de.griefed.serverpackcreator.firstrun","true").get().toBoolean()
get() {
apiProperties.storePreference("de.griefed.serverpackcreator.firstrun", "false")
return field
}
private set

/**
* This instances JSON-ObjectMapper used across ServerPackCreator with which this instance was
* initialized. By default, the ObjectMapper used across ServerPackCreator has the following
Expand Down Expand Up @@ -459,7 +471,7 @@ class ApiWrapper private constructor(

// Print system information to console and logs.
log.debug("Gathering system information to include in log to make debugging easier.")
if (apiProperties.apiVersion.matches(versionsRegex)) {
if (apiProperties.devBuild || apiProperties.preRelease) {
log.debug("Warning user about possible data loss.")
log.warn("################################################################")
log.warn("#.............ALPHA | BETA | DEV VERSION DETECTED..............#")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class MainWindow(
} else {
main.show()
}
if (apiWrapper.apiProperties.firstRun && migrationManager.migrationMessages.isEmpty()) {
if (apiWrapper.firstRun) {
GlobalScope.launch(Dispatchers.Swing, CoroutineStart.UNDISPATCHED) {
if (JOptionPane.showConfirmDialog(
main.frame,
Expand All @@ -103,7 +103,7 @@ class MainWindow(
}
}
} else {
if (apiWrapper.apiProperties.preRelease || migrationManager.migrationMessages.isNotEmpty()) {
if (migrationManager.migrationMessages.isNotEmpty()) {
main.displayMigrationMessages()
}
if (apiWrapper.apiProperties.fallbackUpdated) {
Expand Down

0 comments on commit 25b8f10

Please sign in to comment.