Beispiel #1
0
        public override void Dispose()
        {
            ObjectUtilities.DisposeSafely(Logger, _requestedQueue);
            ObjectUtilities.DisposeSafely(Logger, _dataFlows);
            ObjectUtilities.DisposeSafely(Logger, _services);

            base.Dispose();

            GC.Collect();
        }
Beispiel #2
0
    public void Main()
    {
        var obj = new object();

        // Instead of this
        var result1 = ObjectUtilities.ToStringLower(obj);

        // Do this
        var result2 = obj.ToStringLower();
    }
Beispiel #3
0
        public void SetIndexedValuesInArrays()
        {
            int[] array = new int[2];

            ObjectUtilities.SetIndexedValue(array, new object[] { 0 }, 1);
            ObjectUtilities.SetIndexedValue(array, new object[] { 1 }, 2);

            Assert.AreEqual(1, array[0]);
            Assert.AreEqual(2, array[1]);
        }
        public static IEnumerable <Exception> GetInnerExceptions(this Exception exception)
        {
            var result = exception;

            while (!ObjectUtilities.IsDefault(result))
            {
                yield return(result);

                result = result.InnerException;
            }
        }
        public void AddFunctionAsCalculatorEventHandler()
        {
            Calculator calculator = new Calculator();
            Listener   listener   = new Listener();

            ObjectUtilities.AddHandler(calculator, "MyEvent", listener, null);
            calculator.Add(1, 2);

            Assert.AreEqual(1, listener.X);
            Assert.AreEqual(2, listener.Y);
        }
        public void SetIndexedValuesInDictionary()
        {
            Dictionary <string, int> dictionary = new Dictionary <string, int>();

            ObjectUtilities.SetIndexedValue(dictionary, new object[] { "one" }, 1);
            ObjectUtilities.SetIndexedValue(dictionary, new object[] { "two" }, 2);
            ObjectUtilities.SetIndexedValue(dictionary, new object[] { "three" }, 3);

            Assert.AreEqual(1, dictionary["one"]);
            Assert.AreEqual(2, dictionary["two"]);
            Assert.AreEqual(3, dictionary["three"]);
        }
        public void GetIndexedValuesFromList()
        {
            List <int> list = new List <int>();

            list.Add(1);
            list.Add(2);
            list.Add(3);

            Assert.AreEqual(1, ObjectUtilities.GetIndexedValue(list, new object[] { 0 }));
            Assert.AreEqual(2, ObjectUtilities.GetIndexedValue(list, new object[] { 1 }));
            Assert.AreEqual(3, ObjectUtilities.GetIndexedValue(list, new object[] { 2 }));
        }
        public object Evaluate(IContext context)
        {
            object target = this.targetExpression.Evaluate(context);
            object index  = this.sliceExpression.Evaluate(context);

            if (target is string)
            {
                return(((string)target)[(int)index].ToString());
            }

            return(ObjectUtilities.GetIndexedValue(target, new object[] { index }));
        }
        public void GetIndexedValuesFromDictionary()
        {
            Dictionary <string, int> numbers = new Dictionary <string, int>();

            numbers["one"]   = 1;
            numbers["two"]   = 2;
            numbers["three"] = 3;

            Assert.AreEqual(1, ObjectUtilities.GetIndexedValue(numbers, new object[] { "one" }));
            Assert.AreEqual(2, ObjectUtilities.GetIndexedValue(numbers, new object[] { "two" }));
            Assert.AreEqual(3, ObjectUtilities.GetIndexedValue(numbers, new object[] { "three" }));
        }
Beispiel #10
0
        public override object Execute(List arguments, ValueEnvironment environment)
        {
            var            target = arguments.First;
            IList <object> args   = null;

            if (arguments.Next != null)
            {
                args = arguments.Next.ToObjectArray();
            }

            return(ObjectUtilities.GetNativeValue(target, this.name, args));
        }
Beispiel #11
0
        public async Task <Response> DownloadAsync(Request request)
        {
            HttpResponseMessage httpResponseMessage = null;
            HttpRequestMessage  httpRequestMessage  = null;

            try
            {
                httpRequestMessage = request.ToHttpRequestMessage();

                var httpClient = await CreateClientAsync(request);

                var stopwatch = new Stopwatch();
                stopwatch.Start();

                httpResponseMessage = await SendAsync(httpClient, httpRequestMessage);

                stopwatch.Stop();

                var elapsedMilliseconds = stopwatch.ElapsedMilliseconds;

                var response = await HandleAsync(request, httpResponseMessage);

                if (response != null)
                {
                    response.Version = response.Version == null ? HttpVersion.Version11 : response.Version;
                    return(response);
                }

                response = await httpResponseMessage.ToResponseAsync();

                response.ElapsedMilliseconds = (int)elapsedMilliseconds;
                response.RequestHash         = request.Hash;
                response.Version             = httpResponseMessage.Version;

                return(response);
            }
            catch (Exception e)
            {
                Logger.LogError($"{request.RequestUri} download failed: {e}");
                return(new Response
                {
                    RequestHash = request.Hash,
                    StatusCode = HttpStatusCode.Gone,
                    ReasonPhrase = e.ToString(),
                    Version = HttpVersion.Version11
                });
            }
            finally
            {
                ObjectUtilities.DisposeSafely(Logger, httpResponseMessage, httpRequestMessage);
            }
        }
Beispiel #12
0
 public static string GetAssetPath(this Object obj)
 {
     if (obj == null)
     {
         return("");
     }
     if (!GameObjectToAssetPathCache.TryGetValue(obj, out var assetPath))
     {
         assetPath = ObjectUtilities.ObjectToAssetPath(obj);
         GameObjectToAssetPathCache.Add(obj, assetPath);
     }
     return(assetPath);
 }
Beispiel #13
0
        private Vector2 getHeightOffsetFromItem()
        {
            if (ObjectUtilities.IsSameType(typeof(StardewValley.Object), this.actualItem.GetType()))
            {
                return(new Vector2(0, 64f));
            }
            if (ObjectUtilities.IsSameType(typeof(Revitalize.Framework.Objects.MultiTiledObject), this.actualItem.GetType()))
            {
                return(new Vector2(0, 64f * (this.actualItem as MultiTiledObject).Height));
            }

            return(new Vector2(0, 64f));
        }
Beispiel #14
0
        public void AddFunctionWithoutParametersAsPersonEventHandler()
        {
            Person person = new Person()
            {
                FirstName = "Adam"
            };
            IntListener listener = new IntListener();

            ObjectUtilities.AddHandler(person, "IntEvent", listener, null);
            person.GetName();

            Assert.AreEqual(0, listener.Arity);
        }
        public void SetIndexedValuesInList()
        {
            List <int> list = new List <int>();

            ObjectUtilities.SetIndexedValue(list, new object[] { 0 }, 1);
            ObjectUtilities.SetIndexedValue(list, new object[] { 1 }, 2);
            ObjectUtilities.SetIndexedValue(list, new object[] { 2 }, 3);

            Assert.AreEqual(3, list.Count);
            Assert.AreEqual(1, list[0]);
            Assert.AreEqual(2, list[1]);
            Assert.AreEqual(3, list[2]);
        }
 /// <summary>
 /// Resets tool colors when left click is used for normal tools.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public static void ResetNormalToolsColorOnLeftClick(object sender, StardewModdingAPI.Events.ButtonPressedEventArgs e)
 {
     if (e.Button == SButton.MouseLeft)
     {
         if (Game1.player.CurrentTool != null)
         {
             if (ObjectUtilities.IsSameType(Game1.player.CurrentTool.GetType(), typeof(Pickaxe)) || ObjectUtilities.IsSameType(Game1.player.CurrentTool.GetType(), typeof(Axe)) || ObjectUtilities.IsSameType(Game1.player.CurrentTool.GetType(), typeof(Hoe)) || ObjectUtilities.IsSameType(Game1.player.CurrentTool.GetType(), typeof(WateringCan)))
             {
                 ColorChanger.ResetToolColorSwaps();
             }
         }
     }
 }
 private static IconUtils.Icon GetHierarchyIcon(Object obj)
 {
     IconUtils.Icon iconType = IconUtils.rubyIcon;
     if (ObjectUtilities.ChangesStoredInPrefab(obj))
     {
         iconType = IconUtils.squareIcon;
     }
     if (IsChildNode(obj))
     {
         iconType = IconUtils.childIcon;
     }
     return(iconType);
 }
        public void GetIndexedValuesFromDynamicObject()
        {
            DynamicObject obj = new DynamicObject(null);

            obj.SetValue("name", "Adam");
            obj.SetValue("get_age", new DefinedFunction("get_age", null, null, null));

            Assert.AreEqual("Adam", ObjectUtilities.GetIndexedValue(obj, new object[] { "name" }));

            object f = ObjectUtilities.GetIndexedValue(obj, new object[] { "get_age" });

            Assert.IsNotNull(f);
            Assert.IsInstanceOfType(f, typeof(IFunction));
        }
        public void SetArrayIndexedValueWithoutUsingIndex()
        {
            var array = new int[3];

            try
            {
                ObjectUtilities.SetIndexedValue(array, new object[] { }, 3);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
            }
        }
        public void SetDictionaryIndexedValueWithoutUsingIndex()
        {
            var dict = new Dictionary <string, int>();

            try
            {
                ObjectUtilities.SetIndexedValue(dict, new object[] { }, 3);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
            }
        }
Beispiel #21
0
        /// <summary>
        /// Tests that a specific property of an object changes values.
        /// </summary>
        /// <typeparam name="T">Type of object to test.</typeparam>
        /// <param name="obj">Object with property to test.</param>
        /// <param name="property">Property to test.</param>
        public static void ChangesValue <T>(T obj, PropertyInfo property)
        {
            for (int i = 0; i < new Random().Next(5, 20); i++)
            {
                var randomObject = ObjectUtilities.CreateInstanceWithRandomValues(obj.GetType());
                property.SetValue(obj, property.GetValue(randomObject));
                Assert.AreEqual(property.GetValue(randomObject), property.GetValue(obj));
            }

            if (PropertyUtilities.CheckIfPropertyIsClass(property))
            {
                ChangesValues(property.GetValue(obj));
            }
        }
        public void GetIndexedValuesFromDynamicObject()
        {
            DynamicObject obj = new DynamicObject();

            obj.SetValue("Name", "Adam");
            obj.SetValue("GetAge", new Function(new string[] { "n" }, null));

            Assert.AreEqual("Adam", ObjectUtilities.GetIndexedValue(obj, new object[] { "Name" }));

            object f = ObjectUtilities.GetIndexedValue(obj, new object[] { "GetAge" });

            Assert.IsNotNull(f);
            Assert.IsInstanceOfType(f, typeof(Function));
        }
        public void AddFunctionAsPersonEventHandler()
        {
            Person person = new Person()
            {
                FirstName = "Adam"
            };
            NameListener listener = new NameListener();

            ObjectUtilities.AddHandler(person, "NameEvent", listener, null);
            person.GetName();

            Assert.AreEqual(4, listener.Length);
            Assert.AreEqual("Adam", listener.Name);
        }
        public void Execute(IContext context)
        {
            var target = this.targetExpression.Evaluate(context);
            var value  = this.expression.Evaluate(context);

            IValues values = target as IValues;

            if (values != null)
            {
                values.SetValue(this.name, value);
            }

            ObjectUtilities.SetValue(target, this.name, value);
        }
Beispiel #25
0
        public object GetValue(object obj)
        {
            IValues values = obj as IValues;

            if (values == null)
            {
                IType type = Types.GetType(obj);

                if (type != null)
                {
                    return(type.GetMethod(this.name));
                }

                if (obj is Type)
                {
                    return(TypeUtilities.GetValue((Type)obj, this.name));
                }

                return(ObjectUtilities.GetValue(obj, this.name));
            }

            object value = values.GetValue(this.name);

            if (value != null)
            {
                return(value);
            }

            if (values.HasValue(this.name))
            {
                return(value);
            }

            string typename;

            if (values is BindingEnvironment)
            {
                typename = "module";
            }
            else if (values is DynamicObject)
            {
                typename = ((DynamicObject)values).Class.Name;
            }
            else
            {
                typename = values.GetType().Name;
            }

            throw new AttributeError(string.Format("'{1}' object has no attribute '{0}'", this.name, typename));
        }
        public void SetListIndexedValueWithoutUsingIndex()
        {
            List <int> list = new List <int>();

            try
            {
                ObjectUtilities.SetIndexedValue(list, new object[] { }, 3);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
            }
        }
        public void IsNumber()
        {
            Assert.IsTrue(ObjectUtilities.IsNumber((byte)1));
            Assert.IsTrue(ObjectUtilities.IsNumber((short)2));
            Assert.IsTrue(ObjectUtilities.IsNumber((int)3));
            Assert.IsTrue(ObjectUtilities.IsNumber((long)4));
            Assert.IsTrue(ObjectUtilities.IsNumber((float)1.2));
            Assert.IsTrue(ObjectUtilities.IsNumber((double)2.3));

            Assert.IsFalse(ObjectUtilities.IsNumber(null));
            Assert.IsFalse(ObjectUtilities.IsNumber("foo"));
            Assert.IsFalse(ObjectUtilities.IsNumber('a'));
            Assert.IsFalse(ObjectUtilities.IsNumber(this));
        }
        private void OnEnable()
        {
            if (!ObjectUtilities.TryGetWithTag("Player", out GameObject playerObject))
            {
                return;
            }

            if (!playerObject.TryGetComponent(out PowerupsController powerupsController))
            {
                return;
            }

            powerupsController.PowerupActivated   += OnPowerupActivated;
            powerupsController.PowerupDeactivated += OnPowerupDeactivated;
        }
        public void GetNamesNativeObject()
        {
            var person = new Person();
            var result = ObjectUtilities.GetNames(person);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IList <string>));

            var names = (IList <string>)result;

            Assert.IsTrue(names.Contains("FirstName"));
            Assert.IsTrue(names.Contains("LastName"));
            Assert.IsTrue(names.Contains("GetName"));
            Assert.IsTrue(names.Contains("NameEvent"));
        }
        public void ChangeIndexedValueInList()
        {
            List <int> list = new List <int>();

            list.Add(1);
            list.Add(1);
            list.Add(3);

            ObjectUtilities.SetIndexedValue(list, new object[] { 1 }, 2);

            Assert.AreEqual(3, list.Count);
            Assert.AreEqual(1, list[0]);
            Assert.AreEqual(2, list[1]);
            Assert.AreEqual(3, list[2]);
        }