Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xyz.roolurker.asarify
- import java.awt.GraphicsEnvironment
- import java.io.*
- import java.util.*
- import javax.swing.filechooser.FileSystemView
- /**
- * Created with love by rooLurk on 2/25/2017.
- */
- val cssInjection: String = """window._fs = require("fs");
- window._fileWatcher = null;
- window._styleTag = null;
- window.setupCSS = function (d) {
- var e = window._fs.readFileSync(d, "utf-8");
- if (window._styleTag === null) {
- window._styleTag = document.createElement("style");
- document.head.appendChild(window._styleTag)
- }
- window._styleTag.innerHTML = e;
- if (window._fileWatcher === null) {
- window._fileWatcher = window._fs.watch(d, {encoding: "utf-8"}, function (a, b) {
- if (a === "change") {
- var c = window._fs.readFileSync(d, "utf-8");
- window._styleTag.innerHTML = c
- }
- })
- }
- };
- window.tearDownCSS = function () {
- if (window._styleTag !== null) {
- window._styleTag.innerHTML = ""
- }
- if (window._fileWatcher !== null) {
- window._fileWatcher.close();
- window._fileWatcher = null
- }
- };
- window.applyAndWatchCSS = function (a) {
- window.tearDownCSS();
- window.setupCSS(a)
- };
- window.applyAndWatchCSS('DOCUMENTPATH');"""
- fun main(args: Array<String>) {
- Main().run()
- }
- class Main {
- fun run() {
- val console = System.console()
- if (console == null && !GraphicsEnvironment.isHeadless()) {
- val filename = Asarify::class.java.protectionDomain.codeSource.location.toString().substring(6)
- Runtime.getRuntime().exec(arrayOf("cmd", "/c", "start", "cmd", "/k", "java -jar \"" + filename + "\""))
- } else {
- Asarify().run()
- }
- }
- }
- class Asarify {
- fun run() {
- val local: File = File(System.getenv("LOCALAPPDATA"))
- val discords: ArrayList<File> = arrayListOf()
- local.listFiles().filter { it.name.startsWith("Discord") }.forEach {
- discords.add(it)
- }
- var discord: File? = null
- when (discords.size) {
- 0 -> {
- println("Discord is not installed?"); System.exit(0)
- }
- 1 -> {
- discord = discords.first()
- }
- else -> {
- do {
- val reader = Scanner(System.`in`)
- for (i in 0..discords.size - 1) {
- println("$i: ${discords[i].name}")
- }
- println("Enter a number: ")
- val n = reader.nextInt()
- if (n >= 0 && n < discords.size) {
- discord = discords[n]
- break
- }
- } while (discord == null)
- }
- }
- discord!!.listFiles().filter { it.name.startsWith("app-") }.forEach {
- it.listFiles().filter { it.name == "resources" }.forEach { res ->
- if (res.listFiles().any { it.name == "app" }) {
- println("Already installed")
- } else {
- res.listFiles().filter { it.name == "app.asar" }.forEach { asar ->
- if (!isDiscordRunning()) {
- println("Extracting...")
- println("This might take a while")
- val node: File = File("node/node.exe")
- val asarFile: File = File("node/node_modules/asar/bin/asar.js")
- val sep: String = File.separator
- val p = Runtime.getRuntime().exec("${node.absoluteFile} $asarFile extract ${asar.absoluteFile} ${res.absolutePath}${sep}app$sep")
- val css: File = File(FileSystemView.getFileSystemView().defaultDirectory.path + "${sep}discord.css")
- val errorReader: InputStreamReader = InputStreamReader(p.errorStream)
- errorReader.forEachLine(::println)
- println("Extracted!")
- println("Injecting script...")
- val index: File = File(res.absolutePath + "${sep}app${sep}index.js")
- val injection: File = File(res.absolutePath + "${sep}app${sep}CSSInjection.js")
- val fr: FileReader = FileReader(index)
- val br: BufferedReader = BufferedReader(fr)
- val js: String = br.readText()
- br.close()
- val fw: FileWriter = FileWriter(index)
- val bw: BufferedWriter = BufferedWriter(fw)
- bw.write(js.replace("mainWindow.webContents.on('dom-ready', function () {});",
- "mainWindow.webContents.on('dom-ready', function () {mainWindow.webContents.executeJavaScript(_fs2.default.readFileSync('${injection.absoluteFile.toString().replace("\\", "\\\\")}','utf-8'));});"))
- bw.flush()
- bw.close()
- if (!injection.exists()) {
- injection.createNewFile()
- }
- val fw1: FileWriter = FileWriter(injection)
- val bw1: BufferedWriter = BufferedWriter(fw1)
- bw1.write(cssInjection.replace("DOCUMENTPATH", css.absoluteFile.toString().replace("\\", "\\\\")))
- bw1.flush()
- bw1.close()
- println("Injection complete")
- if (!css.exists()) {
- println("Creating CSS file...")
- css.createNewFile()
- val fw2: FileWriter = FileWriter(css)
- val bw2: BufferedWriter = BufferedWriter(fw2)
- bw2.write("/* Put your CSS here */")
- bw2.flush()
- bw2.close()
- }
- println("Renaming original asar...")
- if (asar.absoluteFile.renameTo(File("${res.absoluteFile}${sep}original_app.asar"))) {
- println("Success!")
- } else {
- println("Failed, please rename the file at ${asar.absoluteFile} to something else")
- }
- println("All done! You can re-launch Discord and edit your CSS file at ${css.absoluteFile}")
- println("You may now close this Window.")
- } else {
- println("Please fully close Discord first!")
- }
- }
- }
- }
- }
- }
- fun isDiscordRunning(): Boolean {
- var pidInfo = ""
- val p = Runtime.getRuntime().exec(System.getenv("windir") + "\\system32\\" + "tasklist.exe")
- val input = BufferedReader(InputStreamReader(p.inputStream))
- input.forEachLine { pidInfo += it }
- input.close()
- if (pidInfo.contains("Discord")) {
- return true
- }
- return false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement