Utility.cs 9.4 KB

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