/
MainApplication.kt
91 lines (80 loc) · 2.91 KB
/
MainApplication.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Condor2 Revive Helper, a program which allows Condor2 to be launched more easily via Revive.
* Copyright (C) 2019 Sam McCormack
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.cabbagedevelopment.condor2helper
import tornadofx.*
/**
* Entry point of the application.
*/
fun main(args: Array<String>) {
// If program run with args, launch Condor2.
if (args.isNotEmpty()) {
return CondorLauncher(Prefs(), args.first()).launch()
}
// If program run normally, open window to configure settings.
launch<MainApplication>(args)
}
/**
* The main application, which is used to configure the program.
* This class is not used when simply launching Condor2.
*/
class MainApplication : App(MainView::class) {
val injectorName = "ReviveInjector.exe"
val prefs = Prefs()
val view = find<MainView>()
init {
// If the required paths are already stored in the preferences,
// set the text fields to those values. Otherwise, find the
// required paths.
prefs.condorPath?.also { view.condorText.text = it } ?: findCondor()
prefs.revivePath?.also { view.reviveText.text = it } ?: findRevive()
}
/**
* Uses a FileLocator instance to locate a file. When a file path is returned,
* onResult is called.
*/
private inline fun findWith(locator: FileLocator, crossinline onResult: (String) -> Unit) {
runAsync {
locator.locate()
} ui { path ->
path?.let { onResult(it) }
}
}
private fun findCondor() {
findWith(CondorLocator()) { path ->
prefs.condorPath = path
view.condorText.text = path
}
}
private fun findRevive() {
findWith(ReviveLocator()) { path ->
// We need the path to the Revive folder inside the Revive installation directory.
val revivePath = "$path\\Revive"
prefs.revivePath = revivePath
prefs.reviveInjectorPath = "$revivePath\\$injectorName"
view.reviveText.text = revivePath
}
}
/**
* Saves the contents of the text fields to the preferences.
*/
fun save() {
prefs.condorPath = view.condorText.text
prefs.revivePath = view.reviveText.text
prefs.reviveInjectorPath = "${prefs.revivePath}\\x64\\$injectorName"
}
}