InfinityColorTextureCache.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. public class InfinityColorTextureCache
  6. {
  7. public InfinityColorTextureCache(Maid maid)
  8. {
  9. this.maid_ = maid;
  10. if (InfinityColorTextureCache.render_cache_ == null)
  11. {
  12. InfinityColorTextureCache.render_cache_ = new RenderTextureCache();
  13. }
  14. }
  15. public static RenderTextureCache render_cache
  16. {
  17. get
  18. {
  19. if (InfinityColorTextureCache.render_cache_ == null)
  20. {
  21. InfinityColorTextureCache.render_cache_ = new RenderTextureCache();
  22. }
  23. return InfinityColorTextureCache.render_cache_;
  24. }
  25. }
  26. public void Update()
  27. {
  28. InfinityColorTextureCache.render_cache_.Update();
  29. }
  30. private void AddTexture(int mat_no, string prop_name, Texture base_tex)
  31. {
  32. if (base_tex == null)
  33. {
  34. return;
  35. }
  36. InfinityColorTextureCache.TextureSet textureSet = this.GetTextureSet(mat_no, prop_name, true);
  37. textureSet.base_tex = base_tex;
  38. textureSet.modified_tex = null;
  39. textureSet.parts_color = MaidParts.PARTS_COLOR.NONE;
  40. }
  41. public void AddTexture(int mat_no, string prop_name, Texture base_tex, MaidParts.PARTS_COLOR parts_color)
  42. {
  43. if (base_tex == null)
  44. {
  45. return;
  46. }
  47. if (parts_color <= MaidParts.PARTS_COLOR.NONE || MaidParts.PARTS_COLOR.MAX <= parts_color)
  48. {
  49. this.AddTexture(mat_no, prop_name, base_tex);
  50. return;
  51. }
  52. InfinityColorTextureCache.TextureSet textureSet = this.GetTextureSet(mat_no, prop_name, true);
  53. textureSet.base_tex = base_tex;
  54. textureSet.parts_color = parts_color;
  55. Action<RenderTexture> value = delegate(RenderTexture tex)
  56. {
  57. InfinityColorTextureCache.TextureSet textureSet2 = null;
  58. foreach (KeyValuePair<int, Dictionary<string, InfinityColorTextureCache.TextureSet>> keyValuePair in this.tex_dic_)
  59. {
  60. Dictionary<string, InfinityColorTextureCache.TextureSet> value2 = keyValuePair.Value;
  61. foreach (KeyValuePair<string, InfinityColorTextureCache.TextureSet> keyValuePair2 in value2)
  62. {
  63. if (!(keyValuePair2.Value.modified_tex != tex))
  64. {
  65. textureSet2 = keyValuePair2.Value;
  66. }
  67. }
  68. }
  69. if (textureSet2 != null)
  70. {
  71. this.UpdateTexture(textureSet2.base_tex, textureSet2.parts_color, tex);
  72. }
  73. };
  74. textureSet.modified_tex = InfinityColorTextureCache.render_cache_.GetTexture(textureSet.base_tex.width, textureSet.base_tex.height, RenderTextureFormat.ARGB32, new KeyValuePair<int, Action<RenderTexture>>(10, value));
  75. this.UpdateTexture(textureSet.base_tex, textureSet.parts_color, textureSet.modified_tex);
  76. }
  77. public void RemoveTexture(int mat_no, string prop_name)
  78. {
  79. if (!this.tex_dic_.ContainsKey(mat_no))
  80. {
  81. return;
  82. }
  83. Dictionary<string, InfinityColorTextureCache.TextureSet> dictionary = this.tex_dic_[mat_no];
  84. if (!dictionary.ContainsKey(prop_name))
  85. {
  86. return;
  87. }
  88. InfinityColorTextureCache.TextureSet textureSet = dictionary[prop_name];
  89. if (textureSet == null)
  90. {
  91. return;
  92. }
  93. if (textureSet.base_tex != null)
  94. {
  95. if (textureSet.base_tex is RenderTexture)
  96. {
  97. InfinityColorTextureCache.render_cache_.RemoveTexture(textureSet.base_tex as RenderTexture);
  98. }
  99. else
  100. {
  101. UnityEngine.Object.Destroy(textureSet.base_tex);
  102. }
  103. }
  104. if (textureSet.modified_tex != null)
  105. {
  106. InfinityColorTextureCache.render_cache_.RemoveTexture(textureSet.modified_tex);
  107. }
  108. dictionary.Remove(prop_name);
  109. if (dictionary.Count <= 0)
  110. {
  111. this.tex_dic_.Remove(mat_no);
  112. }
  113. }
  114. public void RemoveTexture()
  115. {
  116. foreach (KeyValuePair<int, Dictionary<string, InfinityColorTextureCache.TextureSet>> keyValuePair in this.tex_dic_)
  117. {
  118. foreach (KeyValuePair<string, InfinityColorTextureCache.TextureSet> keyValuePair2 in keyValuePair.Value)
  119. {
  120. InfinityColorTextureCache.TextureSet value = keyValuePair2.Value;
  121. if (value != null)
  122. {
  123. if (value.base_tex != null)
  124. {
  125. if (value.base_tex is RenderTexture)
  126. {
  127. InfinityColorTextureCache.render_cache_.RemoveTexture(value.base_tex as RenderTexture);
  128. }
  129. else
  130. {
  131. UnityEngine.Object.Destroy(value.base_tex);
  132. }
  133. }
  134. if (value.modified_tex != null)
  135. {
  136. InfinityColorTextureCache.render_cache_.RemoveTexture(value.modified_tex);
  137. }
  138. }
  139. }
  140. }
  141. this.tex_dic_.Clear();
  142. }
  143. public bool UpdateTexture(int mat_no, string prop_name)
  144. {
  145. InfinityColorTextureCache.TextureSet textureSet = this.GetTextureSet(mat_no, prop_name, false);
  146. return this.UpdateTexture(textureSet.base_tex, textureSet.parts_color, textureSet.modified_tex);
  147. }
  148. public bool UpdateTexture(Texture base_tex, MaidParts.PARTS_COLOR parts_color, RenderTexture target_tex)
  149. {
  150. if (base_tex == null || target_tex == null)
  151. {
  152. return false;
  153. }
  154. if (parts_color <= MaidParts.PARTS_COLOR.NONE || MaidParts.PARTS_COLOR.MAX <= parts_color)
  155. {
  156. return false;
  157. }
  158. RenderTexture active = RenderTexture.active;
  159. Material systemMaterial = GameUty.GetSystemMaterial(GameUty.SystemMaterial.InfinityColor);
  160. systemMaterial.SetTexture("_MultiColTex", this.maid_.Parts.GetPartsColorTableTex(parts_color));
  161. Graphics.Blit(base_tex, target_tex, systemMaterial);
  162. RenderTexture.active = active;
  163. return true;
  164. }
  165. public bool UpdateColor(MaidParts.PARTS_COLOR parts_color)
  166. {
  167. if (parts_color <= MaidParts.PARTS_COLOR.NONE || MaidParts.PARTS_COLOR.MAX <= parts_color)
  168. {
  169. return false;
  170. }
  171. bool result = false;
  172. foreach (KeyValuePair<int, Dictionary<string, InfinityColorTextureCache.TextureSet>> keyValuePair in this.tex_dic_)
  173. {
  174. Dictionary<string, InfinityColorTextureCache.TextureSet> value = keyValuePair.Value;
  175. foreach (KeyValuePair<string, InfinityColorTextureCache.TextureSet> keyValuePair2 in value)
  176. {
  177. InfinityColorTextureCache.TextureSet value2 = keyValuePair2.Value;
  178. if (value2.parts_color == parts_color && value2.base_tex != null && value2.modified_tex != null)
  179. {
  180. result = true;
  181. this.UpdateTexture(value2.base_tex, value2.parts_color, value2.modified_tex);
  182. }
  183. }
  184. }
  185. return result;
  186. }
  187. public Texture GetModifiedTexture(int mat_no, string prop_name)
  188. {
  189. InfinityColorTextureCache.TextureSet textureSet = this.GetTextureSet(mat_no, prop_name, false);
  190. if (textureSet == null)
  191. {
  192. return null;
  193. }
  194. return (!(textureSet.modified_tex != null)) ? textureSet.base_tex : textureSet.modified_tex;
  195. }
  196. private InfinityColorTextureCache.TextureSet GetTextureSet(int mat_no, string prop_name, bool create_tex_set)
  197. {
  198. if (!this.tex_dic_.ContainsKey(mat_no))
  199. {
  200. if (!create_tex_set)
  201. {
  202. return null;
  203. }
  204. this.tex_dic_.Add(mat_no, new Dictionary<string, InfinityColorTextureCache.TextureSet>());
  205. }
  206. Dictionary<string, InfinityColorTextureCache.TextureSet> dictionary = this.tex_dic_[mat_no];
  207. if (!dictionary.ContainsKey(prop_name))
  208. {
  209. if (!create_tex_set)
  210. {
  211. return null;
  212. }
  213. dictionary.Add(prop_name, new InfinityColorTextureCache.TextureSet());
  214. }
  215. return dictionary[prop_name];
  216. }
  217. public static string GetTimeFileName()
  218. {
  219. string text = Path.Combine(GameMain.Instance.SerializeStorageManager.StoreDirectoryPath, "ScreenShot") + "/_tmp";
  220. if (!Directory.Exists(text))
  221. {
  222. Directory.CreateDirectory(text);
  223. }
  224. return text + "\\";
  225. }
  226. private static RenderTextureCache render_cache_;
  227. private Maid maid_;
  228. private Dictionary<int, Dictionary<string, InfinityColorTextureCache.TextureSet>> tex_dic_ = new Dictionary<int, Dictionary<string, InfinityColorTextureCache.TextureSet>>();
  229. public class TextureSet
  230. {
  231. public Texture base_tex;
  232. public RenderTexture modified_tex;
  233. public MaidParts.PARTS_COLOR parts_color;
  234. }
  235. }