Fix #16: crash when trying to clean native DLLs on Windows

This commit is contained in:
UnicornFreedom 2022-06-16 19:00:33 +02:00
parent f0dd326086
commit e4e7aeb39d
2 changed files with 20 additions and 11 deletions

1
.gitignore vendored
View File

@ -38,6 +38,7 @@ cacerts
/*.dll /*.dll
/*.so /*.so
/*.dylib /*.dylib
/natives/
# Log files # Log files
*.log *.log

View File

@ -122,10 +122,14 @@ object UiHandler extends Logging {
} }
} }
var tempDir: String = _ var nativeLibrariesDir: String = _
private def loadLibraries(): Unit = { private def loadLibraries(): Unit = {
tempDir = Files.createTempDirectory("ocelot-desktop").toString // we cannot remove DLL files on Windows after they were loaded by Ocelot
// therefore we will create them in local directory and keep for future
nativeLibrariesDir = if (SystemUtils.IS_OS_WINDOWS)
Paths.get(SystemUtils.USER_DIR, "natives").toString
else Files.createTempDirectory("ocelot-desktop").toString
val arch = System.getProperty("os.arch") val arch = System.getProperty("os.arch")
val is64bit = arch.startsWith("amd64") val is64bit = arch.startsWith("amd64")
@ -146,16 +150,19 @@ object UiHandler extends Logging {
throw new Exception("Unsupported OS") throw new Exception("Unsupported OS")
for (lib <- libs) { for (lib <- libs) {
val dest = new File(Paths.get(nativeLibrariesDir, lib).toString)
if (!dest.exists()) {
val source = getClass.getResourceAsStream("/" + lib) val source = getClass.getResourceAsStream("/" + lib)
val dest = new File(Paths.get(tempDir, lib).toString) if (!dest.getParentFile.exists()) dest.getParentFile.mkdirs()
val output = new FileOutputStream(dest) val output = new FileOutputStream(dest)
output.getChannel.transferFrom(Channels.newChannel(source), 0, Long.MaxValue) output.getChannel.transferFrom(Channels.newChannel(source), 0, Long.MaxValue)
output.flush() output.flush()
output.close() output.close()
source.close() source.close()
} }
}
System.setProperty("org.lwjgl.librarypath", tempDir) System.setProperty("org.lwjgl.librarypath", nativeLibrariesDir)
} }
private def loadIcons(): Unit = { private def loadIcons(): Unit = {
@ -238,7 +245,8 @@ object UiHandler extends Logging {
if (!audioDisabled) if (!audioDisabled)
AL.destroy() AL.destroy()
FileUtils.deleteDirectory(new File(tempDir)) if (!SystemUtils.IS_OS_WINDOWS)
FileUtils.deleteDirectory(new File(nativeLibrariesDir))
} }
private def update(): Unit = { private def update(): Unit = {