Skip to content
This repository was archived by the owner on Dec 9, 2020. It is now read-only.

Fix for 1.12.2 & fix chat inner tilting over time #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Chat/ChatDisplay.Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public Color BackgroundColor
set
{
_chatConfig.BackgroundColor = value;
_chatScreen.gameObject.GetComponent<Image>().material.color = value;
if (_bg != null) _bg.material.color = value;
NotifyPropertyChanged();
}
}
Expand Down
102 changes: 51 additions & 51 deletions Chat/ChatDisplay.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using BeatSaberMarkupLanguage;
using BeatSaberMarkupLanguage.Attributes;
using BeatSaberMarkupLanguage.Components;
using BeatSaberMarkupLanguage.FloatingScreen;
using BeatSaberMarkupLanguage.ViewControllers;
using BS_Utils.Utilities;
using ChatCore.Interfaces;
using EnhancedStreamChat.Graphics;
using EnhancedStreamChat.Utilities;
using HMUI;
using System;
using System.Collections;
using System.Collections.Concurrent;
Expand Down Expand Up @@ -33,11 +35,13 @@ public partial class ChatDisplay : BSMLAutomaticViewController

private void Awake()
{
DontDestroyOnLoad(gameObject);
_chatConfig = ChatConfig.instance;
CreateChatFont();
SetupScreens();
(transform as RectTransform).pivot = new Vector2(0.5f, 0f);

/// Update message position origin
(transform.GetChild(0).transform as RectTransform).pivot = new Vector2(0.5f, 0f);

TextPool = new ObjectPool<EnhancedTextMeshProUGUIWithBackground>(25,
Constructor: () =>
{
Expand All @@ -49,7 +53,7 @@ private void Awake()
msg.SubText.enableWordWrapping = true;
msg.SubText.FontInfo = _chatFont;
(msg.transform as RectTransform).pivot = new Vector2(0.5f, 0);
msg.transform.SetParent(transform, false);
msg.transform.SetParent(transform.GetChild(0).transform, false);
msg.gameObject.SetActive(false);
UpdateMessage(msg);
return msg;
Expand All @@ -76,12 +80,25 @@ private void Awake()
}
}
);
ChatConfig.instance.OnConfigChanged += Instance_OnConfigChanged;
BSEvents.menuSceneActive += BSEvents_menuSceneActive;
BSEvents.gameSceneActive += BSEvents_gameSceneActive;

_waitForEndOfFrame = new WaitForEndOfFrame();
_waitUntilMessagePositionsNeedUpdate = new WaitUntil(() => _updateMessagePositions == true);
StartCoroutine(UpdateMessagePositions());
}
protected override void DidActivate(bool p_FirstActivation, bool p_AddedToHierarchy, bool p_ScreenSystemEnabling)
{
/// Forward event
base.DidActivate(p_FirstActivation, p_AddedToHierarchy, p_ScreenSystemEnabling);

if (p_FirstActivation)
{
GetComponent<CurvedCanvasSettings>().SetRadius(0f);

ChatConfig.instance.OnConfigChanged += Instance_OnConfigChanged;
BSEvents.menuSceneActive += BSEvents_menuSceneActive;
BSEvents.gameSceneActive += BSEvents_gameSceneActive;

SharedCoroutineStarter.instance.StartCoroutine(UpdateMessagePositions());
}
}

// TODO: eventually figure out a way to make this more modular incase we want to create multiple instances of ChatDisplay
Expand All @@ -92,7 +109,7 @@ protected override void OnDestroy()
ChatConfig.instance.OnConfigChanged -= Instance_OnConfigChanged;
BSEvents.menuSceneActive -= BSEvents_menuSceneActive;
BSEvents.gameSceneActive -= BSEvents_gameSceneActive;
StopAllCoroutines();
SharedCoroutineStarter.instance.StopCoroutine(UpdateMessagePositions());
foreach (var msg in _messages)
{
msg.OnLatePreRenderRebuildComplete -= OnRenderRebuildComplete;
Expand Down Expand Up @@ -130,35 +147,39 @@ protected override void OnDestroy()
}
}

private bool _applicationQuitting = false;
private void OnApplicationQuit()
{
_applicationQuitting = true;
}

private FloatingScreen _chatScreen;
private GameObject _rootGameObject;
private Material _chatMoverMaterial;
private Image _bg;
private ImageView _bg = null;
private void SetupScreens()
{
if (_chatScreen == null)
{
_chatScreen = FloatingScreen.CreateFloatingScreen(new Vector2(ChatWidth, ChatHeight), true, ChatPosition, Quaternion.Euler(ChatRotation));
_chatScreen = FloatingScreen.CreateFloatingScreen(new Vector2(ChatWidth, ChatHeight), true, ChatPosition, Quaternion.identity);
_chatScreen.GetComponent<CurvedCanvasSettings>().SetRadius(0f);

var canvas = _chatScreen.GetComponent<Canvas>();
canvas.sortingOrder = 3;
_chatScreen.SetRootViewController(this, true);

_chatScreen.SetRootViewController(this, AnimationType.None);

_rootGameObject = new GameObject();
DontDestroyOnLoad(_rootGameObject);
_chatMoverMaterial = Instantiate(BeatSaberUtils.UINoGlowMaterial);
_chatMoverMaterial.color = Color.clear;
var renderer = _chatScreen.handle.gameObject.GetComponent<Renderer>();
renderer.material = _chatMoverMaterial;
_chatScreen.transform.SetParent(_rootGameObject.transform);
_bg = _chatScreen.gameObject.GetComponent<UnityEngine.UI.Image>();
_chatScreen.ScreenRotation = Quaternion.Euler(ChatRotation);

_chatScreen.HandleReleased += floatingScreen_OnRelease;

_bg = _chatScreen.transform.GetChild(0).gameObject.AddComponent<ImageView>();
_bg.sprite = Resources.FindObjectsOfTypeAll<Sprite>().First(x => x.name == "MainScreenMask");
_bg.type = Image.Type.Sliced;
_bg.material = Instantiate(BeatSaberUtils.UINoGlowMaterial);
_bg.preserveAspect = true;
_bg.color = BackgroundColor;
AddToVRPointer();
}
}

Expand All @@ -167,62 +188,39 @@ private void Instance_OnConfigChanged(ChatConfig obj)
UpdateChatUI();
}

private void floatingScreen_OnRelease(Vector3 pos, Quaternion rot)
private void floatingScreen_OnRelease(object sender, FloatingScreenHandleEventArgs e)
{
if (_isInGame)
{
_chatConfig.Song_ChatPosition = pos;
_chatConfig.Song_ChatRotation = rot.eulerAngles;
_chatConfig.Song_ChatPosition = e.Position;
_chatConfig.Song_ChatRotation = e.Rotation.eulerAngles;
}
else
{
_chatConfig.Menu_ChatPosition = pos;
_chatConfig.Menu_ChatRotation = rot.eulerAngles;
_chatConfig.Menu_ChatPosition = e.Position;
_chatConfig.Menu_ChatRotation = e.Rotation.eulerAngles;
}
_chatConfig.Save();
}

private void BSEvents_gameSceneActive()
{
_isInGame = true;
AddToVRPointer();
UpdateChatUI();
}

private void BSEvents_menuSceneActive()
{
_isInGame = false;
AddToVRPointer();
UpdateChatUI();
}

private void AddToVRPointer()
{
VRPointer pointer = null;
if (_isInGame)
{
pointer = Resources.FindObjectsOfTypeAll<VRPointer>().Last();
}
else
{
pointer = Resources.FindObjectsOfTypeAll<VRPointer>().First();
}
if (_chatScreen.screenMover != null)
{
DestroyImmediate(_chatScreen.screenMover);
_chatScreen.screenMover = pointer.gameObject.AddComponent<FloatingScreenMoverPointer>();
_chatScreen.screenMover.Init(_chatScreen);
_chatScreen.screenMover.OnRelease += floatingScreen_OnRelease;
_chatScreen.screenMover.transform.SetAsFirstSibling();
}
}

private bool _updateMessagePositions = false;
private WaitUntil _waitUntilMessagePositionsNeedUpdate;
private WaitForEndOfFrame _waitForEndOfFrame;
private IEnumerator UpdateMessagePositions()
{
while (!_applicationQuitting)
while (this.isInViewControllerHierarchy)
{
yield return _waitUntilMessagePositionsNeedUpdate;
yield return _waitForEndOfFrame;
Expand Down Expand Up @@ -389,15 +387,17 @@ public void OnMessageCleared(string messageId)

public void OnChatCleared(string userId)
{
if (userId == null)
return;

MainThreadInvoker.Invoke(() =>
{
foreach (var msg in _messages)
{
if (msg.Text.ChatMessage == null)
{
continue;
}
if (userId == null || msg.Text.ChatMessage.Sender.Id == userId)

if (msg.Text.ChatMessage.Sender.Id == userId)
{
ClearMessage(msg);
}
Expand Down
21 changes: 9 additions & 12 deletions Chat/ChatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ public override void OnEnable()
_svcs.OnChannelResourceDataCached += QueueOrSendOnChannelResourceDataCached;
ChatImageProvider.TouchInstance();
Task.Run(HandleOverflowMessageQueue);
BSEvents.menuSceneLoadedFresh += BSEvents_menuSceneLoadedFresh;

if (_chatDisplay != null)
{
DestroyImmediate(_chatDisplay.gameObject);
_chatDisplay = null;
MainThreadInvoker.ClearQueue();
}
_chatDisplay = BeatSaberUI.CreateViewController<ChatDisplay>();
_chatDisplay.gameObject.SetActive(true);
}

private void _sc_OnLogReceived(CustomLogLevel level, string category, string log)
Expand All @@ -68,7 +76,6 @@ public void OnDisable()
_svcs.OnChatCleared -= QueueOrSendOnClearChat;
_svcs.OnMessageCleared -= QueueOrSendOnClearMessage;
_svcs.OnChannelResourceDataCached -= QueueOrSendOnChannelResourceDataCached;
BSEvents.menuSceneLoadedFresh -= BSEvents_menuSceneLoadedFresh;
}
if (_sc != null)
{
Expand All @@ -85,16 +92,6 @@ public void OnDisable()
}

ChatDisplay _chatDisplay;
private void BSEvents_menuSceneLoadedFresh()
{
if (_chatDisplay != null)
{
DestroyImmediate(_chatDisplay.gameObject);
_chatDisplay = null;
MainThreadInvoker.ClearQueue();
}
_chatDisplay = BeatSaberUI.CreateViewController<ChatDisplay>();
}

private ConcurrentQueue<Action> _actionQueue = new ConcurrentQueue<Action>();
private SemaphoreSlim _msgLock = new SemaphoreSlim(1, 1);
Expand Down
25 changes: 14 additions & 11 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace EnhancedStreamChat
{
[Plugin(RuntimeOptions.DynamicInit)]
[Plugin(RuntimeOptions.SingleStartInit)]
public class Plugin
{
internal static Plugin instance { get; private set; }
Expand All @@ -31,19 +31,22 @@ public void Init(IPALogger logger, PluginMetadata meta)
Logger.log = logger;
Logger.log.Debug("Logger initialized.");
var config = ChatConfig.instance;
}

[OnEnable]
public void OnEnable()
}
[OnStart]
public void OnApplicationStart()
{
try
{
ChatManager.instance.enabled = true;
}
catch(Exception ex)
BS_Utils.Utilities.BSEvents.lateMenuSceneLoadedFresh += (x) =>
{
Logger.log.Error(ex);
}
try
{
ChatManager.instance.enabled = true;
}
catch (Exception ex)
{
Logger.log.Error(ex);
}
};
}

[OnDisable]
Expand Down