ToukaScreenShot.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Reflection;
  7. using ExIni;
  8. using UnityEngine;
  9. using UnityInjector;
  10. using UnityInjector.Attributes;
  11. namespace CM3D2.ToukaScreenShot.Plugin
  12. {
  13. [PluginVersion("0.1.2.0")]
  14. [PluginName("Com3d2.ToukaScreenShot.Plugin")]
  15. [PluginFilter("COM3D2x64")]
  16. public class ToukaScreenShot : PluginBase
  17. {
  18. private readonly HashSet<int> defaultVisibleLayers = new HashSet<int>
  19. {
  20. 0,
  21. 1,
  22. 10
  23. };
  24. private readonly FieldInfo fBloom =
  25. typeof(CameraMain).GetField("m_gcBloom", BindingFlags.Instance | BindingFlags.NonPublic);
  26. private int layerMask = 0;
  27. private bool altKey;
  28. private bool bgActiveBack = true;
  29. private Color bgColorBack = Color.black;
  30. private bool bgVisible = true;
  31. private bool bloomBack;
  32. private bool bloomOff = true;
  33. private bool ctrlKey = true;
  34. private string fileNameEnd = "";
  35. private string fileNameHead = "img";
  36. private string folderName = "ScreenShot";
  37. private Camera mainCamera;
  38. private int maskBack;
  39. private bool noConfigFlg;
  40. private bool shiftKey;
  41. private string triggerKey = "s";
  42. private string photoShootKey = "d";
  43. private void Start()
  44. {
  45. for (var i = 0; i < sizeof(int) * 8; i++)
  46. if (GetValueIni("Visible", $"Layer{i}", defaultVisibleLayers.Contains(i)))
  47. layerMask |= 1 << i;
  48. folderName = GetValueIni("File", "Folder", "ScreenShot");
  49. fileNameHead = GetValueIni("File", "Head", "img");
  50. fileNameEnd = GetValueIni("File", "End", "");
  51. bgVisible = GetValueIni("Visible", "BG", false);
  52. bloomOff = GetValueIni("Effect", "BloomOff", true);
  53. altKey = GetValueIni("Command", "Alt", false);
  54. ctrlKey = GetValueIni("Command", "Ctrl", true);
  55. shiftKey = GetValueIni("Command", "Shift", false);
  56. triggerKey = GetValueIni("Command", "Trigger", "s");
  57. photoShootKey = GetValueIni("Command", "Trigger", "d");
  58. if (noConfigFlg) SaveConfig();
  59. }
  60. private T GetValueIni<T>(string section, string key, T @default)
  61. {
  62. bool TryParse(string value, out T resultVal)
  63. {
  64. try
  65. {
  66. resultVal = (T) Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
  67. return true;
  68. }
  69. catch (Exception e)
  70. {
  71. Debug.LogWarning($"Failed to parse {section} => {key} value {value} because {e.Message}");
  72. resultVal = default;
  73. return false;
  74. }
  75. }
  76. var iniKey = Preferences[section][key];
  77. if (iniKey != null && !string.IsNullOrEmpty(iniKey.Value) &&
  78. TryParse(iniKey.Value, out var result)) return result;
  79. iniKey.Value = Convert.ToString(@default, CultureInfo.InvariantCulture);
  80. result = @default;
  81. noConfigFlg = true;
  82. return result;
  83. }
  84. private void Update()
  85. {
  86. var pressedAlt = Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
  87. var pressedCtrl = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
  88. var pressedShift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  89. if (altKey == pressedAlt && ctrlKey == pressedCtrl && shiftKey == pressedShift && Input.GetKeyDown(triggerKey))
  90. StartCoroutine(ExecScreenShot());
  91. else if (altKey == pressedAlt && ctrlKey == pressedCtrl && shiftKey == pressedShift && Input.GetKeyDown(photoShootKey))
  92. StartCoroutine(StartPhotoShoot());
  93. }
  94. private IEnumerator StartPhotoShoot()
  95. {
  96. yield return new WaitForEndOfFrame();
  97. var activeMaidCount = GameMain.Instance.CharacterMgr.GetMaidCount();
  98. Maid activeMaid = null;
  99. for (var i = 0; i < activeMaidCount; i++)
  100. if ((activeMaid = GameMain.Instance.CharacterMgr.GetMaid(i)).Visible)
  101. break;
  102. if (activeMaid == null)
  103. {
  104. Debug.Log("No maid found!");
  105. yield break;
  106. }
  107. var poseList =
  108. Directory.GetFiles(Path.Combine(UTY.gameProjectPath, Path.Combine("PhotoModeData", "MyPose")));
  109. Debug.Log($"Maid to use: {activeMaid.name}");
  110. Debug.Log($"Pose count: {poseList.Length}");
  111. for (var poseNum = 0; poseNum < poseList.Length; poseNum++)
  112. {
  113. Debug.Log($"{poseNum + 1} / {poseList.Length}");
  114. var poseFile = poseList[poseNum];
  115. PhotoMotionData.AddMyPose(poseFile).Apply(activeMaid);
  116. yield return ExecScreenShot();
  117. }
  118. Debug.Log("Done!");
  119. }
  120. private IEnumerator ExecScreenShot()
  121. {
  122. yield return new WaitForEndOfFrame();
  123. mainCamera = Camera.main;
  124. BackUpCamera();
  125. var ss = GetToukaScreenShot();
  126. if (ss != null)
  127. {
  128. File.WriteAllBytes(GetTimeFileName(), ss.EncodeToPNG());
  129. Destroy(ss);
  130. }
  131. // Actually unload textures and GC unused stuff to free up memory
  132. yield return Resources.UnloadUnusedAssets();
  133. GC.Collect();
  134. GameMain.Instance.SoundMgr.PlaySe("se022.ogg", false);
  135. }
  136. private Texture2D GetToukaScreenShot()
  137. {
  138. var ss = GameMain.Instance.CMSystem.ScreenShotSuperSize switch
  139. {
  140. CMSystem.SSSuperSizeType.X1 => 1,
  141. CMSystem.SSSuperSizeType.X2 => 2,
  142. CMSystem.SSSuperSizeType.X4 => 4,
  143. _ => 1
  144. };
  145. var aa = GameMain.Instance.CMSystem.Antialias switch
  146. {
  147. CMSystem.AntiAliasType.None => 0,
  148. CMSystem.AntiAliasType.X2 => 2,
  149. CMSystem.AntiAliasType.X4 => 4,
  150. CMSystem.AntiAliasType.X8 => 8,
  151. _ => 8,
  152. };
  153. var w = Screen.width * ss;
  154. var h = Screen.height * ss;
  155. // Cannot use RenderTexture.CreateTemporary as apparently it can fail on lower end PCs
  156. var rt = new RenderTexture(w, h, 24, RenderTextureFormat.ARGB32);
  157. if (aa != 0)
  158. rt.antiAliasing = aa;
  159. rt.filterMode = FilterMode.Bilinear;
  160. SetCameraMask();
  161. mainCamera.backgroundColor = new Color(0f, 0f, 0f, 1f);
  162. var blackBgTex = RenderScreenShot(rt, w, h);
  163. mainCamera.backgroundColor = new Color(1f, 1f, 1f, 1f);
  164. var whiteBgTex = RenderScreenShot(rt, w, h);
  165. ResetCamera();
  166. mainCamera.targetTexture = null;
  167. rt.Release();
  168. Destroy(rt);
  169. var blackPix = blackBgTex.GetPixels32();
  170. var whitePix = whiteBgTex.GetPixels32();
  171. var resultPix = new Color32[blackPix.Length];
  172. for (var i = 0; i < resultPix.Length; i++)
  173. {
  174. var wp = whitePix[i];
  175. var bp = blackPix[i];
  176. var diff = wp.r - bp.r;
  177. if (diff < 0) diff = 0;
  178. resultPix[i] = new Color32((byte)((wp.r + bp.r) >> 1), (byte)((wp.g + bp.g) >> 1), (byte)((wp.b + bp.b) >> 1), (byte) ~diff);
  179. }
  180. var result = new Texture2D(w, h);
  181. result.SetPixels32(resultPix);
  182. // Mark both for destroying so they can be unloaded by UnloadUnusedAssets
  183. Destroy(blackBgTex);
  184. Destroy(whiteBgTex);
  185. return result;
  186. }
  187. private string GetTimeFileName()
  188. {
  189. var targetFolder = Path.Combine(UTY.gameProjectPath, folderName);
  190. if (!Directory.Exists(targetFolder)) Directory.CreateDirectory(targetFolder);
  191. return Path.Combine(targetFolder, $"{fileNameHead}{DateTime.Now:yyyyMMddHHmmss}{fileNameEnd}.png");
  192. }
  193. private void BackUpCamera()
  194. {
  195. maskBack = mainCamera.cullingMask;
  196. bgColorBack = mainCamera.backgroundColor;
  197. if (!bgVisible) bgActiveBack = GameMain.Instance.BgMgr.current_bg_object.activeSelf;
  198. if (!bloomOff) return;
  199. var bloom = (Bloom) fBloom.GetValue(GameMain.Instance.MainCamera);
  200. bloomBack = bloom.enabled;
  201. }
  202. private void ResetCamera()
  203. {
  204. mainCamera.cullingMask = maskBack;
  205. mainCamera.backgroundColor = bgColorBack;
  206. if (!bgVisible) GameMain.Instance.BgMgr.current_bg_object.SetActive(bgActiveBack);
  207. if (!bloomOff) return;
  208. var bloom = (Bloom) fBloom.GetValue(GameMain.Instance.MainCamera);
  209. bloom.enabled = bloomBack;
  210. }
  211. private void SetCameraMask()
  212. {
  213. mainCamera.cullingMask = layerMask;
  214. if (!bgVisible) GameMain.Instance.BgMgr.current_bg_object.SetActive(false);
  215. if (!bloomOff) return;
  216. var bloom = (Bloom) fBloom.GetValue(GameMain.Instance.MainCamera);
  217. bloom.enabled = false;
  218. }
  219. private Texture2D RenderScreenShot(RenderTexture rt, int w, int h)
  220. {
  221. mainCamera.targetTexture = rt;
  222. mainCamera.Render();
  223. var texture2D = new Texture2D(w, h, TextureFormat.ARGB32, false);
  224. var prev = RenderTexture.active;
  225. RenderTexture.active = rt;
  226. texture2D.ReadPixels(new Rect(0f, 0f, w, h), 0, 0, false);
  227. RenderTexture.active = prev;
  228. return texture2D;
  229. }
  230. }
  231. }