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); }
public void 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(); 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.Value <int>(), yToken.Value <int>())); } } } } } } catch (System.Exception exception) { Debug.LogError("Error when loading blueprint: " + FilePath + ",\n" + nameof(exception) + ":" + exception.Message); } } }