catching error when creating lua state due to missing libraries in factory and logging it right there, to allow printing error to chat the first time, too; different name the lib is saved to the tmp folder to reduce the chance of collisions; explicit message for windows users reminding them to install the vc2012 runtimes on library load errors

This commit is contained in:
Florian Nücke 2013-12-27 00:18:22 +01:00
parent fb6f0a8866
commit 55524c4a78

View File

@ -5,6 +5,7 @@ import com.naef.jnlua.{LuaState, NativeSupport}
import java.io.File
import java.io.FileOutputStream
import java.nio.channels.Channels
import java.util.logging.Level
import li.cil.oc.server.component.Computer
import li.cil.oc.util.ExtendedLuaState._
import li.cil.oc.{OpenComputers, Settings}
@ -27,6 +28,8 @@ object LuaStateFactory {
/** Set to true in initialization code below if available. */
private var haveNativeLibrary = false
private var isWindows = false
private var _is64Bit = false
def is64Bit = _is64Bit
@ -76,6 +79,7 @@ object LuaStateFactory {
break()
}
}
isWindows = extension == ".dll"
val libPath = "/assets/" + Settings.resourceDomain + "/lib/"
val tmpPath = {
@ -92,7 +96,7 @@ object LuaStateFactory {
}
// Found file with proper extension. Create a temporary file.
val file = new File(tmpPath + library)
val file = new File(tmpPath + "OpenComputersMod-" + library)
// Try to delete an old instance of the library, in case we have an update
// and deleteOnExit fails (which it regularly does on Windows it seems).
try {
@ -145,6 +149,7 @@ object LuaStateFactory {
def createState(): Option[LuaState] = {
if (!haveNativeLibrary) return None
try {
val state = new LuaState(Int.MaxValue)
try {
// Load all libraries.
@ -278,12 +283,26 @@ object LuaStateFactory {
state.setGlobal("unicode")
Some(state)
} catch {
case ex: Throwable =>
ex.printStackTrace()
state.close()
return None
return Some(state)
}
catch {
case t: Throwable =>
OpenComputers.log.log(Level.WARNING, "Failed creating Lua state.", t)
state.close()
}
}
catch {
case _: UnsatisfiedLinkError =>
OpenComputers.log.severe("Failed loading the native libraries.")
if (isWindows) {
OpenComputers.log.severe(
"Please ensure you have the Visual C++ 2012 Runtime installed " +
"(when on 64 Bit, both the 32 bit and 64 bit version of the " +
"runtime).")
}
case t: Throwable =>
OpenComputers.log.log(Level.WARNING, "Failed creating Lua state.", t)
}
None
}
}