Fix #33: crash when recent workspace was not found

This commit is contained in:
UnicornFreedom
2022-06-16 19:54:39 +02:00
parent e4e7aeb39d
commit 937addeeef
3 changed files with 53 additions and 15 deletions

View File

@@ -3,7 +3,7 @@ package ocelot.desktop
import li.flor.nativejfilechooser.NativeJFileChooser
import ocelot.desktop.audio.{Audio, SoundSource}
import ocelot.desktop.ui.UiHandler
import ocelot.desktop.ui.widget.{ExitConfirmationDialog, RootWidget, SettingsDialog}
import ocelot.desktop.ui.widget.{NotificationDialog, ExitConfirmationDialog, RootWidget, SettingsDialog}
import ocelot.desktop.util.FileUtils.getOcelotConfigDirectory
import ocelot.desktop.util._
import org.apache.commons.io.FileUtils
@@ -15,7 +15,7 @@ import totoro.ocelot.brain.nbt.{CompressedStreamTools, NBTTagCompound}
import totoro.ocelot.brain.workspace.Workspace
import java.io._
import java.nio.file.{Files, Path}
import java.nio.file.{Files, Path, Paths}
import java.util.concurrent.locks.{Lock, ReentrantLock}
import javax.swing.JFileChooser
import scala.io.Source
@@ -54,7 +54,14 @@ object OcelotDesktop extends Logging {
setupEventHandlers()
if (loadRecentWorkspace) load(Settings.get.recentWorkspace.map(new File(_)))
if (loadRecentWorkspace) {
if (!load(Settings.get.recentWorkspace.map(new File(_)))) {
UiHandler.root.modalDialogPool.pushDialog(
new NotificationDialog("Could not find the recent workspace...\nI will create a default one"))
root.workspaceView.createDefaultWorkspace()
Settings.get.recentWorkspace = None
}
}
new Thread(() => {
while (true) {
@@ -178,16 +185,20 @@ object OcelotDesktop extends Logging {
chooseDirectory(load, JFileChooser.OPEN_DIALOG)
}
def load(dir: Option[File]): Unit = {
def load(dir: Option[File]): Boolean = {
if (dir.isDefined) {
val path = dir.get + "/workspace.nbt"
val reader = new DataInputStream(new FileInputStream(path))
val nbt = CompressedStreamTools.readCompressed(reader)
savePath = Some(dir.get.toPath)
Settings.get.recentWorkspace = dir.map(_.getCanonicalPath)
workspace.path = dir.get.toPath
loadWorld(nbt)
val path = Paths.get(dir.get.getCanonicalPath, "workspace.nbt")
if (Files.exists(path)) {
val reader = new DataInputStream(Files.newInputStream(path))
val nbt = CompressedStreamTools.readCompressed(reader)
savePath = Some(dir.get.toPath)
Settings.get.recentWorkspace = dir.map(_.getCanonicalPath)
workspace.path = dir.get.toPath
loadWorld(nbt)
return true
}
}
false
}
def chooseDirectory(fun: Option[File] => Unit, dialogType: Int): Unit = {

View File

@@ -49,10 +49,8 @@ object Settings extends Logging {
config.withValue(path, ConfigValueFactory.fromIterable(util.Arrays.asList(value.x, value.y)))
}
def withValue(path: String, value: Option[Any]): Config = value match {
case Some(data) => config.withValue(path, ConfigValueFactory.fromAnyRef(data))
case None => config
}
def withValue(path: String, value: Option[Any]): Config =
config.withValue(path, ConfigValueFactory.fromAnyRef(value.orNull))
}
class Int2D(var x: Int, var y: Int) {

View File

@@ -0,0 +1,29 @@
package ocelot.desktop.ui.widget
import ocelot.desktop.geometry.Padding2D
import ocelot.desktop.ui.layout.LinearLayout
import ocelot.desktop.ui.widget.modal.ModalDialog
import ocelot.desktop.util.Orientation
class NotificationDialog(message: String) extends ModalDialog {
children :+= new PaddingBox(new Widget {
override val layout = new LinearLayout(this, orientation = Orientation.Vertical)
children :++= message.split('\n').map(line => new PaddingBox(new Label {
override def text: String = line
}, Padding2D(bottom = 8)))
children :+= new Widget {
children :+= new Filler
children :+= new PaddingBox(new Button {
override def text: String = "Ok"
override def onClick(): Unit = close()
}, Padding2D(right = 8, top = 8))
}
}, Padding2D.equal(16))
def onSaveSelected(): Unit = {}
def onExitSelected(): Unit = {}
}