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
/*.so
/*.dylib
/natives/
# Log files
*.log

View File

@ -122,10 +122,14 @@ object UiHandler extends Logging {
}
}
var tempDir: String = _
var nativeLibrariesDir: String = _
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 is64bit = arch.startsWith("amd64")
@ -146,16 +150,19 @@ object UiHandler extends Logging {
throw new Exception("Unsupported OS")
for (lib <- libs) {
val source = getClass.getResourceAsStream("/" + lib)
val dest = new File(Paths.get(tempDir, lib).toString)
val output = new FileOutputStream(dest)
output.getChannel.transferFrom(Channels.newChannel(source), 0, Long.MaxValue)
output.flush()
output.close()
source.close()
val dest = new File(Paths.get(nativeLibrariesDir, lib).toString)
if (!dest.exists()) {
val source = getClass.getResourceAsStream("/" + lib)
if (!dest.getParentFile.exists()) dest.getParentFile.mkdirs()
val output = new FileOutputStream(dest)
output.getChannel.transferFrom(Channels.newChannel(source), 0, Long.MaxValue)
output.flush()
output.close()
source.close()
}
}
System.setProperty("org.lwjgl.librarypath", tempDir)
System.setProperty("org.lwjgl.librarypath", nativeLibrariesDir)
}
private def loadIcons(): Unit = {
@ -238,7 +245,8 @@ object UiHandler extends Logging {
if (!audioDisabled)
AL.destroy()
FileUtils.deleteDirectory(new File(tempDir))
if (!SystemUtils.IS_OS_WINDOWS)
FileUtils.deleteDirectory(new File(nativeLibrariesDir))
}
private def update(): Unit = {