diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/json/JsonPatch.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/json/JsonPatch.kt index 68736c6b..7db33dfb 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/json/JsonPatch.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/json/JsonPatch.kt @@ -5,11 +5,13 @@ import com.google.gson.JsonElement import com.google.gson.JsonNull import com.google.gson.JsonObject import com.google.gson.JsonSyntaxException +import com.google.gson.stream.JsonReader import kotlinx.coroutines.future.await import org.apache.logging.log4j.LogManager import ru.dbotthepony.kommons.gson.get import ru.dbotthepony.kstarbound.IStarboundFile import ru.dbotthepony.kstarbound.Starbound +import java.util.concurrent.CompletableFuture enum class JsonPatch(val key: String) { TEST("test") { @@ -165,5 +167,25 @@ enum class JsonPatch(val key: String) { return base } + + suspend fun applyAsync(reader: CompletableFuture, source: Collection?): JsonElement { + source ?: return Starbound.ELEMENTS_ADAPTER.read(reader.await()) + val loaded = source.map { it.asyncJsonReader() } + var base = Starbound.ELEMENTS_ADAPTER.read(reader.await()) + val itr = source.iterator() + + for (patch in loaded) { + val file = itr.next() + val read = Starbound.ELEMENTS_ADAPTER.read(patch.await()) + + if (read !is JsonArray) { + LOGGER.error("$patch root element is not an array") + } else { + base = apply(base, read, file) + } + } + + return base + } } }