Esempio n. 1
0
        public bool ReadBinary()
        {
            if (File.Exists(FilePath))
            {
                BuildingConfiguration.Clear();
                DigLocations.Clear();

                try {
                    using (BinaryReader reader = new BinaryReader(File.Open(FilePath, FileMode.Open))) {
                        FriendlyName = reader.ReadString();

                        int buildingCount = reader.ReadInt32();
                        for (int i = 0; i < buildingCount; ++i)
                        {
                            BuildingConfig buildingConfig = new BuildingConfig();
                            if (!buildingConfig.ReadBinary(reader))
                            {
                                return(false);
                            }

                            BuildingConfiguration.Add(buildingConfig);
                        }

                        int digLocationCount = reader.ReadInt32();
                        for (int i = 0; i < digLocationCount; ++i)
                        {
                            DigLocations.Add(new Vector2I(reader.ReadInt32(), reader.ReadInt32()));
                        }
                    }

                    return(true);
                }

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

            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Reads the contents of a JSON-formatted file and adds it to the blueprint.
        /// </summary>
        public void ReadJSON()
        {
            if (File.Exists(FilePath))
            {
                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();
                                buildingConfig.ReadJSON((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 == null ? 0 : xToken.Value <int>(), yToken == null ? 0 : yToken.Value <int>()));
                                }

                                else if (xToken == null && yToken == null)
                                {
                                    DigLocations.Add(new Vector2I(0, 0));
                                }
                            }
                        }
                    }

                    CacheCost();
                }

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