33package wayzer.ext
44
55import arc.struct.StringMap
6- import arc.util.Http
76import arc.util.Strings
87import arc.util.serialization.JsonReader
98import arc.util.serialization.JsonValue
@@ -17,8 +16,10 @@ import wayzer.BaseMapInfo
1716import wayzer.MapInfo
1817import wayzer.MapProvider
1918import wayzer.MapRegistry
19+ import java.io.ByteArrayInputStream
2020import java.net.URL
2121import java.net.URLEncoder
22+ import java.nio.charset.Charset
2223import java.time.Duration
2324import java.util.zip.InflaterInputStream
2425import mindustry.maps.Map as MdtMap
@@ -44,24 +45,23 @@ fun JsonValue.toStringMap() = StringMap().apply {
4445 } while (node != null )
4546}
4647
47- suspend fun httpGet (url : String ) = withContext(Dispatchers .IO ) {
48- runInterruptible {
49- var result: Result <JsonValue > = Result .failure(IllegalStateException (" result not set" ))
50- Http .get(url)
51- .error { result = Result .failure(it) }
52- .block { result = Result .success(JsonReader ().parse(it.resultAsString)) }
53- result.getOrThrow()
48+ suspend fun httpGet (url : String , retry : Int = 3) = withContext(Dispatchers .IO ) {
49+ var result: Result <ByteArray > = Result .failure(IllegalStateException (" result not set" ))
50+ repeat(retry + 1 ) {
51+ result = kotlin.runCatching {
52+ val stream = URL (url).openConnection()
53+ .apply { readTimeout = 1_000 }
54+ .getInputStream()
55+ runInterruptible { stream.readBytes() }
56+ }.onSuccess { return @withContext it }
5457 }
58+ result.getOrThrow()
5559}
5660
5761fun loadMap (map : Map , hash : String ) {
58- val url = URL (" $webRoot /maps/$hash /downloadServer?token=$token " )
59- url.openConnection()
60- .apply { readTimeout = 10_000 }
61- .getInputStream().use { stream ->
62- @Suppress(" INACCESSIBLE_TYPE" )
63- SaveIO .load(InflaterInputStream (stream), world.filterContext(map))
64- }
62+ val bs = runBlocking { httpGet(" $webRoot /maps/$hash /downloadServer?token=$token " , retry = 3 ) }
63+ @Suppress(" INACCESSIBLE_TYPE" )
64+ SaveIO .load(InflaterInputStream (ByteArrayInputStream (bs)), world.filterContext(map))
6565}
6666
6767fun newMapInfo (id : Int , hash : String , tags : StringMap , mode : String ): BaseMapInfo {
@@ -87,6 +87,7 @@ MapRegistry.register(this, object : MapProvider() {
8787 }
8888 searchCache.getIfPresent(mappedSearch)?.let { return it }
8989 val maps = httpGet(" $webRoot /maps/list?prePage=100&search=${URLEncoder .encode(mappedSearch, " utf-8" )} " )
90+ .let { JsonReader ().parse(it.toString(Charset .defaultCharset())) }
9091 .map {
9192 val id = it.getInt(" id" )
9293 val hash = it.getString(" latest" )
@@ -103,6 +104,7 @@ MapRegistry.register(this, object : MapProvider() {
103104 return null
104105 }
105106 val info = httpGet(" $webRoot /maps/thread/$id /latest" )
107+ .let { JsonReader ().parse(it.toString(Charset .defaultCharset())) }
106108 val hash = info.getString(" hash" )
107109 val tags = info.get(" tags" ).toStringMap()
108110 return newMapInfo(id, hash, tags, info.getString(" mode" , " unknown" )).run {
0 commit comments