Utility.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 int Wrap(int value, int min, int max)
  16. {
  17. max -= 1;
  18. return value < min ? max : value > max ? min : value;
  19. }
  20. public static int GetPix(int num)
  21. {
  22. return (int)((1f + (Screen.width / 1280f - 1f) * 0.6f) * num);
  23. }
  24. public static float Bound(float value, float left, float right)
  25. {
  26. if ((double)left > (double)right) return Mathf.Clamp(value, right, left);
  27. else return Mathf.Clamp(value, left, right);
  28. }
  29. public static Texture2D MakeTex(int width, int height, Color color)
  30. {
  31. Color[] colors = new Color[width * height];
  32. for (int i = 0; i < colors.Length; i++)
  33. {
  34. colors[i] = color;
  35. }
  36. Texture2D texture2D = new Texture2D(width, height);
  37. texture2D.SetPixels(colors);
  38. texture2D.Apply();
  39. return texture2D;
  40. }
  41. public static FieldInfo GetFieldInfo<T>(string field)
  42. {
  43. BindingFlags bindingFlags = BindingFlags.Instance
  44. | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
  45. return typeof(T).GetField(field, bindingFlags);
  46. }
  47. public static TValue GetFieldValue<TType, TValue>(TType instance, string field)
  48. {
  49. FieldInfo fieldInfo = GetFieldInfo<TType>(field);
  50. if (fieldInfo == null || !fieldInfo.IsStatic && instance == null) return default(TValue);
  51. return (TValue)fieldInfo.GetValue(instance);
  52. }
  53. public static void SetFieldValue<TType, TValue>(TType instance, string name, TValue value)
  54. {
  55. GetFieldInfo<TType>(name).SetValue(instance, value);
  56. }
  57. public static bool GetModKey(ModKey key)
  58. {
  59. switch (key)
  60. {
  61. case ModKey.Control: return Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
  62. case ModKey.Alt: return Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
  63. case ModKey.Shift: return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  64. default: return false;
  65. }
  66. }
  67. public static bool AnyMouseDown()
  68. {
  69. return Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2);
  70. }
  71. public static string ScreenshotFilename()
  72. {
  73. string screenShotDir = Path.Combine(
  74. GameMain.Instance.SerializeStorageManager.StoreDirectoryPath, "ScreenShot"
  75. );
  76. if (!Directory.Exists(screenShotDir))
  77. {
  78. Directory.CreateDirectory(screenShotDir);
  79. }
  80. return Path.Combine(screenShotDir, $"img{DateTime.Now:yyyyMMddHHmmss}.png");
  81. }
  82. public static void ShowMouseExposition(string text, float time = 2f)
  83. {
  84. MouseExposition mouseExposition = MouseExposition.GetObject();
  85. mouseExposition.SetText(text, time);
  86. }
  87. public static string HandItemToOdogu(string menu)
  88. {
  89. menu = menu.Substring(menu.IndexOf('_') + 1);
  90. menu = menu.Substring(0, menu.IndexOf("_i_.menu"));
  91. menu = $"odogu_{menu}";
  92. return menu;
  93. }
  94. public static void FixGameObjectScale(GameObject go)
  95. {
  96. Vector3 scale = go.transform.localScale;
  97. float largest = Mathf.Max(scale.x, Mathf.Max(scale.y, scale.z));
  98. go.transform.localScale = Vector3.one * (float)Math.Round(largest, 3);
  99. }
  100. public static string SanitizePathPortion(string path)
  101. {
  102. char[] invalid = Path.GetInvalidFileNameChars();
  103. path = path.Trim();
  104. path = string.Join("_", path.Split(invalid)).Replace(".", "").Trim('_');
  105. return path;
  106. }
  107. }
  108. }