Repository navigation
Expand file tree
/
Copy pathWorldBoxMod.cs
More file actions
128 lines (114 loc) · 4.79 KB
/
Copy pathWorldBoxMod.cs
File metadata and controls
128 lines (114 loc) · 4.79 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.IO;
using System.Reflection;
using UnityEngine;
using WorldBoxModLoader.ModAPI;
using Path = System.IO.Path;
namespace WorldBoxModLoader
{
internal sealed class WorldBoxMod : MonoBehaviour
{
private static Assembly ModAssembly = Assembly.GetExecutingAssembly();
private Vector2 newPosition = new Vector2 (154, 18);
private bool initialized;
private void Update()
{
if (Config.gameLoaded && Config.experimentalMode && !initialized)
{
Initialize();
enabled = false;
}
}
private void Initialize()
{
InitFileSystem();
initialized = true;
ModCompiler.Awake();
SetupModsTab();
}
private void SetupModsTab()
{
PowersTab tab = Utils.CreateTab("Mods", "Mods", UtilsInternal.GetIcon());
PowerButton disableAllMods = Utils.CreateSimplePowerButton("Disable all mods", ModLoader.DisableAllMods,
UtilsInternal.LoadManifestSprite("DisableAll.png"), "Disables all mods");
PowerButton enableAllMods = Utils.CreateSimplePowerButton("Enable all mods", ModLoader.EnableAllMods,
UtilsInternal.LoadManifestSprite("EnableAll.png"), "Enables all mods");
Utils.AddButtonToTab(disableAllMods, tab, new Vector2(118, 18));
Utils.AddButtonToTab(enableAllMods, tab, new Vector2(118, -18));
foreach (ModConstants mod in ModCompiler.CompiledMods)
{
PowerButton modButton = UtilsInternal.CreateModPowerButton(mod.ModName, ModLoader.ToggleMod,
UtilsInternal.LoadSprite(Path.Combine(mod.MetaLocation, "icon.png")), mod, mod.Description);
Utils.AddButtonToTab(modButton, tab, newPosition);
if (ModCompiler.CompiledMods.IndexOf(mod) % 2 == 0)
newPosition = new Vector2(newPosition.x, -18);
else
newPosition = new Vector2(newPosition.x + 36, 18);
}
}
private void InitFileSystem()
{
if (!Directory.Exists(Paths.WBMLAssembliesPath))
{
Directory.CreateDirectory(Paths.WBMLAssembliesPath);
}
if (!Directory.Exists(Paths.ModsPath))
{
Directory.CreateDirectory(Paths.ModsPath);
}
if (!Directory.Exists(Paths.CompilationsPath))
{
Directory.CreateDirectory(Paths.CompilationsPath);
}
ExtractAssemblies();
LoadAssemblies();
void ExtractAssemblies()
{
var resources = ModAssembly.GetManifestResourceNames();
foreach (var resource in resources)
{
if (resource.EndsWith(".dll"))
{
var file_name = resource.Replace("WorldBoxModLoader.resources.assemblies.", "");
var file_path = Path.Combine(Paths.WBMLAssembliesPath, file_name).Replace("-renamed", "");
using var stream = ModAssembly.GetManifestResourceStream(resource);
using var file = new FileStream(file_path, FileMode.Create, FileAccess.Write);
stream.CopyTo(file);
stream.Dispose();
file.Dispose();
}
}
}
void LoadAssemblies()
{
foreach (var file_full_path in Directory.GetFiles(Paths.WBMLAssembliesPath, "*.dll"))
{
try
{
Assembly.LoadFrom(file_full_path);
}
catch (BadImageFormatException)
{
Debug.LogError($"" +
$"BadImageFormatException: " +
$"The file {file_full_path} is not a valid assembly.");
}
catch (FileNotFoundException e)
{
Debug.LogError($"FileNotFoundException: " +
$"The file {file_full_path} is not found.");
Debug.LogError(e.Message);
Debug.LogError(e.StackTrace);
}
catch (Exception e)
{
Debug.LogError($"Exception: " +
$"Failed to load assembly {file_full_path}.");
Debug.LogError(e.Message);
Debug.LogError(e.StackTrace);
}
}
}
}
}
}