GraphicInfo.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 int[] 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. graphicInfoData.Blocked = fileReader.ReadByte() == 0;
  85. graphicInfoData.AsGround = fileReader.ReadByte() == 1;
  86. graphicInfoData.Unknow = fileReader.ReadBytes(4);
  87. graphicInfoData.Serial = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  88. graphicInfoData.GraphicReader = graphicFileReader;
  89. //建立Index映射表
  90. _indexCache[Version][graphicInfoData.Index] = graphicInfoData;
  91. //建立Serial映射表
  92. if (graphicInfoData.Serial != 0) _cache[graphicInfoData.Serial] = graphicInfoData;
  93. // _logger.Write("Index: " + graphicInfoData.Index + " Addr: " + graphicInfoData.Addr +
  94. // " Width: " + graphicInfoData.Width +
  95. // " Height: " + graphicInfoData.Height +
  96. // " OffsetX: " + graphicInfoData.OffsetX +
  97. // " OffsetY: " + graphicInfoData.OffsetY +
  98. // " East: " + graphicInfoData.East +
  99. // " South: " + graphicInfoData.South +
  100. // " Blocked: " + graphicInfoData.Blocked +
  101. // " Unknow: " + BitConverter.ToString(graphicInfoData.Unknow).Replace("-", ",") +
  102. // " MapSerial: " + graphicInfoData.MapSerial);
  103. }
  104. Debug.Log("[CGTool] 加载GraphicInfo - 文件: [" +
  105. // (Graphic.Flag_HighVersion[Version] ? "H" : "N") + "] [" +
  106. Version + "] " +
  107. graphicInfoFile.Name + " 贴图总量: " + DataLength);
  108. }
  109. //获取GraphicInfoData
  110. public static GraphicInfoData GetGraphicInfoData(uint Serial)
  111. {
  112. _cache.TryGetValue(Serial, out var graphicInfoData);
  113. return graphicInfoData;
  114. }
  115. public static GraphicInfoData GetGraphicInfoDataByIndex(string Version,uint Index)
  116. {
  117. _indexCache.TryGetValue(Version, out var indexDict);
  118. if(indexDict == null) throw new Exception("找不到对应版本的GraphicInfo数据");
  119. indexDict.TryGetValue(Index, out var graphicInfoData);
  120. return graphicInfoData;
  121. }
  122. }
  123. }