Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.62 KB | None | 0 0
  1. package xyz.roolurker.asarify
  2.  
  3. import java.awt.GraphicsEnvironment
  4. import java.io.*
  5. import java.util.*
  6. import javax.swing.filechooser.FileSystemView
  7.  
  8.  
  9. /**
  10.  * Created with love by rooLurk on 2/25/2017.
  11.  */
  12.  
  13. val cssInjection: String = """window._fs = require("fs");
  14. window._fileWatcher = null;
  15. window._styleTag = null;
  16. window.setupCSS = function (d) {
  17.     var e = window._fs.readFileSync(d, "utf-8");
  18.     if (window._styleTag === null) {
  19.         window._styleTag = document.createElement("style");
  20.         document.head.appendChild(window._styleTag)
  21.     }
  22.     window._styleTag.innerHTML = e;
  23.     if (window._fileWatcher === null) {
  24.         window._fileWatcher = window._fs.watch(d, {encoding: "utf-8"}, function (a, b) {
  25.             if (a === "change") {
  26.                 var c = window._fs.readFileSync(d, "utf-8");
  27.                 window._styleTag.innerHTML = c
  28.             }
  29.         })
  30.     }
  31. };
  32. window.tearDownCSS = function () {
  33.     if (window._styleTag !== null) {
  34.         window._styleTag.innerHTML = ""
  35.     }
  36.     if (window._fileWatcher !== null) {
  37.         window._fileWatcher.close();
  38.         window._fileWatcher = null
  39.     }
  40. };
  41. window.applyAndWatchCSS = function (a) {
  42.     window.tearDownCSS();
  43.     window.setupCSS(a)
  44. };
  45. window.applyAndWatchCSS('DOCUMENTPATH');"""
  46.  
  47. fun main(args: Array<String>) {
  48.     Main().run()
  49. }
  50.  
  51. class Main {
  52.     fun run() {
  53.         val console = System.console()
  54.         if (console == null && !GraphicsEnvironment.isHeadless()) {
  55.             val filename = Asarify::class.java.protectionDomain.codeSource.location.toString().substring(6)
  56.             Runtime.getRuntime().exec(arrayOf("cmd", "/c", "start", "cmd", "/k", "java -jar \"" + filename + "\""))
  57.         } else {
  58.             Asarify().run()
  59.         }
  60.     }
  61. }
  62.  
  63. class Asarify {
  64.     fun run() {
  65.         val local: File = File(System.getenv("LOCALAPPDATA"))
  66.         val discords: ArrayList<File> = arrayListOf()
  67.         local.listFiles().filter { it.name.startsWith("Discord") }.forEach {
  68.             discords.add(it)
  69.         }
  70.  
  71.         var discord: File? = null
  72.  
  73.         when (discords.size) {
  74.             0 -> {
  75.                 println("Discord is not installed?"); System.exit(0)
  76.             }
  77.             1 -> {
  78.                 discord = discords.first()
  79.             }
  80.             else -> {
  81.                 do {
  82.                     val reader = Scanner(System.`in`)
  83.                     for (i in 0..discords.size - 1) {
  84.                         println("$i: ${discords[i].name}")
  85.                     }
  86.                     println("Enter a number: ")
  87.                     val n = reader.nextInt()
  88.                     if (n >= 0 && n < discords.size) {
  89.                         discord = discords[n]
  90.                         break
  91.                     }
  92.                 } while (discord == null)
  93.             }
  94.         }
  95.         discord!!.listFiles().filter { it.name.startsWith("app-") }.forEach {
  96.             it.listFiles().filter { it.name == "resources" }.forEach { res ->
  97.                 if (res.listFiles().any { it.name == "app" }) {
  98.                     println("Already installed")
  99.                 } else {
  100.                     res.listFiles().filter { it.name == "app.asar" }.forEach { asar ->
  101.                         if (!isDiscordRunning()) {
  102.                             println("Extracting...")
  103.                             println("This might take a while")
  104.                             val node: File = File("node/node.exe")
  105.                             val asarFile: File = File("node/node_modules/asar/bin/asar.js")
  106.                             val sep: String = File.separator
  107.                             val p = Runtime.getRuntime().exec("${node.absoluteFile} $asarFile extract ${asar.absoluteFile} ${res.absolutePath}${sep}app$sep")
  108.                             val css: File = File(FileSystemView.getFileSystemView().defaultDirectory.path + "${sep}discord.css")
  109.                             val errorReader: InputStreamReader = InputStreamReader(p.errorStream)
  110.                             errorReader.forEachLine(::println)
  111.                             println("Extracted!")
  112.                             println("Injecting script...")
  113.                             val index: File = File(res.absolutePath + "${sep}app${sep}index.js")
  114.                             val injection: File = File(res.absolutePath + "${sep}app${sep}CSSInjection.js")
  115.                             val fr: FileReader = FileReader(index)
  116.                             val br: BufferedReader = BufferedReader(fr)
  117.                             val js: String = br.readText()
  118.                             br.close()
  119.                             val fw: FileWriter = FileWriter(index)
  120.                             val bw: BufferedWriter = BufferedWriter(fw)
  121.                             bw.write(js.replace("mainWindow.webContents.on('dom-ready', function () {});",
  122.                                     "mainWindow.webContents.on('dom-ready', function () {mainWindow.webContents.executeJavaScript(_fs2.default.readFileSync('${injection.absoluteFile.toString().replace("\\", "\\\\")}','utf-8'));});"))
  123.                             bw.flush()
  124.                             bw.close()
  125.                             if (!injection.exists()) {
  126.                                 injection.createNewFile()
  127.                             }
  128.                             val fw1: FileWriter = FileWriter(injection)
  129.                             val bw1: BufferedWriter = BufferedWriter(fw1)
  130.                             bw1.write(cssInjection.replace("DOCUMENTPATH", css.absoluteFile.toString().replace("\\", "\\\\")))
  131.                             bw1.flush()
  132.                             bw1.close()
  133.                             println("Injection complete")
  134.                             if (!css.exists()) {
  135.                                 println("Creating CSS file...")
  136.                                 css.createNewFile()
  137.                                 val fw2: FileWriter = FileWriter(css)
  138.                                 val bw2: BufferedWriter = BufferedWriter(fw2)
  139.                                 bw2.write("/* Put your CSS here */")
  140.                                 bw2.flush()
  141.                                 bw2.close()
  142.                             }
  143.                             println("Renaming original asar...")
  144.                             if (asar.absoluteFile.renameTo(File("${res.absoluteFile}${sep}original_app.asar"))) {
  145.                                 println("Success!")
  146.                             } else {
  147.                                 println("Failed, please rename the file at ${asar.absoluteFile} to something else")
  148.                             }
  149.                             println("All done! You can re-launch Discord and edit your CSS file at ${css.absoluteFile}")
  150.                             println("You may now close this Window.")
  151.                         } else {
  152.                             println("Please fully close Discord first!")
  153.                         }
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.     }
  159.  
  160.     fun isDiscordRunning(): Boolean {
  161.         var pidInfo = ""
  162.         val p = Runtime.getRuntime().exec(System.getenv("windir") + "\\system32\\" + "tasklist.exe")
  163.         val input = BufferedReader(InputStreamReader(p.inputStream))
  164.         input.forEachLine { pidInfo += it }
  165.         input.close()
  166.         if (pidInfo.contains("Discord")) {
  167.             return true
  168.         }
  169.         return false
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement