这是indexloc提供的服务,不要输入任何密码
Skip to content

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using ChainSafe.Gaming.RPC.Events;
using ChainSafe.Gaming.Web3.Build;

namespace ChainSafe.Gaming.EVM.Events
{
public static class EventExtensionsUnity
{
/// <summary>
/// Enable EVM Event Manager for the Web3 client.
/// </summary>
/// <param name="services">The Web3 services collection.</param>
/// <param name="eventPollerConfig">(Optional) Event Poller configuration. This will only be used for the WebGL platform.</param>
/// <returns></returns>
/// <remarks>
/// For all the platforms that support multithreading the WebSocket strategy will be used.
/// In WebGL this will bind the Polling strategy which will fetch data periodically.
/// </remarks>
public static IWeb3ServiceCollection UseEvents(this IWeb3ServiceCollection services, PollingEventManagerConfig eventPollerConfig = null)
{
#if !UNITY_WEBGL
services.UseEventsWithWebSocket();
#else
if (eventPollerConfig == null)
{
services.UseEventsWithPolling();
}
else
{
services.UseEventsWithPolling(eventPollerConfig);
}
#endif
return services;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/ChainSafe.Gaming.Mud/MudExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ public static IWeb3ServiceCollection UseMud(this IWeb3ServiceCollection services
// Storage strategies
services.AddTransient<InMemoryMudStorage>(); // todo implement OffchainIndexerMudStorage, then register it in the next line

if (!services.IsBound<IEventManager>())
{
services.UseEvents();
}

if (!services.IsBound<INethereumWeb3Adapter>())
{
services.UseNethereumAdapters();
Expand Down
2 changes: 1 addition & 1 deletion src/ChainSafe.Gaming.Mud/MudFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Task<MudWorld> BuildWorld(IMudWorldConfig worldConfig)
{
var stopwatch = Stopwatch.StartNew();
var world = worldFactory.Build(worldConfig);
logWriter.Log($"Loaded world {worldConfig.ContractAddress} in {stopwatch.Elapsed}");
logWriter.Log($"Loaded MUD world {worldConfig.ContractAddress} in {stopwatch.Elapsed}");
return world;
}
}
Expand Down
30 changes: 20 additions & 10 deletions src/ChainSafe.Gaming.Mud/Storages/InMemory/InMemoryMudStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using ChainSafe.Gaming.Mud.Tables;
using ChainSafe.Gaming.RPC.Events;
using ChainSafe.Gaming.Web3;
using ChainSafe.Gaming.Web3.Core.Nethereum;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Mud;
Expand All @@ -18,25 +19,34 @@ namespace ChainSafe.Gaming.Mud.Storages.InMemory
public class InMemoryMudStorage : IMudStorage
{
private readonly INethereumWeb3Adapter nWeb3;
private readonly EventManager eventManager;
private readonly IEventManager eventManager;

private readonly SemaphoreSlim storeUpdateSemaphore = new(1);

private IInMemoryMudStorageConfig config;
private InMemoryTableRepository inMemoryRepository;
private string worldAddress;

public InMemoryMudStorage(INethereumWeb3Adapter nWeb3, EventManager eventManager)
public InMemoryMudStorage(INethereumWeb3Adapter nWeb3, IEventManager eventManager)
{
this.eventManager = eventManager;
this.nWeb3 = nWeb3;
}

public InMemoryMudStorage(INethereumWeb3Adapter nWeb3)
{
throw new Web3Exception($"{nameof(InMemoryMudStorage)} requires {nameof(IEventManager)} to work. " +
$"Please don't forget to register an {nameof(IEventManager)} in order to use " +
$"{nameof(InMemoryMudStorage)}.");
}

public event RecordSetDelegate RecordSet;

public event RecordDeletedDelegate RecordDeleted;

public async Task Initialize(IMudStorageConfig mudStorageConfig, string worldAddress)
{
this.worldAddress = worldAddress;
config = (IInMemoryMudStorageConfig)mudStorageConfig;
inMemoryRepository = new InMemoryTableRepository();
var storeLogProcessingService = new StoreEventsLogProcessingService(nWeb3, worldAddress);
Expand All @@ -46,18 +56,18 @@ await storeLogProcessingService.ProcessAllStoreChangesAsync(
null,
CancellationToken.None);

await eventManager.Subscribe<StoreSetRecordEventDTO>(OnStoreSetRecord);
await eventManager.Subscribe<StoreSpliceStaticDataEventDTO>(OnStoreSpliceStaticData);
await eventManager.Subscribe<StoreSpliceDynamicDataEventDTO>(OnStoreSpliceDynamicDataEventDTO);
await eventManager.Subscribe<StoreDeleteRecordEventDTO>(OnStoreDeleteRecord);
await eventManager.Subscribe<StoreSetRecordEventDTO>(OnStoreSetRecord, worldAddress);
await eventManager.Subscribe<StoreSpliceStaticDataEventDTO>(OnStoreSpliceStaticData, worldAddress);
await eventManager.Subscribe<StoreSpliceDynamicDataEventDTO>(OnStoreSpliceDynamicDataEventDTO, worldAddress);
await eventManager.Subscribe<StoreDeleteRecordEventDTO>(OnStoreDeleteRecord, worldAddress);
}

public async Task Terminate()
{
await eventManager.Unsubscribe<StoreSetRecordEventDTO>(OnStoreSetRecord);
await eventManager.Unsubscribe<StoreSpliceStaticDataEventDTO>(OnStoreSpliceStaticData);
await eventManager.Unsubscribe<StoreSpliceDynamicDataEventDTO>(OnStoreSpliceDynamicDataEventDTO);
await eventManager.Unsubscribe<StoreDeleteRecordEventDTO>(OnStoreDeleteRecord);
await eventManager.Unsubscribe<StoreSetRecordEventDTO>(OnStoreSetRecord, worldAddress);
await eventManager.Unsubscribe<StoreSpliceStaticDataEventDTO>(OnStoreSpliceStaticData, worldAddress);
await eventManager.Unsubscribe<StoreSpliceDynamicDataEventDTO>(OnStoreSpliceDynamicDataEventDTO, worldAddress);
await eventManager.Unsubscribe<StoreDeleteRecordEventDTO>(OnStoreDeleteRecord, worldAddress);
}

public async Task<object[][]> Query(MudTableSchema tableSchema, MudQuery query)
Expand Down
34 changes: 30 additions & 4 deletions src/ChainSafe.Gaming/RPC/Events/EventExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
using ChainSafe.Gaming.Web3.Build;
using ChainSafe.Gaming.Web3.Core;
using ChainSafe.Gaming.Web3.Core.Chains;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace ChainSafe.Gaming.RPC.Events
{
public static class EventExtensions
{
public static IWeb3ServiceCollection UseEvents(this IWeb3ServiceCollection services)
public static IWeb3ServiceCollection UseEventsWithWebSocket(this IWeb3ServiceCollection services)
{
// todo bind EventPoller implementation of IEventManager when running in WebGL build
return services
.AddSingleton<IEventManager, ILifecycleParticipant, IChainSwitchHandler, EventManager>();
services.AssertServiceNotBound<IEventManager>();
services.AddSingleton<IEventManager, ILifecycleParticipant, IChainSwitchHandler, WebSocketEventManager>();
return services;
}

public static IWeb3ServiceCollection UseEventsWithPolling(this IWeb3ServiceCollection services)
{
services.AssertServiceNotBound<IEventManager>();
services.AddSingleton<IEventManager, IChainSwitchHandler, PollingEventManager>();
return services;
}

public static IWeb3ServiceCollection UseEventsWithPolling(this IWeb3ServiceCollection services, PollingEventManagerConfig config)
{
services.AssertServiceNotBound<IEventManager>();
services.AssertConfigurationNotBound<PollingEventManagerConfig>();

services.ConfigureEventsWithPolling(config);
services.UseEventsWithPolling();

return services;
}

public static IWeb3ServiceCollection ConfigureEventsWithPolling(this IWeb3ServiceCollection services, PollingEventManagerConfig pollingConfig)
{
services.Replace(ServiceDescriptor.Singleton(pollingConfig));
return services;
}
}
}
4 changes: 2 additions & 2 deletions src/ChainSafe.Gaming/RPC/Events/IEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace ChainSafe.Gaming.RPC.Events
{
public interface IEventManager
{
Task Subscribe<TEvent>(Action<TEvent> handler)
Task Subscribe<TEvent>(Action<TEvent> handler, params string[] contractAddresses)
where TEvent : IEventDTO, new();

Task Unsubscribe<TEvent>(Action<TEvent> handler)
Task Unsubscribe<TEvent>(Action<TEvent> handler, params string[] contractAddresses)
where TEvent : IEventDTO, new();
}
}
Loading
Loading