From e4e7aeb39d75c768c42418611cae6f9aee64dfe7 Mon Sep 17 00:00:00 2001 From: UnicornFreedom Date: Thu, 16 Jun 2022 19:00:33 +0200 Subject: [PATCH] Fix #16: crash when trying to clean native DLLs on Windows --- .gitignore | 1 + .../scala/ocelot/desktop/ui/UiHandler.scala | 30 ++++++++++++------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 54cb580..6725c54 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ cacerts /*.dll /*.so /*.dylib +/natives/ # Log files *.log diff --git a/src/main/scala/ocelot/desktop/ui/UiHandler.scala b/src/main/scala/ocelot/desktop/ui/UiHandler.scala index 45ffe6a..48d106e 100644 --- a/src/main/scala/ocelot/desktop/ui/UiHandler.scala +++ b/src/main/scala/ocelot/desktop/ui/UiHandler.scala @@ -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 = {