Пример #1
0
        public static void LoadAssemblies(bool force = false)
        {
            if (msAssemblyTypeCaches.Count > 0 && !force)
            {
                return;
            }
            msAssemblyTypeCaches.Clear();

            Assembly[] allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            if (allAssemblies == null)
            {
                return;
            }

            ProfilingUtility.BeginSample("GlobalReflectionCache.LoadAssemblies");
            foreach (Assembly assembly in allAssemblies)
            {
                if (assembly.FullName.Contains("Microsoft."))
                {
                    continue;
                }

                //DebugUtility.LogTrace(LoggerTags.Engine, "GlobalReflectionCache.CollectTypes {0}", assembly.FullName);
                ProfilingUtility.BeginSample("GlobalReflectionCache.CollectTypes_", assembly.FullName);
                GlobalReflectionCache.CollectTypes(assembly);
                ProfilingUtility.EndSample();
            }
            ProfilingUtility.EndSample();
        }
Пример #2
0
        public static List <string> CollectTags()
        {
            if (msAllLoggerTags.Count > 0)
            {
                return(msAllLoggerTags);
            }

            var types = GlobalReflectionCache.FindTypes <LoggerTagsAttribute>(true);

            if (types != null)
            {
                foreach (var tagType in types)
                {
                    var allFields = tagType.GetFields();
                    foreach (var fieldInfo in allFields)
                    {
                        if (fieldInfo.IsStatic && fieldInfo.FieldType == typeof(string))
                        {
                            string o = fieldInfo.GetValue(null) as string;
                            if (!string.IsNullOrEmpty(o))
                            {
                                msAllLoggerTags.Union(o);
                            }
                        }
                    }
                }
            }

            // DebugUtility.LogTrace(LoggerTags.Engine, "The Logger tags count is {0}", msAllLoggerTags.Count);
            return(msAllLoggerTags);
        }
Пример #3
0
 private static void Registers()
 {
     if (!msInitialized)
     {
         msInitialized = true;
         GlobalReflectionCache.LoadAssemblies();
         NameToTypeUtility.Initialize();
         var allModules = GlobalReflectionCache.FindTypes <ModuleAttribute>(false);
         if (allModules != null)
         {
             allModules.Sort(EngineComparers.defaultModuleAttributeComparer);
             foreach (var m in allModules)
             {
                 //DebugUtility.Log(LoggerTags.Engine, "Module {0} Loaded.", m);
                 ModuleAttribute attr = m.GetCustomAttribute <ModuleAttribute>(true);
                 if (!string.IsNullOrEmpty(attr.entryPoint))
                 {
                     var entry = m.GetMethod(attr.entryPoint, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
                     if (entry != null)
                     {
                         //DebugUtility.Log(LoggerTags.Engine, "Module {0} Entry.", m);
                         entry.Invoke(null, null);
                     }
                 }
             }
         }
     }
 }
Пример #4
0
        protected void DrawArrowCaps()
        {
            if (!(target is UObject))
            {
                return;
            }

            UObject t = (UObject)target;

            var attr = GlobalReflectionCache.FindOrAdd(target.GetType()).GetCustomAttribute <DrawArrowCapAttribute>();

            if (attr != null)
            {
                LokiSceneUtility.DrawArrowCap(t.transform);
            }
        }
Пример #5
0
        private static void AutoCollectDebugFilters()
        {
            var loggerTypes = GlobalReflectionCache.FindTypes <LoggerAttribute>(true);

            if (loggerTypes != null && loggerTypes.Count > 0)
            {
                foreach (var type in loggerTypes)
                {
                    string typeFileName = string.Concat(type.Name, ".cs");
                    msBlacklist.Union(typeFileName);
                    msFilterStrings.Union(typeFileName);

                    var loggerAttr = type.GetCustomAttribute <LoggerAttribute>(true);
                    msBlacklist.Union(loggerAttr.loggerBlacklist);
                }
            }
        }
Пример #6
0
        public static void Initialize(bool force = false)
        {
            if (mInitialize)
            {
                return;
            }
            mInitialize = true;

            var types = GlobalReflectionCache.FindTypes <NameToTypeAttribute>(false, msAdditionalTypes);

            foreach (var item in types)
            {
                RegisterType(item);
            }
#if UNITY_EDITOR
            DebugUtility.Log(LoggerTags.Engine, "Register types : {0}", types.Select(type => type.Name));
#endif
        }
Пример #7
0
        private void DoRegister(IConsoleObject o)
        {
            Type type = o.GetType();

            if (!mConsoleInfos.TryGetValue(type, out var consoleInfo))
            {
                consoleInfo = new List <IConsoleObject>();
                mConsoleInfos.Add(type, consoleInfo);
            }

            consoleInfo.Union(o);

            if (mSearchedType.Add(type))
            {
                var reflectTypeInfo = GlobalReflectionCache.FindOrAdd(type);
                var methods         = reflectTypeInfo.FindMethods <ConsoleMethodAttribute>();

                foreach (var m in methods)
                {
                    var        attr     = m.GetCustomAttribute <ConsoleMethodAttribute>();
                    MethodInfo validate = null;
                    if (!string.IsNullOrEmpty(attr.validate))
                    {
                        validate = type.GetMethod(attr.validate);
                    }

                    if (validate != null)
                    {
                        if (validate.IsStatic != m.IsStatic)
                        {
                            DebugUtility.LogError(LoggerTags.Console, "The Method [{0}] and the validate [{1}] must be the same modifier (ex. static)", m.Name, validate.Name);
                            continue;
                        }

                        if (validate.DeclaringType != typeof(bool))
                        {
                            DebugUtility.LogError(LoggerTags.Console, "The Validate [{0}] must return bool (true or false)", validate.Name);
                            continue;
                        }
                    }

                    string cmd = attr.aliasName;
                    if (string.IsNullOrEmpty(cmd))
                    {
                        cmd = string.Concat(type.FullName, ".", m.Name);
                    }

                    if (!mCommandInfos.TryGetValue(cmd, out var cmdGroup))
                    {
                        cmdGroup = new List <ConsoleCommand>();
                        mCommandInfos.Add(cmd, cmdGroup);
                    }
                    else
                    {
                        if (cmdGroup.FindIndex(command => command.owner == type) >= 0)
                        {
                            DebugUtility.LogError(LoggerTags.Engine, "The command is existed, it is not allowed : {0}", string.Concat(type.FullName, ".", m.Name));
                            continue;
                        }
                    }

                    ConsoleCommand entry;
                    entry.owner      = type;
                    entry.attribute  = attr;
                    entry.validate   = validate;
                    entry.memberInfo = m;
                    entry.cmd        = cmd;
                    cmdGroup.Add(entry);
                }

                var fieldsOrProperties = reflectTypeInfo.FindMembers <ConsoleMemberAttribute>();
                foreach (var m in fieldsOrProperties)
                {
                    var        attr     = m.GetCustomAttribute <ConsoleMemberAttribute>();
                    MethodInfo validate = null;
                    if (!string.IsNullOrEmpty(attr.validate))
                    {
                        validate = type.GetMethod(attr.validate);
                    }

                    if (validate != null)
                    {
                        bool memberIsStatic = false;
                        if (m.MemberType == MemberTypes.Field)
                        {
                            memberIsStatic = ((FieldInfo)m).IsStatic;
                        }
                        else if (m.MemberType == MemberTypes.Property)
                        {
                            memberIsStatic = ((PropertyInfo)m).IsStatic();
                        }
                        if (validate.IsStatic != memberIsStatic)
                        {
                            DebugUtility.LogError(LoggerTags.Console, "The Field [{0}] and the validate [{1}] must be the same modifier (ex. static)", m.Name, validate.Name);
                            continue;
                        }

                        if (validate.DeclaringType != typeof(bool))
                        {
                            DebugUtility.LogError(LoggerTags.Console, "The Validate [{0}] must return bool (true or false)", validate.Name);
                            continue;
                        }
                    }

                    string cmd = attr.aliasName;
                    if (string.IsNullOrEmpty(cmd))
                    {
                        cmd = string.Concat(type.FullName, ".", m.Name);
                    }

                    if (!mCommandInfos.TryGetValue(cmd, out var cmdGroup))
                    {
                        cmdGroup = new List <ConsoleCommand>();
                        mCommandInfos.Add(cmd, cmdGroup);
                    }
                    else
                    {
                        if (cmdGroup.FindIndex(command => command.owner == type) >= 0)
                        {
                            DebugUtility.LogError(LoggerTags.Engine, "The command is existed, it is not allowed : {0}", string.Concat(type.FullName, ".", m.Name));
                            continue;
                        }
                    }

                    ConsoleCommand entry;
                    entry.owner      = type;
                    entry.attribute  = attr;
                    entry.validate   = validate;
                    entry.memberInfo = m;
                    entry.cmd        = cmd;

                    cmdGroup.Add(entry);
                }
            }
        }
Пример #8
0
        public static void CheckEngineConfig()
        {
            var moduleConfigs = GlobalReflectionCache.FindTypes(typeof(ModuleSettings), false);

            if (moduleConfigs == null)
            {
                return;
            }

            foreach (var config in moduleConfigs)
            {
                var getOrLoadMethod = config.GetMethod("GetOrLoad");
                if (getOrLoadMethod == null)
                {
                    DebugUtility.LogErrorTrace(LoggerTags.Engine, "The {0} must implement the static Method [public static ConfigSettingsType GetOrLoad()]", config.Name);
                    continue;
                }

                string         ModuleSettingsPath = null;
                ModuleSettings settings           = getOrLoadMethod.Invoke(null, null) as ModuleSettings;
                if (settings == null)
                {
                    settings = ScriptableObject.CreateInstance(config) as ModuleSettings;
                    if (settings == null)
                    {
                        DebugUtility.LogErrorTrace(LoggerTags.Engine, "The {0} must implement the static Method [public static From_ModuleSettings GetOrLoad()]", config.Name);
                        continue;
                    }

                    EFilePathType pathType = EFilePathType.EngineGeneratedConfigPath;                    // settings is EditorModuleSettings ? EFilePathType.EditorGeneratedConfigPath : EFilePathType.EngineGeneratedConfigPath;
                    string        path     = FileSystem.Get().GetAssetPathCheck(pathType, settings.GetAssetFileName(), true);
                    AssetDatabase.CreateAsset(settings, path);
                    settings.OnCreated();

                    ModuleSettingsPath = path;
                }
                else
                {
                    EFilePathType pathType = EFilePathType.EngineGeneratedConfigPath;                    // settings is EditorModuleSettings ? EFilePathType.EditorGeneratedConfigPath : EFilePathType.EngineGeneratedConfigPath;
                    ModuleSettingsPath = FileSystem.Get().GetAssetPathCheck(pathType, settings.GetAssetFileName(), true);
                }

                bool reimport = false;
                if (settings != null)
                {
                    UnityEngine.Object[] subAssets  = AssetDatabase.LoadAllAssetsAtPath(ModuleSettingsPath);
                    FieldInfo[]          fieldInfos = config.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                    foreach (var subAsset in subAssets)
                    {
                        bool isValid = false;
                        if (subAsset != null)
                        {
                            if (subAsset.GetType() == settings.GetType())
                            {
                                isValid = true;
                                continue;
                            }

                            foreach (var info in fieldInfos)
                            {
                                Type fieldType = info.FieldType;
                                if (fieldType.IsSubclassOf(typeof(UAssetObject)) && !fieldType.IsAbstract)
                                {
                                    if (subAsset.GetType() == fieldType)
                                    {
                                        isValid = true;

                                        object fieldValue = info.GetValue(settings);
                                        if (fieldValue == null)
                                        {
                                            info.SetValue(settings, subAsset);
                                        }
                                        break;
                                    }
                                }
                            }
                        }

                        if (!isValid)
                        {
                            AssetDatabase.RemoveObjectFromAsset(subAsset);
                            UnityEngine.Object.DestroyImmediate(subAsset, true);
                            reimport = true;
                        }
                    }

                    foreach (var info in fieldInfos)
                    {
                        if (info.IsPublic || info.GetCustomAttribute <SerializableAttribute>() != null)
                        {
                            Type fieldType = info.FieldType;
                            if (fieldType.IsSubclassOf(typeof(UAssetObject)) && !fieldType.IsAbstract)
                            {
                                object fieldValue = info.GetValue(settings);
                                if (fieldValue == null)
                                {
                                    reimport = true;
                                    UAssetObject asset = asset = ScriptableObject.CreateInstance(fieldType) as UAssetObject;
                                    if (asset != null)
                                    {
                                        asset.name = asset.GetType().Name;
                                        info.SetValue(settings, asset);

                                        if (!asset.isSubAsset)
                                        {
                                            EFilePathType pathType = EFilePathType.EngineGeneratedConfigPath;                                             // asset is EditorModuleSettings ? EFilePathType.EditorGeneratedConfigPath : EFilePathType.EngineGeneratedConfigPath;
                                            string        path     = FileSystem.Get().GetAssetPathCheck(pathType, asset.GetAssetFileName(), true);
                                            AssetDatabase.CreateAsset(asset, path);
                                        }
                                        else
                                        {
                                            AssetDatabase.AddObjectToAsset(asset, ModuleSettingsPath);
                                        }
                                        settings.OnCreated();
                                    }
                                }
                            }
                        }
                    }
                }

                if (reimport)
                {
                    AssetDatabase.ImportAsset(ModuleSettingsPath, ImportAssetOptions.Default);
                }
            }
        }
Пример #9
0
        protected virtual void OnDrawInspectorPreview()
        {
            if (target == null)
            {
                return;
            }

            Type targetType  = target.GetType();
            var  memberInfos = GlobalReflectionCache.FindOrAdd(targetType).FindMembers <PreviewMemberBaseAttribute>();

            if (memberInfos == null || memberInfos.Count <= 0)
            {
                return;
            }

            DrawLine();
            foreach (var m in memberInfos)
            {
                PreviewMemberBaseAttribute attr = m.GetCustomAttribute <PreviewMemberBaseAttribute>(true);
                TooltipAttribute           ta   = m.GetCustomAttribute <TooltipAttribute>(true);
                Type   drawType = null;
                object value    = null;
                Action <object, object> setValue = null;
                bool readonlyMember = false;

                if (m is FieldInfo)
                {
                    var field = (FieldInfo)m;
                    value          = field.GetValue(target);
                    drawType       = field.FieldType;
                    setValue       = field.SetValue;
                    readonlyMember = field.IsInitOnly;
                }
                else if (m is PropertyInfo)
                {
                    var property = (PropertyInfo)m;
                    value          = property.GetValue(target);
                    drawType       = property.PropertyType;
                    setValue       = property.SetValue;
                    readonlyMember = !property.CanWrite;
                }
                else
                {
                    continue;
                }

                string tooltip = ta != null ? ta.tooltip : string.Empty;
                if (string.IsNullOrEmpty(tooltip))
                {
                    var lta = m.GetCustomAttribute <LokiTooltipAttribute>(true);
                    tooltip = lta != null ? lta.tooltip : string.Empty;
                }
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(new GUIContent(m.Name, tooltip), WidthPercent(0.4f));

                EditorGUI.BeginChangeCheck();
                var lastGUIEnable = GUI.enabled;
                GUI.enabled = !readonlyMember;

                if (attr.useRange)
                {
                    float rangeMin = 0.0f;
                    float rangeMax = 100000.0f;
                    if (attr is PreviewMemberAttribute)
                    {
                        PreviewMemberAttribute real = (PreviewMemberAttribute)attr;
                        rangeMin = real.rangeMin;
                        rangeMax = real.rangeMax;
                    }
                    else if (attr is PreviewMemberDynamicPropertyAttribute)
                    {
                        PreviewMemberDynamicPropertyAttribute real = (PreviewMemberDynamicPropertyAttribute)attr;
                        if (drawType == typeof(float) || drawType == typeof(int))
                        {
                            if (!string.IsNullOrEmpty(real.rangeMin))
                            {
                                rangeMin = (float)targetType.GetProperty(real.rangeMin).GetValue(target);
                            }
                            if (!string.IsNullOrEmpty(real.rangeMax))
                            {
                                rangeMax = (float)targetType.GetProperty(real.rangeMax).GetValue(target);
                            }
                        }
                    }
                    else if (attr is PreviewMemberDynamicFieldAttribute)
                    {
                        PreviewMemberDynamicFieldAttribute real = (PreviewMemberDynamicFieldAttribute)attr;
                        if (drawType == typeof(float) || drawType == typeof(int))
                        {
                            if (!string.IsNullOrEmpty(real.rangeMin))
                            {
                                rangeMin = (float)targetType.GetField(real.rangeMin).GetValue(target);
                            }
                            if (!string.IsNullOrEmpty(real.rangeMax))
                            {
                                rangeMax = (float)targetType.GetField(real.rangeMax).GetValue(target);
                            }
                        }
                    }
                    value = EditorGUILayoutObjectField(drawType, value, rangeMin, rangeMax);
                }
                else
                {
                    value = EditorGUILayoutObjectField(drawType, value);
                }
                GUI.enabled = lastGUIEnable;
                if (EditorGUI.EndChangeCheck())
                {
                    setValue(target, value);
                }

                EditorGUILayout.EndHorizontal();
            }
        }
Пример #10
0
        protected static void AutoSerializing(object t)
        {
            Type    targetType = t.GetType();
            UObject target     = t as UObject;

            if (target == null)
            {
                return;
            }

            var fieldsInfo = GlobalReflectionCache.FindOrAdd(targetType).FindSerializeFields();

            if (fieldsInfo == null)
            {
                return;
            }

            foreach (var field in fieldsInfo)
            {
                bool fieldIsGameObject = (field.FieldType == typeof(GameObject));
                bool fieldIsComponent  = (field.FieldType.IsSubclassOf(typeof(Component)));

                string fieldName = field.Name;
                var    attr      = field.GetCustomAttribute <AutoSerializeFieldAttribute>(true);
                if (attr != null)
                {
                    if (!string.IsNullOrEmpty(attr.aliasName))
                    {
                        fieldName = attr.aliasName;
                    }
                    else
                    {
                        string autoRemovePrefix = attr.autoRemovePrefix;
                        if (!string.IsNullOrEmpty(autoRemovePrefix))
                        {
                            if (fieldName.Length > autoRemovePrefix.Length && fieldName.StartsWith(autoRemovePrefix))
                            {
                                fieldName = fieldName.Substring(autoRemovePrefix.Length);
                            }
                        }
                    }
                }

                if (fieldIsComponent || fieldIsGameObject)
                {
                    Transform tr = target.transform.FindUnique(fieldName, StringComparison.OrdinalIgnoreCase);
                    if (tr == null)
                    {
                        if (fieldName.StartsWith("m"))
                        {
                            tr = target.transform.FindUnique(fieldName.Substring(1), StringComparison.OrdinalIgnoreCase);
                        }
                    }
                    if (tr != null)
                    {
                        if (fieldIsGameObject)
                        {
                            field.SetValue(target, tr.gameObject);
                        }
                        else if (fieldIsComponent)
                        {
                            var fieldComponent = tr.GetComponent(field.FieldType);
                            if (fieldComponent != null)
                            {
                                field.SetValue(target, fieldComponent);
                            }
                        }
                        else                         // todo
                        {
                        }
                    }
                }
            }
        }