Browse Source

Create Unity Object inspector ripper

master
NGnius 5 years ago
parent
commit
8bddf7ea5a
9 changed files with 414 additions and 2 deletions
  1. +63
    -0
      .gitattributes
  2. +9
    -2
      .gitignore
  3. +25
    -0
      Dissonance.sln
  4. +23
    -0
      Dissonance/Dissonance.csproj
  5. +22
    -0
      Dissonance/DissonancePatch.cs
  6. +64
    -0
      Dissonance/DissonancePlugin.cs
  7. +123
    -0
      Dissonance/Inspector/Instance.cs
  8. +85
    -0
      Dissonance/Inspector/ObjectInspector.cs
  9. BIN
      IllusionPlugin.dll

+ 63
- 0
.gitattributes View File

@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

+ 9
- 2
.gitignore View File

@@ -1,4 +1,3 @@
# ---> VisualStudio
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
@@ -21,6 +20,8 @@
[Rr]eleases/
x64/
x86/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
@@ -210,7 +211,7 @@ _pkginfo.txt
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
!?*.[Cc]ache/

# Others
ClientBin/
@@ -254,6 +255,7 @@ ServiceFabricBackup/
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- Backup*.rdl

# Microsoft Fakes
FakesAssemblies/
@@ -334,3 +336,8 @@ ASALocalRun/
# Local History for Visual Studio
.localhistory/

# BeatPulse healthcheck temp database
healthchecksdb

# references
ref

+ 25
- 0
Dissonance.sln View File

@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29411.108
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dissonance", "Dissonance\Dissonance.csproj", "{7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FD5A7D8-4F3E-426A-B07D-7DC70442A4DF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {72FB94D0-6C50-475B-81E0-C94C7D7A2A17}
EndGlobalSection
EndGlobal

+ 23
- 0
Dissonance/Dissonance.csproj View File

@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Lib.Harmony" Version="1.2.0.1" />
</ItemGroup>

<ItemGroup>
<Reference Include="IllusionPlugin">
<HintPath>..\IllusionPlugin.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\ref\Gamecraft_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>..\ref\Gamecraft_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
</Reference>
</ItemGroup>

</Project>

+ 22
- 0
Dissonance/DissonancePatch.cs View File

@@ -0,0 +1,22 @@
using System;
using System.Reflection;
using Harmony;
using UnityEngine;

namespace Dissonance
{
//[HarmonyPatch]
class DissonancePatch
{
static void Prefix()
{
Debug.Log("Prefix");
}

[HarmonyTargetMethod]
static MethodBase DissonanceMethod(HarmonyInstance instance)
{
throw new NotImplementedException();
}
}
}

+ 64
- 0
Dissonance/DissonancePlugin.cs View File

@@ -0,0 +1,64 @@
using System;
using IllusionPlugin;
using UnityEngine;
using Harmony;
using System.Reflection;

namespace Dissonance
{
public class DissonancePlugin : IllusionPlugin.IEnhancedPlugin
{
public static HarmonyInstance harmony { get; protected set; }

public string[] Filter { get; } = new string[] { "Gamecraft" };

public string Name { get; } = "Dissonance";

public string Version { get; } = "A";

public string HarmonyID { get; } = "org.git.exmods.modtainers.dissonance";

public void OnApplicationQuit()
{
harmony.UnpatchAll(HarmonyID);
Inspector.ObjectInspector.LogAll();
Debug.Log(Name + " shutdown complete");
}

public void OnApplicationStart()
{
if (harmony == null)
{
harmony = HarmonyInstance.Create(HarmonyID);
harmony.PatchAll(Assembly.GetExecutingAssembly());
}
Inspector.ObjectInspector.LogAll();
Debug.Log(Name + " start & patch complete");
}

public void OnFixedUpdate()
{
//throw new NotImplementedException();
}

public void OnLateUpdate()
{
//throw new NotImplementedException();
}

public void OnLevelWasInitialized(int level)
{
//throw new NotImplementedException();
}

public void OnLevelWasLoaded(int level)
{
//throw new NotImplementedException();
}

public void OnUpdate()
{
//throw new NotImplementedException();
}
}
}

+ 123
- 0
Dissonance/Inspector/Instance.cs View File

@@ -0,0 +1,123 @@
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace Dissonance.Inspector
{
public struct Instance
{
public string name;

public UnityEngine.Object obj;

public Type type;

public MethodBase[] methods;

public Dictionary<string, object> properties;

public UnityEngine.HideFlags hideFlags;

public Instance(UnityEngine.Object obj)
{
this.name = obj.name;
this.obj = obj;
this.type = obj.GetType();
this.methods = this.type.GetMethods();
this.properties = new Dictionary<string, object>();
foreach (PropertyInfo propInf in this.type.GetProperties())
{
if (propInf.CanRead && propInf.GetGetMethod().IsPublic)
{
this.properties.Add(propInf.Name, propInf.GetValue(obj));
}
}
this.hideFlags = obj.hideFlags;
}

public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("{\n");
if (type != null && type.FullName != null)
{
sb.Append($"\"type\": \"{type.FullName}\",\n");
}
if (name != null)
{
sb.Append($"\"name\": \"{name}\",\n");
}
if (obj != null)
{
sb.Append($"\"object\": \"{obj.ToString()}\",\n");
} else
{
sb.Append($"\"object\": null,\n");
}
sb.Append($"\"hideFlags\": \"{hideFlags}\",\n");
sb.Append("\"properties\": {\n");
int count = 1;
foreach (string key in properties.Keys)
{
if (properties[key] != null)
{
sb.Append($" \"{key}\": \"{properties[key].ToString()}\"");
} else
{
sb.Append($" \"{key}\": null");
}
if (count != properties.Keys.Count)
{
sb.Append(",\n");
}
count++;
}
sb.Append("\n},\n");
sb.Append("\"methods\": [\n");
count = 1;
foreach (MethodBase m in methods)
{
sb.Append($" \"{m.Name}");
if (m.ContainsGenericParameters)
{
sb.Append("<");
Type[] genArgs = m.GetGenericArguments();
int argCount = 1;
foreach (Type t in genArgs)
{
sb.Append(t.Name);
if (argCount != genArgs.Length)
{
sb.Append(",");
}
argCount++;
}
sb.Append(">");
}
sb.Append("(");
ParameterInfo[] parameters = m.GetParameters();
int paramCount = 1;
foreach(ParameterInfo p in parameters)
{
sb.Append($"{p.ParameterType.FullName} {p.Name}");
if (paramCount != parameters.Length)
{
sb.Append(", ");
}
paramCount++;
}
sb.Append(")\"");
if (count != methods.Length)
{
sb.Append(",\n");
}
count++;
}
sb.Append("\n]\n");
sb.Append("}");
return sb.ToString();
}
}
}

+ 85
- 0
Dissonance/Inspector/ObjectInspector.cs View File

@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using UnityEngine;

namespace Dissonance.Inspector
{
public class ObjectInspector
{
public static void Log<T>() where T : UnityEngine.Object
{
Instance[] instances = Inspect<T>();
string[] filelines = new string[instances.Length];
for (int i = 0; i < instances.Length; i++)
{
filelines[i] = instances[i].ToString() + ",";
}
Debug.Log("ObjectInspector Log dump\n["+string.Join(",\n", filelines)+"]");
}

public static void LogNames<T>() where T : UnityEngine.Object
{
Debug.Log(string.Join("\n", InspectNames<T>()));
}

public static void LogAll()
{
Log<UnityEngine.Object>();
}

public static void LogNamesAll()
{
LogNames<UnityEngine.Object>();
}

public static void Dump<T>(string filename) where T : UnityEngine.Object
{
Instance[] instances = Inspect<T>();
string[] filelines = new string[instances.Length];
for (int i = 0; i < instances.Length; i++)
{
filelines[i] = instances[i].ToString()+",";
}
File.WriteAllLines(filename, filelines);
}

public static void DumpNames<T>(string filename) where T : UnityEngine.Object
{
File.WriteAllLines(filename, InspectNames<T>());
}

public static void DumpAll(string filename)
{
Dump<UnityEngine.Object>(filename);
}

public static void DumpNamesAll(string filename)
{
DumpNames<UnityEngine.Object>(filename);
}

public static Instance[] Inspect<T>() where T : UnityEngine.Object
{
T[] objects = UnityEngine.Object.FindObjectsOfType<T>();
Instance[] instances = new Instance[objects.Length];
for (int i = 0; i < objects.Length; i++)
{
instances[i] = new Instance(objects[i]);
}
return instances;
}

public static string[] InspectNames<T>() where T : UnityEngine.Object
{
T[] objects = UnityEngine.Object.FindObjectsOfType<T>();
string[] names = new string[objects.Length];
for(int i = 0; i < objects.Length; i++)
{
names[i] = objects[i].name;
}
return names;
}
}
}

BIN
IllusionPlugin.dll View File


Loading…
Cancel
Save