mirror of
https://github.com/sinai-dev/UnityExplorer.git
synced 2025-06-15 22:07:48 +08:00
Merge 56601ac0bdf92b1e3ce2d04f0c33f26d731a4604 into 1e1fb0e27bff9ab0212b4e61ef1ecb38a502b290
This commit is contained in:
commit
8016ad334a
@ -1,4 +1,6 @@
|
|||||||
using HarmonyLib;
|
using System.Linq;
|
||||||
|
using System.Xml.Serialization;
|
||||||
|
using HarmonyLib;
|
||||||
using UnityExplorer.CSConsole;
|
using UnityExplorer.CSConsole;
|
||||||
using UnityExplorer.Runtime;
|
using UnityExplorer.Runtime;
|
||||||
using UnityExplorer.UI.Panels;
|
using UnityExplorer.UI.Panels;
|
||||||
@ -225,12 +227,96 @@ namespace UnityExplorer.Hooks
|
|||||||
CurrentEditedHook.Patch();
|
CurrentEditedHook.Patch();
|
||||||
|
|
||||||
CurrentEditedHook.PatchSourceCode = input;
|
CurrentEditedHook.PatchSourceCode = input;
|
||||||
|
SaveHooks(CurrentEditedHook);
|
||||||
CurrentEditedHook = null;
|
CurrentEditedHook = null;
|
||||||
HookManagerPanel.Instance.SetPage(HookManagerPanel.Pages.ClassMethodSelector);
|
HookManagerPanel.Instance.SetPage(HookManagerPanel.Pages.ClassMethodSelector);
|
||||||
}
|
}
|
||||||
|
|
||||||
HookList.HooksScrollPool.Refresh(true, false);
|
HookList.HooksScrollPool.Refresh(true, false);
|
||||||
}
|
}
|
||||||
|
// Persistent hooks
|
||||||
|
public struct HookData
|
||||||
|
{
|
||||||
|
public string Description;
|
||||||
|
public string SourceCode;
|
||||||
|
public string ReflectedType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SaveHooks(HookInstance hook)
|
||||||
|
{
|
||||||
|
HookData data = new HookData();
|
||||||
|
data.Description = hook.TargetMethod.FullDescription();
|
||||||
|
data.ReflectedType = hook.TargetMethod.ReflectedType.ToString();
|
||||||
|
data.SourceCode = hook.PatchSourceCode;
|
||||||
|
string filename = data.Description.GetHashCode().ToString("X8") + ".txt";
|
||||||
|
string folderpath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "UnityExplorerHarmonyHooks");
|
||||||
|
Directory.CreateDirectory(folderpath);
|
||||||
|
string fpath = Path.Combine(folderpath, filename);
|
||||||
|
XmlSerializer xs = new XmlSerializer(typeof(HookData));
|
||||||
|
TextWriter tw = new StreamWriter(fpath);
|
||||||
|
xs.Serialize(tw, data);
|
||||||
|
tw.Close();
|
||||||
|
ExplorerCore.Log("Save hook: " + data.Description + " to: " + filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void LoadSavedHooks()
|
||||||
|
{
|
||||||
|
string folderpath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "UnityExplorerHarmonyHooks");
|
||||||
|
Directory.CreateDirectory(folderpath);
|
||||||
|
ExplorerCore.Log("Hooks path: " + folderpath);
|
||||||
|
XmlSerializer xs = new XmlSerializer(typeof(HookData));
|
||||||
|
xs.UnknownNode += new XmlNodeEventHandler(xs_UnknownNode);
|
||||||
|
xs.UnknownAttribute += new XmlAttributeEventHandler(xs_UnknownAttribute);
|
||||||
|
DirectoryInfo di = new DirectoryInfo(folderpath);
|
||||||
|
FileInfo[] fs = di.GetFiles("*.txt");
|
||||||
|
foreach (FileInfo fi in fs)
|
||||||
|
{
|
||||||
|
ExplorerCore.Log("Load: " + fi.Name);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
FileStream fileStream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
|
||||||
|
HookData hookData;
|
||||||
|
hookData = (HookData)xs.Deserialize(fileStream);
|
||||||
|
ExplorerCore.Log("+> Hook: " + hookData.Description);
|
||||||
|
Type type = ReflectionUtility.GetTypeByName(hookData.ReflectedType);
|
||||||
|
IEnumerable<MethodInfo> ms = type.GetMethods().Where(
|
||||||
|
mi => mi.FullDescription() == hookData.Description
|
||||||
|
);
|
||||||
|
|
||||||
|
MethodInfo method = ms.First();
|
||||||
|
HookManagerPanel.Instance.SetPage(HookManagerPanel.Pages.ClassMethodSelector);
|
||||||
|
|
||||||
|
if (HookList.hookedSignatures.Contains(hookData.Description))
|
||||||
|
{
|
||||||
|
ExplorerCore.LogWarning($"Method is already hooked!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
HookInstance hook = new(method, hookData.SourceCode);
|
||||||
|
HookList.hookedSignatures.Add(hookData.Description);
|
||||||
|
HookList.currentHooks.Add(hookData.Description, hook);
|
||||||
|
|
||||||
|
AddHooksScrollPool.Refresh(true, false);
|
||||||
|
HookList.HooksScrollPool.Refresh(true, false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ExplorerCore.LogError("Exception when load: " + fi.Name + " ---------\n" + ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void xs_UnknownNode(object sender, XmlNodeEventArgs e)
|
||||||
|
{
|
||||||
|
ExplorerCore.LogWarning("Unknown Node in hooks:" + e.Name + "\t" + e.Text);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void xs_UnknownAttribute(object sender, XmlAttributeEventArgs e)
|
||||||
|
{
|
||||||
|
System.Xml.XmlAttribute attr = e.Attr;
|
||||||
|
ExplorerCore.LogWarning("Unknown attribute in hooks:" + attr.Name + "='" + attr.Value + "'");
|
||||||
|
}
|
||||||
|
|
||||||
// UI Construction
|
// UI Construction
|
||||||
|
|
||||||
|
@ -46,6 +46,19 @@ namespace UnityExplorer.Hooks
|
|||||||
if (CompileAndGenerateProcessor(PatchSourceCode))
|
if (CompileAndGenerateProcessor(PatchSourceCode))
|
||||||
Patch();
|
Patch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HookInstance(MethodInfo targetMethod, string code)
|
||||||
|
{
|
||||||
|
this.TargetMethod = targetMethod;
|
||||||
|
this.signature = TargetMethod.FullDescription();
|
||||||
|
|
||||||
|
PatchSourceCode = code;
|
||||||
|
|
||||||
|
if (CompileAndGenerateProcessor(PatchSourceCode))
|
||||||
|
{
|
||||||
|
Patch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Evaluator.source_file
|
// Evaluator.source_file
|
||||||
private static readonly FieldInfo fi_sourceFile = AccessTools.Field(typeof(Evaluator), "source_file");
|
private static readonly FieldInfo fi_sourceFile = AccessTools.Field(typeof(Evaluator), "source_file");
|
||||||
|
@ -97,6 +97,8 @@ namespace UnityExplorer.UI.Panels
|
|||||||
|
|
||||||
genericArgsHandler.ConstructUI(ContentRoot);
|
genericArgsHandler.ConstructUI(ContentRoot);
|
||||||
genericArgsHandler.UIRoot.SetActive(false);
|
genericArgsHandler.UIRoot.SetActive(false);
|
||||||
|
|
||||||
|
hookCreator.LoadSavedHooks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user