static public ModuleInfo FromModuleInfoFile(string filePath, string folderName = "") { ModuleInfo moduleInfo = new ModuleInfo(); string[] lines = File.ReadAllLines(filePath); for (int i = 0; i < lines.Length; i++) { string line = lines[i]; if (line.StartsWith(ModuleInfo.NameBeginsWith)) { moduleInfo.Name = line.Remove(0, ModuleInfo.NameBeginsWith.Length); Console.WriteLine(moduleInfo.Name); moduleInfo.FolderName = folderName == "" ? moduleInfo.Name.RemoveWhitespace() : folderName; Console.WriteLine(moduleInfo.FolderName); } else if (line.StartsWith(ModuleInfo.VersionBeginsWith)) { moduleInfo.Version = line.Remove(0, ModuleInfo.VersionBeginsWith.Length); Console.WriteLine(moduleInfo.Version); } else if (line.StartsWith(ModuleInfo.AuthorBeginsWith)) { moduleInfo.Author = line.Remove(0, ModuleInfo.AuthorBeginsWith.Length); Console.WriteLine(moduleInfo.Author); } else if (line.StartsWith(ModuleInfo.AuthorLinkBeginsWith)) { moduleInfo.AuthorLink = line.Remove(0, ModuleInfo.AuthorLinkBeginsWith.Length); Console.WriteLine(moduleInfo.AuthorLink); } else if (line.StartsWith(ModuleInfo.AuthorIsWebLinkBeginsWith)) { string booleanString = line.Remove(0, ModuleInfo.AuthorIsWebLinkBeginsWith.Length); try { moduleInfo.AuthorIsWebLink = System.Convert.ToInt32(booleanString) > 0 ? true : false; } catch (System.Exception e) { Console.WriteLine("Can't convert to Integer: " + booleanString); Console.WriteLine(e.Message); moduleInfo.AuthorIsWebLink = false; } Console.WriteLine(moduleInfo.AuthorIsWebLink); } else if (line.StartsWith(ModuleInfo.ImagePathBeginsWith)) { moduleInfo.ImagePath = line.Remove(0, ModuleInfo.ImagePathBeginsWith.Length); Console.WriteLine(moduleInfo.ImagePath); } else if (line.StartsWith(ModuleInfo.IconPathBeginsWith)) { moduleInfo.IconPath = line.Remove(0, ModuleInfo.IconPathBeginsWith.Length); Console.WriteLine(moduleInfo.IconPath); } } return(moduleInfo); }
public Form1() { InitializeComponent(); BannerlordDir = BannerlordDirectoryFinder.Find(); InstallationDir = Path.Combine(BannerlordDir, "Modules"); textBoxInstallationDir.Text = InstallationDir; using (FileStream fs = new FileStream(Application.ExecutablePath, FileMode.Open, FileAccess.Read)) { byte[] magicGuidBytes = Program.MagicGuidBytes; fs.Position = fs.Length - magicGuidBytes.Length; byte[] comparisonBytes = new byte[magicGuidBytes.Length]; fs.Read(comparisonBytes, 0, comparisonBytes.Length); if (Program.ByteArrayCompare(ref magicGuidBytes, ref comparisonBytes)) { ModuleInfo = ModuleInfo.FromPackedStream(fs); } else { MessageBox.Show("Creating ModuleInfo.txt, BuildInstaller.bat and a ModuleFolder.", "Empty Installer"); using (StreamWriter sw = new StreamWriter("ModuleInfo.txt")) { sw.WriteLine(ModuleInfo.NameBeginsWith); sw.WriteLine(ModuleInfo.VersionBeginsWith); sw.WriteLine(ModuleInfo.AuthorBeginsWith); sw.WriteLine(ModuleInfo.AuthorLinkBeginsWith); sw.WriteLine(ModuleInfo.AuthorIsWebLinkBeginsWith); sw.WriteLine(ModuleInfo.ImagePathBeginsWith); sw.WriteLine(ModuleInfo.IconPathBeginsWith); } using (StreamWriter sw = new StreamWriter("BuildInstaller.bat")) { sw.WriteLine("\"" + Path.GetFileName(Application.ExecutablePath) + "\" -packModule ModuleInfo.txt ModuleFolder"); } Directory.CreateDirectory("ModuleFolder"); buttonInstall.Enabled = false; Environment.Exit(0); } } if (ModuleInfo != null) { #if DEBUG //ConsoleHelper.Initialize(); //Console.WriteLine(ModuleInfo.Name); //Console.WriteLine(ModuleInfo.Version); //Console.WriteLine(ModuleInfo.Author); //Console.WriteLine(ModuleInfo.AuthorLink); //Console.WriteLine(ModuleInfo.AuthorIsWebLink); //Console.WriteLine(ModuleInfo.Image); //Console.WriteLine(ModuleInfo.Icon); //ModuleInfo.Image.Save("image.jpg", ImageFormat.Jpeg); //using(FileStream fs = new FileStream("icon.ico", FileMode.Create)) // ModuleInfo.Icon.Save(fs); #endif ModulePath = Path.Combine(InstallationDir, ModuleInfo.FolderName); pictureBoxImage.Image = ModuleInfo.Image; this.Icon = ModuleInfo.Icon; this.Text = ModuleInfo.Name + " " + ModuleInfo.Version + " Installer"; linkLabelAuthor.Text = ModuleInfo.Author; if ((ModuleInfo.AuthorIsWebLink == false && ModuleInfo.AuthorLink == "") == false) { linkLabelAuthor.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelAuthor_LinkClicked); } //Check if Module Already Exists if (Directory.Exists(ModulePath)) { string message = "It appears that this Module is already installed.\n\n" + "A backup will be created.\n\n" + "Do you want to continue?"; DialogResult dialogResult = MessageBox.Show(message, "Warning", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { DoBackup = true; } else if (dialogResult == DialogResult.No) { buttonInstall.Enabled = false; Environment.Exit(0); } } } }