private void ProcessReplace(IEnumerable <string> replacePathes, ProjectScanItem projectScanItem)
 {
     foreach (var replacePath in replacePathes)
     {
         var lines    = File.ReadAllLines(replacePath);
         var newLines = new List <string>();
         foreach (var line in lines)
         {
             var isFound = false;
             foreach (var file in projectScanItem.Files.Where(x => x.NewId.HasValue))
             {
                 var r = Regex.Match(line,
                                     $"  m_Script: {{fileID: {file.OldId}, guid: {projectScanItem.ProjectGuid.ToString("n").ToLower()}, type: 3}}",
                                     RegexOptions.IgnoreCase);
                 if (r.Success)
                 {
                     newLines.Add(
                         r.Result(
                             $"  m_Script: {{fileID: {file.NewId}, guid: {projectScanItem.ProjectGuid.ToString("n").ToLower()}, type: 3}}"));
                     isFound = true;
                     break;
                 }
             }
             if (!isFound)
             {
                 newLines.Add(line);
             }
         }
         File.WriteAllLines(replacePath, newLines.ToArray());
     }
 }
        private void ProcessProjectScanItem(ProjectScanItem projectScanItem)
        {
            var renamed = projectScanItem.Files.Where(x => x.NewId.HasValue);

            foreach (var r in renamed)
            {
                r.OldId = r.NewId.Value;
                r.NewId = null;
            }

            CommonService.SaveSyncProjectItemsToCache();
        }
        private void ProcessFiles(ProjectScanItem projectScanItem)
        {
            var path      = Cache.Instance.Settings.UnityProjectPath;
            var scenes    = GetFilePathes(path, "*.unity");
            var prefabs   = GetFilePathes(path, "*.prefab");
            var replaceIn = new List <string>();

            replaceIn.AddRange(scenes);
            replaceIn.AddRange(prefabs);

            ProcessReplace(replaceIn, projectScanItem);
            ProcessProjectScanItem(projectScanItem);
        }
        private static IEnumerable <string> NavigateProjectItems(ProjectItems projectItems, ProjectScanItem projectScan,
                                                                 IVsHierarchy projectHierarchy)
        {
            var fileGuids = new List <string>();

            if (projectItems == null)
            {
                return(fileGuids);
            }

            foreach (ProjectItem item in projectItems)
            {
                fileGuids.AddRange(NavigateProjectItems(item.ProjectItems, projectScan, projectHierarchy));

                if (item.Kind != "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}") // VSConstants.GUID_ItemType_PhysicalFile
                {
                    continue;
                }

                var fileGuid = ExamineProjectItem(item, projectScan, projectHierarchy);
                if (!string.IsNullOrEmpty(fileGuid))
                {
                    fileGuids.Add(fileGuid);
                }
            }

            return(fileGuids);
        }
        private static string ExamineProjectItem(ProjectItem item, ProjectScanItem projectScan,
                                                 IVsHierarchy projectHierarchy)
        {
            var fileName = item.FileNames[1];

            if (Path.GetExtension(fileName) != ".cs")
            {
                return(null);
            }

            var code = item.FileCodeModel;

            if (code == null)
            {
                return(null);
            }

            var className     = string.Empty;
            var namespaceName = string.Empty;

            foreach (CodeElement codeElement in code.CodeElements)
            {
                if (string.IsNullOrEmpty(className))
                {
                    className = ExamineCodeElement(codeElement, vsCMElement.vsCMElementClass);
                }
                if (string.IsNullOrEmpty(namespaceName))
                {
                    namespaceName = ExamineCodeElement(codeElement, vsCMElement.vsCMElementNamespace);
                }

                if (!string.IsNullOrEmpty(className) && !string.IsNullOrEmpty(namespaceName))
                {
                    break;
                }
            }

            if (string.IsNullOrEmpty(className))
            {
                return(null);
            }
            uint itemId;

            if (projectHierarchy.ParseCanonicalName(item.FileNames[0], out itemId) == VSConstants.S_OK)
            {
                var buildPropertyStorage =
                    projectHierarchy as IVsBuildPropertyStorage;

                if (buildPropertyStorage == null)
                {
                    return(null);
                }
                string fileGuid;
                buildPropertyStorage.GetItemAttribute(itemId, PersistentFileGuidName, out fileGuid);
                if (string.IsNullOrEmpty(fileGuid))
                {
                    fileGuid = Guid.NewGuid().ToString();
                    buildPropertyStorage.SetItemAttribute(itemId, PersistentFileGuidName, fileGuid);
                    item.ContainingProject.Save();
                }

                if (string.IsNullOrEmpty(fileGuid))
                {
                    return(null);
                }

                var file = projectScan.Files.FirstOrDefault(x => x.ProjectFileId == fileGuid);

                var fileId = FileIdGenerator.Compute(namespaceName, className);

                if (file == null)
                {
                    projectScan.Files.Add(new FileScanItem {
                        ProjectFileId = fileGuid, OldId = fileId
                    });
                }
                else
                {
                    var newId = fileId;
                    if (file.OldId != newId)
                    {
                        file.NewId = newId;
                    }
                }

                return(fileGuid);
            }

            return(null);
        }