-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
UIModSourceItem.cs
309 lines (283 loc) · 11.9 KB
/
UIModSourceItem.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Terraria.GameContent.UI.Elements;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader.Core;
using Terraria.ModLoader.Engine;
using Terraria.ModLoader.IO;
using Terraria.ModLoader.UI.ModBrowser;
using Terraria.UI;
using Terraria.UI.Chat;
namespace Terraria.ModLoader.UI
{
//TODO common 'Item' code
internal class UIModSourceItem : UIPanel
{
private readonly string _mod;
internal readonly string modName;
private readonly Texture2D _dividerTexture;
private readonly UIText _modName;
private readonly LocalMod _builtMod;
private bool _upgradePotentialChecked;
public UIModSourceItem(string mod, LocalMod builtMod) {
_mod = mod;
BorderColor = new Color(89, 116, 213) * 0.7f;
_dividerTexture = UICommon.DividerTexture;
Height.Pixels = 90;
Width.Percent = 1f;
SetPadding(6f);
string addendum = Path.GetFileName(mod).Contains(" ") ? $" [c/FF0000:{Language.GetTextValue("tModLoader.MSModSourcesCantHaveSpaces")}]" : "";
modName = Path.GetFileName(mod);
_modName = new UIText(modName + addendum) {
Left = { Pixels = 10 },
Top = { Pixels = 5 }
};
Append(_modName);
var buildButton = new UIAutoScaleTextTextPanel<string>(Language.GetTextValue("tModLoader.MSBuild")) {
Width = { Pixels = 100 },
Height = { Pixels = 36 },
Left = { Pixels = 10 },
Top = { Pixels = 40 }
}.WithFadedMouseOver();
buildButton.PaddingTop -= 2f;
buildButton.PaddingBottom -= 2f;
buildButton.OnClick += BuildMod;
Append(buildButton);
var buildReloadButton = new UIAutoScaleTextTextPanel<string>(Language.GetTextValue("tModLoader.MSBuildReload"));
buildReloadButton.CopyStyle(buildButton);
buildReloadButton.Width.Pixels = 200;
buildReloadButton.Left.Pixels = 150;
buildReloadButton.WithFadedMouseOver();
buildReloadButton.OnClick += BuildAndReload;
Append(buildReloadButton);
_builtMod = builtMod;
if (builtMod != null) {
var publishButton = new UIAutoScaleTextTextPanel<string>(Language.GetTextValue("tModLoader.MSPublish"));
publishButton.CopyStyle(buildReloadButton);
publishButton.Width.Pixels = 100;
publishButton.Left.Pixels = 390;
publishButton.WithFadedMouseOver();
publishButton.OnClick += PublishMod;
Append(publishButton);
}
OnDoubleClick += BuildAndReload;
}
protected override void DrawSelf(SpriteBatch spriteBatch) {
base.DrawSelf(spriteBatch);
CalculatedStyle innerDimensions = GetInnerDimensions();
Vector2 drawPos = new Vector2(innerDimensions.X + 5f, innerDimensions.Y + 30f);
spriteBatch.Draw(_dividerTexture, drawPos, null, Color.White, 0f, Vector2.Zero, new Vector2((innerDimensions.Width - 10f) / 8f, 1f), SpriteEffects.None, 0f);
// This code here rather than ctor since the delay for dozens of mod source folders is noticable.
if (!_upgradePotentialChecked) {
_upgradePotentialChecked = true;
string modFolderName = Path.GetFileName(_mod);
string csprojFile = Path.Combine(_mod, $"{modFolderName}.csproj");
if (!File.Exists(csprojFile) || Interface.createMod.CsprojUpdateNeeded(File.ReadAllText(csprojFile))) {
var icon = UICommon.ButtonExclamationTexture;
var upgradeCSProjButton = new UIHoverImage(icon, Language.GetTextValue("tModLoader.MSUpgradeCSProj")) {
Left = { Pixels = -26, Percent = 1f },
Top = { Pixels = 4 }
};
upgradeCSProjButton.OnClick += (s, e) => {
File.WriteAllText(csprojFile, Interface.createMod.GetModCsproj(modFolderName));
string propertiesFolder = Path.Combine(_mod, "Properties");
string AssemblyInfoFile = Path.Combine(propertiesFolder, "AssemblyInfo.cs");
if (File.Exists(AssemblyInfoFile))
File.Delete(AssemblyInfoFile);
string objFolder = Path.Combine(_mod, "obj"); // Old files can cause some issues.
if (Directory.Exists(objFolder))
Directory.Delete(objFolder, true);
string binFolder = Path.Combine(_mod, "bin");
if (Directory.Exists(binFolder))
Directory.Delete(binFolder, true);
Directory.CreateDirectory(propertiesFolder);
File.WriteAllText(Path.Combine(propertiesFolder, $"launchSettings.json"), Interface.createMod.GetLaunchSettings());
Main.PlaySound(SoundID.MenuOpen);
Main.menuMode = Interface.modSourcesID;
};
Append(upgradeCSProjButton);
}
}
}
public override void MouseOver(UIMouseEvent evt) {
base.MouseOver(evt);
BackgroundColor = UICommon.DefaultUIBlue;
BorderColor = new Color(89, 116, 213);
}
public override void MouseOut(UIMouseEvent evt) {
base.MouseOut(evt);
BackgroundColor = new Color(63, 82, 151) * 0.7f;
BorderColor = new Color(89, 116, 213) * 0.7f;
}
public override int CompareTo(object obj) {
UIModSourceItem uIModSourceItem = obj as UIModSourceItem;
if (uIModSourceItem == null) {
return base.CompareTo(obj);
}
if (uIModSourceItem._builtMod == null && _builtMod == null)
return _modName.Text.CompareTo(uIModSourceItem._modName.Text);
if (uIModSourceItem._builtMod == null)
return -1;
if (_builtMod == null)
return 1;
return uIModSourceItem._builtMod.lastModified.CompareTo(_builtMod.lastModified);
}
private void BuildMod(UIMouseEvent evt, UIElement listeningElement) {
Main.PlaySound(10);
Interface.buildMod.Build(_mod, false);
}
private void BuildAndReload(UIMouseEvent evt, UIElement listeningElement) {
Main.PlaySound(10);
Interface.buildMod.Build(_mod, true);
}
private void PublishMod(UIMouseEvent evt, UIElement listeningElement) {
if (ModLoader.modBrowserPassphrase == "") {
Main.menuMode = Interface.enterPassphraseMenuID;
Interface.enterPassphraseMenu.SetGotoMenu(Interface.modSourcesID);
return;
}
Main.PlaySound(10);
try {
var modFile = _builtMod.modFile;
var bp = _builtMod.properties;
PublishModInner(modFile, bp);
}
catch (WebException e) {
UIModBrowser.LogModBrowserException(e);
}
}
internal static void PublishModCommandLine(string modName, string passphrase, string steamid64) {
try {
InstallVerifier.IsGoG = true;
ModLoader.SteamID64 = steamid64;
ModLoader.modBrowserPassphrase = passphrase;
if (string.IsNullOrWhiteSpace(ModLoader.modBrowserPassphrase) || string.IsNullOrWhiteSpace(ModLoader.SteamID64)) {
throw new Exception("-passphrase and -steamid64 are required for publishing via command line");
}
LocalMod localMod;
var modPath = Path.Combine(ModLoader.ModPath, modName + ".tmod");
var modFile = new TmodFile(modPath);
using (modFile.Open()) // savehere, -tmlsavedirectory, normal (test linux too)
localMod = new LocalMod(modFile);
PublishModInner(modFile, localMod.properties, true);
}
catch (Exception e){
Console.WriteLine("Something went wrong with command line mod publishing.");
Console.WriteLine(e.ToString());
Environment.Exit(1);
}
Environment.Exit(0);
}
private static void PublishModInner(TmodFile modFile, BuildProperties bp, bool commandLine = false) {
var files = new List<UploadFile>();
files.Add(new UploadFile {
Name = "file",
Filename = Path.GetFileName(modFile.path),
// ContentType = "text/plain",
Content = File.ReadAllBytes(modFile.path)
});
if (modFile.HasFile("icon.png")) { // Test this on server
using (modFile.Open())
files.Add(new UploadFile {
Name = "iconfile",
Filename = "icon.png",
Content = modFile.GetBytes("icon.png")
});
}
if (bp.beta)
throw new WebException(Language.GetTextValue("tModLoader.BetaModCantPublishError"));
if (bp.buildVersion != modFile.tModLoaderVersion)
throw new WebException(Language.GetTextValue("OutdatedModCantPublishError.BetaModCantPublishError"));
var values = new NameValueCollection
{
{ "displayname", bp.displayName },
{ "displaynameclean", string.Join("", ChatManager.ParseMessage(bp.displayName, Color.White).Where(x=> x.GetType() == typeof(TextSnippet)).Select(x => x.Text)) },
{ "name", modFile.name },
{ "version", "v"+bp.version },
{ "author", bp.author },
{ "homepage", bp.homepage },
{ "description", bp.description },
{ "steamid64", ModLoader.SteamID64 },
{ "modloaderversion", "tModLoader v"+modFile.tModLoaderVersion },
{ "passphrase", ModLoader.modBrowserPassphrase },
{ "modreferences", String.Join(", ", bp.modReferences.Select(x => x.mod)) },
{ "modside", bp.side.ToFriendlyString() },
};
if (values["steamid64"].Length != 17)
throw new WebException($"The steamid64 '{values["steamid64"]}' is invalid, verify that you are logged into Steam and don't have a pirated copy of Terraria.");
if (string.IsNullOrEmpty(values["author"]))
throw new WebException($"You need to specify an author in build.txt");
ServicePointManager.Expect100Continue = false;
string url = "http://javid.ddns.net/tModLoader/publishmod.php";
using (PatientWebClient client = new PatientWebClient()) {
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, policyErrors) => true;
Interface.progress.Show(displayText: $"Uploading: {modFile.name}", gotoMenu: Interface.modSourcesID, cancel: client.CancelAsync);
var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", System.Globalization.NumberFormatInfo.InvariantInfo);
client.Headers["Content-Type"] = "multipart/form-data; boundary=" + boundary;
//boundary = "--" + boundary;
byte[] data = UploadFile.GetUploadFilesRequestData(files, values, boundary);
if (commandLine) {
var result = client.UploadData(new Uri(url), data); // could use async version for progress output maybe
string response = HandlePublishResponse(modFile, result);
Console.WriteLine(Language.GetTextValue("tModLoader.MBServerResponse", response));
if (result.Length <= 256 || result[result.Length - 256 - 1] != '~') {
throw new Exception("Publish failed due to invalid response from server");
}
}
else {
client.UploadDataCompleted += (s, e) => PublishUploadDataComplete(s, e, modFile);
client.UploadProgressChanged += (s, e) => Interface.progress.Progress = (float)e.BytesSent / e.TotalBytesToSend;
client.UploadDataAsync(new Uri(url), data);
}
}
}
private static void PublishUploadDataComplete(object s, UploadDataCompletedEventArgs e, TmodFile theTModFile) {
if (e.Error != null) {
if (e.Cancelled) {
Main.menuMode = Interface.modSourcesID;
return;
}
UIModBrowser.LogModBrowserException(e.Error);
return;
}
ModLoader.GetMod(theTModFile.name)?.Close();
var result = e.Result;
string response = HandlePublishResponse(theTModFile, result);
UIModBrowser.LogModPublishInfo(response);
}
private static string HandlePublishResponse(TmodFile theTModFile, byte[] result) {
int responseLength = result.Length;
if (result.Length > 256 && result[result.Length - 256 - 1] == '~') {
using (var fileStream = File.Open(theTModFile.path, FileMode.Open, FileAccess.ReadWrite))
using (var fileReader = new BinaryReader(fileStream))
using (var fileWriter = new BinaryWriter(fileStream)) {
fileReader.ReadBytes(4); // "TMOD"
fileReader.ReadString(); // ModLoader.version.ToString()
fileReader.ReadBytes(20); // hash
if (fileStream.Length - fileStream.Position > 256) // Extrememly basic check in case ReadString errors?
fileWriter.Write(result, result.Length - 256, 256);
}
responseLength -= 257;
}
string response = Encoding.UTF8.GetString(result, 0, responseLength);
return response;
}
private class PatientWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri uri) {
HttpWebRequest w = (HttpWebRequest)base.GetWebRequest(uri);
w.Timeout = System.Threading.Timeout.Infinite;
w.AllowWriteStreamBuffering = false; // Should use less ram.
return w;
}
}
}
}