Setup for TileExtension

Moved some code to different files
This commit is contained in:
LadyAliceMargatroid 2025-02-04 23:13:24 -08:00
parent 16d1c2d056
commit 4af194e0f4
11 changed files with 262 additions and 234 deletions

View File

@ -0,0 +1,47 @@
using DunGen;
using DunGenPlus.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DunGenPlus.Collections {
internal class TileExtenderProxy {
public TileProxy TileProxy { get; internal set; }
public TileExtender PrefabTileExtender { get; internal set; }
public List<DoorwayProxy> Entrances { get; internal set; }
public List<DoorwayProxy> Exits { get; internal set; }
public TileExtenderProxy(TileExtenderProxy existingTileExtenderProxy) {
TileProxy = existingTileExtenderProxy.TileProxy;
PrefabTileExtender = existingTileExtenderProxy.PrefabTileExtender;
Entrances = new List<DoorwayProxy>();
Exits = new List<DoorwayProxy>();
foreach(var existingDoorway in TileProxy.doorways) {
if (existingTileExtenderProxy.Entrances.Contains(existingDoorway)) Entrances.Add(existingDoorway);
if (existingTileExtenderProxy.Exits.Contains(existingDoorway)) Exits.Add(existingDoorway);
}
}
public TileExtenderProxy(TileProxy tileProxy) {
TileProxy = tileProxy;
PrefabTileExtender = tileProxy.Prefab.GetComponent<TileExtender>();
Entrances = new List<DoorwayProxy>();
Exits = new List<DoorwayProxy>();
if (PrefabTileExtender == null) {
if (tileProxy.Entrance != null) Entrances.Add(tileProxy.Entrance);
if (tileProxy.Exit != null) Exits.Add(tileProxy.Exit);
return;
}
foreach(var proxyDoorway in tileProxy.doorways) {
if (PrefabTileExtender.entrances.Contains(proxyDoorway.DoorwayComponent)) Entrances.Add(proxyDoorway);
if (PrefabTileExtender.exits.Contains(proxyDoorway.DoorwayComponent)) Exits.Add(proxyDoorway);
}
}
}
}

View File

@ -0,0 +1,18 @@
using DunGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DunGenPlus.Components {
public class TileExtender : MonoBehaviour {
public List<Doorway> entrances = new List<Doorway>();
public List<Doorway> exits = new List<Doorway>();
public List<Doorway> overlappingDoorways = new List<Doorway>();
}
}

View File

@ -145,6 +145,7 @@
<Compile Include="Collections\LocalGlobalPropSettings.cs" />
<Compile Include="Collections\NodeArchetype.cs" />
<Compile Include="Collections\NullObject.cs" />
<Compile Include="Collections\TileExtenderProxy.cs" />
<Compile Include="Components\DoorwayCleanup.cs" />
<Compile Include="Components\DoorwayCleanupScripting\DCSRemoveDoorwayConnectedDoorway.cs" />
<Compile Include="Components\DoorwayCleanupScripting\DCSRemoveDoorwaySpawnedPrefab.cs" />
@ -163,6 +164,9 @@
<Compile Include="MainPathExtender.cs" />
<Compile Include="Managers\EnemyManager.cs" />
<Compile Include="Managers\ScrapItemManager.cs" />
<Compile Include="Patches\BranchCountHelperPatch.cs" />
<Compile Include="Patches\DungeonPatch.cs" />
<Compile Include="Patches\TileProxyPatch.cs" />
<Compile Include="PluginConfig.cs" />
<Compile Include="DevTools\DevDebugManager.cs" />
<Compile Include="DevTools\DevDebugManagerUI.cs" />
@ -196,7 +200,7 @@
<Compile Include="Generation\DunGenPlusGeneratorBranchLoop.cs" />
<Compile Include="Generation\DunGenPlusGeneratorMiscellaneous.cs" />
<Compile Include="Managers\DoorwayManager.cs" />
<Compile Include="Patches\DoorwayConnectionPatch.cs" />
<Compile Include="Patches\DungeonProxyPatch.cs" />
<Compile Include="Generation\DoorwaySistersRule.cs" />
<Compile Include="Patches\DungeonGeneratorPatch.cs" />
<Compile Include="Patches\LethalLevelLoaderPatches.cs" />
@ -204,6 +208,7 @@
<Compile Include="Patches\StartOfRoundPatch.cs" />
<Compile Include="Plugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Components\TileExtender.cs" />
<Compile Include="Utils\TranspilerUtilities.cs" />
<Compile Include="Utils\Utility.cs" />
</ItemGroup>

View File

@ -0,0 +1,73 @@
using DunGen;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using DunGenPlus.Utils;
using DunGenPlus.Generation;
using DunGenPlus.Managers;
using DunGenPlus.Collections;
using DunGenPlus.DevTools;
using DunGen.Graph;
using UnityEngine;
using DunGenPlus.Components;
namespace DunGenPlus.Patches
{
internal class BranchCountHelperPatch {
[HarmonyTranspiler]
[HarmonyPatch(typeof(BranchCountHelper), "ComputeBranchCounts")]
public static IEnumerable<CodeInstruction> ComputeBranchCountsPatch(IEnumerable<CodeInstruction> instructions){
var branchModeField = typeof(DungeonFlow).GetField("BranchMode", BindingFlags.Instance | BindingFlags.Public);
var branchSequence = new InstructionSequenceStandard("BranchMode", false);
branchSequence.AddBasic(OpCodes.Ldfld, branchModeField);
foreach(var instruction in instructions){
if (branchSequence.VerifyStage(instruction)) {
var specialFunction = typeof(DunGenPlusGenerator).GetMethod("GetBranchMode", BindingFlags.Static | BindingFlags.Public);
yield return new CodeInstruction(OpCodes.Call, specialFunction);
continue;
}
yield return instruction;
}
branchSequence.ReportComplete();
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(BranchCountHelper), "ComputeBranchCountsGlobal")]
public static IEnumerable<CodeInstruction> ComputeBranchCountsGlobalPatch(IEnumerable<CodeInstruction> instructions){
var branchCountField = typeof(DungeonFlow).GetField("BranchCount", BindingFlags.Instance | BindingFlags.Public);
var branchSequence = new InstructionSequenceStandard("BranchCount");
branchSequence.AddBasic(OpCodes.Ldfld, branchCountField);
foreach(var instruction in instructions){
if (branchSequence.VerifyStage(instruction)) {
var specialFunction = typeof(DunGenPlusGenerator).GetMethod("GetBranchCount", BindingFlags.Static | BindingFlags.Public);
yield return new CodeInstruction(OpCodes.Call, specialFunction);
continue;
}
yield return instruction;
}
branchSequence.ReportComplete();
}
}
}

View File

@ -47,6 +47,7 @@ namespace DunGenPlus.Patches {
}
if (DunGenPlusGenerator.Active && DunGenPlusGenerator.ActiveAlternative) {
TileProxyPatch.ResetDictionary();
DunGenPlusGenerator.SetCurrentMainPathExtender(0);
MainRoomDoorwayGroups.ModifyGroupBasedOnBehaviourSimpleOnce = false;
}
@ -157,53 +158,6 @@ namespace DunGenPlus.Patches {
nodesSequence.ReportComplete();
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(BranchCountHelper), "ComputeBranchCounts")]
public static IEnumerable<CodeInstruction> ComputeBranchCountsPatch(IEnumerable<CodeInstruction> instructions){
var branchModeField = typeof(DungeonFlow).GetField("BranchMode", BindingFlags.Instance | BindingFlags.Public);
var branchSequence = new InstructionSequenceStandard("BranchMode", false);
branchSequence.AddBasic(OpCodes.Ldfld, branchModeField);
foreach(var instruction in instructions){
if (branchSequence.VerifyStage(instruction)) {
var specialFunction = typeof(DunGenPlusGenerator).GetMethod("GetBranchMode", BindingFlags.Static | BindingFlags.Public);
yield return new CodeInstruction(OpCodes.Call, specialFunction);
continue;
}
yield return instruction;
}
branchSequence.ReportComplete();
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(BranchCountHelper), "ComputeBranchCountsGlobal")]
public static IEnumerable<CodeInstruction> ComputeBranchCountsGlobalPatch(IEnumerable<CodeInstruction> instructions){
var branchCountField = typeof(DungeonFlow).GetField("BranchCount", BindingFlags.Instance | BindingFlags.Public);
var branchSequence = new InstructionSequenceStandard("BranchCount");
branchSequence.AddBasic(OpCodes.Ldfld, branchCountField);
foreach(var instruction in instructions){
if (branchSequence.VerifyStage(instruction)) {
var specialFunction = typeof(DunGenPlusGenerator).GetMethod("GetBranchCount", BindingFlags.Static | BindingFlags.Public);
yield return new CodeInstruction(OpCodes.Call, specialFunction);
continue;
}
yield return instruction;
}
branchSequence.ReportComplete();
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(DungeonGenerator), "InnerGenerate", MethodType.Enumerator)]
@ -243,90 +197,6 @@ namespace DunGenPlus.Patches {
editorCheck.ReportComplete();
}
/*
[HarmonyTranspiler]
[HarmonyPatch(typeof(DungeonGenerator), "AddTile")]
public static IEnumerable<CodeInstruction> AddTilePatch(IEnumerable<CodeInstruction> instructions, ILGenerator generator) {
var getCountFunction = typeof(Queue<DoorwayPair>).GetMethod("get_Count", BindingFlags.Instance | BindingFlags.Public);
var whileLoopSequence = new InstructionSequenceHold("while loop");
whileLoopSequence.AddBasic(OpCodes.Br);
whileLoopSequence.AddBasicLocal(OpCodes.Ldloc_S, 8);
whileLoopSequence.AddAny();
whileLoopSequence.AddBasic(OpCodes.Ldc_I4_0);
whileLoopSequence.AddBasic(OpCodes.Bgt);
foreach(var instruction in instructions){
var yieldInstruction = true;
var result = whileLoopSequence.VerifyStage(instruction);
switch(result) {
case InstructionSequenceHold.HoldResult.None:
break;
case InstructionSequenceHold.HoldResult.Hold:
yieldInstruction = false;
break;
case InstructionSequenceHold.HoldResult.Release:
foreach(var i in whileLoopSequence.Instructions){
yield return i;
}
whileLoopSequence.ClearInstructions();
yieldInstruction = false;
break;
case InstructionSequenceHold.HoldResult.Finished:
// my special function
var specialFunction = typeof(DunGenPlusGenerator).GetMethod("ProcessDoorwayPairs");
var resultLocal = generator.DeclareLocal(typeof((TilePlacementResult result, TileProxy tile)));
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Ldarg_S, 4);
yield return new CodeInstruction(OpCodes.Ldloc_S, 8);
yield return new CodeInstruction(OpCodes.Call, specialFunction);
var item1Field = typeof((TilePlacementResult result, TileProxy tile)).GetField("Item1");
var item2Field = typeof((TilePlacementResult result, TileProxy tile)).GetField("Item2");
yield return new CodeInstruction(OpCodes.Stloc_S, resultLocal);
yield return new CodeInstruction(OpCodes.Ldloc_S, resultLocal);
yield return new CodeInstruction(OpCodes.Ldfld, item1Field);
yield return new CodeInstruction(OpCodes.Stloc_S, 9);
yield return new CodeInstruction(OpCodes.Ldloc_S, resultLocal);
yield return new CodeInstruction(OpCodes.Ldfld, item2Field);
yield return new CodeInstruction(OpCodes.Stloc_S, 10);
whileLoopSequence.ClearInstructions();
yieldInstruction = false;
break;
}
if (yieldInstruction) yield return instruction;
}
whileLoopSequence.ReportComplete();
}
*/
[HarmonyPostfix]
[HarmonyPatch(typeof(RoundManager), "FinishGeneratingLevel")]
public static void GenerateBranchPathsPatch(){
if (DunGenPlusGenerator.Active) {
Plugin.logger.LogDebug("Alt. InnerGenerate() function complete");
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(RoundManager), "SetPowerOffAtStart")]
public static void SetPowerOffAtStartPatch(){
DoorwayManager.onMainEntranceTeleportSpawnedEvent.Call();
}
[HarmonyPostfix]
[HarmonyPatch(typeof(DungeonGenerator), "PostProcess")]
public static void GenerateBranchPathsPatch(ref DungeonGenerator __instance){
@ -364,33 +234,6 @@ namespace DunGenPlus.Patches {
addTileSequence.ReportComplete();
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(Dungeon), "FromProxy")]
public static IEnumerable<CodeInstruction> FromProxyPatch(IEnumerable<CodeInstruction> instructions){
var endSequence = new InstructionSequenceStandard("Forloop End");
endSequence.AddBasicLocal(OpCodes.Ldloca_S, 1);
endSequence.AddBasic(OpCodes.Constrained);
endSequence.AddBasic(OpCodes.Callvirt);
endSequence.AddBasic(OpCodes.Endfinally);
// WE MUST INJECT BEFORE ENDFINALLY
// DiFFoZ says cause try/catch block something
// Idk that makes no sense
// But if it works it works
foreach(var instruction in instructions){
if (endSequence.VerifyStage(instruction)) {
var specialFunction = typeof(DunGenPlusGenerator).GetMethod("AddTileToMainPathDictionary", BindingFlags.Static | BindingFlags.Public);
yield return new CodeInstruction(OpCodes.Ldloc_0);
yield return new CodeInstruction(OpCodes.Call, specialFunction);
}
yield return instruction;
}
endSequence.ReportComplete();
}
[HarmonyPrefix]
[HarmonyPatch(typeof(DungeonGenerator), "ProcessGlobalProps")]
public static bool ProcessGlobalPropsPatch(ref DungeonGenerator __instance){
@ -453,76 +296,5 @@ namespace DunGenPlus.Patches {
lastArchetype = archetype;
}
/*
[HarmonyTranspiler]
[HarmonyPatch(typeof(DungeonGenerator), "GenerateMainPath", MethodType.Enumerator)]
public static IEnumerable<CodeInstruction> GenerateMainPathPatch(IEnumerable<CodeInstruction> instructions){
var addArchFunction = typeof(List<DungeonArchetype>).GetMethod("Add", BindingFlags.Instance | BindingFlags.Public);
//var addNodeFunction = typeof(List<GraphNode>).GetMethod("Add", BindingFlags.Instance | BindingFlags.Public);
var archSequence = new InstructionSequence("archetype node");
archSequence.AddOperandTypeCheck(OpCodes.Ldfld, typeof(List<DungeonArchetype>));
archSequence.AddBasic(OpCodes.Ldnull);
archSequence.AddBasic(OpCodes.Callvirt, addArchFunction);
var nodeSequence = new InstructionSequence("graph node");
nodeSequence.AddBasicLocal(OpCodes.Ldloc_S, 12);
nodeSequence.AddBasicLocal(OpCodes.Stloc_S, 8);
var limitSequence = new InstructionSequence("limit nodes");
limitSequence.AddBasic(OpCodes.Ldnull);
limitSequence.AddBasicLocal(OpCodes.Stloc_S, 13);
limitSequence.AddBasic(OpCodes.Ldloc_1);
limitSequence.AddBasicLocal(OpCodes.Ldloc_S, 13);
foreach(var instruction in instructions){
if (archSequence.VerifyStage(instruction)){
var method = typeof(GeneratePath).GetMethod("ModifyMainBranchNodeArchetype", BindingFlags.Public | BindingFlags.Static);
yield return new CodeInstruction(OpCodes.Ldloc_S, 8);
yield return new CodeInstruction(OpCodes.Call, method);
yield return instruction;
continue;
}
if (nodeSequence.VerifyStage(instruction)){
var method = typeof(GeneratePath).GetMethod("ModifyGraphNode", BindingFlags.Public | BindingFlags.Static);
yield return new CodeInstruction(OpCodes.Call, method);
yield return instruction;
continue;
}
if (limitSequence.VerifyStage(instruction)){
var method = typeof(GeneratePath).GetMethod("LimitTilesToFirstFloor", BindingFlags.Public | BindingFlags.Static);
var field = typeof(DungeonGenerator).Assembly.GetType("DunGen.DungeonGenerator+<GenerateMainPath>d__100").GetField("<j>5__8", BindingFlags.NonPublic | BindingFlags.Instance);
yield return instruction;
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Ldfld, field);
yield return new CodeInstruction(OpCodes.Call, method);
continue;
}
yield return instruction;
}
archSequence.ReportComplete();
nodeSequence.ReportComplete();
limitSequence.ReportComplete();
}
*/
}
}

View File

@ -0,0 +1,52 @@
using DunGen;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using DunGenPlus.Utils;
using DunGenPlus.Generation;
using DunGenPlus.Managers;
using DunGenPlus.Collections;
using DunGenPlus.DevTools;
using DunGen.Graph;
using UnityEngine;
using DunGenPlus.Components;
namespace DunGenPlus.Patches
{
internal class DungeonPatch {
[HarmonyTranspiler]
[HarmonyPatch(typeof(Dungeon), "FromProxy")]
public static IEnumerable<CodeInstruction> FromProxyPatch(IEnumerable<CodeInstruction> instructions){
var endSequence = new InstructionSequenceStandard("Forloop End");
endSequence.AddBasicLocal(OpCodes.Ldloca_S, 1);
endSequence.AddBasic(OpCodes.Constrained);
endSequence.AddBasic(OpCodes.Callvirt);
endSequence.AddBasic(OpCodes.Endfinally);
// WE MUST INJECT BEFORE ENDFINALLY
// DiFFoZ says cause try/catch block something
// Idk that makes no sense
// But if it works it works
foreach(var instruction in instructions){
if (endSequence.VerifyStage(instruction)) {
var specialFunction = typeof(DunGenPlusGenerator).GetMethod("AddTileToMainPathDictionary", BindingFlags.Static | BindingFlags.Public);
yield return new CodeInstruction(OpCodes.Ldloc_0);
yield return new CodeInstruction(OpCodes.Call, specialFunction);
}
yield return instruction;
}
endSequence.ReportComplete();
}
}
}

View File

@ -11,7 +11,7 @@ using System.Threading.Tasks;
using DunGenPlus.Generation;
namespace DunGenPlus.Patches {
internal class DoorwayConnectionPatch {
internal class DungeonProxyPatch {
[HarmonyPatch(typeof(DungeonProxy), "ConnectOverlappingDoorways")]
[HarmonyPrefix]

View File

@ -14,7 +14,22 @@ using Unity.Netcode;
using UnityEngine;
namespace DunGenPlus.Patches {
public class RoundManagerPatch {
internal class RoundManagerPatch {
[HarmonyPostfix]
[HarmonyPatch(typeof(RoundManager), "FinishGeneratingLevel")]
public static void GenerateBranchPathsPatch(){
if (DunGenPlusGenerator.Active) {
Plugin.logger.LogDebug("Alt. InnerGenerate() function complete");
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(RoundManager), "SetPowerOffAtStart")]
public static void SetPowerOffAtStartPatch(){
DoorwayManager.onMainEntranceTeleportSpawnedEvent.Call();
}
[HarmonyPostfix]
[HarmonyPatch(typeof(RoundManager), "Awake")]

View File

@ -0,0 +1,42 @@
using DunGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
using DunGenPlus.Collections;
using UnityEngine;
namespace DunGenPlus.Patches {
internal class TileProxyPatch {
public static Dictionary<TileProxy, TileExtenderProxy> TileExtenderProxyDictionary = new Dictionary<TileProxy, TileExtenderProxy>();
public static void ResetDictionary(){
TileExtenderProxyDictionary.Clear();
}
public static TileExtenderProxy GetTileExtenderProxy(TileProxy proxy){
return TileExtenderProxyDictionary[proxy];
}
public static void AddTileExtenderProxy(TileProxy tileProxy, TileExtenderProxy tileExtenderProxy){
TileExtenderProxyDictionary.Add(tileProxy, tileExtenderProxy);
}
[HarmonyPatch(typeof(TileProxy), MethodType.Constructor, new Type[] { typeof(GameObject), typeof(bool), typeof(Vector3) })]
[HarmonyPostfix]
public static void TileProxyConstructorNewPatch(ref TileProxy __instance){
AddTileExtenderProxy(__instance, new TileExtenderProxy(__instance));
}
[HarmonyPatch(typeof(TileProxy), MethodType.Constructor, new Type[] { typeof(TileProxy) })]
[HarmonyPostfix]
public static void TileProxyConstructorExistingPatch(ref TileProxy __instance, TileProxy existingTile){
AddTileExtenderProxy(__instance, new TileExtenderProxy(GetTileExtenderProxy(existingTile)));
}
}
}

View File

@ -2,6 +2,7 @@
using BepInEx.Logging;
using DunGen;
using DunGen.Graph;
using DunGenPlus.Collections;
using DunGenPlus.Generation;
using DunGenPlus.Managers;
using DunGenPlus.Patches;
@ -44,8 +45,11 @@ namespace DunGenPlus {
PluginConfig.SetupConfig(Config);
Harmony.PatchAll(typeof(DungeonGeneratorPatch));
Harmony.PatchAll(typeof(DoorwayConnectionPatch));
Harmony.PatchAll(typeof(DungeonPatch));
Harmony.PatchAll(typeof(DungeonProxyPatch));
Harmony.PatchAll(typeof(RoundManagerPatch));
Harmony.PatchAll(typeof(BranchCountHelperPatch));
Harmony.PatchAll(typeof(TileProxyPatch));
try {
Harmony.PatchAll(typeof(LethalLevelLoaderPatches));