public List <FileInfo> EnumFiles() { List <FileInfo> files = new List <FileInfo>(); if (IsValid()) { StreamReader indexStram = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("PrivateSetup.Resources.FILE_IDX.TXT")); while (!indexStram.EndOfStream) { var rawLine = indexStram.ReadLine(); var line = rawLine.Split('\t'); files.Add(new FileInfo() { FileName = line[2], Alias = line[0], Hash = line[1] }); } } else { string SourcePath = App.appPath + FilePath; var foundFiles = MiscFunc.EnumAllFiles(SourcePath); foreach (var filePath in foundFiles) { var fileName = filePath.Substring(SourcePath.Length + (SourcePath[SourcePath.Length - 1].Equals('\\') ? 0 : 1)); string hashStr; using (var md5 = MD5.Create()) { using (var inStream = File.OpenRead(filePath)) { byte[] hash = md5.ComputeHash(inStream); hashStr = BitConverter.ToString(hash).Replace("-", ""); } } files.Add(new FileInfo() { FileName = fileName, Hash = hashStr }); } } return(files); }
public bool ExtractFile(FileInfo file, string installationPath) { for (int i = 0; i < 3; i++) { try { string targetPath = installationPath + @"\" + file.FileName; if (File.Exists(targetPath)) { MiscFunc.SafeDelete(targetPath); } else { Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); } if (IsValid()) { using (Stream inStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("PrivateSetup.Resources." + file.Alias)) { using (Stream rawStream = new DeflateStream(inStream, CompressionMode.Decompress)) { MemoryStream memStream = new MemoryStream(); rawStream.CopyTo(memStream); memStream.Seek(0, SeekOrigin.Begin); using (var md5 = MD5.Create()) { using (Stream outStream = File.Create(targetPath)) { memStream.CopyTo(outStream); } using (var testStream = File.OpenRead(targetPath)) { byte[] hash = md5.ComputeHash(testStream); var hashStr = BitConverter.ToString(hash).Replace("-", ""); if (!file.Hash.Equals(hashStr)) { return(false); } } } } } } else { File.Copy(App.appPath + FilePath + @"\" + file.FileName, targetPath); } return(true); } catch { Thread.Sleep(1000 * (i + 1)); } } return(false); }
public bool PrepareSetup(string SourcePath) { if (IsValid()) { App.ShowMessage("Setup is already Prepared."); return(true); } Console.WriteLine("Preparing Setup..."); if (SourcePath == null || SourcePath.Length == 0) { SourcePath = App.appPath + FilePath; } Console.WriteLine("Source Path: {0}", SourcePath); FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(SourcePath + @"\" + SetupData.AppBinary); Directory.CreateDirectory(App.appPath + ResPath); foreach (string fileName in Directory.GetFiles(App.appPath + ResPath)) { File.Delete(fileName); } StreamWriter indexStram = new StreamWriter(App.appPath + ResPath + @"\FILE_IDX.TXT"); var foundFiles = MiscFunc.EnumAllFiles(SourcePath); Console.WriteLine("Found Files:"); for (int i = 0; i < foundFiles.Count; i++) { var filePath = foundFiles[i]; var fileName = filePath.Substring(SourcePath.Length + (SourcePath[SourcePath.Length - 1].Equals('\\') ? 0 : 1)); var resName = "FILE_" + i.ToString().PadLeft(3, '0') + ".BIN"; string hashStr = ""; using (var inStream = File.OpenRead(filePath)) { using (Stream outStream = File.Create(App.appPath + ResPath + @"\" + resName)) { using (Stream packStream = new DeflateStream(outStream, CompressionMode.Compress)) { inStream.CopyTo(packStream); } } using (var md5 = MD5.Create()) { inStream.Seek(0, SeekOrigin.Begin); var hash = md5.ComputeHash(inStream); hashStr = BitConverter.ToString(hash).Replace("-", ""); } } indexStram.WriteLine(resName + "\t" + hashStr + "\t" + fileName); Console.WriteLine(resName + "\t" + hashStr + "\t" + fileName); } Console.WriteLine("+++"); indexStram.Dispose(); try { return(CreateSetup(fvi)); } catch (FileNotFoundException) { Console.WriteLine("Failed to create setup, Mono.Cecil libraries are missing: https://github.com/jbevain/cecil/"); return(false); } }
private List <String> Extract(bool Install = false) { List <String> fileList = new List <string>(); List <Packer.FileInfo> files = App.packer.EnumFiles(); string ExtractPath = Data.InstallationPath; if (!Install) { if (ExtractPath.Contains(App.appPath)) { ExtractPath = "." + ExtractPath.Remove(0, App.appPath.Length); } } for (int i = 0; i < files.Count; i++) { var file = files[i]; string targetPath = Data.InstallationPath + @"\" + file.FileName; fileList.Add(targetPath); if (!Install && File.Exists(targetPath)) { string hashStr; using (var md5 = MD5.Create()) { using (var inStream = File.OpenRead(targetPath)) { byte[] hash = md5.ComputeHash(inStream); hashStr = BitConverter.ToString(hash).Replace("-", ""); } } if (hashStr.Equals(file.Hash)) { #if DEBUG Progress?.Invoke(this, new ProgressArgs() { Progress = (Install ? 75 : 100) * (i + 1) / files.Count, Message = "Skipping: " + ExtractPath + @"\" + file.FileName }); #endif continue; } } string Message = (Install ? "Installing:" : "Extracting: ") + ExtractPath + @"\" + file.FileName; Progress?.Invoke(this, new ProgressArgs() { Progress = (Install ? 75 : 100) * i / files.Count, Message = Message }); Message = null; if (!App.packer.ExtractFile(file, Data.InstallationPath)) { Message = "Extraction failed!"; } Progress?.Invoke(this, new ProgressArgs() { Progress = (Install ? 75 : 100) * (i + 1) / files.Count, Message = Message }); Thread.Sleep(10); } string IniPath; if (Install) { string progData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); if (progData == null) { progData = @"C:\ProgramData"; } IniPath = progData + "\\" + SetupData.AppKey; } else // Note: when the ini file ins inside the application directory the app starts in portable mode { IniPath = Data.InstallationPath + @"\Data"; } if (!Directory.Exists(IniPath)) { Directory.CreateDirectory(IniPath); } MiscFunc.SetAnyDirSec(IniPath); // ensure access for non admins IniPath += @"\" + SetupData.AppKey + ".ini"; App.IniWriteValue(IniPath, "Startup", "Usage", Data.Use.ToString()); if (Data.LicenseFile.Length > 0) { try { File.Copy(Data.LicenseFile, Data.InstallationPath + @"\" + LicenseFile); } catch { string Message = "Failed to copy license file to:" + Data.InstallationPath + @"\" + LicenseFile; Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = Message, Show = true }); } } if (Install) { using (StreamWriter indexStram = new StreamWriter(Data.InstallationPath + @"\" + InstalListFile)) { foreach (var filePath in fileList) { indexStram.WriteLine(filePath); } } } else { Progress?.Invoke(this, new ProgressArgs() { Progress = 100, Message = "Extraction completed." }); Thread.Sleep(100); } return(fileList); }
/*private void RestartService() * { * ServiceController controller = new ServiceController("priv10"); * if (controller.Status == ServiceControllerStatus.Stopped) * controller.Start(); * Progress?.Invoke(this, new ProgressArgs() { Progress = 90, Message = "Service Restarted" }); * }*/ private void Remove() { Progress?.Invoke(this, new ProgressArgs() { Progress = 10, Message = "Removing priv10 Service..." }); if (!Uninstall()) { Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = "Failed to remove Service", Show = true }); } Thread.Sleep(100); List <string> fileList = ReadFileList(); // get a list of old files if (fileList != null) { for (int i = 0; i < fileList.Count; i++) { var filePath = fileList[i]; // don't touch files outside the instalation directory if (!filePath.Substring(0, Data.InstallationPath.Length).Equals(Data.InstallationPath, StringComparison.OrdinalIgnoreCase)) { continue; } Progress?.Invoke(this, new ProgressArgs() { Progress = 20 + 30 * (i + 1) / fileList.Count, Message = "Removing: " + filePath }); if (!MiscFunc.SafeDelete(filePath)) { Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = "Removing failed!", Show = true }); } } MiscFunc.SafeDelete(Data.InstallationPath + @"\" + InstalListFile); MiscFunc.SafeDelete(Data.InstallationPath + @"\" + LicenseFile); // In case the user installed the application where he shouldn't have if (IsUnsafePath(Data.InstallationPath)) { Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = "Can not clean up instalation folder!", Show = true }); } else if (!MiscFunc.DeleteEmptyDir(Data.InstallationPath)) { string Message = "Failed to remove the application directory" + Data.InstallationPath + "\r\nThe folder is eider not empty or in use."; Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = Message, Show = true }); } } else { string Message = "install.lst not found in: " + Data.InstallationPath + "\r\nPlease remove the applciatrion files manually."; Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = Message, Show = true }); } Progress?.Invoke(this, new ProgressArgs() { Progress = 50, Message = "Removing Start Menu Entries..." }); string lnkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms), SetupData.AppTitle + ".lnk"); MiscFunc.SafeDelete(lnkPath); Thread.Sleep(100); if (Data.RemoveUserData) { Progress?.Invoke(this, new ProgressArgs() { Progress = 60, Message = "Removing configuration data..." }); string progData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); if (progData == null) { progData = @"C:\ProgramData"; } var IniPath = progData + "\\" + SetupData.AppKey; if (Directory.Exists(IniPath)) { var userFiles = MiscFunc.EnumAllFiles(IniPath); foreach (var filePath in userFiles) { MiscFunc.SafeDelete(filePath); } if (!MiscFunc.DeleteEmptyDir(IniPath)) { string Message = "Failed to remove the application data" + IniPath + "\r\nThe folder is eider not empty or in use."; Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = Message, Show = true }); } } } Progress?.Invoke(this, new ProgressArgs() { Progress = 80, Message = "Cleaningup Registry..." }); if (!Data.CleanupRegistry()) { Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = "Failed to cleanup Registry", Show = true }); } Thread.Sleep(100); if (Data.ResetFirewall) { Progress?.Invoke(this, new ProgressArgs() { Progress = 90, Message = "Restoring default Firewall configuration..." }); MiscFunc.Exec("netsh.exe", "advfirewall reset"); } Progress?.Invoke(this, new ProgressArgs() { Progress = 100, Message = "Uninstalation completed." }); Thread.Sleep(100); }
private void Update() { Progress?.Invoke(this, new ProgressArgs() { Progress = 0, Message = "Clossing running instances..." }); Shutdown(); List <string> oldList = ReadFileList(); // get a list of old files List <string> fileList = Extract(true); // note: this updates install.lst // remove old files if (oldList != null) { foreach (var filePath in oldList) { if (!fileList.Contains(filePath, StringComparer.OrdinalIgnoreCase)) { // don't touch files outside the instalation directory if (!filePath.Substring(0, Data.InstallationPath.Length).Equals(Data.InstallationPath, StringComparison.OrdinalIgnoreCase)) { continue; } Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = "Removing: " + filePath }); if (!MiscFunc.SafeDelete(filePath)) { Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = "Removing failed!", Show = true }); } } } } Progress?.Invoke(this, new ProgressArgs() { Progress = 75, Message = "Updating Registry..." }); if (!Data.UpdateRegistry()) { Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = "Failed to update the Registry", Show = true }); } Thread.Sleep(100); /*try * { * RestartService(); * } * catch { }*/ Progress?.Invoke(this, new ProgressArgs() { Progress = 85, Message = "Updating priv10 Service..." }); if (!UpdateSvc()) { Progress?.Invoke(this, new ProgressArgs() { Progress = -1, Message = "Failed to update Service", Show = true }); } Thread.Sleep(100); Progress?.Invoke(this, new ProgressArgs() { Progress = 100, Message = "Update completed." }); Thread.Sleep(100); }