11package ru.octol1ttle.flightassistant.screen
22
33import dev.isxander.yacl3.platform.YACLPlatform
4+ import kotlinx.serialization.SerializationException
45import kotlinx.serialization.json.Json
56import net.minecraft.ChatFormatting
67import net.minecraft.Util
@@ -12,6 +13,8 @@ import org.lwjgl.PointerBuffer
1213import org.lwjgl.system.MemoryUtil
1314import org.lwjgl.util.tinyfd.TinyFileDialogs
1415import ru.octol1ttle.flightassistant.FlightAssistant
16+ import ru.octol1ttle.flightassistant.api.flightplan.Waypoint
17+ import ru.octol1ttle.flightassistant.api.flightplan.legacy.FlightPlan
1518import ru.octol1ttle.flightassistant.api.util.extensions.drawMiddleAlignedString
1619import ru.octol1ttle.flightassistant.config.FAConfigScreen
1720import ru.octol1ttle.flightassistant.impl.computer.ComputerHost
@@ -30,6 +33,8 @@ class FlightAssistantSetupScreen : FABaseScreen(null, Component.translatable("me
3033 private lateinit var recallAlertButton: Button
3134
3235 private var saveLoadError = false
36+ private var loadedLegacy = false
37+ private var loadedWithProblems = false
3338
3439 private val hasShiftDown
3540// ? if >=1.21.9 {
@@ -92,6 +97,8 @@ class FlightAssistantSetupScreen : FABaseScreen(null, Component.translatable("me
9297 FlightAssistant .logger.info(" Saved flight plan to $path " )
9398 }
9499 saveLoadError = false
100+ loadedLegacy = false
101+ loadedWithProblems = false
95102 } catch (e: Throwable ) {
96103 FlightAssistant .logger.error(" Unable to save flight plan" , e)
97104 saveLoadError = true
@@ -109,8 +116,60 @@ class FlightAssistantSetupScreen : FABaseScreen(null, Component.translatable("me
109116
110117 val path = TinyFileDialogs .tinyfd_openFileDialog(title, PLANS_PATH .resolve(" flight_plan.json" ).toAbsolutePath().toString(), patterns, description, false )
111118 if (path != null ) {
112- FileReader (path).use { computers.plan.load(Json .decodeFromString(it.readText())) }
119+ loadedLegacy = false
120+ loadedWithProblems = false
121+
122+ try {
123+ FileReader (path).use { computers.plan.load(Json .decodeFromString(it.readText())) }
124+ } catch (e: SerializationException ) {
125+ FlightAssistant .logger.debug(" Unable to deserialize flight plan, attempting legacy conversion" , e)
126+
127+ // try loading legacy flight plan
128+ val legacy: FlightPlan = FileReader (path).use { Json .decodeFromString(it.readText()) }
129+ // convert it to new format
130+ val waypoints = mutableListOf<Waypoint >()
131+ var indexOffset = 0
132+ if (! legacy.departure.isDefault()) {
133+ waypoints.add(
134+ Waypoint (
135+ legacy.departure.coordinatesX,
136+ legacy.departure.coordinatesZ,
137+ legacy.departure.elevation,
138+ // legacy code uses "thrust %", new code uses speed. roughly convert thrust% into (firework) speed
139+ (legacy.departure.takeoffThrust * 30.0f ).toInt(),
140+ Waypoint .PhaseMarker .TAKEOFF
141+ )
142+ )
143+ indexOffset = 1
144+ }
145+ for ((coordinatesX, coordinatesZ, altitude, speed) in legacy.enroute) {
146+ waypoints.add(Waypoint (coordinatesX, coordinatesZ, altitude, speed))
147+ }
148+ if (! legacy.arrival.isDefault()) {
149+ waypoints.add(
150+ Waypoint (
151+ legacy.arrival.coordinatesX,
152+ legacy.arrival.coordinatesZ,
153+ legacy.arrival.elevation,
154+ // legacy code uses "thrust %", new code uses speed. roughly convert thrust% into (firework) speed
155+ (legacy.arrival.landingThrust * 30.0f ).toInt(),
156+ Waypoint .PhaseMarker .LANDING
157+ )
158+ )
159+ val sizeWithOffset =
160+ waypoints.size - indexOffset - 1 // takeoff & landing waypoints were not in the main waypoints list in the legacy format
161+ if (legacy.arrival.approachReEntryWaypointIndex in 0 .. < sizeWithOffset) {
162+ val index = legacy.arrival.approachReEntryWaypointIndex + indexOffset
163+ waypoints[index] = waypoints[index].copy(phaseMarker = Waypoint .PhaseMarker .APPROACH )
164+ }
165+ }
166+ computers.plan.load(waypoints)
167+ loadedLegacy = true
168+ }
113169 WaypointsScreen .reload(computers.plan)
170+ if (WaypointsScreen .problems.isNotEmpty()) {
171+ loadedWithProblems = true
172+ }
114173
115174 FlightAssistant .logger.info(" Loaded flight plan from $path " )
116175 }
@@ -148,7 +207,11 @@ class FlightAssistantSetupScreen : FABaseScreen(null, Component.translatable("me
148207 super .render(guiGraphics, mouseX, mouseY, delta)
149208
150209 if (saveLoadError) {
151- guiGraphics.drawMiddleAlignedString(Component .translatable(" menu.flightassistant.fms.error" ), this .centerX, this .centerY + 75 , ChatFormatting .RED .color!! , true )
210+ guiGraphics.drawMiddleAlignedString(Component .translatable(" menu.flightassistant.fms.error" ), this .centerX, this .centerY + 78 , ChatFormatting .RED .color!! , true )
211+ } else if (loadedWithProblems) {
212+ guiGraphics.drawMiddleAlignedString(Component .translatable(" menu.flightassistant.fms.loaded_with_problems" ), this .centerX, this .centerY + 78 , ChatFormatting .YELLOW .color!! , true )
213+ } else if (loadedLegacy) {
214+ guiGraphics.drawMiddleAlignedString(Component .translatable(" menu.flightassistant.fms.loaded_legacy" ), this .centerX, this .centerY + 78 , ChatFormatting .YELLOW .color!! , true )
152215 }
153216 }
154217
0 commit comments