Skip to content

Commit

Permalink
Externals Finalized
Browse files Browse the repository at this point in the history
  • Loading branch information
Rizlim authored and Rizlim committed Jun 2, 2019
1 parent 66effcf commit fcf2e3b
Show file tree
Hide file tree
Showing 13 changed files with 549 additions and 42 deletions.
135 changes: 134 additions & 1 deletion src/main/kotlin/PAL2/Addons/Externals.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package PAL2.Addons

import PAL2.Database.countExternalAddon
import PAL2.Database.*
import PAL2.GUI.CoreApplication
import PAL2.GUI.LightningEffects
import PAL2.SystemHandling.FileDownloader
import PAL2.SystemHandling.InstallHandler
import PAL2.SystemHandling.InstallHandlerHelpers
import PAL_DataClasses.PAL_External_Addon
import SystemHandling.deleteFile
import javafx.application.Platform
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File
import java.net.URL
import java.nio.file.Files
import java.nio.file.Paths
import java.security.MessageDigest
Expand All @@ -15,6 +24,16 @@ import javax.xml.bind.DatatypeConverter
*/
object Externals
{
val LUTBOT_DL = "http://lutbot.com/ahk/macro.ahk"
val SIC_DL = "https://synthesisparser.herokuapp.com/SynthesisParser.ahk"

fun calcCRC32(bytes: ByteArray): String
{
val hash = CRC32()
hash.update(bytes)
return hash.value.toString(16)
}

fun calcCRC32(file: File): String
{
val bytes = Files.readAllBytes(Paths.get(file.toURI()))
Expand All @@ -39,4 +58,118 @@ object Externals
{
return countExternalAddon()+1
}

fun syncAHKsWithExternals()
{
val ahks = countAHKs()

if (ahks == 0)
return

// Retrieve AHKs
val scripts = getAHKScriptsArray()

for (ahk in scripts)
{
val file = File(ahk.location)
if (file.exists() && !file.isDirectory)
{
val crc32 = Externals.calcCRC32(file)
val cmd = Externals.determineCMD(file)
val dbid = Externals.determineDBID()
val name = file.nameWithoutExtension
val path = file.path
val ea = PAL_External_Addon(dbid, name, crc32, "", "", "", "", cmd, path, ahk.runOnLaunch)
CoreApplication.controller.saveExternal(ea)

}
}

// Delete AHKs from DB
nukeAHK()
}

fun checkForUpdatesAndUpdateExternals()
{
//TODO: This
// Check for updates


// If update found back up current file


//
}

/**
* Returns AID for setting installed, matches based on "website_source"
*/
fun isMajorAddon(string: String): Int
{
return when (string)
{
LUTBOT_DL -> 9
SIC_DL -> 17
else -> 0
}
}

fun isExternal(aid: Int): Boolean
{
return when (aid)
{
17 -> true // Synthesized-Implicit-Calculator
9 -> true // Lutbot
else -> false
}
}

fun addLutBot()
{
if (!GlobalData.addonFolder.exists())
GlobalData.addonFolder.mkdir()

var install = GlobalData.addonFolder.path + File.separator + "lutbot"

if (!File(install).exists())
File(install).mkdir()

install += File.separator + "macro.ahk"

val ea = PAL_External_Addon(-1, "Lutbot", "", "", null, "", LUTBOT_DL, determineCMD(File(install)), install, false)

// Download
CoreApplication.controller.showDownloadPopup(File(ea.webSource).name)
val location = FileDownloader().downloadFile(URL(ea.webSource), GlobalData.temp_down_folder, 1024, GlobalData.noIcon)
val dest = File(ea.path)
deleteFile(dest)
Files.copy(location.toPath(), dest.toPath())
ea.checksum = calcCRC32(dest)

CoreApplication.controller.saveExternal(ea)
}

fun addSIC()
{
if (!GlobalData.addonFolder.exists())
GlobalData.addonFolder.mkdir()

var install = GlobalData.addonFolder.path + File.separator + "Synthesized-Implicit-Calculator"

if (!File(install).exists())
File(install).mkdir()

install += File.separator + "SynthesisParser.ahk"

val ea = PAL_External_Addon(-1, "Synthesized Implicit Calculator", "", "", null, "", SIC_DL, determineCMD(File(install)), install, false)

CoreApplication.controller.showDownloadPopup(File(ea.webSource).name)
val location = FileDownloader().downloadFile(URL(ea.webSource), GlobalData.temp_down_folder, 1024, GlobalData.noIcon)
val dest = File(ea.path)
deleteFile(dest)
Files.copy(location.toPath(), dest.toPath())
ea.checksum = calcCRC32(dest)

CoreApplication.controller.saveExternal(ea)
}
}
69 changes: 68 additions & 1 deletion src/main/kotlin/PAL2/Database/DataBaseHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ fun connectToDB(): Connection
return DriverManager.getConnection("jdbc:sqlite:${GlobalData.db_file.path}")
}

fun nukeAHK()
{
val connection = connectToDB()
connection.createStatement().execute("DELETE from AHK_Scripts")
connection.close()
}

// Reads GlobalData to sync
fun syncGlobalWithDB()
{
Expand Down Expand Up @@ -138,6 +145,7 @@ fun getRunAddonOnLaunch(aid: Int): Boolean
return result
}


fun removeInstalledAddon(aid: Int)
{
val connection = connectToDB()
Expand Down Expand Up @@ -260,6 +268,32 @@ fun getAHKScripts()
connection.close()
}

data class AHK(var location: String, var runOnLaunch: Boolean)

fun getAHKScriptsArray(): Array<AHK>
{
val arr = ArrayList<AHK>()
val connection = connectToDB()
val statement = connection.createStatement()
var rs = statement.executeQuery("SELECT count(*) from AHK_Scripts")
val c = rs.getInt(1)
if (c != 0)
{
rs = statement.executeQuery("SELECT location, run_on_launch from AHK_Scripts")
while (rs.next())
{
arr.add(AHK(rs.getString(1), rs.getBoolean(2)))
}
}
else
{
logger.debug { "No AHK Scripts stored." }
}

connection.close()
return arr.toTypedArray()
}

fun getExternalsOnLaunchCommands(): Array<String>?
{
val count = countExternalAddon()
Expand Down Expand Up @@ -434,13 +468,21 @@ fun countExternalAddon(): Int
val count = rs.getInt(1)
connection.close()
return count
}

fun countAHKs(): Int
{
val connection = connectToDB()
val rs = connection.createStatement().executeQuery("SELECT COUNT(*) FROM AHK_Scripts")
val count = rs.getInt(1)
connection.close()
return count
}

fun insertExternalAddonIntoDB(ea: PAL_External_Addon)
{
val connection = connectToDB()
val sql = "INSERT into ExternalAddon values (${ea.eid}, \'${ea.name}\', \'${ea.path}\', \'${ea.checksum}\', \'${ea.webSource}\', \'${ea.iconUrl}\', \'${ea.launchCMD}\', false);"
val sql = "INSERT into ExternalAddon values (${ea.eid}, \'${ea.name}\', \'${ea.path}\', \'${ea.checksum}\', \'${ea.webSource}\', \'${ea.iconUrl}\', \'${ea.launchCMD}\', ${ea.runOnLaunch});"
println(sql)
connection.createStatement().execute(sql)
connection.close()
Expand Down Expand Up @@ -480,6 +522,30 @@ fun retreiveExternalsFromDB(): ArrayList<PAL_External_Addon>?
return arr
}

/**
* Retreives download source from DB, can be an empty string.
*/
fun retreiveWebSource(eid: Int): String?
{
val c = countExternalAddon()

if (c == 0)
return null

val connection = connectToDB()

var rs = connection.createStatement().executeQuery("SELECT count(*) from ExternalAddon where eid = $eid;")
val count = rs.getInt(1)

if (count != 1)
return null

rs = connection.createStatement().executeQuery("SELECT website_source from ExternalAddon where eid = $eid;")
val str = rs.getString(1)
connection.close()
return if (str != "") str else null
}

fun hideExternalAddon(eid: Int)
{
val c = countExternalAddon()
Expand All @@ -490,6 +556,7 @@ fun hideExternalAddon(eid: Int)
val connection = connectToDB()
connection.createStatement().executeUpdate("UPDATE ExternalAddon set name = \'3c484944453e\' WHERE eid = $eid;")
connection.close()
updateRunOnLaunchExternal(eid, false)
}

fun updateRunOnLaunchExternal(eid: Int, run_on_launch: Boolean)
Expand Down
45 changes: 31 additions & 14 deletions src/main/kotlin/PAL2/GUI/AddonAnchor.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package PAL2.GUI

import PAL2.Addons.Externals
import PAL_DataClasses.PAL_AddonFullData
import PAL2.SystemHandling.FileDownloader
import PAL2.SystemHandling.launchAddon
import PAL_DataClasses.PAL_External_Addon
import SystemHandling.checkForUseableDownloads
import javafx.application.Platform
import javafx.event.EventHandler
Expand Down Expand Up @@ -182,29 +184,44 @@ class AddonAnchor(var name: String, var version: String, var repo: String, var i
{
if(it.button == MouseButton.PRIMARY)
{
val a = GlobalData.getAddonByID(addonID)
if (a != null)
rectButtonLeft.isVisible = false
buttonLeftText.text = "Downloading"
if (Externals.isExternal(addonID))
{
val download_urls = checkForUseableDownloads(a.download_urls, addonID)
if (download_urls.size == 1)
// TODO: Hide download button
when (addonID)
{
CoreApplication.controller.startDownload(download_urls[0], a.aid, icon.image)
17 -> Externals.addSIC()
9 -> Externals.addLutBot()
}
else
buttonLeftText.text = "Installed"
}
else
{
val a = GlobalData.getAddonByID(addonID)
if (a != null)
{
var desc = "No description has been set, sorry."

if (a.description != null)
val download_urls = checkForUseableDownloads(a.download_urls, addonID)
if (download_urls.size == 1)
{
if (a.description is String)
CoreApplication.controller.startDownload(download_urls[0], a.aid, icon.image)
}
else
{
var desc = "No description has been set, sorry."

if (a.description != null)
{
desc = a.description!!
if (a.description is String)
{
desc = a.description!!
}

}

CoreApplication.controller.setDescInfo(icon.image, name, desc, addonID)
CoreApplication.controller.showDownloadsPage(download_urls)
}

CoreApplication.controller.setDescInfo(icon.image, name, desc, addonID)
CoreApplication.controller.showDownloadsPage(download_urls)
}
}
}
Expand Down
Loading

0 comments on commit fcf2e3b

Please sign in to comment.