/// <summary> /// Creates a symbol name. /// </summary> /// <param name="symbol">The symbol name.</param> /// <returns>A string, which represents the symbol name.</returns> public string CreateSymbolName(RuntimeMember symbol) { if (symbol == null) { throw new ArgumentNullException("symbol"); } if (symbol is RuntimeMethod) { return(CreateSymbolName(symbol as RuntimeMethod)); } else if (symbol is RuntimeType) { return(CreateSymbolName(symbol as RuntimeType)); } else { string name; RuntimeType declaringType = symbol.DeclaringType; if (declaringType != null) { string declaringTypeSymbolName = CreateSymbolName(declaringType); name = String.Format("{0}.{1}", declaringTypeSymbolName, symbol.Name); } else { name = symbol.Name; } return(name); } }
bool Matches(RuntimeMember runtimeMember) { if (!runtimeMember.MatchesParameterCount(parameterCount)) { return(false); } if (parameterTypes != null) { for (int i = 0; i < parameterCount; i++) { if (runtimeMember.GetParameterType(i) != parameterTypes[i]) { return(false); } } } if (parameterIdNames != null) { foreach (var name in parameterIdNames) { if (!HasMatchingParameter(runtimeMember, name)) { return(false); } } } return(true); }
public static void ResetTarget(object target) { //Func<Type, bool> IsTypeSerializedByUnity = type => //{ // // Might be forgetting something here... // return (type.IsPrimitive || type.IsEnum || type == typeof(string) // || type.IsA<UnityObject>() // || VFWSerializationLogic.UnityStructs.ContainsValue(type) // || type == typeof(AnimationCurve) // || type == typeof(Gradient)); //}; //Func<FieldInfo, bool> IsFieldSerializedByUnity = field => //{ // var type = field.FieldType; // if (!IsTypeSerializedByUnity(type)) // return false; // if (!field.IsPublic && !field.IsDefined<SerializeField>()) // return false; // if (type.IsArray) // return type.GetArrayRank() == 1 && IsTypeSerializedByUnity(type.GetElementType()); // if (type.IsGenericType) // return type.GetGenericTypeDefinition() == typeof(List<>) && IsTypeSerializedByUnity(type.GetGenericArguments()[0]); // if (type.IsAbstract) // return false; // return type.IsDefined<SerializableAttribute>(); //}; var members = RuntimeMember.CachedWrapMembers(target.GetType()); for (int i = 0; i < members.Count; i++) { var member = members[i]; member.Target = target; var defAttr = member.Info.GetCustomAttribute <DefaultAttribute>(); if (defAttr == null) { // if a field is not serializable by Unity, then Unity won't be able to set it to whatever we initialized it with, // so we will reset it to its default value and unfortunately ignore the field initialization because it's very // hard to obtain that value //var field = member.Info as FieldInfo; //if (field != null && !IsFieldSerializedByUnity(field) || member.Info is PropertyInfo) // member.Value = member.Type.GetDefaultValue(); } else { var value = defAttr.Value; if (value == null && !member.Type.IsAbstract) // null means to instantiate a new instance { value = member.Type.ActivatorInstance(); } member.Value = value; } } }
public BindingOperation Make(Tree <Cell> nameCell) { string name = nameCell.Value.Text.Trim(); if (NoOperationIsImpliedBy(name)) { return(new NoBinding()); } var cellOperation = new CellOperationImpl(processor); if (CheckIsImpliedBy(name)) { return(new CheckBinding(cellOperation, targetProvider, nameCell)); } string memberName = processor.ParseTree <Cell, MemberName>(nameCell).ToString(); RuntimeMember member = RuntimeType.FindInstance(targetProvider, new IdentifierName(memberName), 1); if (member == null && newIdentifier.IsStartOf(name)) { string newMemberName = name.Substring(4); return(new CreateBinding(processor, adapter, newMemberName)); } return(new InputBinding(processor, targetProvider, nameCell)); }
bool Matches(RuntimeMember runtimeMember) { return (specification.MatchesParameterCount(runtimeMember) && specification.MatchesParameterTypes(runtimeMember) && specification.MatchesParameterNames(runtimeMember)); }
/// <summary> /// Allocates memory in the specified section. /// </summary> /// <param name="member">The metadata member to allocate space for.</param> /// <param name="section">The executable section to allocate From.</param> /// <param name="size">The number of bytes to allocate. If zero, indicates an unknown amount of memory is required.</param> /// <param name="alignment">The alignment. A value of zero indicates the use of a default alignment for the section.</param> /// <returns>A stream, which can be used to populate the section.</returns> public virtual Stream Allocate(RuntimeMember member, SectionKind section, int size, int alignment) { // Create a canonical symbol name string name = CreateSymbolName(member); // Create a stream return(Allocate(name, section, size, alignment)); }
public IEnumerable <RuntimeMember> GetSerializableMembers(Type type, object target) { var members = ReflectionUtil.GetMemoizedMembers(type); var enumeration = RuntimeMember.Enumerate(members, target).ToList(); var result = enumeration.Where(IsSerializable).ToList(); return(result); }
[Test] public void QueryableMemberIsInvoked() { var queryable = new QueryableClass(); RuntimeMember method = RuntimeType.GetInstance(new TypedValue(queryable), new IdentifierName("dynamic"), 1); TypedValue result = method.Invoke(new object[] { "stuff" }); Assert.AreEqual("dynamicstuff", result.Value); }
/// <summary> /// Returns a list of RuntimeMember wrapping all the serializable members in the specified type. /// </summary> private List <RuntimeMember> GetSerializableMembers(Type type, object target) { var members = ReflectionHelper.CachedGetMembers(type); var serializableMembers = members.Where(IsSerializableMember); var result = RuntimeMember.WrapMembers(serializableMembers, target); return(result); }
/// <summary> /// Gets the serialization key used to serialize the specified member /// The key in general is: "TypeNiceName MemberName" /// Ex: "int someValue", "GameObject go" /// </summary> public static string GetMemberKey(RuntimeMember member) { if (cachedGetMemberKey == null) { cachedGetMemberKey = new Func <RuntimeMember, string>(x => x.TypeNiceName + " " + x.Name).Memoize(); } return(cachedGetMemberKey(member)); }
object[] GetParameterList(TypedValue instance, Tree <T> parameters, RuntimeMember member) { return(parameters.Branches.Aggregate(new List <object>(), (parameterList, parameter) => { parameterList.Add(ParseParameterValue(member, instance, parameter, parameterList.Count)); return parameterList; }).ToArray()); }
[Test] public void QueryableMemberIsInvoked() { var queryable = new QueryableClass(); RuntimeMember method = GetMethodFromProcessor(queryable, "dynamic", 1); TypedValue result = method.Invoke(new object[] { "stuff" }); Assert.AreEqual("dynamicstuff", result.Value); }
[Test] public void MethodWithUnderscoresIsInvoked() { RuntimeMember method = RuntimeType.GetInstance(new TypedValue(instance), "methodwithunderscores", 0); Assert.IsNotNull(method); TypedValue result = method.Invoke(new object[] {}); Assert.AreEqual("samplereturn", result.Value.ToString()); }
[Test] public void MethodWithParmsIsInvoked() { RuntimeMember method = RuntimeType.GetInstance(new TypedValue(instance), "methodwithparms", 1); Assert.IsNotNull(method); TypedValue result = method.Invoke(new object[] { "input" }); Assert.AreEqual("sampleinput", result.Value.ToString()); }
protected string FormatRuntimeMember(RuntimeMember member) { if (!showTokenValues.Checked) { return(member.Name); } return("[" + TokenToString(member.Token) + "] " + member.Name); }
public static RuntimeMember GetInstance(TypedValue instance, IdentifierName memberName, int parameterCount) { RuntimeMember runtimeMember = FindInstance(instance.Value, memberName, parameterCount); if (runtimeMember == null) { throw new MemberMissingException(instance.Type, memberName.SourceName, parameterCount); } return(runtimeMember); }
/// <summary> /// Initializes a new instance of the <see cref="MemberOperand"/> class. /// </summary> /// <param name="member">The member to reference.</param> /// <param name="type">The type of data held in the operand.</param> /// <param name="offset">The offset From the base register or absolute address to retrieve.</param> public MemberOperand(RuntimeMember member, SigType type, IntPtr offset) : base(type, null, offset) { if (member == null) { throw new ArgumentNullException(@"member"); } this.member = member; }
/// <summary> /// Initializes a new instance of the <see cref="MemberOperand"/> class. /// </summary> /// <param name="method">The method to reference.</param> /// <exception cref="System.ArgumentNullException"><paramref name="method"/> is null.</exception> public MemberOperand(RuntimeMethod method) : base(new SigType(CilElementType.I), null, IntPtr.Zero) { if (method == null) { throw new ArgumentNullException(@"method"); } this.member = method; }
[Test] public void VoidMethodIsInvoked() { RuntimeMember method = RuntimeType.GetInstance(new TypedValue(instance), "voidmethod", 0); Assert.IsNotNull(method); TypedValue result = method.Invoke(new object[] {}); Assert.AreEqual(null, result.Value); Assert.AreEqual(typeof(void), result.Type); }
public RuntimeMember GetConstructor(int parameterCount) { RuntimeMember runtimeMember = FindInstance(Type, IdentifierName.Constructor, parameterCount); if (runtimeMember == null) { throw new ConstructorMissingException(Type, parameterCount); } return(runtimeMember); }
[Test] public void MethodWithReturnIsInvoked() { RuntimeMember method = RuntimeType.GetInstance(new TypedValue(instance), "methodnoparms", 0); Assert.IsNotNull(method); TypedValue result = method.Invoke(new object[] {}); Assert.AreEqual("samplereturn", result.Value.ToString()); Assert.AreEqual(typeof(string), result.Type); }
private string GetMemberKey(RuntimeMember member) { if (_getMemberKey == null) { _getMemberKey = new Func <RuntimeMember, string>(x => string.Format("{0}: {1} {2}", x.Info.MemberType.ToString(), x.TypeNiceName, x.Name) ).Memoize(); } return(_getMemberKey(member)); }
[Test] public void IndexerIsInvoked() { RuntimeMember method = RuntimeType.GetInstance(new TypedValue(instance), "anything", 0); Assert.IsNotNull(method); TypedValue result = method.Invoke(new object[] {}); Assert.AreEqual("indexanything", result.Value); Assert.AreEqual(typeof(string), result.Type); }
/// <summary> /// Initializes a new instance of <see cref="MemberOperand"/>. /// </summary> /// <param name="field">The runtime field to reference.</param> /// <exception cref="System.ArgumentNullException"><paramref name="field"/> is null.</exception> public MemberOperand(RuntimeField field) : base(field.Type, null, IntPtr.Zero) { if (field == null) { throw new ArgumentNullException(@"field"); } this.member = field; }
/// <summary> /// Retrieves a linker symbol. /// </summary> /// <param name="member">The runtime member to retrieve a symbol for.</param> /// <returns> /// A linker symbol, which represents the runtime member. /// </returns> /// <exception cref="System.ArgumentNullException"><paramref name="member"/> is null.</exception> /// <exception cref="System.ArgumentException">There's no symbol of the given name.</exception> public LinkerSymbol GetSymbol(RuntimeMember member) { if (member == null) { throw new ArgumentNullException("member"); } string symbolName = CreateSymbolName(member); return(GetSymbol(symbolName)); }
[Test] public void DuplicateIsInvoked() { RuntimeMember method = GetMethod("duplicate", 1); method.Invoke(new object[] { "stuff" }); method = GetMethod("duplicate", 0); TypedValue result = method.Invoke(new object[] {}); Assert.AreEqual("stuff", result.ToString()); }
[Test] public void MethodIsInvokedViaProcessor() { RuntimeMember method = GetMethodFromProcessor(instance, "countmethod", 0); Assert.IsNotNull(method); TypedValue result = method.Invoke(new object[] {}); Assert.AreEqual(null, result.Value); Assert.AreEqual(typeof(void), result.Type); Assert.AreEqual(1, instance.Count); }
bool HasMatchingParameter(RuntimeMember runtimeMember, NameMatcher name) { for (var i = 0; i < parameterCount; i++) { if (name.Matches(runtimeMember.GetParameterName(i))) { return(true); } } return(false); }
TypedValue CreateWithParameters(Tree <T> parameters, RuntimeType runtimeType) { RuntimeMember member = runtimeType.GetConstructor(parameters.Branches.Count); object[] parameterList = new ParameterList <T>(Processor).GetParameterList(TypedValue.Void, parameters, member); try { return(member.Invoke(parameterList)); } catch (System.Exception e) { throw new CreateException(runtimeType.Type, parameterList.Length, e.InnerException ?? e); } }
object ParseParameterValue(RuntimeMember member, TypedValue instance, Tree <T> parameter, int parameterIndex) { TypedValue parameterValue; try { parameterValue = Processor.Parse(member.GetParameterType(parameterIndex), instance, parameter); } catch (System.Exception e) { throw new ParseException <T>(member.Name, member.GetParameterType(parameterIndex), parameterIndex + 1, parameter.Value, e); } return(parameterValue.Value); }
private static UnityObject ResolveSingle(object target, GameObject gameObject, RuntimeMember member, RequiredAttribute attribute) { var single = (attribute as RequiredSingleAttribute).FromResources ? Resources.FindObjectsOfTypeAll(member.Type).FirstOrDefault() : UnityObject.FindObjectOfType(member.Type); #if dbg Debug.Log("found single " + single + " for " + memberType.Name + " in " + target); #endif return single; }
private static UnityObject ResolveFromTargetRelative(object target, GameObject gameObject, RuntimeMember member, RequiredFromRelativeAttribute attribute, Func<string, GameObject> GetOrAddRelativeAtPath, Func<string, GameObject> GetRelativeAtPath, Func<Type, Component> GetComponentInRelative) { if (member.Type == typeof(GameObject)) { var path = ProcessPath(attribute.Path, member.Name); if (path.IsNullOrEmpty()) return null; if (attribute.Create) return GetOrAddRelativeAtPath(path); try { return GetRelativeAtPath(path); } catch { return null; } } else { Component c = null; var path = attribute.Path; if (path.IsNullOrEmpty()) { c = GetComponentInRelative(member.Type); } else { path = ProcessPath(attribute.Path, member.Name); GameObject relative; if (attribute.Create) relative = GetOrAddRelativeAtPath(path); else { try { relative = GetRelativeAtPath(path); } catch { relative = null; } } if (relative == null) return null; c = relative.GetComponent(member.Type); if (c == null && attribute.Add) { if (member.Type.IsAbstract) Debug.Log("Can't add component `" + member.Type.Name + "` because it's abstract"); else c = relative.AddComponent(member.Type); } } return c; } }
private static UnityObject ResolveFromTargetParents(object target, GameObject gameObject, RuntimeMember member, RequiredAttribute attribute) { return ResolveFromTargetRelative(target, gameObject, member, attribute as RequiredFromRelativeAttribute, gameObject.GetOrAddParentAtPath, gameObject.GetParentAtPath, gameObject.GetComponentInParent); }
private static UnityObject ResolveFromTargetGO(object target, GameObject gameObject, RuntimeMember member, RequiredAttribute attribute) { if (member.Type == typeof(GameObject)) { return gameObject; } var c = gameObject.GetComponent(member.Type); if (c == null) { if ((attribute as RequiredFromThisAttribute).Add) { #if dbg Debug.Log("adding component " + memberType.Name + " to " + gameObject.name); #endif c = gameObject.AddComponent(member.Type); } } return c; }
private static UnityObject DontResolve(object target, GameObject gameObject, RuntimeMember member, RequiredAttribute attribute) { return null; }