|
| 1 | +using BepInEx; |
| 2 | +using System; |
| 3 | +using System.Linq; |
| 4 | +using UnityEngine; |
| 5 | +using UnityEngine.UI; |
| 6 | + |
| 7 | +namespace GTAG_NotificationLib |
| 8 | +{ |
| 9 | + [BepInPlugin("HabibiLars.NotificationLib", "NotificationLib", "1.0")] |
| 10 | + public class NotifiLib : BaseUnityPlugin |
| 11 | + { |
| 12 | + GameObject HUDObj; |
| 13 | + GameObject HUDObj2; |
| 14 | + GameObject MainCamera; |
| 15 | + Text Testtext; |
| 16 | + Material AlertText = new Material(Shader.Find("GUI/Text Shader")); |
| 17 | + int NotificationDecayTime = 150; |
| 18 | + int NotificationDecayTimeCounter = 0; |
| 19 | + public static int NoticationThreshold = 30; //Amount of notifications before they stop queuing up |
| 20 | + string[] Notifilines; |
| 21 | + string newtext; |
| 22 | + public static string PreviousNotifi; |
| 23 | + bool HasInit = false; |
| 24 | + static Text NotifiText; |
| 25 | + public static bool IsEnabled = true; |
| 26 | + private void Awake() |
| 27 | + { |
| 28 | + // Plugin startup logic |
| 29 | + Logger.LogInfo($"Plugin NotificationLib is loaded!"); |
| 30 | + } |
| 31 | + private void Init() |
| 32 | + { |
| 33 | + //this is mostly copy pasted from LHAX, which was also made by me. |
| 34 | + //LHAX got leaked the day before this. so i might as well make this public cus people asked me to. |
| 35 | + MainCamera = GameObject.Find("Main Camera"); |
| 36 | + HUDObj = new GameObject();//GameObject.CreatePrimitive(PrimitiveType.Cube); |
| 37 | + HUDObj2 = new GameObject(); |
| 38 | + HUDObj2.name = "NOTIFICATIONLIB_HUD_OBJ"; |
| 39 | + HUDObj.name = "NOTIFICATIONLIB_HUD_OBJ"; |
| 40 | + HUDObj.AddComponent<Canvas>(); |
| 41 | + HUDObj.AddComponent<CanvasScaler>(); |
| 42 | + HUDObj.AddComponent<GraphicRaycaster>(); |
| 43 | + HUDObj.GetComponent<Canvas>().enabled = true; |
| 44 | + HUDObj.GetComponent<Canvas>().renderMode = RenderMode.WorldSpace; |
| 45 | + HUDObj.GetComponent<Canvas>().worldCamera = MainCamera.GetComponent<Camera>(); |
| 46 | + HUDObj.GetComponent<RectTransform>().sizeDelta = new Vector2(5, 5); |
| 47 | + //HUDObj.CreatePrimitive() |
| 48 | + HUDObj.GetComponent<RectTransform>().position = new Vector3(MainCamera.transform.position.x, MainCamera.transform.position.y, MainCamera.transform.position.z);//new Vector3(-67.151f, 11.914f, -82.749f); |
| 49 | + HUDObj2.transform.position = new Vector3(MainCamera.transform.position.x, MainCamera.transform.position.y, MainCamera.transform.position.z - 4.6f); |
| 50 | + HUDObj.transform.parent = HUDObj2.transform; |
| 51 | + HUDObj.GetComponent<RectTransform>().localPosition = new Vector3(0f, 0f, 1.6f); |
| 52 | + var Temp = HUDObj.GetComponent<RectTransform>().rotation.eulerAngles; |
| 53 | + Temp.y = -270f; |
| 54 | + HUDObj.transform.localScale = new Vector3(1f, 1f, 1f); |
| 55 | + HUDObj.GetComponent<RectTransform>().rotation = Quaternion.Euler(Temp); |
| 56 | + GameObject TestText = new GameObject(); |
| 57 | + TestText.transform.parent = HUDObj.transform; |
| 58 | + Testtext = TestText.AddComponent<Text>(); |
| 59 | + Testtext.text = ""; |
| 60 | + Testtext.fontSize = 10; |
| 61 | + Testtext.font = GameObject.Find("COC Text").GetComponent<Text>().font; |
| 62 | + Testtext.rectTransform.sizeDelta = new Vector2(260, 70); |
| 63 | + Testtext.alignment = TextAnchor.LowerLeft; |
| 64 | + Testtext.rectTransform.localScale = new Vector3(0.01f, 0.01f, 1f); |
| 65 | + Testtext.rectTransform.localPosition = new Vector3(-1.5f, -.9f, -.6f); |
| 66 | + Testtext.material = AlertText; |
| 67 | + NotifiText = Testtext; |
| 68 | + } |
| 69 | + |
| 70 | + private void FixedUpdate() |
| 71 | + { |
| 72 | + //This is a bad way to do this, but i do not want to rely on utila. |
| 73 | + if(HasInit == false) |
| 74 | + { |
| 75 | + if(GameObject.Find("Main Camera") != null) |
| 76 | + { |
| 77 | + Init(); |
| 78 | + HasInit = true; |
| 79 | + } |
| 80 | + } |
| 81 | + HUDObj2.transform.position = new Vector3(MainCamera.transform.position.x, MainCamera.transform.position.y, MainCamera.transform.position.z); |
| 82 | + HUDObj2.transform.rotation = MainCamera.transform.rotation; |
| 83 | + //HUDObj.GetComponent<RectTransform>().localPosition = new Vector3(0f, 0f, 1.6f); |
| 84 | + if (Testtext.text != "") //THIS CAUSES A MEMORY LEAK!!!!! -no longer causes a memory leak |
| 85 | + { |
| 86 | + NotificationDecayTimeCounter++; |
| 87 | + if (NotificationDecayTimeCounter > NotificationDecayTime) |
| 88 | + { |
| 89 | + Notifilines = null; |
| 90 | + newtext = ""; |
| 91 | + NotificationDecayTimeCounter = 0; |
| 92 | + Notifilines = Testtext.text.Split(Environment.NewLine.ToCharArray()).Skip(1).ToArray(); |
| 93 | + foreach (string Line in Notifilines) |
| 94 | + { |
| 95 | + if (Line != "") |
| 96 | + { |
| 97 | + newtext = newtext + Line + "\n"; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + Testtext.text = newtext; |
| 102 | + } |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + NotificationDecayTimeCounter = 0; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public static void SendNotification(string NotificationText) |
| 111 | + { |
| 112 | + if (IsEnabled) |
| 113 | + { |
| 114 | + if (!NotificationText.Contains(Environment.NewLine)) { NotificationText = NotificationText + Environment.NewLine; } |
| 115 | + NotifiText.text = NotifiText.text + NotificationText; |
| 116 | + PreviousNotifi = NotificationText; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public static void ClearAllNotifications() |
| 121 | + { |
| 122 | + NotifiText.text = ""; |
| 123 | + } |
| 124 | + public static void ClearPastNotifications(int amount) |
| 125 | + { |
| 126 | + string[] Notifilines = null; |
| 127 | + string newtext = ""; |
| 128 | + Notifilines = NotifiText.text.Split(Environment.NewLine.ToCharArray()).Skip(amount).ToArray(); |
| 129 | + foreach (string Line in Notifilines) |
| 130 | + { |
| 131 | + if (Line != "") |
| 132 | + { |
| 133 | + newtext = newtext + Line + "\n"; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + NotifiText.text = newtext; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments