@@ -8,10 +8,13 @@ import xyz.bluspring.unitytranslate.library.models.argos.ArgosPackageIndex
88import xyz.bluspring.unitytranslate.library.translator.DummyTranslator
99import xyz.bluspring.unitytranslate.library.translator.ModelBasedTranslator
1010import xyz.bluspring.unitytranslate.library.translator.Translator
11+ import java.nio.file.Files
1112import java.nio.file.Path
13+ import java.nio.file.Paths
14+ import java.nio.file.StandardCopyOption
1215import java.util.concurrent.ConcurrentHashMap
13- import kotlin.io.path.Path
1416import kotlin.io.path.absolutePathString
17+ import kotlin.io.path.exists
1518
1619class UnityTranslateLib (val path : Path ) {
1720 val packageIndex = ModelPackageManager (this ).apply {
@@ -58,8 +61,104 @@ class UnityTranslateLib(val path: Path) {
5861 companion object {
5962 val logger: Logger = LoggerFactory .getLogger(UnityTranslateLib ::class .java)
6063
64+ // Modified from ImGui-java's library loading - https://github.com/SpaiR/imgui-java/blob/main/imgui-binding/src/main/java/imgui/ImGui.java
6165 init {
62- System .load(Path (" UnityTranslateLibRsAgain.dll" ).toAbsolutePath().absolutePathString())
66+ val libPath = System .getProperty(" unitytranslate.library.path" )
67+ val libName = System .getProperty(" unitytranslate.library.name" , " UnityTranslateLib" )
68+ val fullLibName = resolveFullLibName()
69+
70+ if (libPath != null ) {
71+ System .load(Paths .get(libPath).resolve(fullLibName).absolutePathString())
72+ } else {
73+ try {
74+ System .loadLibrary(libName)
75+ } catch (e: Throwable ) {
76+ val extractedPath = try {
77+ tryLoadFromClassPath(fullLibName)
78+ } catch (e2: Exception ) {
79+ val joined = RuntimeException (" Failed to load natives for UnityTranslateLib!" )
80+ joined.addSuppressed(e2)
81+ joined.addSuppressed(e)
82+
83+ throw joined
84+ }
85+
86+ System .load(extractedPath)
87+ }
88+ }
89+ }
90+
91+ private fun resolveFullLibName (): String {
92+ val osName = System .getProperty(" os.name" ).lowercase()
93+ val isWindows = osName.contains(" win" )
94+ val isMac = osName.contains(" mac" )
95+
96+ val libPrefix = if (isWindows) " " else " lib"
97+ val libSuffix = if (isWindows) " .dll" else if (isMac) " .dylib" else " .so"
98+
99+ return System .getProperty(" unitytranslate.library.name" , " ${libPrefix} UnityTranslateLib${libSuffix} " )
100+ }
101+
102+ private val platformLibs: List <String >
103+ get() {
104+ val osName = System .getProperty(" os.name" ).lowercase()
105+ val osArch = System .getProperty(" os.arch" ).lowercase()
106+ val isWindows = osName.contains(" win" )
107+ val isMac = osName.contains(" mac" )
108+
109+ val dir = " unitytranslate/${if (isWindows) " windows" else if (isMac) " osx" else " linux" } -${osArch} "
110+
111+ return if (osArch == " amd64" ) {
112+ if (isWindows)
113+ listOf (
114+ " $dir /UnityTranslateLib.dll" ,
115+ " $dir /ctranslate2.dll" ,
116+ " $dir /cudnn64_9.dll" ,
117+ " $dir /libiomp5md.dll"
118+ )
119+ else if (isMac)
120+ listOf ()
121+ else
122+ listOf (
123+ " $dir /libctranslate2.so" ,
124+ " $dir /libcudnn.so" ,
125+ " $dir /libgomp.so" ,
126+ " $dir /libUnityTranslateLib.so"
127+ )
128+ } else emptyList()
129+ }
130+
131+ private fun tryLoadFromClassPath (fullLibName : String ): String {
132+ val classLoader = UnityTranslateLib ::class .java.classLoader
133+ val libs = platformLibs
134+
135+ if (libs.isEmpty())
136+ throw Exception (" Unsupported platform ${System .getProperty(" os.name" )} (${System .getProperty(" os.arch" )} )!" )
137+
138+ val tmpDir = Paths .get(System .getProperty(" java.io.tmpdir" )).resolve(" unitytranslate-natives" )
139+
140+ if (! tmpDir.exists())
141+ tmpDir.toFile().mkdirs()
142+
143+ for (packedLibPath in libs) {
144+ val libName = packedLibPath.split(" /" ).last()
145+
146+ classLoader.getResourceAsStream(packedLibPath)?.use {
147+ val libPath = tmpDir.resolve(libName)
148+ try {
149+ Files .copy(it, libPath, StandardCopyOption .REPLACE_EXISTING )
150+ } catch (e: AccessDeniedException ) {
151+ if (! libPath.exists())
152+ throw e
153+ }
154+ }
155+ }
156+
157+ val unityTranslatePath = tmpDir.resolve(fullLibName)
158+ if (! unityTranslatePath.exists())
159+ throw Exception (" Failed to load library files for UnityTranslateLib!" )
160+
161+ return unityTranslatePath.absolutePathString()
63162 }
64163 }
65164}
0 commit comments