Palet.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 CrossgateToolkit
  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. _cache.TryGetValue(index, out List<Color32> paletData);
  24. return paletData;
  25. }
  26. //调色板初始化
  27. public static void Init()
  28. {
  29. DirectoryInfo folderInfo = new DirectoryInfo(CGTool.PATH.PAL);
  30. if (!folderInfo.Exists) throw new Exception("调色板目录不存在,请检查CGTool中是否配置相应PATH路径");
  31. FileInfo[] files = folderInfo.GetFiles();
  32. foreach (FileInfo file in files)
  33. {
  34. if (!file.Name.StartsWith("palet_") || !file.Name.EndsWith(".cgp")) continue;
  35. string tmp = file.Name.Split("_")[1];
  36. string indexStr = tmp.Split(".")[0];
  37. // string indexStr = file.Name.Substring(6, 2);
  38. if (int.TryParse(indexStr, out int index))
  39. {
  40. List<Color32> paletData = _loadPalet(file);
  41. if (paletData != null) _cache.Add(index, paletData);
  42. }
  43. }
  44. Debug.Log("[CGTool] 调色板初始化完成,共加载" + _cache.Count + "个调色板");
  45. }
  46. // 添加新调色板
  47. public static void AddPalet(int index, List<Color32> paletData)
  48. {
  49. _cache[index] = paletData;
  50. }
  51. //加载缓存数据
  52. private static List<Color32> _loadPalet(FileInfo paletFile)
  53. {
  54. FileStream paletFileStream = paletFile.OpenRead();
  55. BinaryReader paletReader = new BinaryReader(paletFileStream);
  56. //调色板解析表
  57. List<Color32> PaletColors = new List<Color32>();
  58. //头部调色板
  59. int[] headPlate = new int[]
  60. {
  61. 0x000000, 0x000080, 0x008000, 0x008080, 0x800080, 0x800000, 0x808000, 0xc0c0c0, 0xc0dcc0, 0xf0caa6,
  62. 0x0000de, 0x005fff, 0xa0ffff, 0xd25f00, 0xffd250, 0x28e128
  63. };
  64. //尾部调色板
  65. int[] footPlate = new int[]
  66. {
  67. 0x96c3f5, 0x5fa01e, 0x467dc3, 0x1e559b, 0x374146, 0x1e2328, 0xf0fbff, 0xa56e3a, 0x808080, 0x0000ff,
  68. 0x00ff00, 0x00ffff, 0xff0000, 0xff80ff, 0xffff00, 0xffffff
  69. };
  70. //解压缩
  71. foreach (var i in headPlate)
  72. {
  73. byte[] bytes = BitConverter.GetBytes(i);
  74. Color32 color32 = new Color32();
  75. color32.r = bytes[0];
  76. color32.g = bytes[1];
  77. color32.b = bytes[2];
  78. color32.a = (byte)(i == 0 ? 0x00 : 0xFF);
  79. PaletColors.Add(color32);
  80. }
  81. for (var i = 0; i < 224; i++)
  82. {
  83. byte[] paletBytes = paletReader.ReadBytes(3);
  84. // if(i<16 || i>224) continue;
  85. Color32 color32 = new Color32();
  86. color32.r = paletBytes[2];
  87. color32.g = paletBytes[1];
  88. color32.b = paletBytes[0];
  89. color32.a = 0xFF;
  90. PaletColors.Add(color32);
  91. }
  92. foreach (var i in footPlate)
  93. {
  94. byte[] bytes = BitConverter.GetBytes(i);
  95. Color32 color32 = new Color32();
  96. color32.r = bytes[0];
  97. color32.g = bytes[1];
  98. color32.b = bytes[2];
  99. color32.a = 0xFF;
  100. PaletColors.Add(color32);
  101. }
  102. PaletColors.Add(Color.clear);
  103. //清理缓存
  104. paletReader.Dispose();
  105. paletReader.Close();
  106. paletFileStream.Dispose();
  107. paletFileStream.Close();
  108. return PaletColors;
  109. }
  110. }
  111. }