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);
        }
Beispiel #2
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 ;)
            }
        }