/src/main/kotlin/PAL2/SystemHandling/LaunchHandling.kt

https://github.com/POE-Addon-Launcher/PAL2 · Kotlin · 289 lines · 253 code · 31 blank · 5 comment · 43 complexity · d9d162230b49a1773df8011c5af6ed9d MD5 · raw file

  1. package PAL2.SystemHandling
  2. import GlobalData
  3. import PAL2.Database.getExternalsOnLaunchCommands
  4. import PAL2.Database.getInstallDir
  5. import PAL2.Database.getLaunchCommand
  6. import PAL2.Database.getRunOnLaunchCommands
  7. import kotlinx.coroutines.GlobalScope
  8. import kotlinx.coroutines.launch
  9. import mu.KotlinLogging
  10. import java.awt.Desktop.getDesktop
  11. import java.io.File
  12. import java.io.IOException
  13. import java.net.URI
  14. private val logger = KotlinLogging.logger {}
  15. /**
  16. *
  17. */
  18. fun launchAddon(aid: Int)
  19. {
  20. // Grab launch command from DB
  21. val lc = getLaunchCommand(aid)
  22. when (aid)
  23. {
  24. 14 -> procurementHandler(aid)
  25. 12 -> runPathOfMaps(lc)
  26. 15 -> leagueOverlayHandler(aid, lc)
  27. else -> defaultHandler(aid, lc)
  28. }
  29. }
  30. fun defaultHandler(aid: Int, lc: String)
  31. {
  32. GlobalScope.launch {
  33. // Check for exceptions: ? "SET_AHK_FOLDER" etc
  34. if (lc == "?")
  35. {
  36. logger.error { "NO LAUNCH COMMAND SET!" }
  37. }
  38. else if (lc == "SET_AHK_FOLDER")
  39. {
  40. logger.error { "SET AHK FOLDER!" }
  41. }
  42. else
  43. {
  44. val dir = getInstallDir(aid)
  45. if (dir != null)
  46. {
  47. Runtime.getRuntime().exec(lc, null, dir)
  48. }
  49. else
  50. {
  51. Runtime.getRuntime().exec(lc)
  52. }
  53. }
  54. }
  55. }
  56. fun runPathOfMaps(lc: String)
  57. {
  58. GlobalScope.launch {
  59. Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler $lc")
  60. }
  61. }
  62. fun leagueOverlayHandler(aid: Int, launch_command: String)
  63. {
  64. GlobalScope.launch {
  65. val dir = getInstallDir(aid)
  66. if (dir != null)
  67. {
  68. Runtime.getRuntime().exec(launch_command, null, dir)
  69. }
  70. }
  71. }
  72. fun procurementHandler(aid: Int)
  73. {
  74. GlobalScope.launch {
  75. val f = getInstallDir(aid)
  76. if (f != null)
  77. {
  78. var exe = ""
  79. for (_f in f.listFiles())
  80. {
  81. if (_f.name == "Procurement.exe")
  82. {
  83. exe = _f.path
  84. }
  85. }
  86. if (exe != "")
  87. Runtime.getRuntime().exec("\"$exe\"", null, f)
  88. }
  89. }
  90. }
  91. private fun launch_poe(exe: String)
  92. {
  93. logger.debug { "CALLED: LAUNCH_POE(exe) | exe = \'$exe\'" }
  94. if (exe.contains("PathOfExileSteam.exe") || exe.contains("PathOfExile_x64Steam.exe"))
  95. {
  96. logger.debug { "Attempting to run Steam PoE" }
  97. runSteamPoE()
  98. }
  99. else if (exe.contains("PathOfExile.exe") || exe.contains("PathOfExile_x64.exe"))
  100. {
  101. val dir: String
  102. val executable: String
  103. if (exe.contains("PathOfExile_x64.exe"))
  104. {
  105. logger.debug { "Found PathOfExile_x64.exe" }
  106. executable = "PathOfExile_x64.exe"
  107. dir = exe.replace(executable, "")
  108. logger.debug { "dir = $dir" }
  109. }
  110. else
  111. {
  112. logger.debug { "ELSE case" }
  113. executable = "PathOfExile.exe"
  114. dir = exe.replace(executable, "")
  115. logger.debug { "dir = $dir" }
  116. }
  117. try
  118. {
  119. logger.debug { "Runtime command: \"$exe\", NULL, ${File(dir)}" }
  120. logger.debug { "dir = $dir" }
  121. Runtime.getRuntime().exec("\"$exe\"", null, File(dir))
  122. }
  123. catch (e: IOException)
  124. {
  125. logger.error { e.printStackTrace() }
  126. }
  127. }
  128. }
  129. fun runSteamPoE()
  130. {
  131. val desktop = getDesktop()
  132. val steamProtocol = URI("steam://run/238960")
  133. desktop.browse(steamProtocol)
  134. }
  135. fun launchAddons()
  136. {
  137. if (GlobalData.launch_addons)
  138. {
  139. val arr = getRunOnLaunchCommands()
  140. if (arr != null)
  141. {
  142. for (lc in arr)
  143. {
  144. GlobalScope.launch {
  145. Runtime.getRuntime().exec(lc)
  146. }
  147. }
  148. }
  149. GlobalData.launch_addons = false
  150. }
  151. }
  152. @Deprecated("Uses different method to launch AHKs now.")
  153. fun launchAHKScripts()
  154. {
  155. var arr = GlobalData.ahk_scripts
  156. for (ahk in arr)
  157. {
  158. GlobalScope.launch {
  159. Runtime.getRuntime().exec(createAHKLaunchCommand(ahk))
  160. }
  161. }
  162. }
  163. fun createAHKLaunchCommand(ahk_file: String): String
  164. {
  165. val ahk_folder = GlobalData.ahkFolder
  166. if (ahk_folder.path != "")
  167. {
  168. if (ahk_folder.exists())
  169. {
  170. val stringBuilder = StringBuilder()
  171. stringBuilder.append("\"")
  172. stringBuilder.append(ahk_folder.path)
  173. stringBuilder.append(File.separator)
  174. stringBuilder.append("autohotkey.exe")
  175. stringBuilder.append("\" ")
  176. stringBuilder.append("\"")
  177. stringBuilder.append(ahk_file)
  178. stringBuilder.append("\" ")
  179. logger.debug { stringBuilder.toString() }
  180. return stringBuilder.toString()
  181. }
  182. else
  183. {
  184. return "SET_AHK_FOLDER"
  185. }
  186. }
  187. return "SET_AHK_FOLDER"
  188. }
  189. fun launchPoE()
  190. {
  191. if (GlobalData.steam_poe)
  192. {
  193. GlobalScope.launch {
  194. GlobalScope.launch {
  195. launchAddons()
  196. }
  197. GlobalScope.launch {
  198. launchExternals()
  199. }
  200. GlobalScope.launch {
  201. runSteamPoE()
  202. }
  203. }
  204. return
  205. }
  206. if (GlobalData.poeLocations.size == 0)
  207. {
  208. logger.error { "No path of exile directories are known." }
  209. }
  210. else if (GlobalData.poeLocations.size == 1)
  211. {
  212. GlobalScope.launch {
  213. GlobalScope.launch {
  214. launchAddons()
  215. }
  216. GlobalScope.launch {
  217. launchExternals()
  218. }
  219. GlobalScope.launch {
  220. launch_poe(GlobalData.poeLocations[0])
  221. }
  222. }
  223. }
  224. else
  225. {
  226. val f = GlobalData.primaryPoEFile
  227. if (f != null)
  228. {
  229. if (f.exists())
  230. {
  231. GlobalScope.launch {
  232. GlobalScope.launch {
  233. launchAddons()
  234. }
  235. GlobalScope.launch {
  236. launchExternals()
  237. }
  238. GlobalScope.launch {
  239. launch_poe(f.path)
  240. }
  241. }
  242. }
  243. }
  244. }
  245. }
  246. fun launchExternals()
  247. {
  248. if (!GlobalData.launch_externals)
  249. return
  250. val arr = getExternalsOnLaunchCommands() ?: return
  251. if (arr.isEmpty())
  252. return
  253. for (str in arr)
  254. {
  255. GlobalScope.launch { Runtime.getRuntime().exec(str) }
  256. }
  257. GlobalData.launch_externals = false
  258. }