Skip to content
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
Binary file modified Build/Release/RogueTechPerfFix/RogueTechPerfFixes.dll
Binary file not shown.
53 changes: 53 additions & 0 deletions CustomComponentPerfFix/DataManager/AsyncJsonLoadRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using BattleTech;
using HBS.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RogueTechPerfFixes.DataManager
{
public static class AsyncJsonLoadRequest
{
private static readonly ILog logger = Logger.GetLogger("DataLoader", LogLevel.Log);

public static void LogLoadRequest(BattleTechResourceType resourceType, string identifier, bool allowRequestStacking)
{
logger.Log($"LOAD REQUEST for type: {resourceType} id: {identifier} with allowStacking: {allowRequestStacking}");
StackTrace st = new StackTrace();
logger.Log($" ST: {st}");
}

public static async Task LoadResource(string path, Action<string> handler)
{
try
{
using (FileStream arg = new FileStream(path, FileMode.Open, FileAccess.Read))
{
StreamReader sr = new StreamReader(arg);
//logger.Log($"READ file at path: {path}");

StackTrace st = new StackTrace();
//logger.Log($" ST: {st}");

string content = await sr.ReadToEndAsync();

// TODO: Add DataLoader.Entry references here, so file update monitoring can happen -or- replicate with our own
//logger.Log($"Handling file at path: {path} with content: {content}");
//logger.Log($"HANDLE file at path: {path}");
handler(content);
}
}
catch (Exception exception)
{
string message = $"LoadResource() - Caught exception while loading [{path}]";
logger.LogError(message);
logger.LogException(exception);
handler(null);
}
}
}
}
25 changes: 13 additions & 12 deletions CustomComponentPerfFix/RogueTechPerfFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,45 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\BattleTech_Data\Managed\0Harmony.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\BattleTech_Data\Managed\Assembly-CSharp.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="CustomActivatableEquipment">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\Mods\CustomActivatableEquipment\CustomActivatableEquipment.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\Mods\CustomActivatableEquipment\CustomActivatableEquipment.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="CustomAmmoCategories">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\Mods\CustomAmmoCategories\CustomAmmoCategories.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\Mods\CustomAmmoCategories\CustomAmmoCategories.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="CustomComponents">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\Mods\CustomComponents\CustomComponents.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\Mods\CustomComponents\CustomComponents.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="CustomUnits">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\Mods\CustomUnits\CustomUnits.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\Mods\CustomUnits\CustomUnits.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="DOTween">
<HintPath>..\References\DOTween.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="DOTweenPro">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\BattleTech_Data\Managed\DOTweenPro.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\DOTweenPro.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="LowVisibility, Version=0.9.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\Mods\LowVisibility\LowVisibility.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\Mods_BTA_Shared_Old\LowVisibility\LowVisibility.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\BattleTech_Data\Managed\Newtonsoft.Json.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\Newtonsoft.Json.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
Expand All @@ -91,19 +91,20 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\BattleTech_Data\Managed\UnityEngine.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\BattleTech_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.PhysicsModule">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\BattleTech_Data\Managed\UnityEngine.PhysicsModule.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\UnityEngine.PhysicsModule.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DataManager\AsyncJsonLoadRequest.cs" />
<Compile Include="Models\ActionSemaphore.cs" />
<Compile Include="Models\CecilInjector.cs" />
<Compile Include="Models\HarmonyPatcher.cs" />
Expand Down
14 changes: 8 additions & 6 deletions Injection/Injection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\BattleTech_Data\Managed\Assembly-CSharp.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="DOTween">
<HintPath>..\References\DOTween.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="DOTweenPro">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\BattleTech_Data\Managed\DOTweenPro.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\DOTweenPro.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Mono.Cecil, Version=0.11.0.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\Mods\ModTek\Mono.Cecil.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\MonoMod\Mono.Cecil.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mono.Cecil.Rocks, Version=0.11.0.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\Mods\ModTek\Mono.Cecil.Rocks.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\MonoMod\Mono.Cecil.Rocks.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand All @@ -71,18 +71,20 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\BattleTech_Data\Managed\UnityEngine.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>G:\SteamLibrary\steamapps\common\BATTLETECH\BattleTech_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<HintPath>E:\steam\SteamApps\common\BATTLETECH\BattleTech_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Injection\CecilManager.cs" />
<None Include="Injection\I_AbstractActor_expectedArmorValues.cs" />
<Compile Include="Injection\IInjector.cs" />
<Compile Include="Injection\I_DataManager.cs" />
<Compile Include="Injection\I_StringDataLoadRequest.cs" />
<Compile Include="Injection\I_BTLight.cs" />
<Compile Include="Injection\I_BTLightController.cs" />
<Compile Include="Injection\I_CombatAuraReticle.cs" />
Expand Down
6 changes: 4 additions & 2 deletions Injection/Injection/CecilManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ static CecilManager()
nameof(RTPFVersion) + Mod.Version.ToString().Replace('.', '_')
, FieldAttributes.Private
, _assembly.MainModule.ImportReference(typeof(string)));

TypeDefinition targetType = null;
foreach (TypeDefinition type in _assembly.MainModule.Types)
{
Expand Down Expand Up @@ -157,11 +156,14 @@ public static void Init()
try
{
//Injectors.Add(new I_DesiredAuraReceptionState());

Injectors.Add(new I_CombatAuraReticle());
Injectors.Add(new I_BTLight());
Injectors.Add(new I_BTLightController());

// DataManager fixes
Injectors.Add(new I_StringDataLoadRequest());
//Injectors.Add(new I_DataManager());

//Injectors.Add(new I_DOTweenAnimation());
//Injectors.Add(new I_ElementManager());
//Injectors.Add(new I_SortMoveCandidatesByInfMapNode());
Expand Down
99 changes: 99 additions & 0 deletions Injection/Injection/I_DataManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using BattleTech;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using RogueTechPerfFixes;
using RogueTechPerfFixes.DataManager;
using RogueTechPerfFixes.Injection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Injection.Injection
{
// Injector that logs the DataManager load requests by type
class I_DataManager : IInjector
{
private const string _targetType = "BattleTech.Data.DataManager";

#region Implementation of IInjector

public void Inject(Dictionary<string, TypeDefinition> typeTable, ModuleDefinition module)
{
if (!Mod.Settings.Patch.Vanilla)
return;

if (typeTable.TryGetValue(_targetType, out TypeDefinition type))
{
CecilManager.WriteError($"Injecting IL for targetType: {_targetType}");
InjectIL(type, module);
}
else
{
CecilManager.WriteError($"Can't find target type: {_targetType}");
}
}

#endregion
private static void InjectIL(TypeDefinition type, ModuleDefinition module)
{
// internal DataManager.FileLoadRequest CreateFileRequest(BattleTechResourceType resourceType, string identifier, PrewarmRequest prewarm, bool allowRequestStacking)
const string targetMethod = "CreateFileRequest";

// From class -> JsonLoadRequest -> StringDataLoadRequest
MethodDefinition method =
type.GetMethods().FirstOrDefault(m => m.Name == targetMethod);

if (method == null)
{
CecilManager.WriteError($"Can't find method: {targetMethod}\n");
return;
}

ILProcessor ilProcessor = method.Body.GetILProcessor();
Instruction methodStart = method.Body.Instructions[0];

List<Instruction> newInstructions = CreateInstructions(ilProcessor, methodStart, module);
newInstructions.Reverse();

foreach (Instruction instruction in newInstructions)
{
ilProcessor.InsertBefore(method.Body.Instructions[0], instruction);
}
}

private static List<Instruction> CreateInstructions(ILProcessor ilProcessor, Instruction branchTarget, ModuleDefinition module)
{
// Create an importable reference to our logger
TypeReference ajlr_TR = module.ImportReference(typeof(AsyncJsonLoadRequest));
TypeReference void_TR = module.ImportReference(typeof(void));

MethodReference ajlr_lr_MR = new MethodReference("LogLoadRequest", void_TR, ajlr_TR);

TypeReference battleTechResourceType_TR = module.ImportReference(typeof(BattleTechResourceType));
ajlr_lr_MR.Parameters.Add(new ParameterDefinition(battleTechResourceType_TR));

TypeReference string_TR = module.ImportReference(typeof(string));
ajlr_lr_MR.Parameters.Add(new ParameterDefinition(string_TR));

TypeReference bool_TR = module.ImportReference(typeof(bool));
ajlr_lr_MR.Parameters.Add(new ParameterDefinition(bool_TR));

MethodReference ajlr_lr_Imported_MR = module.ImportReference(ajlr_lr_MR);

List<Instruction> instructions = new List<Instruction>()
{
// Add all params to stack
ilProcessor.Create(OpCodes.Ldarg, 1),
ilProcessor.Create(OpCodes.Ldarg, 2),
ilProcessor.Create(OpCodes.Ldarg, 4),
ilProcessor.Create(OpCodes.Call, ajlr_lr_Imported_MR)
};

return instructions;
}

}
}
Loading