@@ -8,7 +8,9 @@ import kotlinx.coroutines.async
88import kotlinx.coroutines.awaitAll
99import kotlinx.coroutines.runBlocking
1010import org.bundleproject.bundle.entities.Mod
11- import org.bundleproject.bundle.entities.platform.Platform
11+ import org.bundleproject.bundle.api.data.Platform
12+ import org.bundleproject.bundle.api.requests.BulkModRequest
13+ import org.bundleproject.bundle.entities.RemoteMod
1214import org.bundleproject.bundle.gui.LoadingGui
1315import org.bundleproject.bundle.gui.UpdateOverviewGui
1416import org.bundleproject.bundle.utils.*
@@ -36,18 +38,20 @@ class Bundle(private val gameDir: File, private val version: Version, modFolderN
3638
3739 suspend fun start () {
3840 try {
41+ println (" Starting Bundle..." )
3942 try { UIManager .setLookAndFeel(FlatLightLaf ()) }
4043 catch (e: Throwable ) { e.printStackTrace() }
4144
42- checkOutdated()
4345 val outdated = getOutdatedMods()
4446
4547 if (outdated.isEmpty()) return
4648
4749 val lock = ReentrantLock ()
4850 val condition = lock.newCondition()
4951 lock.withLock {
50- UpdateOverviewGui (this , outdated, condition)
52+ UpdateOverviewGui (this , outdated, condition).apply {
53+ isVisible = true
54+ }
5155 condition.await()
5256 }
5357
@@ -59,22 +63,34 @@ class Bundle(private val gameDir: File, private val version: Version, modFolderN
5963 /* *
6064 * Walks through the mods and looks through each file
6165 * and attempts to check if it's a valid mod
62- * and if it's out of date
66+ *
67+ * All the valid mods are collected into a list before
68+ * bulk-requesting the latest versions from the API.
69+ * They are matched into pairs and returned.
6370 *
6471 * @since 0.0.1
6572 */
66- private fun getOutdatedMods (): MutableList <Pair <Mod , Mod >> {
67- val outdated = mutableListOf<Pair < Mod , Mod > >()
73+ private suspend fun getOutdatedMods (): MutableList <Pair <Mod , RemoteMod >> {
74+ val localMods = mutableListOf<Mod >()
6875 for (mod in modsDir.walkTopDown()) {
76+ if (mod.isDirectory) continue
77+
6978 val localMod = getModInfo(mod) ? : continue
70- if (localMod.version != version) continue
79+ if (localMod.minecraftVersion != version) continue
80+
81+ localMods.add(localMod)
82+ }
7183
72- val remoteMod = runBlocking { Mod .fromUrl(localMod.latestUrl) }
84+ val request = BulkModRequest (localMods.map { it.makeRequest() })
85+ val response = request.request()
7386
74- // out of date
75- if (remoteMod.version.greaterThan(localMod.version)) {
76- outdated.add(Pair (localMod, remoteMod))
77- }
87+ val outdated = mutableListOf<Pair <Mod , RemoteMod >>()
88+ for (i in localMods.indices) {
89+ val local = localMods[i]
90+ val remote = local.applyData(response[i])
91+
92+ if (remote > local)
93+ outdated.add(local to remote)
7894 }
7995
8096 return outdated
@@ -138,12 +154,12 @@ class Bundle(private val gameDir: File, private val version: Version, modFolderN
138154
139155
140156 /* *
141- * Goes through a list of mods and linearly deletes
157+ * Goes through a list of mods and asynchronously deletes
142158 * and replaces it with its updated counterpart
143159 *
144160 * @since 0.0.2
145161 */
146- fun updateMods (mods : List <Pair <Mod , Mod >>) {
162+ fun updateMods (mods : List <Pair <Mod , RemoteMod >>) {
147163 launchCoroutine(" Mod Updater" ) {
148164 val loading = LoadingGui (mods.size)
149165 loading.isVisible = true
@@ -152,26 +168,12 @@ class Bundle(private val gameDir: File, private val version: Version, modFolderN
152168 val current = File (modsDir, local.fileName)
153169
154170 Files .delete(current.toPath())
155- runBlocking { URL (remote.latestDownloadUrl ) }
171+ runBlocking { URL (remote.downloadUrl ) }
156172 .download(File (modsDir, remote.fileName))
157173 loading.finish()
158174 }
159175 }.awaitAll()
160176 }
161177
162178 }
163-
164- /* *
165- * Checks if Bundle is outdated.
166- *
167- * @since 0.0.3
168- */
169- private suspend fun checkOutdated () {
170- val versions = JsonParser .parseString(http.get<String >(" $API /$API_VERSION /bundle/version" )).asJsonObject
171-
172- if (Version .valueOf(versions.get(" updater" ).asString).greaterThan(VERSION )) {
173- JOptionPane .showMessageDialog(null , " Bundle is outdated. Please re-run the installer to get the update!" , " Bundle" , JOptionPane .WARNING_MESSAGE )
174- }
175- }
176-
177179}
0 commit comments