Anime.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * 魔力宝贝图档解析脚本 - CGTool
  3. *
  4. * @Author HonorLee (dev@honorlee.me)
  5. * @Version 1.0 (2023-04-15)
  6. * @License GPL-3.0
  7. *
  8. * Anime.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 class AnimeInfo
  19. {
  20. // 版本
  21. public string Version;
  22. //4 bytes 动画序号
  23. public uint Serial;
  24. //4 bytes 动画文件地址
  25. public uint Addr;
  26. //2 bytes 动作数量
  27. public int ActionCount;
  28. //2 bytes 未知字节
  29. public byte[] Unknow;
  30. //动画数据 Direction -> ActionType -> AnimeData
  31. public Dictionary<int, Dictionary<int, AnimeDetail>> AnimeDatas = new Dictionary<int, Dictionary<int, AnimeDetail>>();
  32. }
  33. //动画帧数据
  34. public class AnimeFrameInfo
  35. {
  36. //图档编号
  37. public uint GraphicIndex;
  38. //宽度
  39. public int Width;
  40. //高度
  41. public int Height;
  42. //偏移X
  43. public int OffsetX;
  44. //偏移Y
  45. public int OffsetY;
  46. //音效编号
  47. public int AudioIndex;
  48. //动效编号
  49. public Anime.EffectType Effect;
  50. //GraphicInfo;
  51. public GraphicInfoData GraphicInfo;
  52. //动画Sprite
  53. // public Dictionary<int,Sprite> AnimeSprites = new Dictionary<int, Sprite>();
  54. public Dictionary<int,Dictionary<bool,Sprite>> AnimeSprites = new Dictionary<int, Dictionary<bool, Sprite>>();
  55. }
  56. //动画数据
  57. public class AnimeDetail
  58. {
  59. // 动画编号
  60. public uint Serial;
  61. // 动画版本
  62. public string Version;
  63. // 方向
  64. public int Direction;
  65. // 动作
  66. public int ActionType;
  67. // 动画循环时间
  68. public uint CycleTime;
  69. // 帧数
  70. public uint FrameCount;
  71. // 高版本 - 标识
  72. public bool IsHighVersion;
  73. // 高版本 - 调色板
  74. public int Palet;
  75. // 高版本 - 图像反转
  76. public AnimeFlag FLAG;
  77. // 高版本 - 结束标识
  78. public byte[] FLAG_END;
  79. // public Dictionary<int,Texture2D> AnimeTextures = new Dictionary<int, Texture2D>();
  80. public Dictionary<int,Dictionary<bool,Texture2D>> AnimeTextures = new Dictionary<int, Dictionary<bool, Texture2D>>();
  81. // public Texture2D AnimeTexture;
  82. public List<AnimeFrameInfo> AnimeFrameInfos;
  83. // public byte[] unknown;
  84. }
  85. public class AnimeFlag
  86. {
  87. public bool REVERSE_X;
  88. public bool REVERSE_Y;
  89. public bool LOCK_PAL;
  90. public bool LIGHT_THROUGH;
  91. }
  92. //动画相关Enum类型
  93. public class Anime : MonoBehaviour
  94. {
  95. //方向
  96. public enum DirectionType
  97. {
  98. NULL = -1,
  99. North = 0,
  100. NorthEast = 1,
  101. East = 2,
  102. SouthEast = 3,
  103. South = 4,
  104. SouthWest = 5,
  105. West = 6,
  106. NorthWest = 7
  107. }
  108. //方向九宫映射表
  109. public static DirectionType[,] DirectionTypeMap = new DirectionType[3,3]
  110. {
  111. {DirectionType.North,DirectionType.NorthEast,DirectionType.East},
  112. {DirectionType.NorthWest,DirectionType.NULL,DirectionType.SouthEast},
  113. {DirectionType.West,DirectionType.SouthWest,DirectionType.South}
  114. };
  115. //动作
  116. public enum ActionType
  117. {
  118. NULL = -1,
  119. Stand = 0,
  120. Walk = 1,
  121. BeforeRun = 2,
  122. Run = 3,
  123. AfterRun = 4,
  124. Attack = 5,
  125. Magic = 6,
  126. Throw = 7,
  127. Hurt = 8,
  128. Defence = 9,
  129. Dead = 10,
  130. Sit = 11,
  131. Hi = 12,
  132. Happy = 13,
  133. Angry = 14,
  134. Sad = 15,
  135. Shake = 16,
  136. Rock = 17,
  137. Scissors = 18,
  138. Paper = 19,
  139. Fishing = 20,
  140. }
  141. //动效
  142. public enum EffectType
  143. {
  144. Hit =1,
  145. HitOver =2
  146. }
  147. public enum PlayType
  148. {
  149. Loop,
  150. Once,
  151. OnceAndDestroy
  152. }
  153. private static byte[] highVersionFlag = { 0xFF, 0xFF, 0xFF, 0xFF };
  154. //动画列表缓存 Serial -> AnimeInfo
  155. private static Dictionary<uint, AnimeInfo> _animeInfoCache = new Dictionary<uint, AnimeInfo>();
  156. //加载动画数据
  157. public static void Init(string Version,FileInfo animeInfoFile,FileInfo animeFile)
  158. {
  159. //创建流读取器
  160. FileStream infoFileStream = animeInfoFile.OpenRead();
  161. FileStream dataFileStream = animeFile.OpenRead();
  162. BinaryReader infoFileReader = new BinaryReader(infoFileStream);
  163. BinaryReader dataFileReader = new BinaryReader(dataFileStream);
  164. long DataLength = infoFileStream.Length / 12;
  165. // 循环初始化动画数据
  166. for (int i = 0; i < DataLength; i++)
  167. {
  168. //初始化对象
  169. AnimeInfo animeInfo = new AnimeInfo();
  170. animeInfo.Version = Version;
  171. animeInfo.Serial = BitConverter.ToUInt32(infoFileReader.ReadBytes(4),0);
  172. // Debug.Log(animeInfo.Serial);
  173. animeInfo.Addr = BitConverter.ToUInt32(infoFileReader.ReadBytes(4),0);
  174. animeInfo.ActionCount = infoFileReader.ReadUInt16();
  175. animeInfo.Unknow = infoFileReader.ReadBytes(2);
  176. if (animeInfo.Addr > dataFileStream.Length) break;
  177. dataFileStream.Position = animeInfo.Addr;
  178. for (int j = 0; j < animeInfo.ActionCount; j++)
  179. {
  180. // 高版本标识
  181. bool isHighVersion = false;
  182. dataFileStream.Position += 16;
  183. if(dataFileReader.ReadBytes(4).SequenceEqual(highVersionFlag)) isHighVersion = true;
  184. dataFileStream.Position -= 20;
  185. AnimeDetail animeData = new AnimeDetail();
  186. animeData.Version = Version;
  187. animeData.Serial = animeInfo.Serial;
  188. animeData.Direction = dataFileReader.ReadUInt16();
  189. animeData.ActionType = dataFileReader.ReadUInt16();
  190. animeData.CycleTime = BitConverter.ToUInt32(dataFileReader.ReadBytes(4),0);
  191. animeData.FrameCount = BitConverter.ToUInt32(dataFileReader.ReadBytes(4),0);
  192. // 高版本
  193. if (isHighVersion)
  194. {
  195. animeData.IsHighVersion = true;
  196. animeData.Palet = dataFileReader.ReadUInt16();
  197. int flag = dataFileReader.ReadUInt16();
  198. if (flag > 0)
  199. {
  200. animeData.FLAG = new AnimeFlag();
  201. if ((flag & 1) == (1 << 0)) animeData.FLAG.REVERSE_X = true;
  202. if ((flag & 2) == (1 << 1)) animeData.FLAG.REVERSE_Y = true;
  203. if ((flag & 4) == (1 << 2)) animeData.FLAG.LOCK_PAL = true;
  204. if ((flag & 8) == (1 << 3)) animeData.FLAG.LIGHT_THROUGH = true;
  205. }
  206. animeData.FLAG_END = dataFileReader.ReadBytes(4);
  207. }
  208. animeData.AnimeFrameInfos = new List<AnimeFrameInfo>();
  209. // if (animeInfo.Index == 101201) Debug.Log("----------------------------------");
  210. for (int k = 0; k < animeData.FrameCount; k++)
  211. {
  212. byte[] frameBytes = dataFileReader.ReadBytes(10);
  213. if (frameBytes.Length <10) break;
  214. BinaryReader frameReader = new BinaryReader(new MemoryStream(frameBytes));
  215. AnimeFrameInfo animeFrameInfo = new AnimeFrameInfo();
  216. //GraphicIndex序号
  217. animeFrameInfo.GraphicIndex = BitConverter.ToUInt32(frameReader.ReadBytes(4),0);
  218. animeFrameInfo.OffsetX = BitConverter.ToInt16(frameReader.ReadBytes(2),0);
  219. animeFrameInfo.OffsetY = BitConverter.ToInt16(frameReader.ReadBytes(2), 0);
  220. //标识位
  221. int flag = BitConverter.ToInt16(frameReader.ReadBytes(2),0);
  222. if (flag>20000)
  223. {
  224. //击打判定
  225. animeFrameInfo.Effect = EffectType.Hit;
  226. animeFrameInfo.AudioIndex = flag - 20000;
  227. }
  228. else if(flag>10000)
  229. {
  230. //攻击动作结束判定
  231. animeFrameInfo.Effect = EffectType.HitOver;
  232. animeFrameInfo.AudioIndex = flag - 10000;
  233. }
  234. else
  235. {
  236. animeFrameInfo.AudioIndex = flag;
  237. }
  238. animeData.AnimeFrameInfos.Add(animeFrameInfo);
  239. }
  240. animeData.FrameCount = (uint) animeData.AnimeFrameInfos.Count;
  241. if (!animeInfo.AnimeDatas.ContainsKey(animeData.Direction))
  242. animeInfo.AnimeDatas.Add(animeData.Direction, new Dictionary<int, AnimeDetail>());
  243. animeInfo.AnimeDatas[animeData.Direction][animeData.ActionType] = animeData;
  244. _animeInfoCache[animeInfo.Serial] = animeInfo;
  245. }
  246. }
  247. infoFileReader.Dispose();
  248. infoFileReader.Close();
  249. dataFileReader.Dispose();
  250. dataFileReader.Close();
  251. infoFileStream.Close();
  252. dataFileStream.Close();
  253. Debug.Log("[CGTool] 加载AnimeInfo - 文件: [" +
  254. // (Graphic.Flag_HighVersion[Version] ? "H" : "N") + "] [" +
  255. Version + "] " +
  256. animeInfoFile.Name +
  257. " 动画总量: " + DataLength);
  258. }
  259. //获取动画数据信息
  260. public static AnimeInfo GetAnimeInfo(uint serial)
  261. {
  262. _animeInfoCache.TryGetValue(serial, out var animeInfo);
  263. return animeInfo;
  264. }
  265. //获取动画数据
  266. public static AnimeDetail GetAnimeDetail(uint serial,DirectionType Direction,ActionType Action)
  267. {
  268. AnimeInfo animeInfo = GetAnimeInfo(serial);
  269. if (animeInfo == null) return null;
  270. if (animeInfo.AnimeDatas.ContainsKey((int)Direction))
  271. {
  272. if (animeInfo.AnimeDatas[(int) Direction].ContainsKey((int) Action))
  273. {
  274. AnimeDetail animeDetail = animeInfo.AnimeDatas[(int) Direction][(int) Action];
  275. // if(animeDetail.AnimeTexture == null) prepareAnimeFrames(animeDetail);
  276. return animeDetail;
  277. }
  278. }
  279. return null;
  280. }
  281. //预处理动画图形合批烘焙
  282. public static void BakeAnimeFrames(AnimeDetail animeDetail,int palet = 0, bool linear = false)
  283. {
  284. if (animeDetail.AnimeTextures.ContainsKey(palet))
  285. {
  286. if(animeDetail.AnimeTextures[palet].ContainsKey(linear)) return;
  287. }
  288. //所有帧的图形数据
  289. GraphicDetail[] graphicDetails = new GraphicDetail[animeDetail.FrameCount];
  290. //合并后的Texture2D尺寸
  291. uint textureWidth = 0;
  292. uint textureHeight = 0;
  293. for (var i = 0; i < animeDetail.FrameCount; i++)
  294. {
  295. //载入图档
  296. GraphicInfoData graphicInfoData = GraphicInfo.GetGraphicInfoDataByIndex(animeDetail.Version,animeDetail.AnimeFrameInfos[i].GraphicIndex);
  297. if (graphicInfoData == null) continue;
  298. int subPaletIndex = 0;
  299. if (animeDetail.IsHighVersion) subPaletIndex = (int)animeDetail.Serial;
  300. GraphicDetail graphicDetail = GraphicData.GetGraphicDetail(graphicInfoData, palet, subPaletIndex, linear);
  301. if(graphicDetail == null) continue;
  302. graphicDetails[i] = graphicDetail;
  303. if(graphicDetail.Height > textureHeight) textureHeight = graphicDetail.Height;
  304. textureWidth += graphicDetail.Width + 5;
  305. animeDetail.AnimeFrameInfos[i].Width = (int) graphicDetail.Width;
  306. animeDetail.AnimeFrameInfos[i].Height = (int) graphicDetail.Height;
  307. animeDetail.AnimeFrameInfos[i].OffsetX = (int) graphicInfoData.OffsetX;
  308. animeDetail.AnimeFrameInfos[i].OffsetY = (int) graphicInfoData.OffsetY;
  309. animeDetail.AnimeFrameInfos[i].GraphicInfo = graphicInfoData;
  310. }
  311. //合并图档
  312. Texture2D texture2dMix = new Texture2D((int) textureWidth, (int) textureHeight, TextureFormat.RGBA4444, false,linear);
  313. if(linear) texture2dMix.filterMode = FilterMode.Bilinear;
  314. else texture2dMix.filterMode = FilterMode.Point;
  315. Color32 transparentColor = new Color32(0, 0, 0, 0);
  316. Color32[] transparentColors = new Color32[texture2dMix.width * texture2dMix.height];
  317. for (var i = 0; i < transparentColors.Length; i++)
  318. {
  319. transparentColors[i] = transparentColor;
  320. }
  321. texture2dMix.SetPixels32(transparentColors,0);
  322. int offsetX = 0;
  323. for (var i = 0; i < animeDetail.FrameCount; i++)
  324. {
  325. GraphicDetail graphicDetail = graphicDetails[i];
  326. if(graphicDetail == null) continue;
  327. texture2dMix.SetPixels32((int) offsetX, 0, (int) graphicDetail.Width,
  328. (int) graphicDetail.Height,
  329. graphicDetail.Sprite.texture.GetPixels32());
  330. offsetX += (int) graphicDetail.Width + 5;
  331. }
  332. texture2dMix.Apply();
  333. if(!animeDetail.AnimeTextures.ContainsKey(palet)) animeDetail.AnimeTextures.Add(palet,new Dictionary<bool, Texture2D>());
  334. animeDetail.AnimeTextures[palet].Add(linear, texture2dMix);
  335. //创建动画每帧Sprite
  336. offsetX = 0;
  337. for (var l = 0; l < animeDetail.FrameCount; l++)
  338. {
  339. if(graphicDetails[l] == null) continue;
  340. AnimeFrameInfo animeFrameInfo = animeDetail.AnimeFrameInfos[l];
  341. Vector2 pivot = new Vector2(0f, 1f);
  342. pivot.x += -(animeFrameInfo.OffsetX * 1f) / animeFrameInfo.Width;
  343. pivot.y -= (-animeFrameInfo.OffsetY * 1f) / animeFrameInfo.Height;
  344. Sprite sprite = Sprite.Create(texture2dMix, new Rect(offsetX, 0,
  345. animeDetail.AnimeFrameInfos[l].Width, animeDetail.AnimeFrameInfos[l].Height),
  346. pivot, 1, 1, SpriteMeshType.FullRect);
  347. offsetX += animeDetail.AnimeFrameInfos[l].Width + 5;
  348. if(!animeFrameInfo.AnimeSprites.ContainsKey(palet)) animeFrameInfo.AnimeSprites.Add(palet,new Dictionary<bool, Sprite>());
  349. animeFrameInfo.AnimeSprites[palet].Add(linear, sprite);
  350. }
  351. }
  352. }
  353. }