using Newtonsoft.Json; using Oxide.Core; using Oxide.Core.Plugins; using System.Collections.Generic; using System; using System.IO; using System.Reflection; using UnityEngine; namespace Oxide.Plugins { [Info("TungstonsDumper", "TungstonMiner", "0.1.0")] [Description("Add commands to dump data helpful in writing mods.")] public class TungstonsDumper : RustPlugin { // Constants /////////////////////////////////////////////////////////////////////////////// private const string PREFAB_LOG = "tungstonsdumper-prefabs.json"; private const string TYPE_LOG = "tungstonsdumper-types.json"; // Hooks /////////////////////////////////////////////////////////////////////////////////// void OnServerInitialized() { Puts("Tungston's Dumper is active"); } // Console Commands //////////////////////////////////////////////////////////////////////// [ConsoleCommand("dump.types")] private void DumpTypes(ConsoleSystem.Arg arg) { if (arg.Connection.authLevel < 2) { arg.ReplyWith("You must be an admin to run this command"); return; } var types = new List() { GameManager.server.GetType(), typeof(BasePlayer), typeof(BaseEntity), typeof(Item), typeof(RustPlugin) }; var data = new Dictionary(); foreach (var type in types) { this.ExtractTypeData(type, data); } this.WriteData(data, TYPE_LOG); arg.ReplyWith($"Dumped type data to {TYPE_LOG}"); } // [ConsoleCommand("dump.prefabs")] // private void DumpPrefabs(ConsoleSystem.Arg arg) { // if (arg.Connection.authLevel < 2) { // arg.ReplyWith("You must be an admin to run this command"); // return; // } // // var result = new List(); // foreach (var (id, attributeSet) in PrefabAttribute.server.prefabs) { // result.Add(new { // name = attributeSet.name // }); // } // // this.WriteData(result, PREFAB_LOG); // arg.ReplyWith($"Dumped prefabs to {PREFAB_LOG}"); // } // Helper Methods ////////////////////////////////////////////////////////////////////////// private void ExtractTypeData(Type type, Dictionary data) { if (type == null) return; if (data.ContainsKey(type.FullName)) return; this.ExtractTypeData(type.BaseType, data); var result = new Dictionary() { { "BaseType", type.BaseType != null ? type.BaseType.FullName : "None" }, { "Fields", new List() }, { "Properties", new List() }, { "Methods", new List() } }; var flags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly; foreach (var field in type.GetFields(flags)) { ((List) result["Fields"]).Add(field.Name); } foreach (var property in type.GetProperties(flags)) { ((List) result["Properties"]).Add(property.Name); } foreach (var method in type.GetMethods(flags)) { ((List) result["Methods"]).Add(method.Name); } data[type.FullName] = result; } private void WriteData(object obj, string fileName) { File.WriteAllText( Path.Combine(Interface.Oxide.LogDirectory, fileName), JsonConvert.SerializeObject(obj, Formatting.Indented) ); } } }