Palet.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // using Logger = Util.Logger;
  15. namespace CGTool
  16. {
  17. public class Palet
  18. {
  19. private static Dictionary<int, List<Color32>> _cache = new Dictionary<int, List<Color32>>();
  20. //获取调色板
  21. public static List<Color32> GetPalet(int index)
  22. {
  23. //返回缓存数据
  24. if (_cache.ContainsKey(index)) return _cache[index];
  25. //获取新调色板
  26. List<Color32> paletData = _loadPalet(index);
  27. //加入缓存
  28. if (paletData != null) _cache.Add(index, paletData);
  29. if (paletData == null) paletData = GetPalet(0);
  30. return paletData;
  31. }
  32. //加载缓存数据
  33. private static List<Color32> _loadPalet(int index)
  34. {
  35. //查找调色板文件
  36. DirectoryInfo paletFolderInfo = new DirectoryInfo(CGTool.PaletFolder);
  37. string filledIndex = Util.StringTool.numToString((uint)index, 2, true);
  38. FileInfo[] files = paletFolderInfo.GetFiles("palet_" + filledIndex + ".cgp");
  39. if (files.Length == 0) return null;
  40. // CGTool.Logger.Write("加载调色板 - 编号: " + filledIndex);
  41. //创建流读取器
  42. FileInfo paletFileInfo = files[0];
  43. FileStream paletFileStream = paletFileInfo.OpenRead();
  44. BinaryReader paletReader = new BinaryReader(paletFileStream);
  45. //调色板解析表
  46. List<Color32> PaletColors = new List<Color32>();
  47. //头部调色板
  48. int[] headPlate = new int[]
  49. {
  50. 0x000000, 0x000080, 0x008000, 0x008080, 0x800080, 0x800000, 0x808000, 0xc0c0c0, 0xc0dcc0, 0xf0caa6,
  51. 0x0000de, 0x005fff, 0xa0ffff, 0xd25f00, 0xffd250, 0x28e128
  52. };
  53. //尾部调色板
  54. int[] footPlate = new int[]
  55. {
  56. 0x96c3f5, 0x5fa01e, 0x467dc3, 0x1e559b, 0x374146, 0x1e2328, 0xf0fbff, 0xa56e3a, 0x808080, 0x0000ff,
  57. 0x00ff00, 0x00ffff, 0xff0000, 0xff80ff, 0xffff00, 0xffffff
  58. };
  59. //解压缩
  60. foreach (var i in headPlate)
  61. {
  62. byte[] bytes = BitConverter.GetBytes(i);
  63. Color32 color32 = new Color32();
  64. color32.r = bytes[0];
  65. color32.g = bytes[1];
  66. color32.b = bytes[2];
  67. color32.a = 0xFF;
  68. PaletColors.Add(color32);
  69. }
  70. for (var i = 0; i < 224; i++)
  71. {
  72. byte[] paletBytes = paletReader.ReadBytes(3);
  73. // if(i<16 || i>224) continue;
  74. Color32 color32 = new Color32();
  75. color32.r = paletBytes[2];
  76. color32.g = paletBytes[1];
  77. color32.b = paletBytes[0];
  78. color32.a = 0xFF;
  79. PaletColors.Add(color32);
  80. }
  81. foreach (var i in footPlate)
  82. {
  83. byte[] bytes = BitConverter.GetBytes(i);
  84. Color32 color32 = new Color32();
  85. color32.r = bytes[0];
  86. color32.g = bytes[1];
  87. color32.b = bytes[2];
  88. color32.a = 0xFF;
  89. PaletColors.Add(color32);
  90. }
  91. //清理缓存
  92. paletReader.Dispose();
  93. paletReader.Close();
  94. paletFileStream.Dispose();
  95. paletFileStream.Close();
  96. return PaletColors;
  97. }
  98. }
  99. }