Example #1
0
        public bool ReadJSON()
        {
            if (File.Exists(FilePath))
            {
                BuildingConfiguration.Clear();
                DigLocations.Clear();

                try {
                    using (StreamReader reader = File.OpenText(FilePath))
                        using (JsonTextReader jsonReader = new JsonTextReader(reader)) {
                            JObject rootObject = (JObject)JToken.ReadFrom(jsonReader).Root;

                            JToken friendlyNameToken = rootObject.SelectToken("friendlyname");
                            JToken buildingsToken    = rootObject.SelectToken("buildings");
                            JToken digCommandsToken  = rootObject.SelectToken("digcommands");

                            if (friendlyNameToken != null && friendlyNameToken.Type == JTokenType.String)
                            {
                                FriendlyName = friendlyNameToken.Value <string>();
                            }

                            if (buildingsToken != null)
                            {
                                JArray buildingTokens = buildingsToken.Value <JArray>();

                                if (buildingTokens != null)
                                {
                                    foreach (JToken buildingToken in buildingTokens)
                                    {
                                        BuildingConfig buildingConfig = new BuildingConfig();

                                        if (buildingConfig.ReadJSON((JObject)buildingToken))
                                        {
                                            BuildingConfiguration.Add(buildingConfig);
                                        }

                                        else if (buildingConfig.ReadJSONLegacy((JObject)buildingToken))
                                        {
                                            BuildingConfiguration.Add(buildingConfig);
                                        }
                                    }
                                }
                            }

                            if (digCommandsToken != null)
                            {
                                JArray digCommandTokens = digCommandsToken.Value <JArray>();

                                if (digCommandTokens != null)
                                {
                                    foreach (JToken digCommandToken in digCommandTokens)
                                    {
                                        JToken xToken = digCommandToken.SelectToken("x");
                                        JToken yToken = digCommandToken.SelectToken("y");

                                        if (xToken != null && xToken.Type == JTokenType.Integer && yToken != null & yToken.Type == JTokenType.Integer)
                                        {
                                            DigLocations.Add(new Vector2I(xToken.Value <int>(), yToken.Value <int>()));
                                        }
                                    }
                                }
                            }

                            return(true);
                        }
                }

                catch (System.Exception exception) {
                    Debug.LogError("Error when loading blueprint: " + FilePath + ",\n" + nameof(exception) + ":" + exception.Message);
                }
            }

            return(false);
        }