Graphic.cs 7.9 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. // 整理目录结构,生成对应待处理文件列表
  37. List<DirectoryInfo> _directorys = new List<DirectoryInfo>();
  38. _directorys.Add(new DirectoryInfo(CGTool.PATH.BIN));
  39. _directorys.AddRange(new DirectoryInfo(CGTool.PATH.BIN).GetDirectories().OrderBy(d => d.Name).ToList());
  40. foreach (DirectoryInfo directory in _directorys)
  41. {
  42. AnalysisDirectory(directory);
  43. }
  44. Debug.Log("[CGTool] 图档资源查找完毕,共找到: (" + _graphicFilePairs.Count + ") 个图档文件, (" + _animeFilePairs.Count +
  45. ") 个动画文件");
  46. // 预加载 GraphicInfo
  47. foreach (FilePair graphicFilePair in _graphicFilePairs)
  48. {
  49. GraphicInfo.Init(graphicFilePair.Version,graphicFilePair.InfoFile, graphicFilePair.DataFile);
  50. }
  51. // 预加载 Anime
  52. foreach (FilePair animeFilePair in _animeFilePairs)
  53. {
  54. Anime.Init(animeFilePair.Version, animeFilePair.InfoFile, animeFilePair.DataFile);
  55. }
  56. }
  57. // 版本号分析
  58. private class VersionInfo
  59. {
  60. public string Version;
  61. public int VersionCode;
  62. public string FullVersion;
  63. }
  64. private static VersionInfo AnalysisVersion(string prefix,FileInfo fileinfo)
  65. {
  66. VersionInfo versionInfo = new VersionInfo();
  67. int suffixIndex = fileinfo.Name.LastIndexOf('.');
  68. // 解析版本号
  69. string filename = fileinfo.Name.Substring(0, suffixIndex);
  70. versionInfo.FullVersion = filename.Substring(prefix.Length);
  71. if (string.IsNullOrEmpty(versionInfo.FullVersion))
  72. {
  73. string parentName = fileinfo.Directory != null ? fileinfo.Directory.Name : "";
  74. versionInfo.FullVersion = "";
  75. versionInfo.Version = parentName.ToUpper();
  76. }
  77. else
  78. {
  79. string[] versionArr = versionInfo.FullVersion.Split('_');
  80. if (String.IsNullOrEmpty(versionArr[0]))
  81. {
  82. versionArr = versionArr.Skip(1).ToArray();
  83. }
  84. if (int.TryParse(versionArr[0], out int code))
  85. {
  86. versionInfo.VersionCode = code;
  87. }
  88. else
  89. {
  90. versionInfo.Version = versionArr[0].ToUpper();
  91. if(int.TryParse(versionArr[^1], out int vcode))
  92. {
  93. versionInfo.VersionCode = vcode;
  94. }
  95. }
  96. }
  97. return versionInfo;
  98. }
  99. // 分析目录,并获取对应配对文件
  100. private static void AnalysisDirectory(DirectoryInfo directoryInfo)
  101. {
  102. // Debug.Log("[CGTool] 开始分析目录: " + directoryInfo.FullName);
  103. string Version = directoryInfo.Name;
  104. FileInfo[] fileInfos = directoryInfo.GetFiles();
  105. fileInfos = fileInfos.OrderBy(f => f.Name).ToArray();
  106. foreach (FileInfo fileInfo in fileInfos)
  107. {
  108. if (!fileInfo.Name.EndsWith(".bin",StringComparison.OrdinalIgnoreCase)) continue;
  109. if (fileInfo.Name.StartsWith("graphicinfo", StringComparison.OrdinalIgnoreCase))
  110. {
  111. // 找到GraphicInfo文件
  112. // 获取对应版本号 GraphicInfo(*).bin
  113. VersionInfo versionInfo = AnalysisVersion("graphicinfo", fileInfo);
  114. // 判断是否存在对应graphic文件,忽略大小写
  115. string graphicFileName = "graphic" + versionInfo.FullVersion + ".bin";
  116. FileInfo graphicFileInfo = GetFileInfoByName(fileInfo.Directory, graphicFileName);
  117. if (!graphicFileInfo.Exists)
  118. {
  119. throw new Exception("找不到对应的图档文件: " + fileInfo.FullName);
  120. }
  121. FilePair filePair = new FilePair()
  122. {
  123. InfoFile = fileInfo,
  124. DataFile = graphicFileInfo,
  125. Version = versionInfo.Version ?? Version
  126. };
  127. _graphicFilePairs.Add(filePair);
  128. }else if (fileInfo.Name.StartsWith("animeinfo", StringComparison.OrdinalIgnoreCase))
  129. {
  130. // 找到AnimeInfo文件
  131. // 获取对应版本号 AnimeInfo(*).bin
  132. VersionInfo versionInfo = AnalysisVersion("animeinfo", fileInfo);
  133. // 判断是否存在对应anime文件
  134. string animeFileName = "anime" + versionInfo.FullVersion + ".bin";
  135. FileInfo animeFileInfo = GetFileInfoByName(fileInfo.Directory, animeFileName);
  136. if (!animeFileInfo.Exists)
  137. {
  138. throw new Exception("找不到对应的动画文件: " + fileInfo.FullName);
  139. }
  140. FilePair filePair = new FilePair()
  141. {
  142. InfoFile = fileInfo,
  143. DataFile = animeFileInfo,
  144. Version = versionInfo.Version ?? Version
  145. };
  146. _animeFilePairs.Add(filePair);
  147. }
  148. }
  149. }
  150. // 忽略大小写获取对应文件
  151. private static FileInfo GetFileInfoByName(DirectoryInfo directoryInfo,string fileName)
  152. {
  153. FileInfo[] fileInfos = directoryInfo.GetFiles();
  154. foreach (FileInfo fileInfo in fileInfos)
  155. {
  156. if (fileInfo.Name.Equals(fileName, StringComparison.OrdinalIgnoreCase))
  157. {
  158. return fileInfo;
  159. }
  160. }
  161. return null;
  162. }
  163. // 获取图档数据
  164. public static GraphicDetail GetGraphicDetail(uint serial,int palet = 0,bool linerFilter = false)
  165. {
  166. GraphicInfoData graphicInfoData = GraphicInfo.GetGraphicInfoData(serial);
  167. if (graphicInfoData == null) return null;
  168. return GraphicData.GetGraphicDetail(graphicInfoData, palet, 0, linerFilter);
  169. }
  170. // 获取图档数据
  171. public static GraphicDetail GetGraphicDetailByIndex(string Version,uint index,int palet = 0,bool linerFilter = false)
  172. {
  173. GraphicInfoData graphicInfoData = GraphicInfo.GetGraphicInfoDataByIndex(Version, index);
  174. if (graphicInfoData == null) return null;
  175. return GraphicData.GetGraphicDetail(graphicInfoData, palet, 0, linerFilter);
  176. }
  177. }
  178. }