using System; using System.Linq; using Oxide.Core.Plugins; using System.Collections.Generic; using UnityEngine; using Newtonsoft.Json; using Oxide.Core.Libraries; // Slightly prettier discord // Catch for incorrect version formats. namespace Oxide.Plugins { [Info("UpdatesChecker", "Steenamaroo", "1.1.0", ResourceId = 36)] class UpdatesChecker : RustPlugin { Root a; List PlugList = new List(); public class Root { public List file { get; set; } } public class File { public string file_id { get; set; } public string file_name { get; set; } public object file_image { get; set; } public string file_version { get; set; } public string file_file_1 { get; set; } } void OnServerInitialized() { configData.CheckIntervalMinutes = Mathf.Max(10, configData.CheckIntervalMinutes); GetFiles(); timer.Repeat(configData.CheckIntervalMinutes * 60, 0, () => GetFiles()); } void GetFiles() { PlugList = plugins.GetAll().Where(x => !x.IsCorePlugin).OrderBy(x => x.Name).ToList(); webrequest.Enqueue("https://codefling.com/capi/category-2?do=apiCall", null, GetPlugin, this); } List errors = new List(); class test { public string result; } private void GetPlugin(int code, string response) { if (response != null && code == 200) { response = response.Replace("{}", "\"\""); a = JsonConvert.DeserializeObject(response, new JsonSerializerSettings { Error = (se, ev) => ev.ErrorContext.Handled = true }); List Updates = new List(); foreach (var entry in a.file.ToList()) { if (entry.file_file_1 == null) continue; entry.file_file_1 = entry.file_file_1.Replace(".cs", ""); } foreach (var loaded in PlugList.Where(x => !configData.Ignore.Contains(x.Name))) { if (!configData.Authors.ContainsKey(loaded.Author)) configData.Authors.Add(loaded.Author, true); foreach (var entry in a.file.Where(entry => entry.file_file_1 == loaded.Name)) if (S2V(entry.file_version) > loaded.Version && configData.Authors[loaded.Author]) Updates.Add(entry.file_file_1); } if (Updates.Count == 1) { PrintWarning($"Codefling has an update available for {Updates[0]}."); SendDiscordMessage($"{Updates[0]}", true); } else if (Updates.Count != 0) { PrintWarning("Codefling has updates for the following plugins."); string discordmsg = ""; for (int i = 0; i < Updates.Count; i++) { PrintWarning($"{i + 1} : {Updates[i]}"); discordmsg += $"\n{i + 1} : {Updates[i]}"; } SendDiscordMessage(discordmsg, false); } SaveConf(); } else PrintWarning("Unable to contact Codefling.com"); } private void OnPluginLoaded(Plugin plugin) { if (a == null) return; if (configData.Ignore.Contains(plugin.Name)) return; if (!configData.Authors.ContainsKey(plugin.Author)) configData.Authors.Add(plugin.Author, true); foreach (var entry in a.file.Where(entry => entry.file_file_1 == plugin.Name)) { if (S2V(entry.file_version) > plugin.Version && configData.Authors[plugin.Author]) PrintWarning($"Codefling has an update available for {entry.file_file_1}."); } } Core.VersionNumber S2V(string v) { string[] parts = v.Split('.'); if (parts.Length != 3) return new Core.VersionNumber(0, 0, 0); return new Core.VersionNumber(Convert.ToInt16(parts[0]), Convert.ToInt16(parts[1]), Convert.ToInt16(parts[2])); } void Init() => LoadConfigVariables(); private ConfigData configData; class ConfigData { public int CheckIntervalMinutes = 60; public string DiscordWebhookAddress = ""; public List Ignore = new List(); public Dictionary Authors = new Dictionary(); } private void LoadConfigVariables() { configData = Config.ReadObject(); SaveConf(); } protected override void LoadDefaultConfig() { Puts("Creating new config file."); configData = new ConfigData(); SaveConf(); } void SaveConf() { configData.Authors = configData.Authors.OrderBy(pair => pair.Key).ToDictionary(pair => pair.Key, pair => pair.Value); Config.WriteObject(configData, true); } public void SendDiscordMessage(string message, bool single) { if (!configData.DiscordWebhookAddress.Contains("discord.com/api/webhooks")) return; try { var embeds = new List(); embeds.Add(new Embed() { title = message, description = "https://www.codefling.com" }); Dictionary stuff = new Dictionary() { { "embeds", embeds }, { "avatar_url", "https://pbs.twimg.com/profile_images/1345024685541621761/sc75KlbU_400x400.jpg"}, { "content", single ? "Codefling has an update for" : "Codefling has updates for"}, { "username", "Codefling Updates Checker"} }; webrequest.Enqueue(configData.DiscordWebhookAddress, JsonConvert.SerializeObject(stuff), Callback, this, RequestMethod.POST, new Dictionary { ["Content-Type"] = "application/json" }); } catch { } } public class Embed { public string title; public string description; public string url; } public void Callback(int code, string response) { } public class FileParameter { public byte[] File { get; set; } public string FileName { get; set; } public string ContentType { get; set; } public FileParameter(byte[] file) : this(file, null) { } public FileParameter(byte[] file, string filename) : this(file, filename, null) { } public FileParameter(byte[] file, string filename, string contenttype) { File = file; FileName = filename; ContentType = contenttype; } } } }