IOUtils.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. using System.Linq;
  9. using UnityEngine;
  10. using UnityEditor;
  11. using UnityEditor.Build;
  12. using System.Threading;
  13. using UnityEditor.VersionControl;
  14. using System.Text.RegularExpressions;
  15. namespace AmplifyShaderEditor
  16. {
  17. public enum ShaderLoadResult
  18. {
  19. LOADED,
  20. TEMPLATE_LOADED,
  21. FILE_NOT_FOUND,
  22. ASE_INFO_NOT_FOUND,
  23. UNITY_NATIVE_PATHS
  24. }
  25. public class Worker
  26. {
  27. public static readonly object locker = new object();
  28. public void DoWork()
  29. {
  30. while( IOUtils.ActiveThread )
  31. {
  32. if( IOUtils.SaveInThreadFlag )
  33. {
  34. IOUtils.SaveInThreadFlag = false;
  35. lock( locker )
  36. {
  37. IOUtils.SaveInThreadShaderBody = string.Format( IOUtils.ShaderCopywriteMessage, VersionInfo.StaticToString() ) + IOUtils.SaveInThreadShaderBody;
  38. // Add checksum
  39. string checksum = IOUtils.CreateChecksum( IOUtils.SaveInThreadShaderBody );
  40. IOUtils.SaveInThreadShaderBody += IOUtils.CHECKSUM + IOUtils.VALUE_SEPARATOR + checksum;
  41. // Write to disk
  42. StreamWriter fileWriter = new StreamWriter( IOUtils.SaveInThreadPathName );
  43. try
  44. {
  45. fileWriter.Write( IOUtils.SaveInThreadShaderBody );
  46. Debug.Log( "Saving complete" );
  47. }
  48. catch( Exception e )
  49. {
  50. Debug.LogException( e );
  51. }
  52. finally
  53. {
  54. fileWriter.Close();
  55. }
  56. }
  57. }
  58. }
  59. Debug.Log( "Thread closed" );
  60. }
  61. }
  62. public static class IOUtils
  63. {
  64. public delegate void OnShaderAction( Shader shader , bool isTemplate , string type );
  65. public static OnShaderAction OnShaderSavedEvent;
  66. public static OnShaderAction OnShaderTypeChangedEvent;
  67. public static readonly string ShaderCopywriteMessage = "// Made with Amplify Shader Editor v{0}\n// Available at the Unity Asset Store - http://u3d.as/y3X \n";
  68. public static readonly string GrabPassEmpty = "\t\tGrabPass{ }\n";
  69. public static readonly string GrabPassBegin = "\t\tGrabPass{ \"";
  70. public static readonly string GrabPassEnd = "\" }\n";
  71. public static readonly string PropertiesBegin = "\tProperties\n\t{\n";
  72. public static readonly string PropertiesEnd = "\t}\n";
  73. public static readonly string PropertiesElement = "\t\t{0}\n";
  74. public static readonly string PropertiesElementsRaw = "{0}\n";
  75. public static readonly string PragmaTargetHeader = "\t\t#pragma target {0}\n";
  76. public static readonly string InstancedPropertiesHeader = "multi_compile_instancing";
  77. public static readonly string VirtualTexturePragmaHeader = "multi_compile _ _VT_SINGLE_MODE";
  78. public static readonly string InstancedPropertiesBegin = "UNITY_INSTANCING_CBUFFER_START({0})";
  79. public static readonly string InstancedPropertiesEnd = "UNITY_INSTANCING_CBUFFER_END";
  80. public static readonly string InstancedPropertiesElement = "UNITY_DEFINE_INSTANCED_PROP({0}, {1})";
  81. public static readonly string InstancedPropertiesData = "UNITY_ACCESS_INSTANCED_PROP({0})";
  82. public static readonly string DotsInstancedPropertiesData = "\tUNITY_DOTS_INSTANCED_PROP({0}, {1})";
  83. public static string DotsInstancedDefinesData
  84. {
  85. get
  86. {
  87. if ( ASEPackageManagerHelper.PackageSRPVersion >= ( int )ASESRPBaseline.ASE_SRP_13 )
  88. {
  89. return "#define {1} UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT({0} , {1})";
  90. }
  91. else if ( ASEPackageManagerHelper.PackageSRPVersion >= ( int )ASESRPBaseline.ASE_SRP_12 )
  92. {
  93. return "#define {1} UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO({0} , Metadata{1})";
  94. }
  95. else
  96. {
  97. return "#define {1} UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO({0} , Metadata_{1})";
  98. }
  99. }
  100. }
  101. public static readonly string LWSRPInstancedPropertiesBegin = "UNITY_INSTANCING_BUFFER_START({0})";
  102. public static readonly string LWSRPInstancedPropertiesEnd = "UNITY_INSTANCING_BUFFER_END({0})";
  103. public static readonly string LWSRPInstancedPropertiesElement = "UNITY_DEFINE_INSTANCED_PROP({0}, {1})";
  104. public static readonly string LWSRPInstancedPropertiesData = "UNITY_ACCESS_INSTANCED_PROP({0},{1})";
  105. public static readonly string SRPCBufferPropertiesBegin = "CBUFFER_START( UnityPerMaterial )";//"CBUFFER_START({0})";
  106. public static readonly string SRPCBufferPropertiesEnd = "CBUFFER_END";
  107. public static readonly string InstancedPropertiesBeginTabs = "\t\t" + InstancedPropertiesBegin + "\n";
  108. public static readonly string InstancedPropertiesEndTabs = "\t\t" + InstancedPropertiesEnd + "\n";
  109. public static readonly string InstancedPropertiesElementTabs = "\t\t\t" + InstancedPropertiesElement + "\n";
  110. public static readonly string MetaBegin = "defaultTextures:";
  111. public static readonly string MetaEnd = "userData:";
  112. public static readonly string ShaderBodyBegin = "/*ASEBEGIN";
  113. public static readonly string ShaderBodyEnd = "ASEEND*/";
  114. //public static readonly float CurrentVersionFlt = 0.4f;
  115. //public static readonly string CurrentVersionStr = "Version=" + CurrentVersionFlt;
  116. public static readonly string CHECKSUM = "//CHKSM";
  117. public static readonly string LAST_OPENED_OBJ_ID = "ASELASTOPENOBJID";
  118. public static readonly string MAT_CLIPBOARD_ID = "ASEMATCLIPBRDID";
  119. public static readonly char FIELD_SEPARATOR = ';';
  120. public static readonly char VALUE_SEPARATOR = '=';
  121. public static readonly char LINE_TERMINATOR = '\n';
  122. public static readonly char VECTOR_SEPARATOR = ',';
  123. public static readonly char FLOAT_SEPARATOR = '.';
  124. public static readonly char CLIPBOARD_DATA_SEPARATOR = '|';
  125. public static readonly char MATRIX_DATA_SEPARATOR = '|';
  126. public readonly static string NO_TEXTURES = "<None>";
  127. public static readonly string SaveShaderStr = "Please enter shader name to save";
  128. public static readonly string FloatifyStr = ".0";
  129. // Node parameter names
  130. public const string NodeParam = "Node";
  131. public const string NodePosition = "Position";
  132. public const string NodeId = "Id";
  133. public const string NodeType = "Type";
  134. public const string WireConnectionParam = "WireConnection";
  135. public static readonly uint NodeTypeId = 1;
  136. public static readonly int InNodeId = 1;
  137. public static readonly int InPortId = 2;
  138. public static readonly int OutNodeId = 3;
  139. public static readonly int OutPortId = 4;
  140. public readonly static string DefaultASEDirtyCheckName = "__dirty";
  141. public readonly static string DefaultASEDirtyCheckProperty = "[HideInInspector] " + DefaultASEDirtyCheckName + "( \"\", Int ) = 1";
  142. public readonly static string DefaultASEDirtyCheckUniform = "uniform int " + DefaultASEDirtyCheckName + " = 1;";
  143. public readonly static string MaskClipValueName = "_Cutoff";
  144. public readonly static string MaskClipValueProperty = MaskClipValueName + "( \"{0}\", Float ) = {1}";
  145. public readonly static string MaskClipValueUniform = "uniform float " + MaskClipValueName + " = {0};";
  146. public readonly static string ChromaticAberrationProperty = "_ChromaticAberration";
  147. //public static readonly string ASEFolderGUID = "daca988099666ec40aaa2cde22bb4935";
  148. //public static string ASEResourcesPath = "/Plugins/EditorResources/";
  149. //public static string ASEFolderPath;
  150. //public static bool IsShaderFunctionWindow = false;
  151. public static int DefaultASEDirtyCheckId;
  152. // this is to be used in combination with AssetDatabase.GetAssetPath, both of these include the Assets/ path so we need to remove from one of them
  153. public static string dataPath;
  154. public static string EditorResourcesGUID = "0932db7ec1402c2489679c4b72eab5eb";
  155. public static string GraphBgTextureGUID = "881c304491028ea48b5027ac6c62cf73";
  156. public static string GraphFgTextureGUID = "8c4a7fca2884fab419769ccc0355c0c1";
  157. public static string WireTextureGUID = "06e687f68dd96f0448c6d8217bbcf608";
  158. public static string MasterNodeOnTextureGUID = "26c64fcee91024a49980ea2ee9d1a2fb";
  159. public static string MasterNodeOffTextureGUID = "712aee08d999c16438e2d694f42428e8";
  160. public static string GPUInstancedOnTextureGUID = "4b0c2926cc71c5846ae2a29652d54fb6";
  161. public static string GPUInstancedOffTextureGUID = "486c7766baaf21b46afb63c1121ef03e";
  162. public static string MainSkinGUID = "57482289c346f104a8162a3a79aaff9d";
  163. public static string UpdateOutdatedGUID = "cce638be049286c41bcbd0a26c356b18";
  164. public static string UpdateOFFGUID = "99d70ac09b4db9742b404c3f92d8564b";
  165. public static string UpdateUpToDatedGUID = "ce30b12fbb3223746bcfef9ea82effe3";
  166. public static string LiveOffGUID = "bb16faf366bcc6c4fbf0d7666b105354";
  167. public static string LiveOnGUID = "6a0ae1d7892333142aeb09585572202c";
  168. public static string LivePendingGUID = "e3182200efb67114eb5050f8955e1746";
  169. public static string CleanupOFFGUID = "f62c0c3a5ddcd844e905fb2632fdcb15";
  170. public static string CleanUpOnGUID = "615d853995cf2344d8641fd19cb09b5d";
  171. public static string TakeScreenshotOFFGUID = "7587de2e3bec8bf4d973109524ccc6b1";
  172. public static string TakeScreenshotONGUID = "7587de2e3bec8bf4d973109524ccc6b1";
  173. public static string ShareOFFGUID = "bc5bd469748466a459badfab23915cb0";
  174. public static string ShareONGUID = "bc5bd469748466a459badfab23915cb0";
  175. public static string OpenSourceCodeOFFGUID = "f7e8834b42791124095a8b7f2d4daac2";
  176. public static string OpenSourceCodeONGUID = "8b114792ff84f6546880c031eda42bc0";
  177. public static string FocusNodeGUID = "da673e6179c67d346abb220a6935e359";
  178. public static string FitViewGUID = "1def740f2314c6b4691529cadeee2e9c";
  179. public static string ShowInfoWindowGUID = "77af20044e9766840a6be568806dc22e";
  180. public static string ShowTipsWindowGUID = "066674048bbb1e64e8cdcc6c3b4abbeb";
  181. public static string ShowConsoleWindowGUID = "9a81d7df8e62c044a9d1cada0c8a2131";
  182. public static Dictionary<string , string> NodeTypeReplacer = new Dictionary<string , string>()
  183. {
  184. {"AmplifyShaderEditor.RotateAboutAxis", "AmplifyShaderEditor.RotateAboutAxisNode"},
  185. {"GlobalArrayNode", "AmplifyShaderEditor.GlobalArrayNode"},
  186. {"AmplifyShaderEditor.SimpleMaxOp", "AmplifyShaderEditor.SimpleMaxOpNode"},
  187. {"AmplifyShaderEditor.SimpleMinNode", "AmplifyShaderEditor.SimpleMinOpNode"},
  188. {"AmplifyShaderEditor.TFHCRemap", "AmplifyShaderEditor.TFHCRemapNode"},
  189. {"AmplifyShaderEditor.TFHCPixelateUV", "AmplifyShaderEditor.TFHCPixelate"},
  190. {"AmplifyShaderEditor.VirtualTexturePropertyNode", "AmplifyShaderEditor.VirtualTextureObject"}
  191. };
  192. private static readonly string AmplifyShaderEditorDefineSymbol = "AMPLIFY_SHADER_EDITOR";
  193. /////////////////////////////////////////////////////////////////////////////
  194. // THREAD IO UTILS
  195. public static bool SaveInThreadFlag = false;
  196. public static string SaveInThreadShaderBody;
  197. public static string SaveInThreadPathName;
  198. public static Thread SaveInThreadMainThread;
  199. public static bool ActiveThread = true;
  200. private static bool UseSaveThread = false;
  201. private static bool Initialized = false;
  202. public static bool FunctionNodeChanged = false;
  203. public static List<AmplifyShaderEditorWindow> AllOpenedWindows = new List<AmplifyShaderEditorWindow>();
  204. public static Type[] GetTypesInNamespace( Assembly assembly , string nameSpace )
  205. {
  206. return assembly.GetTypes().Where( t => String.Equals( t.Namespace , nameSpace , StringComparison.Ordinal ) ).ToArray();
  207. }
  208. public static List<Assembly> LoadedAssemblies;
  209. public static Type[] GetAssemblyTypesArray()
  210. {
  211. Type[] availableTypes = null;
  212. if( LoadedAssemblies == null )
  213. {
  214. LoadedAssemblies = new List<Assembly>();
  215. try
  216. {
  217. UnityEditor.Compilation.Assembly[] assemblies = UnityEditor.Compilation.CompilationPipeline.GetAssemblies( UnityEditor.Compilation.AssembliesType.Editor );
  218. for( int i = 0 ; i < assemblies.Length ; i++ )
  219. {
  220. if( !assemblies[ i ].name.StartsWith( "Unity" ) && !assemblies[ i ].name.Equals( "AmplifyShaderEditor" ) )
  221. {
  222. Assembly assembly = Assembly.Load( assemblies[ i ].name );
  223. LoadedAssemblies.Add( assembly );
  224. Type[] extraTypes = GetTypesInNamespace( assembly , "AmplifyShaderEditor" );
  225. if( extraTypes.Length > 0 )
  226. {
  227. availableTypes = ( availableTypes == null ) ? extraTypes : availableTypes.Concat<Type>( extraTypes ).ToArray();
  228. }
  229. }
  230. }
  231. }
  232. catch( Exception e )
  233. {
  234. Debug.LogException( e );
  235. }
  236. }
  237. else
  238. {
  239. int count = LoadedAssemblies.Count;
  240. for( int i = 0 ; i < count ; i++ )
  241. {
  242. Type[] extraTypes = GetTypesInNamespace( LoadedAssemblies[i] , "AmplifyShaderEditor" );
  243. if( extraTypes.Length > 0 )
  244. {
  245. availableTypes = ( availableTypes == null ) ? extraTypes : availableTypes.Concat<Type>( extraTypes ).ToArray();
  246. }
  247. }
  248. }
  249. return availableTypes;
  250. }
  251. public static Type GetAssemblyType( string typeName )
  252. {
  253. if( LoadedAssemblies == null )
  254. {
  255. LoadedAssemblies = new List<Assembly>();
  256. try
  257. {
  258. UnityEditor.Compilation.Assembly[] assemblies = UnityEditor.Compilation.CompilationPipeline.GetAssemblies( UnityEditor.Compilation.AssembliesType.Editor );
  259. for( int i = 0 ; i < assemblies.Length ; i++ )
  260. {
  261. if( !assemblies[ i ].name.StartsWith( "Unity" ) && !assemblies[ i ].name.Equals( "AmplifyShaderEditor" ) )
  262. {
  263. Assembly assembly = Assembly.Load( assemblies[ i ].name );
  264. LoadedAssemblies.Add( assembly );
  265. Type type = assembly.GetType( typeName );
  266. if( type != null )
  267. return type;
  268. }
  269. }
  270. }
  271. catch( Exception e )
  272. {
  273. Debug.LogException( e );
  274. }
  275. }
  276. else
  277. {
  278. int count = LoadedAssemblies.Count;
  279. for( int i = 0 ; i < count ; i++ )
  280. {
  281. Type type = LoadedAssemblies[i].GetType( typeName );
  282. if( type != null )
  283. return type;
  284. }
  285. }
  286. return null;
  287. }
  288. public static void ClearLoadedAssemblies()
  289. {
  290. if( LoadedAssemblies != null )
  291. {
  292. LoadedAssemblies.Clear();
  293. LoadedAssemblies = null;
  294. }
  295. }
  296. public static void StartSaveThread( string shaderBody , string pathName )
  297. {
  298. if( Provider.enabled && Provider.isActive )
  299. {
  300. Asset loadedAsset = Provider.GetAssetByPath( FileUtil.GetProjectRelativePath( pathName ) );
  301. if( loadedAsset != null )
  302. {
  303. //Task statusTask = Provider.Status( loadedAsset );
  304. //statusTask.Wait();
  305. //if( Provider.CheckoutIsValid( statusTask.assetList[ 0 ] ) )
  306. {
  307. Task checkoutTask = Provider.Checkout( loadedAsset , CheckoutMode.Both );
  308. checkoutTask.Wait();
  309. }
  310. }
  311. }
  312. if( UseSaveThread )
  313. {
  314. if( !SaveInThreadFlag )
  315. {
  316. if( SaveInThreadMainThread == null )
  317. {
  318. Worker worker = new Worker();
  319. SaveInThreadMainThread = new Thread( worker.DoWork );
  320. SaveInThreadMainThread.Start();
  321. Debug.Log( "Thread created" );
  322. }
  323. SaveInThreadShaderBody = shaderBody;
  324. SaveInThreadPathName = pathName;
  325. SaveInThreadFlag = true;
  326. }
  327. }
  328. else
  329. {
  330. SaveTextfileToDisk( shaderBody , pathName );
  331. }
  332. }
  333. ////////////////////////////////////////////////////////////////////////////
  334. public static void SetAmplifyDefineSymbolOnBuildTargetGroup( BuildTargetGroup targetGroup )
  335. {
  336. #if UNITY_2021_2_OR_NEWER
  337. var namedBuildTarget = NamedBuildTarget.FromBuildTargetGroup( targetGroup );
  338. string currData = PlayerSettings.GetScriptingDefineSymbols( namedBuildTarget );
  339. #else
  340. string currData = PlayerSettings.GetScriptingDefineSymbolsForGroup( targetGroup );
  341. #endif
  342. if( !currData.Contains( AmplifyShaderEditorDefineSymbol ) )
  343. {
  344. if( string.IsNullOrEmpty( currData ) )
  345. {
  346. #if UNITY_2021_2_OR_NEWER
  347. PlayerSettings.SetScriptingDefineSymbols( namedBuildTarget, AmplifyShaderEditorDefineSymbol );
  348. #else
  349. PlayerSettings.SetScriptingDefineSymbolsForGroup( targetGroup, AmplifyShaderEditorDefineSymbol );
  350. #endif
  351. }
  352. else
  353. {
  354. if( !currData[ currData.Length - 1 ].Equals( ';' ) )
  355. {
  356. currData += ';';
  357. }
  358. currData += AmplifyShaderEditorDefineSymbol;
  359. #if UNITY_2021_2_OR_NEWER
  360. PlayerSettings.SetScriptingDefineSymbols( namedBuildTarget, currData );
  361. #else
  362. PlayerSettings.SetScriptingDefineSymbolsForGroup( targetGroup, currData );
  363. #endif
  364. }
  365. }
  366. }
  367. public static void RemoveAmplifyDefineSymbolOnBuildTargetGroup( BuildTargetGroup targetGroup )
  368. {
  369. #if UNITY_2021_2_OR_NEWER
  370. var namedBuildTarget = NamedBuildTarget.FromBuildTargetGroup( targetGroup );
  371. string currData = PlayerSettings.GetScriptingDefineSymbols( namedBuildTarget );
  372. #else
  373. string currData = PlayerSettings.GetScriptingDefineSymbolsForGroup( targetGroup );
  374. #endif
  375. if( currData.Contains( AmplifyShaderEditorDefineSymbol ) )
  376. {
  377. currData = currData.Replace( AmplifyShaderEditorDefineSymbol + ";" , "" );
  378. currData = currData.Replace( ";" + AmplifyShaderEditorDefineSymbol , "" );
  379. currData = currData.Replace( AmplifyShaderEditorDefineSymbol , "" );
  380. #if UNITY_2021_2_OR_NEWER
  381. PlayerSettings.SetScriptingDefineSymbols( namedBuildTarget, currData );
  382. #else
  383. PlayerSettings.SetScriptingDefineSymbolsForGroup( targetGroup, currData );
  384. #endif
  385. }
  386. }
  387. //Adding this attribute so scripting defining symbol can be registered right away so custom nodes using ASE ( under that symbol ) can be caught
  388. // the first time ASE opens
  389. [InitializeOnLoadMethod]
  390. public static void Init()
  391. {
  392. if( !Initialized )
  393. {
  394. Initialized = true;
  395. Preferences.Initialize();
  396. if ( Preferences.Project.DefineSymbol )
  397. SetAmplifyDefineSymbolOnBuildTargetGroup( EditorUserBuildSettings.selectedBuildTargetGroup );
  398. //Array BuildTargetGroupValues = Enum.GetValues( typeof( BuildTargetGroup ));
  399. //for ( int i = 0; i < BuildTargetGroupValues.Length; i++ )
  400. //{
  401. // if( i != 0 && i != 15 && i != 16 )
  402. // SetAmplifyDefineSymbolOnBuildTargetGroup( ( BuildTargetGroup ) BuildTargetGroupValues.GetValue( i ) );
  403. //}
  404. DefaultASEDirtyCheckId = Shader.PropertyToID( DefaultASEDirtyCheckName );
  405. dataPath = Application.dataPath.Remove( Application.dataPath.Length - 6 );
  406. //ASEFolderPath = AssetDatabase.GUIDToAssetPath( ASEFolderGUID );
  407. //ASEResourcesPath = ASEFolderPath + ASEResourcesPath;
  408. }
  409. }
  410. public static void DumpTemplateManagers()
  411. {
  412. for( int i = 0 ; i < AllOpenedWindows.Count ; i++ )
  413. {
  414. if( AllOpenedWindows[ i ].TemplatesManagerInstance != null )
  415. {
  416. Debug.Log( AllOpenedWindows[ i ].titleContent.text + ": " + AllOpenedWindows[ i ].TemplatesManagerInstance.GetInstanceID() );
  417. }
  418. }
  419. }
  420. public static TemplatesManager FirstValidTemplatesManager
  421. {
  422. get
  423. {
  424. for( int i = 0 ; i < AllOpenedWindows.Count ; i++ )
  425. {
  426. if( AllOpenedWindows[ i ].TemplatesManagerInstance != null )
  427. {
  428. return AllOpenedWindows[ i ].TemplatesManagerInstance;
  429. }
  430. }
  431. return null;
  432. }
  433. }
  434. public static void UpdateSFandRefreshWindows( AmplifyShaderFunction function )
  435. {
  436. for( int i = 0 ; i < AllOpenedWindows.Count ; i++ )
  437. {
  438. AllOpenedWindows[ i ].LateRefreshAvailableNodes();
  439. if( AllOpenedWindows[ i ].IsShaderFunctionWindow )
  440. {
  441. if( AllOpenedWindows[ i ].OpenedShaderFunction == function )
  442. {
  443. AllOpenedWindows[ i ].UpdateTabTitle();
  444. }
  445. }
  446. }
  447. }
  448. public static void UpdateIO()
  449. {
  450. int windowCount = AllOpenedWindows.Count;
  451. if( windowCount == 0 )
  452. {
  453. EditorApplication.update -= IOUtils.UpdateIO;
  454. return;
  455. }
  456. for( int i = 0 ; i < AllOpenedWindows.Count ; i++ )
  457. {
  458. if( AllOpenedWindows[ i ] == EditorWindow.focusedWindow )
  459. {
  460. UIUtils.CurrentWindow = AllOpenedWindows[ i ];
  461. }
  462. if( FunctionNodeChanged )
  463. AllOpenedWindows[ i ].CheckFunctions = true;
  464. if( AllOpenedWindows[ i ] == null )
  465. {
  466. AllOpenedWindows.RemoveAt( i );
  467. i--;
  468. }
  469. }
  470. if( FunctionNodeChanged )
  471. FunctionNodeChanged = false;
  472. }
  473. public static void Destroy()
  474. {
  475. ActiveThread = false;
  476. if( SaveInThreadMainThread != null )
  477. {
  478. SaveInThreadMainThread.Abort();
  479. SaveInThreadMainThread = null;
  480. }
  481. }
  482. public static void GetShaderName( out string shaderName , out string fullPathname , string defaultName , string customDatapath )
  483. {
  484. string currDatapath = String.IsNullOrEmpty( customDatapath ) ? Application.dataPath : customDatapath;
  485. fullPathname = EditorUtility.SaveFilePanelInProject( "Select Shader to save" , defaultName , "shader" , SaveShaderStr , currDatapath );
  486. if( !String.IsNullOrEmpty( fullPathname ) )
  487. {
  488. shaderName = fullPathname.Remove( fullPathname.Length - 7 ); // -7 remove .shader extension
  489. string[] subStr = shaderName.Split( '/' );
  490. if( subStr.Length > 0 )
  491. {
  492. shaderName = subStr[ subStr.Length - 1 ]; // Remove pathname
  493. }
  494. }
  495. else
  496. {
  497. shaderName = string.Empty;
  498. }
  499. }
  500. public static void AddTypeToString( ref string myString , string typeName )
  501. {
  502. myString += typeName;
  503. }
  504. public static void AddFieldToString( ref string myString , string fieldName , object fieldValue )
  505. {
  506. myString += FIELD_SEPARATOR + fieldName + VALUE_SEPARATOR + fieldValue;
  507. }
  508. public static void AddFieldValueToString( ref string myString , object fieldValue )
  509. {
  510. myString += FIELD_SEPARATOR + fieldValue.ToString();
  511. }
  512. public static void AddLineTerminator( ref string myString )
  513. {
  514. myString += LINE_TERMINATOR;
  515. }
  516. public static string CreateChecksum( string buffer )
  517. {
  518. SHA1 sha1 = SHA1.Create();
  519. byte[] buf = System.Text.Encoding.UTF8.GetBytes( buffer );
  520. byte[] hash = sha1.ComputeHash( buf , 0 , buf.Length );
  521. string hashstr = BitConverter.ToString( hash ).Replace( "-" , "" );
  522. return hashstr;
  523. }
  524. public static void SaveTextfileToDisk( string shaderBody , string pathName , bool addAdditionalInfo = true )
  525. {
  526. if( addAdditionalInfo )
  527. {
  528. shaderBody = string.Format( ShaderCopywriteMessage, VersionInfo.StaticToString() ) + shaderBody;
  529. // Add checksum
  530. string checksum = CreateChecksum( shaderBody );
  531. shaderBody += CHECKSUM + VALUE_SEPARATOR + checksum;
  532. }
  533. // Write to disk
  534. StreamWriter fileWriter = new StreamWriter( pathName );
  535. try
  536. {
  537. fileWriter.Write( shaderBody );
  538. }
  539. catch( Exception e )
  540. {
  541. Debug.LogException( e );
  542. }
  543. finally
  544. {
  545. fileWriter.Close();
  546. }
  547. }
  548. public static string AddAdditionalInfo( string shaderBody )
  549. {
  550. shaderBody = string.Format( ShaderCopywriteMessage, VersionInfo.StaticToString() ) + shaderBody;
  551. string checksum = CreateChecksum( shaderBody );
  552. shaderBody += CHECKSUM + VALUE_SEPARATOR + checksum;
  553. return shaderBody;
  554. }
  555. public static string LoadTextFileFromDisk( string pathName )
  556. {
  557. string result = string.Empty;
  558. if( !string.IsNullOrEmpty( pathName ) && File.Exists( pathName ) )
  559. {
  560. StreamReader fileReader = null;
  561. try
  562. {
  563. fileReader = new StreamReader( pathName );
  564. result = fileReader.ReadToEnd();
  565. }
  566. catch( Exception e )
  567. {
  568. Debug.LogException( e );
  569. }
  570. finally
  571. {
  572. if( fileReader != null )
  573. fileReader.Close();
  574. }
  575. }
  576. return result;
  577. }
  578. public static bool IsASEShader( Shader shader )
  579. {
  580. string datapath = AssetDatabase.GetAssetPath( shader );
  581. if( UIUtils.IsUnityNativeShader( datapath ) )
  582. {
  583. return false;
  584. }
  585. string buffer = LoadTextFileFromDisk( datapath );
  586. if( String.IsNullOrEmpty( buffer ) || !IOUtils.HasValidShaderBody( ref buffer ) )
  587. {
  588. return false;
  589. }
  590. return true;
  591. }
  592. public static bool IsShaderFunction( string functionInfo )
  593. {
  594. string buffer = functionInfo;
  595. if( String.IsNullOrEmpty( buffer ) || !IOUtils.HasValidShaderBody( ref buffer ) )
  596. {
  597. return false;
  598. }
  599. return true;
  600. }
  601. public static bool HasValidShaderBody( ref string shaderBody )
  602. {
  603. int shaderBodyBeginId = shaderBody.IndexOf( ShaderBodyBegin );
  604. if( shaderBodyBeginId > -1 )
  605. {
  606. int shaderBodyEndId = shaderBody.IndexOf( ShaderBodyEnd );
  607. return ( shaderBodyEndId > -1 && shaderBodyEndId > shaderBodyBeginId );
  608. }
  609. return false;
  610. }
  611. public static int[] AllIndexesOf( this string str , string substr , bool ignoreCase = false )
  612. {
  613. if( string.IsNullOrEmpty( str ) || string.IsNullOrEmpty( substr ) )
  614. {
  615. throw new ArgumentException( "String or substring is not specified." );
  616. }
  617. List<int> indexes = new List<int>();
  618. int index = 0;
  619. while( ( index = str.IndexOf( substr , index , ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal ) ) != -1 )
  620. {
  621. indexes.Add( index++ );
  622. }
  623. return indexes.ToArray();
  624. }
  625. public static void AddFunctionHeader( ref string function , string header )
  626. {
  627. function += string.Format( "\t\t{0}\n\t\t{{\n", header );
  628. }
  629. public static void AddSingleLineFunction( ref string function , string header )
  630. {
  631. function += string.Format( "\t\t{0}", header );
  632. }
  633. public static void AddFunctionLine( ref string function , string line )
  634. {
  635. function += string.Format( "\t\t\t{0}\n", line );
  636. }
  637. public static void CloseFunctionBody( ref string function )
  638. {
  639. function += "\t\t}\n";
  640. }
  641. public static string CreateFullFunction( string header , params string[] functionLines )
  642. {
  643. string result = string.Empty;
  644. AddFunctionHeader( ref result , header );
  645. for( int i = 0 ; i > functionLines.Length ; i++ )
  646. {
  647. AddFunctionLine( ref result , functionLines[ i ] );
  648. }
  649. CloseFunctionBody( ref result );
  650. return result;
  651. }
  652. public static string CreateCodeComments( bool forceForwardSlash , params string[] comments )
  653. {
  654. string finalComment = string.Empty;
  655. if( comments.Length == 1 )
  656. {
  657. finalComment = "//" + comments[ 0 ];
  658. }
  659. else
  660. {
  661. if( forceForwardSlash )
  662. {
  663. for( int i = 0 ; i < comments.Length ; i++ )
  664. {
  665. finalComment += "//" + comments[ i ];
  666. if( i < comments.Length - 1 )
  667. {
  668. finalComment += "\n\t\t\t";
  669. }
  670. }
  671. }
  672. else
  673. {
  674. finalComment = "/*";
  675. for( int i = 0 ; i < comments.Length ; i++ )
  676. {
  677. if( i != 0 )
  678. finalComment += "\t\t\t";
  679. finalComment += comments[ i ];
  680. if( i < comments.Length - 1 )
  681. finalComment += "\n";
  682. }
  683. finalComment += "*/";
  684. }
  685. }
  686. return finalComment;
  687. }
  688. public static string GetUVChannelDeclaration( string uvName , int channelId , int set )
  689. {
  690. string uvSetStr = ( set == 0 ) ? "uv" : "uv" + Constants.AvailableUVSetsStr[ set ];
  691. return "float2 " + uvSetStr + uvName /*+ " : TEXCOORD" + channelId*/;
  692. }
  693. public static string GetUVChannelName( string uvName , int set )
  694. {
  695. string uvSetStr = ( set == 0 ) ? "uv" : "uv" + Constants.AvailableUVSetsStr[ set ];
  696. return uvSetStr + uvName;
  697. }
  698. public static string GetVertexUVChannelName( int set )
  699. {
  700. string uvSetStr = ( set == 0 ) ? "texcoord" : ( "texcoord" + set.ToString() );
  701. return uvSetStr;
  702. }
  703. //Floatify adds a .0 to the number as soon operarions with floats require that
  704. // if value % 1 != 0 it has decimal numbers
  705. // The regex checks if number is something like 4e+07 which cannot be "floatified"
  706. private const string CheckFloatifyPatt = "[eE][+-]";
  707. public static string Floatify( float value )
  708. {
  709. string finalValue = value.ToString();
  710. return ( value % 1 ) != 0 ? finalValue :
  711. ( Regex.IsMatch( finalValue , CheckFloatifyPatt ) ? finalValue : finalValue + FloatifyStr );
  712. }
  713. public static string Vector2ToString( Vector2 data )
  714. {
  715. return data.x.ToString() + VECTOR_SEPARATOR + data.y.ToString();
  716. }
  717. public static string Vector3ToString( Vector3 data )
  718. {
  719. return data.x.ToString() + VECTOR_SEPARATOR + data.y.ToString() + VECTOR_SEPARATOR + data.z.ToString();
  720. }
  721. public static string Vector4ToString( Vector4 data )
  722. {
  723. return data.x.ToString() + VECTOR_SEPARATOR + data.y.ToString() + VECTOR_SEPARATOR + data.z.ToString() + VECTOR_SEPARATOR + data.w.ToString();
  724. }
  725. public static string ColorToString( Color data )
  726. {
  727. return data.r.ToString() + VECTOR_SEPARATOR + data.g.ToString() + VECTOR_SEPARATOR + data.b.ToString() + VECTOR_SEPARATOR + data.a.ToString();
  728. }
  729. public static string Matrix3x3ToString( Matrix4x4 matrix )
  730. {
  731. return matrix[ 0 , 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 0 , 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 0 , 2 ].ToString() + IOUtils.VECTOR_SEPARATOR +
  732. matrix[ 1 , 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 1 , 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 1 , 2 ].ToString() + IOUtils.VECTOR_SEPARATOR +
  733. matrix[ 2 , 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 2 , 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 2 , 2 ].ToString();
  734. }
  735. public static string Matrix4x4ToString( Matrix4x4 matrix )
  736. {
  737. return matrix[ 0 , 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 0 , 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 0 , 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 0 , 3 ].ToString() + IOUtils.VECTOR_SEPARATOR +
  738. matrix[ 1 , 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 1 , 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 1 , 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 1 , 3 ].ToString() + IOUtils.VECTOR_SEPARATOR +
  739. matrix[ 2 , 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 2 , 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 2 , 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 2 , 3 ].ToString() + IOUtils.VECTOR_SEPARATOR +
  740. matrix[ 3 , 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 3 , 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 3 , 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 3 , 3 ].ToString();
  741. }
  742. public static Vector2 StringToVector2( string data )
  743. {
  744. string[] parsedData = data.Split( VECTOR_SEPARATOR );
  745. if( parsedData.Length >= 2 )
  746. {
  747. return new Vector2( Convert.ToSingle( parsedData[ 0 ] ) ,
  748. Convert.ToSingle( parsedData[ 1 ] ) );
  749. }
  750. return Vector2.zero;
  751. }
  752. public static Vector3 StringToVector3( string data )
  753. {
  754. string[] parsedData = data.Split( VECTOR_SEPARATOR );
  755. if( parsedData.Length >= 3 )
  756. {
  757. return new Vector3( Convert.ToSingle( parsedData[ 0 ] ) ,
  758. Convert.ToSingle( parsedData[ 1 ] ) ,
  759. Convert.ToSingle( parsedData[ 2 ] ) );
  760. }
  761. return Vector3.zero;
  762. }
  763. public static Vector4 StringToVector4( string data )
  764. {
  765. string[] parsedData = data.Split( VECTOR_SEPARATOR );
  766. if( parsedData.Length >= 4 )
  767. {
  768. return new Vector4( Convert.ToSingle( parsedData[ 0 ] ) ,
  769. Convert.ToSingle( parsedData[ 1 ] ) ,
  770. Convert.ToSingle( parsedData[ 2 ] ) ,
  771. Convert.ToSingle( parsedData[ 3 ] ) );
  772. }
  773. return Vector4.zero;
  774. }
  775. public static Color StringToColor( string data )
  776. {
  777. string[] parsedData = data.Split( VECTOR_SEPARATOR );
  778. if( parsedData.Length >= 4 )
  779. {
  780. return new Color( Convert.ToSingle( parsedData[ 0 ] ) ,
  781. Convert.ToSingle( parsedData[ 1 ] ) ,
  782. Convert.ToSingle( parsedData[ 2 ] ) ,
  783. Convert.ToSingle( parsedData[ 3 ] ) );
  784. }
  785. return Color.white;
  786. }
  787. public static Matrix4x4 StringToMatrix3x3( string data )
  788. {
  789. string[] parsedData = data.Split( VECTOR_SEPARATOR );
  790. if( parsedData.Length == 9 )
  791. {
  792. Matrix4x4 matrix = new Matrix4x4();
  793. matrix[ 0 , 0 ] = Convert.ToSingle( parsedData[ 0 ] );
  794. matrix[ 0 , 1 ] = Convert.ToSingle( parsedData[ 1 ] );
  795. matrix[ 0 , 2 ] = Convert.ToSingle( parsedData[ 2 ] );
  796. matrix[ 1 , 0 ] = Convert.ToSingle( parsedData[ 3 ] );
  797. matrix[ 1 , 1 ] = Convert.ToSingle( parsedData[ 4 ] );
  798. matrix[ 1 , 2 ] = Convert.ToSingle( parsedData[ 5 ] );
  799. matrix[ 2 , 0 ] = Convert.ToSingle( parsedData[ 6 ] );
  800. matrix[ 2 , 1 ] = Convert.ToSingle( parsedData[ 7 ] );
  801. matrix[ 2 , 2 ] = Convert.ToSingle( parsedData[ 8 ] );
  802. return matrix;
  803. }
  804. return Matrix4x4.identity;
  805. }
  806. public static Matrix4x4 StringToMatrix4x4( string data )
  807. {
  808. string[] parsedData = data.Split( VECTOR_SEPARATOR );
  809. if( parsedData.Length == 16 )
  810. {
  811. Matrix4x4 matrix = new Matrix4x4();
  812. matrix[ 0 , 0 ] = Convert.ToSingle( parsedData[ 0 ] );
  813. matrix[ 0 , 1 ] = Convert.ToSingle( parsedData[ 1 ] );
  814. matrix[ 0 , 2 ] = Convert.ToSingle( parsedData[ 2 ] );
  815. matrix[ 0 , 3 ] = Convert.ToSingle( parsedData[ 3 ] );
  816. matrix[ 1 , 0 ] = Convert.ToSingle( parsedData[ 4 ] );
  817. matrix[ 1 , 1 ] = Convert.ToSingle( parsedData[ 5 ] );
  818. matrix[ 1 , 2 ] = Convert.ToSingle( parsedData[ 6 ] );
  819. matrix[ 1 , 3 ] = Convert.ToSingle( parsedData[ 7 ] );
  820. matrix[ 2 , 0 ] = Convert.ToSingle( parsedData[ 8 ] );
  821. matrix[ 2 , 1 ] = Convert.ToSingle( parsedData[ 9 ] );
  822. matrix[ 2 , 2 ] = Convert.ToSingle( parsedData[ 10 ] );
  823. matrix[ 2 , 3 ] = Convert.ToSingle( parsedData[ 11 ] );
  824. matrix[ 3 , 0 ] = Convert.ToSingle( parsedData[ 12 ] );
  825. matrix[ 3 , 1 ] = Convert.ToSingle( parsedData[ 13 ] );
  826. matrix[ 3 , 2 ] = Convert.ToSingle( parsedData[ 14 ] );
  827. matrix[ 3 , 3 ] = Convert.ToSingle( parsedData[ 15 ] );
  828. return matrix;
  829. }
  830. return Matrix4x4.identity;
  831. }
  832. public static void SaveTextureToDisk( Texture2D tex , string pathname )
  833. {
  834. byte[] rawData = tex.GetRawTextureData();
  835. Texture2D newTex = new Texture2D( tex.width , tex.height , tex.format , tex.mipmapCount > 1 , false );
  836. newTex.LoadRawTextureData( rawData );
  837. newTex.Apply();
  838. byte[] pngData = newTex.EncodeToPNG();
  839. File.WriteAllBytes( pathname , pngData );
  840. }
  841. //public static void SaveObjToList( string newObj )
  842. //{
  843. // Debug.Log( UIUtils.CurrentWindow.Lastpath );
  844. // UIUtils.CurrentWindow.Lastpath = newObj;
  845. // string lastOpenedObj = EditorPrefs.GetString( IOUtils.LAST_OPENED_OBJ_ID );
  846. // string[] allLocations = lastOpenedObj.Split( ':' );
  847. // string lastLocation = allLocations[ allLocations.Length - 1 ];
  848. // string resave = string.Empty;
  849. // for ( int i = 0; i < allLocations.Length; i++ )
  850. // {
  851. // if ( string.IsNullOrEmpty( allLocations[ i ] ) )
  852. // continue;
  853. // resave += allLocations[ i ];
  854. // resave += ":";
  855. // }
  856. // resave += newObj;
  857. // EditorPrefs.SetString( IOUtils.LAST_OPENED_OBJ_ID, resave );
  858. //}
  859. //public static void DeleteObjFromList( string newObj )
  860. //{
  861. // string lastOpenedObj = EditorPrefs.GetString( IOUtils.LAST_OPENED_OBJ_ID );
  862. // string[] allLocations = lastOpenedObj.Split( ':' );
  863. // string resave = string.Empty;
  864. // for ( int i = 0; i < allLocations.Length; i++ )
  865. // {
  866. // if ( string.IsNullOrEmpty( allLocations[ i ] ) || newObj.Equals( allLocations[ i ] ) )
  867. // continue;
  868. // resave += allLocations[ i ];
  869. // if ( i < allLocations.Length - 1 )
  870. // resave += ":";
  871. // }
  872. // EditorPrefs.SetString( IOUtils.LAST_OPENED_OBJ_ID, resave );
  873. //}
  874. // Polynomial: 0xedb88320
  875. static readonly uint[] crc32_tab = {
  876. 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
  877. 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
  878. 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
  879. 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
  880. 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
  881. 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
  882. 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
  883. 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
  884. 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
  885. 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
  886. 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
  887. 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
  888. 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
  889. 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
  890. 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
  891. 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
  892. 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
  893. 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
  894. 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
  895. 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
  896. 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
  897. 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
  898. 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
  899. 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
  900. 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
  901. 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
  902. 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
  903. 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
  904. 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
  905. 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
  906. 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
  907. 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
  908. 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
  909. 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
  910. 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
  911. 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
  912. 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
  913. 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
  914. 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
  915. 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
  916. 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
  917. 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
  918. 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
  919. };
  920. public static uint CRC32( byte[] buf, uint crc = 0 )
  921. {
  922. uint i = 0;
  923. uint size = ( uint )buf.Length;
  924. crc = crc ^ 0xFFFFFFFF;
  925. while ( size-- > 0 )
  926. {
  927. crc = crc32_tab[ ( crc ^ buf[ i++ ] ) & 0xFF ] ^ ( crc >> 8 );
  928. }
  929. return crc ^ 0xFFFFFFFF;
  930. }
  931. }
  932. }