Skip to content

Commit

Permalink
[VE] Fix Screencap mode not working in KKS (#20)
Browse files Browse the repository at this point in the history
Fixes #19
  • Loading branch information
ManlyMarco committed Jun 4, 2022
1 parent ce9f251 commit d5e286c
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 153 deletions.
4 changes: 3 additions & 1 deletion ToolBox/HarmonyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public static IEnumerable<CodeInstruction> ReplaceCodePattern(IEnumerable<CodeIn
for (int k = 0; k < replacement.replacer.Length; k++)
{
int finalIndex = i + k;
codeInstructions[finalIndex] = new CodeInstruction(replacement.replacer[k]) { labels = new List<Label>(codeInstructions[finalIndex].labels) };
//codeInstructions[finalIndex] = new CodeInstruction(replacement.replacer[k]) { labels = new List<Label>(codeInstructions[finalIndex].labels) };
codeInstructions[finalIndex].opcode = replacement.replacer[k].opcode;
codeInstructions[finalIndex].operand= replacement.replacer[k].operand;
}
i += replacement.replacer.Length;
}
Expand Down
8 changes: 6 additions & 2 deletions VideoExport.AI/VideoExport.AI.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -39,6 +39,10 @@
<HintPath>..\packages\IllusionLibs.BepInEx.Harmony.2.5.4\lib\net35\0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AI_Screencap, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ScreenshotManager.AIGirl.18.2.0\lib\net46\AI_Screencap.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IllusionLibs.AIGirl.Assembly-CSharp.2020.5.29.4\lib\net46\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
Expand Down Expand Up @@ -130,4 +134,4 @@
<Import Project="..\packages\IllusionLibs.AIGirl.UnityEngine.AnimationModule.2018.2.21.4\build\IllusionLibs.AIGirl.UnityEngine.AnimationModule.targets" Condition="Exists('..\packages\IllusionLibs.AIGirl.UnityEngine.AnimationModule.2018.2.21.4\build\IllusionLibs.AIGirl.UnityEngine.AnimationModule.targets')" />
<Import Project="..\packages\IllusionLibs.AIGirl.UnityEngine.IMGUIModule.2018.2.21.4\build\IllusionLibs.AIGirl.UnityEngine.IMGUIModule.targets" Condition="Exists('..\packages\IllusionLibs.AIGirl.UnityEngine.IMGUIModule.2018.2.21.4\build\IllusionLibs.AIGirl.UnityEngine.IMGUIModule.targets')" />
<Import Project="..\packages\IllusionLibs.AIGirl.UnityEngine.ImageConversionModule.2018.2.21.4\build\IllusionLibs.AIGirl.UnityEngine.ImageConversionModule.targets" Condition="Exists('..\packages\IllusionLibs.AIGirl.UnityEngine.ImageConversionModule.2018.2.21.4\build\IllusionLibs.AIGirl.UnityEngine.ImageConversionModule.targets')" />
</Project>
</Project>
1 change: 1 addition & 0 deletions VideoExport.AI/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<package id="IllusionLibs.BepInEx" version="5.4.15" targetFramework="net46" />
<package id="IllusionLibs.BepInEx.Harmony" version="2.5.4" targetFramework="net46" />
<package id="Microsoft.Unity.Analyzers" version="1.11.2" targetFramework="net46" developmentDependency="true" />
<package id="ScreenshotManager.AIGirl" version="18.2.0" targetFramework="net46" developmentDependency="true" />
</packages>
Original file line number Diff line number Diff line change
@@ -1,74 +1,4 @@
#if HONEYSELECT
using System;
using System.Reflection;
using Harmony;
using IllusionPlugin;
using UnityEngine;

namespace VideoExport.ScreenshotPlugins
{
public class Screencap : IScreenshotPlugin
{
private delegate byte[] CaptureFunctionDelegate();

private CaptureFunctionDelegate _captureFunction;
private Vector2 _currentSize;

public string name { get { return "Screencap"; } }
public Vector2 currentSize { get { return this._currentSize; } }
public bool transparency { get { return false; } }
public string extension { get { return "png"; } }
public byte bitDepth { get { return 8; } }

public bool Init(HarmonyInstance harmony)
{
Type screencapType = Type.GetType("ScreencapMB,Screencap");
if (screencapType == null)
return false;
object plugin = GameObject.FindObjectOfType(screencapType);
if (plugin == null)
return false;
MethodInfo captureOpaque = screencapType.GetMethod("CaptureOpaque", BindingFlags.NonPublic | BindingFlags.Instance);
if (captureOpaque == null)
{
VideoExport.Logger.LogError("VideoExport: Screencap was found but seems out of date, please update it.");
return false;
}
this._captureFunction = (CaptureFunctionDelegate)Delegate.CreateDelegate(typeof(CaptureFunctionDelegate), plugin, captureOpaque);

int downscalingRate = ModPrefs.GetInt("Screencap", "DownscalingRate", 1, true);
this._currentSize = new Vector2(ModPrefs.GetInt("Screencap", "Width", 1280, true) * downscalingRate, ModPrefs.GetInt("Screencap", "Height", 720, true) * downscalingRate);
return true;
}

public void UpdateLanguage()
{
}

public void OnStartRecording()
{
}

public byte[] Capture(string saveTo)
{
return this._captureFunction();
}

public void OnEndRecording()
{
}

public void DisplayParams()
{
}

public void SaveParams()
{
}
}
}

#elif KOIKATSU
#if KOIKATSU
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -84,7 +14,7 @@ public void SaveParams()

namespace VideoExport.ScreenshotPlugins
{
public class Screencap : IScreenshotPlugin
public class ScreencapPlugin : IScreenshotPlugin
{
#region Private Types
private enum CaptureType
Expand Down Expand Up @@ -122,50 +52,50 @@ public Vector2 currentSize
#endregion

#region Private Variables
private static readonly HarmonyExtensions.Replacement[] _replacements =
private static readonly HarmonyExtensions.Replacement[] _replacements =
{
new HarmonyExtensions.Replacement()
new HarmonyExtensions.Replacement()
{
pattern = new[]
pattern = new[]
{
new CodeInstruction(OpCodes.Call, typeof(File).GetMethod("WriteAllBytes", BindingFlags.Public | BindingFlags.Static)),
new CodeInstruction(OpCodes.Call, typeof(File).GetMethod(nameof(File.WriteAllBytes), BindingFlags.Public | BindingFlags.Static)),
},
replacer = new[]
replacer = new[]
{
new CodeInstruction(OpCodes.Call, typeof(Screencap).GetMethod(nameof(WriteAllBytesReplacement), BindingFlags.NonPublic | BindingFlags.Static)),
new CodeInstruction(OpCodes.Call, typeof(ScreencapPlugin).GetMethod(nameof(WriteAllBytesReplacement), BindingFlags.NonPublic | BindingFlags.Static)),
}
},
new HarmonyExtensions.Replacement()
new HarmonyExtensions.Replacement()
{
pattern = new[]
pattern = new[]
{
new CodeInstruction(OpCodes.Callvirt, typeof(ManualLogSource).GetMethod("Log", BindingFlags.Instance | BindingFlags.Public)),
new CodeInstruction(OpCodes.Callvirt, typeof(ManualLogSource).GetMethod(nameof(ManualLogSource.Log), BindingFlags.Instance | BindingFlags.Public)),
},
replacer = new[]
replacer = new[]
{
new CodeInstruction(OpCodes.Call, typeof(Screencap).GetMethod(nameof(LogReplacement), BindingFlags.NonPublic | BindingFlags.Static)),
new CodeInstruction(OpCodes.Call, typeof(ScreencapPlugin).GetMethod(nameof(LogReplacement), BindingFlags.NonPublic | BindingFlags.Static)),
}
},
new HarmonyExtensions.Replacement()
new HarmonyExtensions.Replacement()
{
pattern = new[]
pattern = new[]
{
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Utils.Sound), "Play", new[]{typeof(SystemSE)})),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Utils.Sound), nameof(Utils.Sound.Play), new[]{typeof(SystemSE)})),
},
replacer = new[]
replacer = new[]
{
new CodeInstruction(OpCodes.Call, typeof(Screencap).GetMethod(nameof(PlayReplacement), BindingFlags.NonPublic | BindingFlags.Static)),
new CodeInstruction(OpCodes.Call, typeof(ScreencapPlugin).GetMethod(nameof(PlayReplacement), BindingFlags.NonPublic | BindingFlags.Static)),
}
},
new HarmonyExtensions.Replacement()
new HarmonyExtensions.Replacement()
{
pattern = new[]
pattern = new[]
{
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(GL), "Clear", new[]{typeof(bool), typeof(bool), typeof(Color)})),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(GL), nameof(GL.Clear), new[]{typeof(bool), typeof(bool), typeof(Color)})),
},
replacer = new[]
replacer = new[]
{
new CodeInstruction(OpCodes.Call, typeof(Screencap).GetMethod(nameof(ClearReplacement), BindingFlags.NonPublic | BindingFlags.Static)),
new CodeInstruction(OpCodes.Call, typeof(ScreencapPlugin).GetMethod(nameof(ClearReplacement), BindingFlags.NonPublic | BindingFlags.Static)),
}
}
};
Expand All @@ -190,34 +120,31 @@ public Vector2 currentSize
#region Public Methods
public bool Init(Harmony harmony)
{
Type screencapType = Type.GetType("Screencap.ScreenshotManager,Screencap");
if (screencapType == null)
return false;
object screenshotManager = GameObject.FindObjectOfType(screencapType);
var screenshotManager = Screencap.ScreenshotManager.Instance;
if (screenshotManager == null)
return false;
this._captureType = (CaptureType)VideoExport._configFile.AddInt("Screencap_captureType", 0, true);
this._in3d = VideoExport._configFile.AddBool("Screencap_in3d", false, true);
this._resolutionX = (ConfigEntry<int>)screencapType.GetPrivateProperty("ResolutionX");
this._resolutionY = (ConfigEntry<int>)screencapType.GetPrivateProperty("ResolutionY");
this._resolution360 = (ConfigEntry<int>)screencapType.GetPrivateProperty("Resolution360");
this._imageSeparationOffset = (ConfigEntry<float>)screencapType.GetPrivateProperty("ImageSeparationOffset");
this._downscalingRate = (ConfigEntry<int>)screencapType.GetPrivateProperty("DownscalingRate");
this._captureAlpha = (ConfigEntry<bool>)screencapType.GetPrivateProperty("CaptureAlpha");
this._useJpg = (ConfigEntry<bool>)screencapType.GetPrivateProperty("UseJpg");
MethodInfo takeCharScreenshot = screencapType.GetMethod("TakeCharScreenshot", BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo take360Screenshot = screencapType.GetMethod("Take360Screenshot", BindingFlags.NonPublic | BindingFlags.Instance);
if (takeCharScreenshot == null || take360Screenshot == null)
return false;
this._takeCharScreenshot = (Func<bool, IEnumerator>)Delegate.CreateDelegate(typeof(Func<bool, IEnumerator>), screenshotManager, takeCharScreenshot);
this._take360Screenshot = (Func<bool, IEnumerator>)Delegate.CreateDelegate(typeof(Func<bool, IEnumerator>), screenshotManager, take360Screenshot);
if (this._take360Screenshot == null || this._takeCharScreenshot == null)
this._resolutionX = Screencap.ScreenshotManager.ResolutionX;
this._resolutionY = Screencap.ScreenshotManager.ResolutionY;
this._resolution360 = Screencap.ScreenshotManager.Resolution360;
this._imageSeparationOffset = Screencap.ScreenshotManager.ImageSeparationOffset;
this._downscalingRate = Screencap.ScreenshotManager.DownscalingRate;
this._captureAlpha = Screencap.ScreenshotManager.CaptureAlpha;
this._useJpg = Screencap.ScreenshotManager.UseJpg;

var tv = Traverse.Create(screenshotManager);
var takeCharScreenshot = tv.Method("TakeCharScreenshot", new[] { typeof(bool) });
var take360Screenshot = tv.Method("Take360Screenshot", new[] { typeof(bool) });
if (!takeCharScreenshot.MethodExists() || !take360Screenshot.MethodExists())
return false;
this._takeCharScreenshot = in3D => takeCharScreenshot.GetValue<IEnumerator>(in3D);
this._take360Screenshot = in3D => take360Screenshot.GetValue<IEnumerator>(in3D);
try
{
harmony.Patch(screencapType.GetCoroutineMethod("TakeCharScreenshot"), transpiler: new HarmonyMethod(typeof(Screencap).GetMethod(nameof(GeneralTranspiler), BindingFlags.Static | BindingFlags.NonPublic)));
harmony.Patch(screencapType.GetCoroutineMethod("Take360Screenshot"), transpiler: new HarmonyMethod(typeof(Screencap).GetMethod(nameof(GeneralTranspiler), BindingFlags.Static | BindingFlags.NonPublic)));
harmony.Patch(Type.GetType("alphaShot.AlphaShot2,Screencap").GetMethod("PerformCapture"), transpiler: new HarmonyMethod(typeof(Screencap).GetMethod(nameof(GeneralTranspiler), BindingFlags.Static | BindingFlags.NonPublic)));
harmony.Patch(typeof(Screencap.ScreenshotManager).GetCoroutineMethod("TakeCharScreenshot"), transpiler: new HarmonyMethod(typeof(ScreencapPlugin).GetMethod(nameof(GeneralTranspiler), BindingFlags.Static | BindingFlags.NonPublic)));
harmony.Patch(typeof(Screencap.ScreenshotManager).GetCoroutineMethod("Take360Screenshot"), transpiler: new HarmonyMethod(typeof(ScreencapPlugin).GetMethod(nameof(GeneralTranspiler), BindingFlags.Static | BindingFlags.NonPublic)));
harmony.Patch(typeof(alphaShot.AlphaShot2).GetMethod("PerformCapture"), transpiler: new HarmonyMethod(typeof(ScreencapPlugin).GetMethod(nameof(GeneralTranspiler), BindingFlags.Static | BindingFlags.NonPublic)));
}
catch (Exception e)
{
Expand Down Expand Up @@ -299,11 +226,20 @@ private static void LogReplacement(ManualLogSource self, BepInEx.Logging.LogLeve
self.Log(level, obj);
}

#if !SUNSHINE
private static void PlayReplacement(SystemSE se)
{
if (!_videoExportCapture)
Utils.Sound.Play(se);
}
#else
private static AudioSource PlayReplacement(SystemSE se)
{
if (!_videoExportCapture)
return Utils.Sound.Play(se);
return null;
}
#endif

private static void ClearReplacement(bool clearDepth, bool clearColor, Color backgroundColor)
{
Expand All @@ -313,6 +249,7 @@ private static void ClearReplacement(bool clearDepth, bool clearColor, Color bac

private static IEnumerable<CodeInstruction> GeneralTranspiler(IEnumerable<CodeInstruction> instructions)
{
//return instructions;
return HarmonyExtensions.ReplaceCodePattern(instructions, _replacements);
}
#endregion
Expand All @@ -336,17 +273,17 @@ private static IEnumerable<CodeInstruction> GeneralTranspiler(IEnumerable<CodeIn

namespace VideoExport.ScreenshotPlugins
{
public class Screencap : IScreenshotPlugin
public class ScreencapPlugin : IScreenshotPlugin
{
#region Accessors
#region Accessors
public string name { get { return "Screenshot Manager"; } }
public Vector2 currentSize { get { return new Vector2(this._captureWidth.Value, this._captureHeight.Value); } }
public bool transparency { get { return this._alpha.Value; } }
public string extension { get { return "png"; } }
public byte bitDepth { get { return 8; } }
#endregion
#endregion

#region Private Variables
#region Private Variables
private static bool _videoExportCapture = false;
private static byte[] _imageBytes;
private static Texture2D _texture = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false, true);
Expand All @@ -356,19 +293,13 @@ public class Screencap : IScreenshotPlugin
private ConfigEntry<int> _captureWidth;
private ConfigEntry<int> _captureHeight;
private ConfigEntry<bool> _alpha;
#endregion
#endregion

#region Public Methods
#region Public Methods
public bool Init(Harmony harmony)
{
#if AISHOUJO
Type screencapType = Type.GetType("Screencap.ScreenshotManager,AI_Screencap");
#elif HONEYSELECT2
Type screencapType = Type.GetType("Screencap.ScreenshotManager,HS2_Screencap");
#endif
if (screencapType == null)
return false;
object screenshotManager = GameObject.FindObjectOfType(screencapType);
var screencapType = typeof(Screencap.ScreenshotManager);
var screenshotManager = GameObject.FindObjectOfType(screencapType);
if (screenshotManager == null)
return false;
this._captureWidth = (ConfigEntry<int>)screenshotManager.GetPrivateProperty("CaptureWidth");
Expand All @@ -383,7 +314,7 @@ public bool Init(Harmony harmony)
return false;
try
{
harmony.Patch(screencapType.GetMethod("WriteTex", BindingFlags.NonPublic | BindingFlags.Instance), new HarmonyMethod(typeof(Screencap).GetMethod(nameof(WriteTex_Prefix), BindingFlags.Static | BindingFlags.NonPublic)));
harmony.Patch(screencapType.GetMethod("WriteTex", BindingFlags.NonPublic | BindingFlags.Instance), new HarmonyMethod(typeof(ScreencapPlugin).GetMethod(nameof(WriteTex_Prefix), BindingFlags.Static | BindingFlags.NonPublic)));
}
catch (Exception e)
{
Expand Down Expand Up @@ -416,9 +347,9 @@ public void DisplayParams()
public void SaveParams()
{
}
#endregion
#endregion

#region Private Methods
#region Private Methods
private static bool WriteTex_Prefix(RenderTexture rt, bool alpha, ref IEnumerator __result)
{
if (_videoExportCapture)
Expand All @@ -443,7 +374,7 @@ private static IEnumerator RoutineReplacement()
{
yield break;
}
#endregion
#endregion
}
}
#endif

0 comments on commit d5e286c

Please sign in to comment.