源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
6.1 KiB

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using LuaInterface;
  5. using System;
  6. using System.IO;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. namespace LuaFramework
  10. {
  11. public static class FileTools
  12. {
  13. private static List<string> paths = new List<string>();
  14. private static StringBuilder files = new StringBuilder();
  15. private static List<string> str = new List<string>();
  16. private static List<string> cfg_names_list = new List<string>();
  17. private static List<string> fina_list = new List<string>();
  18. //返回的是内容组成的字符串
  19. public static string GetPathFiles(string path, string pre = "", string match_str = "")
  20. {
  21. paths.Clear();
  22. files.Clear();
  23. if (Directory.Exists(path))
  24. {
  25. Recursive(path, pre, match_str);
  26. }
  27. else
  28. {
  29. files.Append("null");
  30. }
  31. return files.ToString();
  32. }
  33. //判断路径是否存在
  34. public static bool CheckPathExists(string path)
  35. {
  36. bool has_path = false;
  37. has_path = Directory.Exists(path);
  38. return has_path;
  39. }
  40. /// 遍历目录及其子目录 pre是只有包含这个符号的才会添加
  41. private static void Recursive(string path, string pre, string match_str)
  42. {
  43. string[] names = Directory.GetFiles(path);
  44. string[] dirs = Directory.GetDirectories(path);
  45. foreach (string filename in names)
  46. {
  47. string ext = Path.GetExtension(filename);
  48. if (!ext.Equals(match_str)) continue;
  49. if (pre != "" && !filename.Contains(pre)) continue;
  50. string real_name = filename.Replace('\\', '/');
  51. files.Append(real_name);
  52. files.Append("\n");
  53. }
  54. foreach (string dir in dirs)
  55. {
  56. paths.Add(dir.Replace('\\', '/'));
  57. Recursive(dir, pre, match_str);
  58. }
  59. }
  60. public static void OutPutValue(string file_name, string value_str)
  61. {
  62. str.Clear();
  63. if (!string.IsNullOrEmpty(value_str))
  64. {
  65. string[] str_list = value_str.Split('&');
  66. foreach (string s in str_list)
  67. {
  68. str.Add(s);
  69. }
  70. if (str.Count >= 0)
  71. {
  72. try
  73. {
  74. //string root_str = Application.dataPath;
  75. //if (!Application.isEditor)
  76. //{
  77. // root_str = root_str.Replace("ClientApp_Data", "");
  78. // root_str = root_str.Replace("localTest", "");
  79. // root_str = root_str.Replace("app", "");
  80. //}
  81. //root_str = root_str.Replace("Assets", "");
  82. //string target_path = root_str + "/" + file_name + ".csv";
  83. string target_path = Application.dataPath.Replace("Assets", "") + "/" + file_name + ".csv";
  84. File.WriteAllLines(target_path, str.ToArray(), Encoding.GetEncoding("GB2312"));
  85. }
  86. catch (Exception e)
  87. {
  88. throw;
  89. }
  90. }
  91. }
  92. }
  93. public static void MatchCfg(string path, string pre = "", string match_str = "", string Config = "")
  94. {
  95. paths.Clear();
  96. files.Clear();
  97. cfg_names_list.Clear();
  98. fina_list.Clear();
  99. byte[] bytes = new byte[1024 * 1024];
  100. string root_path = Application.dataPath;
  101. if (Directory.Exists(path))
  102. {
  103. Recursive(path, pre, match_str);
  104. }
  105. else
  106. {
  107. files.Append("null");
  108. }
  109. if (files.Length > 0)
  110. {
  111. string str = files.ToString();
  112. string[] arr = str.Split('\n');
  113. for (int i = 0; i < arr.Length; i++)
  114. {
  115. string cfg_path = arr[i];
  116. string[] arr2 = Regex.Split(cfg_path, "Lua/", RegexOptions.IgnoreCase);
  117. string s = arr2[arr2.Length - 1].Replace(".lua", "").Replace('/', '.');
  118. cfg_names_list.Add(s);
  119. }
  120. }
  121. Config = Config + ".lua";
  122. if (File.Exists(Config))
  123. {
  124. FileStream fs = new FileStream(Config, FileMode.Open);
  125. int count = (int)fs.Length;
  126. fs.Read(bytes, 0, count);
  127. string str = Encoding.UTF8.GetString(bytes);
  128. if (!string.IsNullOrEmpty(str))
  129. {
  130. string title = "是否引用配置,lua名,配置文件名&";
  131. fina_list.Add(title);
  132. for (int i = 0; i < cfg_names_list.Count; i++)
  133. {
  134. bool use_cfg = str.Contains(cfg_names_list[i]);
  135. string desc = "";
  136. if (use_cfg == false)
  137. {
  138. desc = "未引用此配置";
  139. }
  140. string[] arr3 = cfg_names_list[i].Split('.');
  141. string file_name = arr3[arr3.Length - 1];
  142. string name = cfg_names_list[i];
  143. string fina_str = desc + "," + file_name + "," + name + "&";
  144. fina_list.Add(fina_str);
  145. }
  146. }
  147. fs.Close();
  148. }
  149. if (fina_list.Count > 0)
  150. {
  151. string target_path = Application.dataPath.Replace("Assets", "") + "/" + "配置关系表" + ".csv";
  152. File.WriteAllLines(target_path, fina_list.ToArray(), Encoding.GetEncoding("GB2312"));
  153. Debug.Log("配置关系表生成完成");
  154. }
  155. }
  156. }
  157. }