mirror of
https://gitlab.com/cc-ru/ocelot/ocelot-desktop.git
synced 2025-12-20 11:09:20 +01:00
Refactor Setting loading method
This commit is contained in:
parent
da17dc81a8
commit
c9434089ea
@ -5,11 +5,9 @@ import ocelot.desktop.Settings.ExtendedConfig
|
|||||||
import ocelot.desktop.util.{Logging, SettingsData}
|
import ocelot.desktop.util.{Logging, SettingsData}
|
||||||
import org.apache.commons.lang3.SystemUtils
|
import org.apache.commons.lang3.SystemUtils
|
||||||
|
|
||||||
import java.io.InputStream
|
|
||||||
import java.nio.charset.StandardCharsets
|
import java.nio.charset.StandardCharsets
|
||||||
import java.nio.file.{Files, Path}
|
import java.nio.file.{Files, Path}
|
||||||
import java.util
|
import java.util
|
||||||
import scala.io.{Codec, Source}
|
|
||||||
|
|
||||||
class Settings(val config: Config) extends SettingsData {
|
class Settings(val config: Config) extends SettingsData {
|
||||||
// TODO: refactor this mess (having to declare every field 3 times is extremely error-prone)
|
// TODO: refactor this mess (having to declare every field 3 times is extremely error-prone)
|
||||||
@ -137,39 +135,18 @@ object Settings extends Logging {
|
|||||||
def get: Settings = settings
|
def get: Settings = settings
|
||||||
|
|
||||||
def load(path: Path): Unit = {
|
def load(path: Path): Unit = {
|
||||||
import java.lang.System.{lineSeparator => EOL}
|
|
||||||
|
|
||||||
if (Files.exists(path)) {
|
if (Files.exists(path)) {
|
||||||
var stream: InputStream = null
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
stream = Files.newInputStream(path)
|
settings = new Settings(ConfigFactory.parseFile(path.toFile))
|
||||||
val source = Source.fromInputStream(stream)(Codec.UTF8)
|
|
||||||
val plain = source.getLines().mkString("", EOL, EOL)
|
|
||||||
val config = ConfigFactory.parseString(plain)
|
|
||||||
settings = new Settings(config)
|
|
||||||
source.close()
|
|
||||||
|
|
||||||
logger.info(s"Loaded Ocelot Desktop configuration from: $path")
|
logger.info(s"Loaded Ocelot Desktop configuration from: $path")
|
||||||
|
|
||||||
return
|
return
|
||||||
} catch {
|
} catch {
|
||||||
case _: Throwable =>
|
case t: Throwable => logger.error(s"Failed to parse $path!", t)
|
||||||
logger.info(s"Failed to parse $path, using default Ocelot Desktop configuration.")
|
|
||||||
} finally {
|
|
||||||
if (stream != null)
|
|
||||||
stream.close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val defaults = {
|
logger.info(s"Using default Ocelot Desktop configuration...")
|
||||||
val in = getClass.getResourceAsStream("/ocelot/desktop/ocelot.conf")
|
settings = new Settings(ConfigFactory.parseResources("/ocelot/desktop/ocelot.conf"))
|
||||||
val config = Source.fromInputStream(in)(Codec.UTF8).getLines().mkString("", EOL, EOL)
|
|
||||||
in.close()
|
|
||||||
ConfigFactory.parseString(config)
|
|
||||||
}
|
|
||||||
|
|
||||||
settings = new Settings(defaults)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def save(path: Path): Unit = {
|
def save(path: Path): Unit = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user