Utility.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. public static readonly BepInEx.Logging.ManualLogSource Logger
  10. = BepInEx.Logging.Logger.CreateLogSource(MeidoPhotoStudio.pluginName);
  11. public enum ModKey
  12. {
  13. Control, Shift, Alt
  14. }
  15. public static void LogInfo(object data) => Logger.LogInfo(data);
  16. public static void LogMessage(object data) => Logger.LogInfo(data);
  17. public static void LogWarning(object data) => Logger.LogWarning(data);
  18. public static void LogError(object data) => Logger.LogError(data);
  19. public static void LogDebug(object data) => Logger.LogDebug(data);
  20. public static int Wrap(int value, int min, int max)
  21. {
  22. max -= 1;
  23. return value < min ? max : value > max ? min : value;
  24. }
  25. public static int GetPix(int num)
  26. {
  27. return (int)((1f + (Screen.width / 1280f - 1f) * 0.6f) * num);
  28. }
  29. public static float Bound(float value, float left, float right)
  30. {
  31. if ((double)left > (double)right) return Mathf.Clamp(value, right, left);
  32. else return Mathf.Clamp(value, left, right);
  33. }
  34. public static int Bound(int value, int left, int right)
  35. {
  36. if (left > right) return Mathf.Clamp(value, right, left);
  37. else return Mathf.Clamp(value, left, right);
  38. }
  39. public static Texture2D MakeTex(int width, int height, Color color)
  40. {
  41. Color[] colors = new Color[width * height];
  42. for (int i = 0; i < colors.Length; i++)
  43. {
  44. colors[i] = color;
  45. }
  46. Texture2D texture2D = new Texture2D(width, height);
  47. texture2D.SetPixels(colors);
  48. texture2D.Apply();
  49. return texture2D;
  50. }
  51. public static FieldInfo GetFieldInfo<T>(string field)
  52. {
  53. BindingFlags bindingFlags = BindingFlags.Instance
  54. | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
  55. return typeof(T).GetField(field, bindingFlags);
  56. }
  57. public static TValue GetFieldValue<TType, TValue>(TType instance, string field)
  58. {
  59. FieldInfo fieldInfo = GetFieldInfo<TType>(field);
  60. if (fieldInfo == null || !fieldInfo.IsStatic && instance == null) return default(TValue);
  61. return (TValue)fieldInfo.GetValue(instance);
  62. }
  63. public static void SetFieldValue<TType, TValue>(TType instance, string name, TValue value)
  64. {
  65. GetFieldInfo<TType>(name).SetValue(instance, value);
  66. }
  67. public static bool GetModKey(ModKey key)
  68. {
  69. switch (key)
  70. {
  71. case ModKey.Control: return Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
  72. case ModKey.Alt: return Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
  73. case ModKey.Shift: return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  74. default: return false;
  75. }
  76. }
  77. public static bool AnyMouseDown()
  78. {
  79. return Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2);
  80. }
  81. public static string ScreenshotFilename()
  82. {
  83. string screenShotDir = Path.Combine(
  84. GameMain.Instance.SerializeStorageManager.StoreDirectoryPath, "ScreenShot"
  85. );
  86. if (!Directory.Exists(screenShotDir))
  87. {
  88. Directory.CreateDirectory(screenShotDir);
  89. }
  90. return Path.Combine(screenShotDir, $"img{DateTime.Now:yyyyMMddHHmmss}.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 string HandItemToOdogu(string menu)
  98. {
  99. menu = menu.Substring(menu.IndexOf('_') + 1);
  100. menu = menu.Substring(0, menu.IndexOf("_i_.menu"));
  101. menu = $"odogu_{menu}";
  102. return menu;
  103. }
  104. public static void FixGameObjectScale(GameObject go)
  105. {
  106. Vector3 scale = go.transform.localScale;
  107. float largest = Mathf.Max(scale.x, Mathf.Max(scale.y, scale.z));
  108. go.transform.localScale = Vector3.one * (float)Math.Round(largest, 3);
  109. }
  110. public static string SanitizePathPortion(string path)
  111. {
  112. char[] invalid = Path.GetInvalidFileNameChars();
  113. path = path.Trim();
  114. path = string.Join("_", path.Split(invalid)).Replace(".", "").Trim('_');
  115. return path;
  116. }
  117. public static string GP01FbFaceHash(TMorph face, string hash)
  118. {
  119. if ((face.bodyskin.PartsVersion >= 120) && (hash != "eyeclose3") && hash.StartsWith("eyeclose"))
  120. {
  121. if (hash == "eyeclose") hash += '1';
  122. hash += TMorph.crcFaceTypesStr[(int)face.GetFaceTypeGP01FB()];
  123. }
  124. return hash;
  125. }
  126. }
  127. internal static class BinaryExtensions
  128. {
  129. public static string ReadNullableString(this BinaryReader binaryReader)
  130. {
  131. return binaryReader.ReadBoolean() ? binaryReader.ReadString() : null;
  132. }
  133. public static void WriteNullableString(this BinaryWriter binaryWriter, string str)
  134. {
  135. binaryWriter.Write(str != null);
  136. if (str != null) binaryWriter.Write(str);
  137. }
  138. public static void WriteVector3(this BinaryWriter binaryWriter, UnityEngine.Vector3 vector3)
  139. {
  140. binaryWriter.Write(vector3.x);
  141. binaryWriter.Write(vector3.y);
  142. binaryWriter.Write(vector3.z);
  143. }
  144. public static UnityEngine.Vector3 ReadVector3(this BinaryReader binaryReader)
  145. {
  146. return new UnityEngine.Vector3(
  147. binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()
  148. );
  149. }
  150. public static void WriteQuaternion(this BinaryWriter binaryWriter, UnityEngine.Quaternion quaternion)
  151. {
  152. binaryWriter.Write(quaternion.x);
  153. binaryWriter.Write(quaternion.y);
  154. binaryWriter.Write(quaternion.z);
  155. binaryWriter.Write(quaternion.w);
  156. }
  157. public static UnityEngine.Quaternion ReadQuaternion(this BinaryReader binaryReader)
  158. {
  159. return new UnityEngine.Quaternion
  160. (
  161. binaryReader.ReadSingle(), binaryReader.ReadSingle(),
  162. binaryReader.ReadSingle(), binaryReader.ReadSingle()
  163. );
  164. }
  165. public static void WriteColour(this BinaryWriter binaryWriter, UnityEngine.Color colour)
  166. {
  167. binaryWriter.Write(colour.r);
  168. binaryWriter.Write(colour.g);
  169. binaryWriter.Write(colour.b);
  170. binaryWriter.Write(colour.a);
  171. }
  172. public static UnityEngine.Color ReadColour(this BinaryReader binaryReader)
  173. {
  174. return new Color(
  175. binaryReader.ReadSingle(),
  176. binaryReader.ReadSingle(),
  177. binaryReader.ReadSingle(),
  178. binaryReader.ReadSingle()
  179. );
  180. }
  181. }
  182. }