Exemplo n.º 1
0
        /// <summary>
        /// Get the Type of a class by the name of the class.
        /// </summary>
        /// <param name="path">The path of the meta file we're getting the name from</param>
        /// <returns></returns>
        private string getTypeByMetafileFileName(string path)
        {
            string fileName = Path.GetFileName(path);

            fileName = fileName.Replace(".cs.meta", "");
            string fileNameLower = fileName.ToLower();


            if (cachedLowerTypeList == null)
            {
                cachedLowerTypeList = assemblies.SelectMany(assembly => assembly.GetTypes())
                                      .Select(type => new KeyValuePair <string, Type>(type.Name.ToLower(), type)).ToArray();
            }

            Type[] types = cachedLowerTypeList.Where(loweredTypeName => loweredTypeName.Key == fileNameLower)
                           .Select(pair => pair.Value).ToArray();

            if (types.Length == 0)
            {
                Debug.Log("Checked for type  \"" + fileName +
                          "\" no types were found.");
                return(null);
            }

            if (types.Length == 1)
            {
                return(types[0].FullName);
            }

            // Check if they're monoBehaviours and if they are return those.
            List <Type> monoBehaviours = new List <Type>();

            foreach (Type type in types)
            {
                if (type.IsSubclassOf(typeof(MonoBehaviour)) &&
                    // Apparently we sometimes use the same dll in the same project causing the same classes(including same namespace), using the same name.
                    // As this causes the same fileID we just use the first one
                    monoBehaviours.FirstOrDefault(mono => mono.Name == type.Name) == null)
                {
                    monoBehaviours.Add(type);
                }
            }

            if (monoBehaviours.Count == 0)
            {
                Debug.LogWarning("Class : " + fileName +
                                 " could not be found and is not an MonoBehaviour so will skip");
                return(null);
            }

            if (monoBehaviours.Count == 1)
            {
                return(monoBehaviours[0].FullName);
            }

            string[] options = monoBehaviours.Select(type => type.FullName).ToArray();
            return(MigrationWindow.OpenOptionsWindow("Class cannot be found, select which one to choose", fileName,
                                                     options));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Finds the new GUID and fileID from the old IDs and the new IDs by checking for the classname in both
        /// </summary>
        /// <param name="newIDs"></param>
        /// <param name="old"></param>
        /// <returns></returns>
        private ClassModel findNewID(List <ClassModel> newIDs, ClassModel old)
        {
            if (old == null)
            {
                throw new NullReferenceException("Old ClassData cannot be null in the findNewID");
            }

            // Check if there is an exact match
            ClassModel newFileModel = newIDs.FirstOrDefault(data => data.FullName.Equals(old.FullName));

            if (newFileModel != null)
            {
                return(newFileModel);
            }

            //Get all classes including all subclasses and check if it might be a subclass
            Dictionary <string, ClassModel> allClassData = generateOptions(newIDs);

            if (allClassData.ContainsKey(old.Name))
            {
                return(allClassData[old.Name]);
            }

            // Check if there is an exact match with only the classname
            ClassModel[] classModels = allClassData.Select(pair => pair.Value)
                                       .Where(model => model.NameLower == old.NameLower).ToArray();
            if (classModels.Length == 1)
            {
                return(classModels[0]);
            }

            // Generate the options for the options window
            string[] options = allClassData.Select(pair => pair.Key)
                               .OrderBy(name => Levenshtein.Compute(name, old.FullName)).ToArray();

            // Open the options window
            string result = MigrationWindow.OpenOptionsWindow(
                "Could not find class, please select which class to use",
                old.FullName,
                options
                );

            // Return the selected class
            if (string.IsNullOrEmpty(result))
            {
                Debug.LogError("[Data loss] Could not find class for : " + old.FullName +
                               " and no new class was chosen. This script will not be migrated!");

                return(null);
            }

            return(allClassData[result]);
        }