-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDynamicTrees.cs
More file actions
98 lines (82 loc) · 3.3 KB
/
DynamicTrees.cs
File metadata and controls
98 lines (82 loc) · 3.3 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
global using DynamicTrees.Utilities.JSON;
using MelonLoader.Utils;
using ComplexLogger;
using DynamicTrees.Utilities;
using DynamicTrees.DynamicTreesComponent;
namespace DynamicTrees
{
public class Main : MelonMod
{
public static string SaveDataFile { get; } = Path.Combine(MelonEnvironment.ModsDirectory, "DynamicTrees", "DynamicTrees.json");
internal static AssetBundle TexturesBundle { get; set; }
internal static SaveDataManager SaveDataManager;
internal static ComplexLogger<Main> Logger = new();
internal static DynamicTreeData DynamicTreeData;
internal static List<DynamicTreeTexture> TreeTextures { get; set; } = new List<DynamicTreeTexture>();
public override async void OnInitializeMelon()
{
SaveDataManager ??= new();
if (SaveDataManager == null)
{
Logger.Log("SaveDataManager remains null", FlaggedLoggingLevel.Error);
return;
}
if (!Directory.Exists(Path.Combine(MelonEnvironment.ModsDirectory, "DynamicTrees"))) Directory.CreateDirectory(Path.Combine(MelonEnvironment.ModsDirectory, "DynamicTrees"));
TexturesBundle = LoadAssetBundleFromStream("DynamicTrees.Textures.dynamictrees");
if (TexturesBundle == null) Logger.Log($"TexturesBundle is null", FlaggedLoggingLevel.Error);
await LoadAllTreeTextures();
Logger.Log("Dynamic Trees is online.", FlaggedLoggingLevel.Always);
}
public override void OnSceneWasInitialized(int buildIndex, string sceneName)
{
if (SceneUtilities.IsScenePlayable(sceneName))
{
GameObject DynamicTreeObject = new() { name = "DynamicTreeObject", layer = vp_Layer.Default };
UnityEngine.Object.Instantiate(DynamicTreeObject, GameManager.GetVpFPSPlayer().transform);
GameObject.DontDestroyOnLoad(DynamicTreeObject);
DynamicTreeData ??= DynamicTreeObject.AddComponent<DynamicTreeData>();
}
TextureHelper.ReplaceTreeTextures(sceneName);
}
public override async void OnSceneWasLoaded(int buildIndex, string sceneName)
{
if (SaveDataManager != null && DynamicTreeData != null && DynamicTreeData.SaveDataProxy != null)
{
await SaveDataManager.Save(DynamicTreeData.SaveDataProxy);
}
}
public override void OnApplicationQuit()
{
SaveDataManager.Save(DynamicTreeData.SaveDataProxy);
base.OnApplicationQuit();
}
public static AssetBundle LoadAssetBundleFromStream(string name)
{
Logger.Log($"Attempting to load an AssetBundle with name: {name}", FlaggedLoggingLevel.Debug);
using (Stream? stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
{
MemoryStream? memory = new((int)stream.Length);
stream!.CopyTo(memory);
Il2CppSystem.IO.MemoryStream memoryStream = new Il2CppSystem.IO.MemoryStream(memory.ToArray());
AssetBundle loadFromMemoryInternal = AssetBundle.LoadFromStream(memoryStream);
return loadFromMemoryInternal;
}
}
public static async Task LoadAllTreeTextures()
{
foreach (string item in TextureHelper.pineTrees)
{
await LoadTreeTexture(item);
}
foreach (string item in TextureHelper.cedarTrees)
{
await LoadTreeTexture(item);
}
}
public static async Task LoadTreeTexture(string name)
{
//Logger.Log($"LoadTreeTexture({name})", FlaggedLoggingLevel.Debug);
TreeTextures.Add(new DynamicTreeTexture() { Name = name, Texture = TexturesBundle?.LoadAsset<Texture>(name) });
}
}
}