Palet.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 indexStr = file.Name.Substring(6, 2);
  36. int index = Convert.ToInt32(indexStr);
  37. List<Color32> paletData = _loadPalet(file);
  38. if (paletData != null) _cache.Add(index, paletData);
  39. }
  40. Debug.Log("[CGTool] 调色板初始化完成,共加载" + _cache.Count + "个调色板");
  41. }
  42. //加载缓存数据
  43. private static List<Color32> _loadPalet(FileInfo paletFile)
  44. {
  45. FileStream paletFileStream = paletFile.OpenRead();
  46. BinaryReader paletReader = new BinaryReader(paletFileStream);
  47. //调色板解析表
  48. List<Color32> PaletColors = new List<Color32>();
  49. //头部调色板
  50. int[] headPlate = new int[]
  51. {
  52. 0x000000, 0x000080, 0x008000, 0x008080, 0x800080, 0x800000, 0x808000, 0xc0c0c0, 0xc0dcc0, 0xf0caa6,
  53. 0x0000de, 0x005fff, 0xa0ffff, 0xd25f00, 0xffd250, 0x28e128
  54. };
  55. //尾部调色板
  56. int[] footPlate = new int[]
  57. {
  58. 0x96c3f5, 0x5fa01e, 0x467dc3, 0x1e559b, 0x374146, 0x1e2328, 0xf0fbff, 0xa56e3a, 0x808080, 0x0000ff,
  59. 0x00ff00, 0x00ffff, 0xff0000, 0xff80ff, 0xffff00, 0xffffff
  60. };
  61. //解压缩
  62. foreach (var i in headPlate)
  63. {
  64. byte[] bytes = BitConverter.GetBytes(i);
  65. Color32 color32 = new Color32();
  66. color32.r = bytes[0];
  67. color32.g = bytes[1];
  68. color32.b = bytes[2];
  69. color32.a = 0xFF;
  70. PaletColors.Add(color32);
  71. }
  72. for (var i = 0; i < 224; i++)
  73. {
  74. byte[] paletBytes = paletReader.ReadBytes(3);
  75. // if(i<16 || i>224) continue;
  76. Color32 color32 = new Color32();
  77. color32.r = paletBytes[2];
  78. color32.g = paletBytes[1];
  79. color32.b = paletBytes[0];
  80. color32.a = 0xFF;
  81. PaletColors.Add(color32);
  82. }
  83. foreach (var i in footPlate)
  84. {
  85. byte[] bytes = BitConverter.GetBytes(i);
  86. Color32 color32 = new Color32();
  87. color32.r = bytes[0];
  88. color32.g = bytes[1];
  89. color32.b = bytes[2];
  90. color32.a = 0xFF;
  91. PaletColors.Add(color32);
  92. }
  93. PaletColors.Add(Color.clear);
  94. //清理缓存
  95. paletReader.Dispose();
  96. paletReader.Close();
  97. paletFileStream.Dispose();
  98. paletFileStream.Close();
  99. return PaletColors;
  100. }
  101. }
  102. }