Skip to content

Commit

Permalink
fix(SDK): provide alternative way of getting unknown assembly types
Browse files Browse the repository at this point in the history
Previously, the SDK Defines would require the use of an assembly name
when attempting to get a type and if it was not found then it would
cause an error.

This fix adds a new method that attempts to get an unknown assembly
type and if it can't be found then it returns `null`.
  • Loading branch information
thestonefox committed Jun 21, 2017
1 parent e6b92f4 commit fa0849a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Assets/VRTK/SDK/Daydream/SDK_DaydreamDefines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class SDK_DaydreamDefines
[SDK_ScriptingDefineSymbolPredicate(ScriptingDefineSymbol, "Android")]
private static bool IsDaydreamAvailable()
{
return typeof(SDK_DaydreamDefines).Assembly.GetType("GvrController") != null;
return VRTK_SharedMethods.GetTypeUnknownAssembly("GvrController") != null;
}
}
}
6 changes: 3 additions & 3 deletions Assets/VRTK/SDK/Oculus/SDK_OculusDefines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ private static bool IsUtilitiesVersion1110OrOlder()
private static bool IsAvatarAvailable()
{
return (IsUtilitiesVersion1120OrNewer() || IsUtilitiesVersion1110OrOlder())
&& typeof(SDK_OculusDefines).Assembly.GetType("OvrAvatar") != null;
&& VRTK_SharedMethods.GetTypeUnknownAssembly("OvrAvatar") != null;
}

private static Version GetOculusWrapperVersion()
{
Type pluginClass = typeof(SDK_OculusDefines).Assembly.GetType("OVRPlugin");
Type pluginClass = VRTK_SharedMethods.GetTypeUnknownAssembly("OVRPlugin");
if (pluginClass == null)
{
return null;
Expand All @@ -63,7 +63,7 @@ private static Version GetOculusWrapperVersion()

private static Version GetOculusRuntimeVersion()
{
Type pluginClass = typeof(SDK_OculusDefines).Assembly.GetType("OVRPlugin");
Type pluginClass = VRTK_SharedMethods.GetTypeUnknownAssembly("OVRPlugin");
if (pluginClass == null)
{
return null;
Expand Down
8 changes: 4 additions & 4 deletions Assets/VRTK/SDK/SteamVR/SDK_SteamVRDefines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class SDK_SteamVRDefines
[SDK_ScriptingDefineSymbolPredicate(SDK_ScriptingDefineSymbolPredicateAttribute.RemovableSymbolPrefix + "STEAMVR_PLUGIN_1_2_1_OR_NEWER", BuildTargetGroupName)]
private static bool IsPluginVersion121OrNewer()
{
Type eventClass = typeof(SDK_SteamVRDefines).Assembly.GetType("SteamVR_Events");
Type eventClass = VRTK_SharedMethods.GetTypeUnknownAssembly("SteamVR_Events");
if (eventClass == null)
{
return false;
Expand All @@ -38,14 +38,14 @@ private static bool IsPluginVersion121OrNewer()
return false;
}

return systemMethodParameters[0].ParameterType == Type.GetType("Valve.VR.EVREventType");
return systemMethodParameters[0].ParameterType == VRTK_SharedMethods.GetTypeUnknownAssembly("Valve.VR.EVREventType");
}

[SDK_ScriptingDefineSymbolPredicate(ScriptingDefineSymbol, BuildTargetGroupName)]
[SDK_ScriptingDefineSymbolPredicate(SDK_ScriptingDefineSymbolPredicateAttribute.RemovableSymbolPrefix + "STEAMVR_PLUGIN_1_2_0", BuildTargetGroupName)]
private static bool IsPluginVersion120()
{
Type eventClass = typeof(SDK_SteamVRDefines).Assembly.GetType("SteamVR_Events");
Type eventClass = VRTK_SharedMethods.GetTypeUnknownAssembly("SteamVR_Events");
if (eventClass == null)
{
return false;
Expand All @@ -70,7 +70,7 @@ private static bool IsPluginVersion120()
[SDK_ScriptingDefineSymbolPredicate(SDK_ScriptingDefineSymbolPredicateAttribute.RemovableSymbolPrefix + "STEAMVR_PLUGIN_1_1_1_OR_OLDER", BuildTargetGroupName)]
private static bool IsPluginVersion111OrOlder()
{
Type utilsClass = typeof(SDK_SteamVRDefines).Assembly.GetType("SteamVR_Utils");
Type utilsClass = VRTK_SharedMethods.GetTypeUnknownAssembly("SteamVR_Utils");
if (utilsClass == null)
{
return false;
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRTK/SDK/Ximmerse/SDK_XimmerseDefines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class SDK_XimmerseDefines
[SDK_ScriptingDefineSymbolPredicate(ScriptingDefineSymbol, "Android")]
private static bool IsXimmerseAvailable()
{
return typeof(SDK_XimmerseDefines).Assembly.GetType("Ximmerse.InputSystem.XDevicePlugin") != null;
return VRTK_SharedMethods.GetTypeUnknownAssembly("Ximmerse.InputSystem.XDevicePlugin") != null;
}
}
}
23 changes: 23 additions & 0 deletions Assets/VRTK/Scripts/Utilities/VRTK_SharedMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,29 @@ public static void SetGlobalScale(this Transform transform, Vector3 globalScale)
transform.localScale = new Vector3(globalScale.x / transform.lossyScale.x, globalScale.y / transform.lossyScale.y, globalScale.z / transform.lossyScale.z);
}

/// <summary>
/// The GetTypeUnknownAssembly method is used to find a Type without knowing the exact assembly it is in.
/// </summary>
/// <param name="typeName">The name of the type to get.</param>
/// <returns>The Type, or null if none is found.</returns>
public static Type GetTypeUnknownAssembly(string typeName)
{
Type type = Type.GetType(typeName);
if (type != null)
{
return type;
}
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
{
type = a.GetType(typeName);
if (type != null)
{
return type;
}
}
return null;
}

#if UNITY_EDITOR
public static BuildTargetGroup[] GetValidBuildTargetGroups()
{
Expand Down
11 changes: 11 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -6940,6 +6940,17 @@ The NumberPercent method is used to determine the percentage of a given value.

The SetGlobalScale method is used to set a transform scale based on a global scale instead of a local scale.

#### GetTypeUnknownAssembly/1

> `public static Type GetTypeUnknownAssembly(string typeName)`
* Parameters
* `string typeName` - The name of the type to get.
* Returns
* `Type` - The Type, or null if none is found.

The GetTypeUnknownAssembly method is used to find a Type without knowing the exact assembly it is in.

---

## Policy List (VRTK_PolicyList)
Expand Down

0 comments on commit fa0849a

Please sign in to comment.