Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.96 KB | None | 0 0
  1. public bool LoadPersistentData(IMyTerminalBlock store)
  2. {
  3.     string error = PersistentDataLoader.LoadData(store.CustomData, out persistentData);
  4.      
  5.     if (error != "")
  6.     {
  7.         Warn("Error loading persistent data: " + error);
  8.         return false;
  9.     }
  10.     else
  11.     {
  12.         currentRoute = persistentData.Route;
  13.         return true;
  14.     }
  15. }
  16.  
  17. public void SavePersistentData(IMyTerminalBlock store)
  18. {
  19.     persistentData.Route = currentRoute;
  20.      
  21.     store.CustomData = PersistentDataLoader.SaveData(persistentData, store.CustomData);
  22. }
  23.  
  24. public class Route
  25. {
  26.     public struct Waypoint
  27.     {
  28.         public Vector3D Position;
  29.            
  30.         public Waypoint(Vector3D position, int waitTime)
  31.         {
  32.             Position = position;
  33.         }
  34.     }
  35.        
  36.     List<Waypoint> m_waypoints;
  37.    
  38.     public Route()
  39.     {
  40.         m_waypoints = new List<Waypoint>();
  41.     }
  42.    
  43.     public void AddWaypoint(Vector3D point)
  44.     {
  45.         m_waypoints.Add(new Waypoint(point));
  46.     }
  47.    
  48.     public void ClearWaypoints()
  49.     {
  50.         m_waypoints.Clear();
  51.     }
  52.        
  53.     public List<string> SaveToLines()
  54.     {
  55.         var lines = new List<string>();
  56.         foreach (var waypoint in m_waypoints)
  57.         {
  58.             lines.Add(waypoint.Position.X + "," +
  59.                         waypoint.Position.Y + "," +
  60.                         waypoint.Position.Z + "," +
  61.                         waypoint.WaitTime);
  62.         }
  63.         return lines;
  64.     }
  65.        
  66.     public void LoadFromLines(List<string> lines)
  67.     {
  68.         // clear current waypoints
  69.         ClearWaypoints();
  70.            
  71.         //  add new waypoints
  72.         foreach (var line in lines)
  73.         {
  74.             // validate and add waypoint
  75.             var data = line.Split(',');
  76.             double x, y, z;
  77.             int waitTime;
  78.             if (data.Count() == 4 &&
  79.                 double.TryParse(data[0], out x) &&
  80.                 double.TryParse(data[1], out y) &&
  81.                 double.TryParse(data[2], out z))
  82.             {
  83.                 AddWaypoint(new Vector3D(x, y, z));
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89.  
  90. public struct PersistentData
  91. {
  92.     public Route Route;
  93. }
  94.  
  95. public static class PersistentDataLoader
  96. {
  97.     static string dataBeginMarker = "PERSISTENT_DATA_BEGIN";
  98.     static string dataEndMarker = "PERSISTENT_DATA_END";
  99.     static string waypointMarker = "waypoint:";
  100.      
  101.     enum ErrorTypes
  102.     {
  103.         NoError,
  104.         DataParseError,
  105.         NoDataFoundError,
  106.         DataDidNotBeginError,
  107.         UnknownError
  108.     }
  109.        
  110.     // creates the string conversion of a PersistentData struct and inserts it into custom data
  111.     public static string SaveData(PersistentData pData, string text)
  112.     {
  113.         // first, get converted data
  114.         var cData = SaveData(pData);
  115.            
  116.         // if no text, just save right here
  117.         if (text == "")
  118.         {
  119.             return cData;
  120.         }
  121.            
  122.         // if there is text, break out any existing PersistentData
  123.         var lines = System.Text.RegularExpressions.Regex.Split(text, "\r\n|\r|\n");
  124.         var newLines = new List<string>();
  125.         var sb = new StringBuilder();
  126.         if (lines.Count() < 500)
  127.         {
  128.             // look for beginning of data persistence
  129.             bool hasStarted = false;
  130.             foreach (var line in lines)
  131.             {
  132.                 if (hasStarted)
  133.                 {
  134.                     if (line == dataEndMarker)
  135.                     {
  136.                         hasStarted = false;
  137.                     }
  138.                 }
  139.                 else if (line == dataBeginMarker)
  140.                 {
  141.                     hasStarted = true;
  142.                 }
  143.                 else
  144.                 {
  145.                     newLines.Add(line);
  146.                 }
  147.             }
  148.              
  149.             for (int i = 0; i < newLines.Count(); i++)
  150.             {
  151.                 if (i == newLines.Count() - 1)
  152.                 {
  153.                     sb.Append(newLines[i]);
  154.                 }
  155.                 else
  156.                 {
  157.                     sb.AppendLine(newLines[i]);
  158.                 }
  159.             }
  160.         }
  161.         else
  162.         {
  163.             sb.Append(text);
  164.         }
  165.          
  166.         sb.Append(cData);
  167.        
  168.         return sb.ToString();
  169.     }
  170.        
  171.     // returns the string conversion of a PersistentData struct
  172.     public static string SaveData(PersistentData pData)
  173.     {
  174.         // begin line strings
  175.         var lines = new List<string>();
  176.         lines.Add(dataBeginMarker);
  177.            
  178.         // save waypoints info
  179.         var waypoints = pData.Route.SaveToLines();
  180.         foreach (var waypoint in waypoints)
  181.         {
  182.             lines.Add(waypointMarker + waypoint);
  183.         }
  184.        
  185.         // add ending
  186.         lines.Add(dataEndMarker);
  187.            
  188.         // send back string to append to custom data
  189.         var sb = new StringBuilder();
  190.         foreach (var line in lines)
  191.         {
  192.             sb.AppendLine(line);
  193.         }
  194.         return sb.ToString();
  195.     }
  196.        
  197.     // loads data from a custom data string into an existing PersistentData struct
  198.     public static string LoadData(string text, out PersistentData pData)
  199.     {
  200.         // prepare struct for use by guaranteeing all fields are filled out
  201.         pData.Route = new Route();
  202.          
  203.         string error = "";
  204.         var data = new List<string>();
  205.          
  206.         if (text == "")
  207.         {
  208.             var sb = new StringBuilder();
  209.             sb.AppendLine(dataBeginMarker);
  210.             sb.Append(dataEndMarker);
  211.             text = sb.ToString();
  212.         }
  213.          
  214.         // split into lines
  215.         var lines = System.Text.RegularExpressions.Regex.Split(text, "\r\n|\r|\n");
  216.          
  217.         // don't mess it all up!!!
  218.         if (lines.Count() > 500)
  219.             return GetErrorMessage(ErrorTypes.DataParseError);
  220.        
  221.         // look for beginning of data persistence
  222.         bool hasStarted = false;
  223.         foreach (var line in lines)
  224.         {
  225.             if (hasStarted)
  226.             {
  227.                 if (line == dataEndMarker)
  228.                 {
  229.                     break;
  230.                 }
  231.                 else
  232.                 {
  233.                     data.Add(line);
  234.                 }
  235.             }
  236.             else if (line == dataBeginMarker)
  237.             {
  238.                 hasStarted = true;
  239.             }
  240.         }
  241.        
  242.         // load version
  243.         pData.Route = new Route();
  244.          
  245.         // prepare data that must be finalized post-search
  246.         var waypointLines = new List<string>();
  247.          
  248.         // load general data
  249.         foreach (var line in data)
  250.         {
  251.             // load waypoints
  252.             if (line.StartsWith(waypointMarker))
  253.             {
  254.                 waypointLines.Add(line.Substring(waypointMarker.Count()));
  255.             }
  256.         }
  257.          
  258.         // finalize data where necessary
  259.         pData.Route.LoadFromLines(waypointLines);
  260.          
  261.         // no errors occurred!
  262.         return "";
  263.     }
  264.        
  265.     static string GetErrorMessage(string type, string details = "")
  266.     {
  267.         string errorMessage = type;
  268.         if (details != "")
  269.         {
  270.             errorMessage += " { " + details + " }";
  271.         }
  272.         return errorMessage;
  273.     }
  274.        
  275.     static string GetErrorMessage(ErrorTypes type, string details = "")
  276.     {
  277.         switch (type)
  278.         {
  279.             case ErrorTypes.NoError: return GetErrorMessage("", details);
  280.             case ErrorTypes.DataParseError: return GetErrorMessage("An error occurred while parsing the custom data.", details);
  281.             case ErrorTypes.NoDataFoundError: return GetErrorMessage("No data was given.", details);
  282.             case ErrorTypes.DataDidNotBeginError: return GetErrorMessage("Could not find the beginning of the data.", details);
  283.             default: return GetErrorMessage("Unknown error.", details);
  284.         }
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement