Exemple #1
0
            private IValue GetElementValue(IValueReference <TValue> elementReference, IValueFetchOptions options)
            {
                try
                {
                    var elementRole = elementReference.AsObjectSafe(options);
                    if (elementRole == null)
                    {
                        return(null);
                    }

                    var isNameFromValue = true;
                    var name            = elementRole.GetInstancePropertyReference("name", true)?.AsStringSafe(options)
                                          ?.GetString();
                    if (name == null)
                    {
                        name            = "Game Object";
                        isNameFromValue = false;
                    }

                    // Tell the value presenter to hide the name field, if we're using it for the key. Also hide the default
                    // type presentation - we know it's a GameObject, it's under a group called "Game Objects"
                    return(new CalculatedValueReferenceDecorator <TValue>(elementRole.ValueReference,
                                                                          myValueServices.RoleFactory, name, !isNameFromValue, false).ToValue(myValueServices));
                }
                catch (Exception e)
                {
                    // We must always return a value, as we're effectively showing the contents of an array here. We're
                    // possibly also being evaluated lazily, thanks to chunked arrays, so can't rely on the caller
                    // catching exceptions.
                    myLogger.LogExceptionSilently(e);
                    return(myValueServices.ValueRenderers.GetValueStubForException(e, "Game Object",
                                                                                   elementReference.OriginatingFrame) as IValue
                           ?? new ErrorValue("Game Object", "Error retrieving child game object"));
                }
            }
Exemple #2
0
        /// <summary>
        /// Sets Item: variable or type
        /// </summary>
        /// <param name="id">item's id</param>
        /// <param name="value">value</param>
        public virtual void SetItem(string id, object value)
        {
            IValueReference reference = value as IValueReference;

            if (reference != null)
            {
                _vars[id] = reference;

                //if (_vars.ContainsKey(id))
                //  _vars[id] = reference;
                //else
                //  _vars.Add(id, reference);

                return;
            }

            if (_vars.TryGetValue(id, out reference))
            {
                reference.Value = value;
            }
            else
            {
                _vars.Add(id, new ValueReference(id, value)
                {
                    Scope = this
                });
            }
        }
Exemple #3
0
        private void AssignIdentifier(object value, IScriptContext context)
        {
            if (IsGlobal)
            {
                SetToParentScope(context.Scope.Parent, _identifier, value);
                _variable = null;
                return;
            }

            var scope = context.Scope as LocalScope;

            if (IsVar && scope != null)
            {
                scope.CreateVariable(_identifier, value);
                return;
            }

            if (_variable != null)
            {
                _variable.Value = value;
            }
            else
            {
                context.SetItem(_identifier, value);

                object tmp;
                _variable = CreateRef(_identifier, context, false, out tmp);
            }
        }
Exemple #4
0
        private IValueReference CreateRef(string id, IScriptContext context, bool resolve, out object value)
        {
            IValueReference result = context.Ref(id);

            value = RuntimeHost.NoVariable;

            if (result != null)
            {
                result.Removed += ReferenceRemoved;

                if (resolve)
                {
                    value = result.Value;
                }
            }
            else
            {
                if (resolve)
                {
                    value = context.GetItem(_identifier, false);
                }
            }

            return(result);
        }
Exemple #5
0
        /// <summary>
        /// Store the loaded value in cache.
        /// </summary>
        /// <param name="key">
        /// The key associated with the value.
        /// </param>
        /// <param name="old_value_reference">
        /// A <see cref="LoadingValueReference{T}"/> that was used to load the
        /// value.
        /// </param>
        /// <param name="new_value">
        /// The value to be stored in cache.
        /// </param>
        /// <returns>
        /// <c>true</c> if the value was successfully stored in cache; otherwise,
        /// false.
        /// </returns>
        /// <remarks>
        /// This method is called to store the result of the loading computation.
        /// If the value was sucessfully loaded it will be stored in cache using
        /// the given key.
        /// <para>
        /// This method fails to store the value if a value for the given key was
        /// already replaced by another item, while it is loading.
        /// </para>
        /// </remarks>
        bool StoreLoadedValue(string key,
                              LoadingValueReference <T> old_value_reference, T new_value)
        {
            lock (mutex_) {
                long           now = Clock.NanoTime;
                CacheEntry <T> entry;
                if (cache_provider_.Get(CacheKey(key), out entry))
                {
                    IValueReference <T> value_reference = entry.ValueReference;
                    if (old_value_reference == value_reference)
                    {
                        // If the old value is still active notify the caller that we
                        // are replacing it.
                        if (old_value_reference.IsActive)
                        {
                            // TODO(neylor.silva): Notify the caller about the removal cause
                            // of the item from the cache(REPLACED).
                        }
                        SetValue(entry, key, new_value, now);
                        return(true);
                    }

                    // the loaded value was already clobbered.
                    // TODO(neylor.silva): Notify the caller about the removal cause
                    // of the item from the cache(REPLACED).
                    return(false);
                }

                // an entry for the given key does not exist yet, create a new one.
                entry = new CacheEntry <T>(key);
                SetValue(entry, key, new_value, now);
                return(true);
            }
        }
Exemple #6
0
 /// <summary>
 /// Remove the loading value reference from the cache.
 /// </summary>
 /// <param name="key">The key associated with the loading value.</param>
 /// <param name="value_reference">The value reference to be removed from
 /// the cache.</param>
 /// <returns>
 /// <c>true</c> if the value is succesfully removed from the cache;
 /// otherwise, false.
 /// </returns>
 /// <remarks>
 /// If a loading value reference has an active value means that the loading
 /// process has failed and the old valud is still valid, in that case this
 /// method will replace the loading value reference with the old value.
 /// </remarks>
 bool RemoveLoadingValue(string key, LoadingValueReference <T> value_reference)
 {
     lock (mutex_) {
         CacheEntry <T> entry;
         if (cache_provider_.Get(CacheKey(key), out entry))
         {
             IValueReference <T> value = entry.ValueReference;
             if (value == value_reference)
             {
                 // If a loading value is active means that the new value has failed
                 // to load and the old value is still valid.
                 if (value_reference.IsActive)
                 {
                     entry.ValueReference = value_reference.OldValue;
                 }
                 else
                 {
                     cache_provider_.Remove(CacheKey(key));
                 }
                 return(true);
             }
         }
         return(false);
     }
 }
Exemple #7
0
        /// <summary>
        /// Creates a new <see cref="LoadingValueReference{T}"/> and inserts it on
        /// the cache by using the <paramref name="key"/>.
        /// </summary>
        /// <param name="key">The key that will be associated with the newly
        /// created <see cref="LoadingValueReference{T}"/>.</param>
        /// <returns>The newly inserted <see cref="LoadingValueReference{T}"/>, or
        /// null if the live value reference is already loading.</returns>
        LoadingValueReference <T> InsertLoadingValueReference(string key)
        {
            lock (mutex_) {
                long now = Clock.NanoTime;

                LoadingValueReference <T> loading_value_reference;

                // look for an existing entry
                CacheEntry <T> entry;
                if (cache_provider_.Get(CacheKey(key), out entry))
                {
                    IValueReference <T> value_reference = entry.ValueReference;
                    if (value_reference.IsLoading)
                    {
                        // refresh is a no-op if loading is pending.
                        return(null);
                    }

                    // continue returning old value while loading
                    loading_value_reference = new LoadingValueReference <T>(value_reference);
                    entry.ValueReference    = loading_value_reference;
                    return(loading_value_reference);
                }

                loading_value_reference = new LoadingValueReference <T>();
                entry = new CacheEntry <T>(key);
                entry.ValueReference = loading_value_reference;

                // send the entry to the cache provider.
                cache_provider_.Set(CacheKey(key), entry);
                return(loading_value_reference);
            }
        }
Exemple #8
0
            private string GetComponentName(IValueReference <TValue> componentValue,
                                            [CanBeNull] IReifiedType <TValue> objectNamesType,
                                            [CanBeNull] IMetadataMethodLite getInspectorTitleMethod,
                                            IStackFrame frame,
                                            IValueFetchOptions options, IValueServicesFacade <TValue> services)
            {
                if (objectNamesType != null && getInspectorTitleMethod != null)
                {
                    try
                    {
                        var inspectorTitle = objectNamesType.CallStaticMethod(frame, options, getInspectorTitleMethod,
                                                                              componentValue.GetValue(options));
                        var stringValueRole =
                            new SimpleValueReference <TValue>(inspectorTitle, frame, services.RoleFactory)
                            .AsStringSafe(options);
                        if (stringValueRole != null)
                        {
                            return(stringValueRole.GetString());
                        }
                    }
                    catch (Exception e)
                    {
                        myLogger.Error(e, "Unable to fetch object names for {0}", componentValue);
                    }
                }

                return(componentValue.GetPrimaryRole(options).ReifiedType.MetadataType.ShortName);
            }
Exemple #9
0
 public GameObjectChildrenGroup(IValueReference <TValue> transformReference,
                                IValueServicesFacade <TValue> services)
     : base("Children")
 {
     myTransformReference = transformReference;
     myServices           = services;
 }
Exemple #10
0
 public ExtraDetailValueReferenceDecorator([NotNull] IValueReference <TValue> valueReferenceImplementation,
                                           IValueRoleFactory <TValue> roleFactory,
                                           string extraDetail)
     : base(valueReferenceImplementation, roleFactory)
 {
     ExtraDetail = extraDetail;
 }
Exemple #11
0
 private void ReferenceRemoved(object sender, EventArgs e)
 {
     if (sender == _variable)
     {
         _variable.Removed -= ReferenceRemoved;
         _variable          = null;
     }
 }
Exemple #12
0
 internal void NotifyReferenceCreated(IValueReference reference)
 {
     if (_referenceCache.Contains(reference))
     {
         return;
     }
     _referenceCache.Add(reference);
     reference.Removed += ReferenceRemovedHandler;
 }
Exemple #13
0
            /// <summary>
            /// Initializes a new instance of the <see cref="CacheEntry{T}"/> class
            /// by using the specified key and value.
            /// </summary>
            public CacheEntry(string key)
            {
                key_ = key;

                long now = (long)0.ToNanos(TimeUnit.Milliseconds);

                access_time_     = new AtomicLong(now);
                write_time_      = new AtomicLong(now);
                value_reference_ = (IValueReference <T>)UnsetValueReference <T> .UNSET;
            }
 public NamedReferenceDecorator(IValueReference <TValue> originalReference, [NotNull] string name,
                                ValueOriginKind kind, [CanBeNull] IMetadataTypeLite declaredType,
                                IValueRoleFactory <TValue> roleFactory)
 {
     myOriginalReference = originalReference;
     DefaultName         = name;
     OriginKind          = kind;
     DeclaredType        = declaredType;
     myRoleFactory       = roleFactory;
 }
 public CalculatedValueReferenceDecorator([NotNull] IValueReference <TValue> valueReferenceImplementation,
                                          IValueRoleFactory <TValue> roleFactory,
                                          string name,
                                          bool allowNameInValue             = true,
                                          bool allowDefaultTypePresentation = true)
     : base(valueReferenceImplementation, roleFactory)
 {
     DefaultName                  = name;
     AllowNameInValue             = allowNameInValue;
     AllowDefaultTypePresentation = allowDefaultTypePresentation;
 }
Exemple #16
0
            private string FormatValue(IValueReference iValueReference)
            {
                if (iValueReference == null)
                {
                    return("Null");
                }
                if (iValueReference.Value == null)
                {
                    return("Null");
                }

                return(iValueReference.Value.ToString());
            }
        protected override bool IsAllowedReference(IValueReference <TValue> reference)
        {
            if (reference is IMetadataEntityValueReference metadataEntityValueReference &&
                metadataEntityValueReference.Entity != null)
            {
                // We can happily ignore all obsolete properties - GameObject is a sealed class, and we know
                // that all of the deprecated properties have obsolete. We could also check for
                // [EditorBrowsable(EditorBrowsableState.Never)] but that's a bit belt and braces
                return(metadataEntityValueReference.Entity.CustomAttributes.All(a =>
                                                                                a.UsedConstructor?.OwnerType.FullName != "System.ObsoleteAttribute"));
            }

            return(true);
        }
Exemple #18
0
        public static string GetEnumValueIndexAsEnumName <TValue>(IObjectValueRole <TValue> serializedProperty,
                                                                  IValueReference <TValue> enumValueIndexReference,
                                                                  IPresentationOptions options)
            where TValue : class
        {
            var primitiveValue = enumValueIndexReference.AsPrimitiveSafe(options)?.GetPrimitive();

            if (primitiveValue is int enumValueIndex &&
                serializedProperty.GetInstancePropertyReference("enumNames")?.GetPrimaryRole(options) is
                IArrayValueRole <TValue> enumNamesArray)
            {
                return(enumNamesArray.GetElementReference(enumValueIndex).AsStringSafe(options)?.GetString());
            }

            return(null);
        }
Exemple #19
0
 public NamedReferenceDecorator(IValueReference <TValue> originalReference,
                                [NotNull] string name,
                                ValueOriginKind kind,
                                ValueFlags flags,
                                [CanBeNull] IMetadataTypeLite declaredType,
                                IValueRoleFactory <TValue> roleFactory,
                                bool isNameFromValue = false)
 {
     myOriginalReference = originalReference;
     DefaultName         = name;
     OriginKind          = kind;
     DefaultFlags        = flags;
     DeclaredType        = declaredType;
     myRoleFactory       = roleFactory;
     IsNameFromValue     = isNameFromValue;
 }
Exemple #20
0
        public static string GetIntValueAsPrintableChar <TValue>(IValueReference <TValue> intValueReference,
                                                                 IValueFetchOptions options)
            where TValue : class
        {
            var primitiveValue = intValueReference.AsPrimitiveSafe(options)?.GetPrimitive();

            if (primitiveValue != null)
            {
                var value = primitiveValue as long? ?? primitiveValue as int?;
                if (value < char.MaxValue)
                {
                    return($"'{ToPrintableChar((char) value)}'");
                }
            }

            return(null);
        }
Exemple #21
0
        /// <summary>
        /// Blocks while waiting for the value to load.
        /// </summary>
        /// <param name="entry">
        /// The entry that references the loading value reference.</param>
        /// <param name="key">
        /// The key associated with the cache entry.
        /// </param>
        /// <param name="value_reference">
        ///
        /// </param>
        /// <returns>
        /// The loaded value.
        /// </returns>
        T WaitForLoadingValue(CacheEntry <T> entry, string key,
                              IValueReference <T> value_reference)
        {
            // TODO (neylor.silva): Add a mechanism to detect recursive load.
            try {
                T value = value_reference.WaitForValue();
                if (!t_is_value_type_ && (object)value == null)
                {
                    throw new InvalidCacheLoadException(
                              "CacheLoader returned null for key " + key + ".");
                }

                // re-read time now that loading has completed.
                long now = Clock.NanoTime;
                RecordRead(entry, now);
                return(value);
            } finally {
                // TODO(neylor.silva): Record the cache misses statistics.
            }
        }
Exemple #22
0
        /// <summary>
        /// Gets the value associated with the given key, creating or retrieving
        /// that value if necessary.
        /// </summary>
        /// <param name="key">The identifier for the cache item to retrieve.
        /// </param>
        /// <param name="loader">A <see cref="CacheLoader{T}"/> object that could
        /// be used to create the value if it is not present in the cache.</param>
        /// <remarks>
        /// No state associated with this cache is modified until loading is
        /// complete.
        /// </remarks>
        /// <exception cref="ExecutionException">A failure occur while loading
        /// the item using the specified loader delegate.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> or
        /// <paramref name="loader"/> are <c>null</c>.</exception>
        public T Get(string key, CacheLoader <T> loader)
        {
            if (key == null || loader == null)
            {
                Thrower.ThrowArgumentNullException(
                    key == null ? ExceptionArgument.key : ExceptionArgument.loader);
            }

            try {
                T value;
                CacheEntry <T> entry;

                // the cache provider should provide the thread safeness behavior for
                // the reading operation.
                bool ok = cache_provider_.Get(CacheKey(key), out entry);
                if (ok)
                {
                    long now = Clock.NanoTime;
                    if (GetLiveValue(entry, now, out value))
                    {
                        RecordRead(entry, now);
                        return(ScheduleRefresh(entry, key, value, now, loader));
                    }
                    IValueReference <T> value_reference = entry.ValueReference;
                    if (value_reference.IsLoading)
                    {
                        return(WaitForLoadingValue(entry, key, value_reference));
                    }
                }

                // at this point entry does not exists or is expired, so lets load
                // the value.
                return(LockedGetOrLoad(key, loader));
            } catch (Exception e) {
                MustLogger.ForCurrentProcess.Error(kTypeForLogger + "Get]", e);
                throw;
            }
        }
Exemple #23
0
        private object GetIndentifierValue(IScriptContext context, string identifier)
        {
            //object result = context.GetItem(identifier, false);
            //if (result != RuntimeHost.NoVariable) return result;

            if (IsGlobal)
            {
                _variable = null;
                IScriptScope scope = context.Scope.Parent;
                while (scope != null)
                {
                    if (scope.HasVariable(identifier))
                    {
                        return(scope.GetItem(identifier, true));
                    }
                    scope = scope.Parent;
                }
            }
            else
            {
                if (_variable != null && _variable.Value != null)
                {
                    return(_variable.Value);
                }

                object result;
                _variable = CreateRef(identifier, context, true, out result);

                if (result != RuntimeHost.NoVariable)
                {
                    return(result);
                }
            }

            return(RuntimeHost.HasType(identifier)
               ? (object)RuntimeHost.GetType(identifier)
               : NamespaceResolver.Get(identifier));
        }
Exemple #24
0
            private string GetComponentName(IValueReference <TValue> componentValue,
                                            [CanBeNull] IReifiedType <TValue> objectNamesType,
                                            [CanBeNull] IMetadataMethodLite getInspectorTitleMethod,
                                            IStackFrame frame,
                                            IValueFetchOptions options,
                                            IValueServicesFacade <TValue> services,
                                            out bool isNameFromValue)
            {
                if (objectNamesType != null && getInspectorTitleMethod != null)
                {
                    try
                    {
                        var inspectorTitle = objectNamesType.CallStaticMethod(frame, options, getInspectorTitleMethod,
                                                                              componentValue.GetValue(options));
                        var stringValueRole =
                            new SimpleValueReference <TValue>(inspectorTitle, frame, services.RoleFactory)
                            .AsStringSafe(options);
                        if (stringValueRole != null)
                        {
                            isNameFromValue = true;
                            return(stringValueRole.GetString());
                        }
                    }
                    catch (EvaluatorAbortedException e)
                    {
                        myLogger.LogExceptionSilently(e);
                    }
                    catch (Exception e)
                    {
                        myLogger.Warn(e, ExceptionOrigin.Algorithmic,
                                      $"Unable to fetch object names for {componentValue}");
                    }
                }

                isNameFromValue = false;
                return(componentValue.GetPrimaryRole(options).ReifiedType.MetadataType.ShortName);
            }
            private IValue GetElementValue(IValueReference <TValue> elementReference, IValueFetchOptions options)
            {
                var elementRole = elementReference.AsObjectSafe(options);

                if (elementRole == null)
                {
                    return(null);
                }

                var isNameFromValue = true;
                var name            = elementRole.GetInstancePropertyReference("name", true)?.AsStringSafe(options)
                                      ?.GetString();

                if (name == null)
                {
                    name            = "Game Object";
                    isNameFromValue = false;
                }

                return(new NamedReferenceDecorator <TValue>(elementRole.ValueReference, name,
                                                            ValueOriginKind.Property, ValueFlags.None | ValueFlags.IsReadOnly,
                                                            elementRole.ReifiedType.MetadataType, myValueServices.RoleFactory, isNameFromValue)
                       .ToValue(myValueServices));
            }
            private IValue GetElementValue(IValueReference <TValue> elementReference, IValueFetchOptions options)
            {
                var elementRole = elementReference.AsObjectSafe(options);

                if (elementRole == null)
                {
                    return(null);
                }

                var isNameFromValue = true;
                var name            = elementRole.GetInstancePropertyReference("name", true)?.AsStringSafe(options)
                                      ?.GetString();

                if (name == null)
                {
                    name            = "Game Object";
                    isNameFromValue = false;
                }

                // Tell the value presenter to hide the name field, if we're using it for the key. Also hide the default
                // type presentation - we know it's a GameObject, it's under a group called "Game Objects"
                return(new CalculatedValueReferenceDecorator <TValue>(elementRole.ValueReference,
                                                                      myValueServices.RoleFactory, name, !isNameFromValue, false).ToValue(myValueServices));
            }
Exemple #27
0
 internal void NotifyReferenceCreated(IValueReference reference)
 {
     if (_referenceCache.Contains(reference)) return;
       _referenceCache.Add(reference);
       reference.Removed += ReferenceRemovedHandler;
 }
Exemple #28
0
      private string FormatValue(IValueReference iValueReference)
      {
        if (iValueReference == null) return "Null";
        if (iValueReference.Value == null) return "Null";

        return iValueReference.Value.ToString();
      }
Exemple #29
0
 private void ReferenceRemoved(object sender, EventArgs e)
 {
   if (sender == _variable)
   {
     _variable.Removed -= ReferenceRemoved;
     _variable = null;
   }
 }
Exemple #30
0
    private void AssignIdentifier(object value, IScriptContext context)
    {
      if (IsGlobal)
      {
        SetToParentScope(context.Scope.Parent, _identifier, value);
        _variable = null;
        return;
      }

      if (IsVar && ((context.Scope is LocalScope) || (context.Scope is FunctionScope)))
      {
        context.Scope.CreateVariable(_identifier, value);
        return;
      }

      if (_variable != null)
      {
        _variable.Value = value;
      }
      else
      {
        context.SetItem(_identifier, value);
        
        object tmp;
        _variable = CreateRef(_identifier, context, false, out tmp);
      }
    }
Exemple #31
0
    private object GetIndentifierValue(IScriptContext context, string identifier)
    {
      //object result = context.GetItem(identifier, false);
      //if (result != RuntimeHost.NoVariable) return result;

      if (IsGlobal)
      {
        _variable = null;
        IScriptScope scope = context.Scope.Parent;
        while (scope != null)
        {
          if (scope.HasVariable(identifier))
          {
            return scope.GetItem(identifier, true);
          }
          scope = scope.Parent;
        }
      }
      else
      {
        if (_variable != null && _variable.Value != null) return _variable.Value;

        object result;         
        _variable = CreateRef(identifier, context, true, out result);

        if (result != RuntimeHost.NoVariable)
        {
          return result;
        }
      }

      return RuntimeHost.HasType(identifier)
               ? (object) RuntimeHost.GetType(identifier)
               : NamespaceResolver.Get(identifier);
    }
 static UnsetValueReference()
 {
     UNSET = new UnsetValueReference <T>();
 }
Exemple #33
0
 public ReferencingEventArgs(bool cancel, IValueReference reference)
 {
   Cancel = cancel;
   Ref = reference;
 }
Exemple #34
0
 protected abstract bool IsAllowedReference(IValueReference <TValue> reference);
Exemple #35
0
 public ReferencingEventArgs(bool cancel, IValueReference reference)
 {
     Cancel = cancel;
     Ref    = reference;
 }
 protected ValueReferenceDecoratorBase([NotNull] IValueReference <TValue> valueReferenceImplementation,
                                       IValueRoleFactory <TValue> roleFactory)
 {
     myValueReferenceImplementation = valueReferenceImplementation;
     myRoleFactory = roleFactory;
 }