Utility.cs 9.5 KB

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