ToukaScreenShot.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.1.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 void Start()
  43. {
  44. for (var i = 0; i < sizeof(int) * 8; i++)
  45. if (GetValueIni("Visible", $"Layer{i}", defaultVisibleLayers.Contains(i)))
  46. layerMask |= 1 << i;
  47. folderName = GetValueIni("File", "Folder", "ScreenShot");
  48. fileNameHead = GetValueIni("File", "Head", "img");
  49. fileNameEnd = GetValueIni("File", "End", "");
  50. bgVisible = GetValueIni("Visible", "BG", false);
  51. bloomOff = GetValueIni("Effect", "BloomOff", true);
  52. altKey = GetValueIni("Command", "Alt", false);
  53. ctrlKey = GetValueIni("Command", "Ctrl", true);
  54. shiftKey = GetValueIni("Command", "Shift", false);
  55. triggerKey = GetValueIni("Command", "Trigger", "s");
  56. if (noConfigFlg) SaveConfig();
  57. }
  58. private T GetValueIni<T>(string section, string key, T @default)
  59. {
  60. bool TryParse(string value, out T resultVal)
  61. {
  62. try
  63. {
  64. resultVal = (T) Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
  65. return true;
  66. }
  67. catch (Exception e)
  68. {
  69. Debug.LogWarning($"Failed to parse {section} => {key} value {value} because {e.Message}");
  70. resultVal = default;
  71. return false;
  72. }
  73. }
  74. var iniKey = Preferences[section][key];
  75. if (iniKey != null && !string.IsNullOrEmpty(iniKey.Value) &&
  76. TryParse(iniKey.Value, out var result)) return result;
  77. iniKey.Value = Convert.ToString(@default, CultureInfo.InvariantCulture);
  78. result = @default;
  79. noConfigFlg = true;
  80. return result;
  81. }
  82. private void Update()
  83. {
  84. var pressedAlt = Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
  85. var pressedCtrl = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
  86. var pressedShift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  87. if (altKey == pressedAlt && ctrlKey == pressedCtrl && shiftKey == pressedShift && Input.GetKeyDown(triggerKey))
  88. StartCoroutine(ExecScreenShot());
  89. }
  90. private IEnumerator ExecScreenShot()
  91. {
  92. yield return new WaitForEndOfFrame();
  93. mainCamera = Camera.main;
  94. BackUpCamera();
  95. var ss = GetToukaScreenShot();
  96. File.WriteAllBytes(GetTimeFileName(), ss.EncodeToPNG());
  97. Destroy(ss);
  98. // Actually unload textures and GC unused stuff to free up memory
  99. yield return Resources.UnloadUnusedAssets();
  100. GC.Collect();
  101. GameMain.Instance.SoundMgr.PlaySe("se022.ogg", false);
  102. }
  103. private Texture2D GetToukaScreenShot()
  104. {
  105. var ss = GameMain.Instance.CMSystem.ScreenShotSuperSize switch
  106. {
  107. CMSystem.SSSuperSizeType.X1 => 1,
  108. CMSystem.SSSuperSizeType.X2 => 2,
  109. CMSystem.SSSuperSizeType.X4 => 4,
  110. _ => 1
  111. };
  112. var aa = GameMain.Instance.CMSystem.Antialias switch
  113. {
  114. CMSystem.AntiAliasType.None => 0,
  115. CMSystem.AntiAliasType.X2 => 2,
  116. CMSystem.AntiAliasType.X4 => 4,
  117. CMSystem.AntiAliasType.X8 => 8,
  118. _ => 8,
  119. };
  120. var w = Screen.width * ss;
  121. var h = Screen.height * ss;
  122. var rt = RenderTexture.GetTemporary(w, h, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB, aa);
  123. rt.filterMode = FilterMode.Bilinear;
  124. SetCameraMask();
  125. mainCamera.backgroundColor = new Color(0f, 0f, 0f, 1f);
  126. var blackBgTex = RenderScreenShot(rt, w, h);
  127. mainCamera.backgroundColor = new Color(1f, 1f, 1f, 1f);
  128. var whiteBgTex = RenderScreenShot(rt, w, h);
  129. ResetCamera();
  130. mainCamera.targetTexture = null;
  131. RenderTexture.ReleaseTemporary(rt);
  132. var blackPix = blackBgTex.GetPixels32();
  133. var whitePix = whiteBgTex.GetPixels32();
  134. var resultPix = new Color32[blackPix.Length];
  135. for (var i = 0; i < resultPix.Length; i++)
  136. {
  137. var wp = whitePix[i];
  138. var bp = blackPix[i];
  139. var diff = wp.r - bp.r;
  140. if (diff < 0) diff = 0;
  141. resultPix[i] = new Color32((byte)((wp.r + bp.r) >> 1), (byte)((wp.g + bp.g) >> 1), (byte)((wp.b + bp.b) >> 1), (byte) ~diff);
  142. }
  143. var result = new Texture2D(w, h);
  144. result.SetPixels32(resultPix);
  145. // Mark both for destroying so they can be unloaded by UnloadUnusedAssets
  146. Destroy(blackBgTex);
  147. Destroy(whiteBgTex);
  148. return result;
  149. }
  150. private string GetTimeFileName()
  151. {
  152. var targetFolder = Path.Combine(UTY.gameProjectPath, folderName);
  153. if (!Directory.Exists(targetFolder)) Directory.CreateDirectory(targetFolder);
  154. return Path.Combine(targetFolder, $"{fileNameHead}{DateTime.Now:yyyyMMddHHmmss}{fileNameEnd}.png");
  155. }
  156. private void BackUpCamera()
  157. {
  158. maskBack = mainCamera.cullingMask;
  159. bgColorBack = mainCamera.backgroundColor;
  160. if (!bgVisible) bgActiveBack = GameMain.Instance.BgMgr.current_bg_object.activeSelf;
  161. if (!bloomOff) return;
  162. var bloom = (Bloom) fBloom.GetValue(GameMain.Instance.MainCamera);
  163. bloomBack = bloom.enabled;
  164. }
  165. private void ResetCamera()
  166. {
  167. mainCamera.cullingMask = maskBack;
  168. mainCamera.backgroundColor = bgColorBack;
  169. if (!bgVisible) GameMain.Instance.BgMgr.current_bg_object.SetActive(bgActiveBack);
  170. if (!bloomOff) return;
  171. var bloom = (Bloom) fBloom.GetValue(GameMain.Instance.MainCamera);
  172. bloom.enabled = bloomBack;
  173. }
  174. private void SetCameraMask()
  175. {
  176. mainCamera.cullingMask = layerMask;
  177. if (!bgVisible) GameMain.Instance.BgMgr.current_bg_object.SetActive(false);
  178. if (!bloomOff) return;
  179. var bloom = (Bloom) fBloom.GetValue(GameMain.Instance.MainCamera);
  180. bloom.enabled = false;
  181. }
  182. private Texture2D RenderScreenShot(RenderTexture rt, int w, int h)
  183. {
  184. mainCamera.targetTexture = rt;
  185. mainCamera.Render();
  186. var texture2D = new Texture2D(w, h, TextureFormat.ARGB32, false);
  187. var prev = RenderTexture.active;
  188. RenderTexture.active = rt;
  189. texture2D.ReadPixels(new Rect(0f, 0f, w, h), 0, 0, false);
  190. RenderTexture.active = prev;
  191. return texture2D;
  192. }
  193. }
  194. }