private static void ProcessStaticMethodAttributes(System.Type type) { List <string> methodNames = null; List <RuntimeInitializeLoadType> loadTypes = null; MethodInfo[] methods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); for (int i = 0; i < methods.GetLength(0); i++) { MethodInfo element = methods[i]; if (Attribute.IsDefined(element, typeof(RuntimeInitializeOnLoadMethodAttribute))) { RuntimeInitializeLoadType afterSceneLoad = RuntimeInitializeLoadType.AfterSceneLoad; object[] customAttributes = element.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), false); if ((customAttributes != null) && (customAttributes.Length > 0)) { afterSceneLoad = ((RuntimeInitializeOnLoadMethodAttribute)customAttributes[0]).loadType; } if (methodNames == null) { methodNames = new List <string>(); loadTypes = new List <RuntimeInitializeLoadType>(); } methodNames.Add(element.Name); loadTypes.Add(afterSceneLoad); } if (Attribute.IsDefined(element, typeof(InitializeOnLoadMethodAttribute))) { element.Invoke(null, null); } } if (methodNames != null) { StoreRuntimeInitializeClassInfo(type, methodNames, loadTypes); } }
private static void ProcessStaticMethodAttributes(Type type) { List <string> list = null; List <RuntimeInitializeLoadType> list2 = null; MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); for (int i = 0; i < methods.GetLength(0); i++) { MethodInfo methodInfo = methods[i]; if (Attribute.IsDefined(methodInfo, typeof(RuntimeInitializeOnLoadMethodAttribute))) { RuntimeInitializeLoadType item = RuntimeInitializeLoadType.AfterSceneLoad; object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), false); if (customAttributes != null && customAttributes.Length > 0) { item = ((RuntimeInitializeOnLoadMethodAttribute)customAttributes[0]).loadType; } if (list == null) { list = new List <string>(); list2 = new List <RuntimeInitializeLoadType>(); } list.Add(methodInfo.Name); list2.Add(item); } if (Attribute.IsDefined(methodInfo, typeof(InitializeOnLoadMethodAttribute))) { methodInfo.Invoke(null, null); } } if (list != null) { EditorAssemblies.StoreRuntimeInitializeClassInfo(type, list, list2); } }
private static void ProcessRuntimeInitializeOnLoad(MethodInfo method) { RuntimeInitializeLoadType item = RuntimeInitializeLoadType.AfterSceneLoad; object[] customAttributes = method.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), false); if (customAttributes != null && customAttributes.Length > 0) { item = ((RuntimeInitializeOnLoadMethodAttribute)customAttributes[0]).loadType; } EditorAssemblies.StoreRuntimeInitializeClassInfo(method.DeclaringType, new List <string> { method.Name }, new List <RuntimeInitializeLoadType> { item }); }
private static void ProcessRuntimeInitializeOnLoad(MethodInfo method) { RuntimeInitializeLoadType loadType = RuntimeInitializeLoadType.AfterSceneLoad; object[] attrs = method.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), false); if (attrs != null && attrs.Length > 0) { loadType = ((RuntimeInitializeOnLoadMethodAttribute)attrs[0]).loadType; } StoreRuntimeInitializeClassInfo(method.DeclaringType, new List <string>() { method.Name }, new List <RuntimeInitializeLoadType>() { loadType }); }
static void FindRuntimeInitializeOnLoadMethodAttributes(TypeDefinition type, string assemblyName, ref List <RuntimeInitializeClassInfo> classInfoList, ref int methodCount) { if (!type.HasMethods) { return; } foreach (var method in type.Methods) { // RuntimeInitializeOnLoadMethod only works on static methods. if (!method.IsStatic) { continue; } foreach (var attribute in method.CustomAttributes) { if (attribute.AttributeType.FullName == "UnityEngine.RuntimeInitializeOnLoadMethodAttribute") { RuntimeInitializeLoadType loadType = RuntimeInitializeLoadType.AfterSceneLoad; if (attribute.ConstructorArguments != null && attribute.ConstructorArguments.Count > 0) { loadType = (RuntimeInitializeLoadType)attribute.ConstructorArguments[0].Value; } RuntimeInitializeClassInfo classInfo = new RuntimeInitializeClassInfo(); classInfo.assemblyName = assemblyName; classInfo.className = type.FullName; classInfo.methodNames = new[] { method.Name }; classInfo.loadTypes = new[] { loadType }; classInfoList.Add(classInfo); methodCount++; } } } }
private static void OnResetStaticSection() { DrawHeaderLabel("Reset Static"); bool includeInBuild = CecilAttributesSettings.Instance.IncludeResetStaticInBuild; RuntimeInitializeLoadType resetStaticMode = CecilAttributesSettings.Instance.ResetStaticMode; EditorGUI.BeginChangeCheck(); includeInBuild = EditorGUILayout.Toggle(new GUIContent("Include In Build", "If true, the reset static method will be included in the built version."), includeInBuild); if (EditorGUI.EndChangeCheck()) { CecilAttributesSettings.Instance.IncludeResetStaticInBuild = includeInBuild; } EditorGUI.BeginChangeCheck(); resetStaticMode = (RuntimeInitializeLoadType)EditorGUILayout.EnumPopup(new GUIContent("Reset Static Mode", "The type of initialization for resting statics."), resetStaticMode); if (EditorGUI.EndChangeCheck()) { CecilAttributesSettings.Instance.ResetStaticMode = resetStaticMode; } }
private static void SetLoadTime(RuntimeInitializeLoadType loadType) { if (!LoadTime.ContainsKey(loadType)) { LoadTime.Add(loadType, DateTime.Now); LogUtils.Log($"Set {loadType} time to {LoadTime[loadType]}"); } else { throw new TimeParadoxException($"Attempted to set the {nameof(LoadTime)} for {loadType} twice!!"); } //set the load duration if (!LoadDuration.ContainsKey(loadType)) { LoadDuration.Add(loadType, UnityEngine.Time.realtimeSinceStartup); } else { throw new TimeParadoxException($"Attempted to set the {nameof(LoadDuration)} for {loadType} twice!!"); } }
private static void ProcessStaticMethodAttributes(System.Type type) { List <string> methodNames = (List <string>)null; List <RuntimeInitializeLoadType> loadTypes = (List <RuntimeInitializeLoadType>)null; MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); for (int index = 0; index < methods.GetLength(0); ++index) { MethodInfo methodInfo = methods[index]; if (Attribute.IsDefined((MemberInfo)methodInfo, typeof(RuntimeInitializeOnLoadMethodAttribute))) { RuntimeInitializeLoadType initializeLoadType = RuntimeInitializeLoadType.AfterSceneLoad; object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), false); if (customAttributes != null && customAttributes.Length > 0) { initializeLoadType = ((RuntimeInitializeOnLoadMethodAttribute)customAttributes[0]).loadType; } if (methodNames == null) { methodNames = new List <string>(); loadTypes = new List <RuntimeInitializeLoadType>(); } methodNames.Add(methodInfo.Name); loadTypes.Add(initializeLoadType); } if (Attribute.IsDefined((MemberInfo)methodInfo, typeof(InitializeOnLoadMethodAttribute))) { methodInfo.Invoke((object)null, (object[])null); } } if (methodNames == null) { return; } EditorAssemblies.StoreRuntimeInitializeClassInfo(type, methodNames, loadTypes); }
public RuntimeInitializeOnLoadMethodAttribute(RuntimeInitializeLoadType loadType) { this.loadType = loadType; }
} // 0x0000000180581A10-0x0000000180581A30 public RuntimeInitializeOnLoadMethodAttribute(RuntimeInitializeLoadType loadType) { } // 0x0000000180289950-0x0000000180289980
/// <summary> /// <para>Allow an runtime class method to be initialized when Unity game loads runtime without action from the user.</para> /// </summary> /// <param name="loadType">RuntimeInitializeLoadType: Before or After scene is loaded.</param> public RuntimeInitializeOnLoadMethodAttribute() { this.loadType = RuntimeInitializeLoadType.AfterSceneLoad; }
private void GenerateInitObjects(GeneratorExecutionContext context, CreateOnInitSyntaxReceiver createOnInitSyntaxReceiver) { //Get the CreateOnInit attribute, and make sure it exits. The only assembly that will have this is the main TC one INamedTypeSymbol onInitAttribute = context.Compilation.GetTypeByMetadataName("Team_Capture.CreateOnInit"); if (onInitAttribute == null) { return; } //TODO: Check if class inherits from mono behaviour INamedTypeSymbol monoBehaviourSymbol = context.Compilation.GetTypeByMetadataName("UnityEngine.MonoBehaviour"); if (monoBehaviourSymbol == null) { return; } SemanticModel model = null; foreach (ClassDeclarationSyntax classDeclaration in createOnInitSyntaxReceiver.Classes) { //Get model if (model == null || model.SyntaxTree != classDeclaration.SyntaxTree) { model = context.Compilation.GetSemanticModel(classDeclaration.SyntaxTree); } ISymbol classSymbol = ModelExtensions.GetDeclaredSymbol(model, classDeclaration); //Make sure the class has the CreateOnInit attribute AttributeData attributeData = classSymbol?.GetAttributes().FirstOrDefault(x => SymbolEqualityComparer.Default.Equals(x.AttributeClass, onInitAttribute)); if (attributeData == null) { continue; } //Check if the class is partial or not bool isPartial = classDeclaration.Modifiers .Any(m => m.IsKind(SyntaxKind.PartialKeyword)); if (!isPartial) { context.ReportDiagnostic(Diagnostic.Create(DiagnosticHelper.NotPartialDiagnostic, Location.None)); continue; } string visibility = classDeclaration.Modifiers.GetIfModiferIncludesPublicOrInternal(); string className = classSymbol.Name; //TODO: We should check that the argument exists and has no arguments //Get what method to call on init TypedConstant callOnInitData = attributeData.GetArgument(CallOnInitArgumentName); string callOnInit = callOnInitData.GetValueOrDefault <string>(null); //Get the object's name TypedConstant objectNameOverride = attributeData.GetArgument(ObjectNameOverrideName); string objectName = objectNameOverride.GetValueOrDefault(className); //Get what our load type is TypedConstant loadTypeData = attributeData.GetArgument(LoadTypeName); RuntimeInitializeLoadType loadType = loadTypeData.GetValueOrDefault(RuntimeInitializeLoadType.BeforeSceneLoad); //Generate code string code = Template.Parse(CodeTemplates.CreateOnInitTemplate).Render(new { Namespace = classSymbol.ContainingNamespace.ToString(), Visiblity = visibility, Classname = className, Loadtype = loadType.ToString(), Objectname = objectName, Methodtocall = callOnInit == null ? string.Empty : $"{callOnInit}();" }); context.AddSource($"{className}.gen", code); } }
public AutoInitializedAttribute(RuntimeInitializeLoadType initializeLoadType) { InitializeLoadType = initializeLoadType; }