GraphicInfo.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. //已解压的调色板索引
  51. public int[] UnpackedPaletIndex;
  52. }
  53. public class GraphicInfo:MonoBehaviour
  54. {
  55. //索引字典 Serial -> GraphicInfoData
  56. private static readonly Dictionary<uint,GraphicInfoData> _cache = new Dictionary<uint, GraphicInfoData>();
  57. // private static readonly Dictionary<uint,GraphicInfoData> _indexCache = new Dictionary<uint, GraphicInfoData>();
  58. private static readonly Dictionary<string,Dictionary<uint,GraphicInfoData>> _indexCache = new Dictionary<string, Dictionary<uint, GraphicInfoData>>();
  59. public static void Init(string Version,FileInfo graphicInfoFile,FileInfo graphicFile)
  60. {
  61. if(!_indexCache.ContainsKey(Version)) _indexCache.Add(Version,new Dictionary<uint, GraphicInfoData>());
  62. //创建流读取器
  63. FileStream fileStream = graphicInfoFile.OpenRead();
  64. BinaryReader fileReader = new BinaryReader(fileStream);
  65. FileStream graphicFileStream = graphicFile.OpenRead();
  66. BinaryReader graphicFileReader = new BinaryReader(graphicFileStream);
  67. //解析Info数据表
  68. // List<GraphicInfoData> infoDatas = new List<GraphicInfoData>();
  69. long DataLength = fileStream.Length/40;
  70. for (int i = 0; i < DataLength; i++)
  71. {
  72. GraphicInfoData graphicInfoData = new GraphicInfoData();
  73. graphicInfoData.Index = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  74. graphicInfoData.Addr = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  75. graphicInfoData.Length = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  76. graphicInfoData.OffsetX = BitConverter.ToInt32(fileReader.ReadBytes(4),0);
  77. graphicInfoData.OffsetY = BitConverter.ToInt32(fileReader.ReadBytes(4),0);
  78. graphicInfoData.Width = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  79. graphicInfoData.Height = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  80. graphicInfoData.East = fileReader.ReadByte();
  81. graphicInfoData.South = fileReader.ReadByte();
  82. graphicInfoData.Blocked = fileReader.ReadByte() == 0;
  83. graphicInfoData.AsGround = fileReader.ReadByte() == 1;
  84. graphicInfoData.Unknow = fileReader.ReadBytes(4);
  85. graphicInfoData.Serial = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  86. graphicInfoData.GraphicReader = graphicFileReader;
  87. //建立Index映射表
  88. _indexCache[Version][graphicInfoData.Index] = graphicInfoData;
  89. //建立Serial映射表
  90. if (graphicInfoData.Serial != 0) _cache[graphicInfoData.Serial] = graphicInfoData;
  91. // _logger.Write("Index: " + graphicInfoData.Index + " Addr: " + graphicInfoData.Addr +
  92. // " Width: " + graphicInfoData.Width +
  93. // " Height: " + graphicInfoData.Height +
  94. // " OffsetX: " + graphicInfoData.OffsetX +
  95. // " OffsetY: " + graphicInfoData.OffsetY +
  96. // " East: " + graphicInfoData.East +
  97. // " South: " + graphicInfoData.South +
  98. // " Blocked: " + graphicInfoData.Blocked +
  99. // " Unknow: " + BitConverter.ToString(graphicInfoData.Unknow).Replace("-", ",") +
  100. // " MapSerial: " + graphicInfoData.MapSerial);
  101. }
  102. Debug.Log("[CGTool] 加载GraphicInfo - 文件: " + graphicInfoFile.Name + " 贴图总量: " + DataLength);
  103. }
  104. //获取GraphicInfoData
  105. public static GraphicInfoData GetGraphicInfoData(uint Serial)
  106. {
  107. _cache.TryGetValue(Serial, out var graphicInfoData);
  108. return graphicInfoData;
  109. }
  110. public static GraphicInfoData GetGraphicInfoDataByIndex(string Version,uint Index)
  111. {
  112. _indexCache.TryGetValue(Version, out var indexDict);
  113. if(indexDict == null) throw new Exception("找不到对应版本的GraphicInfo数据");
  114. indexDict.TryGetValue(Index, out var graphicInfoData);
  115. return graphicInfoData;
  116. }
  117. }
  118. }