private static void SetConfig(string configOption, string newValue)
        {
            var property = ConfigState.Instance.GetType().GetProperty(configOption, BindingFlags.Instance | BindingFlags.Public);

            if (property != null)
            {
                property.SetValue(ConfigState.Instance, TypeUtils.CoerceValue(newValue, property.PropertyType)); //TODO handle enums
            }
            else
            {
                ConsoleModule.WriteLine("not found");
            }
        }
 private static void SetCustomVar(string customVar, string newValue)
 {
     if (ConfigState.Instance.CustomConfigVars.ContainsKey(customVar))
     {
         //value exists: coerce the value
         object value          = ConfigState.Instance.CustomConfigVars[customVar];
         object newValueParsed = TypeUtils.CoerceValue(newValue, value.GetType());
         ConfigState.Instance.CustomConfigVars[customVar] = newValueParsed;
     }
     else
     {
         //value doesn't exist: warn and exit
         ConsoleModule.WriteLine("Value doesn't already exist; can't determine type to set. Use SetCustomVarTyped instead.");
     }
 }
Exemple #3
0
        public override void PaintValues()
        {
            Initialize();

            var options = ConfigState.Instance.GetSpeedHacksOptions();

            var props = typeof(SpeedHacksOptions).GetProperties(BindingFlags.Public | BindingFlags.Instance)
                        .Where(p => p.PropertyType == typeof(bool));

            foreach (var prop in props)
            {
                var go = GetFieldForProp(prop.Name);

                var value = TypeUtils.CoerceValue <bool>(prop.GetValue(options));

                go.GetComponentInChildren <Toggle>().isOn = value;
            }
        }
        private static object[] CoerceArguments(object[] args, MethodInfo method)
        {
            var parameters = method.GetParameters();
            int numArgs    = Math.Min(args.Length, parameters.Length - 1);

            if (numArgs <= 0)
            {
                return new object[] { }
            }
            ;
            object[] typedArgs = new object[numArgs];
            for (int i = 0; i < numArgs; i++)
            {
                object arg       = args[i];
                var    parameter = parameters[i + 1]; //account for ScriptExecutionContext
                object typedArg  = TypeUtils.CoerceValue(arg, parameter.ParameterType);
                typedArgs[i] = typedArg;
            }

            return(typedArgs);
        }
Exemple #5
0
 /// <summary>
 /// Calls a script through the scripting system on an object, returning a typed result
 /// </summary>
 public static T CallOnForResult <T>(string script, object instance, ScriptExecutionContext context, params object[] args)
 {
     return(TypeUtils.CoerceValue <T>(Instance.CallScript(script, instance, context, args)));
 }
Exemple #6
0
        /// <summary>
        /// Sets proxied fields from a source object to a target object
        /// </summary>
        public static void SetProxyFields(object source, object target, bool throwOnError = false)
        {
            //main fields
            var sourceFields = source.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                               .Where(x => x.GetCustomAttribute <ProxyFieldAttribute>() != null);

            foreach (var sourceInfo in sourceFields)
            {
                try
                {
                    var proxyAttribute = sourceInfo.GetCustomAttribute <ProxyFieldAttribute>();

                    if (proxyAttribute.LinkToTypes != null && proxyAttribute.LinkToTypes.Length > 0)
                    {
                        if (!proxyAttribute.LinkToTypes.Contains(target.GetType().Name)) //correct I hope
                        {
                            continue;
                        }
                    }

                    string fieldName    = !string.IsNullOrEmpty(proxyAttribute.TargetField) ? proxyAttribute.TargetField : sourceInfo.Name;
                    var    bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

                    var targetField = target.GetType().GetField(fieldName, bindingFlags);
                    if (targetField == null)
                    {
                        continue;
                    }

                    if (!proxyAttribute.BindPrivate && !targetField.IsPublic && targetField.GetCustomAttribute <SerializeField>() == null)
                    {
                        continue;
                    }

                    object coercedValue = TypeUtils.CoerceValue(sourceInfo.GetValue(source), targetField.FieldType);
                    targetField.SetValue(target, coercedValue);
                }
                catch (Exception e)
                {
                    Debug.LogError($"Failed to bind {sourceInfo.Name} from {source.GetType().Name} to {target.GetType().Name}");
                    Debug.LogException(e);

                    if (throwOnError)
                    {
                        throw e;
                    }
                }
            }

            //ProxyExtensionData
            try
            {
                var sourceExtensionDataMember = source.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                                                .Where(x => x.GetCustomAttribute <ProxyExtensionDataAttribute>() != null)
                                                .SingleOrDefault();
                IEnumerable sourceExtensionData = (IEnumerable)sourceExtensionDataMember.GetValue(source); //is this legit?

                foreach (var element in sourceExtensionData)
                {
                    KeyValuePair <string, object> elementKvp;
                    if (element is KeyValuePair <string, object> kvp)
                    {
                        elementKvp = kvp;
                    }
                    else if (element is KeyValuePair <string, string> kvps)
                    {
                        elementKvp = new KeyValuePair <string, object>(kvps.Key, kvps.Value);
                    }
                    else if (element is ITuple tuple)
                    {
                        elementKvp = new KeyValuePair <string, object>((string)tuple[0], tuple[1]);
                    }
                    else
                    {
                        throw new InvalidCastException();
                    }

                    var targetField = target.GetType().GetField(elementKvp.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    if (targetField == null)
                    {
                        continue;
                    }

                    object coercedValue = TypeUtils.CoerceValue(elementKvp.Value, targetField.FieldType);
                    targetField.SetValue(target, coercedValue);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"Failed to bind extension data from {source.GetType().Name} to {target.GetType().Name}");
                Debug.LogException(e);

                if (throwOnError)
                {
                    throw e;
                }
            }
        }