Skip to content

Commit

Permalink
Initial and finalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
raulssorban committed Jun 27, 2024
1 parent 0e835b1 commit df3aed5
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 48 deletions.
Binary file added carbon/Carbon.Common.dll
Binary file not shown.
Binary file added carbon/Carbon.SDK.dll
Binary file not shown.
55 changes: 46 additions & 9 deletions src/RustServerMetrics/MetricsLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using API.Hooks;
using Carbon;
using Carbon.Components;
using UnityEngine;
using Harmony = HarmonyLib.Harmony;
using Logger = Carbon.Logger;

namespace RustServerMetrics
{
Expand Down Expand Up @@ -80,23 +85,20 @@ internal static void Initialize()
internal void OnServerStarted()
{
RustServerMetricsLoader.__serverStarted = true;

Debug.Log($"[ServerMetrics]: Applying Startup Patches");
var assembly = GetType().Assembly;

var harmonyInstance = HarmonyLoader.loadedMods.FirstOrDefault(x => x.Assembly == assembly)?.Harmony.harmonyObject;
if (harmonyInstance == null)
{
RustServerMetricsLoader.__harmonyInstance ??= new Harmony("RustServerMetrics" + "PATCH");
harmonyInstance = RustServerMetricsLoader.__harmonyInstance;
}
RustServerMetricsLoader.__harmonyInstance ??= new Harmony("RustServerMetrics" + "PATCH");

HandleCarbon(RustServerMetricsLoader.__harmonyInstance);

var nestedTypes = assembly.GetTypes();
foreach (var nestedType in nestedTypes)
{
if (nestedType.GetCustomAttribute<DelayedHarmonyPatchAttribute>(false) == null) continue;
var patchProcessor = new PatchClassProcessor((Harmony)harmonyInstance, nestedType);

var patchProcessor = new PatchClassProcessor(RustServerMetricsLoader.__harmonyInstance, nestedType);
Debug.Log(patchProcessor.Patch() == null ? $"[ServerMetrics]: Failed to apply patch: {nestedType.Name}" : $"[ServerMetrics]: Applied Startup Patch: {nestedType.Name}");
}
}
Expand Down Expand Up @@ -135,6 +137,41 @@ public void StartLoggingMetrics()

#endregion

internal void HandleCarbon(Harmony instance)
{
var assembly = typeof(MetricsLogger).Assembly;
var types = assembly.GetTypes();
var count = 0;
using var args = TempArray<object>.New([instance]);

foreach (var type in types.Where(x => x.GetCustomAttribute<DelayedHarmonyPatchAttribute>() != null))
{
foreach (var method in type.GetMethods(BindingFlags.Static | BindingFlags.Public).Where(x => x.GetCustomAttribute<HarmonyTargetMethods>() != null))
{
IEnumerable<MethodBase> results = method.Invoke(null, args.Array) as IEnumerable<MethodBase>;

Logger.Log($"Lookup {type.Name}.{method.Name}: {results == null} {results?.Count()}");

foreach (var patchedMethod in results)
{
var hook = Community.Runtime.HookManager.LoadedDynamicHooks.FirstOrDefault(x =>
(x.TargetMethods != null && x.TargetMethods.Count != 0 && x.TargetMethods[0] == patchedMethod));

if (hook == null)
{
Logger.Log($" Not found: {patchedMethod.Name}");
continue;
}

Community.Runtime.HookManager.Subscribe(hook.Identifier, "RSM.Static");
count++;
}
}
}

Logger.Warn($"[RSM] Force patching {count:n0} static hooks");
Community.Runtime.HookManager.ForceUpdateHooks();
}

internal void OnPlayerInit(BasePlayer player)
{
Expand Down
11 changes: 6 additions & 5 deletions src/RustServerMetrics/RustServerMetrics.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>RustServerMetrics</RootNamespace>
<AssemblyName>RustServerMetrics</AssemblyName>
<AssemblyName>Carbon.RSM</AssemblyName>
<ProjectGuid>{3C3A47A5-709A-42BB-B2BF-DB1FA0FEE316}</ProjectGuid>

<TargetFramework>net48</TargetFramework>
Expand All @@ -15,9 +15,9 @@
<GamePlatform Condition=" '$(Configuration)' == 'Windows' ">windows</GamePlatform>
<GamePlatform Condition=" '$(GamePlatform)' == '' ">linux</GamePlatform>

<AssemblySearchPaths>..\..\deps\$(GamePlatform);$(AssemblySearchPaths)</AssemblySearchPaths>
<AssemblySearchPaths>..\..\deps\$(GamePlatform);$(AssemblySearchPaths);..\..\carbon</AssemblySearchPaths>

<LangVersion>10</LangVersion>
<LangVersion>preview</LangVersion>

<Nullable>disable</Nullable>
</PropertyGroup>
Expand Down Expand Up @@ -50,6 +50,8 @@
</Target>

<ItemGroup>
<Reference Include="Carbon.Common" />
<Reference Include="Carbon.SDK" />
<Reference Include="0Harmony" />
<Reference Include="Accessibility" />
<Reference Include="Assembly-CSharp" />
Expand All @@ -73,7 +75,6 @@
<Reference Include="Rust.Data" />
<Reference Include="Rust.Demo" />
<Reference Include="Rust.Global" />
<Reference Include="Rust.Harmony" />
<Reference Include="Rust.Localization" />
<Reference Include="Rust.Platform" />
<Reference Include="Rust.Platform.Common" />
Expand Down Expand Up @@ -150,4 +151,4 @@
<Reference Include="UnityEngine.WindModule" />
<Reference Include="UnityEngine.XRModule" />
</ItemGroup>
</Project>
</Project>
79 changes: 45 additions & 34 deletions src/RustServerMetrics/RustServerMetricsLoader.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,67 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Reflection;
using API.Assembly;
using API.Events;
using HarmonyLib;
using UnityEngine;
using Logger = Carbon.Logger;
using Object = UnityEngine.Object;

namespace RustServerMetrics;

public class RustServerMetricsLoader : IHarmonyModHooks
public class RustServerMetricsLoader : IModulePackage
{
public static bool __serverStarted = false;

public static Harmony __harmonyInstance;
public static List<Harmony> __modTimeWarningsHarmonyInstances = new ();

public void OnLoaded(OnHarmonyModLoadedArgs args)
{
if (!Bootstrap.bootstrapInitRun)
return;

MetricsLogger.Initialize();

if (MetricsLogger.Instance != null)
MetricsLogger.Instance.OnServerStarted();
}

public void OnUnloaded(OnHarmonyModUnloadedArgs args)
{
__harmonyInstance?.UnpatchAll();
foreach (var instance in __modTimeWarningsHarmonyInstances)
{
instance?.UnpatchAll();
}

if (MetricsLogger.Instance != null)
Object.DestroyImmediate(MetricsLogger.Instance);
}

public void AddModTimeWarnings(List<MethodInfo> methods)
{
{
var instance = new Harmony($"RustServerMetrics.ModTimeWarnings.{__modTimeWarningsHarmonyInstances.Count}");
__modTimeWarningsHarmonyInstances.Add(instance);

ModTimeWarnings.Methods.Clear();
ModTimeWarnings.Methods.AddRange(methods);

var patchProcessor = new PatchClassProcessor(instance, typeof(ModTimeWarnings));
patchProcessor.Patch();

foreach (var method in methods)
{
Debug.Log($"{method.DeclaringType?.Name}.{method.Name}");
Logger.Log($"{method.DeclaringType?.Name}.{method.Name}");
}

Debug.Log($"[ServerMetrics]: Added {methods.Count} ModTimeWarnings");
Logger.Log($"[ServerMetrics]: Added {methods.Count} ModTimeWarnings");
}

public void Awake(EventArgs args)
{
Logger.Log($"[ServerMetrics]: Carbon Community version {typeof(RustServerMetricsLoader).Assembly.GetName().Version} [module]");
}

public void OnLoaded(EventArgs args)
{
if (!Bootstrap.bootstrapInitRun)
return;

Carbon.Community.Runtime.Events.Subscribe(CarbonEvent.HookValidatorRefreshed, _ =>
{
Carbon.Components.Harmony.PatchAll(Assembly.GetExecutingAssembly());

MetricsLogger.Initialize();
});
}

public void OnUnloaded(EventArgs args)
{
__harmonyInstance?.UnpatchAll();
foreach (var instance in __modTimeWarningsHarmonyInstances)
{
instance?.UnpatchAll();
}

if (MetricsLogger.Instance != null)
Object.DestroyImmediate(MetricsLogger.Instance);
}
}
}

0 comments on commit df3aed5

Please sign in to comment.