SavePreset.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace COM3D2.PropMyItem.Plugin
  6. {
  7. // Token: 0x0200000B RID: 11
  8. public class SavePreset
  9. {
  10. // Token: 0x06000048 RID: 72 RVA: 0x00007AEC File Offset: 0x00005CEC
  11. public Texture2D ThumShot(Camera camera, Maid f_maid)
  12. {
  13. int width = 138;
  14. int height = 200;
  15. Transform transform = CMT.SearchObjName(f_maid.body0.m_Bones.transform, "Bip01 HeadNub", true);
  16. camera.transform.position = transform.TransformPoint(transform.localPosition + new Vector3(0.38f, 1.07f, 0f));
  17. camera.transform.rotation = transform.rotation * Quaternion.Euler(90f, 0f, 90f);
  18. camera.fieldOfView = 30f;
  19. RenderTexture renderTexture = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32);
  20. renderTexture.filterMode = FilterMode.Bilinear;
  21. renderTexture.antiAliasing = 8;
  22. RenderTexture f_rtSub = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32);
  23. Texture2D result = this.RenderThum(camera, renderTexture, f_rtSub, width, height);
  24. this.Restore();
  25. return result;
  26. }
  27. // Token: 0x06000049 RID: 73 RVA: 0x00007BBC File Offset: 0x00005DBC
  28. public void SaveRestore()
  29. {
  30. this._savedPos = GameMain.Instance.MainCamera.GetTargetPos();
  31. this._savedAngle = GameMain.Instance.MainCamera.GetAroundAngle();
  32. this._savedDistance = GameMain.Instance.MainCamera.GetDistance();
  33. this.px = GameMain.Instance.MainCamera.transform.position.x;
  34. this.py = GameMain.Instance.MainCamera.transform.position.y;
  35. this.pz = GameMain.Instance.MainCamera.transform.position.z;
  36. this.rx = GameMain.Instance.MainCamera.transform.rotation.x;
  37. this.ry = GameMain.Instance.MainCamera.transform.rotation.y;
  38. this.rz = GameMain.Instance.MainCamera.transform.rotation.z;
  39. this.rw = GameMain.Instance.MainCamera.transform.rotation.w;
  40. this.fov = GameMain.Instance.MainCamera.camera.fieldOfView;
  41. this.bg = GameMain.Instance.BgMgr.GetBGName();
  42. GameMain.Instance.BgMgr.DeleteBg();
  43. this.isStock = true;
  44. }
  45. // Token: 0x0600004A RID: 74 RVA: 0x00007D28 File Offset: 0x00005F28
  46. public void Restore()
  47. {
  48. if (this.isStock)
  49. {
  50. this.isStock = false;
  51. GameMain.Instance.BgMgr.ChangeBg(this.bg);
  52. GameMain.Instance.MainCamera.transform.position.Set(this.px, this.py, this.pz);
  53. GameMain.Instance.MainCamera.transform.rotation.Set(this.rx, this.ry, this.rz, this.rw);
  54. GameMain.Instance.MainCamera.camera.fieldOfView = this.fov;
  55. GameMain.Instance.MainCamera.SetTargetPos(this._savedPos, true);
  56. GameMain.Instance.MainCamera.SetAroundAngle(this._savedAngle, true);
  57. GameMain.Instance.MainCamera.SetDistance(this._savedDistance, true);
  58. }
  59. }
  60. // Token: 0x0600004B RID: 75 RVA: 0x00007E1C File Offset: 0x0000601C
  61. public Texture2D RenderThum(Camera f_cam, RenderTexture f_rtMain, RenderTexture f_rtSub, int width, int height)
  62. {
  63. RenderTexture targetTexture = f_cam.targetTexture;
  64. bool enabled = f_cam.enabled;
  65. f_cam.targetTexture = f_rtMain;
  66. f_cam.enabled = true;
  67. f_cam.Render();
  68. f_cam.enabled = false;
  69. Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);
  70. RenderTexture active = RenderTexture.active;
  71. RenderTexture.active = f_rtSub;
  72. GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
  73. GL.PushMatrix();
  74. GL.LoadPixelMatrix(0f, (float)width, (float)height, 0f);
  75. Graphics.DrawTexture(new Rect(0f, 0f, (float)width, (float)height), f_rtMain);
  76. GL.PopMatrix();
  77. texture2D.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0);
  78. texture2D.Apply();
  79. RenderTexture.active = active;
  80. f_cam.targetTexture = targetTexture;
  81. f_cam.enabled = enabled;
  82. return texture2D;
  83. }
  84. // Token: 0x0600004C RID: 76 RVA: 0x00007EFC File Offset: 0x000060FC
  85. public CharacterMgr.Preset PresetSave(Maid f_maid, CharacterMgr.PresetType f_type)
  86. {
  87. CharacterMgr.Preset preset = new CharacterMgr.Preset();
  88. Texture2D texture2D = this.ThumShot(GameMain.Instance.MainCamera.camera, f_maid);
  89. MemoryStream memoryStream = new MemoryStream();
  90. BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
  91. binaryWriter.Write("CM3D2_PRESET");
  92. binaryWriter.Write(1160);
  93. binaryWriter.Write((int)f_type);
  94. if (texture2D != null)
  95. {
  96. byte[] array = texture2D.EncodeToPNG();
  97. binaryWriter.Write(array.Length);
  98. binaryWriter.Write(array);
  99. }
  100. else
  101. {
  102. binaryWriter.Write(0);
  103. }
  104. f_maid.SerializeProp(binaryWriter);
  105. f_maid.SerializeMultiColor(binaryWriter);
  106. f_maid.SerializeBody(binaryWriter);
  107. string text = string.Concat(new string[]
  108. {
  109. "pre_",
  110. f_maid.status.lastName,
  111. f_maid.status.firstName,
  112. "_",
  113. DateTime.Now.ToString("yyyyMMddHHmmss")
  114. });
  115. text = UTY.FileNameEscape(text);
  116. text += ".preset";
  117. string text2 = Path.GetFullPath(".\\") + "Preset";
  118. if (!Directory.Exists(text2))
  119. {
  120. Directory.CreateDirectory(text2);
  121. }
  122. File.WriteAllBytes(text2 + "\\" + text, memoryStream.ToArray());
  123. memoryStream.Dispose();
  124. preset.texThum = texture2D;
  125. preset.strFileName = text;
  126. preset.ePreType = f_type;
  127. return preset;
  128. }
  129. // Token: 0x0600004D RID: 77 RVA: 0x00008058 File Offset: 0x00006258
  130. public string GetNewestFileName(string folderName)
  131. {
  132. string[] files = Directory.GetFiles(folderName, "*.preset", SearchOption.TopDirectoryOnly);
  133. string path = string.Empty;
  134. DateTime t = DateTime.MinValue;
  135. foreach (string text in files)
  136. {
  137. FileInfo fileInfo = new FileInfo(text);
  138. if (fileInfo.LastWriteTime > t)
  139. {
  140. t = fileInfo.LastWriteTime;
  141. path = text;
  142. }
  143. }
  144. return Path.GetFileName(path);
  145. }
  146. // Token: 0x0600004E RID: 78 RVA: 0x000080BC File Offset: 0x000062BC
  147. public byte[] LoadMenuInternal(string filename)
  148. {
  149. byte[] result;
  150. try
  151. {
  152. using (AFileBase afileBase = GameUty.FileOpen(filename, null))
  153. {
  154. if (!afileBase.IsValid())
  155. {
  156. throw new Exception();
  157. }
  158. result = afileBase.ReadAll();
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. throw ex;
  164. }
  165. return result;
  166. }
  167. // Token: 0x0600004F RID: 79 RVA: 0x00008114 File Offset: 0x00006314
  168. public int GetPriority(string fileName)
  169. {
  170. int result = 0;
  171. using (MemoryStream memoryStream = new MemoryStream(this.LoadMenuInternal(fileName), false))
  172. {
  173. using (BinaryReader binaryReader = new BinaryReader(memoryStream, Encoding.UTF8))
  174. {
  175. binaryReader.ReadString();
  176. binaryReader.ReadInt32();
  177. binaryReader.ReadString();
  178. binaryReader.ReadString();
  179. binaryReader.ReadString();
  180. binaryReader.ReadString();
  181. binaryReader.ReadInt32();
  182. for (;;)
  183. {
  184. int num = (int)binaryReader.ReadByte();
  185. if (num == 0)
  186. {
  187. break;
  188. }
  189. string a = binaryReader.ReadString();
  190. string[] array = new string[num - 1];
  191. for (int i = 0; i < num - 1; i++)
  192. {
  193. array[i] = binaryReader.ReadString();
  194. }
  195. if (a == "priority")
  196. {
  197. result = int.Parse(array[0]);
  198. }
  199. }
  200. }
  201. }
  202. return result;
  203. }
  204. // Token: 0x0400004D RID: 77
  205. private float px;
  206. // Token: 0x0400004E RID: 78
  207. private float py;
  208. // Token: 0x0400004F RID: 79
  209. private float pz;
  210. // Token: 0x04000050 RID: 80
  211. private float rx;
  212. // Token: 0x04000051 RID: 81
  213. private float ry;
  214. // Token: 0x04000052 RID: 82
  215. private float rz;
  216. // Token: 0x04000053 RID: 83
  217. private float rw;
  218. // Token: 0x04000054 RID: 84
  219. private float fov;
  220. // Token: 0x04000055 RID: 85
  221. private string bg = string.Empty;
  222. // Token: 0x04000056 RID: 86
  223. private bool isStock;
  224. // Token: 0x04000057 RID: 87
  225. private Vector3 _savedPos;
  226. // Token: 0x04000058 RID: 88
  227. private Vector2 _savedAngle;
  228. // Token: 0x04000059 RID: 89
  229. private float _savedDistance = 1f;
  230. }
  231. }