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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
package at.hannibal2.skyhanni.data.repo
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigManager
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.LorenzUtils
import com.google.gson.JsonObject
import net.minecraft.client.Minecraft
import org.apache.commons.io.FileUtils
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.net.URL
import java.nio.charset.StandardCharsets
import java.util.concurrent.CompletableFuture
import java.util.concurrent.atomic.AtomicBoolean
class RepoManager(private val configLocation: File) {
private val gson get() = ConfigManager.gson
private var latestRepoCommit: String? = null
private val repoLocation: File = File(configLocation, "repo")
private var error = false
companion object {
val successfulConstants = mutableListOf<String>()
val unsuccessfulConstants = mutableListOf<String>()
private var lastConstant: String? = null
fun setlastConstant(constant: String) {
lastConstant?.let {
successfulConstants.add(it)
}
lastConstant = constant
}
}
fun loadRepoInformation() {
atomicShouldManuallyReload.set(true)
if (SkyHanniMod.feature.dev.repoAutoUpdate) {
fetchRepository(false).thenRun(this::reloadRepository)
} else {
reloadRepository()
}
}
private val atomicShouldManuallyReload = AtomicBoolean(false)//TODO remove the workaround
fun updateRepo() {
atomicShouldManuallyReload.set(true)
fetchRepository(true).thenRun { this.reloadRepository("Repo updated successful.") }
}
fun reloadLocalRepo() {
atomicShouldManuallyReload.set(true)
reloadRepository("Repo loaded from local files successful.")
}
private fun fetchRepository(command: Boolean): CompletableFuture<Boolean> {
return CompletableFuture.supplyAsync {
try {
val currentCommitJSON: JsonObject? = getJsonFromFile(File(configLocation, "currentCommit.json"))
latestRepoCommit = null
try {
InputStreamReader(URL(getCommitApiUrl()).openStream())
.use { inReader ->
val commits: JsonObject = gson.fromJson(inReader, JsonObject::class.java)
latestRepoCommit = commits["sha"].asString
}
} catch (e: Exception) {
e.printStackTrace()
}
if (latestRepoCommit == null || latestRepoCommit!!.isEmpty()) return@supplyAsync false
val file = File(configLocation, "repo")
if (file.exists() && currentCommitJSON != null && currentCommitJSON["sha"].asString == latestRepoCommit
) {
if (unsuccessfulConstants.isEmpty()) {
if (command) {
LorenzUtils.chat("§e[SkyHanni] §7The repo is already up to date!")
atomicShouldManuallyReload.set(false)
}
return@supplyAsync false
}
}
RepoUtils.recursiveDelete(repoLocation)
repoLocation.mkdirs()
val itemsZip = File(repoLocation, "sh-repo-main.zip")
try {
itemsZip.createNewFile()
} catch (e: IOException) {
return@supplyAsync false
}
val url = URL(getDownloadUrl(latestRepoCommit))
val urlConnection = url.openConnection()
urlConnection.connectTimeout = 15000
urlConnection.readTimeout = 30000
try {
urlConnection.getInputStream().use { `is` ->
FileUtils.copyInputStreamToFile(
`is`,
itemsZip
)
}
} catch (e: IOException) {
Exception(
"Failed to download SkyHanni Repo! Please report this issue on the discord!",
e
).printStackTrace()
if (command) {
LorenzUtils.error("An error occurred while trying to reload the repo! See logs for more info.")
}
return@supplyAsync false
}
RepoUtils.unzipIgnoreFirstFolder(
itemsZip.absolutePath,
repoLocation.absolutePath
)
if (currentCommitJSON == null || currentCommitJSON["sha"].asString != latestRepoCommit) {
val newCurrentCommitJSON = JsonObject()
newCurrentCommitJSON.addProperty("sha", latestRepoCommit)
try {
writeJson(newCurrentCommitJSON, File(configLocation, "currentCommit.json"))
} catch (ignored: IOException) {
}
}
} catch (e: Exception) {
e.printStackTrace()
}
true
}
}
private fun reloadRepository(answerMessage: String = ""): CompletableFuture<Void?> {
val comp = CompletableFuture<Void?>()
if (!atomicShouldManuallyReload.get()) return comp
ErrorManager.resetCache()
Minecraft.getMinecraft().addScheduledTask {
error = false
successfulConstants.clear()
unsuccessfulConstants.clear()
lastConstant = null
RepositoryReloadEvent(repoLocation, gson).postAndCatchAndBlock(ignoreErrorCache = true) {
error = true
lastConstant?.let {
unsuccessfulConstants.add(it)
}
lastConstant = null
}
comp.complete(null)
if (answerMessage.isNotEmpty() && !error) {
LorenzUtils.chat("§e[SkyHanni] §a$answerMessage")
}
if (error) {
LorenzUtils.clickableChat(
"§e[SkyHanni] Error with the repo detected, try /shupdaterepo to fix it!",
"shupdaterepo"
)
if (unsuccessfulConstants.isEmpty()) {
unsuccessfulConstants.add("All Constants")
}
}
}
return comp
}
fun displayRepoStatus(joinEvent: Boolean) {
if (joinEvent) {
if (unsuccessfulConstants.isNotEmpty()) {
LorenzUtils.chat("§c[SkyHanni] §7Repo Issue! Some features may not work. Please report this error on the Discord!")
LorenzUtils.chat("§7Repo Auto Update Value: §c${SkyHanniMod.feature.dev.repoAutoUpdate}")
LorenzUtils.chat("§7If you have Repo Auto Update turned off, please try turning that on.\n§cUnsuccessful Constants §7(${unsuccessfulConstants.size}):")
for (constant in unsuccessfulConstants) {
LorenzUtils.chat(" §e- §7$constant")
}
}
return
}
if (unsuccessfulConstants.isEmpty() && successfulConstants.isNotEmpty()) {
LorenzUtils.chat("§a[SkyHanni] Repo working fine!")
return
}
if (successfulConstants.isNotEmpty()) LorenzUtils.chat("§a[SkyHanni] Successful Constants §7(${successfulConstants.size}):")
for (constant in successfulConstants) {
LorenzUtils.chat(" §a- §7$constant")
}
LorenzUtils.chat("§c[SkyHanni] Unsuccessful Constants §7(${unsuccessfulConstants.size}):")
for (constant in unsuccessfulConstants) {
LorenzUtils.chat(" §e- §7$constant")
}
}
/**
* Parses a file in to a JsonObject.
*/
private fun getJsonFromFile(file: File?): JsonObject? {
try {
BufferedReader(
InputStreamReader(
FileInputStream(file),
StandardCharsets.UTF_8
)
).use { reader ->
return gson.fromJson(reader, JsonObject::class.java)
}
} catch (e: java.lang.Exception) {
return null
}
}
private fun getCommitApiUrl(): String {
val repoUser = "hannibal002"
val repoName = "SkyHanni-REPO"
val repoBranch = "main"
return String.format("https://api.github.com/repos/%s/%s/commits/%s", repoUser, repoName, repoBranch)
}
private fun getDownloadUrl(commitId: String?): String {
val repoUser = "hannibal002"
val repoName = "SkyHanni-REPO"
return String.format("https://github.com/%s/%s/archive/%s.zip", repoUser, repoName, commitId)
}
@Throws(IOException::class)
fun writeJson(json: JsonObject?, file: File) {
file.createNewFile()
BufferedWriter(
OutputStreamWriter(
FileOutputStream(file),
StandardCharsets.UTF_8
)
).use { writer -> writer.write(gson.toJson(json)) }
}
}
|