Skip to content

Commit

Permalink
Some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Aug 22, 2024
1 parent ba3ff0a commit b051804
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MonkeyLoader.Resonite.DataFeeds
{
public abstract class DataFeedBuilderMonkey<TMonkey> : ResoniteMonkey<TMonkey>

Check warning on line 9 in MonkeyLoader.Resonite.Integration/DataFeeds/DataFeedBuilderMonkey.cs

View workflow job for this annotation

GitHub Actions / Build

Missing XML comment for publicly visible type or member 'DataFeedBuilderMonkey<TMonkey>'
where TMonkey : DataFeedBuilderMonkey<TMonkey>, new()
{
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,73 @@
using FrooxEngine;
using HarmonyLib;
using MonkeyLoader.Events;
using MonkeyLoader.Patching;
using MonkeyLoader.Resonite.Locale;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MonkeyLoader.Resonite.DataFeeds
{
internal abstract class DataFeedInjectorMonkey<TMonkey, TDataFeed> : ResoniteMonkey<TMonkey>,
internal sealed class DataFeedInjectorMonkey<TDataFeed> : ResoniteAsyncEventHandlerMonkey<DataFeedInjectorMonkey<TDataFeed>, FallbackLocaleGenerationEvent>,
IAsyncEventSource<EnumerateDataFeedEvent<TDataFeed>>
where TMonkey : DataFeedInjectorMonkey<TMonkey, TDataFeed>, new()
where TDataFeed : IDataFeed
{
private static AsyncEventDispatching<EnumerateDataFeedEvent<TDataFeed>>? _dispatching;

[HarmonyPostfix]
[HarmonyPatch(nameof(IDataFeed.Enumerate), [typeof(IReadOnlyList<string>), typeof(IReadOnlyList<string>), typeof(string)])]
private static IAsyncEnumerable<DataFeedItem> EnumeratePostfix(IAsyncEnumerable<DataFeedItem> __result, TDataFeed __instance, IReadOnlyList<string> path, IReadOnlyList<string> groupKeys, string searchPhrase)
/// <inheritdoc/>
public override bool CanBeDisabled => true;

/// <inheritdoc/>
public override string Id { get; } = typeof(DataFeedInjectorMonkey<TDataFeed>).CompactDescription();

/// <inheritdoc/>
public override int Priority => HarmonyLib.Priority.Normal;

protected override bool AppliesTo(FallbackLocaleGenerationEvent eventData) => !Failed && Enabled;

protected override IEnumerable<IFeaturePatch> GetFeaturePatches() => [];

protected override Task Handle(FallbackLocaleGenerationEvent eventData)
{
eventData.AddMessage(this.GetLocaleKey("Name"), $"{typeof(TDataFeed).CompactDescription()}-Injector");
eventData.AddMessage(this.GetLocaleKey("Description"), $"Sends out the {typeof(EnumerateDataFeedEvent<TDataFeed>).CompactDescription()} event for monkeys to manipulate the items returned.");

return Task.CompletedTask;
}

protected override bool OnEngineReady()
{
var eventData = new EnumerateDataFeedEvent<TDataFeed>(__instance, __result, path, groupKeys, searchPhrase);
if (!Prepare())
{
Logger.Error(() => $"Failed to find Enumerate(IReadOnlyList<string>, IReadOnlyList<string>, string) method declared on type [{typeof(TDataFeed).CompactDescription()}]");

return false;
}

Mod.RegisterEventSource(this);

return base.OnEngineReady();
}

[HarmonyPrefix]
private static IAsyncEnumerable<DataFeedItem> EnumeratePostfix(IAsyncEnumerable<DataFeedItem> __result, TDataFeed __instance, IReadOnlyList<string> path, IReadOnlyList<string> groupKeys, string searchPhrase, object viewData)
{
var eventData = new EnumerateDataFeedEvent<TDataFeed>(__instance, __result, path, groupKeys, searchPhrase, viewData);

_dispatching?.Invoke(eventData);

return eventData.FinalResult;
}

private static bool Prepare() => TargetMethod() is not null;

private static MethodBase TargetMethod()
=> AccessTools.DeclaredMethod(typeof(TDataFeed), nameof(IDataFeed.Enumerate), [typeof(IReadOnlyList<string>), typeof(IReadOnlyList<string>), typeof(string), typeof(object)]);

event AsyncEventDispatching<EnumerateDataFeedEvent<TDataFeed>>? IAsyncEventSource<EnumerateDataFeedEvent<TDataFeed>>.Dispatching
{
add => _dispatching += value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace MonkeyLoader.Resonite.DataFeeds
/// Represents the event data for the Enumerate Data Feed Event.
/// </summary>
/// <remarks>
/// This event is used by concrete <see cref="DataFeedInjectorMonkey{TMonkey, TDataFeed}"/>
/// This event is used by concrete <see cref="DataFeedInjectorMonkey{TDataFeed}"/>
/// implementations to signal that the <see cref="IDataFeed"/> they're patching is being enumerated.
/// </remarks>
/// <typeparam name="TDataFeed">The type of the data feed.</typeparam>
Expand Down Expand Up @@ -106,14 +106,20 @@ public IAsyncEnumerable<DataFeedItem> FinalResult
/// </summary>
public string? SearchPhrase { get; }

/// <summary>
/// Gets the view data for this enumeration request.
/// </summary>
public object ViewData { get; }

internal EnumerateDataFeedEvent(TDataFeed dataFeed, IAsyncEnumerable<DataFeedItem> result,
IReadOnlyList<string>? path, IReadOnlyList<string>? groupKeys, string? searchPhrase)
IReadOnlyList<string>? path, IReadOnlyList<string>? groupKeys, string? searchPhrase, object viewData)
{
DataFeed = dataFeed;
OriginalResult = result;
Path = path ?? [];
GroupKeys = groupKeys ?? [];
SearchPhrase = searchPhrase;
ViewData = viewData;
}

/// <summary>
Expand Down

0 comments on commit b051804

Please sign in to comment.