private void ValidateFolder(ResourceFolder folder)
        {
            // Search for resources with the same name as a folder in the same directory
            for (var i = 0; i < folder.Resources.Count; i++)
            {
                var r = folder.Resources[i];

                if (folder.Folders.Any(p => p.Name == r.Name))
                {
                    TSLog.LogWarning(LogCategory.Compile,
                                     string.Format("Name for resource at [{0}] conflicts with a folder of the same name.", r.FullPath),
                                     AssetDatabase.LoadAssetAtPath(r.FullPath, typeof(Object)));

                    // Remove existing resource
                    if (!Remove(r))
                    {
                        throw new Exception("Fatal error, failed removing resource from ResourceDatabase. ");
                    }

                    // Add again with new name
                    folder.Resources.Add(new ResourceDefinition("_" + r.Name, r.Path, r.FullPath, r.Type));
                }
            }

            foreach (var resourceFolder in folder.Folders)
            {
                ValidateFolder(resourceFolder);
            }
        }
        public static bool GetAssetType(string assetPath, out Type type, out TypeSource source)
        {
            var ext = Path.GetExtension(assetPath);

            if (!string.IsNullOrEmpty(ext))
            {
                if (_knownTypes.TryGetValue(ext.ToLower(), out type))
                {
                    source = TypeSource.FileName;
                    return(true);
                }
            }

            if (AssetTypeCache.GetAssetType(assetPath, out type))
            {
                source = TypeSource.Cache;
                return(true);
            }

            source = TypeSource.Unity;
            var asset = AssetDatabase.LoadMainAssetAtPath(assetPath);

            if (asset == null)
            {
                TSLog.LogWarning(LogCategory.Trace, string.Format("Failed loading asset at path [{0}].", assetPath));
                type = null;
                return(false);
            }

            type = asset.GetType();
            AssetTypeCache.PostAssetType(assetPath, type);

            return(true);
        }
        public void Add(ResourceDefinition definition)
        {
            var folder = GetFolderForPath(definition.Path);

            if (definition.Name == folder.Name)
            {
                TSLog.LogWarning(LogCategory.Compile, Strings.Warning_ResourceNameCannotBeSameAsParent,
                                 AssetDatabase.LoadAssetAtPath(definition.FullPath, typeof(Object)));

                definition = new ResourceDefinition("_" + definition.Name, definition.Path, definition.FullPath,
                                                    definition.Type);
            }

            foreach (var p in folder.Resources)
            {
                if (p.Name == definition.Name)
                {
                    TSLog.LogError(LogCategory.Compile,
                                   string.Format("Name for resource at [{0}] conflicts with another resource.", definition.FullPath),
                                   AssetDatabase.LoadAssetAtPath(definition.FullPath, typeof(Object)));

                    TSLog.LogError(LogCategory.Compile, string.Format("  \\_ Other Resource: {0}", p.FullPath),
                                   AssetDatabase.LoadAssetAtPath(p.FullPath, typeof(Object)));

                    return;
                }
            }

            folder.Resources.Add(definition);
        }
        private ResourceFolder GetFolderForPath(string path)
        {
            var components = path.Split('/');

            var f = RootFolder;

            for (var i = 0; i < components.Length - 1; i++)
            {
                var c = CompilerUtil.GetSafeName(components[i], true);

                var isDuplicate = f.Name == c;
                var folderName  = isDuplicate ? "_" + c : c;

                var folder = f.Folders.FirstOrDefault(p => p.Name == folderName);

                if (folder == null)
                {
                    var folderPath = components.Take(i).ToArray();

                    // Post warning the first time this folder is encountered
                    if (isDuplicate)
                    {
                        TSLog.LogWarning(LogCategory.Compile,
                                         string.Format(Strings.Warning_FolderNameCannotBeSameAsParent, string.Join("/", folderPath)));
                    }

                    folder = new ResourceFolder(folderName, string.Join("/", folderPath));
                    f.Folders.Add(folder);
                }

                f = folder;
            }

            return(f);
        }
        private static void PopulateClass(TypeSafeDataUnit dataUnit, CodeTypeDeclaration container)
        {
            foreach (var nestedUnit in dataUnit.NestedUnits)
            {
                string className;

                if (!CompilerUtil.GetSafeNameAndVerifyNotDuplicate(nestedUnit.ClassName, container, out className))
                {
                    continue;
                }

                if (className == container.Name)
                {
                    TSLog.LogWarning(LogCategory.Compile, Strings.Warning_NameCannotBeSameAsParent);
                    className = "_" + className;

                    // Check for duplicates again after having added the '_'
                    if (!CompilerUtil.GetSafeNameAndVerifyNotDuplicate(className, container, out className))
                    {
                        continue;
                    }
                }

                var nestedContainer = CompilerUtil.CreateStaticType(className);
                PopulateClass(nestedUnit, nestedContainer);

                container.Members.Add(nestedContainer);
            }

            PopulateClassMembers(dataUnit, container);
        }
        public CodeCompileUnit Compile(TypeSafeDataUnit dataUnit)
        {
            var compileUnit = new CodeCompileUnit();

            compileUnit.UserData.Add(Strings.CompileUnitUserDataKey, dataUnit.FileName);

            CompilerUtil.WriteHeader(compileUnit);

            var ns = new CodeNamespace(Namespace);

            compileUnit.Namespaces.Add(ns);

            var className = CompilerUtil.GetSafeName(dataUnit.ClassName, false, false);

            if (className != dataUnit.ClassName)
            {
                TSLog.LogWarning(LogCategory.Compile,
                                 string.Format("Class name was modified to conform to C# standards ({0} -> {1})", dataUnit.ClassName, className));
            }

            var container = CompilerUtil.CreateStaticType(className);

            CompilerUtil.AddTypeSafeTag(container);

            PopulateClass(dataUnit, container);

            ns.Types.Add(container);
            return(compileUnit);
        }
示例#7
0
        public static void DrawLogo(Texture2D logo)
        {
            if (logo == null)
            {
                TSLog.LogWarning(LogCategory.Trace, "Logo is null");
                return;
            }

            var rect = EditorGUILayout.BeginVertical();

            GUILayout.Space(20);

            EditorGUILayout.BeginHorizontal();

            GUILayout.FlexibleSpace();

            GUI.DrawTexture(
                GUILayoutUtility.GetRect(logo.width, logo.width, logo.height, logo.height, GUILayout.ExpandHeight(false),
                                         GUILayout.ExpandWidth(false)),
                logo);

            GUILayout.FlexibleSpace();

            EditorGUILayout.EndHorizontal();

            GUILayout.Space(15);

            EditorGUILayout.EndVertical();

            var size = EditorStyles.miniLabel.CalcSize(new GUIContent(Strings.Version));

            GUI.Label(new Rect(rect.xMax - size.x, rect.yMax - size.y, size.x, size.y), Strings.Version,
                      EditorStyles.miniLabel);
        }
        public TypeSafeDataUnit GetTypeSafeDataUnit()
        {
            var data = new List <TypeSafeDataEntry>();

            TSLog.Log(LogCategory.Scanner, "Beginning Scene Scan");

            var i = 0;

            var scenes = new List <string>();

            foreach (var scene in EditorBuildSettings.scenes)
            {
                if (!Settings.Instance.IncludeNonActiveScenes && !scene.enabled)
                {
                    continue;
                }

                if (scenes.Any(p => p == scene.path))
                {
                    TSLog.Log(LogCategory.Scanner, string.Format("Duplicate Scene: {0}, {1}", i, scene.path));
                    continue;
                }

                var name = Path.GetFileNameWithoutExtension(scene.path);

                TSLog.Log(LogCategory.Scanner, string.Format("Scene: {0} (name={1}, isEnabled: {2})", scene.path, name, scene.enabled));

                if (string.IsNullOrEmpty(name))
                {
                    TSLog.LogWarning(LogCategory.Scanner, "Scene name is empty, skipping.");
                    continue;
                }

                scenes.Add(scene.path);

                var warning = scene.enabled ? null : Strings.Warning_NonActiveScene;

                data.Add(new TypeSafeDataEntry(name, new object[] { name, scene.enabled ? i : -1 },
                                               obsoleteWarning: warning));

                if (scene.enabled)
                {
                    i++;
                }
            }

            TSLog.Log(LogCategory.Scanner, "Scene Scan Complete");

            return(new TypeSafeDataUnit(TypeSafeUtil.GetFinalClassName(Strings.ScenesTypeName), typeof(Scene), data,
                                        true,
                                        "Scenes"));
        }
        public static string GetSafeName(string name, bool suppressWarning = false, bool checkReserved = true)
        {
            var n = GetSafeNameInternal(name);

            if (checkReserved && IsReservedName(n))
            {
                if (!suppressWarning)
                {
                    TSLog.LogWarning(LogCategory.Compile,
                                     string.Format("\"{0}\" is reserved by TypeSafe. Will have underscore prefixed. [{1}] -> [_{2}]", n, name, n));
                }

                n = "_" + n;
            }

            return(n);
        }
        private TypeSafeDataUnit CreateAnimatorDataUnit(AnimatorController controller)
        {
            var unit = new TypeSafeDataUnit(controller.name, typeof(string));

            var parametersUnit = new TypeSafeDataUnit("Parameters", typeof(int));

            foreach (var param in controller.parameters)
            {
                TSLog.Log(LogCategory.Trace, string.Format("Parameter: {0} ({1})", param.name, param.nameHash));

                if (parametersUnit.Data.Any(p => p.PropertyName == param.name))
                {
                    TSLog.LogWarning(LogCategory.Compile, string.Format("[AnimatorController] Duplicate parameter name ({0}).", param.name), controller);
                    continue;
                }

                parametersUnit.Data.Add(new TypeSafeDataEntry(param.name, new object[] { param.nameHash }));
            }

            var layersUnit = new TypeSafeDataUnit("Layers", typeof(int));

            for (var i = 0; i < controller.layers.Length; i++)
            {
                var layer = controller.layers[i];
                TSLog.Log(LogCategory.Trace, string.Format("Layer: {0}, Index: {1}", layer.name, i));

                if (layersUnit.Data.Any(p => p.PropertyName == layer.name))
                {
                    TSLog.LogWarning(LogCategory.Compile, string.Format("[AnimatorController] Duplicate layer name ({0}).", layer.name),
                                     controller);
                    continue;
                }

                layersUnit.Data.Add(new TypeSafeDataEntry(layer.name, new object[] { i }));
            }

            unit.NestedUnits.Add(parametersUnit);
            unit.NestedUnits.Add(layersUnit);

            return(unit);
        }
        private TypeSafeDataUnit CreateMixerDataUnit(AudioMixerControllerWrapper wrapper)
        {
            var unit = new TypeSafeDataUnit(wrapper.Mixer.name, typeof(string));

            var parametersUnit = new TypeSafeDataUnit("Parameters", typeof(string));

            foreach (var param in wrapper.exposedParameters)
            {
                TSLog.Log(LogCategory.Trace, string.Format("Parameter: {0}", param.name));

                if (parametersUnit.Data.Any(p => p.PropertyName == param.name))
                {
                    TSLog.LogWarning(LogCategory.Compile, string.Format("[AudioMixer] Duplicate parameter name ({0}).", param.name), wrapper.Mixer);
                    continue;
                }

                parametersUnit.Data.Add(new TypeSafeDataEntry(param.name, new object[] { param.name }));
            }

            var snapshotsUnit = new TypeSafeDataUnit("Snapshots", typeof(string));

            foreach (var snapshot in wrapper.snapshots)
            {
                TSLog.Log(LogCategory.Trace, string.Format("Snapshot: {0}", snapshot.name));

                if (snapshotsUnit.Data.Any(p => p.PropertyName == snapshot.name))
                {
                    TSLog.LogWarning(LogCategory.Compile, string.Format("[AudioMixer] Duplicate snapshot name ({0}).", snapshot.name), wrapper.Mixer);
                    continue;
                }

                snapshotsUnit.Data.Add(new TypeSafeDataEntry(snapshot.name, new object[] { snapshot.name }));
            }

            unit.NestedUnits.Add(parametersUnit);
            unit.NestedUnits.Add(snapshotsUnit);

            return(unit);
        }
        private static void LoadCache()
        {
            if (_typeCache != null)
            {
                return;
            }

            TSLog.Log(LogCategory.Trace, "[AssetTypeCache] Loading Asset Type Cache");

            _typeCache = new Dictionary <string, Type>();

            var cache = TypeCache.Instance.Contents;

            if (cache == null)
            {
                return;
            }

            for (var i = 0; i < cache.Length; i++)
            {
                var type = Type.GetType(cache[i].Type, false);

                if (type == null)
                {
                    TSLog.LogWarning(LogCategory.Trace,
                                     string.Format("[AssetTypeCache] Type from cache was not found (path: {0}, type: {1})", cache[i].Path, cache[i].Type));
                    continue;
                }

                if (_typeCache.ContainsKey(cache[i].Path))
                {
                    TSLog.LogError(LogCategory.Trace, string.Format("[AssetTypeCache] Duplicate path in type cache ({0})", cache[i].Path));
                    continue;
                }

                _typeCache.Add(cache[i].Path, type);
            }
        }