public void LoadOrbitalBodies() { // Path.Combine combines strings into a file path // Application.StreamingAssets points to Assets/StreamingAssets in the Editor, and the StreamingAssets folder in a build string filePath = Application.dataPath + gameDataFileName; if (File.Exists(filePath)) { // Read the json from the file into a string string dataAsJson = File.ReadAllText(filePath); string[] jsonBodies = dataAsJson.Split(new[] { '{' }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in jsonBodies) { string temp = "{" + item; BodySaveData loadedBody = JsonUtility.FromJson <BodySaveData>(temp); loadedBodies.Add(loadedBody); } Debug.Log("Loaded BodyData"); } else { Debug.LogError("Cannot load game data!"); } }
/// <summary> /// Sets OrbitalBody values to BodySaveData (Serialized, saveable data) /// </summary> public void SetBodyReference(BodySaveData o) { m_id = o.ID; m_x_value = o.X_value; m_y_value = o.Y_value; m_z_value = o.Z_value; m_data_date = DateTime.Parse(o.Data_Date); cScale = transform.localScale; }
//Barely Used but kept for compatibility /* * public void SaveOrbitalBodies(List<OrbitalBody> objToSave) * { * StringBuilder sb = new StringBuilder(); * * foreach (var item in objToSave) * { * var saveObj = new BodySaveData(item.ID, item.X_value, item.Y_value, item.Z_value , item.Data_Date //if used again fix); * sb.Append(JsonUtility.ToJson(saveObj)); * } * * string filePath = Application.dataPath + gameDataFileName; * File.WriteAllText(filePath, sb.ToString()); * } */ public void SaveOrbitalBodies(List <BodySaveData> objToSave) { StringBuilder sb = new StringBuilder(); foreach (var item in objToSave) { var saveObj = new BodySaveData(item.ID, item.X_value, item.Y_value, item.Z_value, item.Data_Date); sb.Append(JsonUtility.ToJson(saveObj)); } string filePath = Application.dataPath + gameDataFileName; File.WriteAllText(filePath, sb.ToString()); }
/// <summary> /// Accesses orbital body through horizons and extracts ephemeris into vars and returns those vars into a double[] /// </summary> /// <param name="id"></param> public BodySaveData AccessBody(string id) { string[] initCommands = new string[] { "e", "v", "500@0", "y", "eclip", today.ToString(), tomorrow.ToString(), "1d", "y", "1", "n" }; string[] followUpCommands = new string[] { "e+", "n" }; double[] bodyChar = new double[3]; StringBuilder sb = new StringBuilder(); string m_Stringholder; UnityEngine.Debug.Log("Starting Body" + id); //Send body ID JplWrite(id); sb.Append(JplRead()); if (id == startingBody) { m_Stringholder = SendCommandsForBody(sb, initCommands); } else { m_Stringholder = SendCommandsForBody(sb, followUpCommands); } //Get empheris //$$SOE Start of ephemeris int startPos = m_Stringholder.LastIndexOf("$$SOE") + "$$SOE".Length + 1; //$$EOE End of ephemeris int length = m_Stringholder.IndexOf("$$EOE") - startPos; string sub = m_Stringholder.Substring(startPos, length); //Split into only today and it X Y Z sections m_Stringholder = sub.Substring(sub.IndexOf("X"), sub.IndexOf("\r\n VX") - sub.IndexOf("X")); UnityEngine.Debug.Log("Ephemeris done"); //Split into Vars bodyChar = SplitIntoVars(m_Stringholder); BodySaveData orbitalBody = new BodySaveData(id, bodyChar[0], bodyChar[1], bodyChar[2], today.ToString()); UnityEngine.Debug.Log(id + " body done"); return(orbitalBody); }