Load, Save, Import, Export KerbalData - KSP Data Unlocked!

This topic contains the following sections.

Loading Saving Importing and Exporting

Loading and Saving Data

The quickest and easiest way to load data is to point KerbalData at a KSP installation. When created KerbalData only scans the install path for valid files to create name lists. Data is only loaded once a particular item is called for. Using KerbalData each object collection is accessible by a different property

                    var kd = KerbalData.Create("C:\games\ksp_win");

var save = kd.Saves["mysave"];
var scenario = kd.Scenarios["myscenario"];
var trainingScenario = kd.TrainingScenarios["mytrainingscenario"];
var vabCraft = kd.CraftInVab["myvabcraft"];
var sphCraft = kd.CraftInSph["mysphcraft"];
var part = kd.Parts["mypart"];
var settings = kd.KspSettings["mysetting"];

// To access craft for a particular save, access the properties under that save instance 
var saveVabCraft = save.CraftInVab["myvabcraft"];
var saveSphCraft = save.CraftInSph["mysphcraft"];

Saving data loaded from a KerbalData install only requires a call to the Save() method found on every storable data object (Saves, Craft, Part and Settings). By default data is backed up to another file before being saved.

save.Save();
scenario.Save();
trainingScenario.Save();
vabCraft.Save();
sphCraft.Save();
part.Save();
settings.Save();
saveVabCraft.Save();
saveSphCraft.Save();

// The save method accepts 2 parameters the first is a name, the second disables/enables backup.  
// If a new name is provided the data and changes are copied to the new name, the old data remains
save.Save("mynewsave");

// Backup can be turned off by passing a false as the second parameter. If you don't wish to change the name, null must be passed.
save.Save(null, false);

To load single files the KspData class provides base methods for loading and saving KSP data. The models loaded from KSP data are the very same as provided by KerbalData. Note that KspData requires you to tell the method what type you are loading so that the correct serilaizers are loaded.

                    var save = KspData.LoadKspFile<SaveFile>(@"C:\games\ksp_win\saves\mysave\persistent.sfs");

// Data in the save is accessed the same way as if it was loaded from KerbalData. It can also be saved  
// using the same methods.

save.Save(); // Saves any changes 

// Saves to a new file. Note: unlike when loaded through KerbalData, directories are not automatically created.
save.Save(@"C:\games\ksp_win\saves\mysave\newpersistent.sfs");
Importing and Exporting Data

Importing an external file into a KSP installation requires an active KerbalData instance and a file loaded using KspData.

                    var kd = KerbalData.Create("C:\games\ksp_win");
var saveToImport = KspData.LoadKspFile<SaveFile>(@"C:\downloads\customsave.sfs");

// Add the loaded save into the Saves handler in KerbalData
kd.Saves.Add("MyImportedSave", saveToImport);

// The import file is now tied to KerbalData, Calling save will create a new directory 
// based on the name provided and save the file as persistent.sfs.
saveToImport.Save();

// The same process applies to any type of file 
// Note: Scenarios are treated as Save files for loading/serialization 
var scenarioToImport = KspData.LoadKspFile<SaveFile>(@"C:\downloads\customscenario.sfs");
kd.Scenarios.Add("MyImportedScenario", scenarioToImport);
scenarioToImport.Save();      

var trainingToImport = KspData.LoadKspFile<SaveFile>(@"C:\downloads\customtraining.sfs");
kd.TrainingScenario.Add("MyImportedTraining", trainingTooImport);
trainingToImport.Save(); 

var vabCraftToImport = KspData.LoadKspFile<CraftFile>(@"C:\downloads\customvabCraft.sfs");
kd.CraftInVab.Add("MyImportedCraft", vabCraftToImport);
vabCraftToImport.Save(); 

var sphCraftToImport = KspData.LoadKspFile<CraftFile>(@"C:\downloads\customsphCraft.sfs");
kd.CraftInSph.Add("MyImportedCraft", sphCraftToImport);
sphCraftToImport.Save(); 

var partToImport = KspData.LoadKspFile<PartFile>(@"C:\downloads\custompart.sfs");
kd.Parts.Add("MyImportedPart", partToImport);
partToImport.Save(); 

var configToImport = KspData.LoadKspFile<ConfigFile>(@"C:\downloads\customconfig.sfs");
kd.Scenarios.Add("MyImportedConfig", configToImport);
configToImport.Save();

To export data from a KSP install use the KspData class to save data from a KerbalData object.

                    var kd = KerbalData.Create("C:\games\ksp_win");
var craft = KerbalData.CraftInVab["mycraft"];

// Note: There is no backup when using SaveFile
KspData.SaveFile(@"C:\games\myexports\myexported.craft");