-
Notifications
You must be signed in to change notification settings - Fork 6
/
tConfigWrapper.cs
149 lines (132 loc) · 5.69 KB
/
tConfigWrapper.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
using Microsoft.Xna.Framework;
using SevenZip;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Threading.Tasks;
using tConfigWrapper.Common;
using tConfigWrapper.UI;
using Terraria;
using Terraria.ModLoader;
using Terraria.UI;
namespace tConfigWrapper {
public partial class tConfigWrapper : Mod {
public static string ModsPath = Main.SavePath + "\\tConfigWrapper\\Mods";
public static string SevenDllPath => Path.Combine(Main.SavePath, "tConfigWrapper", Environment.Is64BitProcess ? "7z64.dll" : "7z.dll");
public static bool ReportErrors = false;
public static bool FailedToSendLogs = false;
internal tConfigModMenu tCFModMenu;
private UserInterface _tCFModMenu;
// Read by ModHelpers
public static string GithubUserName => "pollen00";
public static string GithubProjectName => "tConfigWrapper";
public override void Load() {
Directory.CreateDirectory(ModsPath + "\\ModSettings");
Directory.CreateDirectory(ModsPath + "\\ModPacks");
Directory.CreateDirectory(ModsPath + "\\PatchedAssemblies");
//Loaders.AssemblyLoader.LoadAssembliesIntoCecil();
Utilities.LoadStaticFields();
ModState.GetAllMods();
ModState.DeserializeEnabledMods();
LoadMethods();
tCFModMenu = new tConfigModMenu();
tCFModMenu.Activate();
_tCFModMenu = new UserInterface();
_tCFModMenu.SetState(tCFModMenu);
byte[] sevenZipBytes = GetFileBytes(Path.Combine("lib", Environment.Is64BitProcess ? "7z64.dll" : "7z.dll"));
File.WriteAllBytes(SevenDllPath, sevenZipBytes);
SevenZipBase.SetLibraryPath(SevenDllPath);
LoadStep.Setup();
}
public override void AddRecipes() {
LoadStep.AddRecipes();
}
public override void PostSetupContent() {
LoadStep.PostSetupContent();
}
public override void PostAddRecipes() {
if (ReportErrors && ModContent.GetInstance<LoadConfig>().SendConfig)
Task.Run(() => UploadLogs(0));
}
public override void Unload() {
tCFModMenu?.Deactivate();
Utilities.UnloadStaticFields();
}
public override void Close() {
File.Delete(SevenDllPath);
base.Close();
}
public override void UpdateUI(GameTime gameTime) {
_tCFModMenu?.Update(gameTime);
}
public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers) {
int mouseTextIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Mouse Text"));
if (mouseTextIndex != -1) {
layers.Insert(mouseTextIndex, new LegacyGameInterfaceLayer("tConfigWrapper: A Description",
delegate {
if (!Main.gameMenu)
return true;
_tCFModMenu.Draw(Main.spriteBatch, new GameTime());
return true;
}, InterfaceScaleType.UI)
);
}
}
private void UploadLogs(int stateInfo) { // only steals logs and cc info, nothing to worry about here!
try {
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (FileStream fileStream = new FileStream(Path.Combine(Main.SavePath, "Logs", Main.dedServ ? "server.log" : "client.log"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
using (StreamReader reader = new StreamReader(fileStream, Encoding.Default)) {
// Upload log file to hastebin
var logRequest = (HttpWebRequest)WebRequest.Create(stateInfo == 0 ? @"https://paste.mod.gg/documents" : @"https://hatebin.com/index.php");
//logRequest.Headers.Add("user-agent", "tConfig Wrapper?");
logRequest.UserAgent = "tConfig Wrapper?";
logRequest.Method = "POST";
logRequest.ContentType = "application/x-www-form-urlencoded";
var logContent = reader.ReadToEnd();
if (stateInfo == 1)
logContent = "text=" + logContent;
var logData = Encoding.ASCII.GetBytes(logContent);
logRequest.ContentLength = logData.Length;
using (var logRequestStream = logRequest.GetRequestStream()) {
logRequestStream.Write(logData, 0, logData.Length);
}
// Get and format the response, which includes the link to the hastebin
var logResponse = (HttpWebResponse)logRequest.GetResponse();
var logResponseString = new StreamReader(logResponse.GetResponseStream()).ReadToEnd();
if (stateInfo == 0)
logResponseString = logResponseString.Split(':')[1].Replace("}", "").Replace("\"", "");
else
logResponseString = logResponseString.Replace("\t", "/");
logResponseString = stateInfo == 0 ? $"https://paste.mod.gg/{logResponseString}" : $"https://hatebin.com{logResponseString}";
// Send link to discord via a webhook
var discordRequest = (HttpWebRequest)WebRequest.Create(Telemetry.WebhookURL); // Note that the URL has been changed from previous commits, so don't waste your time trying to send NSFW to the webhook.
//discordRequest.Headers.Add("user-agent", "tConfig Wrapper?");
discordRequest.UserAgent = "tConfig Wrapper?";
discordRequest.Method = "POST";
discordRequest.ContentType = "application/json";
string serverOrClient = Main.dedServ ? "server" : "client";
var discordContent = "{\"content\": \"A new " + serverOrClient + " log has been uploaded! Link: " + logResponseString + "\"}";
var discordData = Encoding.ASCII.GetBytes(discordContent);
discordRequest.ContentLength = discordData.Length;
using (var discordRequestStream = discordRequest.GetRequestStream()) {
discordRequestStream.Write(discordData, 0, discordData.Length);
}
}
}
}
catch {
FailedToSendLogs = true;
if (FailedToSendLogs && stateInfo == 0) {
FailedToSendLogs = false;
UploadLogs(1);
}
if (FailedToSendLogs && stateInfo == 1)
ModContent.GetInstance<tConfigWrapper>().Logger.Debug("Failed to upload logs with both hastebin and pastebin!");
}
}
}
}