Skip to content

Commit

Permalink
Use a new configuration format more suited for multiple installation …
Browse files Browse the repository at this point in the history
…directories.
  • Loading branch information
Lymia committed Dec 17, 2023
1 parent 0cef39e commit 028c061
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 155 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ git.formattedShaVersion := {

// Scala configuration
scalaVersion := "3.3.1"
scalacOptions ++= "-release:17 -deprecation -unchecked".split(" ").toSeq
scalacOptions ++= "-release:21 -deprecation -unchecked".split(" ").toSeq
crossPaths := false

// Dependencies
Expand Down
105 changes: 105 additions & 0 deletions src/main/scala/moe/lymia/mppatch/ui/ConfigurationStore.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2015-2023 Lymia Kanokawa <lymia@lymia.moe>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package moe.lymia.mppatch.ui

import moe.lymia.mppatch.ui.InstallationConfiguration.*
import play.api.libs.json.*
import play.api.libs.json.Reads.*
import play.api.libs.json.Writes.*

import java.nio.file.{Path, Paths}
import java.util.Locale
import javax.swing.JFrame

object ConfigurationStore extends LaunchFrameError {
private val prefs = java.util.prefs.Preferences.userNodeForPackage(getClass)
class ConfigKey[T: Reads: Format](val name: String, default: => T) {
def this(name: String) = this(name, sys.error("preference " + name + " not set"))

protected def encode(v: T): String = Json.stringify(Json.toJson(v))
protected def decode(s: String): T = Json.fromJson(Json.parse(s)).get

def hasValue = prefs.get(name, null) != null
def clear() = prefs.remove(name)
def valueOption = try {
val pref = prefs.get(name, null)
if (pref == null) None else Some(decode(pref))
} catch {
case _: Exception => None
}
def value = valueOption.fold(default)(identity)
def value_=(t: T) = prefs.put(name, encode(t))
}
private class RawStringConfigKey(name: String) extends ConfigKey[String](name) {
override protected def encode(v: String): String = v
override protected def decode(s: String): String = s
}

private val configVersion = new ConfigKey[Int]("installer_config_version")
val installationDirs = new ConfigKey[Seq[String]]("installer_v1_installation_dirs", Seq())
def installationConf(path: Path) = {
val canonical = path.toRealPath().toString
new ConfigKey[InstallationConfiguration](s"installer_v1_conf|$canonical")
}

val legacyInstallationDirectory: ConfigKey[String] = new RawStringConfigKey("installationDirectory")
val legacyEnableDebug: ConfigKey[Boolean] = new ConfigKey("enableDebug", false)
val legacyEnableLogging: ConfigKey[Boolean] = new ConfigKey("enableLogging", true)
val legacyEnableMultiplayerPatch: ConfigKey[Boolean] = new ConfigKey("enableMultiplayerPatch", true)
val legacyEnableLuaJIT: ConfigKey[Boolean] = new ConfigKey("enableLuaJIT", true)

private def hasLegacyValues: Boolean =
legacyInstallationDirectory.hasValue || legacyEnableDebug.hasValue || legacyEnableLogging.hasValue ||
legacyEnableMultiplayerPatch.hasValue || legacyEnableLuaJIT.hasValue
def updatePreferences(findDefaultDirectory: => Option[Path]): Unit =
if (!configVersion.hasValue && hasLegacyValues) {
configVersion.value = 1
val defaultDirectory = legacyInstallationDirectory.valueOption match {
case Some(x) =>
val realPath = Paths.get(x).toRealPath().toString
installationDirs.value = installationDirs.value :+ realPath
Some(Paths.get(x))
case None =>
val _ = installationDirs.value // make sure the key exists, but don't do anything else
findDefaultDirectory
}
defaultDirectory match {
case Some(defaultDirectory) =>
installationConf(defaultDirectory).value = InstallationConfiguration(
enableDebug = legacyEnableDebug.value,
enableLogging = legacyEnableLogging.value,
enableMultiplayerPatch = legacyEnableMultiplayerPatch.value,
enableLuaJit = legacyEnableLuaJIT.value
)
case _ =>
}
} else if (configVersion.hasValue && configVersion.value != 1) {
if (confirmDialog("error.configVersionTooNew")) {
configVersion.value = 1
} else {
throw new InstallerException("Cancelling configuration downgrade", null)
}
} else {
// do nothing
}
}
9 changes: 6 additions & 3 deletions src/main/scala/moe/lymia/mppatch/ui/InstallationManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

package moe.lymia.mppatch.ui

import play.api.libs.json.{Json, OFormat}

import java.nio.file.Path

case class InstallationConfiguration(
Expand All @@ -30,9 +32,10 @@ case class InstallationConfiguration(
enableMultiplayerPatch: Boolean,
enableLuaJit: Boolean
)

class Installation(rootDir: Path) {

object InstallationConfiguration {
implicit val jsonFormat: OFormat[InstallationConfiguration] = Json.format[InstallationConfiguration]
}

class Installation(rootDir: Path) {}

class InstallationManager {}
16 changes: 13 additions & 3 deletions src/main/scala/moe/lymia/mppatch/ui/MPPatchInstaller.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ package moe.lymia.mppatch.ui

import com.formdev.flatlaf.fonts.roboto.FlatRobotoFont
import com.formdev.flatlaf.{FlatIntelliJLaf, FlatLaf}
import moe.lymia.mppatch.core.PlatformType
import moe.lymia.mppatch.core.{PatchPackage, Platform, PlatformType}
import moe.lymia.mppatch.util.io.ResourceDataSource
import moe.lymia.mppatch.util.{Logger, SimpleLogger, VersionInfo}

import java.io.{File, FileOutputStream, OutputStreamWriter, PrintWriter}
import java.nio.charset.StandardCharsets
import java.nio.file.Path
import java.text.DateFormat
import java.util.Locale
import javax.swing.{JFrame, JOptionPane, UIManager}
Expand Down Expand Up @@ -141,11 +143,11 @@ object MPPatchInstaller extends LaunchFrameError {
// generate native image configs
log.warn("Generating native image configs...")
log.warn("If you did not intend this, I don't know what to say.")
Preferences.updatePreferences()
ConfigurationStore.updatePreferences(defaultCivilizationPath)
NativeImageGenConfig.run()
} else {
// start main frame
Preferences.updatePreferences()
ConfigurationStore.updatePreferences(defaultCivilizationPath)
new MainFrame(locale).showForm()
}
} catch {
Expand All @@ -156,4 +158,12 @@ object MPPatchInstaller extends LaunchFrameError {
case _: InstallerException => // ignored
}
}

private def defaultCivilizationPath: Option[Path] = {
val pkg = new PatchPackage(ResourceDataSource("builtin_patch"))
val platform = Platform(PlatformType.currentPlatform).get
val validPaths =
for (path <- platform.defaultSystemPaths if pkg.detectInstallationPlatform(path).isDefined) yield path
validPaths.headOption
}
}
16 changes: 8 additions & 8 deletions src/main/scala/moe/lymia/mppatch/ui/MainFrame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class MainFrame(val locale: Locale) extends FrameBase[JFrame] {
protected var currentStatus: JTextField = _

private def packages = {
val logging = if (Preferences.legacyEnableLogging.value) Set("logging") else Set[String]()
val multiplayer = if (Preferences.legacyEnableMultiplayerPatch.value) Set("multiplayer") else Set[String]()
val luajit = if (Preferences.legacyEnableLuaJIT.value) Set("luajit") else Set[String]()
val debug = if (Preferences.legacyEnableDebug.value) Set("debug") else Set[String]()
val logging = if (ConfigurationStore.legacyEnableLogging.value) Set("logging") else Set[String]()
val multiplayer = if (ConfigurationStore.legacyEnableMultiplayerPatch.value) Set("multiplayer") else Set[String]()
val luajit = if (ConfigurationStore.legacyEnableLuaJIT.value) Set("luajit") else Set[String]()
val debug = if (ConfigurationStore.legacyEnableDebug.value) Set("debug") else Set[String]()
debug ++ multiplayer ++ luajit ++ logging
}

Expand Down Expand Up @@ -71,7 +71,7 @@ class MainFrame(val locale: Locale) extends FrameBase[JFrame] {
installer.foreach(_.releaseLock())
if (isValid) {
instance.acquireLock()
if (changeByUser) Preferences.legacyInstallationDirectory.value = path.toFile.toString
if (changeByUser) ConfigurationStore.legacyInstallationDirectory.value = path.toFile.toString
}
installer = Some(instance)
} else {
Expand All @@ -94,11 +94,11 @@ class MainFrame(val locale: Locale) extends FrameBase[JFrame] {
case Some(x) => changeInstaller(x, false)
case None =>
}
if (Preferences.legacyInstallationDirectory.hasValue) {
val configPath = Paths.get(Preferences.legacyInstallationDirectory.value)
if (ConfigurationStore.legacyInstallationDirectory.hasValue) {
val configPath = Paths.get(ConfigurationStore.legacyInstallationDirectory.value)
if (checkPath(configPath)) changeInstaller(configPath, false)
else {
Preferences.legacyInstallationDirectory.clear()
ConfigurationStore.legacyInstallationDirectory.clear()
pathFromRegistry()
}
} else pathFromRegistry()
Expand Down
100 changes: 0 additions & 100 deletions src/main/scala/moe/lymia/mppatch/ui/Preferences.scala

This file was deleted.

16 changes: 8 additions & 8 deletions src/main/scala/moe/lymia/mppatch/ui/SettingsDialog.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class SettingsDialog(val locale: Locale, main: MainFrame) extends FrameBase[JDia
private def applySettings(): Unit = {
main.changeInstaller(Paths.get(installPath.getText))

Preferences.legacyEnableDebug.value = enableDebug.isSelected
Preferences.legacyEnableLogging.value = enableLogging.isSelected
Preferences.legacyEnableMultiplayerPatch.value = enableMultiplayerPatch.isSelected
Preferences.legacyEnableLuaJIT.value = enableLuaJIT.isSelected
ConfigurationStore.legacyEnableDebug.value = enableDebug.isSelected
ConfigurationStore.legacyEnableLogging.value = enableLogging.isSelected
ConfigurationStore.legacyEnableMultiplayerPatch.value = enableMultiplayerPatch.isSelected
ConfigurationStore.legacyEnableLuaJIT.value = enableLuaJIT.isSelected

main.update()
}
Expand Down Expand Up @@ -79,16 +79,16 @@ class SettingsDialog(val locale: Locale, main: MainFrame) extends FrameBase[JDia
}

enableLogging = options.gridCheckRow(1, "logging")
enableLogging.setSelected(Preferences.legacyEnableLogging.value)
enableLogging.setSelected(ConfigurationStore.legacyEnableLogging.value)

enableMultiplayerPatch = options.gridCheckRow(2, "modding")
enableMultiplayerPatch.setSelected(Preferences.legacyEnableMultiplayerPatch.value)
enableMultiplayerPatch.setSelected(ConfigurationStore.legacyEnableMultiplayerPatch.value)

enableLuaJIT = options.gridCheckRow(3, "luajit")
enableLuaJIT.setSelected(Preferences.legacyEnableLuaJIT.value)
enableLuaJIT.setSelected(ConfigurationStore.legacyEnableLuaJIT.value)

enableDebug = options.gridCheckRow(4, "debug")
enableDebug.setSelected(Preferences.legacyEnableDebug.value)
enableDebug.setSelected(ConfigurationStore.legacyEnableDebug.value)
}

frame.add(
Expand Down
4 changes: 4 additions & 0 deletions src/patch/mppatch-core/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[target.i686-pc-windows-gnu]
linker = "i686-w64-mingw32-gcc"
ar = "i686-w64-mingw32-gcc-ar"

Loading

0 comments on commit 028c061

Please sign in to comment.