2022-01-22 20:13:49 -08:00
using System ;
using System.Reflection ;
using BepInEx.Bootstrap ;
using UnityEngine ;
using UnityEngine.UI ;
using FistVR ;
using RUST.Steamworks ;
using System.Collections.Generic ;
namespace TNHQoLImprovements
{
public class LeaderboardPlayerCount : MonoBehaviour
{
private bool initialized = false ;
private bool tnhTweakerInstalled = false ;
private string curID ;
2022-01-26 01:23:39 -08:00
private string loadingStr ;
2022-01-22 20:13:49 -08:00
private TNH_ScoreDisplay scoreDisplay ;
private Text lblGlobalScores ;
private GameObject gObjLoading ;
2022-01-22 23:41:31 -08:00
#region INITIALIZATION
2022-01-22 20:13:49 -08:00
public void Init ( TNH_ScoreDisplay tnhScore , Text scoreLabel , GameObject gObjLoading )
{
if ( initialized )
return ;
2022-01-30 14:41:03 -08:00
// don't run with TNHTweaker installed
this . gObjLoading = gObjLoading ;
var loadedAssemblies = System . AppDomain . CurrentDomain . GetAssemblies ( ) ;
if ( Array . Exists < Assembly > ( loadedAssemblies , x = > x . GetName ( ) . Name = = "TakeAndHoldTweaker" ) )
{
tnhTweakerInstalled = true ;
2022-02-14 13:02:43 -08:00
this . gObjLoading . transform . GetChild ( 0 ) . GetComponent < Text > ( ) . text = "<color=lightblue><size=30>Online leaderboards player count unavailable for TNHTweaker.</size></color>" ;
2022-01-30 14:41:03 -08:00
this . gObjLoading . SetActive ( true ) ;
return ;
}
this . scoreDisplay = tnhScore ;
2022-01-22 20:13:49 -08:00
this . lblGlobalScores = scoreLabel ;
this . lblGlobalScores . resizeTextForBestFit = true ;
this . lblGlobalScores . horizontalOverflow = HorizontalWrapMode . Overflow ;
2022-01-26 01:23:39 -08:00
loadingStr = gObjLoading . GetComponentInChildren < Text > ( ) . text ;
2022-01-22 20:13:49 -08:00
2022-01-30 14:41:03 -08:00
2022-01-22 20:13:49 -08:00
initialized = true ;
}
2022-01-22 23:41:31 -08:00
#endregion
2022-01-22 20:13:49 -08:00
2022-01-22 23:41:31 -08:00
#region UPDATE
private void Update ( )
2022-01-22 20:13:49 -08:00
{
if ( ! initialized | | tnhTweakerInstalled )
return ;
string newID = scoreDisplay . m_curSequenceID ;
if ( newID ! = curID )
UpdatePlayerCountDisplay ( newID ) ;
}
private void UpdatePlayerCountDisplay ( string id )
2022-01-26 01:23:39 -08:00
{
try
{
string playerCountText = Steamworks . SteamUserStats
. GetLeaderboardEntryCount ( HighScoreManager . Leaderboards [ id ] )
. ToString ( "N0" ) ;
lblGlobalScores . text = "Global Scores: <color=lightblue>(" + playerCountText + " players)</color>" ;
curID = id ;
2022-01-22 20:13:49 -08:00
gObjLoading . SetActive ( false ) ;
2022-01-26 01:23:39 -08:00
}
catch ( KeyNotFoundException e )
{
lblGlobalScores . text = "Global Scores:" ;
gObjLoading . GetComponentInChildren < Text > ( ) . text = loadingStr ;
2022-01-22 20:13:49 -08:00
gObjLoading . SetActive ( true ) ;
2022-01-26 01:23:39 -08:00
curID = null ;
}
catch ( Exception e )
{
2022-01-30 14:41:03 -08:00
gObjLoading . GetComponentInChildren < Text > ( ) . text = string . Format ( "<color=lightblue><size=30>Unknown error occured trying to retrieve online player count.</size></color>\n" +
2022-01-26 01:23:39 -08:00
"<color=red>{0}</color>" , e ) ;
gObjLoading . SetActive ( true ) ;
2022-01-22 20:13:49 -08:00
}
2022-01-26 01:23:39 -08:00
}
}
2022-01-22 23:41:31 -08:00
#endregion
2022-01-22 20:13:49 -08:00
}