Utility.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 Texture2D MakeTex(int width, int height, Color color)
  35. {
  36. Color[] colors = new Color[width * height];
  37. for (int i = 0; i < colors.Length; i++)
  38. {
  39. colors[i] = color;
  40. }
  41. Texture2D texture2D = new Texture2D(width, height);
  42. texture2D.SetPixels(colors);
  43. texture2D.Apply();
  44. return texture2D;
  45. }
  46. public static FieldInfo GetFieldInfo<T>(string field)
  47. {
  48. BindingFlags bindingFlags = BindingFlags.Instance
  49. | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
  50. return typeof(T).GetField(field, bindingFlags);
  51. }
  52. public static TValue GetFieldValue<TType, TValue>(TType instance, string field)
  53. {
  54. FieldInfo fieldInfo = GetFieldInfo<TType>(field);
  55. if (fieldInfo == null || !fieldInfo.IsStatic && instance == null) return default(TValue);
  56. return (TValue)fieldInfo.GetValue(instance);
  57. }
  58. public static void SetFieldValue<TType, TValue>(TType instance, string name, TValue value)
  59. {
  60. GetFieldInfo<TType>(name).SetValue(instance, value);
  61. }
  62. public static bool GetModKey(ModKey key)
  63. {
  64. switch (key)
  65. {
  66. case ModKey.Control: return Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
  67. case ModKey.Alt: return Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
  68. case ModKey.Shift: return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  69. default: return false;
  70. }
  71. }
  72. public static bool AnyMouseDown()
  73. {
  74. return Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2);
  75. }
  76. public static string ScreenshotFilename()
  77. {
  78. string screenShotDir = Path.Combine(
  79. GameMain.Instance.SerializeStorageManager.StoreDirectoryPath, "ScreenShot"
  80. );
  81. if (!Directory.Exists(screenShotDir))
  82. {
  83. Directory.CreateDirectory(screenShotDir);
  84. }
  85. return Path.Combine(screenShotDir, $"img{DateTime.Now:yyyyMMddHHmmss}.png");
  86. }
  87. public static void ShowMouseExposition(string text, float time = 2f)
  88. {
  89. MouseExposition mouseExposition = MouseExposition.GetObject();
  90. mouseExposition.SetText(text, time);
  91. }
  92. public static string HandItemToOdogu(string menu)
  93. {
  94. menu = menu.Substring(menu.IndexOf('_') + 1);
  95. menu = menu.Substring(0, menu.IndexOf("_i_.menu"));
  96. menu = $"odogu_{menu}";
  97. return menu;
  98. }
  99. public static void FixGameObjectScale(GameObject go)
  100. {
  101. Vector3 scale = go.transform.localScale;
  102. float largest = Mathf.Max(scale.x, Mathf.Max(scale.y, scale.z));
  103. go.transform.localScale = Vector3.one * (float)Math.Round(largest, 3);
  104. }
  105. public static string SanitizePathPortion(string path)
  106. {
  107. char[] invalid = Path.GetInvalidFileNameChars();
  108. path = path.Trim();
  109. path = string.Join("_", path.Split(invalid)).Replace(".", "").Trim('_');
  110. return path;
  111. }
  112. public static string GP01FbFaceHash(TMorph face, string hash)
  113. {
  114. if ((face.bodyskin.PartsVersion >= 120) && (hash != "eyeclose3") && hash.StartsWith("eyeclose"))
  115. {
  116. if (hash == "eyeclose") hash += '1';
  117. hash += TMorph.crcFaceTypesStr[(int)face.GetFaceTypeGP01FB()];
  118. }
  119. return hash;
  120. }
  121. }
  122. }