/// <summary> /// Get the ReadMe file path if it exists. /// </summary> /// <param name="p_strFileName">The mod file name.</param> /// <param name="p_intReadmeFile">The index of the readme file to get.</param> public string GetModReadMe(string p_strModName, string p_strFileName) { string strReadMe = String.Empty; if (m_dicReadMeFiles.ContainsKey(p_strModName)) { string strModReadMeFile = p_strModName + ".7z"; strModReadMeFile = Path.Combine(m_strReadMePath, strModReadMeFile); if (File.Exists(strModReadMeFile)) { PurgeTempFolder(); Archive arcFile = new Archive(strModReadMeFile); byte[] bteData = arcFile.GetFileContents(p_strFileName); if ((bteData != null) && (bteData.Length > 0)) { TxFileManager tfmFileManager = new TxFileManager(); string strReadMeFile = Path.Combine(ReadMeTempPath, p_strFileName); tfmFileManager.WriteAllBytes(strReadMeFile, bteData); return(strReadMeFile); } } } return(strReadMe); }
/// <summary> /// Verifies if the readme file exists in the archive and saves it to the ReadMe folder. /// </summary> /// <param name="p_strArchivePath">The path to the mod archive.</param> public bool VerifyReadMeFile(TxFileManager p_tfmFileManager, string p_strArchivePath) { try { Archive arcFile = new Archive(p_strArchivePath); List <string> lstFiles = GetFileList(arcFile, true); string strReadMePath = m_strReadMePath; string strFileName = String.Empty; string strReadMeFile = String.Empty; string strModFile = Path.GetFileName(p_strArchivePath); string strArchiveFile = Path.GetFileNameWithoutExtension(strModFile) + ".7z"; byte[] bteData = null; PurgeTempFolder(); for (int i = 0; i < lstFiles.Count; i++) { strFileName = lstFiles[i].ToString(); if (Readme.IsValidReadme(strFileName)) { bteData = arcFile.GetFileContents(lstFiles[i]); if (bteData.Length > 0) { strReadMeFile = Path.GetFileName(strFileName); strReadMePath = Path.Combine(ReadMeTempPath, strReadMeFile); p_tfmFileManager.WriteAllBytes(strReadMePath, bteData); break; } } } string[] strFilesToCompress = Directory.GetFiles(ReadMeTempPath, "*.*", SearchOption.AllDirectories); if (strFilesToCompress.Length > 0) { if (CreateReadMeArchive(strArchiveFile, strFilesToCompress)) { for (int i = 0; i < strFilesToCompress.Length; i++) { strFilesToCompress[i] = Path.GetFileName(strFilesToCompress[i]); } m_dicReadMeFiles.Add(Path.GetFileNameWithoutExtension(strArchiveFile), strFilesToCompress); } } } catch { return(false); } return(true); }
public async Task HandlesAsync() { var scheduler = new ThreadedPerTaskScheduler(); async Task RunInNewThread(Func <Task> action) { await Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, scheduler); } var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); IFileManager fm = null; await RunInNewThread(() => { fm = new TxFileManager(); fm.WriteAllBytes("test", new byte[] { 1, 2, 3 }); return(Task.CompletedTask); }); ts.Complete(); ts.Dispose(); }
static void RunStressTest() { Stopwatch sw = new Stopwatch(); sw.Start(); { // Pre-test checks string tempDir; IFileManager fm = new TxFileManager(); using (TransactionScope s1 = new TransactionScope()) { tempDir = (new DirectoryInfo(fm.CreateTempDirectory())).Parent.FullName; Console.WriteLine("Temp path: " + tempDir); } string[] directories = Directory.GetDirectories(tempDir); string[] files = Directory.GetFiles(tempDir); if (directories.Length > 0 || files.Length > 0) { Console.WriteLine(string.Format("ERROR Please ensure temp path {0} has no children before running this test.", tempDir)); return; } } // Start each test in its own thread and repeat for a few interations const int numThreads = 10; const int iterations = 250; const string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; long count = 0; IList <Thread> threads = new List <Thread>(); for (int i = 0; i < numThreads; i++) { Thread t = new Thread(() => { IFileManager fm = new TxFileManager(); for (int j = 0; j < iterations; j++) { using (TransactionScope s1 = new TransactionScope()) { TransactionOptions to = new TransactionOptions(); to.Timeout = TimeSpan.FromMinutes(30); long myCount = Interlocked.Increment(ref count); if (myCount % 250 == 0) { Console.WriteLine(myCount + " (" + myCount * 100 / (numThreads * iterations) + " %)"); } string f1 = fm.CreateTempFileName(); string f2 = fm.CreateTempFileName(); string d1 = fm.CreateTempDirectory(); if (i % 100 == 0) { Console.WriteLine(i); } fm.AppendAllText(f1, text); fm.Copy(f1, f2, false); fm.CreateDirectory(d1); fm.Delete(f2); fm.DeleteDirectory(d1); bool b1 = fm.DirectoryExists(d1); bool b2 = fm.FileExists(f1); string f3 = fm.CreateTempFileName(); fm.Move(f1, f3); string f4 = fm.CreateTempFileName(); fm.Snapshot(f4); fm.WriteAllBytes(f4, new byte[] { 64, 65 }); string f5 = fm.CreateTempFileName(); fm.WriteAllText(f5, text); fm.Delete(f1); fm.Delete(f2); fm.Delete(f3); fm.Delete(f4); fm.Delete(f5); fm.DeleteDirectory(d1); s1.Complete(); } } }); threads.Add(t); } foreach (Thread t in threads) { t.Start(); } Console.WriteLine("All threads started."); foreach (Thread t in threads) { t.Join(); } sw.Stop(); Console.WriteLine("All threads joined. Elapsed: {0}.", sw.ElapsedMilliseconds); }