Utility.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 AnyMouseDown()
  74. {
  75. return Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2);
  76. }
  77. public static string ScreenshotFilename()
  78. {
  79. string screenShotDir = Path.Combine(
  80. GameMain.Instance.SerializeStorageManager.StoreDirectoryPath, "ScreenShot"
  81. );
  82. if (!Directory.Exists(screenShotDir))
  83. {
  84. Directory.CreateDirectory(screenShotDir);
  85. }
  86. return Path.Combine(screenShotDir, $"img{DateTime.Now:yyyyMMddHHmmss}.png");
  87. }
  88. public static string TempScreenshotFilename()
  89. {
  90. return Path.Combine(Path.GetTempPath(), $"cm3d2_{System.Guid.NewGuid().ToString()}.png");
  91. }
  92. public static void ShowMouseExposition(string text, float time = 2f)
  93. {
  94. MouseExposition mouseExposition = MouseExposition.GetObject();
  95. mouseExposition.SetText(text, time);
  96. }
  97. public static bool IsGuidString(string guid)
  98. {
  99. if (string.IsNullOrEmpty(guid) || guid.Length != 36) return false;
  100. return guidRegEx.IsMatch(guid);
  101. }
  102. public static string HandItemToOdogu(string menu)
  103. {
  104. menu = menu.Substring(menu.IndexOf('_') + 1);
  105. menu = menu.Substring(0, menu.IndexOf("_i_.menu"));
  106. menu = $"odogu_{menu}";
  107. return menu;
  108. }
  109. public static void FixGameObjectScale(GameObject go)
  110. {
  111. Vector3 scale = go.transform.localScale;
  112. float largest = Mathf.Max(scale.x, Mathf.Max(scale.y, scale.z));
  113. go.transform.localScale = Vector3.one * (float)Math.Round(largest, 3);
  114. }
  115. public static string SanitizePathPortion(string path)
  116. {
  117. char[] invalid = Path.GetInvalidFileNameChars();
  118. path = path.Trim();
  119. path = string.Join("_", path.Split(invalid)).Replace(".", "").Trim('_');
  120. return path;
  121. }
  122. public static string GP01FbFaceHash(TMorph face, string hash)
  123. {
  124. if ((face.bodyskin.PartsVersion >= 120) && (hash != "eyeclose3") && hash.StartsWith("eyeclose"))
  125. {
  126. if (hash == "eyeclose") hash += '1';
  127. hash += TMorph.crcFaceTypesStr[(int)face.GetFaceTypeGP01FB()];
  128. }
  129. return hash;
  130. }
  131. public static void ResizeToFit(Texture2D texture, int maxWidth, int maxHeight)
  132. {
  133. int width = texture.width;
  134. int height = texture.height;
  135. if (width != maxWidth || height != maxHeight)
  136. {
  137. float scale = Mathf.Min((float)maxWidth / (float)width, (float)maxHeight / (float)height);
  138. width = Mathf.RoundToInt((float)width * scale);
  139. height = Mathf.RoundToInt((float)height * scale);
  140. TextureScale.Bilinear(texture, width, height);
  141. }
  142. }
  143. public static bool BytesEqual(byte[] buffer, byte[] other)
  144. {
  145. if (buffer.Length != other.Length) return false;
  146. for (int i = 0; i < buffer.Length; i++)
  147. {
  148. if (buffer[i] != other[i]) return false;
  149. }
  150. return true;
  151. }
  152. public static bool IsPngFile(Stream stream)
  153. {
  154. byte[] buffer = new byte[8];
  155. stream.Read(buffer, 0, 8);
  156. stream.Position = 0L;
  157. return BytesEqual(buffer, pngHeader);
  158. }
  159. public static bool SeekPngEnd(Stream stream)
  160. {
  161. byte[] buffer = new byte[8];
  162. stream.Read(buffer, 0, 8);
  163. if (!BytesEqual(buffer, pngHeader)) return false;
  164. buffer = new byte[4];
  165. do
  166. {
  167. stream.Read(buffer, 0, 4);
  168. if (BitConverter.IsLittleEndian) Array.Reverse(buffer);
  169. uint length = System.BitConverter.ToUInt32(buffer, 0);
  170. stream.Read(buffer, 0, 4);
  171. stream.Seek(length + 4L, SeekOrigin.Current);
  172. } while (!BytesEqual(buffer, pngEnd));
  173. return true;
  174. }
  175. }
  176. internal static class BinaryExtensions
  177. {
  178. public static string ReadNullableString(this BinaryReader binaryReader)
  179. {
  180. return binaryReader.ReadBoolean() ? binaryReader.ReadString() : null;
  181. }
  182. public static void WriteNullableString(this BinaryWriter binaryWriter, string str)
  183. {
  184. binaryWriter.Write(str != null);
  185. if (str != null) binaryWriter.Write(str);
  186. }
  187. public static void WriteVector3(this BinaryWriter binaryWriter, UnityEngine.Vector3 vector3)
  188. {
  189. binaryWriter.Write(vector3.x);
  190. binaryWriter.Write(vector3.y);
  191. binaryWriter.Write(vector3.z);
  192. }
  193. public static UnityEngine.Vector3 ReadVector3(this BinaryReader binaryReader)
  194. {
  195. return new UnityEngine.Vector3(
  196. binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()
  197. );
  198. }
  199. public static void WriteQuaternion(this BinaryWriter binaryWriter, UnityEngine.Quaternion quaternion)
  200. {
  201. binaryWriter.Write(quaternion.x);
  202. binaryWriter.Write(quaternion.y);
  203. binaryWriter.Write(quaternion.z);
  204. binaryWriter.Write(quaternion.w);
  205. }
  206. public static UnityEngine.Quaternion ReadQuaternion(this BinaryReader binaryReader)
  207. {
  208. return new UnityEngine.Quaternion
  209. (
  210. binaryReader.ReadSingle(), binaryReader.ReadSingle(),
  211. binaryReader.ReadSingle(), binaryReader.ReadSingle()
  212. );
  213. }
  214. public static void WriteColour(this BinaryWriter binaryWriter, UnityEngine.Color colour)
  215. {
  216. binaryWriter.Write(colour.r);
  217. binaryWriter.Write(colour.g);
  218. binaryWriter.Write(colour.b);
  219. binaryWriter.Write(colour.a);
  220. }
  221. public static UnityEngine.Color ReadColour(this BinaryReader binaryReader)
  222. {
  223. return new Color(
  224. binaryReader.ReadSingle(),
  225. binaryReader.ReadSingle(),
  226. binaryReader.ReadSingle(),
  227. binaryReader.ReadSingle()
  228. );
  229. }
  230. }
  231. }