Graphic.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * 魔力宝贝图档解析脚本 - CGTool
  3. *
  4. * @Author HonorLee (dev@honorlee.me)
  5. * @Version 1.0 (2023-04-15)
  6. * @License GPL-3.0
  7. *
  8. * Graphic.cs 图档解析类
  9. */
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using UnityEngine;
  15. namespace CGTool
  16. {
  17. public class GraphicData
  18. {
  19. //版本号
  20. public int Version;
  21. //索引
  22. public uint Index;
  23. //地图编号
  24. public uint MapSerial;
  25. //图档宽度
  26. public uint Width;
  27. //图档高度
  28. public uint Height;
  29. //图档偏移X
  30. public int OffsetX;
  31. //图档偏移Y
  32. public int OffsetY;
  33. //Palet调色板Index
  34. public int PaletIndex;
  35. //图档Sprite
  36. public Sprite Sprite;
  37. //图档主色调,用于小地图绘制
  38. public Color32 PrimaryColor;
  39. }
  40. public class Graphic
  41. {
  42. //缓存Addr Version -> Addr -> PaletIndex -> GraphicData
  43. private static Dictionary<int, Dictionary<uint, Dictionary<int, GraphicData>>> _cache =
  44. new Dictionary<int, Dictionary<uint, Dictionary<int, GraphicData>>>();
  45. //
  46. // //缓存Index映射 Version -> Index -> PaletIndex -> GraphicData
  47. // private static Dictionary<int, Dictionary<uint, Dictionary<int, GraphicData>>> _indexCache =
  48. // new Dictionary<int, Dictionary<uint, Dictionary<int, GraphicData>>>();
  49. //
  50. // //缓存MapSerial映射 Version -> MapSerial -> PaletIndex -> GraphicData
  51. // private static Dictionary<int, Dictionary<uint, Dictionary<int, GraphicData>>> _serialCache =
  52. // new Dictionary<int, Dictionary<uint, Dictionary<int, GraphicData>>>();
  53. private static List<string> _graphicPaths = new List<string>()
  54. {
  55. //龙之沙漏 之前版本前图档数据
  56. "Graphic_66.bin",
  57. //龙之沙漏 版本图档数据
  58. "GraphicEx_5.bin"
  59. };
  60. //根据地址获取GraphicData
  61. public static GraphicData GetGraphicData(GraphicInfoData graphicInfoData,int PaletIndex=0)
  62. {
  63. GraphicData graphicData = null;
  64. //缓存数据
  65. if (_cache.ContainsKey(graphicInfoData.Version))
  66. {
  67. if (_cache[graphicInfoData.Version].ContainsKey(graphicInfoData.Addr))
  68. {
  69. if (_cache[graphicInfoData.Version][graphicInfoData.Addr].ContainsKey(PaletIndex))
  70. {
  71. graphicData = _cache[graphicInfoData.Version][graphicInfoData.Addr][PaletIndex];
  72. }
  73. }
  74. }
  75. //无缓存则加载数据
  76. if (graphicData == null) graphicData = _loadGraphicData(graphicInfoData, PaletIndex);
  77. return graphicData;
  78. }
  79. //初始化加载GraphicInfo
  80. private static GraphicData _loadGraphicData(GraphicInfoData graphicInfoData, int PaletIndex = 0)
  81. {
  82. //查找图档文件
  83. string fileName = _graphicPaths[graphicInfoData.Version];
  84. FileInfo file = new FileInfo(CGTool.BaseFolder + "/" + fileName);
  85. if (!file.Exists) return null;
  86. //创建流读取器
  87. FileStream fileStream = file.OpenRead();
  88. BinaryReader fileReader = new BinaryReader(fileStream);
  89. //获取调色板
  90. List<Color32> palet = Palet.GetPalet(PaletIndex);
  91. GraphicData graphicData = new GraphicData();
  92. List<Color32> pixels = new List<Color32>();
  93. //调整流指针
  94. fileStream.Position = graphicInfoData.Addr;
  95. //读入目标字节集
  96. byte[] Content = fileReader.ReadBytes((int) graphicInfoData.Length);
  97. //关闭文件链接
  98. fileReader.Dispose();
  99. fileReader.Close();
  100. fileStream.Close();
  101. //读取缓存字节集
  102. BinaryReader contentReader = new BinaryReader(new MemoryStream(Content));
  103. //16字节头信息
  104. byte[] HEAD = contentReader.ReadBytes(2);
  105. int Version = contentReader.ReadByte();
  106. int Unknow = contentReader.ReadByte();
  107. uint Width = contentReader.ReadUInt32();
  108. uint Height = contentReader.ReadUInt32();
  109. uint Length = contentReader.ReadUInt32();
  110. //主色调色值
  111. int r = 0;
  112. int g = 0;
  113. int b = 0;
  114. //数据长度
  115. uint contentLen = Length - 16;
  116. //非压缩型数据
  117. if (Version == 0)
  118. {
  119. while (true)
  120. {
  121. Color32 color32;
  122. try
  123. {
  124. color32 = palet[contentReader.ReadByte()];
  125. }
  126. catch (Exception e)
  127. {
  128. break;
  129. }
  130. pixels.Add(color32);
  131. r += color32.r;
  132. g += color32.g;
  133. b += color32.b;
  134. }
  135. }
  136. else
  137. //压缩型数据解压
  138. {
  139. int count = 0;
  140. while (true)
  141. {
  142. count++;
  143. int head;
  144. try
  145. {
  146. head = contentReader.ReadByte();
  147. }
  148. catch (Exception e)
  149. {
  150. break;
  151. }
  152. int repeat = 0;
  153. Color32 color32;
  154. if (head < 0x10)
  155. {
  156. repeat = head;
  157. for (var i = 0; i < repeat; i++)
  158. {
  159. color32 = palet[contentReader.ReadByte()];
  160. r += color32.r;
  161. g += color32.g;
  162. b += color32.b;
  163. pixels.Add(color32);
  164. }
  165. }
  166. else if (head < 0x20)
  167. {
  168. repeat = head % 0x10 * 0x100 + contentReader.ReadByte();
  169. for (var i = 0; i < repeat; i++)
  170. {
  171. color32 = palet[contentReader.ReadByte()];
  172. r += color32.r;
  173. g += color32.g;
  174. b += color32.b;
  175. pixels.Add(color32);
  176. }
  177. }
  178. else if (head < 0x80)
  179. {
  180. repeat = head % 0x20 * 0x10000 + contentReader.ReadByte() * 0x100 + contentReader.ReadByte();
  181. for (var i = 0; i < repeat; i++)
  182. {
  183. color32 = palet[contentReader.ReadByte()];
  184. r += color32.r;
  185. g += color32.g;
  186. b += color32.b;
  187. pixels.Add(color32);
  188. }
  189. }
  190. else if (head < 0x90)
  191. {
  192. repeat = head % 0x80;
  193. color32 = palet[contentReader.ReadByte()];
  194. for (var i = 0; i < repeat; i++)
  195. {
  196. r += color32.r;
  197. g += color32.g;
  198. b += color32.b;
  199. pixels.Add(color32);
  200. }
  201. }
  202. else if (head < 0xa0)
  203. {
  204. color32 = palet[contentReader.ReadByte()];
  205. repeat = head % 0x90 * 0x100 + contentReader.ReadByte();
  206. for (var i = 0; i < repeat; i++)
  207. {
  208. r += color32.r;
  209. g += color32.g;
  210. b += color32.b;
  211. pixels.Add(color32);
  212. }
  213. }
  214. else if (head < 0xc0)
  215. {
  216. color32 = palet[contentReader.ReadByte()];
  217. repeat = head % 0xa0 * 0x10000 + contentReader.ReadByte() * 0x100 + contentReader.ReadByte();
  218. for (var i = 0; i < repeat; i++)
  219. {
  220. r += color32.r;
  221. g += color32.g;
  222. b += color32.b;
  223. pixels.Add(color32);
  224. }
  225. }
  226. else if (head < 0xd0)
  227. {
  228. color32 = Color.clear;
  229. repeat = head % 0xc0;
  230. for (var i = 0; i < repeat; i++)
  231. {
  232. r += color32.r;
  233. g += color32.g;
  234. b += color32.b;
  235. pixels.Add(color32);
  236. }
  237. }
  238. else if (head < 0xe0)
  239. {
  240. color32 = Color.clear;
  241. repeat = head % 0xd0 * 0x100 + contentReader.ReadByte();
  242. for (var i = 0; i < repeat; i++)
  243. {
  244. r += color32.r;
  245. g += color32.g;
  246. b += color32.b;
  247. pixels.Add(color32);
  248. }
  249. }
  250. else
  251. {
  252. color32 = Color.clear;
  253. repeat = head % 0xe0 * 0x10000 + contentReader.ReadByte() * 0x100 + contentReader.ReadByte();
  254. for (var i = 0; i < repeat; i++)
  255. {
  256. r += color32.r;
  257. g += color32.g;
  258. b += color32.b;
  259. pixels.Add(color32);
  260. }
  261. }
  262. }
  263. }
  264. //主色调计算及提亮
  265. r = r / pixels.Count * 2;
  266. g = g / pixels.Count * 2;
  267. b = b / pixels.Count * 2;
  268. if (r > 255) r = 255;
  269. if (g > 255) g = 255;
  270. if (b > 255) b = 255;
  271. //主色调
  272. Color32 primaryColor32 = new Color32((byte) r, (byte) g, (byte) b, 255);
  273. //释放连接
  274. contentReader.Dispose();
  275. contentReader.Close();
  276. //创建Sprite对象
  277. Texture2D texture2D = new Texture2D((int) graphicInfoData.Width, (int) graphicInfoData.Height,
  278. TextureFormat.RGBA32, false);
  279. int len = (int) (graphicInfoData.Width * graphicInfoData.Height);
  280. if (pixels.Count != len)
  281. {
  282. if (pixels.Count > len)
  283. {
  284. pixels = pixels.GetRange(0, len);
  285. }
  286. else
  287. {
  288. Color32[] temc = new Color32[len - pixels.Count];
  289. ArrayList.Repeat(Color.clear, len - pixels.Count).CopyTo(temc);
  290. pixels.AddRange(temc);
  291. }
  292. }
  293. texture2D.SetPixels32(pixels.ToArray());
  294. texture2D.filterMode = FilterMode.Point;
  295. // texture2D.Compress(true);
  296. texture2D.Apply();
  297. //直接通过Texture2D做偏移,并转为Sprite的偏移量
  298. Vector2 offset = new Vector2(0f, 1f);
  299. offset.x += -(graphicInfoData.OffsetX * 1f) / graphicInfoData.Width;
  300. offset.y -= (-graphicInfoData.OffsetY * 1f) / graphicInfoData.Height;
  301. Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), offset, 1,1,SpriteMeshType.FullRect);
  302. //写入数据
  303. graphicData.Version = graphicInfoData.Version;
  304. graphicData.Index = graphicInfoData.Index;
  305. graphicData.MapSerial = graphicInfoData.MapSerial;
  306. graphicData.Width = graphicInfoData.Width;
  307. graphicData.Height = graphicInfoData.Height;
  308. graphicData.OffsetX = graphicInfoData.OffsetX;
  309. graphicData.OffsetY = graphicInfoData.OffsetY;
  310. graphicData.PaletIndex = PaletIndex;
  311. graphicData.Sprite = sprite;
  312. graphicData.PrimaryColor = primaryColor32;
  313. //缓存
  314. if (!_cache.ContainsKey(graphicInfoData.Version))
  315. _cache.Add(graphicInfoData.Version, new Dictionary<uint, Dictionary<int, GraphicData>>());
  316. if(!_cache[graphicInfoData.Version].ContainsKey(graphicInfoData.Addr)) _cache[graphicInfoData.Version].Add(graphicInfoData.Addr,new Dictionary<int, GraphicData>());
  317. if (!_cache[graphicInfoData.Version][graphicInfoData.Addr].ContainsKey(PaletIndex))
  318. _cache[graphicInfoData.Version][graphicInfoData.Addr].Add(PaletIndex, graphicData);
  319. return graphicData;
  320. }
  321. }
  322. }