Advertisement
Guest User

Untitled

a guest
May 17th, 2021
1,943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.52 KB | None | 0 0
  1.         public void ParseTwineText( string twineText )
  2.         {
  3.             string[] nodeData = twineText.Split(new string[] { "::" }, StringSplitOptions.None);
  4.  
  5.             bool passedHeader = false;
  6.             const int kIndexOfContentStart = 4;
  7.             for ( int i = 0; i < nodeData.Length; i++ )
  8.             {
  9.  
  10.                 // The first node comes after the UserStylesheet node
  11.                 if ( !passedHeader )
  12.                 {
  13.                     if ( nodeData[ i ].StartsWith( " UserStylesheet" ) )
  14.                         passedHeader = true;
  15.  
  16.                     continue;
  17.                 }
  18.  
  19.                 // Note: tags are optional
  20.                 // Normal Format: "NodeTitle [Tags, comma, seperated] \r\n Message Text \r\n [[Response One]] \r\n [[Response Two]]"
  21.                 // No-Tag Format: "NodeTitle \r\n Message Text \r\n [[Response One]] \r\n [[Response Two]]"
  22.                 string currLineText = nodeData[i];
  23.  
  24.                 // Remove position data
  25.                 int posBegin = currLineText.IndexOf("{\"position");
  26.                 if ( posBegin != -1 )
  27.                 {
  28.                     int posEnd = currLineText.IndexOf("}", posBegin);
  29.                     currLineText = currLineText.Substring( 0, posBegin ) + currLineText.Substring( posEnd + 1 );
  30.                 }
  31.  
  32.                 bool tagsPresent = currLineText.IndexOf( "[" ) < currLineText.IndexOf( "\r\n" );
  33.                 int endOfFirstLine = currLineText.IndexOf( "\r\n" );
  34.  
  35.                 int startOfResponses = -1;
  36.                 int startOfResponseDestinations = currLineText.IndexOf( "[[" );
  37.                 bool lastNode = (startOfResponseDestinations == -1);
  38.                 if ( lastNode )
  39.                     startOfResponses = currLineText.Length;
  40.                 else
  41.                 {
  42.                     // Last new line before "[["
  43.                     startOfResponses = currLineText.Substring( 0, startOfResponseDestinations ).LastIndexOf( "\r\n" );
  44.                 }
  45.  
  46.                 // Extract Title
  47.                 int titleStart = 0;
  48.                 int titleEnd = tagsPresent
  49.                     ? currLineText.IndexOf( "[" )
  50.                     : endOfFirstLine;
  51.                 UnityEngine.Assertions.Assert.IsTrue( titleEnd > 0, "Maybe you have a node with no responses?" );
  52.                 string title = currLineText.Substring(titleStart, titleEnd).Trim();
  53.  
  54.                 // Extract Tags (if any)
  55.                 string tags = tagsPresent
  56.                     ? currLineText.Substring( titleEnd + 1, (endOfFirstLine - titleEnd)-2)
  57.                     : "";
  58.  
  59.                 if ( !string.IsNullOrEmpty(tags) && tags[ tags.Length - 1 ] == ']' )
  60.                     tags = tags.Substring( 0, tags.Length - 1 );
  61.  
  62.                 // Extract Message Text & Responses
  63.                 string messsageText = currLineText.Substring( endOfFirstLine, startOfResponses - endOfFirstLine).Trim();
  64.                 string responseText = currLineText.Substring( startOfResponses ).Trim();
  65.  
  66.                 Node curNode = new Node();
  67.                 curNode.title = title;
  68.                 curNode.text = messsageText;
  69.                 curNode.tags = new List<string>( tags.Split( new string[] { " " }, StringSplitOptions.None ) );
  70.  
  71.                 if ( curNode.tags.Contains( kStart ) )
  72.                 {
  73.                     UnityEngine.Assertions.Assert.IsTrue( null == titleOfStartNode );
  74.                     titleOfStartNode = curNode.title;
  75.                 }
  76.  
  77.                 // Note: response messages are optional (if no message then destination is the message)
  78.                 // With Message Format: "\r\n Message[[Response One]]"
  79.                 // Message-less Format: "\r\n [[Response One]]"
  80.                 curNode.responses = new List<Response>();
  81.                 if ( !lastNode )
  82.                 {
  83.                     List<string> responseData = new List<string>(responseText.Split( new string [] { "\r\n" }, StringSplitOptions.None ));
  84.                     for ( int k = responseData.Count - 1; k >= 0; k-- )
  85.                     {
  86.                         string curResponseData = responseData[k];
  87.  
  88.                         if ( string.IsNullOrEmpty( curResponseData ) )
  89.                         {
  90.                             responseData.RemoveAt( k );
  91.                             continue;
  92.                         }
  93.  
  94.                         Response curResponse = new Response();
  95.                         int destinationStart = curResponseData.IndexOf( "[[" );
  96.                         int destinationEnd = curResponseData.IndexOf( "]]" );
  97.                         UnityEngine.Assertions.Assert.IsFalse( destinationStart == -1, "No destination around in node titled, '" + curNode.title + "'" );
  98.                         UnityEngine.Assertions.Assert.IsFalse( destinationEnd == -1, "No destination around in node titled, '" + curNode.title + "'" );
  99.                         string destination = curResponseData.Substring(destinationStart + 2, (destinationEnd - destinationStart)-2);
  100.                         curResponse.destinationNode = destination;
  101.                         if ( destinationStart == 0 )
  102.                             curResponse.displayText = ""; // If message-less, then message is an empty string
  103.                         else
  104.                             curResponse.displayText = curResponseData.Substring( 0, destinationStart );
  105.                         curNode.responses.Add( curResponse );
  106.                     }
  107.                 }
  108.  
  109.                 nodes[ curNode.title ] = curNode;
  110.             }
  111.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement