Palet.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * 魔力宝贝图档解析脚本 - CGTool
  3. *
  4. * @Author HonorLee (dev@honorlee.me)
  5. * @Version 1.0 (2023-04-15)
  6. * @License GPL-3.0
  7. *
  8. * Palet.cs 调色板解析类
  9. */
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using UnityEngine;
  14. namespace CGTool
  15. {
  16. public class Palet
  17. {
  18. private static Dictionary<int, List<Color32>> _cache = new Dictionary<int, List<Color32>>();
  19. //获取调色板
  20. public static List<Color32> GetPalet(int index)
  21. {
  22. //返回缓存数据
  23. if (_cache.ContainsKey(index)) return _cache[index];
  24. //获取新调色板
  25. List<Color32> paletData = _loadPalet(index);
  26. //加入缓存
  27. if (paletData != null) _cache.Add(index, paletData);
  28. if (paletData == null) paletData = GetPalet(0);
  29. return paletData;
  30. }
  31. //将数字生成指定长度字符串,不足位数补进行填充
  32. private static string NumToString(uint num, int len, bool fillZero)
  33. {
  34. string numStr = num.ToString();
  35. if (numStr.Length < len && fillZero)
  36. {
  37. int count = len - numStr.Length;
  38. for (int i = 0; i < count; i++)
  39. {
  40. numStr = "0" + numStr;
  41. }
  42. }
  43. return numStr;
  44. }
  45. //加载缓存数据
  46. private static List<Color32> _loadPalet(int index)
  47. {
  48. //查找调色板文件
  49. DirectoryInfo paletFolderInfo = new DirectoryInfo(CGTool.PaletFolder);
  50. string filledIndex = NumToString((uint)index, 2, true);
  51. FileInfo[] files = paletFolderInfo.GetFiles("palet_" + filledIndex + ".cgp");
  52. if (files.Length == 0) return null;
  53. // CGTool.Logger.Write("加载调色板 - 编号: " + filledIndex);
  54. //创建流读取器
  55. FileInfo paletFileInfo = files[0];
  56. FileStream paletFileStream = paletFileInfo.OpenRead();
  57. BinaryReader paletReader = new BinaryReader(paletFileStream);
  58. //调色板解析表
  59. List<Color32> PaletColors = new List<Color32>();
  60. //头部调色板
  61. int[] headPlate = new int[]
  62. {
  63. 0x000000, 0x000080, 0x008000, 0x008080, 0x800080, 0x800000, 0x808000, 0xc0c0c0, 0xc0dcc0, 0xf0caa6,
  64. 0x0000de, 0x005fff, 0xa0ffff, 0xd25f00, 0xffd250, 0x28e128
  65. };
  66. //尾部调色板
  67. int[] footPlate = new int[]
  68. {
  69. 0x96c3f5, 0x5fa01e, 0x467dc3, 0x1e559b, 0x374146, 0x1e2328, 0xf0fbff, 0xa56e3a, 0x808080, 0x0000ff,
  70. 0x00ff00, 0x00ffff, 0xff0000, 0xff80ff, 0xffff00, 0xffffff
  71. };
  72. //解压缩
  73. foreach (var i in headPlate)
  74. {
  75. byte[] bytes = BitConverter.GetBytes(i);
  76. Color32 color32 = new Color32();
  77. color32.r = bytes[0];
  78. color32.g = bytes[1];
  79. color32.b = bytes[2];
  80. color32.a = 0xFF;
  81. PaletColors.Add(color32);
  82. }
  83. for (var i = 0; i < 224; i++)
  84. {
  85. byte[] paletBytes = paletReader.ReadBytes(3);
  86. // if(i<16 || i>224) continue;
  87. Color32 color32 = new Color32();
  88. color32.r = paletBytes[2];
  89. color32.g = paletBytes[1];
  90. color32.b = paletBytes[0];
  91. color32.a = 0xFF;
  92. PaletColors.Add(color32);
  93. }
  94. foreach (var i in footPlate)
  95. {
  96. byte[] bytes = BitConverter.GetBytes(i);
  97. Color32 color32 = new Color32();
  98. color32.r = bytes[0];
  99. color32.g = bytes[1];
  100. color32.b = bytes[2];
  101. color32.a = 0xFF;
  102. PaletColors.Add(color32);
  103. }
  104. PaletColors.Add(Color.clear);
  105. //清理缓存
  106. paletReader.Dispose();
  107. paletReader.Close();
  108. paletFileStream.Dispose();
  109. paletFileStream.Close();
  110. return PaletColors;
  111. }
  112. }
  113. }