116 lines
4.0 KiB
C#
116 lines
4.0 KiB
C#
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<Type>() {
|
|
GameManager.server.GetType(),
|
|
typeof(BasePlayer),
|
|
typeof(BaseEntity),
|
|
typeof(Item),
|
|
typeof(RustPlugin)
|
|
};
|
|
|
|
var data = new Dictionary<string, object>();
|
|
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<object>();
|
|
// 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<string, object> data) {
|
|
if (type == null) return;
|
|
if (data.ContainsKey(type.FullName)) return;
|
|
|
|
this.ExtractTypeData(type.BaseType, data);
|
|
|
|
var result = new Dictionary<string, object>() {
|
|
{ "BaseType", type.BaseType != null ? type.BaseType.FullName : "None" },
|
|
{ "Fields", new List<string>() },
|
|
{ "Properties", new List<string>() },
|
|
{ "Methods", new List<string>() }
|
|
};
|
|
|
|
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<string>) result["Fields"]).Add(field.Name);
|
|
}
|
|
|
|
foreach (var property in type.GetProperties(flags)) {
|
|
((List<string>) result["Properties"]).Add(property.Name);
|
|
}
|
|
|
|
foreach (var method in type.GetMethods(flags)) {
|
|
((List<string>) 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)
|
|
);
|
|
}
|
|
}
|
|
}
|