Skip to content

Commit

Permalink
Remove as many calls to Resources.FindObjectsOfTypeAll as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Mar 23, 2024
1 parent 3f929b6 commit 6a5e103
Show file tree
Hide file tree
Showing 26 changed files with 58 additions and 64 deletions.
1 change: 1 addition & 0 deletions BeatSaberMarkupLanguage/BeatSaberMarkupLanguage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Reference Include="Menu.ColorSettings">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\Menu.ColorSettings.dll</HintPath>
<Private>False</Private>
<Publicize>True</Publicize>
</Reference>
<Reference Include="Menu.CommonLib">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\Menu.CommonLib.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion BeatSaberMarkupLanguage/Components/Backgroundable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private static ImageView FindTemplate(string name, string backgroundName)
{
string objectName = ObjectNames[name];
string parentName = ObjectParentNames[name];
ImageView[] images = Resources.FindObjectsOfTypeAll<ImageView>();
ImageView[] images = FindObjectsOfType<ImageView>(true);
for (int i = 0; i < images.Length; i++)
{
ImageView image = images[i];
Expand Down
3 changes: 1 addition & 2 deletions BeatSaberMarkupLanguage/Components/ModalKeyboard.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BeatSaberMarkupLanguage.Parser;
using BGLib.Polyglot;
using HMUI;
Expand Down Expand Up @@ -195,7 +194,7 @@ public void SetButtonType(string buttonName = "Q")
{
if (BaseButton == null)
{
BaseButton = Resources.FindObjectsOfTypeAll<Button>().Where(x => x.name == "Q").First();
BaseButton = BeatSaberUI.DiContainer.Resolve<UIKeyboardManager>().transform.Find("KeyboardWrapper/Keyboard/Letters/Row/Q").GetComponent<Button>();
}
}

Expand Down
7 changes: 3 additions & 4 deletions BeatSaberMarkupLanguage/Tags/ButtonTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using BeatSaberMarkupLanguage.Components;
using BeatSaberMarkupLanguage.Components;
using BGLib.Polyglot;
using TMPro;
using UnityEngine;
Expand All @@ -13,13 +12,13 @@ public class ButtonTag : BSMLTag

public override string[] Aliases => new[] { "button" };

public virtual string PrefabButton => "PracticeButton";
public virtual Button PrefabButton => BeatSaberUI.DiContainer.Resolve<StandardLevelDetailViewController>()._standardLevelDetailView.practiceButton;

public override GameObject CreateObject(Transform parent)
{
if (buttonPrefab == null)
{
buttonPrefab = Resources.FindObjectsOfTypeAll<Button>().Where(x => x.name == PrefabButton).Last();
buttonPrefab = PrefabButton;
}

Button button = Object.Instantiate(buttonPrefab, parent, false);
Expand Down
2 changes: 1 addition & 1 deletion BeatSaberMarkupLanguage/Tags/ButtonWithIconTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override GameObject CreateObject(Transform parent)
{
if (buttonWithIconTemplate == null)
{
buttonWithIconTemplate = Resources.FindObjectsOfTypeAll<Button>().Where(x => x.name == "PracticeButton").Last();
buttonWithIconTemplate = BeatSaberUI.DiContainer.Resolve<StandardLevelDetailViewController>()._standardLevelDetailView.practiceButton;
}

Button button = Object.Instantiate(buttonWithIconTemplate, parent, false);
Expand Down
13 changes: 8 additions & 5 deletions BeatSaberMarkupLanguage/Tags/CustomListTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using BeatSaberMarkupLanguage.Components;
using BeatSaberMarkupLanguage.Components;
using HMUI;
using UnityEngine;
using UnityEngine.UI;
Expand All @@ -17,8 +16,12 @@ public class CustomListTag : BSMLTag

public override GameObject CreateObject(Transform parent)
{
RectTransform container = (RectTransform)new GameObject("BSMLCustomListContainer", typeof(RectTransform)).transform;
LayoutElement layoutElement = container.gameObject.AddComponent<LayoutElement>();
GameObject containerObject = new("BSMLCustomListContainer", typeof(RectTransform), typeof(LayoutElement))
{
layer = 5,
};

RectTransform container = (RectTransform)containerObject.transform;
container.SetParent(parent, false);

GameObject gameObject = new("BSMLCustomList")
Expand All @@ -31,7 +34,7 @@ public override GameObject CreateObject(Transform parent)

if (canvasTemplate == null)
{
canvasTemplate = Resources.FindObjectsOfTypeAll<Canvas>().Where(x => x.name == "DropdownTableView").First();
canvasTemplate = DiContainer.Resolve<GameplaySetupViewController>()._playerSettingsPanelController._noteJumpStartBeatOffsetDropdown._simpleTextDropdown._tableView.GetComponent<Canvas>();
}

gameObject.AddComponent<ScrollRect>();
Expand Down
5 changes: 2 additions & 3 deletions BeatSaberMarkupLanguage/Tags/IconSegmentedControlTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using HMUI;
using HMUI;
using UnityEngine;

namespace BeatSaberMarkupLanguage.Tags
Expand All @@ -14,7 +13,7 @@ public override GameObject CreateObject(Transform parent)
{
if (segmentedControlTemplate == null)
{
segmentedControlTemplate = Resources.FindObjectsOfTypeAll<IconSegmentedControl>().Where(x => x.name == "BeatmapCharacteristicSegmentedControl" && x._container != null).First();
segmentedControlTemplate = DiContainer.Resolve<StandardLevelDetailViewController>()._standardLevelDetailView._beatmapCharacteristicSegmentedControlController.GetComponent<IconSegmentedControl>();
}

IconSegmentedControl iconSegmentedControl = DiContainer.InstantiatePrefabForComponent<IconSegmentedControl>(segmentedControlTemplate, parent);
Expand Down
5 changes: 2 additions & 3 deletions BeatSaberMarkupLanguage/Tags/LeaderboardTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using UnityEngine;
using UnityEngine;

namespace BeatSaberMarkupLanguage.Tags
{
Expand All @@ -13,7 +12,7 @@ public override GameObject CreateObject(Transform parent)
{
if (leaderboardTemplate == null)
{
leaderboardTemplate = Resources.FindObjectsOfTypeAll<LeaderboardTableView>().Where(x => x.name == "LeaderboardTableView").First();
leaderboardTemplate = DiContainer.Resolve<PlatformLeaderboardViewController>()._leaderboardTableView;
}

LeaderboardTableView table = DiContainer.InstantiatePrefabForComponent<LeaderboardTableView>(leaderboardTemplate, parent);
Expand Down
13 changes: 8 additions & 5 deletions BeatSaberMarkupLanguage/Tags/ListTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using BeatSaberMarkupLanguage.Components;
using BeatSaberMarkupLanguage.Components;
using HMUI;
using UnityEngine;
using UnityEngine.UI;
Expand All @@ -15,8 +14,12 @@ public class ListTag : BSMLTag

public override GameObject CreateObject(Transform parent)
{
RectTransform container = new GameObject("BSMLListContainer", typeof(RectTransform)).transform as RectTransform;
LayoutElement layoutElement = container.gameObject.AddComponent<LayoutElement>();
GameObject containerObject = new("BSMLListContainer", typeof(RectTransform), typeof(LayoutElement))
{
layer = 5,
};

RectTransform container = (RectTransform)containerObject.transform;
container.SetParent(parent, false);

GameObject gameObject = new("BSMLList")
Expand All @@ -29,7 +32,7 @@ public override GameObject CreateObject(Transform parent)

if (canvasTemplate == null)
{
canvasTemplate = Resources.FindObjectsOfTypeAll<Canvas>().Where(x => x.name == "DropdownTableView").First();
canvasTemplate = DiContainer.Resolve<GameplaySetupViewController>()._playerSettingsPanelController._noteJumpStartBeatOffsetDropdown._simpleTextDropdown._tableView.GetComponent<Canvas>();
}

gameObject.AddComponent<ScrollRect>();
Expand Down
6 changes: 2 additions & 4 deletions BeatSaberMarkupLanguage/Tags/LoadingIndicatorTag.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;
using HMUI;
using UnityEngine;
using UnityEngine;
using UnityEngine.UI;

namespace BeatSaberMarkupLanguage.Tags
Expand All @@ -15,7 +13,7 @@ public override GameObject CreateObject(Transform parent)
{
if (loadingTemplate == null)
{
loadingTemplate = Resources.FindObjectsOfTypeAll<ImageView>().Where(x => x.gameObject.name == "LoadingIndicator").First().gameObject;
loadingTemplate = DiContainer.Resolve<LevelCollectionNavigationController>()._loadingControl._loadingContainer.transform.Find("LoadingIndicator").gameObject;
}

GameObject loadingIndicator = Object.Instantiate(loadingTemplate, parent, false);
Expand Down
7 changes: 3 additions & 4 deletions BeatSaberMarkupLanguage/Tags/ModalColorPickerTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using BeatSaberMarkupLanguage.Components;
using BeatSaberMarkupLanguage.Components;
using HMUI;
using UnityEngine;
using UnityEngine.UI;
Expand All @@ -10,7 +9,7 @@ public class ModalColorPickerTag : ModalTag
{
private RGBPanelController rgbTemplate;
private HSVPanelController hsvTemplate;
private ImageView currentColorTemplate;
private Image currentColorTemplate;

public override string[] Aliases => new[] { "modal-color-picker" };

Expand Down Expand Up @@ -38,7 +37,7 @@ public override GameObject CreateObject(Transform parent)

if (currentColorTemplate == null)
{
currentColorTemplate = Resources.FindObjectsOfTypeAll<ImageView>().Where(iv => iv.transform.parent != null && iv.gameObject.name == "SaberColorA" && iv.transform.parent.name == "ColorSchemeView").First();
currentColorTemplate = DiContainer.Resolve<GameplaySetupViewController>()._colorsOverrideSettingsPanelController._colorSchemeDropDown._cellPrefab._colorSchemeView._saberAColorImage;
}

RGBPanelController rgbController = Object.Instantiate(rgbTemplate, gameObject.transform, false);
Expand Down
5 changes: 2 additions & 3 deletions BeatSaberMarkupLanguage/Tags/ModalTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using HMUI;
using HMUI;
using UnityEngine;
using UnityEngine.UI;

Expand All @@ -15,7 +14,7 @@ public override GameObject CreateObject(Transform parent)
{
if (modalViewTemplate == null)
{
modalViewTemplate = Resources.FindObjectsOfTypeAll<ModalView>().Where(mv => mv.name == "DropdownTableView" && mv.transform.parent != null && mv.transform.parent.name == "ColorSchemeDropDown").First();
modalViewTemplate = DiContainer.Resolve<GameplaySetupViewController>()._colorsOverrideSettingsPanelController._colorSchemeDropDown._modalView;
}

ModalView modalView = DiContainer.InstantiatePrefabForComponent<ModalView>(modalViewTemplate, parent);
Expand Down
2 changes: 1 addition & 1 deletion BeatSaberMarkupLanguage/Tags/ModifierTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override GameObject CreateObject(Transform parent)
{
if (toggleTemplate == null)
{
toggleTemplate = Resources.FindObjectsOfTypeAll<GameplayModifierToggle>().Where(x => x.name == "InstaFail").First();
toggleTemplate = DiContainer.Resolve<GameplaySetupViewController>()._gameplayModifiersPanelController.GetComponentsInChildren<GameplayModifierToggle>().First(gmt => gmt.name == "InstaFail");
}

GameplayModifierToggle baseModifier = Object.Instantiate(toggleTemplate, parent, false);
Expand Down
3 changes: 2 additions & 1 deletion BeatSaberMarkupLanguage/Tags/PageButtonTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using BeatSaberMarkupLanguage.Components;
using HMUI;
using UnityEngine;
using UnityEngine.UI;

Expand All @@ -15,7 +16,7 @@ public override GameObject CreateObject(Transform parent)
{
if (buttonTemplate == null)
{
buttonTemplate = Resources.FindObjectsOfTypeAll<Button>().Where(x => x.name == "UpButton").Last();
buttonTemplate = DiContainer.Resolve<PlayerOptionsViewController>()._playerSettingsPanelController.GetComponent<ScrollView>()._pageUpButton;
}

Button button = Object.Instantiate(buttonTemplate, parent, false);
Expand Down
2 changes: 1 addition & 1 deletion BeatSaberMarkupLanguage/Tags/PrimaryButtonTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class PrimaryButtonTag : ButtonTag
{
public override string[] Aliases => new[] { "primary-button", "action-button" };

public override string PrefabButton => "PlayButton";
public override Button PrefabButton => BeatSaberUI.DiContainer.Resolve<PracticeViewController>()._playButton;

public override GameObject CreateObject(Transform parent)
{
Expand Down
5 changes: 2 additions & 3 deletions BeatSaberMarkupLanguage/Tags/ScrollIndicatorTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using BeatSaberMarkupLanguage.Components;
using BeatSaberMarkupLanguage.Components;
using HMUI;
using UnityEngine;
using Object = UnityEngine.Object;
Expand All @@ -16,7 +15,7 @@ public static GameObject VerticalScrollTemplate
{
if (verticalScrollTemplate == null)
{
verticalScrollTemplate = Resources.FindObjectsOfTypeAll<VerticalScrollIndicator>().First().gameObject;
verticalScrollTemplate = BeatSaberUI.DiContainer.Resolve<LevelCollectionNavigationController>()._levelCollectionViewController._levelCollectionTableView.GetComponent<ScrollView>()._verticalScrollIndicator.gameObject;
}

return verticalScrollTemplate;
Expand Down
4 changes: 2 additions & 2 deletions BeatSaberMarkupLanguage/Tags/Settings/ColorSettingTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override GameObject CreateObject(Transform parent)
{
if (baseSettings == null)
{
baseSettings = Resources.FindObjectsOfTypeAll<FormattedFloatListSettingsValueController>().Where(x => x.name == "VRRenderingScale").First();
baseSettings = DiContainer.Resolve<MainSettingsMenuViewController>()._settingsSubMenuInfos.Select(m => m.viewController).First(vc => vc.name == "GraphicSettings").transform.Find("ViewPort/Content/VRRenderingScale").GetComponent<FormattedFloatListSettingsValueController>();
}

FormattedFloatListSettingsValueController baseSetting = Object.Instantiate(baseSettings, parent, false);
Expand Down Expand Up @@ -62,7 +62,7 @@ public override GameObject CreateObject(Transform parent)

if (colorImage == null)
{
colorImage = Resources.FindObjectsOfTypeAll<Image>().Where(i => i.sprite != null && i.gameObject.name == "ColorImage" && i.sprite.name == "NoteCircle").First();
colorImage = (ImageView)DiContainer.Resolve<GameplaySetupViewController>()._colorsOverrideSettingsPanelController._editColorSchemeController._previousColorPanelController._graphics.First(g => g.name == "ColorImage");
}

Image instance = Object.Instantiate(colorImage, valuePick, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using BeatSaberMarkupLanguage.Components;
using BeatSaberMarkupLanguage.Components.Settings;
using BGLib.Polyglot;
Expand All @@ -20,7 +19,7 @@ public override void Setup()
{
if (dropdownTemplate == null)
{
dropdownTemplate = Resources.FindObjectsOfTypeAll<SimpleTextDropdown>().Where(td => td.transform.parent != null && td.transform.parent.name == "NormalLevels").First().transform.parent.gameObject;
dropdownTemplate = DiContainer.Resolve<GameplaySetupViewController>()._environmentOverrideSettingsPanelController._elementsGO.transform.Find("NormalLevels").gameObject;
}

safePrefab = Object.Instantiate(dropdownTemplate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override GameObject CreateObject(Transform parent)
{
if (controllersTransformTemplate == null)
{
controllersTransformTemplate = Resources.FindObjectsOfTypeAll<LayoutElement>().Where(x => x.name == "PositionX").First();
controllersTransformTemplate = DiContainer.Resolve<MainSettingsMenuViewController>()._settingsSubMenuInfos.First(m => m.viewController is ControllersTransformSettingsViewController).viewController.transform.Find("Content/PositionX").GetComponent<LayoutElement>();
}

LayoutElement baseSetting = Object.Instantiate(controllersTransformTemplate, parent, false);
Expand Down
2 changes: 1 addition & 1 deletion BeatSaberMarkupLanguage/Tags/Settings/IncDecSettingTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override GameObject CreateObject(Transform parent)
{
if (valueControllerTemplate == null)
{
valueControllerTemplate = Resources.FindObjectsOfTypeAll<FormattedFloatListSettingsValueController>().Where(x => x.name == "VRRenderingScale").First();
valueControllerTemplate = DiContainer.Resolve<MainSettingsMenuViewController>()._settingsSubMenuInfos.Select(m => m.viewController).First(vc => vc.name == "GraphicSettings").transform.Find("ViewPort/Content/VRRenderingScale").GetComponent<FormattedFloatListSettingsValueController>();
}

FormattedFloatListSettingsValueController baseSetting = Object.Instantiate(valueControllerTemplate, parent, false);
Expand Down
2 changes: 1 addition & 1 deletion BeatSaberMarkupLanguage/Tags/Settings/StringSettingTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override GameObject CreateObject(Transform parent)
{
if (valueControllerTemplate == null)
{
valueControllerTemplate = Resources.FindObjectsOfTypeAll<FormattedFloatListSettingsValueController>().Where(x => x.name == "VRRenderingScale").First();
valueControllerTemplate = DiContainer.Resolve<MainSettingsMenuViewController>()._settingsSubMenuInfos.Select(m => m.viewController).First(vc => vc.name == "GraphicSettings").transform.Find("ViewPort/Content/VRRenderingScale").GetComponent<FormattedFloatListSettingsValueController>();
}

FormattedFloatListSettingsValueController baseSetting = Object.Instantiate(valueControllerTemplate, parent, false);
Expand Down
4 changes: 1 addition & 3 deletions BeatSaberMarkupLanguage/Tags/Settings/ToggleSettingTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ public class ToggleSettingTag : BSMLTag

public override string[] Aliases => new[] { "toggle-setting", "bool-setting", "checkbox-setting", "checkbox" };

public virtual string PrefabToggleName => "Fullscreen";

public override GameObject CreateObject(Transform parent)
{
if (toggleTemplate == null)
{
toggleTemplate = Resources.FindObjectsOfTypeAll<Toggle>().Select(x => x.transform.parent.gameObject).Where(p => p.name == PrefabToggleName).First();
toggleTemplate = DiContainer.Resolve<MainSettingsMenuViewController>()._settingsSubMenuInfos.Select(m => m.viewController).First(vc => vc.name == "GraphicSettings").transform.Find("ViewPort/Content/Fullscreen").gameObject;
}

GameObject gameObject = Object.Instantiate(toggleTemplate, parent, false);
Expand Down
5 changes: 2 additions & 3 deletions BeatSaberMarkupLanguage/Tags/TabSelectorTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using BeatSaberMarkupLanguage.Components;
using BeatSaberMarkupLanguage.Components;
using HMUI;
using UnityEngine;

Expand All @@ -15,7 +14,7 @@ public override GameObject CreateObject(Transform parent)
{
if (segmentControlTemplate == null)
{
segmentControlTemplate = Resources.FindObjectsOfTypeAll<TextSegmentedControl>().Where(x => x.transform.parent.name == "PlayerStatisticsViewController" && x._container != null).FirstOrDefault();
segmentControlTemplate = DiContainer.Resolve<PlayerStatisticsViewController>()._statsScopeSegmentedControl;
}

TextSegmentedControl textSegmentedControl = DiContainer.InstantiatePrefabForComponent<TextSegmentedControl>(segmentControlTemplate, parent);
Expand Down
5 changes: 2 additions & 3 deletions BeatSaberMarkupLanguage/Tags/TextSegmentedControlTag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using HMUI;
using HMUI;
using UnityEngine;

namespace BeatSaberMarkupLanguage.Tags
Expand All @@ -14,7 +13,7 @@ public override GameObject CreateObject(Transform parent)
{
if (segmentControlTemplate == null)
{
segmentControlTemplate = Resources.FindObjectsOfTypeAll<TextSegmentedControl>().Where(x => x.name == "BeatmapDifficultySegmentedControl" && x._container != null).First();
segmentControlTemplate = DiContainer.Resolve<StandardLevelDetailViewController>()._standardLevelDetailView._beatmapDifficultySegmentedControlController.GetComponent<TextSegmentedControl>();
}

TextSegmentedControl textSegmentedControl = DiContainer.InstantiatePrefabForComponent<TextSegmentedControl>(segmentControlTemplate, parent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using HMUI;
using HMUI;
using UnityEngine;

namespace BeatSaberMarkupLanguage.Tags
Expand All @@ -14,8 +13,7 @@ public override GameObject CreateObject(Transform parent)
{
if (prefab == null)
{
PlatformLeaderboardViewController vc = Resources.FindObjectsOfTypeAll<PlatformLeaderboardViewController>().First();
prefab = vc._scopeSegmentedControl;
prefab = DiContainer.Resolve<PlatformLeaderboardViewController>()._scopeSegmentedControl;
}

IconSegmentedControl control = DiContainer.InstantiatePrefabForComponent<IconSegmentedControl>(prefab, parent);
Expand Down
Loading

0 comments on commit 6a5e103

Please sign in to comment.