Skip to content

immcrab/Lasting-Utilla

Ownership not verified 1
Open on GitHub

Viewing main at commit a6a25ac430“Add files via upload”· 11 months ago

297 lines · 14 KBRaw on GitHub
// Decompiled with JetBrains decompiler
// Type: Utilla.Behaviours.UtillaGamemodeSelector
// Assembly: Utilla, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: FDD08D98-E155-49FA-85BC-1930928C3BAA
// Assembly location: C:\Program Files (x86)\Steam\steamapps\common\Gorilla Tag\BepInEx\plugins\Utilla\Utilla.dll

using GameObjectScheduling;
using GorillaGameModes;
using GorillaNetworking;
using GorillaTag;
using GorillaTagScripts.VirtualStumpCustomMaps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using Utilla.Models;
using Utilla.Tools;

#nullable disable
namespace Utilla.Behaviours;

[RequireComponent(typeof (GameModeSelectorButtonLayout))]
[DisallowMultipleComponent]
internal class UtillaGamemodeSelector : MonoBehaviour
{
  public static Dictionary<GTZone, UtillaGamemodeSelector> SelectorLookup = new Dictionary<GTZone, UtillaGamemodeSelector>();
  public readonly Dictionary<bool, List<Gamemode>> SelectorGameModes = new Dictionary<bool, List<Gamemode>>();
  public GameModeSelectorButtonLayout Layout;
  public GTZone Zone;
  public int CurrentPage;
  public int PageCount;
  public int PageCapacity;
  private readonly List<ModeSelectButton> moddedSelectionButtons = new List<ModeSelectButton>();
  private static GameObject fallbackTemplateButton = (GameObject) null;

  public async void Awake()
  {
    UtillaGamemodeSelector gamemodeSelector = this;
    gamemodeSelector.Layout = ((Component) gamemodeSelector).GetComponent<GameModeSelectorButtonLayout>();
    gamemodeSelector.Zone = gamemodeSelector.Layout.zone;
    if (UtillaGamemodeSelector.SelectorLookup.ContainsKey(gamemodeSelector.Zone))
    {
      Logging.Warning((object) $"Found duplicate game mode selector for zone {gamemodeSelector.Zone}");
    }
    else
    {
      UtillaGamemodeSelector.SelectorLookup.Add(gamemodeSelector.Zone, gamemodeSelector);
      Logging.Info((object) $"Initializing game mode selector for zone {gamemodeSelector.Zone}");
    }
    while (gamemodeSelector.Layout.currentButtons.Count == 0)
      await Task.Delay(100);
    gamemodeSelector.ModifySelectionButtons();
    gamemodeSelector.CreatePageButtons(((Component) gamemodeSelector.Layout.currentButtons.First<ModeSelectButton>()).gameObject);
    Logging.Info((object) "Checking for game mode manager");
    if (!GamemodeManager.HasInstance && gamemodeSelector.Zone == PhotonNetworkController.Instance.StartZone)
    {
      Logging.Info((object) "Start zone detected - creating game mode manager");
      Plugin.PostInitialized();
    }
    else
    {
      if (!GamemodeManager.Initialized)
      {
        Logging.Info((object) "Waiting for game mode manager");
        GamemodeManager task = await GamemodeManager.InitializeTask.Task;
      }
      List<GTZone> activeZones = ZoneManagement.instance.activeZones;
      if (activeZones != null && activeZones.Contains(gamemodeSelector.Zone) || gamemodeSelector.Layout is CustomMapModeSelector)
      {
        Logging.Info((object) "Checking game mode validity");
        gamemodeSelector.CheckGameMode();
      }
      gamemodeSelector.PageCount = Mathf.CeilToInt((float) gamemodeSelector.GetSelectorGameModes().Count / (float) gamemodeSelector.PageCapacity);
      gamemodeSelector.ShowPage();
    }
  }

  public void OnEnable()
  {
    NetworkSystem instance1 = NetworkSystem.Instance;
    instance1.OnJoinedRoomEvent = DelegateListProcessorPlusMinus<DelegateListProcessor, Action>.op_Addition((DelegateListProcessorPlusMinus<DelegateListProcessor, Action>) instance1.OnJoinedRoomEvent, new Action(this.ShowPage));
    NetworkSystem instance2 = NetworkSystem.Instance;
    instance2.OnReturnedToSinglePlayer = DelegateListProcessorPlusMinus<DelegateListProcessor, Action>.op_Addition((DelegateListProcessorPlusMinus<DelegateListProcessor, Action>) instance2.OnReturnedToSinglePlayer, new Action(this.ShowPage));
  }

  public void OnDisable()
  {
    NetworkSystem instance1 = NetworkSystem.Instance;
    instance1.OnJoinedRoomEvent = DelegateListProcessorPlusMinus<DelegateListProcessor, Action>.op_Subtraction((DelegateListProcessorPlusMinus<DelegateListProcessor, Action>) instance1.OnJoinedRoomEvent, new Action(this.ShowPage));
    NetworkSystem instance2 = NetworkSystem.Instance;
    instance2.OnReturnedToSinglePlayer = DelegateListProcessorPlusMinus<DelegateListProcessor, Action>.op_Subtraction((DelegateListProcessorPlusMinus<DelegateListProcessor, Action>) instance2.OnReturnedToSinglePlayer, new Action(this.ShowPage));
  }

  public void OnSelectorSetup()
  {
    if (!(this.Layout is CustomMapModeSelector))
      return;
    bool sessionIsPrivate = NetworkSystem.Instance.SessionIsPrivate;
    if (this.SelectorGameModes.ContainsKey(sessionIsPrivate))
      this.SelectorGameModes.Remove(sessionIsPrivate);
    this.ModifySelectionButtons();
    this.ShowPage();
  }

  public List<Gamemode> GetSelectorGameModes()
  {
    bool sessionIsPrivate = NetworkSystem.Instance.SessionIsPrivate;
    List<Gamemode> selectorGameModes;
    if (this.SelectorGameModes.TryGetValue(sessionIsPrivate, out selectorGameModes))
      return selectorGameModes;
    List<Gamemode> source = new List<Gamemode>();
    Logging.Info((object) $"GetSelectorGameModes {this.Zone}");
    GameModeType[] gameModeTypeArray = this.Layout is CustomMapModeSelector ? CustomMapModeSelector.gamemodes.ToArray() : GameMode.GameModeZoneMapping.GetModesForZone(this.Zone, NetworkSystem.Instance.SessionIsPrivate).ToArray<GameModeType>();
    for (int index = 0; index < gameModeTypeArray.Length; ++index)
    {
      Gamemode gamemode;
      if (GamemodeManager.Instance.DefaultGameModesPerMode.TryGetValue((GameModeType) (int) gameModeTypeArray[index], out gamemode))
        source.Add(gamemode);
    }
    for (int index = 0; index < gameModeTypeArray.Length; ++index)
    {
      Gamemode gamemode;
      if (GamemodeManager.Instance.ModdedGamemodesPerMode.TryGetValue((GameModeType) (int) gameModeTypeArray[index], out gamemode))
      {
        Logging.Info((object) $"+ \"{gamemode.DisplayName}\" ({EnumUtilExt.GetName<GameModeType>((GameModeType) (int) gameModeTypeArray[index])})");
        source.Add(gamemode);
      }
      else
        source.Add((Gamemode) null);
    }
    List<Gamemode> customGameModes = GamemodeManager.Instance.CustomGameModes;
    if (customGameModes != null)
    {
      for (int index = 0; index < customGameModes.Count; ++index)
      {
        Gamemode gamemode = customGameModes[index];
        Logging.Info((object) $"+ \"{gamemode.DisplayName}\"");
        source.Add(gamemode);
      }
    }
    if (this.SelectorGameModes.TryAdd(sessionIsPrivate, source))
      Logging.Info((object) string.Join(", ", source.Select<Gamemode, string>((Func<Gamemode, string>) (gameMode => gameMode.DisplayName)).Select<string, string>((Func<string, string>) (gameMode => $"\"{gameMode}\""))));
    return source;
  }

  public void CheckGameMode()
  {
    string currentGameMode = GorillaComputer.instance.currentGameMode.Value;
    if (GamemodeManager.Instance.CustomGameModes.Exists((Predicate<Gamemode>) (customMode => customMode.ID == currentGameMode)))
      return;
    if (this.Layout is CustomMapModeSelector)
    {
      this.SetDefaultMode(EnumUtilExt.GetName<GameModeType>(CustomMapModeSelector.defaultGamemodeForLoadedMap));
    }
    else
    {
      List<string> list = this.GetSelectorGameModes().Where<Gamemode>((Func<Gamemode, bool>) (gameMode => gameMode != null)).Select<Gamemode, string>((Func<Gamemode, string>) (game_mode => game_mode.ID)).ToList<string>();
      if (!list.Contains(currentGameMode))
        this.SetDefaultMode(list.First<string>());
      else
        this.ShowPage();
    }
  }

  public void SetDefaultMode(string gameMode)
  {
    Logging.Message((object) ("SetDefaultMode : " + gameMode));
    if (GorillaComputer.instance.currentGameMode.Value.StartsWith("MODDED_"))
    {
      GameModeType result;
      GameModeType? nullable = Enum.TryParse<GameModeType>(gameMode, out result) ? new GameModeType?(result) : new GameModeType?();
      Gamemode gamemode;
      if (nullable.HasValue && GamemodeManager.Instance.ModdedGamemodesPerMode.TryGetValue(nullable.Value, out gamemode))
        gameMode = gamemode.ID;
    }
    GorillaComputer.instance.SetGameModeWithoutButton(gameMode);
    int index = this.GetSelectorGameModes().Select<Gamemode, string>((Func<Gamemode, string>) (game_mode => game_mode == null ? string.Empty : game_mode.ID)).ToList<string>().FindIndex((Predicate<string>) (gamemode => gamemode == gameMode));
    this.CurrentPage = index != -1 ? Mathf.FloorToInt((float) index / (float) this.PageCapacity) : 0;
    Logging.Info((object) $"Set to {gameMode} on page {this.CurrentPage}");
    this.ShowPage();
  }

  public void NextPage()
  {
    this.CurrentPage = (this.CurrentPage + 1) % this.PageCount;
    this.ShowPage();
  }

  public void PreviousPage()
  {
    this.CurrentPage = this.CurrentPage <= 0 ? this.PageCount - 1 : this.CurrentPage - 1;
    this.ShowPage();
  }

  public void ShowPage()
  {
    List<Gamemode> list = this.GetSelectorGameModes().Skip<Gamemode>(this.CurrentPage * this.PageCapacity).Take<Gamemode>(this.PageCapacity).ToList<Gamemode>();
    for (int index = 0; index < this.Layout.currentButtons.Count; ++index)
    {
      Gamemode gamemode = list.ElementAtOrDefault<Gamemode>(index);
      bool flag = gamemode != null && gamemode.ID != null && gamemode.ID.Length != 0;
      ModeSelectButton currentButton = this.Layout.currentButtons[index];
      if (((Component) currentButton).gameObject.activeSelf != flag)
        ((Component) currentButton).gameObject.SetActive(flag);
      if (!((Component) currentButton).gameObject.activeSelf)
      {
        ((Behaviour) currentButton).enabled = false;
        currentButton.SetInfo("", "", false, (CountdownTextDate) null);
      }
      else
      {
        ((Behaviour) currentButton).enabled = true;
        currentButton.SetInfo(gamemode.ID, gamemode.DisplayName.ToUpper(), false, (CountdownTextDate) null);
        currentButton.OnGameModeChanged(GorillaComputer.instance.currentGameMode.Value);
      }
    }
  }

  private void CreatePageButtons(GameObject templateButton)
  {
    GameObject primitive = GameObject.CreatePrimitive((PrimitiveType) 3);
    primitive.SetActive(false);
    MeshFilter meshFilter = primitive.GetComponent<MeshFilter>();
    bool flag = this.Layout is CustomMapModeSelector;
    CreatePageButton("-->", new Action(this.NextPage)).transform.localPosition = new Vector3(flag ? -0.78f : -0.745f, flag ? 0.1f : -0.095f, -0.03f);
    CreatePageButton("<--", new Action(this.PreviousPage)).transform.localPosition = new Vector3(flag ? -0.78f : -0.745f, -0.75f, -0.03f);
    Object.Destroy((Object) primitive);
    if (templateButton.transform.childCount != 0)
      UtillaGamemodeSelector.fallbackTemplateButton = templateButton;
    this.Invoke("ShowPage", 1f);

    GameObject CreatePageButton(string text, Action onPressed)
    {
      GameObject pageButton = Object.Instantiate<GameObject>(templateButton.transform.childCount == 0 ? UtillaGamemodeSelector.fallbackTemplateButton : templateButton);
      pageButton.GetComponent<MeshFilter>().mesh = meshFilter.mesh;
      pageButton.GetComponent<Renderer>().material = templateButton.GetComponent<GorillaPressableButton>().unpressedMaterial;
      pageButton.transform.parent = templateButton.transform.parent;
      pageButton.transform.localRotation = templateButton.transform.localRotation;
      pageButton.transform.localScale = Vector3.op_Multiply(Vector3.one, 0.1427168f);
      TMP_Text tmpText = ((Component) pageButton.transform.Find("Title"))?.GetComponent<TMP_Text>() ?? pageButton.GetComponentInChildren<TMP_Text>(true);
      if (Object.op_Implicit((Object) tmpText))
      {
        ((Component) tmpText).gameObject.SetActive(true);
        ((Behaviour) tmpText).enabled = true;
        tmpText.transform.localPosition = Vector3.op_Multiply(Vector3.forward, 0.525f);
        tmpText.transform.localEulerAngles = Vector3.op_Multiply(Vector3.up, 180f);
        tmpText.transform.localScale = Vector3.Scale(tmpText.transform.localScale, new Vector3(0.5f, 0.5f, 1f));
        tmpText.text = text;
        ((Graphic) tmpText).color = Color.black;
        tmpText.horizontalAlignment = (HorizontalAlignmentOptions) 2;
        StaticLodGroup staticLodGroup;
        if (((Component) tmpText).TryGetComponent<StaticLodGroup>(ref staticLodGroup))
          Object.Destroy((Object) staticLodGroup);
      }
      else
      {
        Text componentInChildren = pageButton.GetComponentInChildren<Text>();
        if (componentInChildren != null)
        {
          componentInChildren.text = text;
          ((Component) componentInChildren).transform.localScale = Vector3.Scale(((Component) componentInChildren).transform.localScale, new Vector3(2f, 2f, 1f));
        }
      }
      Object.Destroy((Object) pageButton.GetComponent<ModeSelectButton>());
      UnityEvent unityEvent = new UnityEvent();
      // ISSUE: method pointer
      unityEvent.AddListener(new UnityAction((object) onPressed, __methodptr(Invoke)));
      pageButton.AddComponent<GorillaPressableButton>().onPressButton = unityEvent;
      Transform transform = pageButton.transform.Find("NewSplash");
      if (transform != null && Object.op_Implicit((Object) transform) && ((Component) transform).gameObject.activeSelf)
        ((Component) transform).gameObject.SetActive(false);
      return pageButton;
    }
  }

  private void ModifySelectionButtons()
  {
    this.PageCapacity = this.Layout.currentButtons.Count<ModeSelectButton>((Func<ModeSelectButton, bool>) (button => ((Component) button).gameObject.activeSelf));
    foreach (ModeSelectButton currentButton in this.Layout.currentButtons)
    {
      if (!this.moddedSelectionButtons.Contains(currentButton))
      {
        this.moddedSelectionButtons.Add(currentButton);
        TMP_Text gameModeTitle = currentButton.gameModeTitle;
        gameModeTitle.fontSizeMax = gameModeTitle.fontSize;
        gameModeTitle.fontSizeMin = 0.0f;
        gameModeTitle.enableAutoSizing = true;
        gameModeTitle.transform.localPosition = new Vector3(gameModeTitle.transform.localPosition.x, 0.0f, gameModeTitle.transform.localPosition.z + 0.08f);
      }
    }
  }
}

Code is shown from GitHub as-is. Linking a repository doesn’t prove that a release file on Canopy was built from it.