Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ScriptData Build(string f)
- {
- ScriptData script = new ScriptData { ScriptName = Path.GetFileNameWithoutExtension(f) };
- using (var reader = new StreamReader(File.OpenRead(f)))
- {
- ScriptNode node = null;
- while (!reader.EndOfStream)
- {
- string line = reader.ReadLine().Trim();
- // strip out comments which are anything past the first instance of // on a line
- int commentStart = line.IndexOf("//");
- if (commentStart >= 0)
- {
- line = line.Remove(commentStart).Trim();
- }
- // start of a new node
- if (line.StartsWith("=="))
- {
- if (node != null)
- {
- CheckNodeLogicOnly(node);
- script.Nodes.Add(node.ID, node);
- }
- node = new ScriptNode { ID = line.Substring(2, line.Length - 4).Trim() };
- }
- else if (!string.IsNullOrEmpty(line))
- {
- if (node == null)
- {
- throw new InvalidOperationException(string.Format("{0}: Unexpected line {1} outside of a node.", f, line));
- }
- if (line.StartsWith("start"))
- {
- node.Start = bool.Parse(line.Substring("start:".Length).Trim());
- }
- else if (line.StartsWith("location"))
- {
- node.Location = (DialogDisplayLocation)Enum.Parse(typeof(DialogDisplayLocation), line.Substring("location:".Length).Trim(), true);
- }
- else if (line.StartsWith("condition"))
- {
- node.Conditions = ParseConditions(node.ID, f, line.Substring("condition:".Length).Trim());
- }
- else if (line.StartsWith("text"))
- {
- node.Text = line.Substring("text:".Length).Trim();
- }
- else if (line.StartsWith("sound"))
- {
- node.SoundEffect = line.Substring("sound:".Length).Trim();
- }
- else if (line.StartsWith("map"))
- {
- var pieces = line.Substring("map:".Length).Trim().Split(' ');
- node.Map = pieces[0].Trim();
- node.MapMarker = pieces[1].Trim();
- }
- else if (line.StartsWith("sequence"))
- {
- node.Sequence = line.Substring("sequence:".Length).Trim();
- }
- else if (line.StartsWith("set_marker"))
- {
- string marker = line.Substring("set_marker:".Length).Trim();
- if (!node.Markers.Contains(marker))
- {
- node.Markers.Add(marker);
- }
- }
- else if (line.StartsWith("clear_marker"))
- {
- string marker = line.Substring("clear_marker:".Length).Trim();
- if (!node.ClearMarkers.Contains(marker))
- {
- node.ClearMarkers.Add(marker);
- }
- }
- else if (line.StartsWith("add_item"))
- {
- string item = line.Substring("add_item:".Length).Trim();
- 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
- }
- else if (line.StartsWith("remove_item"))
- {
- string item = line.Substring("remove_item:".Length).Trim();
- 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
- }
- else if (line.StartsWith("trigger"))
- {
- string item = line.Substring("trigger:".Length).Trim();
- if (!node.Triggers.Contains(item))
- {
- node.Triggers.Add(item);
- }
- }
- else if (line.StartsWith("destroy_object"))
- {
- string item = line.Substring("destroy_object:".Length).Trim();
- if (!node.DestroyObjects.Contains(item))
- {
- node.DestroyObjects.Add(item);
- }
- }
- else if (line.StartsWith("add_money"))
- {
- int amount = int.Parse(line.Substring("add_money:".Length).Trim(), CultureInfo.InvariantCulture);
- node.MoneyChange += amount;
- }
- else if (line.StartsWith("remove_money"))
- {
- int amount = int.Parse(line.Substring("remove_money:".Length).Trim(), CultureInfo.InvariantCulture);
- node.MoneyChange -= amount;
- }
- else if (line.StartsWith("goto"))
- {
- if (node.Options.Count > 0)
- {
- throw new InvalidOperationException(string.Format("{0}: Node {1} has options and goto.", f, node.ID));
- }
- if (node.Goto.Count > 0)
- {
- throw new InvalidOperationException(string.Format("{0}: Node {1} has more than one goto.", f, node.ID));
- }
- node.Goto = line.Substring("goto:".Length).Trim().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(g => g.Trim()).ToList();
- }
- else if (line.StartsWith("options"))
- {
- if (node.Goto.Count > 0)
- {
- throw new InvalidOperationException(string.Format("{0}: Node {1} has options and goto.", f, node.ID));
- }
- if (node.Switch.Count > 0)
- {
- throw new InvalidOperationException(string.Format("{0}: Node {1} has switch and options.", f, node.ID));
- }
- if (node.Options.Count > 0)
- {
- throw new InvalidOperationException(string.Format("{0}: Node {1} more than one options collection.", f, node.ID));
- }
- line = reader.ReadLine();
- while (line.StartsWith(" "))
- {
- string[] parts = line.Split(':');
- if (parts.Length != 2)
- {
- throw new InvalidOperationException(string.Format("{0}: Node {1} has invalid option: {2}.", f, node.ID, line));
- }
- node.Options.Add(new ScriptOption { Text = parts[0].Trim(), Goto = parts[1].Trim() });
- line = reader.ReadLine();
- }
- if (node.Options.Count != 2)
- {
- throw new InvalidOperationException(string.Format("{0}: Node {1} doesn't have two options.", f, node.ID));
- }
- }
- else if (line.StartsWith("switch"))
- {
- if (node.Options.Count > 0)
- {
- throw new InvalidOperationException(string.Format("{0}: Node {1} has options and switch.", f, node.ID));
- }
- if (node.Switch.Count > 0)
- {
- throw new InvalidOperationException(string.Format("{0}: Node {1} has multiple switches.", f, node.ID));
- }
- line = reader.ReadLine();
- while (line.StartsWith(" "))
- {
- string[] parts = line.Split(':');
- if (parts.Length != 2)
- {
- throw new InvalidOperationException(string.Format("{0}: Node {1} has invalid switch: {2}.", f, node.ID, line));
- }
- node.Switch.Add(new ScriptSwitchOption(
- ParseConditions(node.ID, f, parts[0].Trim()),
- parts[1].Trim()));
- line = reader.ReadLine();
- }
- if (node.Switch.Count == 0)
- {
- throw new InvalidOperationException(string.Format("{0}: Node {1} has switch property with no switches.", f, node.ID));
- }
- }
- }
- }
- if (node != null)
- {
- CheckNodeLogicOnly(node);
- script.Nodes.Add(node.ID, node);
- }
- }
- return script;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement