Example #1
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="configSectionName">configuration section to use for processor lookup</param>
 /// <returns>KSP data as a string</returns>
 public static string Convert <T>(T obj, string configSectionName = null) where T : class, new()
 {
     return(Convert <T>(obj,
                        !string.IsNullOrEmpty(configSectionName)
                     ? ProcessorRegistry.Create(configSectionName).Create <T>()
                     : DefaultRegistry.Create <T>()));
 }
Example #2
0
        /*
         * /// <summary>
         * /// Loads a JSON formatting file into JObject
         * /// </summary>
         * /// <param name="path">full path of file location</param>
         * /// <returns>Json.NET JObject instance</returns>
         * public static JObject LoadJsonFile(string path)
         * {
         *  if (!File.Exists(path))
         *  {
         *      throw new KerbalDataException("The KSP Data file cannot be found. Path: " + path);
         *  }
         *
         *  JObject obj;
         *
         *  using (var file = File.Open(path, FileMode.Open))
         *  {
         *      try
         *      {
         *          obj = JObject.Parse((new StreamReader(file)).ReadToEnd());
         *      }
         *      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;
         * }
         *
         * /// <summary>
         * /// Loads a JSON formatting file into requested type
         * /// </summary>
         * /// <typeparam name="T">model to map to</typeparam>
         * /// <param name="path">full path of file location</param>
         * /// <returns>object instance</returns>
         * public static T LoadJsonFile<T>(string path)
         * {
         *  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 = JObject.Parse((new StreamReader(file)).ReadToEnd()).ToObject<T>();
         *      }
         *      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;
         * }*/



        /// <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 to use when serializing data</param>
        /// <param name="configSectionName">configuration section name to use for processor lookup</param>
        public static void SaveFile <T>(string path, T obj, string configSectionName = null) where T : class, new()
        {
            SaveFile <T>(path, obj,
                         !string.IsNullOrEmpty(configSectionName)
                            ? ProcessorRegistry.Create(configSectionName).Create <T>()
                            : DefaultRegistry.Create <T>());
        }
Example #3
0
        /// <summary>
        /// Handles initial setup and population of data properties.
        /// This is some of the "magic" that allows developers creating custom models to easily map
        /// StorableObjects instances with the correct repositories to custom properties without
        /// a ton of wire up code.
        /// </summary>
        private void Init()
        {
            procRegistry = ProcessorRegistry.Create(ApiConfig);
            repoFactory  = new RepoFactory(ApiConfig.Repositories, procRegistry);

            var managerType = this.GetType();
            var method      = repoFactory.GetType().GetMethod("Create");

            foreach (var prop in managerType.GetProperties().Where(p => p.PropertyType.FullName.Contains("StorableObjects")))
            {
                var modelType = prop.PropertyType.GetGenericArguments()[0];
                var repo      =
                    method.MakeGenericMethod(new[] { modelType }).Invoke(
                        repoFactory,
                        new object[] { null, prop.Name });
                //new object[] { new Dictionary<string, object>() { { "BaseUri", BaseUri } }, prop.Name });
#if NET45
                prop.SetMethod.Invoke(this, new object[] { Activator.CreateInstance(prop.PropertyType, new object[] { repo, this }) });
#elif NET40 || NET35 || MONO210
                prop.SetValue(this, Activator.CreateInstance(prop.PropertyType, new object[] { repo, this }), BindingFlags.SetProperty, null, null, null);
#endif
            }
        }
            public IKerbalDataRepo <T> GetRepo <T>(ProcessorRegistry registry, IDictionary <string, object> parameters = null) where T : class, IStorable, new()
            {
                var type = typeof(T);

                // Accept runtime provided values and overwrite any configured values of the same name (runtime values ALWAYS take precedence)
                var paramList = new Dictionary <string, object>(Parameters);

                if (parameters != null)
                {
                    foreach (var param in parameters)
                    {
                        paramList[param.Key] = param.Value;
                    }
                }

                try
                {
                    return(Activator.CreateInstance(Type.MakeGenericType(new Type[] { type }), new object[] { registry, paramList }) as IKerbalDataRepo <T>);
                }
                catch (Exception ex)
                {
                    throw new KerbalDataException("An error has occurred while creating an instance of the repository of type " + Type.FullName + " See inner exception for details", ex);
                }
            }
Example #5
0
 static KspData()
 {
     DefaultRegistry = ProcessorRegistry.Create(DefaultConfig);
 }
Example #6
0
 /// <summary>
 /// Loads and de-serializes a KSP data file 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="configSectionName">configuration section name to use for processor lookup</param>
 /// <returns>de-serialized object instance</returns>
 /// <example>
 /// <para>
 /// A null configuration causes the class to load embedded defaults. This is good for most common uses and is the recommended way for loading individual files. A type parameter must be supplied to the method so the correct type can be serialized. KerbalData is not yet able to determine the type from the data loaded. The developer must provide this information.
 /// </para>
 /// <code language="c#" region="en-us" title="Using Section Name Overload DEFAULT">
 /// var data = KspData.LoadKspFile&lt;SaveFile&gt;(@"C:\my\file\location\filename.ext");
 /// </code>
 /// <para>
 /// If a configuration section name is provided, KspData will load the named .NET configuration and use it's data in creating the <see cref="ProcessorRegistry"/> used in serialization. The following example is the same configuration that is used by default.
 /// </para>
 /// <code language="c#" region="en-us" title="Using Section Name Overload w/ Custom Section">
 /// var data = KspData.LoadKspFile&lt;SaveFile&gt;@"C:\my\file\location\filename.ext", "kerbalData");
 /// </code>
 /// </example>
 public static T LoadKspFile <T>(string path, string configSectionName = null) where T : class, new()
 {
     return(!string.IsNullOrEmpty(configSectionName)
                ? LoadKspFile <T>(path, ProcessorRegistry.Create(configSectionName).Create <T>())
                : LoadKspFile <T>(path, DefaultRegistry.Create <T>()));
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="RepoFactory"/> class.
        /// </summary>
        /// <param name="config">configuration data to use</param>
        /// <param name="registry">processor registry to pass to created repositories</param>
        public RepoFactory(RepositoriesConfig config, ProcessorRegistry registry)
        {
            this.registry = registry;

            InitConfig(config);
        }