private void LoadFiles(string extension, Func <string, IEditableFile> createNew)
        {
            ConcurrentQueue <IEditableFile> filesList = null;

            LoadingProcessWindow.ShowWindow(
                beforeStarting: () => IsBusy = true,
                threadActions: (cts, invoker) =>
            {
                invoker.IsIndeterminate = true;

                string[] files = Directory.GetFiles(_globalVariables.CurrentProjectFolder.Value, "*" + extension, SearchOption.AllDirectories);

                invoker.IsIndeterminate = false;

                invoker.ProcessMax = files.Length;

                filesList = new ConcurrentQueue <IEditableFile>();

                Parallel.ForEach(files, file =>
                {
                    if (!CommonUtils.CheckFilePath(file))
                    {
                        return;
                    }

                    cts.ThrowIfCancellationRequested();

                    try
                    {
                        filesList.Enqueue(createNew(file));
                    }
                    catch (Exception)
                    {
                        // ignored
                    }

                    invoker.ProcessValue++;
                });
            },
                finishActions: () =>
            {
                List <IEditableFile> res =
                    (filesList ?? Enumerable.Empty <IEditableFile>())
                    .Where(file => file.Details != null && file.Details.Count > 0)
                    .ToList();

                IsBusy = false;

                WindowManager.ActivateWindow <EditorWindow>();

                ManualEventManager.GetEvent <AddEditableFilesEvent>()
                .Publish(new AddEditableFilesEvent(res));
            },
                ownerWindow: _window
                );
        }
Exemple #2
0
        private void FindFilesCommand_Execute()
        {
            if (_globalVariables.CurrentProjectFolder.Value.IsNullOrEmpty())
            {
                MessBox.ShowDial(StringResources.SearchWindow_FolderIsNotSelected);
                return;
            }

            string GetFormattedName(string fileName)
            {
                return(StartFormattedString + fileName.Substring(_globalVariables.CurrentProjectFolder.Value.Length + 1));
            }

            AddToSearchAdds(TextToSearch.Value);

            var filesToAdd = new List <FoundItem>();

            LoadingProcessWindow.ShowWindow(
                beforeStarting: () => IsBusy = true,
                threadActions: (cts, invoker) =>
            {
                var projectFolder = _globalVariables.CurrentProjectFolder.Value;
                var buildFolder   = Path.DirectorySeparatorChar + "build";
                var buildFolderM  = Path.DirectorySeparatorChar + "build" + Path.DirectorySeparatorChar;

                List <string> xmlFiles =
                    Directory.EnumerateFiles(projectFolder, "*.xml", SearchOption.AllDirectories)
                    .Where(file =>
                {
                    // ReSharper disable once PossibleNullReferenceException
                    var dir = Path.GetDirectoryName(file).Substring(projectFolder.Length);
                    return(dir != buildFolder && !dir.StartsWith(buildFolderM, StringComparison.Ordinal));
                }
                           )
                    .ToList();

                List <string> smaliFiles = Directory.EnumerateFiles(projectFolder, "*.smali", SearchOption.AllDirectories).ToList();

                invoker.ProcessValue = 0;
                invoker.ProcessMax   = xmlFiles.Count + smaliFiles.Count;

                bool onlyFullWords = OnlyFullWords.Value;
                bool matchCase     = MatchCase.Value;

                Func <string, string, bool> checkRules;

                if (!matchCase && !onlyFullWords)
                {
                    checkRules = (f, s) => f.IndexOf(s, StringComparison.OrdinalIgnoreCase) != -1;
                }
                else if (!matchCase /*&& onlyFullWords*/)
                {
                    checkRules = (f, s) => f.Equals(s, StringComparison.OrdinalIgnoreCase);
                }
                else if (/*matchCase &&*/ !onlyFullWords)
                {
                    checkRules = (f, s) => f.IndexOf(s, StringComparison.Ordinal) != -1;
                }
                else     /*if (matchCase && onlyFullWords)*/
                {
                    checkRules = (f, s) => f.Equals(s, StringComparison.Ordinal);
                }

                IEnumerable <IEditableFile> union =
                    xmlFiles.SelectSafe <string, IEditableFile>(XmlFile.Create)
                    .Concat(smaliFiles.SelectSafe(it => new SmaliFile(it)));

                foreach (IEditableFile file in union)
                {
                    cts.ThrowIfCancellationRequested();

                    IOneString found = file.Details?.FirstOrDefault(str => checkRules(str.OldText, TextToSearch.Value));

                    if (found != null)
                    {
                        filesToAdd.Add(new FoundItem(GetFormattedName(file.FileName), found.OldText));
                    }

                    invoker.ProcessValue++;
                }
            },
                finishActions: () =>
            {
                IsBusy = false;

                if (filesToAdd.Count == 0)
                {
                    Files.Clear();
                    MessBox.ShowDial(StringResources.TextNotFound);
                }
                else
                {
                    Files.ReplaceRange(filesToAdd);
                }
            },
                ownerWindow: _window
                );
        }
        private void LoadFolder(string folderPath, bool haveLogger = false)
        {
            if (string.IsNullOrEmpty(folderPath))
            {
                return;
            }

            if (!haveLogger)
            {
                string logPath = folderPath + "_log.txt";

                if (!TryCreateNewLog(logPath))
                {
                    return;
                }
            }

            _globalVariables.CurrentProjectFile.Value = folderPath + ".apk";

            if (Apk != null)
            {
                Apk.Logging -= VisLog;
            }

            Apk = new Apktools(
                _globalVariables.CurrentProjectFile.Value,
                GlobalVariables.PathToResources,
                GlobalVariables.CurrentApktoolPath
                );

            Apk.Logging += VisLog;

            FilesFilesTreeViewModel.Children.Clear();
            FilesFilesTreeViewModel.Children.Add(
                new FilesTreeViewNodeModel
            {
                Name          = StringResources.AllXml,
                Options       = new Options(fullPath: string.Empty, isFolder: false, isImageLoaded: true),
                DoubleClicked = LoadXmlFiles,
                Image         = GlobalResources.IconUnknownFile
            }
                );
            FilesFilesTreeViewModel.Children.Add(
                new FilesTreeViewNodeModel
            {
                Name          = StringResources.AllSmali,
                Options       = new Options(fullPath: string.Empty, isFolder: false, isImageLoaded: true),
                DoubleClicked = LoadSmaliFiles,
                Image         = GlobalResources.IconUnknownFile
            }
                );

            LoadingProcessWindow.ShowWindow(
                beforeStarting: () => IsBusy = true,
                threadActions: (cts, invoker) =>
            {
                invoker.IsIndeterminate = true;

                invoker.ProcessMax = Directory.EnumerateFiles(folderPath, "*", SearchOption.AllDirectories).Count();

                invoker.IsIndeterminate = false;

                CommonUtils.LoadFilesToTreeView(Application.Current.Dispatcher, folderPath, FilesFilesTreeViewModel, GlobalVariables.AppSettings.EmptyFolders, cts, () => invoker.ProcessValue++);
            },
                finishActions: () =>
            {
                IsBusy = false;

                FilesFilesTreeViewModel.Children.ForEach(ImageUtils.LoadIconForItem);

                RaiseCommandsCanExecute();
            },
                ownerWindow: _window
                );
        }