Utils.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Security.AccessControl;
  6. using System.Security.Principal;
  7. using TMPro;
  8. using UnityEngine;
  9. namespace SongCore.Utilities
  10. {
  11. public static class Utils
  12. {
  13. public static bool IsModInstalled(string ModName)
  14. {
  15. // Logging.Log($"Checking for Mod: {ModName}");
  16. foreach (var mod in IPA.Loader.PluginManager.Plugins)
  17. {
  18. // Logging.Log($"Comparing to: {mod.Name}");
  19. if (mod.Name == ModName)
  20. return true;
  21. }
  22. foreach (var mod in IPA.Loader.PluginManager.AllPlugins)
  23. {
  24. // Logging.Log($"Comparing to: {mod.Metadata.Id}");
  25. if (mod.Id == ModName)
  26. return true;
  27. }
  28. return false;
  29. }
  30. public static Color ColorFromMapColor(Data.ExtraSongData.MapColor mapColor)
  31. {
  32. return new Color(mapColor.r, mapColor.g, mapColor.b);
  33. }
  34. public static TEnum ToEnum<TEnum>(this string strEnumValue, TEnum defaultValue)
  35. {
  36. if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
  37. return defaultValue;
  38. return (TEnum)Enum.Parse(typeof(TEnum), strEnumValue);
  39. }
  40. public static bool IsDirectoryEmpty(string path)
  41. {
  42. return !Directory.EnumerateFileSystemEntries(path).Any();
  43. }
  44. public static void GrantAccess(string file)
  45. {
  46. bool exists = System.IO.Directory.Exists(file);
  47. if (!exists)
  48. {
  49. DirectoryInfo di = System.IO.Directory.CreateDirectory(file);
  50. // Console.WriteLine("The Folder is created Sucessfully");
  51. }
  52. else
  53. {
  54. // Console.WriteLine("The Folder already exists");
  55. }
  56. try
  57. {
  58. DirectoryInfo dInfo = new DirectoryInfo(file);
  59. DirectorySecurity dSecurity = dInfo.GetAccessControl();
  60. dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
  61. dInfo.SetAccessControl(dSecurity);
  62. }
  63. catch
  64. {
  65. Logging.logger.Error("Exception trying to Grant access to " + file);
  66. }
  67. }
  68. public static string TrimEnd(this string text, string value)
  69. {
  70. if (!text.EndsWith(value))
  71. return text;
  72. return text.Remove(text.LastIndexOf(value));
  73. }
  74. public static TextMeshProUGUI CreateText(RectTransform parent, string text, Vector2 anchoredPosition)
  75. {
  76. return CreateText(parent, text, anchoredPosition, new Vector2(60f, 10f));
  77. }
  78. public static TextMeshProUGUI CreateText(RectTransform parent, string text, Vector2 anchoredPosition, Vector2 sizeDelta)
  79. {
  80. GameObject gameObj = new GameObject("CustomUIText");
  81. gameObj.SetActive(false);
  82. TextMeshProUGUI textMesh = gameObj.AddComponent<TextMeshProUGUI>();
  83. textMesh.font = UnityEngine.Object.Instantiate(Resources.FindObjectsOfTypeAll<TMP_FontAsset>().First(t => t.name == "Teko-Medium SDF No Glow"));
  84. textMesh.rectTransform.SetParent(parent, false);
  85. textMesh.text = text;
  86. textMesh.fontSize = 4;
  87. textMesh.color = Color.white;
  88. textMesh.rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
  89. textMesh.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
  90. textMesh.rectTransform.sizeDelta = sizeDelta;
  91. textMesh.rectTransform.anchoredPosition = anchoredPosition;
  92. gameObj.SetActive(true);
  93. return textMesh;
  94. }
  95. public static Sprite LoadSpriteRaw(byte[] image, float PixelsPerUnit = 100.0f)
  96. {
  97. return LoadSpriteFromTexture(LoadTextureRaw(image), PixelsPerUnit);
  98. }
  99. public static Sprite LoadSpriteFromTexture(Texture2D SpriteTexture, float PixelsPerUnit = 100.0f)
  100. {
  101. if (SpriteTexture)
  102. return Sprite.Create(SpriteTexture, new Rect(0, 0, SpriteTexture.width, SpriteTexture.height), new Vector2(0, 0), PixelsPerUnit);
  103. return null;
  104. }
  105. public static Sprite LoadSpriteFromFile(string FilePath, float PixelsPerUnit = 100.0f)
  106. {
  107. return LoadSpriteFromTexture(LoadTextureFromFile(FilePath), PixelsPerUnit);
  108. }
  109. public static Sprite LoadSpriteFromResources(string resourcePath, float PixelsPerUnit = 100.0f)
  110. {
  111. return LoadSpriteRaw(GetResource(Assembly.GetCallingAssembly(), resourcePath), PixelsPerUnit);
  112. }
  113. public static byte[] GetResource(Assembly asm, string ResourceName)
  114. {
  115. System.IO.Stream stream = asm.GetManifestResourceStream(ResourceName);
  116. byte[] data = new byte[stream.Length];
  117. stream.Read(data, 0, (int)stream.Length);
  118. return data;
  119. }
  120. public static void PrintHierarchy(Transform transform, string spacing = "|-> ")
  121. {
  122. spacing = spacing.Insert(1, " ");
  123. var tempList = transform.Cast<Transform>().ToList();
  124. foreach (var child in tempList)
  125. {
  126. Console.WriteLine($"{spacing}{child.name}");
  127. PrintHierarchy(child, "|" + spacing);
  128. }
  129. }
  130. public static Texture2D LoadTextureRaw(byte[] file)
  131. {
  132. if (file.Count() > 0)
  133. {
  134. Texture2D Tex2D = new Texture2D(2, 2);
  135. if (Tex2D.LoadImage(file))
  136. return Tex2D;
  137. }
  138. return null;
  139. }
  140. public static Texture2D LoadTextureFromFile(string FilePath)
  141. {
  142. //TODO: Redirect?
  143. if (File.Exists(FilePath))
  144. return LoadTextureRaw(File.ReadAllBytes(FilePath));
  145. return null;
  146. }
  147. public static Texture2D LoadTextureFromResources(string resourcePath)
  148. {
  149. return LoadTextureRaw(GetResource(Assembly.GetCallingAssembly(), resourcePath));
  150. }
  151. }
  152. }