Advertisement
Guest User

Untitled

a guest
Apr 17th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.96 KB | None | 0 0
  1. public ScriptData Build(string f)
  2. {
  3.     ScriptData script = new ScriptData { ScriptName = Path.GetFileNameWithoutExtension(f) };
  4.  
  5.     using (var reader = new StreamReader(File.OpenRead(f)))
  6.     {
  7.         ScriptNode node = null;
  8.  
  9.         while (!reader.EndOfStream)
  10.         {
  11.             string line = reader.ReadLine().Trim();
  12.  
  13.             // strip out comments which are anything past the first instance of // on a line
  14.             int commentStart = line.IndexOf("//");
  15.             if (commentStart >= 0)
  16.             {
  17.                 line = line.Remove(commentStart).Trim();
  18.             }
  19.  
  20.             // start of a new node
  21.             if (line.StartsWith("=="))
  22.             {
  23.                 if (node != null)
  24.                 {
  25.                     CheckNodeLogicOnly(node);
  26.                     script.Nodes.Add(node.ID, node);
  27.                 }
  28.  
  29.                 node = new ScriptNode { ID = line.Substring(2, line.Length - 4).Trim() };
  30.             }
  31.             else if (!string.IsNullOrEmpty(line))
  32.             {
  33.                 if (node == null)
  34.                 {
  35.                     throw new InvalidOperationException(string.Format("{0}: Unexpected line {1} outside of a node.", f, line));
  36.                 }
  37.  
  38.                 if (line.StartsWith("start"))
  39.                 {
  40.                     node.Start = bool.Parse(line.Substring("start:".Length).Trim());
  41.                 }
  42.                 else if (line.StartsWith("location"))
  43.                 {
  44.                     node.Location = (DialogDisplayLocation)Enum.Parse(typeof(DialogDisplayLocation), line.Substring("location:".Length).Trim(), true);
  45.                 }
  46.                 else if (line.StartsWith("condition"))
  47.                 {
  48.                     node.Conditions = ParseConditions(node.ID, f, line.Substring("condition:".Length).Trim());
  49.                 }
  50.                 else if (line.StartsWith("text"))
  51.                 {
  52.                     node.Text = line.Substring("text:".Length).Trim();
  53.                 }
  54.                 else if (line.StartsWith("sound"))
  55.                 {
  56.                     node.SoundEffect = line.Substring("sound:".Length).Trim();
  57.                 }
  58.                 else if (line.StartsWith("map"))
  59.                 {
  60.                     var pieces = line.Substring("map:".Length).Trim().Split(' ');
  61.                     node.Map = pieces[0].Trim();
  62.                     node.MapMarker = pieces[1].Trim();
  63.                 }
  64.                 else if (line.StartsWith("sequence"))
  65.                 {
  66.                     node.Sequence = line.Substring("sequence:".Length).Trim();
  67.                 }
  68.                 else if (line.StartsWith("set_marker"))
  69.                 {
  70.                     string marker = line.Substring("set_marker:".Length).Trim();
  71.                     if (!node.Markers.Contains(marker))
  72.                     {
  73.                         node.Markers.Add(marker);
  74.                     }
  75.                 }
  76.                 else if (line.StartsWith("clear_marker"))
  77.                 {
  78.                     string marker = line.Substring("clear_marker:".Length).Trim();
  79.                     if (!node.ClearMarkers.Contains(marker))
  80.                     {
  81.                         node.ClearMarkers.Add(marker);
  82.                     }
  83.                 }
  84.                 else if (line.StartsWith("add_item"))
  85.                 {
  86.                     string item = line.Substring("add_item:".Length).Trim();
  87.                     node.AddItems.Add(item); // don't check for uniqueness because A) the game state will do that and B) it lets us add multiples of items like arrows
  88.                 }
  89.                 else if (line.StartsWith("remove_item"))
  90.                 {
  91.                     string item = line.Substring("remove_item:".Length).Trim();
  92.                     node.RemoveItems.Add(item); // don't check for uniqueness because A) the game state will do that and B) it lets us remove multiple items
  93.                 }
  94.                 else if (line.StartsWith("trigger"))
  95.                 {
  96.                     string item = line.Substring("trigger:".Length).Trim();
  97.                     if (!node.Triggers.Contains(item))
  98.                     {
  99.                         node.Triggers.Add(item);
  100.                     }
  101.                 }
  102.                 else if (line.StartsWith("destroy_object"))
  103.                 {
  104.                     string item = line.Substring("destroy_object:".Length).Trim();
  105.                     if (!node.DestroyObjects.Contains(item))
  106.                     {
  107.                         node.DestroyObjects.Add(item);
  108.                     }
  109.                 }
  110.                 else if (line.StartsWith("add_money"))
  111.                 {
  112.                     int amount = int.Parse(line.Substring("add_money:".Length).Trim(), CultureInfo.InvariantCulture);
  113.                     node.MoneyChange += amount;
  114.                 }
  115.                 else if (line.StartsWith("remove_money"))
  116.                 {
  117.                     int amount = int.Parse(line.Substring("remove_money:".Length).Trim(), CultureInfo.InvariantCulture);
  118.                     node.MoneyChange -= amount;
  119.                 }
  120.                 else if (line.StartsWith("goto"))
  121.                 {
  122.                     if (node.Options.Count > 0)
  123.                     {
  124.                         throw new InvalidOperationException(string.Format("{0}: Node {1} has options and goto.", f, node.ID));
  125.                     }
  126.                     if (node.Goto.Count > 0)
  127.                     {
  128.                         throw new InvalidOperationException(string.Format("{0}: Node {1} has more than one goto.", f, node.ID));
  129.                     }
  130.  
  131.                     node.Goto = line.Substring("goto:".Length).Trim().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(g => g.Trim()).ToList();
  132.                 }
  133.                 else if (line.StartsWith("options"))
  134.                 {
  135.                     if (node.Goto.Count > 0)
  136.                     {
  137.                         throw new InvalidOperationException(string.Format("{0}: Node {1} has options and goto.", f, node.ID));
  138.                     }
  139.                     if (node.Switch.Count > 0)
  140.                     {
  141.                         throw new InvalidOperationException(string.Format("{0}: Node {1} has switch and options.", f, node.ID));
  142.                     }
  143.                     if (node.Options.Count > 0)
  144.                     {
  145.                         throw new InvalidOperationException(string.Format("{0}: Node {1} more than one options collection.", f, node.ID));
  146.                     }
  147.  
  148.                     line = reader.ReadLine();
  149.                     while (line.StartsWith(" "))
  150.                     {
  151.                         string[] parts = line.Split(':');
  152.                         if (parts.Length != 2)
  153.                         {
  154.                             throw new InvalidOperationException(string.Format("{0}: Node {1} has invalid option: {2}.", f, node.ID, line));
  155.                         }
  156.  
  157.                         node.Options.Add(new ScriptOption { Text = parts[0].Trim(), Goto = parts[1].Trim() });
  158.                         line = reader.ReadLine();
  159.                     }
  160.  
  161.                     if (node.Options.Count != 2)
  162.                     {
  163.                         throw new InvalidOperationException(string.Format("{0}: Node {1} doesn't have two options.", f, node.ID));
  164.                     }
  165.                 }
  166.                 else if (line.StartsWith("switch"))
  167.                 {
  168.                     if (node.Options.Count > 0)
  169.                     {
  170.                         throw new InvalidOperationException(string.Format("{0}: Node {1} has options and switch.", f, node.ID));
  171.                     }
  172.                     if (node.Switch.Count > 0)
  173.                     {
  174.                         throw new InvalidOperationException(string.Format("{0}: Node {1} has multiple switches.", f, node.ID));
  175.                     }
  176.  
  177.                     line = reader.ReadLine();
  178.                     while (line.StartsWith(" "))
  179.                     {
  180.                         string[] parts = line.Split(':');
  181.                         if (parts.Length != 2)
  182.                         {
  183.                             throw new InvalidOperationException(string.Format("{0}: Node {1} has invalid switch: {2}.", f, node.ID, line));
  184.                         }
  185.  
  186.                         node.Switch.Add(new ScriptSwitchOption(
  187.                             ParseConditions(node.ID, f, parts[0].Trim()),
  188.                             parts[1].Trim()));
  189.  
  190.                         line = reader.ReadLine();
  191.                     }
  192.  
  193.                     if (node.Switch.Count == 0)
  194.                     {
  195.                         throw new InvalidOperationException(string.Format("{0}: Node {1} has switch property with no switches.", f, node.ID));
  196.                     }
  197.                 }
  198.             }
  199.         }
  200.  
  201.         if (node != null)
  202.         {
  203.             CheckNodeLogicOnly(node);
  204.             script.Nodes.Add(node.ID, node);
  205.         }
  206.     }
  207.  
  208.     return script;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement