Utility.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.IO;
  4. using System.Reflection;
  5. using UnityEngine;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. internal static class Utility
  9. {
  10. internal static readonly byte[] pngHeader = { 137, 80, 78, 71, 13, 10, 26, 10 };
  11. internal static readonly byte[] pngEnd = System.Text.Encoding.ASCII.GetBytes("IEND");
  12. internal static readonly Regex guidRegEx = new Regex(
  13. @"^[a-f0-9]{8}(\-[a-f0-9]{4}){3}\-[a-f0-9]{12}$", RegexOptions.IgnoreCase
  14. );
  15. public static readonly BepInEx.Logging.ManualLogSource Logger
  16. = BepInEx.Logging.Logger.CreateLogSource(MeidoPhotoStudio.pluginName);
  17. public enum ModKey
  18. {
  19. Control, Shift, Alt
  20. }
  21. public static void LogInfo(object data) => Logger.LogInfo(data);
  22. public static void LogMessage(object data) => Logger.LogInfo(data);
  23. public static void LogWarning(object data) => Logger.LogWarning(data);
  24. public static void LogError(object data) => Logger.LogError(data);
  25. public static void LogDebug(object data) => Logger.LogDebug(data);
  26. public static int Wrap(int value, int min, int max)
  27. {
  28. max -= 1;
  29. return value < min ? max : value > max ? min : value;
  30. }
  31. public static int GetPix(int num)
  32. {
  33. return (int)((1f + (Screen.width / 1280f - 1f) * 0.6f) * num);
  34. }
  35. public static float Bound(float value, float left, float right)
  36. {
  37. if ((double)left > (double)right) return Mathf.Clamp(value, right, left);
  38. else return Mathf.Clamp(value, left, right);
  39. }
  40. public static int Bound(int value, int left, int right)
  41. {
  42. if (left > right) return Mathf.Clamp(value, right, left);
  43. else return Mathf.Clamp(value, left, right);
  44. }
  45. public static Texture2D MakeTex(int width, int height, Color color)
  46. {
  47. Color[] colors = new Color[width * height];
  48. for (int i = 0; i < colors.Length; i++)
  49. {
  50. colors[i] = color;
  51. }
  52. Texture2D texture2D = new Texture2D(width, height);
  53. texture2D.SetPixels(colors);
  54. texture2D.Apply();
  55. return texture2D;
  56. }
  57. public static FieldInfo GetFieldInfo<T>(string field)
  58. {
  59. BindingFlags bindingFlags = BindingFlags.Instance
  60. | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
  61. return typeof(T).GetField(field, bindingFlags);
  62. }
  63. public static TValue GetFieldValue<TType, TValue>(TType instance, string field)
  64. {
  65. FieldInfo fieldInfo = GetFieldInfo<TType>(field);
  66. if (fieldInfo == null || !fieldInfo.IsStatic && instance == null) return default(TValue);
  67. return (TValue)fieldInfo.GetValue(instance);
  68. }
  69. public static void SetFieldValue<TType, TValue>(TType instance, string name, TValue value)
  70. {
  71. GetFieldInfo<TType>(name).SetValue(instance, value);
  72. }
  73. public static bool GetModKey(ModKey key)
  74. {
  75. switch (key)
  76. {
  77. case ModKey.Control: return Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
  78. case ModKey.Alt: return Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
  79. case ModKey.Shift: return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  80. default: return false;
  81. }
  82. }
  83. public static bool AnyMouseDown()
  84. {
  85. return Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2);
  86. }
  87. public static string ScreenshotFilename()
  88. {
  89. string screenShotDir = Path.Combine(
  90. GameMain.Instance.SerializeStorageManager.StoreDirectoryPath, "ScreenShot"
  91. );
  92. if (!Directory.Exists(screenShotDir))
  93. {
  94. Directory.CreateDirectory(screenShotDir);
  95. }
  96. return Path.Combine(screenShotDir, $"img{DateTime.Now:yyyyMMddHHmmss}.png");
  97. }
  98. public static string TempScreenshotFilename()
  99. {
  100. return Path.Combine(Path.GetTempPath(), $"cm3d2_{System.Guid.NewGuid().ToString()}.png");
  101. }
  102. public static void ShowMouseExposition(string text, float time = 2f)
  103. {
  104. MouseExposition mouseExposition = MouseExposition.GetObject();
  105. mouseExposition.SetText(text, time);
  106. }
  107. public static bool IsGuidString(string guid)
  108. {
  109. if (string.IsNullOrEmpty(guid) || guid.Length != 36) return false;
  110. return guidRegEx.IsMatch(guid);
  111. }
  112. public static string HandItemToOdogu(string menu)
  113. {
  114. menu = menu.Substring(menu.IndexOf('_') + 1);
  115. menu = menu.Substring(0, menu.IndexOf("_i_.menu"));
  116. menu = $"odogu_{menu}";
  117. return menu;
  118. }
  119. public static void FixGameObjectScale(GameObject go)
  120. {
  121. Vector3 scale = go.transform.localScale;
  122. float largest = Mathf.Max(scale.x, Mathf.Max(scale.y, scale.z));
  123. go.transform.localScale = Vector3.one * (float)Math.Round(largest, 3);
  124. }
  125. public static string SanitizePathPortion(string path)
  126. {
  127. char[] invalid = Path.GetInvalidFileNameChars();
  128. path = path.Trim();
  129. path = string.Join("_", path.Split(invalid)).Replace(".", "").Trim('_');
  130. return path;
  131. }
  132. public static string GP01FbFaceHash(TMorph face, string hash)
  133. {
  134. if ((face.bodyskin.PartsVersion >= 120) && (hash != "eyeclose3") && hash.StartsWith("eyeclose"))
  135. {
  136. if (hash == "eyeclose") hash += '1';
  137. hash += TMorph.crcFaceTypesStr[(int)face.GetFaceTypeGP01FB()];
  138. }
  139. return hash;
  140. }
  141. public static void ResizeToFit(Texture2D texture, int maxWidth, int maxHeight)
  142. {
  143. int width = texture.width;
  144. int height = texture.height;
  145. if (width != maxWidth || height != maxHeight)
  146. {
  147. float scale = Mathf.Min((float)maxWidth / (float)width, (float)maxHeight / (float)height);
  148. width = Mathf.RoundToInt((float)width * scale);
  149. height = Mathf.RoundToInt((float)height * scale);
  150. TextureScale.Bilinear(texture, width, height);
  151. }
  152. }
  153. public static bool BytesEqual(byte[] buffer, byte[] other)
  154. {
  155. if (buffer.Length != other.Length) return false;
  156. for (int i = 0; i < buffer.Length; i++)
  157. {
  158. if (buffer[i] != other[i]) return false;
  159. }
  160. return true;
  161. }
  162. public static bool IsPngFile(Stream stream)
  163. {
  164. byte[] buffer = new byte[8];
  165. stream.Read(buffer, 0, 8);
  166. stream.Position = 0L;
  167. return BytesEqual(buffer, pngHeader);
  168. }
  169. public static bool SeekPngEnd(Stream stream)
  170. {
  171. byte[] buffer = new byte[8];
  172. stream.Read(buffer, 0, 8);
  173. if (!BytesEqual(buffer, pngHeader)) return false;
  174. buffer = new byte[4];
  175. do
  176. {
  177. stream.Read(buffer, 0, 4);
  178. if (BitConverter.IsLittleEndian) Array.Reverse(buffer);
  179. uint length = System.BitConverter.ToUInt32(buffer, 0);
  180. stream.Read(buffer, 0, 4);
  181. stream.Seek(length + 4L, SeekOrigin.Current);
  182. } while (!BytesEqual(buffer, pngEnd));
  183. return true;
  184. }
  185. }
  186. internal static class BinaryExtensions
  187. {
  188. public static string ReadNullableString(this BinaryReader binaryReader)
  189. {
  190. return binaryReader.ReadBoolean() ? binaryReader.ReadString() : null;
  191. }
  192. public static void WriteNullableString(this BinaryWriter binaryWriter, string str)
  193. {
  194. binaryWriter.Write(str != null);
  195. if (str != null) binaryWriter.Write(str);
  196. }
  197. public static void WriteVector3(this BinaryWriter binaryWriter, UnityEngine.Vector3 vector3)
  198. {
  199. binaryWriter.Write(vector3.x);
  200. binaryWriter.Write(vector3.y);
  201. binaryWriter.Write(vector3.z);
  202. }
  203. public static UnityEngine.Vector3 ReadVector3(this BinaryReader binaryReader)
  204. {
  205. return new UnityEngine.Vector3(
  206. binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()
  207. );
  208. }
  209. public static void WriteQuaternion(this BinaryWriter binaryWriter, UnityEngine.Quaternion quaternion)
  210. {
  211. binaryWriter.Write(quaternion.x);
  212. binaryWriter.Write(quaternion.y);
  213. binaryWriter.Write(quaternion.z);
  214. binaryWriter.Write(quaternion.w);
  215. }
  216. public static UnityEngine.Quaternion ReadQuaternion(this BinaryReader binaryReader)
  217. {
  218. return new UnityEngine.Quaternion
  219. (
  220. binaryReader.ReadSingle(), binaryReader.ReadSingle(),
  221. binaryReader.ReadSingle(), binaryReader.ReadSingle()
  222. );
  223. }
  224. public static void WriteColour(this BinaryWriter binaryWriter, UnityEngine.Color colour)
  225. {
  226. binaryWriter.Write(colour.r);
  227. binaryWriter.Write(colour.g);
  228. binaryWriter.Write(colour.b);
  229. binaryWriter.Write(colour.a);
  230. }
  231. public static UnityEngine.Color ReadColour(this BinaryReader binaryReader)
  232. {
  233. return new Color(
  234. binaryReader.ReadSingle(),
  235. binaryReader.ReadSingle(),
  236. binaryReader.ReadSingle(),
  237. binaryReader.ReadSingle()
  238. );
  239. }
  240. }
  241. }