Exemplo n.º 1
0
        public void Generate(IInstancePool instancePool)
        {
            if (instancePool == null)
            {
                throw new ArgumentNullException(nameof(instancePool));
            }

            var listingTypes = new List <Type>();

            listingTypes.Add(typeof(Fractions));
            listingTypes.Add(typeof(BuildingRestrictions));
            listingTypes.Add(typeof(Fertilities));
            listingTypes.Add(typeof(WaterResources));
            listingTypes.Add(typeof(MountainResources));
            listingTypes.Add(typeof(ConstructionMaterials));
            listingTypes.Add(typeof(WarfareMaterials));
            listingTypes.Add(typeof(RawMaterials));
            listingTypes.Add(typeof(ConsumableGoods));
            listingTypes.Add(typeof(Buildings));
            listingTypes.Add(typeof(ProductionChains));
            listingTypes.Add(typeof(PopulationGroups));

            foreach (var listingType in listingTypes)
            {
                foreach (var field in listingType.GetFields(BindingFlags.Public | BindingFlags.Static))
                {
                    instancePool.Register((Persistable)field.GetValue(null));
                }
            }
        }
Exemplo n.º 2
0
 public static T GetFromPool <T>(this IInstancePool <T> pool, Func <T> funcCreate, Action <T> funcInit) where T : class
 {
     if (pool != null)
     {
         var list = pool.Pool;
         if (list.Count > 0)
         {
             var rv = list[list.Count - 1];
             list.RemoveAt(list.Count - 1);
             if (funcInit != null)
             {
                 funcInit(rv);
             }
             return(rv);
         }
         else
         {
             if (funcCreate != null)
             {
                 return(funcCreate());
             }
             else
             {
                 return((T)Activator.CreateInstance(typeof(T)));
             }
         }
     }
     return(default(T));
 }
Exemplo n.º 3
0
        public Repository(params Assembly[] searchAssemblies)
        {
            mInstancePool = new JsonReferenceResolver();

            mSchema     = PersistedNode.GetSchema(searchAssemblies ?? new Assembly[0]);
            mConverters = PersistedNode.GetConverters(searchAssemblies ?? new Assembly[0]);
        }
Exemplo n.º 4
0
 public static void ReturnToPool <T>(this IInstancePool <T> pool, T obj) where T : class
 {
     if (obj != null)
     {
         if (pool != null)
         {
             var list = pool.Pool;
             list.Add(obj);
         }
     }
 }
Exemplo n.º 5
0
        public static void ReturnToPool <T>(this IInstancePool <T> pool, T obj) where T : class
        {
#if ENABLE_OBJ_POOL
            if (obj != null)
            {
                if (pool != null)
                {
                    var list = pool.Pool;
                    list.Add(obj);
                }
            }
#endif
        }
Exemplo n.º 6
0
 public static T TryGetFromPool <T>(this IInstancePool <T> pool) where T : class
 {
     if (pool != null)
     {
         var list = pool.Pool;
         if (list.Count > 0)
         {
             var rv = list[list.Count - 1];
             list.RemoveAt(list.Count - 1);
             return(rv);
         }
     }
     return(default(T));
 }
Exemplo n.º 7
0
        private IList <ObjectStoreModel> GetAvailableObjectStores(
            [NotNull] IInstancePool instancePool,
            [NotNull] IDataProvider dataProvider)
        {
            if (instancePool == null)
            {
                throw new ArgumentNullException(nameof(instancePool));
            }

            if (dataProvider == null)
            {
                throw new ArgumentNullException(nameof(dataProvider));
            }

            const string profileDirectoryName = "Profiles";

            var profileDirectory = WorkingDirectory.GetChildStore(profileDirectoryName, true);
            var generators       = GetModelGenerators().AsList();
            var objectStores     = new List <ObjectStoreModel>();

            foreach (var generator in generators)
            {
                var profileModel = new PresetModel
                {
                    Key         = generator.Key,
                    DisplayName = generator.DisplayName,
                    Generator   = generator,
                    IconStore   = generator.Icons
                };

                instancePool.Register(profileModel);
                objectStores.Add(profileModel);
            }

            if (WorkingDirectory.HasChildStore(profileDirectoryName))
            {
                var profiles = profileDirectory.GetChildStoreKeys();
                var messages = new List <string>();

                foreach (var storageKey in profiles)
                {
                    var currentProfileDirectory = profileDirectory.GetChildStore(storageKey);
                    if (!currentProfileDirectory.ContainsKey("profile.json"))
                    {
                        continue;
                    }

                    try
                    {
                        const string dataDirectoryName  = "Data";
                        const string iconsDirectoryName = "Icons";

                        var profileModel = dataProvider.Read <ProfileModel>(currentProfileDirectory.Open("profile.json"));
                        if (profileModel == null)
                        {
                            continue;
                        }

                        profileModel.DataStore = currentProfileDirectory.GetChildStore(dataDirectoryName);
                        profileModel.IconStore = currentProfileDirectory.GetChildStore(iconsDirectoryName);

                        instancePool.Register(profileModel);
                        objectStores.Add(profileModel);
                    }
                    catch (Exception exception)
                    {
                        messages.Add($"{storageKey}: {exception.GetOriginalMessage()}");
                    }
                }

                if (messages.Any())
                {
                    Dialog.Error(string.Join("\r\n", messages)).Display();
                }
            }

            return(objectStores);
        }
Exemplo n.º 8
0
 public static T GetFromPool <T>(this IInstancePool <T> pool) where T : class
 {
     return(GetFromPool <T>(pool, null));
 }
Exemplo n.º 9
0
 public static T GetFromPool <T>(this IInstancePool <T> pool, Func <T> funcCreate) where T : class
 {
     return(GetFromPool <T>(pool, funcCreate, null));
 }
Exemplo n.º 10
0
        internal static Persistable Deserialize(StringKey key, [NotNull] Type type, [NotNull] TextReader source, [NotNull] IInstancePool references, [NotNull] IEnumerable <JsonConverter> converters)
        {
            if (key.IsEmpty)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (references == null)
            {
                throw new ArgumentNullException(nameof(references));
            }

            if (converters == null)
            {
                throw new ArgumentNullException(nameof(converters));
            }

            if (!type.IsSubclassOf(typeof(Persistable)))
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            var jsonSerializer = GetSerializer(type);

            jsonSerializer.ReferenceResolver = references as IReferenceResolver;
            jsonSerializer.Converters.AddRange(converters);

            var obj = (Persistable)jsonSerializer.Deserialize(new JsonTextReader(source), type);

            if (obj != null)
            {
                obj.Key = key.RawData;
            }

            return(obj);
        }
Exemplo n.º 11
0
 internal static T Deserialize <T>(StringKey key, [NotNull] TextReader source, [NotNull] IInstancePool references, [NotNull] IEnumerable <JsonConverter> converters) where T : Persistable
 {
     return((T)Deserialize(key, typeof(T), source, references, converters));
 }