-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTModder.cs
165 lines (149 loc) · 4.77 KB
/
TModder.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.IO;
using Terraria.ModLoader;
using Terraria.ModLoader.UI;
using Terraria.UI;
using Microsoft.Xna.Framework.Graphics;
/// <summary>
/// Entry point and manager for TModder functionality.
/// </summary>
public class TerrariaModder : Mod
{
private const string TitleMenuLogoText = "TModder";
private const string ModExtension = ".tmo";
private const string ModsDirectory = "Mods";
/// <summary>
/// Loads TModder and manages mods.
/// </summary>
public override void Load()
{
LoadAndManageMods();
PatchTitleMenuLogo();
}
/// <summary>
/// Loads and manages mods found in the Mods directory.
/// </summary>
private void LoadAndManageMods()
{
string modsDirectory = Path.Combine(Main.SavePath, ModsDirectory);
if (!Directory.Exists(modsDirectory))
{
Console.WriteLine($"Mods directory not found: {modsDirectory}");
return;
}
string[] modFiles = Directory.GetFiles(modsDirectory, $"*{ModExtension}");
if (modFiles.Length == 0)
{
Console.WriteLine($"No mod files ({ModExtension}) found in the directory.");
return;
}
foreach (string modFile in modFiles)
{
CompileModToTMod(modFile);
PatchMod(modFile);
}
}
/// <summary>
/// Compiles the mod to .tmo format.
/// </summary>
private void CompileModToTMod(string modFile)
{
Console.WriteLine($"Compiling mod to .tmo: {modFile}");
try
{
string modName = Path.GetFileNameWithoutExtension(modFile);
string modDirectory = Path.GetDirectoryName(modFile);
string buildCommand = $"dotnet build {modName}.csproj -c Release -o {modDirectory} --output {modDirectory}";
var processInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + buildCommand)
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
using (var process = System.Diagnostics.Process.Start(processInfo))
{
process.WaitForExit();
if (process.ExitCode == 0)
{
Console.WriteLine($"Mod compiled to .tmo successfully: {modName}");
}
else
{
Console.WriteLine($"Error compiling mod to .tmo: {process.StandardError.ReadToEnd()}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error compiling mod to .tmo file: {ex.Message}");
}
}
/// <summary>
/// Loads and patches the mod.
/// </summary>
private void PatchMod(string modFile)
{
Console.WriteLine($"Patching mod: {modFile}");
try
{
Mod mod = ModLoader.GetModFromFile(modFile);
if (mod != null)
{
mod.Autoload();
Console.WriteLine($"Mod patched successfully: {mod.Name}");
}
else
{
Console.WriteLine($"Failed to load mod from file: {modFile}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error patching mod: {ex.Message}");
}
}
/// <summary>
/// Patches the title menu logo with the TModder logo.
/// </summary>
private void PatchTitleMenuLogo()
{
try
{
Main.logoTexture = GetTitleLogoTexture();
Console.WriteLine("Title menu logo patched successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to patch title menu logo: {ex.Message}");
}
}
/// <summary>
/// Retrieves the title menu logo texture.
/// </summary>
/// <returns>The title menu logo texture.</returns>
private Texture2D GetTitleLogoTexture()
{
string logoFilePath = "Content/Logo/Logo.png";
try
{
using (FileStream stream = new FileStream(logoFilePath, FileMode.Open))
{
return Texture2D.FromStream(Main.instance.GraphicsDevice, stream);
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load title menu logo texture: {ex.Message}");
return null;
}
}
/// <summary>
/// Modifies the interface layers to include mod management UI.
/// </summary>
/// <param name="layers">List of interface layers.</param>
public override void ModifyInterfaceLayers(System.Collections.Generic.List<GameInterfaceLayer> layers)
{
ModUIManager.ModUIInterface.AddLayer(layers);
}
}