@@ -30,7 +30,7 @@ class UnityTranslateLib(val path: Path) {
3030 private suspend fun createTranslator (code : String , useCuda : Boolean = false): Translator {
3131 val split = code.split(" _" )
3232
33- val translator = if (split[0 ] == split[1 ])
33+ val translator = if (split[0 ] == split[1 ] || ! isAvailable() )
3434 // Just passthrough if they are the same
3535 DummyTranslator (this , code)
3636 else
@@ -65,28 +65,35 @@ class UnityTranslateLib(val path: Path) {
6565
6666 // Modified from ImGui-java's library loading - https://github.com/SpaiR/imgui-java/blob/main/imgui-binding/src/main/java/imgui/ImGui.java
6767 init {
68- val libPath = System .getProperty(" unitytranslate.library.path" )
69- val libName = System .getProperty(" unitytranslate.library.name" , " UnityTranslateLib" )
70- val fullLibName = resolveFullLibName()
71-
72- if (libPath != null ) {
73- System .load(Paths .get(libPath).resolve(fullLibName).absolutePathString())
74- } else {
75- try {
76- System .loadLibrary(libName)
77- } catch (e: Throwable ) {
78- val extractedPath = try {
79- tryLoadFromClassPath(fullLibName)
80- } catch (e2: Exception ) {
81- val joined = RuntimeException (" Failed to load natives for UnityTranslateLib!" )
82- joined.addSuppressed(e2)
83- joined.addSuppressed(e)
84-
85- throw joined
68+ if (isAvailable()) {
69+ val libPath = System .getProperty(" unitytranslate.library.path" )
70+ val libName = System .getProperty(" unitytranslate.library.name" , " UnityTranslateLib" )
71+ val fullLibName = resolveFullLibName()
72+
73+ if (libPath != null ) {
74+ System .load(Paths .get(libPath).resolve(fullLibName).absolutePathString())
75+ } else {
76+ try {
77+ System .loadLibrary(libName)
78+ } catch (e: Throwable ) {
79+ val extractedPath = try {
80+ tryLoadFromClassPath(fullLibName)
81+ } catch (e2: Exception ) {
82+ val joined = RuntimeException (" Failed to load natives for UnityTranslateLib!" )
83+ joined.addSuppressed(e2)
84+ joined.addSuppressed(e)
85+
86+ throw joined
87+ }
88+
89+ for (lib in platformLibs.reversed()) {
90+ System .load(extractedPath.resolve(lib).absolutePathString())
91+ }
8692 }
87-
88- System .load(extractedPath)
8993 }
94+ } else {
95+ logger.warn(" UnityTranslateLib is unsupported on platform ${System .getProperty(" os.name" )} (${System .getProperty(" os.arch" )} )!" )
96+ logger.warn(" As a result, UnityTranslateLib will not be translating, and may cause errors if any native calls are attempted." )
9097 }
9198 }
9299
@@ -122,15 +129,15 @@ class UnityTranslateLib(val path: Path) {
122129 listOf ()
123130 else
124131 listOf (
132+ " $dir /libUnityTranslateLib.so" ,
125133 " $dir /libctranslate2.so" ,
126134 " $dir /libcudnn.so" ,
127- " $dir /libgomp.so" ,
128- " $dir /libUnityTranslateLib.so"
135+ " $dir /libgomp.so"
129136 )
130137 } else emptyList()
131138 }
132139
133- private fun tryLoadFromClassPath (fullLibName : String ): String {
140+ private fun tryLoadFromClassPath (fullLibName : String ): Path {
134141 val classLoader = UnityTranslateLib ::class .java.classLoader
135142 val libs = platformLibs
136143
@@ -160,7 +167,13 @@ class UnityTranslateLib(val path: Path) {
160167 if (! unityTranslatePath.exists())
161168 throw Exception (" Failed to load library files for UnityTranslateLib!" )
162169
163- return unityTranslatePath.absolutePathString()
170+ return tmpDir
171+ }
172+
173+ private val cachedPlatformLibs = platformLibs
174+
175+ fun isAvailable (): Boolean {
176+ return cachedPlatformLibs.isNotEmpty()
164177 }
165178 }
166179}
0 commit comments