Esempio n. 1
0
        public void runAsar(string path, string fname = "")
        {
            string arg2 = "\\ASM\\Main.asm";

            Asar.init();


            if (fname == "")
            {
                Console.WriteLine("NoError = " + Asar.patch(path + arg2, ref ROM.DATA));
                Asarerror[] errors = Asar.geterrors();
                foreach (Asarerror e in errors)
                {
                    Console.WriteLine(e.Rawerrdata);
                }
                FileStream fs = new FileStream(path + "//TestROM//working.sfc", FileMode.OpenOrCreate, FileAccess.Write);
                fs.Write(ROM.DATA, 0, ROM.DATA.Length);
                fs.Close();
            }
            else
            {
                Console.WriteLine("NoError = " + Asar.patch(path + arg2, ref ROM.DATA));
                Asarerror[] errors = Asar.geterrors();
                foreach (Asarerror e in errors)
                {
                    Console.WriteLine(e.Rawerrdata);
                }
                Console.WriteLine("NoError " + fname);
                FileStream fs = new FileStream(fname, FileMode.OpenOrCreate, FileAccess.Write);
                fs.Write(ROM.DATA, 0, ROM.DATA.Length);
                fs.Close();
            }


            Asar.close();

            /*var process = new Process();
             * process.StartInfo.FileName = "cmd.exe";
             * //Do not create command propmpt window
             * process.StartInfo.CreateNoWindow = false;
             *
             * //Do not use shell execution
             * process.StartInfo.UseShellExecute = false;
             *
             * //Redirects error and output of the process (command prompt).
             * process.StartInfo.RedirectStandardError = true;
             * process.StartInfo.RedirectStandardOutput = true;
             * process.StartInfo.RedirectStandardInput = true;
             * //start a new process
             * process.Start();
             * string arg1 = "\"xkas.exe\"";
             * string arg2 = "\"ASM\\Main.asm\"";
             * string arg3 = "\"TestROM\\working.sfc\"";
             * process.StandardInput.WriteLine(@"cd " + path);
             * //WriteLog(@"cd " + path, Color.Black);
             * process.StandardInput.WriteLine(arg1 + " " + arg2 + " " + arg3);
             * //process.StandardInput.WriteLine(arg3);
             * process.StandardInput.WriteLine("exit");
             * //wait until process is running
             * process.WaitForExit();
             *
             * //reads output and error of command prompt to string.
             * string output = process.StandardOutput.ReadToEnd();
             * string error = process.StandardError.ReadToEnd();
             */
        }
Esempio n. 2
0
        /// <summary>
        /// Picks a ROM through a <see cref="SaveFileDialog"/> if not specified (or forced) and patch multi_midway.asm.
        /// </summary>
        private void PatchRom(bool saveAs = false)
        {
            try
            {
                // Similar as in ExportAsmTable(): Compress the tables first.
                CompressTable();

                // Now we save the midway points with a fixed name.
                Midway.ExportAsm(midways, new FileStream(directory + MmpAsm, FileMode.Create));
                UnsavedChanges = false;
            }
            catch
            {
                MessageBox.Show("Cannot create multi_midway_table.asm. Please check the file's permission or whether it's already in use.", "Cannot create " + MmpAsm, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (Asar.init())
            {
                Asar.reset();
                if (string.IsNullOrEmpty(romName) || saveAs)
                {
                    if (patchRomDialog.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    romName = patchRomDialog.FileName;
                }

                try
                {
                    // Remember that Asar expects headerless ROMs.
                    // The code seperates the raw data from the header.
                    byte[] fullRom = File.ReadAllBytes(patchRomDialog.FileName);

                    int    lHeader = fullRom.Length & 0x7fff;
                    byte[] header  = new byte[lHeader];
                    byte[] rom     = new byte[fullRom.Length - lHeader];
                    Array.Copy(fullRom, 0, header, 0, lHeader);
                    Array.Copy(fullRom, lHeader, rom, 0, fullRom.Length - lHeader);

                    // Patching starts...
                    if (Asar.patch(directory + "multi_midway.asm", ref rom))
                    {
                        try
                        {
                            // Now it's time to merge them back.
                            fullRom = new byte[rom.Length + lHeader];
                            Array.Copy(header, 0, fullRom, 0, lHeader);
                            Array.Copy(rom, 0, fullRom, lHeader, rom.Length);

                            File.WriteAllBytes(patchRomDialog.FileName, fullRom);
                        }
                        catch (IOException ex)
                        {
                            MessageBox.Show("An error appeared when patching Multiple Midway Points: " + ex.Message, "ROM couldn't be written", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Asar.close();
                            return;
                        }
                        StringBuilder warningBuilder = new StringBuilder();
                        foreach (var warning in Asar.getwarnings())
                        {
                            warningBuilder.AppendLine(warning.Fullerrdata);
                        }
                        string warnings    = warningBuilder.ToString();
                        string fullWarning = !string.IsNullOrEmpty(warnings) ? "The following warnings appeared:\n" + warnings : "";
                        using (LongMessage message = new LongMessage("Multiple Midway Points has been inserted successfully. It uses " + Asar.getprints()[0] + " bytes of freespace.\n" + fullWarning, "Patching successful"))
                        {
                            message.ShowDialog(this);
                        }
                    }
                    else
                    {
                        MessageBox.Show("An error appeared when patching Multiple Midway Points. See mmp.log for more information.", "ROM couldn't be patched", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        using (FileStream stream = new FileStream("mmp.log", FileMode.Create))
                        {
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                foreach (var warning in Asar.getwarnings())
                                {
                                    writer.WriteLine(warning.Fullerrdata);
                                }
                                foreach (var error in Asar.geterrors())
                                {
                                    writer.WriteLine(error.Fullerrdata);
                                }
                            }
                        }
                    }
                }
                catch (IOException ex)
                {
                    MessageBox.Show("An error appeared when patching Multiple Midway Points: " + ex.Message, "ROM couldn't be opened", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                finally
                {
                    Asar.close();
                }
            }
            else
            {
                MessageBox.Show("Asar couldn't be started. (Perhaps asar.dll is missing or a wrong version is used?)", "Couldn't open Asar", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }