Graphic.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * 魔力宝贝图档解析脚本 - CGTool
  3. *
  4. * @Author HonorLee (dev@honorlee.me)
  5. * @Version 1.0 (2023-04-15)
  6. * @License GPL-3.0
  7. *
  8. * Graphic.cs 图档解析类
  9. */
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using UnityEngine;
  15. namespace CrossgateToolkit
  16. {
  17. //图档类
  18. public static class Graphic
  19. {
  20. private class FilePair
  21. {
  22. public FileInfo InfoFile;
  23. public FileInfo DataFile;
  24. public string Version;
  25. }
  26. // 临时文件列表缓存
  27. private static List<FilePair> _graphicFilePairs = new List<FilePair>();
  28. private static List<FilePair> _animeFilePairs = new List<FilePair>();
  29. // 高版本号标识
  30. // public static readonly Dictionary<string,bool> Flag_HighVersion = new Dictionary<string, bool>();
  31. // 初始化
  32. public static void Init()
  33. {
  34. // 解析Bin文件目录结构
  35. if(!Directory.Exists(CGTool.PATH.BIN)) throw new Exception("图档目录不存在,请检查CGTool中是否配置相应PATH路径");
  36. //float time = Time.realtimeSinceStartup;
  37. // 整理目录结构,生成对应待处理文件列表
  38. List<DirectoryInfo> _directorys = new List<DirectoryInfo>();
  39. _directorys.Add(new DirectoryInfo(CGTool.PATH.BIN));
  40. _directorys.AddRange(new DirectoryInfo(CGTool.PATH.BIN).GetDirectories().OrderBy(d => d.Name).ToList());
  41. foreach (DirectoryInfo directory in _directorys)
  42. {
  43. AnalysisDirectory(directory);
  44. }
  45. Debug.Log("[CGTool] 图档资源查找完毕,共找到: (" + _graphicFilePairs.Count + ") 个图档文件, (" + _animeFilePairs.Count +
  46. ") 个动画文件");
  47. // 预加载 GraphicInfo
  48. foreach (FilePair graphicFilePair in _graphicFilePairs)
  49. {
  50. GraphicInfo.Init(graphicFilePair.Version,graphicFilePair.InfoFile, graphicFilePair.DataFile);
  51. }
  52. // 预加载 Anime
  53. foreach (FilePair animeFilePair in _animeFilePairs)
  54. {
  55. Anime.Init(animeFilePair.Version, animeFilePair.InfoFile, animeFilePair.DataFile);
  56. }
  57. //Debug.Log("[CGTool] 图档资源加载完毕,耗时: " + (Time.realtimeSinceStartup - time) + "s");
  58. }
  59. // 版本号分析
  60. private class VersionInfo
  61. {
  62. public string Version;
  63. public int VersionCode;
  64. public string FullVersion;
  65. }
  66. private static VersionInfo AnalysisVersion(string prefix,FileInfo fileinfo)
  67. {
  68. VersionInfo versionInfo = new VersionInfo();
  69. int suffixIndex = fileinfo.Name.LastIndexOf('.');
  70. // 解析版本号
  71. string filename = fileinfo.Name.Substring(0, suffixIndex);
  72. versionInfo.FullVersion = filename.Substring(prefix.Length);
  73. if (string.IsNullOrEmpty(versionInfo.FullVersion))
  74. {
  75. string parentName = fileinfo.Directory != null ? fileinfo.Directory.Name : "";
  76. versionInfo.FullVersion = "";
  77. versionInfo.Version = parentName.ToUpper();
  78. }
  79. else
  80. {
  81. string[] versionArr = versionInfo.FullVersion.Split('_');
  82. if (String.IsNullOrEmpty(versionArr[0]))
  83. {
  84. versionArr = versionArr.Skip(1).ToArray();
  85. }
  86. if (int.TryParse(versionArr[0], out int code))
  87. {
  88. versionInfo.VersionCode = code;
  89. }
  90. else
  91. {
  92. versionInfo.Version = versionArr[0].ToUpper();
  93. if(int.TryParse(versionArr[^1], out int vcode))
  94. {
  95. versionInfo.VersionCode = vcode;
  96. }
  97. }
  98. }
  99. return versionInfo;
  100. }
  101. // 分析目录,并获取对应配对文件
  102. private static void AnalysisDirectory(DirectoryInfo directoryInfo)
  103. {
  104. // Debug.Log("[CGTool] 开始分析目录: " + directoryInfo.FullName);
  105. string Version = directoryInfo.Name;
  106. FileInfo[] fileInfos = directoryInfo.GetFiles();
  107. fileInfos = fileInfos.OrderBy(f => f.Name).ToArray();
  108. foreach (FileInfo fileInfo in fileInfos)
  109. {
  110. if (!fileInfo.Name.EndsWith(".bin",StringComparison.OrdinalIgnoreCase)) continue;
  111. if (fileInfo.Name.StartsWith("graphicinfo", StringComparison.OrdinalIgnoreCase))
  112. {
  113. // 找到GraphicInfo文件
  114. // 获取对应版本号 GraphicInfo(*).bin
  115. VersionInfo versionInfo = AnalysisVersion("graphicinfo", fileInfo);
  116. // 判断是否存在对应graphic文件,忽略大小写
  117. string graphicFileName = "graphic" + versionInfo.FullVersion + ".bin";
  118. FileInfo graphicFileInfo = GetFileInfoByName(fileInfo.Directory, graphicFileName);
  119. if (!graphicFileInfo.Exists)
  120. {
  121. throw new Exception("找不到对应的图档文件: " + fileInfo.FullName);
  122. }
  123. FilePair filePair = new FilePair()
  124. {
  125. InfoFile = fileInfo,
  126. DataFile = graphicFileInfo,
  127. Version = versionInfo.Version ?? Version
  128. };
  129. _graphicFilePairs.Add(filePair);
  130. }else if (fileInfo.Name.StartsWith("animeinfo", StringComparison.OrdinalIgnoreCase))
  131. {
  132. // 找到AnimeInfo文件
  133. // 获取对应版本号 AnimeInfo(*).bin
  134. VersionInfo versionInfo = AnalysisVersion("animeinfo", fileInfo);
  135. // 判断是否存在对应anime文件
  136. string animeFileName = "anime" + versionInfo.FullVersion + ".bin";
  137. FileInfo animeFileInfo = GetFileInfoByName(fileInfo.Directory, animeFileName);
  138. if (!animeFileInfo.Exists)
  139. {
  140. throw new Exception("找不到对应的动画文件: " + fileInfo.FullName);
  141. }
  142. FilePair filePair = new FilePair()
  143. {
  144. InfoFile = fileInfo,
  145. DataFile = animeFileInfo,
  146. Version = versionInfo.Version ?? Version
  147. };
  148. _animeFilePairs.Add(filePair);
  149. }
  150. }
  151. }
  152. // 忽略大小写获取对应文件
  153. private static FileInfo GetFileInfoByName(DirectoryInfo directoryInfo,string fileName)
  154. {
  155. FileInfo[] fileInfos = directoryInfo.GetFiles();
  156. foreach (FileInfo fileInfo in fileInfos)
  157. {
  158. if (fileInfo.Name.Equals(fileName, StringComparison.OrdinalIgnoreCase))
  159. {
  160. return fileInfo;
  161. }
  162. }
  163. return null;
  164. }
  165. // 获取图档数据
  166. public static GraphicDetail GetGraphicDetail(uint serial,int palet = 0,bool linerFilter = false)
  167. {
  168. GraphicInfoData graphicInfoData = GraphicInfo.GetGraphicInfoData(serial);
  169. if (graphicInfoData == null) return null;
  170. return GraphicData.GetGraphicDetail(graphicInfoData, palet, -1, linerFilter);
  171. }
  172. // 获取图档数据
  173. public static GraphicDetail GetGraphicDetailByIndex(string Version,uint index,int palet = 0,bool linerFilter = false)
  174. {
  175. GraphicInfoData graphicInfoData = GraphicInfo.GetGraphicInfoDataByIndex(Version, index);
  176. if (graphicInfoData == null) return null;
  177. return GraphicData.GetGraphicDetail(graphicInfoData, palet, -1, linerFilter);
  178. }
  179. }
  180. }