public bool Convert() { // 0. Write header for(int i = 0; i < coHeader.Length; i++) { AddScriptLine(coHeader[i]); } // Defines all values from ProductInfo as symbols AddScriptLine(""); AddCommentLine("Product details"); mDefinedSymbols = new Dictionary<ProductInfo,string>((int)ProductInfo._Count); for(int i = 0; i < (int)ProductInfo._Count; i++) { string value = GetValue((ProductInfo)i); if(!String.IsNullOrEmpty(value)) { AddScriptLine(DefineSymbol((ProductInfo)i, value)); } } // 1. Get general installer info - [Setup] section //AddCommentLine("Installer details"); AddSection("[Setup]"); AddSetupDirective("AppName", GetValue(ProductInfo.ProductName)); AddSetupDirective("AppVersion", GetValue(ProductInfo.ProductVersion)); AddSetupDirective("AppId", GetValue(ProductInfo.ProductCode)); AddSetupDirective("DefaultDirName", ReplaceAllVariablesInText(mVdProject.DefaultDir)); AddSetupDirective("DefaultGroupName", GetValue(ProductInfo.ProductName)); AddSetupDirective("AppCopyright", "Copyright (c) " + DateTime.Now.Year.ToString() + " " + GetValue(ProductInfo.Manufacturer)); AddSetupDirective("AppPublisher", GetValue(ProductInfo.Manufacturer)); if (!String.IsNullOrEmpty(mVdProject.Output)) { int pos = mVdProject.Output.LastIndexOf('\\'); if (pos > 0) { AddSetupDirective("OutputDir", mVdProject.Output.Substring(0, pos)); AddSetupDirective("OutputBaseFilename", RemoveExtension(mVdProject.Output.Substring(pos + 1))); } else AddSetupDirective("OutputBaseFilename", RemoveExtension(mVdProject.Output)); } // 2-1. Create files and directories - [Files] section if (mVdProject.Files != null && mVdProject.Files.Count > 0) { AddSection("[Files]"); for (int i = 0; i < mVdProject.Files.Count; i++) { // Source: "CTL3DV2.DLL"; DestDir: "{sys}"; DestName: "MYPROG2.EXE" Flags: onlyifdoesntexist uninsneveruninstall string line = null; if (String.IsNullOrEmpty(mVdProject.Files[i].DestName)) { line = string.Format("Source: \"{0}\"; DestDir: \"{1}\"; %1Flags: {2}", ReplaceFolderWithSymbol(mVdProject.Files[i].SourceName, 0), ReplaceFolderVariables(mVdProject.GetFolderPathById(mVdProject.Files[i].FolderId), 0), GetFileFlags(mVdProject.Files[i])); } else { line = string.Format("Source: \"{0}\"; DestDir: \"{1}\"; DestName: \"{2}\"; %1Flags: {3}", ReplaceFolderWithSymbol(mVdProject.Files[i].SourceName, 0), ReplaceFolderVariables(mVdProject.GetFolderPathById(mVdProject.Files[i].FolderId), 0), mVdProject.Files[i].DestName, GetFileFlags(mVdProject.Files[i])); } switch (mVdProject.Files[i].Details.Type) { case FileType.Assembly: { if (mVdProject.Files[i].Details.IsInGAC) { // If Processor architecture is missing, fill in MSIL string asmName = mVdProject.Files[i].Details.AsmName; if (!asmName.Contains("ProcessorArchitecture")) asmName = asmName + ", ProcessorArchitecture=MSIL"; line = line.Replace("%1", "StrongAssemblyName: \"" + asmName + "\"; "); } } break; case FileType.File: { } break; case FileType.Font: { if (mVdProject.Files[i].Details.Register) line = line.Replace("%1", "FontInstall: \"<Insert name of your font HERE!>\"; "); } break; case FileType.Library: { } break; } line = line.Replace("%1", ""); if (mVdProject.Files[i].Exclude) CommentLine(AddScriptLine(line)); else AddScriptLine(line); } } // 2-2. Insert Other files into [Files] section if (mVdProject.OtherTypeObjects != null && mVdProject.OtherTypeObjects.Count > 0) { if (mVdProject.Files == null || mVdProject.Files.Count == 0) AddSection("[Files]"); for (int i = 0; i < mVdProject.OtherTypeObjects.Count; i++) { // At this moment add only ProjectOutput files if (mVdProject.OtherTypeObjects[i].Type != OtherType.ProjectOutput) continue; // Source: "CTL3DV2.DLL"; DestDir: "{sys}"; DestName: "MYPROG2.EXE" Flags: onlyifdoesntexist uninsneveruninstall string line = null; if (String.IsNullOrEmpty(mVdProject.OtherTypeObjects[i].DestName)) { line = string.Format("Source: \"{0}\"; DestDir: \"{1}\";", ReplaceFolderWithSymbol(mVdProject.OtherTypeObjects[i].SourceName, 0), ReplaceFolderVariables(mVdProject.GetFolderPathById(mVdProject.OtherTypeObjects[i].FolderId), 0)); } else { line = string.Format("Source: \"{0}\"; DestDir: \"{1}\"; DestName: \"{2}\";", ReplaceFolderWithSymbol(mVdProject.OtherTypeObjects[i].SourceName, 0), ReplaceFolderVariables(mVdProject.GetFolderPathById(mVdProject.OtherTypeObjects[i].FolderId), 0), mVdProject.OtherTypeObjects[i].DestName); } if (mVdProject.OtherTypeObjects[i].Exclude) CommentLine(AddScriptLine(line)); else AddScriptLine(line); } } // 3. Create registry entries - [Registry] section if (mVdProject.RegistryKeys != null && mVdProject.RegistryKeys.Count > 0) { AddSection("[Registry]"); for (int i = 0; i < mVdProject.RegistryKeys.Count; i++) { // Root: HKLM; Subkey: "Software\My Company"; Flags: uninsdeletekeyifempty string line = null; if (mVdProject.RegistryKeys[i].Values == null) { line = String.Format("Root: {0}; Subkey: \"{1}\"; Flags: uninsdeletekeyifempty", mVdProject.RegistryKeys[i].Root.ToString(), ReplaceSetupVariables(mVdProject.RegistryKeys[i].Path, 0)); AddScriptLine(line); } else { for (int j = 0; j < mVdProject.RegistryKeys[i].Values.Count; j++) { line = String.Format("Root: {0}; Subkey: \"{1}\"; ValueType: {2}; ValueName: \"{3}\"; ValueData: \"{4}\"; Flags: uninsdeletekeyifempty", mVdProject.RegistryKeys[i].Root.ToString(), ReplaceSetupVariables(mVdProject.RegistryKeys[i].Path, 0), GetRegKeyType(mVdProject.RegistryKeys[i].Values[j].Type), mVdProject.RegistryKeys[i].Values[j].Name, mVdProject.RegistryKeys[i].Values[j].Value); AddScriptLine(line); } } } } // 4. Create shortcuts - [Icons] section if (mVdProject.Shortcuts != null && mVdProject.Shortcuts.Count > 0) { AddSection("[Icons]"); for (int i = 0; i < mVdProject.Shortcuts.Count; i++) { // Name: "{group}\My Program"; Filename: "{app}\MYPROG.EXE"; WorkingDir: "{app}" string line = null; //if (String.IsNullOrEmpty(mVdProject.Shortcuts[i].Name)) { // Set required parameters line = string.Format("Name: \"{0}\"; Filename: \"{1}\";%1%2%3%4 Flags: {2}", GetShortcutFullName(mVdProject.Shortcuts[i]), GetShortcutTarget(mVdProject.Shortcuts[i].TargetId), GetShortcutFlag(mVdProject.Shortcuts[i].IsFolder)); // Replace %1 -> Parameters if (String.IsNullOrEmpty(mVdProject.Shortcuts[i].Arguments)) line = line.Replace("%1", ""); else line = line.Replace("%1", " Parameters: \"" + mVdProject.Shortcuts[i].Arguments + "\";"); // Replace %2 -> WorkingDir if (String.IsNullOrEmpty(mVdProject.Shortcuts[i].WorkingFolderId)) line = line.Replace("%2", ""); else line = line.Replace("%2", " WorkingDir: \"" + ReplaceFolderVariables(mVdProject.GetFolderPathById(mVdProject.Shortcuts[i].WorkingFolderId), 0) + "\";"); // Replace %3 -> IconFilename string iconFile = null; if (String.IsNullOrEmpty(mVdProject.Shortcuts[i].IconId)) line = line.Replace("%3", ""); else { // a) Get file BaseFile shortcutFile = mVdProject.GetFileById(mVdProject.Shortcuts[i].IconId); if (shortcutFile != null) { iconFile = ReplaceFolderVariables(mVdProject.GetFolderPathById(shortcutFile.FolderId) + "\\" + shortcutFile.GetName(), 0); line = line.Replace("%3", " IconFilename: \"" + iconFile + "\";"); } else line = line.Replace("%3", ""); } // Replace %4 -> IconIndex if (String.IsNullOrEmpty(iconFile)) line = line.Replace("%4", ""); else line = line.Replace("%4", " IconIndex: \"" + mVdProject.Shortcuts[i].IconIndex.ToString() + "\";"); } AddScriptLine(line); } } return true; }
public bool Convert() { Dictionary <int, string> sections = new Dictionary <int, string>(5); bool useUninstaller = true; // 0. Write header for (int i = 0; i < coHeader.Length; i++) { AddScriptLine(coHeader[i]); } // Include files AddScriptLine(""); AddCommentLine("Included files"); AddScriptLine("!include \"MUI2.nsh\""); // Defines all values from ProductInfo as symbols AddScriptLine(""); AddCommentLine("Product details"); mDefinedSymbols = new Dictionary <ProductInfo, string>((int)ProductInfo._Count); for (int i = 0; i < (int)ProductInfo._Count; i++) { string value = GetValue((ProductInfo)i); if (!String.IsNullOrEmpty(value)) { AddScriptLine(DefineSymbol((ProductInfo)i, value)); } } // 1. Get general installer info AddScriptLine(""); AddCommentLine("Installer details"); AddSetupProperty("Name", GetValue(ProductInfo.ProductName), true); AddSetupProperty("Caption", GetValue(ProductInfo.ProductName) + " " + GetValue(ProductInfo.ProductVersion), true); AddSetupProperty("BrandingText", GetValue(ProductInfo.ProductName), true); AddSetupProperty("ShowInstDetails", "show", false); AddSetupProperty("InstallDir", ReplaceAllVariablesInText(mVdProject.DefaultDir), true); //AddSetupProperty("DefaultGroupName", GetValue(ProductInfo.ProductName)); //AddSetupProperty("AppCopyright", "Copyright (c) " + DateTime.Now.Year.ToString() + " " + GetValue(ProductInfo.Manufacturer)); AddSetupProperty("RequestExecutionLevel", "user", false); if (!String.IsNullOrEmpty(mVdProject.Output)) { AddSetupProperty("OutFile", RemoveExtension(mVdProject.Output) + ".exe", true); } // Interface Settings AddScriptLine(""); AddCommentLine("Interface Settings"); AddScriptLine("!define MUI_ABORTWARNING"); AddScriptLine("!insertmacro MUI_PAGE_WELCOME"); //AddScriptLine("!insertmacro MUI_PAGE_LICENSE "${NSISDIR}\Docs\Modern UI\License.txt""); AddScriptLine("!insertmacro MUI_PAGE_COMPONENTS"); AddScriptLine("!insertmacro MUI_PAGE_DIRECTORY"); AddScriptLine("!insertmacro MUI_PAGE_INSTFILES"); AddScriptLine("!insertmacro MUI_UNPAGE_CONFIRM"); AddScriptLine("!insertmacro MUI_UNPAGE_INSTFILES"); AddScriptLine("!insertmacro MUI_PAGE_FINISH"); if (useUninstaller) { AddScriptLine(""); AddScriptLine("!insertmacro MUI_UNPAGE_WELCOME"); AddScriptLine("!insertmacro MUI_UNPAGE_CONFIRM"); AddScriptLine("!insertmacro MUI_UNPAGE_INSTFILES"); AddScriptLine("!insertmacro MUI_UNPAGE_FINISH"); } AddScriptLine(""); AddScriptLine("!insertmacro MUI_LANGUAGE \"English\""); // Create .onInit Function AddFunctionStart(".onInit"); AddScriptLine(" InitPluginsDir"); AddFunctionEnd(); AddScriptLine(""); AddCommentLine("Installer Sections"); // 2. Create files and directories - Main section if (mVdProject.Files != null && mVdProject.Files.Count > 0) { AddSectionStart(GetValue(ProductInfo.ProductName), 1); sections.Add(1, GetValue(ProductInfo.ProductName)); AddCommentLine("This is main section of installer - it contains files and folders"); if (mVdProject.Folders != null && mVdProject.Folders.Count > 0) { for (int i = 0; i < mVdProject.Folders.Count; i++) { string line = null; // Out directory line = String.Format(" SetOutPath \"{0}\"", ReplaceFolderVariables(mVdProject.GetFolderPathById(mVdProject.Folders[i].Id), 0)); AddScriptLine(line); // Add all files in this directory for (int j = 0; j < mVdProject.Files.Count; j++) { if (mVdProject.Files[j].FolderId == mVdProject.Folders[i].Id) { // File [/nonfatal] [/a] ([/r] [/x file|wildcard [...]] (file|wildcard) [...] | /oname=file.dat infile.dat) if (String.IsNullOrEmpty(mVdProject.Files[j].DestName)) { line = string.Format(" File \"{0}\"", ReplaceFolderWithSymbol(mVdProject.Files[j].SourceName, 0)); } else { line = string.Format(" File \"/oname={0}\" \"{1}\"", mVdProject.Files[j].DestName, ReplaceFolderWithSymbol(mVdProject.Files[j].SourceName, 0)); } if (mVdProject.Files[j].Exclude) { CommentLine(AddScriptLine(line)); } else { AddScriptLine(line); } } } // Add other files in this directory (ProjectOutput) if (mVdProject.OtherTypeObjects != null) { for (int j = 0; j < mVdProject.OtherTypeObjects.Count; j++) { if (mVdProject.OtherTypeObjects[j].FolderId == mVdProject.Folders[i].Id && mVdProject.OtherTypeObjects[j].Type == OtherType.ProjectOutput) { // File [/nonfatal] [/a] ([/r] [/x file|wildcard [...]] (file|wildcard) [...] | /oname=file.dat infile.dat) if (String.IsNullOrEmpty(mVdProject.OtherTypeObjects[j].DestName)) { line = string.Format(" File \"{0}\"", mVdProject.OtherTypeObjects[j].SourceName); } else { line = string.Format(" File \"{0}\" \"/oname={1}\"", mVdProject.OtherTypeObjects[j].SourceName, mVdProject.OtherTypeObjects[j].DestName); } if (mVdProject.OtherTypeObjects[j].Exclude) { CommentLine(AddScriptLine(line)); } else { AddScriptLine(line); } } } } } } AddSectionEnd(); } // 3. Create shortcuts - Shortcuts section if (mVdProject.Shortcuts != null && mVdProject.Shortcuts.Count > 0) { AddSectionStart("Create Shortcuts", 2); sections.Add(2, "Create Shortcuts"); for (int i = 0; i < mVdProject.Shortcuts.Count; i++) { // CreateShortCut link.lnk target.file [parameters [icon.file [icon_index_number [start_options [keyboard_shortcut [description]]]]]] string line = null; //if (String.IsNullOrEmpty(mVdProject.Shortcuts[i].Name)) { // Set required parameters line = string.Format(" CreateShortCut \"{0}.lnk\" \"{1}\"%1%3%4", GetShortcutFullName(mVdProject.Shortcuts[i]), GetShortcutTarget(mVdProject.Shortcuts[i].TargetId)); // Replace %1 -> Parameters if (String.IsNullOrEmpty(mVdProject.Shortcuts[i].Arguments)) { line = line.Replace("%1", ""); } else { line = line.Replace("%1", " \"" + mVdProject.Shortcuts[i].Arguments + "\""); } /* * // Replace %2 -> WorkingDir * if (String.IsNullOrEmpty(mVdProject.Shortcuts[i].WorkingFolderId)) * line = line.Replace("%2", ""); * else * line = line.Replace("%2", " WorkingDir: \"" + ReplaceFolderVariables(mVdProject.GetFolderPathById(mVdProject.Shortcuts[i].WorkingFolderId)) + "\""); */ // Replace %3 -> IconFilename string iconFile = null; if (String.IsNullOrEmpty(mVdProject.Shortcuts[i].IconId)) { line = line.Replace("%3", ""); } else { // a) Get file BaseFile shortcutFile = mVdProject.GetFileById(mVdProject.Shortcuts[i].IconId); if (shortcutFile != null) { iconFile = ReplaceFolderVariables(mVdProject.GetFolderPathById(shortcutFile.FolderId) + "\\" + shortcutFile.GetName(), 0); line = line.Replace("%3", " \"" + iconFile + "\""); } } // Replace %4 -> IconIndex if (String.IsNullOrEmpty(iconFile)) { line = line.Replace("%4", ""); } else { line = line.Replace("%4", " " + mVdProject.Shortcuts[i].IconIndex.ToString()); } } AddScriptLine(line); } AddSectionEnd(); } // 4. Create registry entries - -Post section AddSectionStart("-Post", 3); sections.Add(3, "Finalize"); if (mVdProject.RegistryKeys != null && mVdProject.RegistryKeys.Count > 0) { for (int i = 0; i < mVdProject.RegistryKeys.Count; i++) { // HKLM "Software\My Company" "Path" value string line = null; if (mVdProject.RegistryKeys[i].Values != null) { for (int j = 0; j < mVdProject.RegistryKeys[i].Values.Count; j++) { line = String.Format(" {0} {1} \"{2}\" \"{3}\" \"{4}\"", GetRegKeyType(mVdProject.RegistryKeys[i].Values[j].Type), mVdProject.RegistryKeys[i].Root.ToString(), ReplaceSetupVariables(mVdProject.RegistryKeys[i].Path, 0), mVdProject.RegistryKeys[i].Values[j].Name, mVdProject.RegistryKeys[i].Values[j].Value); AddScriptLine(line); } } } } // Write uninstaller if (useUninstaller) { AddCommentLine("Create uninstaller"); AddScriptLine(" WriteUninstaller \"$INSTDIR\\Uninstall.exe\""); } AddSectionEnd(); // 5. Create descriptions AddScriptLine(""); AddCommentLine("Sections descriptions"); string sectionName = null; for (int i = 0; i < sections.Count; i++) { sections.TryGetValue((i + 1), out sectionName); AddScriptLine("LangString DESC_Section0" + (i + 1).ToString() + " ${LANG_ENGLISH} \"" + sectionName + "\""); } AddScriptLine(""); AddCommentLine("Assign language strings to sections"); AddScriptLine("!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN"); for (int i = 0; i < sections.Count; i++) { AddScriptLine(" !insertmacro MUI_DESCRIPTION_TEXT ${Section0" + (i + 1).ToString() + "} $(DESC_Section0" + (i + 1).ToString() + ")"); } AddScriptLine("!insertmacro MUI_FUNCTION_DESCRIPTION_END"); #region Uninstaller // 6. Uninstaller section if (useUninstaller) { AddScriptLine(""); AddCommentLine("Uninstaller Section"); AddSectionStart("Uninstall", 9); AddCommentLine("Delete uninstaller"); AddScriptLine(" Delete \"$INSTDIR\\Uninstall.exe\""); // 6-1. Delete shortcuts if (mVdProject.Shortcuts != null && mVdProject.Shortcuts.Count > 0) { AddScriptLine(""); AddCommentLine("Delete shortcuts"); for (int i = 0; i < mVdProject.Shortcuts.Count; i++) { string line = null; line = string.Format(" Delete \"{0}.lnk\"", GetShortcutFullName(mVdProject.Shortcuts[i])); AddScriptLine(line); } } // 6-2. Delete files and directories if (mVdProject.Files != null && mVdProject.Files.Count > 0) { AddScriptLine(""); AddCommentLine("Delete files and folders"); if (mVdProject.Folders != null && mVdProject.Folders.Count > 0) { for (int i = 0; i < mVdProject.Folders.Count; i++) { string line = null; // Do not delete System directories! string systemDirectory = mVdProject.GetFolderPathById(mVdProject.Folders[i].Id); // Delete all files in this directory for (int j = 0; j < mVdProject.Files.Count; j++) { if (mVdProject.Files[j].FolderId == mVdProject.Folders[i].Id) { if (String.IsNullOrEmpty(mVdProject.Files[j].DestName)) { line = string.Format(" Delete \"{0}\\{1}\"", ReplaceFolderVariables(systemDirectory, 0), mVdProject.Files[j].SourceName); } else { line = string.Format(" Delete \"{0}\\{1}\"", ReplaceFolderVariables(systemDirectory, 0), mVdProject.Files[j].DestName); } line = line.Replace(@"\\", @"\"); if (mVdProject.Files[j].Exclude) { CommentLine(AddScriptLine(line)); } else { AddScriptLine(line); } } } // Delete other type files in this directory (ProjectOutput, ...) if (mVdProject.OtherTypeObjects != null) { for (int j = 0; j < mVdProject.OtherTypeObjects.Count; j++) { if (mVdProject.OtherTypeObjects[j].FolderId == mVdProject.Folders[i].Id && mVdProject.OtherTypeObjects[j].Type == OtherType.ProjectOutput) { if (String.IsNullOrEmpty(mVdProject.OtherTypeObjects[j].DestName)) { line = string.Format(" Delete \"{0}\\{1}\"", ReplaceFolderVariables(systemDirectory, 0), mVdProject.OtherTypeObjects[j].SourceName); } else { line = string.Format(" Delete \"{0}\\{1}\"", ReplaceFolderVariables(systemDirectory, 0), mVdProject.OtherTypeObjects[j].DestName); } line = line.Replace(@"\\", @"\"); if (mVdProject.OtherTypeObjects[j].Exclude) { CommentLine(AddScriptLine(line)); } else { AddScriptLine(line); } } } } // Delete directory itself systemDirectory = mVdProject.GetFolderPathById(mVdProject.Folders[i].Id); if (!IsSystemDirectory(systemDirectory)) { systemDirectory = ReplaceFolderVariables(systemDirectory, 0); line = String.Format(" RMDir \"{0}\"", systemDirectory); AddScriptLine(line); AddScriptLine(""); } } } } // 6-3. Create registry entries - -Post section if (mVdProject.RegistryKeys != null && mVdProject.RegistryKeys.Count > 0) { AddScriptLine(""); AddCommentLine("Delete registry keys"); for (int i = 0; i < mVdProject.RegistryKeys.Count; i++) { string line = null; if (mVdProject.RegistryKeys[i].Values != null) { for (int j = 0; j < mVdProject.RegistryKeys[i].Values.Count; j++) { line = String.Format(" DeleteRegValue {0} \"{1}\" \"{2}\"", mVdProject.RegistryKeys[i].Root.ToString(), ReplaceSetupVariables(mVdProject.RegistryKeys[i].Path, 0), mVdProject.RegistryKeys[i].Values[j].Name); AddScriptLine(line); line = String.Format(" DeleteRegKey /ifempty {0} \"{1}\"", mVdProject.RegistryKeys[i].Root.ToString(), ReplaceSetupVariables(mVdProject.RegistryKeys[i].Path, 0)); AddScriptLine(line); } } } } // 6-4. Final AddSectionEnd(); } #endregion return(true); }