GetCode() public method

public GetCode ( BootMenu menu ) : string
menu BootMenu
return string
示例#1
0
        private void btnCustomCode_Click(object sender, EventArgs e)
        {
            var cit = lvIsos.SelectedRows[0];
            var bmi = new BootMenuItem(
                cit.Cells[0].Value?.ToString() ?? "",
                cit.Cells[3].Value?.ToString() ?? "",
                selectediline().EntryType,
                cit.Cells[4].Value?.ToString() ?? "")
            {
                Start = false
            };

            var cod = Grub2.GetCode(bmi);

            var edfrm = new EditCodeFrm("/images/" + Path.GetFileName(cit.Cells[4].Value.ToString()))
            {
                Code = cod
            };

            if (edfrm.ShowDialog(this) == DialogResult.OK)
            {
                lvIsos.SelectedRows[0].Cells[5].Value = edfrm.Code == cod ? "" : edfrm.Code;
                CurImages.First(x => x.Name == cit.Cells[0].Value.ToString()).CustomCode =
                    lvIsos.SelectedRows[0].Cells[5].Value.ToString();
            }
        }
示例#2
0
        public void Generate()
        {
            Thread.CurrentThread.CurrentCulture   = new CultureInfo(Settings.Default.Lang);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Settings.Default.Lang);


            var f = Program.GetTemporaryDirectory();

            lblStatus.Text = Strings.Init;
            Thread.Sleep(1000);

            var ext = new SevenZipExtractor();

            var isodir = _usb ? OutputFilepath : Path.Combine(f, "iso");

            if (_usb)
            {
                // format
                if (
                    MessageBox.Show(Strings.FormatWillErase.Replace(@"\n", "\n"), "SharpBoot", MessageBoxButtons.YesNo) !=
                    DialogResult.Yes)
                {
                    abort = true;
                    return;
                }
                var tries = 1;
                while (true)
                {
                    if (tries == 5)
                    {
                        MessageBox.Show(Strings.FormatError, "SharpBoot", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        abort = true;
                        return;
                    }
                    ChangeProgress(0, 100, Strings.Formatting);
                    var askform = new AskPath();
                    askform.SetTextMode(Text, Strings.VolumeLabel, string.Concat(Title.Where(char.IsLetter)));
                    var volumeLabel = "";
                    if (askform.ShowDialog() == DialogResult.OK)
                    {
                        volumeLabel = askform.FileName;
                    }
                    else
                    {
                        abort = true;
                        return;
                    }
                    uint res = 1;
                    if ((res = Utils.FormatDrive(OutputFilepath.Substring(0, 2), filesystem,
                                                 label: volumeLabel)) == 0)
                    {
                        ChangeProgress(100, 100, Strings.Formatting);

                        break;
                    }
                    else
                    {
                        switch (res)
                        {
                        case 3:
                            MessageBox.Show(Strings.NeedAdmin, "SharpBoot", MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                            abort = true;
                            return;

                        case 2:
                            abort = true;
                            return;
                        }
                        if (
                            MessageBox.Show(Strings.FormatError, "SharpBoot", MessageBoxButtons.RetryCancel,
                                            MessageBoxIcon.Error) == DialogResult.Cancel)
                        {
                            abort = true;
                            return;
                        }
                    }
                    tries++;
                }
            }

            if (bwkISO.CancellationPending)
            {
                abort = true;
                return;
            }

            var workingDir = Path.Combine(isodir, "boot", "grub");

            if (!Directory.Exists(workingDir))
            {
                Directory.CreateDirectory(workingDir);
            }

            var isoroot = Path.Combine(isodir, "images");


            var mkisofsexe = Path.Combine(f, "mkisofs", "mkisofs.exe");
            var archs      = Path.Combine(f, "arch");

            Directory.CreateDirectory(archs);

            File.WriteAllBytes(Path.Combine(archs, "basedisk.7z"), Resources.basedisk);
            if (!_usb)
            {
                File.WriteAllBytes(Path.Combine(archs, "mkisofs.7z"), Resources.mkisofs);
            }

            ChangeProgress(0, 100, Strings.ExtractBaseDisk + " 1/6");
            ext.Extract(Path.Combine(archs, "basedisk.7z"), isodir);
            if (bwkISO.CancellationPending)
            {
                abort = true;
                return;
            }

            Image img = null;

            ChangeProgress(30, 100, Strings.ExtractBaseDisk + " 4/6");
            if (IsoBackgroundImage == "")
            {
                var ms = new MemoryStream(Resources.sharpboot);
                img = Image.FromStream(ms);
            }
            else if (IsoBackgroundImage != "$$NONE$$")
            {
                img = Image.FromFile(IsoBackgroundImage);
            }
            ChangeProgress(35, 100, Strings.ExtractBaseDisk + " 5/6");
            Grub2.SetImage(img, Res, workingDir);
            ChangeProgress(45, 100, Strings.ExtractBaseDisk + " 6/6");
            if (!_usb)
            {
                ChangeProgress(50, 100, Strings.Extracting.FormatEx("Mkisofs"));

                ext.Extract(Path.Combine(archs, "mkisofs.7z"), Path.Combine(f, "mkisofs"));
            }
            ChangeProgressBar(60, 100);
            Program.SafeDel(archs);

            if (bwkISO.CancellationPending)
            {
                abort = true;
                return;
            }

            // copier les fichiers dans le rep temporaire
            ChangeProgress(0, Images.Count, Strings.CopyISOfiles);
            for (var i = 0; i < Images.Count; i++)
            {
                var current = Images[i].FilePath;
                ChangeProgress(i, Images.Count, string.Format(Strings.Copying, Path.GetFileName(current)));
                while (!Directory.Exists(isoroot))
                {
                    Directory.CreateDirectory(isoroot);
                }
                for (var j = 0; j < 5; j++)
                {
                    try
                    {
                        XCopy.Copy(current, Path.Combine(isoroot, Path.GetFileName(current)), true,
                                   true,
                                   (o, pce) => { ChangeProgressBar(pce.ProgressPercentage, 100); });
                        break;
                    }
                    catch
                    {
                    }
                }
            }
            ChangeProgressBar(0, CustomFiles.Count);

            for (var i = 0; i < CustomFiles.Count; i++)
            {
                var current = CustomFiles.ToList()[i];
                var local   = current.Key;
                var remote  = current.Value;
                if (remote.StartsWith("/"))
                {
                    remote = remote.Substring(1);
                }
                ChangeProgress(i, CustomFiles.Count, string.Format(Strings.Copying, Path.GetFileName(local)));
                while (!Directory.Exists(isodir))
                {
                    Directory.CreateDirectory(isodir);
                }
                for (var j = 0; j < 5; j++)
                {
                    try
                    {
                        XCopy.Copy(local, Path.Combine(isodir, remote), true,
                                   true,
                                   (o, pce) => { ChangeProgressBar(pce.ProgressPercentage, 100); });
                        break;
                    }
                    catch
                    {
                    }
                }
            }

            if (bwkISO.CancellationPending)
            {
                abort = true;
                return;
            }

            ChangeProgress(0, Categories.Count, Strings.GenMenus);


            var main = new BootMenu(Title, true);

            main.Items.Add(new BootMenuItem(Strings.BootFromHDD, Strings.BootFromHDD,
                                            EntryType.BootHDD));

            var ii = 0;

            //var itype = new Func<string, EntryType>(fn => Path.GetExtension(fn).ToLower() == ".img" ? EntryType.IMG : EntryType.ISO);

            if (bwkISO.CancellationPending)
            {
                abort = true;
                return;
            }

            foreach (var c in Categories)
            {
                if (string.IsNullOrWhiteSpace(c))
                {
                    ChangeProgress(ii, Categories.Count, Strings.GenMainMenu);
                    Images.Where(x => x.Category == c).All(x =>
                    {
                        main.Items.Add(new BootMenuItem(x.Name, x.Description,
                                                        x.EntryType, x.FilePath, false, x.CustomCode));
                        return(true);
                    });
                }
                else
                {
                    ChangeProgress(ii, Categories.Count, string.Format(Strings.GenMenu, c));
                    var t = new BootMenu(c, false);
                    Images.Where(x => x.Category == c).All(x =>
                    {
                        t.Items.Add(new BootMenuItem(x.Name, x.Description,
                                                     x.EntryType, x.FilePath, false, x.CustomCode));
                        return(true);
                    });

                    File.WriteAllText(Path.Combine(workingDir, Utils.CRC32(c)) + ".cfg",
                                      Grub2.GetCode(t), Program.GetEnc());
                    main.Items.Add(new BootMenuItem(c, c, EntryType.Category, Utils.CRC32(c), false));
                }

                ii++;

                if (bwkISO.CancellationPending)
                {
                    abort = true;
                    return;
                }
            }
            File.WriteAllText(Path.Combine(workingDir, "grub.cfg"), Grub2.GetCode(main));

            if (bwkISO.CancellationPending)
            {
                abort = true;
                return;
            }

            if (_usb)
            {
                ChangeProgress(23, 100, string.Format(Strings.InstallingBoot, "Grub2", OutputFilepath));
                Grub2.Install(OutputFilepath);
                GenF(f);
            }
            else
            {
                // TODO: Implement working progress printing (I can't get OutputDataReceived to work on my computer)
                ChangeProgress(23, 100, Strings.CreatingISO);
                Thread.Sleep(500);
                var p = new Process
                {
                    StartInfo =
                    {
                        UseShellExecute        = false,
                        FileName               = mkisofsexe,
                        RedirectStandardOutput = true,
                        CreateNoWindow         = true
                    }
                };
                p.StartInfo.Arguments += " -publisher \"SharpBoot\" -no-emul-boot -boot-load-size 4 -boot-info-table -r -J -b boot/grub/eltorito.img";
                p.StartInfo.Arguments += " -o \"" + OutputFilepath + "\" \"" + isodir + "\"";
                p.EnableRaisingEvents  = true;

                /*var a = new Action<object, DataReceivedEventArgs>((o, args) => MessageBox.Show(args.Data));
                 *
                 * p.OutputDataReceived += new DataReceivedEventHandler(a);
                 * p.ErrorDataReceived += new DataReceivedEventHandler(a);*/
                bool exitCaught = false;
                p.Exited += delegate
                {
                    exitCaught = true;
                    GenF(f);
                };

                Thread.Sleep(500);

                if (bwkISO.CancellationPending)
                {
                    abort = true;
                    return;
                }
                ChangeProgress(33, 100, string.Format(Strings.Extracting, "Mkisofs"));
                int iter = 0;
                while (true)
                {
                    if (iter == 5)
                    {
                        MessageBox.Show("Extraction of Mkisofs failed after: 5 attempts. Aborting.");
                        abort = true;
                        return;
                    }
                    if (!File.Exists(mkisofsexe))
                    {
                        if (!Directory.Exists(archs))
                        {
                            Directory.CreateDirectory(archs);
                        }
                        File.WriteAllBytes(Path.Combine(archs, "mkisofs.7z"), Resources.mkisofs);
                        ext.Extract(Path.Combine(archs, "mkisofs.7z"), Path.Combine(f, "mkisofs"));
                    }
                    else
                    {
                        break;
                    }
                    Thread.Sleep(500);
                    iter++;
                }
                ChangeProgress(43, 100, Strings.CreatingISO);
                try
                {
                    p.OutputDataReceived += (sender, args) =>
                    {
                        try
                        {
                            if (args?.Data == null)
                            {
                                return;
                            }
                            var o = args.Data.Trim();
                            if (!o.Contains('%'))
                            {
                                return;
                            }
                            var     pp = o.Substring(1, 5).Trim();
                            decimal d;
                            if (decimal.TryParse(pp, out d))
                            {
                                if (o[0] == ' ' && o[3] == '.' && o[6] == '%')
                                {
                                    ChangeProgress(Convert.ToInt32(Math.Round(d, 0, MidpointRounding.AwayFromZero)), 100, Strings.CreatingISO + "\t" + pp + "%");
                                }
                            }
                        }
                        catch
                        {
                        }
                    };
                    p.Start();
                    p.BeginOutputReadLine();
                    p.WaitForExit();

                    /*while (!p.WaitForExit(1))
                     * {
                     *  System.Threading.Thread.Sleep(500);
                     *  p.Refresh();
                     *  Application.DoEvents();
                     * }*/
                }
                catch (FileNotFoundException)
                {
                }

                /*p.BeginOutputReadLine();
                 *
                 * using (var reader = p.StandardOutput)
                 * {
                 * while (!reader.EndOfStream)
                 * {
                 *  var o = reader.ReadLine().Trim();
                 *  var pp = o.Substring(1, 5).Trim();
                 *  var d = decimal.Parse(pp);
                 *  if (o[0] == ' ' && o[3] == '.' && o[6] == '%')
                 *  {
                 *      ChangeProgress(Convert.ToInt32(Math.Round(d, 0, MidpointRounding.AwayFromZero)), 100, Strings.CopyISOfiles + "\t" + pp + "%");
                 *  }
                 * }
                 * }*/

                Thread.Sleep(500);
                if (!exitCaught)
                {
                    GenF(f);
                }
            }
            ext.Close();
        }