/// <summary> /// Adds a cross-scene reference. /// </summary> /// <param name="reference"></param> public void AddReference( RuntimeCrossSceneReference reference ) { int index = _crossSceneReferences.FindIndex( reference.IsSameSource ); if ( index >= 0 ) { _crossSceneReferences[index] = reference; } else { _crossSceneReferences.Add( reference ); } }
/// <summary> /// Adds a cross-scene reference. /// </summary> /// <param name="reference"></param> public void AddReference(RuntimeCrossSceneReference reference) { int index = _crossSceneReferences.FindIndex(reference.IsSameSource); if (index >= 0) { _crossSceneReferences[index] = reference; } else { _crossSceneReferences.Add(reference); } }
public bool IsSameSource( RuntimeCrossSceneReference other ) { try { return (this.fromObject == other.fromObject) && (this._sourceField == other._sourceField); } catch ( System.Exception ex ) { AmsDebug.Log( null, "IsSameSource: Could not compare: {0} and {1}: {2}", ToString(), other, ex ); } return false; }
/// <summary> /// Perform a resolve on a cross-scene reference. /// This functions throws an exception if it fails. /// </summary> public static void Resolve( RuntimeCrossSceneReference xRef ) { var fromObject = xRef.fromObject; if ( !fromObject ) throw new ResolveException( string.Format( "Cross-Scene Ref: {0}. fromObject is null.", xRef ) ); Object toObject = xRef.toObject; if ( !toObject ) throw new ResolveException( string.Format( "Cross-Scene Ref: {0}. Could not Resolve toObject {1}", xRef, toObject ) ); // Try all of the resolvers (oldest last) for ( int i = _resolvers.Count - 1; i >= 0; --i ) { if ( _resolvers[i]( xRef ) ) break; } }
public bool IsSameSource( RuntimeCrossSceneReference other ) { return other._fromObject.Equals(this._fromObject) && other._fromField == this._fromField; }
/// <summary> /// Resolve a cross-scene reference piecewise. This does the heavy lifting of figuring out how to parse the path. /// </summary> /// <param name="fromObject">The object that is the source of the cross-scene reference</param> /// <param name="toObject">The object that the cross-scene reference is referring to</param> /// <param name="fromFieldPath">The path of the field that fromObject uses to point to</param> /// <param name="debugThis">Debug information about which cross-scene reference this is coming from</param> private static void ResolveInternal( System.Object fromObject, Object toObject, string fromFieldPath, RuntimeCrossSceneReference debugThis ) { // Sub-object path is indicated by a dot string[] splitPaths = fromFieldPath.Split('.'); // Since the property is of the form: field1.field2.arrayName,arrayIndex.final_field or simply final_field, we need to chase // the property down the rabbit hole starting with the base fromObject and going all the way to final_field. for (int i = 0 ; i < splitPaths.Length - 1 ; ++i) { try { fromObject = GetObjectFromField( fromObject, splitPaths[i] ); if ( fromObject == null ) { throw new ResolveException( string.Format("Cross-Scene Ref: {0}. Could not follow path {1} because {2} was null", debugThis, fromFieldPath, splitPaths[i]) ); } else if ( !fromObject.GetType().IsClass ) { throw new ResolveException( string.Format("Cross-Scene Ref: {0}. Could not follow path {1} because {2} was not a class (probably a struct). This is unsupported.", debugThis, fromFieldPath, splitPaths[i]) ); } } catch ( System.Exception ex ) { throw new ResolveException( string.Format("Cross-Scene Ref: {0}. {1}", debugThis, ex.Message) ); } } // Finally, get the final field. FieldInfo field; int arrayIndex; string fieldName = splitPaths[ splitPaths.Length-1 ]; if ( !GetFieldFromObject(fromObject, fieldName, out field, out arrayIndex ) ) throw new ResolveException( string.Format("Cross-Scene Ref: {0}. Could not parse piece of path {1} from {2}", debugThis, fieldName, fromFieldPath) ); // Now we can finally assign it! AssignField( fromObject, toObject, field, arrayIndex ); }
/// <summary> /// The default resolver will set the source's field to point to the destination /// </summary> private static bool DefaultResolve( RuntimeCrossSceneReference xRef ) { ResolveToField( xRef.fromObject, xRef.toObject, xRef.sourceField, xRef ); return true; }
public static void EditorOnly_ResolveToField( System.Object fromObject, Object toObject, string fromFieldPath, RuntimeCrossSceneReference debugThis ) { ResolveToField( fromObject, toObject, fromFieldPath, debugThis ); }
public bool IsSameSource( RuntimeCrossSceneReference other ) { return other._fromObject.Equals(this._fromObject) && other._fromField == this._fromField; }
/// <summary> /// Resolve a cross-scene reference piecewise. This does the heavy lifting of figuring out how to parse the path. /// </summary> /// <param name="fromObject">The object that is the source of the cross-scene reference</param> /// <param name="toObject">The object that the cross-scene reference is referring to</param> /// <param name="fromFieldPath">The path of the field that fromObject uses to point to</param> /// <param name="debugThis">Debug information about which cross-scene reference this is coming from</param> private static void ResolveInternal( System.Object fromObject, Object toObject, string fromFieldPath, RuntimeCrossSceneReference debugThis ) { // Sub-object path is indicated by a dot string[] splitPaths = fromFieldPath.Split('.'); // Since the property is of the form: field1.field2.arrayName,arrayIndex.final_field or simply final_field, we need to chase // the property down the rabbit hole starting with the base fromObject and going all the way to final_field. for (int i = 0 ; i < splitPaths.Length - 1 ; ++i) { try { fromObject = GetObjectFromField( fromObject, splitPaths[i] ); if ( fromObject == null ) { throw new ResolveException( string.Format("Cross-Scene Ref: {0}. Could not follow path {1} because {2} was null", debugThis, fromFieldPath, splitPaths[i]) ); } else if ( !fromObject.GetType().IsClass ) { throw new ResolveException( string.Format("Cross-Scene Ref: {0}. Could not follow path {1} because {2} was not a class (probably a struct). This is unsupported.", debugThis, fromFieldPath, splitPaths[i]) ); } } catch ( System.Exception ex ) { throw new ResolveException( string.Format("Cross-Scene Ref: {0}. {1}", debugThis, ex.Message) ); } } // Finally, get the final field. FieldInfo field; int arrayIndex; string fieldName = splitPaths[ splitPaths.Length-1 ]; if ( !GetFieldFromObject(fromObject, fieldName, out field, out arrayIndex ) ) throw new ResolveException( string.Format("Cross-Scene Ref: {0}. Could not parse piece of path {1} from {2}", debugThis, fieldName, fromFieldPath) ); // Now we can finally assign it! AssignField( fromObject, toObject, field, arrayIndex ); }