Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added exempting concealed grids from meteor shower #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Concealment/Concealment.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<DependentUpon>DynamicConcealmentEditor.xaml</DependentUpon>
</Compile>
<Compile Include="DynamicConcealmentManager.cs" />
<Compile Include="MeteorShowerTargetPatch.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Settings.cs" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions Concealment/ConcealmentPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Torch.API.Session;
using Torch.Collections;
using Torch.Managers;
using Torch.Managers.PatchManager;
using Torch.Session;
using VRage.Game;
using VRage.Game.Components;
Expand Down Expand Up @@ -74,6 +75,8 @@ public override void Init(ITorchBase torch)
Settings.Data.PropertyChanged += Data_PropertyChanged;
_concealedAabbTree = new MyDynamicAABBTreeD(MyConstants.GAME_PRUNING_STRUCTURE_AABB_EXTENSION);
torch.Managers.GetManager<ITorchSessionManager>()?.AddFactory(CreateManager);

MeteorShowerTargetPatch.Patch(torch.Managers.GetManager<PatchManager>().AcquireContext(), this);
}

private IManager CreateManager(ITorchSession session)
Expand Down
63 changes: 63 additions & 0 deletions Concealment/MeteorShowerTargetPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using ParallelTasks;
using Sandbox.Game.Entities;
using Torch.Managers.PatchManager;
using Torch.Managers.PatchManager.MSIL;
using Torch.Utils;

namespace Concealment
{
public static class MeteorShowerTargetPatch
{
[ReflectedMethodInfo(typeof(MeteorShowerTargetPatch), nameof(Transpiler))]
#pragma warning disable 649
private static readonly MethodInfo _transpilerMethod;

[ReflectedMethodInfo(typeof(MeteorShowerTargetPatch), nameof(FixTargets))]
private static readonly MethodInfo _fixTargetsMethod;
#pragma warning restore 649

private static ConcealmentPlugin _plugin;

public static void Patch(PatchContext ctx, ConcealmentPlugin plugin)
{
_plugin = plugin;
ctx.GetPattern(typeof(MyMeteor).Assembly.GetType("Sandbox.Game.Entities.MyMeteorShower", true)
.GetMethod("GetTargets", BindingFlags.Static | BindingFlags.NonPublic)).Transpilers
.Add(_transpilerMethod);
}

private static IEnumerable<MsilInstruction> Transpiler(IEnumerable<MsilInstruction> ins)
{
var found = false;
foreach (var instruction in ins)
{
if (!found && instruction.OpCode == OpCodes.Stloc_0)
{
found = true;
yield return instruction;
yield return new MsilInstruction(OpCodes.Ldloc_0);
yield return new MsilInstruction(OpCodes.Call).InlineValue(_fixTargetsMethod);
continue;
}

yield return instruction;
}
}

private static void FixTargets(List<MyCubeGrid> grids)
{
// idk about that, just shitty coded thing
var toRemove = new List<MyCubeGrid>();
Parallel.ForEach(_plugin.ConcealedGroups.SelectMany(b => b.Grids), grid =>
{
if (grids.Contains(grid))
toRemove.Add(grid);
}, blocking: true);
toRemove.ForEach(b => grids.Remove(b));
}
}
}