Ejemplo n.º 1
0
 internal static void PatchScript(IWin32Window owner, PatchScript script, PatchProfile profile, SM64Lib.RomManager rommgr)
 {
     try
     {
         TweakBeforeApply?.Invoke();
         var mgr = new PatchingManager();
         mgr.Patch(
             script,
             rommgr,
             owner,
             new Dictionary <string, object>()
         {
             { "romfile", rommgr.RomFile },
             { "rommgr", rommgr },
             { "profilepath", profile?.FileName },
             { "files", profile.EmbeddedFiles },
             { "owner", owner }
         },
             General.GetAdditionalReferencedAssemblied());
         TweakAfterApply?.Invoke();
         MessageBoxEx.Show(owner, "Patched successfully.", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     catch (Exception)
     {
         TweakFailedApply?.Invoke();
         MessageBoxEx.Show(owner, "Error while executing the script. It probably contains errors.", "Script Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Ejemplo n.º 2
0
        private void ButtonX_ImportMdl_Click(object sender, EventArgs e)
        {
            ButtonX_ImportMdl.Image = null;
            var pap          = SelectedProfileAndPreset();
            var profile      = pap.profile;
            var preset       = pap.preset;
            int romAddr      = preset.RomAddress; // ValueFromText(TextBoxX_RomAddr.Text)
            int bankAddr     = preset.RamAddress; // ValueFromText(TextBoxX_BankAddr.Text)
            int maxLength    = preset.MaxLength;  // ValueFromText(TextBoxX_MaxLength.Text)
            var pm           = new PatchingManager();
            var scriptparams = new Dictionary <string, object>()
            {
                { "romfile", RomFile },
                { "presetName", preset.Name },
                { "presetDescription", preset.Description },
                { "RomAddress", preset.RomAddress },
                { "RamAddress", preset.RamAddress },
                { "MaxLength", preset.MaxLength },
                { "CollisionPointersArray", preset.CollisionPointers.ToArray() },
                { "GeoPointersArray", preset.GeometryPointers.ToArray() },
                { "ConvertedModelLength", mdl.Length },
                { "ConvertedModel", mdl },
                { "profilepath", profile.FileName },
                { "files", profile.EmbeddedFiles }
            };

            if (maxLength > 0 && mdl.Length > maxLength)
            {
                MessageBoxEx.Show("Model is bigger then the max allowed length!", "Model too big", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            ClearOutput();

            // Execute Script Before
            if (preset.ScriptBefore is object && !string.IsNullOrEmpty(preset.ScriptBefore.Script))
            {
                WriteOutput("Executing Script ...");
                scriptparams.AddOrUpdate("script", preset.ScriptBefore);
                pm.Patch(preset.ScriptBefore, this, scriptparams, General.GetAdditionalReferencedAssemblied());
            }

            int col = -1;
            var geo = Array.Empty <Geopointer>();
            int len = 0;

            ObjectModel.SaveResult sr = null;
            bool iscollisionempty     = mdl.Collision is null;
            bool isf3disempty         = mdl.Fast3DBuffer is null;
            var  fs = new FileStream(_RomFile, FileMode.Open, FileAccess.ReadWrite);
            var  bw = new BinaryWriter(fs);

            // Write to stream
            WriteOutput("Writing Model ...");
            sr = mdl.ToStream(fs, romAddr, romAddr - (bankAddr & 0xFFFFFF), (int)(bankAddr & 0xFF000000));
            if (sr is object)
            {
                geo = sr.GeoPointers.ToArray();
                col = sr.CollisionPointer;
                len = Conversions.ToInteger(sr.Length);
            }

            // Write Collision Pointer
            if (col > -1)
            {
                WriteOutput("Chaning Collision Pointers ...");
                foreach (int cp in SelectedPreset().CollisionPointers)
                {
                    fs.Position = cp;
                    bw.Write(SwapInts.SwapInt32(col));
                }
            }

            // Write Geopointer
            if (geo.Length > 0)
            {
                WriteOutput("Chaning Geometry Pointers ...");
                foreach (int gp in SelectedPreset().GeometryPointers)
                {
                    fs.Position = gp;
                    bw.Write(SwapInts.SwapInt32(geo[0].SegPointer));
                    fs.Position = gp - 4;
                    if (fs.ReadByte() == 0x15)
                    {
                        fs.WriteByte(geo[0].Layer);
                    }
                    else
                    {
                        fs.Position = gp - 8;
                        if (fs.ReadByte() == 0x13)
                        {
                            fs.WriteByte(geo[0].Layer);
                        }
                    }
                }
            }

            fs.Close();
            if (preset.ScriptAfter is object && !string.IsNullOrEmpty(preset.ScriptAfter.Script))
            {
                WriteOutput("Executing Script ...");
                scriptparams.AddOrUpdate("script", preset.ScriptAfter);
                pm.Patch(preset.ScriptAfter, this, scriptparams, General.GetAdditionalReferencedAssemblied());
            }

            if (col > -1)
            {
                WriteOutput($"Collision Pointer:{Constants.vbTab}{sr.CollisionPointer.ToString("X")}");
            }

            foreach (Geopointer g in geo)
            {
                WriteOutput($"DL-Pointer:{Constants.vbTab}{g.SegPointer.ToString("X")} ({g.Layer.ToString()})");
            }
            WriteOutput();
            WriteOutput(DateAndTime.Now.ToShortTimeString() + " - Done");

            // MessageBoxEx.Show("Model has been imported succesfully!", "Model imported", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ButtonX_ImportMdl.Image = My.Resources.Resources.icons8_checkmark_16px_1;
        }