public static JsonFormation ParseJsonFormation(string fileUri, IList <string> outErrors) { try { JsonFormation res = new JsonFormation(); using (var fileReader = File.OpenText(fileUri)) { JObject obj = JObject.Parse(fileReader.ReadToEnd()); IList <string> errors = new List <string>(); obj.IsValid(Schemas.GetFormationSchema(), out errors); if (errors.Any()) { foreach (var error in errors) { outErrors.Add(String.Format("Parse Error: {0}", error)); } return(null); } JArray version = (JArray)obj["version"]; (int major, int minor) = ((int)version[0], (int)version[1]); // TODO: Check formation file version res.Key = (string)obj["key"]; foreach (var item in (JArray)obj["schedule"]) { int startTime = (int)item["time"]; var posObj = (JArray)item["position"]; Vector2 pos = new Vector2((float)posObj[0], (float)posObj[1]); res.schedule.Add(new Tuple <int, Vector2>(startTime, pos)); } res.schedule = res.schedule.OrderBy(item => item.Item1).ToList(); } return(res); } catch (Exception ex) { outErrors.Add(String.Format("Exception: {0}", ex.Message)); return(null); } }
/// <summary> /// Load formations from file. /// </summary> /// <param name="uri">The directory to search for files</param> private void LoadFormations(string uri) { Game.Log.Load(" - Searching: " + uri); foreach (var fileUri in Directory.GetFiles(uri, "*.fdat", SearchOption.AllDirectories)) { using (var file = File.OpenRead(fileUri)) { try { var formation = JsonFormation.FromStream(file); if (formation == null) { Game.Log.Error("Failed to load enemy formation from file: " + fileUri); } else if (formations.ContainsKey(formation.Key)) { Game.Log.Error(" - Attempted to add formation with existing key: " + formation.Key); Game.Log.Error(" - - When loading file: " + fileUri); } else { Game.Log.Debug(String.Format("Loaded '{0}' from {1}", formation.Key, fileUri)); formations[formation.Key] = formation; } } catch (Exception ex) { Game.Log.Error("Exception loading formation: " + ExceptionToS(ex)); } } } }