@@ -22,7 +22,9 @@ import com.demonwav.mcdev.platform.architectury.version.FabricVersion
2222import com.demonwav.mcdev.platform.forge.version.ForgeVersion
2323import com.demonwav.mcdev.util.License
2424import com.demonwav.mcdev.util.SemanticVersion
25+ import com.demonwav.mcdev.util.asyncIO
2526import com.demonwav.mcdev.util.modUpdateStep
27+ import com.intellij.openapi.diagnostic.Logger
2628import com.intellij.ui.CollectionComboBoxModel
2729import com.intellij.ui.EnumComboBoxModel
2830import java.awt.event.ActionListener
@@ -37,7 +39,7 @@ import kotlin.math.min
3739import kotlinx.coroutines.CoroutineScope
3840import kotlinx.coroutines.Dispatchers
3941import kotlinx.coroutines.Job
40- import kotlinx.coroutines.async
42+ import kotlinx.coroutines.awaitAll
4143import kotlinx.coroutines.coroutineScope
4244import kotlinx.coroutines.launch
4345import kotlinx.coroutines.swing.Swing
@@ -234,6 +236,7 @@ class ArchitecturyProjectSettingsWizard(private val creator: MinecraftProjectCre
234236 updateMcForm(data)
235237 }
236238 } catch (e: Exception ) {
239+ LOGGER .error(" Failed to update versions form" , e)
237240 error()
238241 }
239242
@@ -250,9 +253,9 @@ class ArchitecturyProjectSettingsWizard(private val creator: MinecraftProjectCre
250253 }
251254
252255 private suspend fun downloadVersions () = coroutineScope {
253- val fabricVersionJob = async( Dispatchers . IO ) { FabricVersion .downloadData() }
254- val forgeVersionJob = async( Dispatchers . IO ) { ForgeVersion .downloadData() }
255- val architecturyApiVersionJob = async( Dispatchers . IO ) { ArchitecturyVersion .downloadData() }
256+ val fabricVersionJob = asyncIO { FabricVersion .downloadData() }
257+ val forgeVersionJob = asyncIO { ForgeVersion .downloadData() }
258+ val architecturyApiVersionJob = asyncIO { ArchitecturyVersion .downloadData() }
256259
257260 versions = ArchitecturyVersions (
258261 fabricVersionJob.await() ? : return @coroutineScope,
@@ -262,29 +265,42 @@ class ArchitecturyProjectSettingsWizard(private val creator: MinecraftProjectCre
262265 }
263266
264267 private suspend fun updateForm (): Data ? = coroutineScope {
265- val vers = versions ? : return @coroutineScope null
268+ try {
269+ val vers = versions ? : return @coroutineScope null
266270
267- val selectedVersion = version ? : vers.forgeVersion.sortedMcVersions.firstOrNull() ? : return @coroutineScope null
271+ val selectedVersion = version ? : vers.forgeVersion.sortedMcVersions.firstOrNull()
272+ ? : return @coroutineScope null
268273
269- val fabricVersionsJob = async(Dispatchers .IO ) { vers.fabricVersion.getFabricVersions(selectedVersion) }
270- val forgeVersionsJob = async(Dispatchers .IO ) { vers.forgeVersion.getForgeVersions(selectedVersion) }
271- val fabricApiVersionsJob = async(Dispatchers .IO ) { vers.fabricVersion.getFabricApiVersions(selectedVersion) }
272- val architecturyApiVersionsJob = async(Dispatchers .IO ) {
273- vers.architecturyVersion.getArchitecturyVersions(
274- selectedVersion
275- )
276- }
274+ val fabricVersionsJob = asyncIO { vers.fabricVersion.getFabricVersions(selectedVersion) }
275+ val forgeVersionsJob = asyncIO { vers.forgeVersion.getForgeVersions(selectedVersion) }
276+ val fabricApiVersionsJob = asyncIO { vers.fabricVersion.getFabricApiVersions(selectedVersion) }
277+ val architecturyApiVersionsJob = asyncIO {
278+ vers.architecturyVersion.getArchitecturyVersions(selectedVersion)
279+ }
277280
278- val fabricVersions = fabricVersionsJob.await()
279- val forgeVersions = forgeVersionsJob.await()
280- val fabricApiVersions = fabricApiVersionsJob.await()
281- val architecturyApiVersions = architecturyApiVersionsJob.await()
281+ // awaitAll is better than calling .await() individually
282+ val (
283+ fabricVersions,
284+ forgeVersions,
285+ fabricApiVersions,
286+ architecturyApiVersions,
287+ ) = listOf (
288+ fabricVersionsJob,
289+ forgeVersionsJob,
290+ fabricApiVersionsJob,
291+ architecturyApiVersionsJob
292+ ).awaitAll()
282293
283- val data = Data (0 , fabricVersions, 0 , forgeVersions, 0 , fabricApiVersions, 0 , architecturyApiVersions, 0 )
294+ val data = Data (0 , fabricVersions, 0 , forgeVersions, 0 , fabricApiVersions, 0 , architecturyApiVersions, 0 )
284295
285- mcVersionUpdate(data)
296+ mcVersionUpdate(data)
286297
287- return @coroutineScope data
298+ return @coroutineScope data
299+ } catch (e: Exception ) {
300+ // log error manually - something is weird about intellij & coroutine exception handling
301+ LOGGER .error(" Error while updating Architectury form version fields" , e)
302+ return @coroutineScope null
303+ }
288304 }
289305
290306 private fun updateMcForm (data : Data ) {
@@ -293,9 +309,16 @@ class ArchitecturyProjectSettingsWizard(private val creator: MinecraftProjectCre
293309 minecraftVersionBox.removeActionListener(minecraftBoxActionListener)
294310 minecraftVersionBox.removeAllItems()
295311
296- minecraftVersionBox.model = CollectionComboBoxModel (
297- vers.forgeVersion.sortedMcVersions.filter { it >= SemanticVersion .release(1 , 16 ) }
298- )
312+ // make copy, so the next 2 operations don't mess up the map
313+ val mcVersions = vers.architecturyVersion.versions.keys.toCollection(LinkedHashSet ())
314+ mcVersions.retainAll(vers.forgeVersion.sortedMcVersions.toSet())
315+ // Fabric also targets preview versions which aren't semver
316+ // The other option would be to try to parse all of them and catching any exceptions
317+ // But exceptions are slow, so this should be more efficient
318+ val fabricMcVersions = vers.fabricVersion.versions.minecraftVersions.mapTo(HashSet ()) { it.name }
319+ mcVersions.retainAll { fabricMcVersions.contains(it.toString()) }
320+
321+ minecraftVersionBox.model = CollectionComboBoxModel (mcVersions.sortedDescending())
299322 minecraftVersionBox.selectedIndex = data.mcSelectedIndex
300323 minecraftVersionBox.addActionListener(minecraftBoxActionListener)
301324 }
@@ -311,4 +334,8 @@ class ArchitecturyProjectSettingsWizard(private val creator: MinecraftProjectCre
311334 val architecturyApiVersions : List <SemanticVersion >,
312335 val architecturyApiSelectedIndex : Int
313336 )
337+
338+ companion object {
339+ val LOGGER = Logger .getInstance(ArchitecturyProjectSettingsWizard ::class .java)
340+ }
314341}
0 commit comments