TestString.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.IO;
  2. using System.Text.RegularExpressions;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using XGame.Editor.Asset;
  6. namespace FL.Editor.Tests
  7. {
  8. public static class TestString
  9. {
  10. [MenuItem("Test/String/DeSerialization")]
  11. public static void DeSerialization()
  12. {
  13. var filePath = PathDefine.DefaultOutputPath + "/config.bytes";
  14. if (!File.Exists(filePath))
  15. {
  16. return;
  17. }
  18. //FileStream fileStream = File.OpenRead(filePath);
  19. var text = File.ReadAllText(filePath);
  20. const char openBracket = '[';
  21. const char closeBracket = ']';
  22. const char openBrace = '{';
  23. const char closeBrace = '}';
  24. Regex regex = new Regex(@"Config/(.+?)\.json");
  25. var result = regex.Matches(text);
  26. var regCount = result.Count;
  27. Debug.Log($"Regex count:{regCount}");
  28. for(var regIdx = 0; regIdx < regCount; regIdx++)
  29. {
  30. var match = result[regIdx];
  31. Debug.Log($"match:{match.Value} index:{match.Index} value1:{match.Groups[1].Value}");
  32. var startIndex = match.Index + match.Value.Length;
  33. var openBracketIdx = text.IndexOf(openBracket, startIndex);
  34. var openBraceIdx = text.IndexOf(openBrace, startIndex);
  35. //两个值取小的
  36. var openIdx = openBracketIdx < openBraceIdx ? openBracketIdx : openBraceIdx;
  37. var closeChar = openBracketIdx < openBraceIdx ? closeBracket : closeBrace;
  38. int closeIdx;
  39. if (regIdx < regCount - 1)
  40. { //
  41. var nextMatch = result[regIdx + 1];
  42. closeIdx = text.IndexOfReverse(closeChar, nextMatch.Index - 1);
  43. }
  44. else
  45. { //最后一个json
  46. closeIdx = text.IndexOfReverse(closeChar);
  47. }
  48. if (closeIdx <= openIdx)
  49. {
  50. Debug.LogError($"字符串匹配错误: Match:{match} Index:{match.Index} Open:{openIdx} Close:{closeIdx}");
  51. continue;
  52. }
  53. var json = text.Substring(openIdx, closeIdx - openIdx + 1);
  54. var jsonPath = $"{PathDefine.DefaultOutputPath}/{match.Value}";
  55. var jsonDir = Path.GetDirectoryName(jsonPath);
  56. if (!Directory.Exists(jsonDir))
  57. {
  58. Directory.CreateDirectory(jsonDir);
  59. }
  60. File.WriteAllText(jsonPath, json);
  61. }
  62. }
  63. /// <summary>
  64. /// 反向查找指定字符的索引
  65. /// </summary>
  66. /// <param name="text"></param>
  67. /// <param name="char"></param>
  68. /// <param name="startIndex">小于零或者大于字符串长度,则从字符串最后一个字符开始查找</param>
  69. /// <returns></returns>
  70. private static int IndexOfReverse(this string text, char @char, int startIndex = -1)
  71. {
  72. if (startIndex < 0 || startIndex > text.Length - 1)
  73. {
  74. startIndex = text.Length - 1;
  75. }
  76. for(var index = startIndex; index >= 0; index--)
  77. {
  78. if (@char == text[index])
  79. {
  80. return index;
  81. }
  82. }
  83. return -1;
  84. }
  85. }
  86. }