ShaderBodyTokenizer.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Text;
  8. using System.Globalization;
  9. using System.Text.RegularExpressions;
  10. namespace AmplifyShaderEditor
  11. {
  12. public struct TokenDesc
  13. {
  14. public string name;
  15. public int position;
  16. public int line;
  17. public TokenDesc( string name, int position, int line )
  18. {
  19. this.name = name;
  20. this.position = position;
  21. this.line = line;
  22. }
  23. }
  24. public class ShaderBodyTokenTable
  25. {
  26. private int count = 0;
  27. public int Count { get { return count; } }
  28. private LinkedList<TokenDesc> tokens = new LinkedList<TokenDesc>();
  29. private Dictionary<string, List<LinkedListNode<TokenDesc>>> tokensByName = new Dictionary<string, List<LinkedListNode<TokenDesc>>>();
  30. private Dictionary<int, List<LinkedListNode<TokenDesc>>> tokensByLine = new Dictionary<int, List<LinkedListNode<TokenDesc>>>();
  31. private static List<LinkedListNode<TokenDesc>> EmptyTokenList = new List<LinkedListNode<TokenDesc>>();
  32. public bool Contains( string token )
  33. {
  34. return tokensByName.ContainsKey( token );
  35. }
  36. public List<LinkedListNode<TokenDesc>> ListTokensByName( string name )
  37. {
  38. if ( tokensByName.TryGetValue( name, out List<LinkedListNode<TokenDesc>> list ) )
  39. {
  40. return list;
  41. }
  42. return EmptyTokenList;
  43. }
  44. public List<LinkedListNode<TokenDesc>> ListTokensByLine( int line )
  45. {
  46. if ( tokensByLine.TryGetValue( line, out List<LinkedListNode<TokenDesc>> list ) )
  47. {
  48. return list;
  49. }
  50. return EmptyTokenList;
  51. }
  52. public void Add( string name, int position, int line )
  53. {
  54. var node = tokens.AddLast( new TokenDesc( name, position, line ) );
  55. if ( !tokensByName.TryGetValue( name, out List<LinkedListNode<TokenDesc>> listPerName ) )
  56. {
  57. listPerName = new List<LinkedListNode<TokenDesc>>();
  58. tokensByName.Add( name, listPerName );
  59. }
  60. if ( !tokensByLine.TryGetValue( line, out List<LinkedListNode<TokenDesc>> listPerLine ) )
  61. {
  62. listPerLine = new List<LinkedListNode<TokenDesc>>();
  63. tokensByLine.Add( line, listPerLine );
  64. }
  65. listPerName.Add( node );
  66. listPerLine.Add( node );
  67. count++;
  68. }
  69. }
  70. public class ShaderBodyTokenizer
  71. {
  72. private static double TimeSinceStartup
  73. {
  74. #if UNITY_2020_2_OR_NEWER
  75. get { return Time.realtimeSinceStartupAsDouble; }
  76. #else
  77. get { return Time.realtimeSinceStartup; }
  78. #endif
  79. }
  80. public static ShaderBodyTokenTable Process( string body )
  81. {
  82. var tokens = new ShaderBodyTokenTable();
  83. int charIndex = 0;
  84. int charCount = body.Length;
  85. int line = 0;
  86. var tokenBuilder = new StringBuilder( 1024 );
  87. do
  88. {
  89. char c = body[ charIndex++ ];
  90. bool isBreak = ( c == '\n' );
  91. bool isEmpty = ( isBreak || c == ' ' || c == '\t' || c == '\r' || c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']' || c == ';' || c == ',' || c == '\"' );
  92. if ( !isEmpty )
  93. {
  94. tokenBuilder.Clear();
  95. int position = charIndex;
  96. while ( !isEmpty && charIndex < charCount )
  97. {
  98. tokenBuilder.Append( c );
  99. c = body[ charIndex++ ];
  100. isBreak = ( c == '\n' );
  101. isEmpty = ( isBreak || c == ' ' || c == '\t' || c == '\r' || c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']' || c == ';' || c == ',' || c == '\"' );
  102. line += isBreak ? 1 : 0;
  103. }
  104. string token = tokenBuilder.ToString();
  105. if ( !token.StartsWith( "//" ) )
  106. {
  107. tokens.Add( token, position, line );
  108. }
  109. }
  110. else
  111. {
  112. line += isBreak ? 1 : 0;
  113. }
  114. } while ( charIndex < charCount );
  115. return tokens;
  116. }
  117. public static void TestProcess( string body )
  118. {
  119. UnityEngine.Profiling.Profiler.BeginSample( "Tokenize" );
  120. double start = TimeSinceStartup;
  121. ShaderBodyTokenTable tokens = ShaderBodyTokenizer.Process( body );
  122. UnityEngine.Profiling.Profiler.EndSample();
  123. Debug.Log( "Found " + tokens.Count + " tokens, taking " + ( ( TimeSinceStartup - start ) * 1000 ) + " ms" );
  124. Debug.Log( "Has Fallback " + tokens.Contains( "Fallback" ) );
  125. Debug.Log( "Has CustomEditor " + tokens.Contains( "CustomEditor" ) );
  126. var list = tokens.ListTokensByName( "CustomEditor" );
  127. foreach ( var node in list )
  128. {
  129. Debug.Log( "Name: " + node.Value.name + ", Position: " + node.Value.position + ", Line: " + node.Value.line );
  130. }
  131. //foreach ( var node in list )
  132. //
  133. //File.WriteAllLines( "C:/Users/Diogo/Desktop/dump.txt", tokens.Keys );
  134. }
  135. }
  136. }