Skip to content

Commit 32b8423

Browse files
committed
fix: Enable additional platforms to be added to PlatformManager via a partial method definition.
1 parent e9fe231 commit 32b8423

File tree

3 files changed

+59
-17
lines changed

3 files changed

+59
-17
lines changed

Assets/Plugins/Source/Editor/Utility/ReflectionUtility.cs renamed to com.playeveryware.eos/Runtime/Core/Common/Utility/ReflectionUtility.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,35 @@
2020
* SOFTWARE.
2121
*/
2222

23-
namespace PlayEveryWare.EpicOnlineServices.Editor.Utility
23+
namespace PlayEveryWare.Common.Utility
2424
{
2525
using System.Collections.Generic;
2626
using System;
2727
using System.Linq;
2828
using System.Reflection;
29+
using PlayEveryWare.EpicOnlineServices;
30+
using System.Runtime.CompilerServices;
2931

3032
public static class ReflectionUtility
3133
{
34+
/// <summary>
35+
/// Calls the static constructors on all classes that derive from the
36+
/// given type.
37+
/// </summary>
38+
/// <param name="baseType">
39+
/// The type for which to call static constructors on.
40+
/// </param>
41+
public static void CallStaticConstructorsOnDerivingClasses<TBase>()
42+
{
43+
var types = Assembly.GetExecutingAssembly().GetTypes()
44+
.Where(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(TBase)));
45+
46+
foreach (var type in types)
47+
{
48+
RuntimeHelpers.RunClassConstructor(type.TypeHandle);
49+
}
50+
}
51+
3252
public static List<object> CreateInstancesOfDerivedGenericClasses(Type genericBaseType)
3353
{
3454
if (!genericBaseType.IsGenericTypeDefinition || !genericBaseType.IsAbstract)

Assets/Plugins/Source/Editor/Utility/ReflectionUtility.cs.meta renamed to com.playeveryware.eos/Runtime/Core/Common/Utility/ReflectionUtility.cs.meta

File renamed without changes.

com.playeveryware.eos/Runtime/Core/PlatformManager.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace PlayEveryWare.EpicOnlineServices
3535
using UnityEngine;
3636
#endif
3737
using Utility;
38+
using PlayEveryWare.Common.Utility;
3839

3940
public static partial class PlatformManager
4041
{
@@ -87,26 +88,14 @@ public static PlatformInfo Create<T>(string fullName, string configFileName, str
8788
}
8889
}
8990

90-
#if !INCLUDE_RESTRICTED_PLATFORMS
9191
/// <summary>
9292
/// Private collection to store information about each platform.
9393
/// </summary>
94-
private static readonly IDictionary<Platform, PlatformInfo> PlatformInformation =
95-
new Dictionary<Platform, PlatformInfo>()
96-
{
97-
#if !EXTERNAL_TO_UNITY
98-
{ Platform.Android, PlatformInfo.Create<AndroidConfig>("Android", "eos_android_config.json", null, "Android")},
99-
{ Platform.iOS, PlatformInfo.Create<IOSConfig> ("iOS", "eos_ios_config.json", null, "iPhone") },
100-
{ Platform.Linux, PlatformInfo.Create<LinuxConfig> ("Linux", "eos_linux_config.json", ".so", "Standalone") },
101-
{ Platform.macOS, PlatformInfo.Create<MacOSConfig> ("macOS", "eos_macos_config.json", ".dylib", "Standalone") },
102-
#endif
103-
{ Platform.Windows, PlatformInfo.Create<WindowsConfig>("Windows", "eos_windows_config.json", ".dll", "Standalone") },
104-
};
105-
#endif
94+
private static IDictionary<Platform, PlatformInfo> PlatformInformation = new Dictionary<Platform, PlatformInfo>();
10695

107-
/// <summary>
108-
/// Backing value for the CurrentPlatform property.
109-
/// </summary>
96+
/// <summary>
97+
/// Backing value for the CurrentPlatform property.
98+
/// </summary>
11099
private static Platform s_CurrentPlatform;
111100

112101
/// <summary>
@@ -139,8 +128,41 @@ public static Platform CurrentPlatform
139128
}
140129
}
141130

131+
/// <summary>
132+
/// To be accessible to the platform manager, the static constructors
133+
/// for each platform config need to be executed, this function ensures
134+
/// that happens.
135+
/// </summary>
136+
private static void InitializePlatformConfigs()
137+
{
138+
// This compile conditional is here because in the editor, it is
139+
// acceptable to use reflection to make sure all the
140+
// PlatformConfigs have their static constructors executed.
141+
#if UNITY_EDITOR
142+
ReflectionUtility.CallStaticConstructorsOnDerivingClasses<PlatformConfig>();
143+
#endif
144+
145+
#if !EXTERNAL_TO_UNITY
146+
PlatformInformation.Add(Platform.Android, PlatformInfo.Create<AndroidConfig>("Android", "eos_android_config.json", null, "Android"));
147+
PlatformInformation.Add(Platform.iOS, PlatformInfo.Create<IOSConfig>("iOS", "eos_ios_config.json", null, "iPhone"));
148+
PlatformInformation.Add(Platform.Linux, PlatformInfo.Create<LinuxConfig>("Linux", "eos_linux_config.json", ".so", "Standalone"));
149+
PlatformInformation.Add(Platform.macOS, PlatformInfo.Create<MacOSConfig>("macOS", "eos_macos_config.json", ".dylib", "Standalone"));
150+
#endif
151+
PlatformInformation.Add(Platform.Windows, PlatformInfo.Create<WindowsConfig>("Windows", "eos_windows_config.json", ".dll", "Standalone"));
152+
}
153+
154+
/// <summary>
155+
/// This partial method is defined here so that a partial class
156+
/// definition can provide implementation that initializes other
157+
/// platform configs.
158+
/// </summary>
159+
static partial void InitializeProprietaryPlatformConfigs();
160+
142161
static PlatformManager()
143162
{
163+
InitializePlatformConfigs();
164+
InitializeProprietaryPlatformConfigs();
165+
144166
// If external to unity, then we know that the current platform
145167
// is Windows.
146168
#if EXTERNAL_TO_UNITY

0 commit comments

Comments
 (0)