GraphicInfo.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 UnityEngine;
  14. // using Logger = Util.Logger;
  15. namespace CGTool
  16. {
  17. //GraphicInfo数据块
  18. public class GraphicInfoData
  19. {
  20. //版本号
  21. public int Version;
  22. //4 bytes 索引
  23. public uint Index;
  24. //4 bytes Graphic 地址
  25. public uint Addr;
  26. //4 bytes Graphic 数据长度
  27. public uint Length;
  28. //4 bytes Graphic 偏移 - X
  29. public int OffsetX;
  30. //4 bytes Graphic 偏移 - Y
  31. public int OffsetY;
  32. //4 bytes Graphic 宽
  33. public uint Width;
  34. //4 bytes Graphic 高
  35. public uint Height;
  36. //4 bytes Graphic East占地
  37. public int East;
  38. //4 bytes Graphic South 占地
  39. public int South;
  40. //bool 穿越标识
  41. public bool Blocked;
  42. //1 byte 作为地面无层级遮挡[Test]
  43. public byte AsGround;
  44. //4 bytes 未知标识
  45. public byte[] Unknow;
  46. //4 bytes 地图编号
  47. public uint MapSerial;
  48. }
  49. public class GraphicInfo:MonoBehaviour
  50. {
  51. // private static Logger _logger = new Logger("GraphicInfo", false);
  52. //版本索引字典 版本编号
  53. private static Dictionary<int, List<GraphicInfoData>> _cache = new Dictionary<int, List<GraphicInfoData>>();
  54. //版本-Addr映射字典 版本编号 -> Index -> GraphicInfoData
  55. private static Dictionary<int, Dictionary<uint, GraphicInfoData>>
  56. _indexDict = new Dictionary<int, Dictionary<uint, GraphicInfoData>>();
  57. //版本-Map编号映射字典 版本编号 -> MapSerial -> GraphicInfoData
  58. private static Dictionary<int, Dictionary<uint, GraphicInfoData>>
  59. _mapSerialDict = new Dictionary<int, Dictionary<uint, GraphicInfoData>>();
  60. private static List<string> _graphicInfoPaths = new List<string>()
  61. {
  62. //龙之沙漏 之前版本前Info数据
  63. "GraphicInfo_66.bin",
  64. //龙之沙漏 版本Info数据
  65. "GraphicInfoEx_5.bin"
  66. };
  67. //获取GraphicInfo数据,Info数据加载后会缓存
  68. public static List<GraphicInfoData> GetGraphicInfo(int Version)
  69. {
  70. //返回缓存数据
  71. if (_cache.ContainsKey(Version)) return _cache[Version];
  72. //初始化映射库
  73. _indexDict.Add(Version,new Dictionary<uint, GraphicInfoData>());
  74. _mapSerialDict.Add(Version,new Dictionary<uint, GraphicInfoData>());
  75. //加载并初始化数据
  76. List<GraphicInfoData> infoDatas = _loadGraphicInfo(Version);
  77. _cache.Add(Version, infoDatas);
  78. return infoDatas;
  79. }
  80. //通过地面编号获取GraphicInfo数据
  81. public static GraphicInfoData GetGraphicInfoDataByMapSerial(int Version, uint MapSerial)
  82. {
  83. GraphicInfoData graphicInfoData = null;
  84. if (_mapSerialDict.ContainsKey(Version))
  85. {
  86. _mapSerialDict[Version].TryGetValue(MapSerial, out graphicInfoData);
  87. // graphicInfoData = _mapSerialDict[Version][MapSerial];
  88. }
  89. return graphicInfoData;
  90. }
  91. //通过索引获取GraphicInfo数据
  92. public static GraphicInfoData GetGraphicInfoDataByIndex(int Version, uint Index)
  93. {
  94. GraphicInfoData graphicInfoData = null;
  95. if (_indexDict.ContainsKey(Version) && _indexDict[Version].ContainsKey(Index))
  96. {
  97. graphicInfoData = _indexDict[Version][Index];
  98. }
  99. return graphicInfoData;
  100. }
  101. //初始化加载GraphicInfo
  102. private static List<GraphicInfoData> _loadGraphicInfo(int Version)
  103. {
  104. //查找Info文件
  105. string fileName = _graphicInfoPaths[Version];
  106. FileInfo file = new FileInfo(CGTool.BaseFolder + "/" + fileName);
  107. if (!file.Exists) return null;
  108. //创建流读取器
  109. FileStream fileStream = file.OpenRead();
  110. BinaryReader fileReader = new BinaryReader(fileStream);
  111. //解析Info数据表
  112. List<GraphicInfoData> infoDatas = new List<GraphicInfoData>();
  113. long DataLength = fileStream.Length/40;
  114. for (int i = 0; i < DataLength; i++)
  115. {
  116. GraphicInfoData graphicInfoData = new GraphicInfoData();
  117. graphicInfoData.Version = Version;
  118. graphicInfoData.Index = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  119. graphicInfoData.Addr = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  120. graphicInfoData.Length = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  121. graphicInfoData.OffsetX = BitConverter.ToInt32(fileReader.ReadBytes(4),0);
  122. graphicInfoData.OffsetY = BitConverter.ToInt32(fileReader.ReadBytes(4),0);
  123. graphicInfoData.Width = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  124. graphicInfoData.Height = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  125. graphicInfoData.East = fileReader.ReadByte();
  126. graphicInfoData.South = fileReader.ReadByte();
  127. graphicInfoData.Blocked = fileReader.ReadByte() == 0;
  128. graphicInfoData.AsGround = fileReader.ReadByte();
  129. graphicInfoData.Unknow = fileReader.ReadBytes(4);
  130. graphicInfoData.MapSerial = BitConverter.ToUInt32(fileReader.ReadBytes(4),0);
  131. //建立映射表
  132. if(!_indexDict[Version].ContainsKey(graphicInfoData.Index)) _indexDict[Version].Add(graphicInfoData.Index, graphicInfoData);
  133. if(graphicInfoData.MapSerial > 0 && !_mapSerialDict[Version].ContainsKey(graphicInfoData.MapSerial)) _mapSerialDict[Version].Add(graphicInfoData.MapSerial, graphicInfoData);
  134. infoDatas.Add(graphicInfoData);
  135. // _logger.Write("Index: " + graphicInfoData.Index + " Addr: " + graphicInfoData.Addr +
  136. // " Width: " + graphicInfoData.Width +
  137. // " Height: " + graphicInfoData.Height +
  138. // " OffsetX: " + graphicInfoData.OffsetX +
  139. // " OffsetY: " + graphicInfoData.OffsetY +
  140. // " East: " + graphicInfoData.East +
  141. // " South: " + graphicInfoData.South +
  142. // " Blocked: " + graphicInfoData.Blocked +
  143. // " Unknow: " + BitConverter.ToString(graphicInfoData.Unknow).Replace("-", ",") +
  144. // " MapSerial: " + graphicInfoData.MapSerial);
  145. }
  146. // CGTool.Logger.Write("加载GraphicInfo - 版本: " + Version + " 文件: " + fileName + " 贴图总量: "+ infoDatas.Count);
  147. return infoDatas;
  148. }
  149. }
  150. }