-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoImporter.cs
More file actions
107 lines (93 loc) · 3.41 KB
/
AutoImporter.cs
File metadata and controls
107 lines (93 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using Sunless.Game.UI.Menus;
using Sunless.Game.Import;
using Sunless.Game.ApplicationProviders;
using System.Reflection;
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
namespace SDLS
{
internal sealed class AutoImporter : MonoBehaviour
{
private static AutoImporter _instance;
public static AutoImporter Instance
{
get
{
if (_instance == null)
{
GameObject gobj = new GameObject("AutoImporter");
_instance = gobj.AddComponent<AutoImporter>();
DontDestroyOnLoad(gobj);
}
return _instance;
}
}
private bool _inTitleScreen = false;
private void Awake()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
_inTitleScreen = scene.name == "TitleScreen";
}
public bool CheckForNewContent()
{
try
{
if (!Importer.Instance.CheckIfNewContent()) return false;
Plugin.Log("New Stories available, updating them...");
StartCoroutine(WaitForMenuAndUpdate());
}
catch // We don't care about the error
{
Plugin.Error("Something seems to have gone wrong checking for new Stories. You, or Failbetter, are offline.");
return false;
}
return true;
}
// This method is a mess, since MainMenu doesn't have an Instance, instead its stored in MenuProvider as a private field.
private IEnumerator WaitForMenuAndUpdate()
{
while (!_inTitleScreen)
{
yield return null;
}
// Get the singleton instance of MenuProvider
var menuProvider = MenuProvider.Instance;
if (menuProvider == null)
{
Plugin.Error("MenuProvider.Instance is null??");
yield break;
}
// Get the private field '_mainMenu' from MenuProvider instance
FieldInfo mainMenuField = typeof(MenuProvider).GetField("_mainMenu", BindingFlags.NonPublic | BindingFlags.Instance);
if (mainMenuField == null)
{
Plugin.Error("Field '_mainMenu' not found in MenuProvider??");
yield break;
}
// Get the MainMenu instance stored in _mainMenu
if (mainMenuField.GetValue(menuProvider) is not MainMenu mainMenuInstance)
{
Plugin.Error("_mainMenu instance is null??");
yield break;
}
// Get the private method 'UpdateContent' from MainMenu class
MethodInfo updateContentMethod = typeof(MainMenu).GetMethod("UpdateContent", BindingFlags.NonPublic | BindingFlags.Instance);
if (updateContentMethod == null)
{
Plugin.Error("Method 'UpdateContent' not found in MainMenu");
yield break;
}
// Invoke UpdateContent on the MainMenu instance
updateContentMethod.Invoke(mainMenuInstance, null);
Destroy(gameObject);
}
}
}