示例#1
0
        public static StaticDataStore GetStaticData(string dataDir)
        {
            StaticDataStore newStore    = new StaticDataStore();//.Clone();
            string          curFileName = "";
            JObject         obj;

            //try
            {
                string dataDirectory = Path.Combine(Path.Combine(SerializationManager.GetWorkingDirectory(), DataDirectory), dataDir);

                // we start by looking for a version file, no version file, no load.

                // now we can move on to looking for json files:
                string[] jsonfiles  = Directory.GetFiles(dataDirectory, "*.json");
                string[] hjsonfiles = Directory.GetFiles(dataDirectory, "*.hjson");

                if (jsonfiles.GetLength(0) < 1 && hjsonfiles.GetLength(0) < 1)
                {
                    return(null);
                }

                foreach (string file in jsonfiles)
                {
                    curFileName = file;
                    obj         = Load(file);
                    StoreObject(obj, newStore);
                }
                foreach (string file in hjsonfiles)
                {
                    curFileName = file;
                    obj         = Load(file);
                    StoreObject(obj, newStore);
                }



                //Test the components formula for parsability
                List <Guid> badComponents = new List <Guid>();
                foreach (var componentKVP in newStore.ComponentTemplates)
                {
                    if (!ComponentParseCheck.IsParseable(componentKVP.Value, out var errors))
                    {
                        badComponents.Add(componentKVP.Key);
                        foreach (var error in errors)
                        {
                            StaticRefLib.EventLog.AddEvent(Event.NewComponentParseError(componentKVP.Value.Name, error));
                        }
                    }
                }

                foreach (var componentID in badComponents)
                {
                    newStore.ComponentTemplates.Remove(componentID);
                }
            }


            return(newStore);
        }
示例#2
0
        public static void LoadData(string dataDir, Game game)
        {
            StaticDataStore newStore = game.StaticData.Clone();

            try
            {
                string dataDirectory = Path.Combine(Path.Combine(SerializationManager.GetWorkingDirectory(), DataDirectory), dataDir);

                // we start by looking for a version file, no version file, no load.
                DataVersionInfo dataVInfo;
                if (CheckDataDirectoryVersion(dataDirectory, game.StaticData, out dataVInfo) == false)
                {
                    throw new StaticDataLoadException("Static Data is explicitly incompatible with currently loaded data.");
                }

                // now we can move on to looking for json files:
                string[] files = Directory.GetFiles(dataDirectory, "*.json");

                if (files.GetLength(0) < 1)
                {
                    return;
                }

                foreach (string file in files)
                {
                    JObject obj = Load(file);
                    StoreObject(obj, newStore);
                }

                if (!newStore.LoadedDataSets.Contains(dataVInfo))
                {
                    newStore.LoadedDataSets.Add(dataVInfo);
                }
                newStore.SetStorageTypeMap();
                game.StaticData = newStore;
            }
            catch (Exception e)
            {
                if (e.GetType() == typeof(JsonSerializationException) || e.GetType() == typeof(JsonReaderException))
                {
                    throw new StaticDataLoadException("Bad Json provided in directory: " + dataDir, e);
                }


                throw;  // rethrow exception if not known ;)
            }
        }
示例#3
0
        /// <summary>
        /// Exports the provided static data into the specified file.
        /// </summary>
        public static void ExportStaticData(object staticData, string file)
        {
            var data = new DataExportContainer {
                Data = staticData, Type = StaticDataStore.GetTypeString(staticData.GetType())
            };

            string workingDir = Path.Combine(SerializationManager.GetWorkingDirectory(), DataDirectory);

            file = Path.Combine(workingDir, file);

            if (string.IsNullOrEmpty(data.Type) == false)
            {
                using (var sw = new StreamWriter(file))
                    using (JsonWriter writer = new JsonTextWriter(sw))
                    {
                        Serializer.Serialize(writer, data);
                    }
            }
        }
示例#4
0
        public static List <DataVersionInfo> AvailableData()
        {
            string dataDirectory = Path.Combine(SerializationManager.GetWorkingDirectory(), DataDirectory);
            var    availableDirs = new List <string>(Directory.GetDirectories(dataDirectory));
            var    availableData = new List <DataVersionInfo>();

            foreach (string possibleMod in availableDirs)
            {
                DataVersionInfo dvi;
                if (CheckDataDirectoryVersion(possibleMod, new StaticDataStore(), out dvi))
                {
                    if (availableData.Contains(dvi))
                    {
                        throw new AmbiguousMatchException($"Found two mods in different directories with the same version info: {dvi.FullVersion}");
                    }
                    availableData.Add(dvi);
                }
            }

            return(availableData);
        }
示例#5
0
        public static void LoadData(string dataDir, Game game)
        {
            StaticDataStore newStore    = game.StaticData;//.Clone();
            string          curFileName = "";
            JObject         obj;

            try
            {
                string dataDirectory = Path.Combine(Path.Combine(SerializationManager.GetWorkingDirectory(), DataDirectory), dataDir);

                // we start by looking for a version file, no version file, no load.
                DataVersionInfo dataVInfo;
                if (CheckDataDirectoryVersion(dataDirectory, game.StaticData, out dataVInfo) == false)
                {
                    throw new StaticDataLoadException("Static Data is explicitly incompatible with currently loaded data.");
                }

                // now we can move on to looking for json files:
                string[] files = Directory.GetFiles(dataDirectory, "*.json");

                if (files.GetLength(0) < 1)
                {
                    return;
                }

                foreach (string file in files)
                {
                    curFileName = file;
                    obj         = Load(file);
                    StoreObject(obj, newStore);
                }

                if (!newStore.LoadedDataSets.Contains(dataVInfo))
                {
                    newStore.LoadedDataSets.Add(dataVInfo);
                }
                newStore.SetStorageTypeMap();


                //Test the components formula for parsability
                List <Guid> badComponents = new List <Guid>();
                foreach (var componentKVP in newStore.ComponentTemplates)
                {
                    if (!ComponentParseCheck.IsParseable(componentKVP.Value, out var errors))
                    {
                        badComponents.Add(componentKVP.Key);
                        foreach (var error in errors)
                        {
                            StaticRefLib.EventLog.AddEvent(Event.NewComponentParseError(componentKVP.Value.Name, error));
                        }
                    }
                }

                foreach (var componentID in badComponents)
                {
                    newStore.ComponentTemplates.Remove(componentID);
                }
            }

            catch (Exception e)
            {
                if (e.GetType() == typeof(JsonSerializationException) || e.GetType() == typeof(JsonReaderException))
                {
                    throw new StaticDataLoadException("Bad Json provided in directory: " + curFileName, e);
                }


                throw;  // rethrow exception if not known ;)
            }
        }