Example #1
0
        /// <summary>
        /// Loads and de-serializes a KSP data files into the requested type
        /// </summary>
        /// <typeparam name="T">model type to de-serialize to</typeparam>
        /// <param name="path">full path of file location</param>
        /// <param name="processor">processor instance to use for de-serialization</param>
        /// <returns>de-serialized object instance</returns>
        /// <example>
        /// <para>To load a file a path and a <see cref="KspProcessor{T}"/> instance are required. A <see cref="KspProcessor{T}"/> instance can be acquired from the <see cref="ProcessorRegistry"/> class.</para>
        /// <code language="c#" region="en-us" title="Using Section Name Overload w/ Custom Section">
        /// // Creating a registry using the config named "kerbalData" see the documentation on the ProcessorRegistry for more details on the Create(string) and Create(ApiConfig) methods.
        /// var registry = ProcessorRegistry.Create("kerbalData");
        ///
        /// // When the file is loaded the Create method is called on the registry instance and the desired serialization type is provided to the registry to acquire the right instance.
        /// var data = KspData.LoadKspFile&lt;SaveFile&gt;(@"C:\my\file\location\filename.sfs", registry.Create&lt;SaveFile&gt;());
        /// </code>
        /// </example>
        public static T LoadKspFile <T>(string path, KspProcessor <T> processor) where T : class, new()
        {
            if (!File.Exists(path))
            {
                throw new KerbalDataException("The KSP Data file cannot be found. Path: " + path);
            }

            T obj;

            using (var file = File.Open(path, FileMode.Open))
            {
                try
                {
                    obj = processor.Process(file);

                    var storable = obj as StorableObject;
                    if (storable != null)
                    {
                        storable.Uri = path;
                    }
                }
                catch (Exception ex)
                {
                    throw new KerbalDataException("An error has occurred while attempting to load the KSP Data file. See inner exception for details. Path: " + path, ex);
                }

                file.Close();
            }

            return(obj);
        }
Example #2
0
        /// <summary>
        /// Converts and KSP formatting string into a de-serialized object
        /// </summary>
        /// <typeparam name="T">model type to convert to</typeparam>
        /// <param name="data">data to parse</param>
        /// <param name="processor">processor to use for de-serialization</param>
        /// <returns>de-serialized object instance</returns>
        public static T Convert <T>(string data, KspProcessor <T> processor) where T : class, new()
        {
            T obj;

            try
            {
                obj = processor.Process(new MemoryStream(Encoding.UTF8.GetBytes(data)));
            }
            catch (Exception ex)
            {
                throw new KerbalDataException("An error has occurred while attempting to load the KSP Data file. See inner exception for details.", ex);
            }

            return(obj);
        }
Example #3
0
        /// <summary>
        /// Converts an object instance to a KSP data string
        /// </summary>
        /// <typeparam name="T">model type to convert to</typeparam>
        /// <param name="obj">object data</param>
        /// <param name="processor">processor to use for de-serialization</param>
        /// <returns>KSP data as a string</returns>
        public static string Convert <T>(T obj, KspProcessor <T> processor) where T : class, new()
        {
            string kspData;

            try
            {
                var stream = processor.Process(obj);
                kspData = (new StreamReader(stream)).ReadToEnd();
            }
            catch (Exception ex)
            {
                throw new KerbalDataException("An error has occurred while attempting to load or read the KSP Data file. See inner exception for details.", ex);
            }

            return(kspData);
        }
Example #4
0
 /// <summary>
 /// Saves provided data to KSP data format at the provided path
 /// </summary>
 /// <typeparam name="T">model type to save</typeparam>
 /// <param name="path">path to save file to</param>
 /// <param name="obj">object data to use</param>
 /// <param name="processor">processor to use for serialization</param>
 public static void SaveFile <T>(string path, T obj, KspProcessor <T> processor) where T : class, new()
 {
     Convert(obj, processor).WriteToFile(path);
 }