forked from hantabaru1014/ResoniteScreenshotExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageImporterPatch.cs
More file actions
56 lines (50 loc) · 2.04 KB
/
ImageImporterPatch.cs
File metadata and controls
56 lines (50 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using ResoniteModLoader;
using HarmonyLib;
using FrooxEngine;
using System.Reflection;
using System.Collections.Generic;
using System.Reflection.Emit;
using System;
namespace ResoniteScreenshotExtensions;
public partial class ResoniteScreenshotExtensions : ResoniteMod
{
[HarmonyPatch]
class ImageImporter_Patch
{
static Type TargetClass()
{
return AccessTools.FirstInner(typeof(ImageImporter), t => t.Name.Contains(nameof(ImageImporter.ImportImage)));
}
static MethodBase TargetMethod()
{
return AccessTools.Method(TargetClass(), "MoveNext");
}
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
var targetClass = TargetClass();
var setupMethod = AccessTools.Method(typeof(ImageImporter), nameof(ImageImporter.SetupTextureProxyComponents));
var loadThis = new CodeInstruction(OpCodes.Ldarg_0);
return new CodeMatcher(instructions, generator)
.SearchForward(code => code.Calls(setupMethod))
.Advance(1)
.Insert(
loadThis,
CodeInstruction.LoadField(targetClass, "targetSlot"),
loadThis,
CodeInstruction.LoadField(targetClass, "item"),
loadThis,
CodeInstruction.LoadField(targetClass, "setupScreenshotMetadata"),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ImageImporter_Patch), nameof(LoadMetadata)))
)
.InstructionEnumeration();
}
static void LoadMetadata(Slot targetSlot, ImportItem item, bool skip)
{
if (skip || !(_config?.GetValue(LoadPhotoMetadataFromFileKey) ?? false) || string.IsNullOrEmpty(item.filePath)) return;
if (XmpMetadata.TryLoadPhotoMetadata(item.filePath, targetSlot))
{
Msg("Loaded PhotoMetadata from XMP");
}
}
}
}