Skip to content

Commit

Permalink
Merge pull request #712 from Cysharp/feature/MoveDynamicClientFactory…
Browse files Browse the repository at this point in the history
…Providers

Move DynamicClientFactoryProviders to DynamicClient
  • Loading branch information
mayuki authored Oct 31, 2023
2 parents 75f3916 + 8f1ca6d commit 59890da
Showing 19 changed files with 198 additions and 159 deletions.
13 changes: 0 additions & 13 deletions src/MagicOnion.Abstractions/MagicOnion.Abstractions.csproj
Original file line number Diff line number Diff line change
@@ -22,16 +22,6 @@ MagicOnion.IgnoreAttribute</Description>
<PackageReference Include="MessagePack" />
</ItemGroup>

<!-- Copy files for unity MagicOnion -->
<PropertyGroup>
<DestinationRoot>$(ProjectDir)..\MagicOnion.Client.Unity\Assets\Scripts\MagicOnion\MagicOnion.Abstractions\</DestinationRoot>
</PropertyGroup>
<ItemGroup>
<TargetFiles Include="$(ProjectDir)\**\*.cs" Exclude="**\bin\**\*.*;**\obj\**\*.*;InternalsVisibleTo.cs" />
</ItemGroup>
<ItemGroup>
<TargetFiles Remove="InternalsVisibleTo.cs" />
</ItemGroup>
<ItemGroup>
<None Update="DynamicArgumentTuple.tt">
<LastGenOutput>DynamicArgumentTuple.cs</LastGenOutput>
@@ -48,8 +38,5 @@ MagicOnion.IgnoreAttribute</Description>
<DependentUpon>DynamicArgumentTuple.tt</DependentUpon>
</Compile>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Copy SourceFiles="@(TargetFiles)" DestinationFiles="$(DestinationRoot)\%(RecursiveDir)%(Filename)%(Extension)" SkipUnchangedFiles="true" />
</Target>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace MagicOnion.Client.DynamicClient
{
public class DynamicNotSupportedMagicOnionClientFactoryProvider : IMagicOnionClientFactoryProvider
{
public static IMagicOnionClientFactoryProvider Instance { get; } = new DynamicNotSupportedMagicOnionClientFactoryProvider();

DynamicNotSupportedMagicOnionClientFactoryProvider() { }

public bool TryGetFactory<T>([NotNullWhen(true)] out MagicOnionClientFactoryDelegate<T>? factory) where T : IService<T>
{
throw new InvalidOperationException($"Unable to find a client factory of type '{typeof(T)}'. If the application is running on IL2CPP or AOT, dynamic code generation is not supported. Please use the code generator (moc).");
}
}

#if ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)
/// <summary>
/// Provides to get a MagicOnionClient factory of the specified service type. The provider is backed by DynamicMagicOnionClientBuilder.
/// </summary>
public class DynamicMagicOnionClientFactoryProvider : IMagicOnionClientFactoryProvider
{
public static IMagicOnionClientFactoryProvider Instance { get; } = new DynamicMagicOnionClientFactoryProvider();

DynamicMagicOnionClientFactoryProvider() { }

public bool TryGetFactory<T>([NotNullWhen(true)] out MagicOnionClientFactoryDelegate<T>? factory) where T : IService<T>
{
factory = Cache<T>.Factory;
return true;
}

static class Cache<T> where T : IService<T>
{
public static readonly MagicOnionClientFactoryDelegate<T> Factory
= (clientOptions, serializerProvider) => (T)Activator.CreateInstance(DynamicClientBuilder<T>.ClientType, clientOptions, serializerProvider)!;
}
}
#endif
}

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,39 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace MagicOnion.Client.DynamicClient
{
public class DynamicNotSupportedStreamingHubClientFactoryProvider : IStreamingHubClientFactoryProvider
{
public static IStreamingHubClientFactoryProvider Instance { get; } = new DynamicNotSupportedStreamingHubClientFactoryProvider();

DynamicNotSupportedStreamingHubClientFactoryProvider() { }

public bool TryGetFactory<TStreamingHub, TReceiver>([NotNullWhen(true)] out StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver>? factory) where TStreamingHub : IStreamingHub<TStreamingHub, TReceiver>
{
throw new InvalidOperationException($"Unable to find a client factory of type '{typeof(TStreamingHub)}'. If the application is running on IL2CPP or AOT, dynamic code generation is not supported. Please use the code generator (moc).");
}
}

#if ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)
public class DynamicStreamingHubClientFactoryProvider : IStreamingHubClientFactoryProvider
{
public static IStreamingHubClientFactoryProvider Instance { get; } = new DynamicStreamingHubClientFactoryProvider();

DynamicStreamingHubClientFactoryProvider() { }

public bool TryGetFactory<TStreamingHub, TReceiver>([NotNullWhen(true)] out StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver>? factory) where TStreamingHub : IStreamingHub<TStreamingHub, TReceiver>
{
factory = Cache<TStreamingHub, TReceiver>.Factory;
return true;
}

static class Cache<TStreamingHub, TReceiver> where TStreamingHub : IStreamingHub<TStreamingHub, TReceiver>
{
public static readonly StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver> Factory
= (callInvoker, receiver, host, callOptions, serializerProvider, logger)
=> (TStreamingHub)Activator.CreateInstance(DynamicStreamingHubClientBuilder<TStreamingHub, TReceiver>.ClientType, callInvoker, host, callOptions, serializerProvider, logger)!;
}
}
#endif
}

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
@@ -15,9 +15,9 @@ public static class MagicOnionClientFactoryProvider
/// </summary>
public static IMagicOnionClientFactoryProvider Default { get; set; }
#if ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)
= DynamicMagicOnionClientFactoryProvider.Instance;
= DynamicClient.DynamicMagicOnionClientFactoryProvider.Instance;
#else
= DynamicNotSupportedMagicOnionClientFactoryProvider.Instance;
= DynamicClient.DynamicNotSupportedMagicOnionClientFactoryProvider.Instance;
#endif
}

@@ -65,40 +65,4 @@ public bool TryGetFactory<T>([NotNullWhen(true)] out MagicOnionClientFactoryDele
return false;
}
}

public class DynamicNotSupportedMagicOnionClientFactoryProvider : IMagicOnionClientFactoryProvider
{
public static IMagicOnionClientFactoryProvider Instance { get; } = new DynamicNotSupportedMagicOnionClientFactoryProvider();

DynamicNotSupportedMagicOnionClientFactoryProvider() { }

public bool TryGetFactory<T>([NotNullWhen(true)] out MagicOnionClientFactoryDelegate<T>? factory) where T : IService<T>
{
throw new InvalidOperationException($"Unable to find a client factory of type '{typeof(T)}'. If the application is running on IL2CPP or AOT, dynamic code generation is not supported. Please use the code generator (moc).");
}
}

#if ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)
/// <summary>
/// Provides to get a MagicOnionClient factory of the specified service type. The provider is backed by DynamicMagicOnionClientBuilder.
/// </summary>
public class DynamicMagicOnionClientFactoryProvider : IMagicOnionClientFactoryProvider
{
public static IMagicOnionClientFactoryProvider Instance { get; } = new DynamicMagicOnionClientFactoryProvider();

DynamicMagicOnionClientFactoryProvider() { }

public bool TryGetFactory<T>([NotNullWhen(true)] out MagicOnionClientFactoryDelegate<T>? factory) where T : IService<T>
{
factory = Cache<T>.Factory;
return true;
}

static class Cache<T> where T : IService<T>
{
public static readonly MagicOnionClientFactoryDelegate<T> Factory
= (clientOptions, serializerProvider) => (T)Activator.CreateInstance(DynamicClient.DynamicClientBuilder<T>.ClientType, clientOptions, serializerProvider)!;
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -16,9 +16,9 @@ public static class StreamingHubClientFactoryProvider
/// </summary>
public static IStreamingHubClientFactoryProvider Default { get; set; }
#if ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)
= DynamicStreamingHubClientFactoryProvider.Instance;
= DynamicClient.DynamicStreamingHubClientFactoryProvider.Instance;
#else
= DynamicNotSupportedStreamingHubClientFactoryProvider.Instance;
= DynamicClient.DynamicNotSupportedStreamingHubClientFactoryProvider.Instance;
#endif
}

@@ -69,37 +69,4 @@ public bool TryGetFactory<TStreamingHub, TReceiver>([NotNullWhen(true)] out Stre
}
}

public class DynamicNotSupportedStreamingHubClientFactoryProvider : IStreamingHubClientFactoryProvider
{
public static IStreamingHubClientFactoryProvider Instance { get; } = new DynamicNotSupportedStreamingHubClientFactoryProvider();

DynamicNotSupportedStreamingHubClientFactoryProvider() { }

public bool TryGetFactory<TStreamingHub, TReceiver>([NotNullWhen(true)] out StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver>? factory) where TStreamingHub : IStreamingHub<TStreamingHub, TReceiver>
{
throw new InvalidOperationException($"Unable to find a client factory of type '{typeof(TStreamingHub)}'. If the application is running on IL2CPP or AOT, dynamic code generation is not supported. Please use the code generator (moc).");
}
}

#if ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)
public class DynamicStreamingHubClientFactoryProvider : IStreamingHubClientFactoryProvider
{
public static IStreamingHubClientFactoryProvider Instance { get; } = new DynamicStreamingHubClientFactoryProvider();

DynamicStreamingHubClientFactoryProvider() { }

public bool TryGetFactory<TStreamingHub, TReceiver>([NotNullWhen(true)] out StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver>? factory) where TStreamingHub : IStreamingHub<TStreamingHub, TReceiver>
{
factory = Cache<TStreamingHub, TReceiver>.Factory;
return true;
}

static class Cache<TStreamingHub, TReceiver> where TStreamingHub : IStreamingHub<TStreamingHub, TReceiver>
{
public static readonly StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver> Factory
= (callInvoker, receiver, host, callOptions, serializerProvider, logger)
=> (TStreamingHub)Activator.CreateInstance(DynamicClient.DynamicStreamingHubClientBuilder<TStreamingHub, TReceiver>.ClientType, callInvoker, host, callOptions, serializerProvider, logger)!;
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace MagicOnion.Client.DynamicClient
{
public class DynamicNotSupportedMagicOnionClientFactoryProvider : IMagicOnionClientFactoryProvider
{
public static IMagicOnionClientFactoryProvider Instance { get; } = new DynamicNotSupportedMagicOnionClientFactoryProvider();

DynamicNotSupportedMagicOnionClientFactoryProvider() { }

public bool TryGetFactory<T>([NotNullWhen(true)] out MagicOnionClientFactoryDelegate<T>? factory) where T : IService<T>
{
throw new InvalidOperationException($"Unable to find a client factory of type '{typeof(T)}'. If the application is running on IL2CPP or AOT, dynamic code generation is not supported. Please use the code generator (moc).");
}
}

#if ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)
/// <summary>
/// Provides to get a MagicOnionClient factory of the specified service type. The provider is backed by DynamicMagicOnionClientBuilder.
/// </summary>
public class DynamicMagicOnionClientFactoryProvider : IMagicOnionClientFactoryProvider
{
public static IMagicOnionClientFactoryProvider Instance { get; } = new DynamicMagicOnionClientFactoryProvider();

DynamicMagicOnionClientFactoryProvider() { }

public bool TryGetFactory<T>([NotNullWhen(true)] out MagicOnionClientFactoryDelegate<T>? factory) where T : IService<T>
{
factory = Cache<T>.Factory;
return true;
}

static class Cache<T> where T : IService<T>
{
public static readonly MagicOnionClientFactoryDelegate<T> Factory
= (clientOptions, serializerProvider) => (T)Activator.CreateInstance(DynamicClientBuilder<T>.ClientType, clientOptions, serializerProvider)!;
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace MagicOnion.Client.DynamicClient
{
public class DynamicNotSupportedStreamingHubClientFactoryProvider : IStreamingHubClientFactoryProvider
{
public static IStreamingHubClientFactoryProvider Instance { get; } = new DynamicNotSupportedStreamingHubClientFactoryProvider();

DynamicNotSupportedStreamingHubClientFactoryProvider() { }

public bool TryGetFactory<TStreamingHub, TReceiver>([NotNullWhen(true)] out StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver>? factory) where TStreamingHub : IStreamingHub<TStreamingHub, TReceiver>
{
throw new InvalidOperationException($"Unable to find a client factory of type '{typeof(TStreamingHub)}'. If the application is running on IL2CPP or AOT, dynamic code generation is not supported. Please use the code generator (moc).");
}
}

#if ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)
public class DynamicStreamingHubClientFactoryProvider : IStreamingHubClientFactoryProvider
{
public static IStreamingHubClientFactoryProvider Instance { get; } = new DynamicStreamingHubClientFactoryProvider();

DynamicStreamingHubClientFactoryProvider() { }

public bool TryGetFactory<TStreamingHub, TReceiver>([NotNullWhen(true)] out StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver>? factory) where TStreamingHub : IStreamingHub<TStreamingHub, TReceiver>
{
factory = Cache<TStreamingHub, TReceiver>.Factory;
return true;
}

static class Cache<TStreamingHub, TReceiver> where TStreamingHub : IStreamingHub<TStreamingHub, TReceiver>
{
public static readonly StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver> Factory
= (callInvoker, receiver, host, callOptions, serializerProvider, logger)
=> (TStreamingHub)Activator.CreateInstance(DynamicStreamingHubClientBuilder<TStreamingHub, TReceiver>.ClientType, callInvoker, host, callOptions, serializerProvider, logger)!;
}
}
#endif
}
40 changes: 2 additions & 38 deletions src/MagicOnion.Client/MagicOnionClientFactoryProvider.cs
Original file line number Diff line number Diff line change
@@ -15,9 +15,9 @@ public static class MagicOnionClientFactoryProvider
/// </summary>
public static IMagicOnionClientFactoryProvider Default { get; set; }
#if ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)
= DynamicMagicOnionClientFactoryProvider.Instance;
= DynamicClient.DynamicMagicOnionClientFactoryProvider.Instance;
#else
= DynamicNotSupportedMagicOnionClientFactoryProvider.Instance;
= DynamicClient.DynamicNotSupportedMagicOnionClientFactoryProvider.Instance;
#endif
}

@@ -65,40 +65,4 @@ public bool TryGetFactory<T>([NotNullWhen(true)] out MagicOnionClientFactoryDele
return false;
}
}

public class DynamicNotSupportedMagicOnionClientFactoryProvider : IMagicOnionClientFactoryProvider
{
public static IMagicOnionClientFactoryProvider Instance { get; } = new DynamicNotSupportedMagicOnionClientFactoryProvider();

DynamicNotSupportedMagicOnionClientFactoryProvider() { }

public bool TryGetFactory<T>([NotNullWhen(true)] out MagicOnionClientFactoryDelegate<T>? factory) where T : IService<T>
{
throw new InvalidOperationException($"Unable to find a client factory of type '{typeof(T)}'. If the application is running on IL2CPP or AOT, dynamic code generation is not supported. Please use the code generator (moc).");
}
}

#if ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)
/// <summary>
/// Provides to get a MagicOnionClient factory of the specified service type. The provider is backed by DynamicMagicOnionClientBuilder.
/// </summary>
public class DynamicMagicOnionClientFactoryProvider : IMagicOnionClientFactoryProvider
{
public static IMagicOnionClientFactoryProvider Instance { get; } = new DynamicMagicOnionClientFactoryProvider();

DynamicMagicOnionClientFactoryProvider() { }

public bool TryGetFactory<T>([NotNullWhen(true)] out MagicOnionClientFactoryDelegate<T>? factory) where T : IService<T>
{
factory = Cache<T>.Factory;
return true;
}

static class Cache<T> where T : IService<T>
{
public static readonly MagicOnionClientFactoryDelegate<T> Factory
= (clientOptions, serializerProvider) => (T)Activator.CreateInstance(DynamicClient.DynamicClientBuilder<T>.ClientType, clientOptions, serializerProvider)!;
}
}
#endif
}
Loading

0 comments on commit 59890da

Please sign in to comment.