Utility.cs 9.4 KB

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