Exemple #1
0
 private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
 {
     if ((pak != null) && (pak.isOpen))
     {
         pak.ClosePak();
         pak = null;
     }
     UpdateMM();
 }
        private void BtnCreateMod_Click(object sender, EventArgs e)
        {
            if (cbCreateSFX.Checked)
            {
                saveFileDlg.FilterIndex = 1;
            }
            else
            {
                saveFileDlg.FilterIndex = 2;
            }
            if (saveFileDlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            try
            {
                File.Delete(saveFileDlg.FileName);
                AAPak  modpak          = new AAPak(saveFileDlg.FileName, false, true);
                Bitmap customIconImage = null;

                // First check image we will use (if any)
                if (useDefaultImage)
                {
                    MemoryStream iconStream = new MemoryStream();
                    Properties.Resources.mod_example_icon.Save(iconStream, System.Drawing.Imaging.ImageFormat.Png);

                    /*
                     * if (!modpak.AddFileFromStream(ModPNGImageFileName, iconStream, DateTime.Now, DateTime.Now, false, out _))
                     * {
                     *  MessageBox.Show("Failed to add default icon");
                     *  modpak.ClosePak();
                     *  return;
                     * }
                     */
                    customIconImage = new Bitmap(Properties.Resources.mod_example_icon.GetThumbnailImage(128, 128, null, new IntPtr()));
                }
                else
                if (NewCustomImage != string.Empty)
                {
                    FileStream iconStream = File.Open(NewCustomImage, FileMode.Open, FileAccess.Read, FileShare.Read);

                    /*
                     * if ( (Path.GetExtension(NewCustomImage).ToLower() == ".png") && (!modpak.AddFileFromStream(ModPNGImageFileName, iconStream, DateTime.Now, DateTime.Now, false, out _)) )
                     * {
                     *  MessageBox.Show("Failed to add PNG icon");
                     *  modpak.ClosePak();
                     *  return;
                     * }
                     * else
                     * if ((Path.GetExtension(NewCustomImage).ToLower() == ".jpg") && (!modpak.AddFileFromStream(ModJPGImageFileName, iconStream, DateTime.Now, DateTime.Now, false, out _)))
                     * {
                     *  MessageBox.Show("Failed to add JPG icon");
                     *  modpak.ClosePak();
                     *  return;
                     * }
                     */
                    Image img = Image.FromStream(iconStream);
                    customIconImage = new Bitmap(img.GetThumbnailImage(128, 128, null, new IntPtr()));
                }
                else
                if (useOldPNGImage)
                {
                    var oldpngStream = mainPak.ExportFileAsStream(ModPNGImageFileName);

                    /*
                     * if ((Path.GetExtension(NewCustomImage).ToLower() == ".png") && (!modpak.AddFileFromStream(ModPNGImageFileName, oldpngStream, DateTime.Now, DateTime.Now, false, out _)))
                     * {
                     *  MessageBox.Show("Failed to copy PNG icon");
                     *  modpak.ClosePak();
                     *  return;
                     * }
                     */
                    Image img = Image.FromStream(oldpngStream);
                    customIconImage = new Bitmap(img.GetThumbnailImage(128, 128, null, new IntPtr()));
                }
                else
                if (useOldJPGImage)
                {
                    var oldjpgStream = mainPak.ExportFileAsStream(ModJPGImageFileName);

                    /*
                     * if ((Path.GetExtension(NewCustomImage).ToLower() == ".png") && (!modpak.AddFileFromStream(ModJPGImageFileName, oldjpgStream, DateTime.Now, DateTime.Now, false, out _)))
                     * {
                     *  MessageBox.Show("Failed to copy PNG icon");
                     *  modpak.ClosePak();
                     *  return;
                     * }
                     */
                    Image img = Image.FromStream(oldjpgStream);
                    customIconImage = new Bitmap(img.GetThumbnailImage(128, 128, null, new IntPtr()));
                }
                else
                {
                    // No image used
                }

                if ((cbCreateSFX.Checked) && (customIconImage != null))
                {
                    // Create some temp files
                    string tempIconFile = Path.GetTempFileName(); // .GetTempPath() + "aamod_tmp.ico";
                    string tempEXEFile  = Path.GetTempFileName(); // .GetTempPath() + "aamod_tmp.exe";
                    try
                    {
                        // Very hacky way of adding/editing a icon to the sfx exe to add

                        // Create 64x64 "thumbnail" as icon
                        var pu   = GraphicsUnit.Pixel;
                        var cImg = customIconImage.Clone(customIconImage.GetBounds(ref pu), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        cImg.SetResolution(72, 72);
                        var tempImg = cImg.GetThumbnailImage(64, 64, null, new IntPtr());
                        // tempImg.Save(Path.GetTempPath() + "tmp_aamod.bmp");

                        // Create and Save temporary ico file
                        // Icon ic = ImageToIcon(tempImg);
                        var tempBitmap = new Bitmap(tempImg, new Size(64, 64));

                        MemoryStream iconStream = SaveAsIconMemoryStream(tempBitmap);
                        // MemoryStream iconStream = new MemoryStream();
                        // ic.Save(iconStream);
                        FileStream iconStreamFile = File.Create(tempIconFile);
                        iconStream.Position = 0;
                        iconStream.CopyTo(iconStreamFile);
                        iconStreamFile.Close();

                        // Save temporary exe file
                        MemoryStream exeStream = new MemoryStream(Properties.Resources.AAModSFX);
                        FileStream   fs        = File.Create(tempEXEFile);
                        exeStream.CopyTo(fs);
                        fs.Close();
                        exeStream.Close();


                        // Create IconFile from temporary ico file
                        IconFile icf = new IconFile(tempIconFile);

                        // Delete old Icon resources
                        ResourceInfo vi = new ResourceInfo();
                        vi.Load(tempEXEFile);
                        foreach (ResourceId id in vi.ResourceTypes)
                        {
                            if (id.ResourceType != Kernel32.ResourceTypes.RT_GROUP_ICON)
                            {
                                continue;
                            }
                            foreach (Resource resource in vi.Resources[id])
                            {
                                resource.DeleteFrom(tempEXEFile);
                            }
                        }

                        // Add to IconDirectory
                        IconDirectoryResource iconDirectoryResource = new IconDirectoryResource(icf);
                        IconResource          icr = new IconResource();
                        // Save to temporary exe
                        iconDirectoryResource.SaveTo(tempEXEFile);

                        // Read temporary exe back into stream
                        fs        = File.OpenRead(tempEXEFile);
                        exeStream = new MemoryStream();
                        fs.CopyTo(exeStream);

                        // Add modified exe to modpak
                        if (!modpak.AddFileFromStream(SFXInfoFileName, exeStream, DateTime.Now, DateTime.Now, true, out _))
                        {
                            MessageBox.Show("Failed to add modified SFX executable");
                            modpak.ClosePak();
                            return;
                        }
                    }
                    catch (Exception x)
                    {
                        MessageBox.Show("Exception editing icon:\n" + x.Message);
                        modpak.ClosePak();
                        return;
                    }
                    try
                    {
                        File.Delete(tempEXEFile);
                    }
                    catch { }
                    try
                    {
                        File.Delete(tempIconFile);
                    }
                    catch { }
                }
                else
                // If you want to use the aamod as a SFX, the .exe needs to be the first file in the pak
                if (cbCreateSFX.Checked)
                {
                    // This AAModSFX resource is loaded from the RELEASE build of the AAMod project, make sure it's compiled as release first if you made changes to it
                    MemoryStream sfxStream = new MemoryStream(Properties.Resources.AAModSFX);
                    // We will be possibly be editing the icon, so it's a good idea to have some spare space here
                    if (!modpak.AddFileFromStream(SFXInfoFileName, sfxStream, DateTime.Now, DateTime.Now, true, out _))
                    {
                        MessageBox.Show("Failed to add SFX executable");
                        modpak.ClosePak();
                        return;
                    }
                }

                var modInfoStream = AAPak.StringToStream(tDescription.Text);
                if (!modpak.AddFileFromStream(ModInfoFileName, modInfoStream, DateTime.Now, DateTime.Now, false, out _))
                {
                    MessageBox.Show("Failed to add description");
                    modpak.ClosePak();
                    return;
                }

                if (customIconImage != null)
                {
                    MemoryStream imgStream = new MemoryStream();
                    customIconImage.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png);
                    if (!modpak.AddFileFromStream(ModPNGImageFileName, imgStream, DateTime.Now, DateTime.Now, false, out _))
                    {
                        MessageBox.Show("Failed to add icon image");
                        modpak.ClosePak();
                        return;
                    }
                }


                // Copy all files
                foreach (var fi in mainPak.files)
                {
                    if (fi.name.StartsWith(ModFileFolderName))
                    {
                        continue;
                    }
                    var ms = mainPak.ExportFileAsStream(fi);
                    if (!modpak.AddFileFromStream(fi.name, ms, DateTime.FromFileTimeUtc(fi.createTime), DateTime.FromFileTimeUtc(fi.modifyTime), false, out _))
                    {
                        MessageBox.Show("Failed to copy \n" + fi.name + "\nAborting !", "Copy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        modpak.ClosePak();
                        return;
                    }
                }
                modpak.ClosePak();

                MessageBox.Show("AAMod create completed !", "AAMod Create", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
            }
            catch (Exception x)
            {
                MessageBox.Show("Exception: " + x.Message);
            }
        }