GraphicInfo.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * 魔力宝贝图档解析脚本 - CGTool
  3. *
  4. * @Author HonorLee (dev@honorlee.me)
  5. * @Version 1.0 (2023-04-15)
  6. * @License GPL-3.0
  7. *
  8. * GraphicInfo.cs 图档索引解析类
  9. */
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Text.RegularExpressions;
  14. using UnityEngine;
  15. namespace CrossgateToolkit
  16. {
  17. //GraphicInfo数据块
  18. public class GraphicInfoData
  19. {
  20. // GraphicInfo基础数据
  21. //4 bytes 索引
  22. public uint Index;
  23. //4 bytes Graphic 地址
  24. public uint Addr;
  25. //4 bytes Graphic 数据长度
  26. public uint Length;
  27. //4 bytes Graphic 偏移 - X
  28. public int OffsetX;
  29. //4 bytes Graphic 偏移 - Y
  30. public int OffsetY;
  31. //4 bytes Graphic 宽
  32. public uint Width;
  33. //4 bytes Graphic 高
  34. public uint Height;
  35. //4 bytes Graphic East占地
  36. public int East;
  37. //4 bytes Graphic South 占地
  38. public int South;
  39. //bool 穿越标识
  40. public bool Blocked;
  41. //1 byte 作为地面无层级遮挡[Test]
  42. public bool AsGround;
  43. //4 bytes 未知标识
  44. public byte[] Unknow;
  45. //4 bytes 编号
  46. public uint Serial;
  47. // GraphicInfo附加数据
  48. // GraphicInfo对应Graphic文件流读取器
  49. public BinaryReader GraphicReader;
  50. public byte VERSION_FLAG;
  51. //已解压的调色板索引
  52. public byte[] UnpackedPaletIndex;
  53. public List<Color32> InnerPalet;
  54. }
  55. public class GraphicInfo:MonoBehaviour
  56. {
  57. //索引字典 Serial -> GraphicInfoData
  58. private static readonly Dictionary<uint,GraphicInfoData> _cache = new Dictionary<uint, GraphicInfoData>();
  59. // private static readonly Dictionary<uint,GraphicInfoData> _indexCache = new Dictionary<uint, GraphicInfoData>();
  60. private static readonly Dictionary<string,Dictionary<uint,GraphicInfoData>> _indexCache = new Dictionary<string, Dictionary<uint, GraphicInfoData>>();
  61. public static void Init(string Version,FileInfo graphicInfoFile,FileInfo graphicFile)
  62. {
  63. if(!_indexCache.ContainsKey(Version)) _indexCache.Add(Version,new Dictionary<uint, GraphicInfoData>());
  64. //创建流读取器
  65. FileStream fileStream = graphicInfoFile.OpenRead();
  66. BinaryReader fileReader = new BinaryReader(fileStream);
  67. FileStream graphicFileStream = graphicFile.OpenRead();
  68. BinaryReader graphicFileReader = new BinaryReader(graphicFileStream);
  69. //解析Info数据表
  70. // List<GraphicInfoData> infoDatas = new List<GraphicInfoData>();
  71. long DataLength = fileStream.Length/40;
  72. for (int i = 0; i < DataLength; i++)
  73. {
  74. GraphicInfoData graphicInfoData = new GraphicInfoData();
  75. graphicInfoData.Index = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  76. graphicInfoData.Addr = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  77. graphicInfoData.Length = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  78. graphicInfoData.OffsetX = BitConverter.ToInt32(fileReader.ReadBytes(4),0);
  79. graphicInfoData.OffsetY = BitConverter.ToInt32(fileReader.ReadBytes(4),0);
  80. graphicInfoData.Width = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  81. graphicInfoData.Height = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  82. graphicInfoData.East = fileReader.ReadByte();
  83. graphicInfoData.South = fileReader.ReadByte();
  84. byte Blocked = fileReader.ReadByte();
  85. graphicInfoData.Blocked = Blocked % 2 == 0;
  86. graphicInfoData.AsGround = fileReader.ReadByte() == 1;
  87. graphicInfoData.Unknow = fileReader.ReadBytes(4);
  88. graphicInfoData.Serial = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  89. graphicInfoData.GraphicReader = graphicFileReader;
  90. // if (graphicInfoData.Serial == 220759)
  91. // {
  92. // Debug.LogError(graphicInfoData.Serial + "穿越" + Blocked);
  93. // Debug.LogError(graphicInfoData.Serial + "穿越" + graphicInfoData.Blocked);
  94. // Debug.LogError(graphicInfoData.Serial + "穿越" + graphicInfoData.Width);
  95. // Debug.LogError(graphicInfoData.Serial + "穿越" + graphicInfoData.Height);
  96. // }
  97. // if (graphicInfoData.Serial >= 220000 && graphicInfoData.Width == 64 && graphicInfoData.Height == 47 && graphicInfoData.Blocked)
  98. // {
  99. // Debug.LogError(graphicInfoData.Serial + "穿越" + Blocked);
  100. // }
  101. //建立Index映射表
  102. _indexCache[Version][graphicInfoData.Index] = graphicInfoData;
  103. //建立Serial映射表
  104. if (graphicInfoData.Serial != 0) _cache[graphicInfoData.Serial] = graphicInfoData;
  105. // _logger.Write("Index: " + graphicInfoData.Index + " Addr: " + graphicInfoData.Addr +
  106. // " Width: " + graphicInfoData.Width +
  107. // " Height: " + graphicInfoData.Height +
  108. // " OffsetX: " + graphicInfoData.OffsetX +
  109. // " OffsetY: " + graphicInfoData.OffsetY +
  110. // " East: " + graphicInfoData.East +
  111. // " South: " + graphicInfoData.South +
  112. // " Blocked: " + graphicInfoData.Blocked +
  113. // " Unknow: " + BitConverter.ToString(graphicInfoData.Unknow).Replace("-", ",") +
  114. // " MapSerial: " + graphicInfoData.MapSerial);
  115. }
  116. Debug.Log("[CGTool] 加载GraphicInfo - 文件: [" +
  117. // (Graphic.Flag_HighVersion[Version] ? "H" : "N") + "] [" +
  118. Version + "] " +
  119. graphicInfoFile.Name + " 贴图总量: " + DataLength);
  120. }
  121. //获取GraphicInfoData
  122. public static GraphicInfoData GetGraphicInfoData(uint Serial)
  123. {
  124. _cache.TryGetValue(Serial, out var graphicInfoData);
  125. return graphicInfoData;
  126. }
  127. public static GraphicInfoData GetGraphicInfoDataByIndex(string Version,uint Index)
  128. {
  129. _indexCache.TryGetValue(Version, out var indexDict);
  130. if(indexDict == null) throw new Exception("找不到对应版本的GraphicInfo数据");
  131. indexDict.TryGetValue(Index, out var graphicInfoData);
  132. return graphicInfoData;
  133. }
  134. }
  135. }