-
-
Notifications
You must be signed in to change notification settings - Fork 260
Decoupled ParseObject subclassing #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using System; | ||
|
|
||
| namespace System { | ||
| /// <summary> | ||
| /// Unity does not have an API for GetTypeInfo(), instead they expose most of the methods | ||
| /// on System.Reflection.TypeInfo on the type itself. This poses a problem for compatibility | ||
| /// with the rest of the C# world, as we expect the result of GetTypeInfo() to be an actual TypeInfo, | ||
| /// as well as be able to be converted back to a type using AsType(). | ||
| /// | ||
| /// This class simply implements some of the simple missing methods on Type to make it as API-compatible | ||
| /// as possible to TypeInfo. | ||
| /// </summary> | ||
| internal static class TypeExtensions { | ||
| public static Type AsType(this Type type) { | ||
| return type; | ||
| } | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
Parse/Internal/Object/Subclassing/IObjectSubclassingController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Parse.Internal { | ||
| internal interface IObjectSubclassingController { | ||
| String GetClassName(Type type); | ||
| Type GetType(String className); | ||
|
|
||
| bool IsTypeValid(String className, Type type); | ||
|
|
||
| void RegisterSubclass(Type t); | ||
| void UnregisterSubclass(Type t); | ||
|
|
||
| ParseObject Instantiate(String className); | ||
| IDictionary<String, String> GetPropertyMappings(String className); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Reflection; | ||
|
|
||
| #if UNITY | ||
| using TypeInfo = System.Type; | ||
| #endif | ||
|
|
||
| namespace Parse.Internal { | ||
| internal class ObjectSubclassInfo { | ||
| public ObjectSubclassInfo(Type type, ConstructorInfo constructor) { | ||
| TypeInfo = type.GetTypeInfo(); | ||
| ClassName = GetClassName(TypeInfo); | ||
| Constructor = constructor; | ||
| PropertyMappings = type.GetProperties() | ||
| .Select(prop => Tuple.Create(prop, prop.GetCustomAttribute<ParseFieldNameAttribute>(true))) | ||
| .Where(t => t.Item2 != null) | ||
| .Select(t => Tuple.Create(t.Item1, t.Item2.FieldName)) | ||
| .ToDictionary(t => t.Item1.Name, t => t.Item2); | ||
| } | ||
|
|
||
| public TypeInfo TypeInfo { get; private set; } | ||
| public String ClassName { get; private set; } | ||
| public IDictionary<String, String> PropertyMappings { get; private set; } | ||
| private ConstructorInfo Constructor { get; set; } | ||
|
|
||
| public ParseObject Instantiate() { | ||
| return (ParseObject)Constructor.Invoke(null); | ||
| } | ||
|
|
||
| internal static String GetClassName(TypeInfo type) { | ||
| var attribute = type.GetCustomAttribute<ParseClassNameAttribute>(); | ||
| return attribute != null ? attribute.ClassName : null; | ||
| } | ||
| } | ||
| } |
127 changes: 127 additions & 0 deletions
127
Parse/Internal/Object/Subclassing/ObjectSubclassingController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Threading; | ||
|
|
||
| #if UNITY | ||
| using TypeInfo = System.Type; | ||
| #endif | ||
|
|
||
| namespace Parse.Internal { | ||
| internal class ObjectSubclassingController : IObjectSubclassingController { | ||
| private readonly ReaderWriterLockSlim mutex; | ||
| private readonly IDictionary<String, ObjectSubclassInfo> registeredSubclasses; | ||
| private IDictionary<String, Action> registerActions; | ||
|
|
||
| public ObjectSubclassingController(IDictionary<Type, Action> actions) { | ||
| mutex = new ReaderWriterLockSlim(); | ||
| registeredSubclasses = new Dictionary<String, ObjectSubclassInfo>(); | ||
| registerActions = actions.ToDictionary(p => GetClassName(p.Key), p => p.Value); | ||
| } | ||
|
|
||
| public String GetClassName(Type type) { | ||
| return ObjectSubclassInfo.GetClassName(type.GetTypeInfo()); | ||
| } | ||
|
|
||
| public Type GetType(String className) { | ||
| ObjectSubclassInfo info = null; | ||
| mutex.EnterReadLock(); | ||
| registeredSubclasses.TryGetValue(className, out info); | ||
| mutex.ExitReadLock(); | ||
|
|
||
| return info != null | ||
| ? info.TypeInfo.AsType() | ||
| : null; | ||
| } | ||
|
|
||
| public bool IsTypeValid(String className, Type type) { | ||
| ObjectSubclassInfo subclassInfo = null; | ||
|
|
||
| mutex.EnterReadLock(); | ||
| registeredSubclasses.TryGetValue(className, out subclassInfo); | ||
| mutex.ExitReadLock(); | ||
|
|
||
| return subclassInfo == null | ||
| ? type == typeof(ParseObject) | ||
| : subclassInfo.TypeInfo == type.GetTypeInfo(); | ||
| } | ||
|
|
||
| public void RegisterSubclass(Type type) { | ||
| TypeInfo typeInfo = type.GetTypeInfo(); | ||
| if (!typeInfo.IsSubclassOf(typeof(ParseObject))) { | ||
| throw new ArgumentException("Cannot register a type that is not a subclass of ParseObject"); | ||
| } | ||
|
|
||
| String className = ObjectSubclassInfo.GetClassName(typeInfo); | ||
|
|
||
| try { | ||
| // Perform this as a single independent transaction, so we can never get into an | ||
| // intermediate state where we *theoretically* register the wrong class due to a | ||
| // TOCTTOU bug. | ||
| mutex.EnterWriteLock(); | ||
|
|
||
| ObjectSubclassInfo previousInfo = null; | ||
| if (registeredSubclasses.TryGetValue(className, out previousInfo)) { | ||
| if (typeInfo.IsAssignableFrom(previousInfo.TypeInfo)) { | ||
| // Previous subclass is more specific or equal to the current type, do nothing. | ||
| return; | ||
| } else if (previousInfo.TypeInfo.IsAssignableFrom(typeInfo)) { | ||
| // Previous subclass is parent of new child, fallthrough and actually register | ||
| // this class. | ||
| /* Do nothing */ | ||
| } else { | ||
| throw new ArgumentException( | ||
| "Tried to register both " + previousInfo.TypeInfo.FullName + " and " + typeInfo.FullName + | ||
| " as the ParseObject subclass of " + className + ". Cannot determine the right class " + | ||
| "to use because neither inherits from the other." | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| ConstructorInfo constructor = type.FindConstructor(); | ||
| if (constructor == null) { | ||
| throw new ArgumentException("Cannot register a type that does not implement the default constructor!"); | ||
| } | ||
|
|
||
| registeredSubclasses[className] = new ObjectSubclassInfo(type, constructor); | ||
| } finally { | ||
| mutex.ExitWriteLock(); | ||
| } | ||
|
|
||
| Action toPerform = null; | ||
| if (registerActions.TryGetValue(className, out toPerform)) { | ||
| toPerform(); | ||
| } | ||
| } | ||
|
|
||
| public void UnregisterSubclass(Type type) { | ||
| mutex.EnterWriteLock(); | ||
| registeredSubclasses.Remove(GetClassName(type)); | ||
| mutex.ExitWriteLock(); | ||
| } | ||
|
|
||
| public ParseObject Instantiate(String className) { | ||
| ObjectSubclassInfo info = null; | ||
|
|
||
| mutex.EnterReadLock(); | ||
| registeredSubclasses.TryGetValue(className, out info); | ||
| mutex.ExitReadLock(); | ||
|
|
||
| return info != null | ||
| ? info.Instantiate() | ||
| : new ParseObject(className); | ||
| } | ||
|
|
||
| public IDictionary<String, String> GetPropertyMappings(String className) { | ||
| ObjectSubclassInfo info = null; | ||
| mutex.EnterReadLock(); | ||
| registeredSubclasses.TryGetValue(className, out info); | ||
| mutex.ExitReadLock(); | ||
|
|
||
| return info != null | ||
| ? info.PropertyMappings | ||
| : null; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah. I see it. This is good.