Skip to content

QuickStart : Editor ship autosave can result in corrupt ship save #15

@gotmachine

Description

@gotmachine

This :

IEnumerator autoSaveShip() {
QDebug.Log ("autoSaveShip: start", "QPersistent");
while (HighLogic.LoadedSceneIsEditor && QSettings.Instance.enableEditorAutoSaveShip) {
QDebug.Log("autoSaveShip: before WaitForSeconds(" + QSettings.Instance.editorTimeToSave + "", "QPersistent");
yield return new WaitForSeconds (QSettings.Instance.editorTimeToSave);
List<Part> parts = EditorLogic.fetch.ship != null ? EditorLogic.fetch.ship.Parts : new List<Part>();
if (parts.Count > 0)
{
QDebug.Log("autoSaveShip: before saveShip", "QPersistent");
ShipConstruction.SaveShip(shipFilename);
QDebug.Log("autoSaveShip: after saveShip", "QPersistent");
}
}
QDebug.Log ("autoSaveShip: end", "QPersistent");
}

I'm not sure exactly why, by the call to ShipConstruction.SaveShip() occasionally result in a borked ship save. My guess is that yield return new WaitForSeconds (QSettings.Instance.editorTimeToSave); result in the method being called at a time where the ship / editor isn't a steady state.

Instead of saving the current ship instance, maybe you could get the last backup. Try replacing :

List<Part> parts = EditorLogic.fetch.ship != null ? EditorLogic.fetch.ship.Parts : new List<Part>();

if (parts.Count > 0)
{
  QDebug.Log("autoSaveShip: before saveShip", "QPersistent");
  ShipConstruction.SaveShip(shipFilename);
  QDebug.Log("autoSaveShip: after saveShip", "QPersistent");
}

by

if (ShipConstruction.backups.Count > 0)
{
	ShipConstruction.backups[ShipConstruction.backups.Count - 1].Save(shipFilename);
}

Also, instead of using a coroutine, you could subscribe to GameEvents.onEditorSetBackup.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions