Beispiel #1
0
        public void GoogleDriveTests()
        {
            var ini = @"[General]
                        directURL=https://drive.google.com/file/d/1grLRTrpHxlg7VPxATTFNfq2OkU_Plvh_/view?usp=sharing";

            var state = (AbstractDownloadState)DownloadDispatcher.ResolveArchive(ini.LoadIniString());

            Assert.IsNotNull(state);

            var converted = state.ViaJSON();

            Assert.IsTrue(converted.Verify());
            var filename = Guid.NewGuid().ToString();

            Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist {
                GoogleIDs = new List <string> {
                    "1grLRTrpHxlg7VPxATTFNfq2OkU_Plvh_"
                }
            }));
            Assert.IsFalse(converted.IsWhitelisted(new ServerWhitelist {
                GoogleIDs = new List <string>()
            }));

            converted.Download(new Archive {
                Name = "MEGA Test.txt"
            }, filename);

            Assert.AreEqual("Lb1iTsz3iyZeHGs3e94TVmOhf22sqtHLhqkCdXbjiyc=", Utils.FileSHA256(filename));

            Assert.AreEqual(File.ReadAllText(filename), "Cheese for Everyone!");
        }
        private void ButtonToPDF_Click(object sender, RoutedEventArgs e)
        {
            string filename_pdf = TempFile.GenerateTempFilename("pdf");

            using (PdfDocument doc = new PdfDocument())
            {
                PdfPage page   = doc.Pages.Add();
                SizeF   bounds = page.GetClientSize();

                string filename_rtf = SaveToRTF();
                string text         = File.ReadAllText(filename_rtf);

                PdfMetafile             metafile = (PdfMetafile)PdfImage.FromRtf(text, bounds.Width, PdfImageType.Metafile);
                PdfMetafileLayoutFormat format   = new PdfMetafileLayoutFormat();

                // Allow the text to flow multiple pages without any breaks.
                format.SplitTextLines = true;
                format.SplitImages    = true;

                // Draw the image.
                metafile.Draw(page, 0, 0, format);

                doc.Save(filename_pdf);
            }

            Process.Start(filename_pdf);
        }
Beispiel #3
0
        public void TestReplaceWithNewLineWorks(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase8"), Path.Combine(destFolder, "TestCase8"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase8"), "test.txt"), SearchType.Regex, "here", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Single(results[0].SearchResults);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase8\test.txt");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.Regex, "here", "\\\\n", GrepSearchOption.None, -1);
            Assert.Equal(2, File.ReadAllText(testFile, Encoding.ASCII).Trim().Split('\n').Length);
        }
Beispiel #4
0
        public void DropboxTests()
        {
            var ini = @"[General]
                        directURL=https://www.dropbox.com/s/5hov3m2pboppoc2/WABBAJACK_TEST_FILE.txt?dl=0";

            var state = (AbstractDownloadState)DownloadDispatcher.ResolveArchive(ini.LoadIniString());

            Assert.IsNotNull(state);

            var converted = state.ViaJSON();

            Assert.IsTrue(converted.Verify());
            var filename = Guid.NewGuid().ToString();

            Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist {
                AllowedPrefixes = new List <string> {
                    "https://www.dropbox.com/s/5hov3m2pboppoc2/WABBAJACK_TEST_FILE.txt?"
                }
            }));
            Assert.IsFalse(converted.IsWhitelisted(new ServerWhitelist {
                AllowedPrefixes = new List <string> {
                    "blerg"
                }
            }));

            converted.Download(new Archive {
                Name = "MEGA Test.txt"
            }, filename);

            Assert.AreEqual("Lb1iTsz3iyZeHGs3e94TVmOhf22sqtHLhqkCdXbjiyc=", Utils.FileSHA256(filename));

            Assert.AreEqual(File.ReadAllText(filename), "Cheese for Everyone!");
        }
Beispiel #5
0
        public void TestReplaceWithPattern(SearchType type, bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase9", destFolder + "\\TestCase9", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase9", "test.txt"), type, "here", GrepSearchOption.None, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 6);

            string testFile = Path.Combine(destFolder, @"TestCase9\test.txt");
            Dictionary <string, string> files = new Dictionary <string, string>
            {
                { testFile, Guid.NewGuid().ToString() + ".txt" }
            };

            core.Replace(files, type, "here", "$(guid)", GrepSearchOption.None, -1);
            string fileContent = File.ReadAllText(destFolder + "\\TestCase9\\test.txt", Encoding.ASCII).Trim();

            Assert.Equal(6, guidPattern.Matches(fileContent).Count);
            HashSet <string> uniqueGuids = new HashSet <string>();

            foreach (Match match in guidPattern.Matches(fileContent))
            {
                if (!uniqueGuids.Contains(match.Value))
                {
                    uniqueGuids.Add(match.Value);
                }
                else
                {
                    Assert.True(false, "All guides should be unique.");
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Shows a simple modal list of release notes.
        /// </summary>
        public void ViewChanges(object dummy = null)
        {
            try
            {
                string release_notes;

                if (_latestClientVersionInformation != null)
                {
                    release_notes = _latestClientVersionInformation.ReleaseNotes;
                    if (string.IsNullOrEmpty(release_notes))
                    {
                        Logging.Warn("No release notes from server, nothing to do");
                        return;
                    }
                }
                else
                {
                    string ChangelogFilename = Path.GetFullPath(Path.Combine(ConfigurationManager.Instance.StartupDirectoryForQiqqa, @"CHANGELOG.md"));

                    release_notes = File.ReadAllText(ChangelogFilename, Encoding.UTF8);
                }
                release_notes = release_notes.Trim();

                //  switch to the gui thread
                WPFDoEvents.InvokeInUIThread(() => _showReleaseNotesDelegate(release_notes));
            }
            catch (Exception e)
            {
                Logging.Error(e, "Problem viewing changes for new client version");
            }
        }
Beispiel #7
0
        public void TestReplaceAndUndoWorks(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase3", destinationFolder + "\\TestCase3", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "test-file-code.cs"), SearchType.PlainText, "body", GrepSearchOption.None, -1);

            Assert.Equal(1, results.Count);
            Assert.Equal(2, results[0].SearchResults.Where(r => r.IsContext).Count());

            string testFile = Path.Combine(destFolder, @"TestCase3\test-file-code.cs");
            Dictionary <string, string> files = new Dictionary <string, string>
            {
                { testFile, Guid.NewGuid().ToString() + ".cs" }
            };

            core.Replace(files, SearchType.PlainText, "body", "text", GrepSearchOption.None, -1);
            string content = File.ReadAllText(testFile, Encoding.ASCII);

            Assert.False(content.Contains("body"));
            Assert.True(content.Contains("text"));

            core.Undo(files);
            content = File.ReadAllText(testFile, Encoding.ASCII);
            Assert.False(content.Contains("text"));
            Assert.True(content.Contains("body"));
        }
Beispiel #8
0
 public static T FromJSON <T>(this string filename)
 {
     return(JsonConvert.DeserializeObject <T>(File.ReadAllText(filename), new JsonSerializerSettings()
     {
         TypeNameHandling = TypeNameHandling.Auto
     }));
 }
Beispiel #9
0
        public void TestSearchAndFilteredReplaceXPathWithMissingXmlDeclaration(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase4"), Path.Combine(destFolder, "TestCase4"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase4"), "books_no_decl.xml"),
                                                          SearchType.XPath, "(//@currency)", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Equal(5, results[0].Matches.Count);

            // mark 2nd and 4th matches for replace
            results[0].Matches[1].ReplaceMatch = true;
            results[0].Matches[3].ReplaceMatch = true;

            string            testFile = Path.Combine(destinationFolder, "TestCase4", "books_no_decl.xml");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.XPath, "(//@currency)", "EUR", GrepSearchOption.None, -1);

            var fileContent = File.ReadAllText(testFile, Encoding.UTF8);

            Assert.Equal(2, Regex.Matches(fileContent, "EUR").Count);
        }
Beispiel #10
0
        public void MediaFireDownload()
        {
            var ini = @"[General]
                        directURL=http://www.mediafire.com/file/agiqzm1xwebczpx/WABBAJACK_TEST_FILE.txt";

            var state = (AbstractDownloadState)DownloadDispatcher.ResolveArchive(ini.LoadIniString());

            Assert.IsNotNull(state);

            var url_state = DownloadDispatcher.ResolveArchive(
                "http://www.mediafire.com/file/agiqzm1xwebczpx/WABBAJACK_TEST_FILE.txt");

            Assert.AreEqual("http://www.mediafire.com/file/agiqzm1xwebczpx/WABBAJACK_TEST_FILE.txt",
                            ((MediaFireDownloader.State)url_state).Url);

            var converted = state.ViaJSON();

            Assert.IsTrue(converted.Verify());
            var filename = Guid.NewGuid().ToString();

            Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist {
                AllowedPrefixes = new List <string> {
                    "http://www.mediafire.com/file/agiqzm1xwebczpx/"
                }
            }));
            Assert.IsFalse(converted.IsWhitelisted(new ServerWhitelist {
                AllowedPrefixes = new List <string>()
            }));

            converted.Download(new Archive {
                Name = "Media Fire Test.txt"
            }, filename);

            Assert.AreEqual(File.ReadAllText(filename), "Cheese for Everyone!");
        }
Beispiel #11
0
        public void MegaDownload()
        {
            var ini = @"[General]
                        directURL=https://mega.nz/#!CsMSFaaJ!-uziC4mbJPRy2e4pPk8Gjb3oDT_38Be9fzZ6Ld4NL-k";

            var state = (AbstractDownloadState)DownloadDispatcher.ResolveArchive(ini.LoadIniString());

            Assert.IsNotNull(state);

            var converted = state.ViaJSON();

            Assert.IsTrue(converted.Verify());
            var filename = Guid.NewGuid().ToString();

            Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist {
                AllowedPrefixes = new List <string> {
                    "https://mega.nz/#!CsMSFaaJ!-uziC4mbJPRy2e4pPk8Gjb3oDT_38Be9fzZ6Ld4NL-k"
                }
            }));
            Assert.IsFalse(converted.IsWhitelisted(new ServerWhitelist {
                AllowedPrefixes = new List <string> {
                    "blerg"
                }
            }));

            converted.Download(new Archive {
                Name = "MEGA Test.txt"
            }, filename);

            Assert.AreEqual("Lb1iTsz3iyZeHGs3e94TVmOhf22sqtHLhqkCdXbjiyc=", Utils.FileSHA256(filename));

            Assert.AreEqual(File.ReadAllText(filename), "Cheese for Everyone!");
        }
Beispiel #12
0
        public void HttpDownload()
        {
            var ini = @"[General]
                        directURL=https://raw.githubusercontent.com/wabbajack-tools/opt-out-lists/master/ServerWhitelist.yml";

            var state = (AbstractDownloadState)DownloadDispatcher.ResolveArchive(ini.LoadIniString());

            Assert.IsNotNull(state);

            var converted = state.ViaJSON();

            Assert.IsTrue(converted.Verify());
            var filename = Guid.NewGuid().ToString();

            Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist {
                AllowedPrefixes = new List <string> {
                    "https://raw.githubusercontent.com/"
                }
            }));
            Assert.IsFalse(converted.IsWhitelisted(new ServerWhitelist {
                AllowedPrefixes = new List <string>()
            }));

            converted.Download(new Archive {
                Name = "Github Test Test.txt"
            }, filename);

            Assert.IsTrue(File.ReadAllText(filename).StartsWith("# Server whitelist for Wabbajack"));
        }
Beispiel #13
0
        public void TestRegexCaptureReplace(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase3"), Path.Combine(destinationFolder, "TestCase3"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase3"), "test-file-code.cs"),
                                                          SearchType.Regex, @"-(\d)", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Single(results[0].SearchResults.Where(r => !r.IsContext));

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase3\test-file-code.cs");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.Regex, @"-(\d)", @"$1", GrepSearchOption.None, -1);
            string content = File.ReadAllText(testFile, Encoding.ASCII);

            Assert.DoesNotContain("= -1;", content);
            Assert.Contains("= 1;", content);

            core.Undo(files);
            content = File.ReadAllText(testFile, Encoding.ASCII);
            Assert.DoesNotContain("= 1;", content);
            Assert.Contains("= -1;", content);
        }
Beispiel #14
0
        public void TestSearchAndFilteredReplace(SearchType type, GrepSearchOption option, string searchFor, string replaceWith)
        {
            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase15"), Path.Combine(destinationFolder, "TestCase15"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destinationFolder, "TestCase15"), "books.xml"),
                                                          type, searchFor, option, -1);

            Assert.Single(results);
            Assert.Equal(2, results[0].Matches.Count);

            // mark only the second match for replace
            results[0].Matches[1].ReplaceMatch = true;

            string            testFile = Path.Combine(destinationFolder, "TestCase15", "books.xml");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, type, searchFor, replaceWith, option, -1);

            var fileContent = File.ReadAllText(testFile, Encoding.UTF8);

            Assert.Contains("<year>2003</year>", fileContent);
            Assert.Single(Regex.Matches(fileContent, "2002"));
            Assert.Single(Regex.Matches(fileContent, "2003"));
        }
Beispiel #15
0
 public static T FromJSON <T>(this string filename,
                              TypeNameHandling handling             = TypeNameHandling.All,
                              TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full)
 {
     return(JsonConvert.DeserializeObject <T>(File.ReadAllText(filename),
                                              new JsonSerializerSettings {
         TypeNameHandling = handling, TypeNameAssemblyFormatHandling = format
     }));
 }
        // Populated from http://www.kfunigraz.ac.at/~binder/texhelp/bibtx-7.html (not active anymore in 2019 A.D.)
        // Also at http://nwalsh.com/tex/texhelp/bibtx-7.html
        //
        // Other potential sources:
        // - http://bib-it.sourceforge.net/help/fieldsAndEntryTypes.php
        // - https://www.bibtex.com/e/book-entry/
        // - https://en.wikipedia.org/wiki/BibTeX#Style_files
        //
        private EntryTypes()
        {
            string filename = EntryTypes.EntryTypesDefinitionFilename;

            // when the setup file does not exist, yak!
            if (!File.Exists(filename))
            {
                Logging.Error("BibTeX entry types definition JSON file '{0}' does not exist! Your install may be boogered!", filename);

                // do it old skool style. Only a few so it's easily noticed by the user!
                AddEntryType(
                    new EntryType(
                        "book",
                        new string[] { "author", "editor", "title", "publisher", "year" },
                        new string[] { "volume", "series", "address", "edition", "month", "note" }
                        ));

                AddEntryType(
                    new EntryType(
                        "misc",
                        null,
                        new string[] { "title", "author", "year", "DOI", "PMID" }
                        ));

#if false && TEST
                string json = JsonConvert.SerializeObject(entry_types, Formatting.Indented);
                File.WriteAllText(filename, json);
#endif
            }
            else
            {
                // file exists; load it to get all the goodness.
                try
                {
                    string json = File.ReadAllText(filename, System.Text.Encoding.UTF8);
                    entry_types = JsonConvert.DeserializeObject <Dictionary <string, EntryType> >(json);
                    Logging.Info("Loaded {0} BibTeX entry types from configuration file '{1}':\n    [{2}]", entry_types.Count, filename, String.Join(", ", entry_types.Keys));
                }
                catch (Exception ex)
                {
                    Logging.Error(ex, "Failed to load/parse the BibTeX entry types definition file '{0}'.", filename);
                }
            }

            // see if the 'common denominator' entry has been defined in there.
            // IFF it's not, we add it on the spot and barge on.
            if (!entry_types.ContainsKey("misc"))
            {
                AddEntryType(
                    new EntryType(
                        "misc",
                        null,
                        new string[] { "title", "author", "year", "DOI", "PMID" }
                        ));
            }
        }
 public string ReadFile(string filename)
 {
     if (null == encoding)
     {
         return(File.ReadAllText(filename));
     }
     else
     {
         return(File.ReadAllText(filename, encoding));
     }
 }
Beispiel #18
0
        public override IEnumerable <ICompilationStep> GetStack()
        {
            var s          = Consts.TestMode ? DownloadsFolder : VortexFolder;
            var userConfig = Path.Combine(s, "compilation_stack.yml");

            if (File.Exists(userConfig))
            {
                return(Serialization.Deserialize(File.ReadAllText(userConfig), this));
            }

            var stack = MakeStack();

            var compilationSteps = stack.ToList();

            File.WriteAllText(Path.Combine(s, "_current_compilation_stack.yml"),
                              Serialization.Serialize(compilationSteps));

            return(compilationSteps);
        }
Beispiel #19
0
        private void ButtonOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog open_file_dialog = new OpenFileDialog();

            open_file_dialog.AddExtension     = true;
            open_file_dialog.CheckPathExists  = true;
            open_file_dialog.CheckFileExists  = true;
            open_file_dialog.DereferenceLinks = true;
            open_file_dialog.ValidateNames    = true;
            open_file_dialog.DefaultExt       = "csl";
            open_file_dialog.Filter           = "CSL files (*.csl)|*.csl|All files (*.*)|*.*";
            open_file_dialog.FileName         = last_filename;

            if (true == open_file_dialog.ShowDialog())
            {
                last_filename     = open_file_dialog.FileName;
                ObjCSLEditor.Text = File.ReadAllText(open_file_dialog.FileName);
            }
        }
Beispiel #20
0
        public void TestGuidxReplaceWithPatternRegex()
        {
            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase9"), Path.Combine(destinationFolder, "TestCase9"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destinationFolder, "TestCase9"), "guidx.txt"),
                                                          SearchType.Regex, "h\\wre", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Equal(6, results[0].SearchResults.Count);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destinationFolder, @"TestCase9\guidx.txt");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            // all instances of the same string matched will get the same guid
            core.Replace(files, SearchType.Regex, "h\\wre", "$(guidx)", GrepSearchOption.None, -1);
            string fileContent = File.ReadAllText(testFile, Encoding.UTF8).Trim();

            Assert.Equal(6, guidPattern.Matches(fileContent).Count);
            Dictionary <string, int> uniqueGuids = new Dictionary <string, int>();

            foreach (Match match in guidPattern.Matches(fileContent))
            {
                if (!uniqueGuids.ContainsKey(match.Value))
                {
                    uniqueGuids[match.Value] = 1;
                }
                else
                {
                    uniqueGuids[match.Value]++;
                }
            }
            Assert.Equal(2, uniqueGuids.Keys.Count);
        }
Beispiel #21
0
        public void TestReplaceWithPattern(SearchType type, bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase9"), Path.Combine(destFolder, "TestCase9"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase9"), "test.txt"),
                                                          type, "here", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Equal(6, results[0].SearchResults.Count);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase9\test.txt");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, type, "here", "$(guid)", GrepSearchOption.None, -1);
            string fileContent = File.ReadAllText(testFile, Encoding.UTF8).Trim();

            Assert.Equal(6, guidPattern.Matches(fileContent).Count);
            HashSet <string> uniqueGuids = new HashSet <string>();

            foreach (Match match in guidPattern.Matches(fileContent))
            {
                if (!uniqueGuids.Contains(match.Value))
                {
                    uniqueGuids.Add(match.Value);
                }
                else
                {
                    Assert.True(false, "All GUIDs should be unique.");
                }
            }
        }
Beispiel #22
0
        public void TestReplaceWithNewLineWorks(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase8", destFolder + "\\TestCase8", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase8", "test.txt"), SearchType.Regex, "here", GrepSearchOption.None, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 1);

            string testFile = Path.Combine(destFolder, @"TestCase8\test.txt");
            Dictionary <string, string> files = new Dictionary <string, string>
            {
                { testFile, Guid.NewGuid().ToString() + ".txt" }
            };

            core.Replace(files, SearchType.Regex, "here", "\\\\n", GrepSearchOption.None, -1);
            Assert.Equal(File.ReadAllText(testFile, Encoding.ASCII).Trim().Split('\n').Length, 2);
        }
        internal static bool IsDependentStyle(string style_xml_filename, out string parent_filename, out string parent_url)
        {
            string style_xml = File.ReadAllText(style_xml_filename);

            try
            {
                // Lets parse the XML and look for the info/link node
                XmlDocument xml_doc = new XmlDocument();
                xml_doc.LoadXml(style_xml);

                XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml_doc.NameTable);
                nsmgr.AddNamespace("csl", "http://purl.org/net/xbiblio/csl");

                XmlNode xml_node = xml_doc.SelectSingleNode("/csl:style/csl:info/csl:link", nsmgr);
                if (null != xml_node)
                {
                    string relative_type = xml_node.Attributes["rel"].InnerText;
                    if ("independent-parent" == relative_type)
                    {
                        parent_url = xml_node.Attributes["href"].InnerText;

                        // Get the rightmost "folder name" from the url - that is the name of the parent style
                        string[] url_parts = parent_url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                        parent_filename = url_parts[url_parts.Length - 1] + ".csl";

                        // This is a dependent style
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Warn(ex, "There is a problem with the XML while checking if it is a dependent style");
            }

            // Thisis not a dependent style, so keep calm and carry on
            parent_filename = null;
            parent_url      = null;
            return(false);
        }
Beispiel #24
0
        public void HttpDownload()
        {
            var ini = @"[General]
                        directURL=http://build.wabbajack.org/WABBAJACK_TEST_FILE.txt";

            var state = (AbstractDownloadState)DownloadDispatcher.ResolveArchive(ini.LoadIniString());

            Assert.IsNotNull(state);

            var url_state = DownloadDispatcher.ResolveArchive("http://build.wabbajack.org/WABBAJACK_TEST_FILE.txt");

            Assert.AreEqual("http://build.wabbajack.org/WABBAJACK_TEST_FILE.txt",
                            ((HTTPDownloader.State)url_state).Url);

            var converted = state.ViaJSON();

            Assert.IsTrue(converted.Verify());
            var filename = Guid.NewGuid().ToString();

            Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist {
                AllowedPrefixes = new List <string> {
                    "http://build.wabbajack.org/"
                }
            }));
            Assert.IsFalse(converted.IsWhitelisted(new ServerWhitelist {
                AllowedPrefixes = new List <string>()
            }));

            converted.Download(new Archive {
                Name = "MEGA Test.txt"
            }, filename);

            Assert.AreEqual("eSIyd+KOG3s=", Utils.FileHash(filename));

            Assert.AreEqual(File.ReadAllText(filename), "Cheese for Everyone!");
        }
Beispiel #25
0
 private static string TextLoad_NotRedundant(string filename)
 {
     return(File.ReadAllText(filename));
 }
Beispiel #26
0
 public override string ReadAllText(string path, Encoding encoding)
 {
     return(AfsFile.ReadAllText(path, encoding));
 }
Beispiel #27
0
        private int CountPDFPages()
        {
            string cached_count_filename = MakeFilename_PageCount();
            int    num;

            // Try the cached version
            try
            {
                if (File.Exists(cached_count_filename))
                {
                    num = Convert.ToInt32(File.ReadAllText(cached_count_filename));
                    if (0 != num)
                    {
                        return(num);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Warn(ex, "There was a problem loading the cached page count.");

                FileTools.Delete(cached_count_filename);
            }

            // If we get here, either the pagecountfile doesn't exist, or there was an exception
            Logging.Debug特("Using calculated PDF page count for file {0}", pdf_filename);
            num = PDFTools.CountPDFPages(pdf_filename);
            Logging.Info("The result is {1} for using calculated PDF page count for file {0}", pdf_filename, num);
            do
            {
                if (0 != num)
                {
                    num_pages_calc_try_count = 0;  // reset recalc fail counter

                    try
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(cached_count_filename));
                        string outputText = Convert.ToString(num, 10);
                        File.WriteAllText(cached_count_filename, outputText);
                        // Open the file and read back what was written: we don't do a file lock for this one, but reckon all threads who
                        // do this, will produce the same number = file content.
                        //
                        // Besides, we don't know if WriteAllText() is atomic; it probably is not.
                        // Hence we read back to verify written content and if there's anything off about it, we
                        // blow it out of the water and retry later, possibly.
                        string readText = File.ReadAllText(cached_count_filename);
                        if (readText != outputText)
                        {
                            throw new IOException("CountPDFPages: cache content as read back from cache file does not match written data. Cache file is untrustworthy.");
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.Error(ex, "There was a problem using calculated PDF page count for file {0} / {1}", pdf_filename, cached_count_filename);

                        FileTools.Delete(cached_count_filename);

                        // we know the *calculated* pagecount is A-okay, so we can pass that on.
                        // It's just the file I/O for caching the value that may have gone awry,
                        // so we should retry that the next time we (re)calculate the pagecount number
                        // as it is a heavy operation!
                        //
                        //num = 0;
                    }
                }
                else
                {
                    // Special case: when the CountPDFPages() API does not deliver a sane, that is non-zero, page count,
                    // then we've got a PDF file issue on our hands, very probably a damaged PDF.
                    //
                    // Damaged PDF files SHOULD NOT burden us forever, hence we introduce the heauristic of Three Strikes:
                    // when we've (re)calculated the PDF page count three times in a row with errors (num == 0), then we
                    // don't retry ever again, at least not during this application run.
                    //
                    // we DO NOT count I/O errors around the cache file, we only count (re)calc errors:
                    num_pages_calc_try_count++;
                    if (num_pages_calc_try_count >= 3)
                    {
                        num = -1; // signal final failure

                        Logging.Warn("Marking this PDF Document as uncompromisingly stubborn in its failure to be inspected for file {0}", pdf_filename);

                        continue; // and cache this value
                    }
                }
                break;
            } while (true);

            return(num);
        }
Beispiel #28
0
        public override ParseFileResult GetResult()
        {
            // Parse the endnote clump
            string endnote_text = File.ReadAllText(ExportFileName);
            List <EndNoteToBibTex.EndNoteRecord> endnote_records = EndNoteToBibTex.Parse(endnote_text);

            // Then process each one
            foreach (EndNoteToBibTex.EndNoteRecord endnote_record in endnote_records)
            {
                bool associated_filename_is_rooted = false; // By default they're relative to the import file directory

                // Check if there is an associated filename with this guy, use it.  Note that we stop at the FIRST associated PDF file...all the rest are ignored!
                string associated_filename = null;
                {
                    if (endnote_record.attributes.ContainsKey("%>"))
                    {
                        string   filenames_combined = endnote_record.attributes["%>"][0];
                        string[] filenames          = filenames_combined.Split('\n');
                        foreach (string filename in filenames)
                        {
                            string test_filename = filename;
                            test_filename = test_filename.ToLower();

                            // Looks like this:
                            // internal-pdf://2020827050-1893725446/2020827050.pdf
                            if (test_filename.StartsWith("internal-pdf://") && test_filename.EndsWith(".pdf"))
                            {
                                associated_filename = test_filename.Substring("internal-pdf://".Length);
                                break;
                            }

                            // Or sometimes like this:
                            // file://F:\Manzotti\Stuff on Library\EL\MEL.Data\PDF\Bain - 2007 - The Southern Journal of Philosophy - Color Externalism and Switch Cases.pdf
                            if (test_filename.StartsWith("file://"))
                            {
                                associated_filename           = test_filename.Substring("file://".Length);
                                associated_filename_is_rooted = true;
                            }
                        }
                    }
                }

                string notes = null;
                {
                    if (endnote_record.attributes.ContainsKey("%Z"))
                    {
                        notes = endnote_record.attributes["%Z"][0];
                    }
                }

                // Create our import entry
                BibTeXEntry bibtex_entry = new BibTeXEntry();
                bibtex_entry.Item      = endnote_record.ToBibTeX();
                bibtex_entry.EntryType = bibtex_entry.Item.Type;
                bibtex_entry.BibTeX    = bibtex_entry.Item.ToBibTex();
                bibtex_entry.Notes     = notes;
                if (null != _pdfRootDir /* a valid root dir has been chosen */ && null != associated_filename)
                {
                    if (associated_filename_is_rooted)
                    {
                        bibtex_entry.Filename = associated_filename;
                    }
                    else
                    {
                        bibtex_entry.Filename = Path.Combine(_pdfRootDir, associated_filename);
                    }
                    bibtex_entry.FileType = "pdf";
                }

                Entries.Add(bibtex_entry);
            }


            var res = CreateFinalResult();

            if (endnote_text.Length > 0 && Entries.Count == 0)
            {
                // Perhaps they tried to import the endnote library file, as opposed to export...
                InputFileAppearsToBeWrongFormat = true;
            }

            return(res);
        }
Beispiel #29
0
 public static string GetAPIKey()
 {
     return(File.ReadAllText(Path.Combine(Consts.LocalAppDataPath, "author-api-key.txt")).Trim());
 }
Beispiel #30
0
        protected override async Task <bool> _Begin(CancellationToken cancel)
        {
            if (cancel.IsCancellationRequested)
            {
                return(false);
            }

            Info($"Starting Vortex compilation for {GameName} at {GamePath} with staging folder at {StagingFolder} and downloads folder at {DownloadsFolder}.");

            ConfigureProcessor(12, ConstructDynamicNumThreads(await RecommendQueueSize()));
            UpdateTracker.Reset();

            if (cancel.IsCancellationRequested)
            {
                return(false);
            }
            UpdateTracker.NextStep("Parsing deployment file");
            ParseDeploymentFile();

            if (cancel.IsCancellationRequested)
            {
                return(false);
            }
            UpdateTracker.NextStep("Creating metas for archives");
            await CreateMetaFiles();

            Utils.Log($"VFS File Location: {VFSCacheName}");

            if (cancel.IsCancellationRequested)
            {
                return(false);
            }
            await VFS.IntegrateFromFile(VFSCacheName);

            var roots = new List <string> {
                StagingFolder, GamePath, DownloadsFolder
            };

            AddExternalFolder(ref roots);

            if (cancel.IsCancellationRequested)
            {
                return(false);
            }
            UpdateTracker.NextStep("Indexing folders");
            await VFS.AddRoots(roots);

            await VFS.WriteToFile(VFSCacheName);

            if (cancel.IsCancellationRequested)
            {
                return(false);
            }
            UpdateTracker.NextStep("Cleaning output folder");
            if (Directory.Exists(ModListOutputFolder))
            {
                Utils.DeleteDirectory(ModListOutputFolder);
            }

            Directory.CreateDirectory(ModListOutputFolder);

            UpdateTracker.NextStep("Finding Install Files");
            var vortexStagingFiles = Directory.EnumerateFiles(StagingFolder, "*", SearchOption.AllDirectories)
                                     .Where(p => p.FileExists() && p != StagingMarkerName && !p.Contains(Consts.ManualGameFilesDir))
                                     .Select(p => new RawSourceFile(VFS.Index.ByRootPath[p], p.RelativeTo(StagingFolder)));

            var vortexDownloads = Directory.EnumerateFiles(DownloadsFolder, "*", SearchOption.AllDirectories)
                                  .Where(p => p.FileExists() && p != DownloadMarkerName)
                                  .Select(p => new RawSourceFile(VFS.Index.ByRootPath[p], p.RelativeTo(DownloadsFolder)));

            var gameFiles = Directory.EnumerateFiles(GamePath, "*", SearchOption.AllDirectories)
                            .Where(p => p.FileExists())
                            .Select(p => new RawSourceFile(VFS.Index.ByRootPath[p], Path.Combine(Consts.GameFolderFilesDir, p.RelativeTo(GamePath))));

            Info("Indexing Archives");
            IndexedArchives = Directory.EnumerateFiles(DownloadsFolder)
                              .Where(f => File.Exists(f + Consts.MetaFileExtension))
                              .Select(f => new IndexedArchive
            {
                File    = VFS.Index.ByRootPath[f],
                Name    = Path.GetFileName(f),
                IniData = (f + Consts.MetaFileExtension).LoadIniFile(),
                Meta    = File.ReadAllText(f + Consts.MetaFileExtension)
            })
                              .ToList();

            Info("Indexing Files");
            IndexedFiles = IndexedArchives.SelectMany(f => f.File.ThisAndAllChildren)
                           .OrderBy(f => f.NestingFactor)
                           .GroupBy(f => f.Hash)
                           .ToDictionary(f => f.Key, f => f.AsEnumerable());

            AllFiles = vortexStagingFiles.Concat(vortexDownloads)
                       .Concat(gameFiles)
                       .DistinctBy(f => f.Path)
                       .ToList();

            Info($"Found {AllFiles.Count} files to build into mod list");

            if (cancel.IsCancellationRequested)
            {
                return(false);
            }
            UpdateTracker.NextStep("Verifying destinations");
            var duplicates = AllFiles.GroupBy(f => f.Path)
                             .Where(fs => fs.Count() > 1)
                             .Select(fs =>
            {
                Utils.Log($"Duplicate files installed to {fs.Key} from : {string.Join(", ", fs.Select(f => f.AbsolutePath))}");
                return(fs);
            }).ToList();

            if (duplicates.Count > 0)
            {
                Error($"Found {duplicates.Count} duplicates, exiting");
            }

            for (var i = 0; i < AllFiles.Count; i++)
            {
                var f = AllFiles[i];
                if (!f.Path.StartsWith(Consts.GameFolderFilesDir) || !IndexedFiles.ContainsKey(f.Hash))
                {
                    continue;
                }

                if (!IndexedFiles.TryGetValue(f.Hash, out var value))
                {
                    continue;
                }

                var element = value.ElementAt(0);

                if (!f.Path.Contains(element.Name))
                {
                    continue;
                }

                IndexedArchive targetArchive = null;
                IndexedArchives.Where(a => a.File.ThisAndAllChildren.Contains(element)).Do(a => targetArchive = a);

                if (targetArchive == null)
                {
                    continue;
                }

                if (targetArchive.IniData?.General?.tag == null || targetArchive.IniData?.General?.tag != Consts.WABBAJACK_VORTEX_MANUAL)
                {
                    continue;
                }

                #if DEBUG
                Utils.Log($"Double hash for: {f.AbsolutePath}");
                #endif

                var replace     = f;
                var name        = replace.File.Name;
                var archiveName = targetArchive.Name;
                var elementPath = element.FullPath.Substring(element.FullPath.LastIndexOf('|') + 1);
                var gameToFile  = name.Substring(GamePath.Length + 1).Replace(elementPath, "");
                if (gameToFile.EndsWith("\\"))
                {
                    gameToFile = gameToFile.Substring(0, gameToFile.Length - 1);
                }
                //replace.Path = replace.Path.Replace(Consts.GameFolderFilesDir, Consts.ManualGameFilesDir);
                replace.Path = Path.Combine(Consts.ManualGameFilesDir, archiveName, gameToFile, elementPath);
                //replace.Path = Path.Combine(Consts.ManualGameFilesDir, element.FullPath.Substring(DownloadsFolder.Length + 1).Replace('|', '\\'));
                AllFiles.RemoveAt(i);
                AllFiles.Insert(i, replace);
                //AllFiles.Replace(f, replace);
            }

            var stack = MakeStack();

            Info("Running Compilation Stack");
            var results = await AllFiles.PMap(Queue, f => RunStack(stack.Where(s => s != null), f));

            var noMatch = results.OfType <NoMatch>().ToList();
            PrintNoMatches(noMatch);
            if (CheckForNoMatchExit(noMatch))
            {
                return(false);
            }

            InstallDirectives = results.Where(i => !(i is IgnoredDirectly)).ToList();

            Info("Getting Nexus api_key, please click authorize if a browser window appears");

            if (cancel.IsCancellationRequested)
            {
                return(false);
            }
            UpdateTracker.NextStep("Gathering Archives");
            await GatherArchives();

            ModList = new ModList
            {
                Name        = ModListName ?? "",
                Author      = ModListAuthor ?? "",
                Description = ModListDescription ?? "",
                Readme      = ModListReadme ?? "",
                Image       = ModListImage ?? "",
                Website     = ModListWebsite ?? "",
                Archives    = SelectedArchives.ToList(),
                ModManager  = ModManager.Vortex,
                Directives  = InstallDirectives,
                GameType    = Game
            };

            UpdateTracker.NextStep("Running Validation");
            await ValidateModlist.RunValidation(Queue, ModList);

            UpdateTracker.NextStep("Generating Report");
            GenerateManifest();

            UpdateTracker.NextStep("Exporting ModList");
            ExportModList();

            ResetMembers();

            UpdateTracker.NextStep("Done Building ModList");

            return(true);
        }