Beispiel #1
0
        private void SavePatchXML()
        {
            List <AsmPatch> patches = GetAllSelectedPatches();

            foreach (AsmPatch patch in patches)
            {
                patch.Update(AsmUtility);
            }

            FreeSpaceMode mode = FreeSpace.GetMode(AsmUtility);
            string        xml  = PatchXmlReader.CreatePatchXML(patches, mode);

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter          = "XML file (*.xml)|*.xml";
            saveFileDialog.FileName        = string.Empty;
            saveFileDialog.CheckFileExists = false;

            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                btn_SavePatchXML.Enabled = false;
                System.IO.File.WriteAllText(saveFileDialog.FileName, xml, Encoding.UTF8);
                PatcherLib.MyMessageBox.Show(this, "Complete!", "Complete!", MessageBoxButtons.OK);
                Close();
            }
        }
Beispiel #2
0
        private void btn_OpenConflictChecker_Click(object sender, EventArgs e)
        {
            FreeSpaceMode       mode = FreeSpace.GetMode(asmUtility);
            ConflictCheckerForm conflictCheckerForm = new ConflictCheckerForm(GetCurrentFilePatches(), mode);

            conflictCheckerForm.Show();
        }
Beispiel #3
0
 public ConflictCheckerForm(IList <AsmPatch> patchList, FreeSpaceMode mode)
 {
     InitializeComponent();
     this.origPatchList     = patchList;
     this.conflictPatchData = ConflictHelper.CheckConflicts(patchList, mode);
     FindPatchColors(this.conflictPatchData.PatchList);
     ShowPatches(this.conflictPatchData);
 }
Beispiel #4
0
        public FreeSpaceForm(List <AsmPatch> patchList, ASMEncoding.ASMEncodingUtility asmUtility)
        {
            InitializeComponent();
            FreeSpaceMode mode = (asmUtility.EncodingMode == ASMEncoding.ASMEncodingMode.PSP) ? FreeSpaceMode.PSP : FreeSpaceMode.PSX;

            InitFiles(mode);
            Init(patchList, asmUtility, mode);
        }
Beispiel #5
0
        public string CreateXML(FreeSpaceMode mode)
        {
            Context context = (mode == FreeSpaceMode.PSP) ? Context.US_PSP : Context.US_PSX;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendFormat("    <Patch name=\"{0}\">{1}", Name, Environment.NewLine);

            if (!string.IsNullOrEmpty(Description))
            {
                sb.AppendFormat("        <Description>    {0}    </Description>{1}", Description, Environment.NewLine);
            }

            sb.Append(PatcherLib.Utilities.Utilities.CreatePatchXML(innerList, context));

            foreach (VariableType variable in Variables)
            {
                int value = variable.ByteArray.ToIntLE();
                System.Text.StringBuilder sbSpecific = new System.Text.StringBuilder();
                int patchCount = variable.Content.Count;

                for (int index = 0; index < patchCount; index++)
                {
                    PatchedByteArray patchedByteArray = variable.Content[index];
                    //string file = Enum.GetName(typeof(PatcherLib.Iso.PsxIso.Sectors), patchedByteArray.Sector);
                    //string file = PatcherLib.Iso.PsxIso.GetSectorName(patchedByteArray.Sector);

                    /*
                     * Type sectorType = (mode == FreeSpaceMode.PSP) ? typeof(PatcherLib.Iso.PspIso.Sectors) : typeof(PatcherLib.Iso.PsxIso.Sectors);
                     * Enum sector = (Enum)Enum.ToObject(sectorType, patchedByteArray.Sector);
                     * string file = (mode == FreeSpaceMode.PSP)
                     *  ? PatcherLib.Iso.PspIso.GetSectorName((PatcherLib.Iso.PspIso.Sectors)sector)
                     *  : PatcherLib.Iso.PsxIso.GetSectorName((PatcherLib.Iso.PsxIso.Sectors)sector);
                     */

                    string file = ISOHelper.GetSectorName(patchedByteArray.SectorEnum);
                    sbSpecific.AppendFormat("{0}:{1}{2}", file, patchedByteArray.Offset.ToString("X"), ((index < (patchCount - 1)) ? "," : ""));
                }

                string strVariableReference = "";
                if (variable.IsReference)
                {
                    strVariableReference = String.Format(" reference=\"{0}\" operator=\"{1}\" operand=\"{2}\"", variable.Reference.Name, variable.Reference.Operand, variable.Reference.OperatorSymbol);
                }

                string strDefault = variable.IsReference ? "" : String.Format(" default=\"{0}\"", value.ToString("X"));

                string strSpecific = sbSpecific.ToString();
                string strContent  = string.IsNullOrEmpty(strSpecific) ? "symbol=\"true\"" : String.Format("specific=\"{0}\"", strSpecific);

                sb.AppendFormat("        <Variable name=\"{0}\" {1} bytes=\"{2}\"{3}{4} />{5}",
                                variable.Name, strContent, variable.NumBytes, strDefault, strVariableReference, Environment.NewLine);
            }

            sb.AppendLine("    </Patch>");
            return(sb.ToString());
        }
Beispiel #6
0
        public static bool IsContainedWithinFreeSpace(PatchRange range, FreeSpaceMode mode)
        {
            foreach (PatchRange freeSpaceRange in GetRanges(mode))
            {
                if (range.IsContainedWithin(freeSpaceRange))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #7
0
        public static bool HasFreeSpaceOverlap(PatchRange range, FreeSpaceMode mode)
        {
            foreach (PatchRange freeSpaceRange in GetRanges(mode))
            {
                if (range.HasOverlap(freeSpaceRange))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #8
0
        public static string CreatePatchXML(IEnumerable <AsmPatch> patches, FreeSpaceMode mode)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            sb.AppendLine("<Patches>");

            foreach (AsmPatch patch in patches)
            {
                sb.Append(patch.CreateXML(mode));
            }

            sb.AppendLine("</Patches>");

            return(sb.ToString());
        }
Beispiel #9
0
        public static FreeSpaceMaps GetFreeSpaceMaps(IEnumerable <AsmPatch> patches, FreeSpaceMode mode)
        {
            Dictionary <PatchedByteArray, AsmPatch>           innerPatchMap      = new Dictionary <PatchedByteArray, AsmPatch>();
            Dictionary <PatchRange, List <PatchedByteArray> > patchRangeMap      = new Dictionary <PatchRange, List <PatchedByteArray> >();
            Dictionary <PatchRange, HashSet <AsmPatch> >      outerPatchRangeMap = new Dictionary <PatchRange, HashSet <AsmPatch> >();

            PatchRange[] ranges = GetRanges(mode);

            foreach (AsmPatch patch in patches)
            {
                List <PatchedByteArray> combinedPatchList = patch.GetCombinedPatchList();

                foreach (PatchedByteArray patchedByteArray in combinedPatchList)
                {
                    if (patchedByteArray is InputFilePatch)
                    {
                        continue;
                    }
                    if (patchedByteArray.SectorEnum.GetType() == typeof(FFTPack.Files))
                    {
                        continue;
                    }

                    if (!innerPatchMap.ContainsKey(patchedByteArray))
                    {
                        innerPatchMap.Add(patchedByteArray, patch);
                    }

                    //long patchedByteArrayEndOffset = patchedByteArray.Offset + patchedByteArray.GetBytes().Length - 1;
                    foreach (PatchRange range in ranges)
                    {
                        //long positionEndOffset = position.StartLocation + position.Length - 1;
                        //if ((((PsxIso.Sectors)patchedByteArray.Sector) == position.Sector) && (patchedByteArrayEndOffset >= position.StartLocation) && (patchedByteArray.Offset <= positionEndOffset))
                        if (range.HasOverlap(patchedByteArray))
                        {
                            if (patchRangeMap.ContainsKey(range))
                            {
                                patchRangeMap[range].Add(patchedByteArray);
                            }
                            else
                            {
                                patchRangeMap.Add(range, new List <PatchedByteArray>()
                                {
                                    patchedByteArray
                                });
                            }

                            if (outerPatchRangeMap.ContainsKey(range))
                            {
                                outerPatchRangeMap[range].Add(patch);
                            }
                            else
                            {
                                outerPatchRangeMap.Add(range, new HashSet <AsmPatch>()
                                {
                                    patch
                                });
                            }
                        }
                    }
                }

                foreach (PatchRange range in ranges)
                {
                    if (patchRangeMap.ContainsKey(range))
                    {
                        patchRangeMap[range].Sort(
                            delegate(PatchedByteArray patchedByteArray1, PatchedByteArray patchedByteArray2)
                        {
                            return(patchedByteArray1.Offset.CompareTo(patchedByteArray2.Offset));
                        }
                            );
                    }
                }
            }

            return(new FreeSpaceMaps
            {
                InnerPatchMap = innerPatchMap,
                PatchRangeMap = patchRangeMap,
                OuterPatchRangeMap = outerPatchRangeMap
            });
        }
Beispiel #10
0
 public static Context GetContext(FreeSpaceMode mode)
 {
     return((mode == FreeSpaceMode.PSP) ? Context.US_PSP : Context.US_PSX);
 }
Beispiel #11
0
 private void Init(List <AsmPatch> patchList, ASMEncoding.ASMEncodingUtility asmUtility, FreeSpaceMode mode)
 {
     this.patchList     = patchList;
     this.asmUtility    = asmUtility;
     this.freeSpaceMaps = FreeSpace.GetFreeSpaceMaps(patchList, mode);
 }
        public static ConflictCheckResult CheckConflicts(IList <AsmPatch> patchList, FreeSpaceMode mode)
        {
            List <AsmPatch> resultPatchList = new List <AsmPatch>();
            Dictionary <AsmPatch, List <PatchRangeConflict> > conflictMap          = new Dictionary <AsmPatch, List <PatchRangeConflict> >();
            Dictionary <AsmPatch, List <PatchedByteArray> >   combinedPatchListMap = new Dictionary <AsmPatch, List <PatchedByteArray> >();

            foreach (AsmPatch patch in patchList)
            {
                combinedPatchListMap[patch] = patch.GetCombinedPatchList();
            }

            for (int patchIndex = 0; patchIndex < patchList.Count; patchIndex++)
            {
                AsmPatch patch = patchList[patchIndex];

                List <PatchedByteArray> combinedPatchList = combinedPatchListMap[patch];
                foreach (PatchedByteArray patchedByteArray in combinedPatchList)
                {
                    if (patchedByteArray is InputFilePatch)
                    {
                        continue;
                    }

                    PatchRange range = new PatchRange(patchedByteArray);
                    for (int conflictPatchIndex = patchIndex + 1; conflictPatchIndex < patchList.Count; conflictPatchIndex++)
                    {
                        AsmPatch conflictPatch = patchList[conflictPatchIndex];

                        List <PatchedByteArray> conflictCombinedPatchList = combinedPatchListMap[conflictPatch];
                        foreach (PatchedByteArray conflictPatchedByteArray in conflictCombinedPatchList)
                        {
                            if (conflictPatchedByteArray is InputFilePatch)
                            {
                                continue;
                            }

                            //if (patchedByteArray.IsPatchEqual(conflictPatchedByteArray))
                            if (!patchedByteArray.HasConflict(conflictPatchedByteArray))
                            {
                                continue;
                            }

                            PatchRange conflictRange = new PatchRange(conflictPatchedByteArray);
                            if (range.HasOverlap(conflictRange))
                            {
                                bool isInFreeSpace = FreeSpace.IsContainedWithinFreeSpace(range, mode);

                                PatchRangeConflict patchConflict        = new PatchRangeConflict(range, conflictPatch, conflictRange, isInFreeSpace);
                                PatchRangeConflict reversePatchConflict = new PatchRangeConflict(conflictRange, patch, range, isInFreeSpace);

                                if (conflictMap.ContainsKey(patch))
                                {
                                    conflictMap[patch].Add(patchConflict);
                                }
                                else
                                {
                                    conflictMap.Add(patch, new List <PatchRangeConflict> {
                                        patchConflict
                                    });
                                }

                                if (conflictMap.ContainsKey(conflictPatch))
                                {
                                    conflictMap[conflictPatch].Add(reversePatchConflict);
                                }
                                else
                                {
                                    conflictMap.Add(conflictPatch, new List <PatchRangeConflict> {
                                        reversePatchConflict
                                    });
                                }
                            }
                        }
                    }
                }

                if (conflictMap.ContainsKey(patch))
                {
                    resultPatchList.Add(patch);
                }
            }

            return(new ConflictCheckResult(resultPatchList, conflictMap));
        }
Beispiel #13
0
        public void MoveBlocks(ASMEncoding.ASMEncodingUtility utility, IEnumerable <MovePatchRange> movePatchRanges)
        {
            /*
             * Dictionary<PatchRange, bool> isSequentialAdd = new Dictionary<PatchRange, bool>();
             * foreach (KeyValuePair<PatchRange, uint> blockMove in blockMoves)
             * {
             *  isSequentialAdd[blockMove.Key] = false;
             * }
             */
            List <ASMEncoding.Helpers.BlockMove> blockMoves = new List <ASMEncoding.Helpers.BlockMove>();

            foreach (MovePatchRange patchRange in movePatchRanges)
            {
                //PatchRange patchRange = movePair.Key;
                //uint fileToRamOffset = PatcherLib.Iso.PsxIso.GetRamOffset(patchRange.Sector, true);
                FreeSpaceMode mode            = FreeSpace.GetMode(utility);
                Type          sectorType      = (mode == FreeSpaceMode.PSP) ? typeof(PatcherLib.Iso.PspIso.Sectors) : typeof(PatcherLib.Iso.PsxIso.Sectors);
                Enum          sector          = (Enum)Enum.ToObject(sectorType, patchRange.Sector);
                uint          fileToRamOffset = (mode == FreeSpaceMode.PSP)
                    ? PatcherLib.Iso.PspIso.GetRamOffsetUnsigned((PatcherLib.Iso.PspIso.Sectors)sector)
                    : PatcherLib.Iso.PsxIso.GetRamOffset((PatcherLib.Iso.PsxIso.Sectors)sector, true);

                ASMEncoding.Helpers.BlockMove blockMove = new ASMEncoding.Helpers.BlockMove();
                blockMove.Location    = (uint)patchRange.StartOffset + fileToRamOffset;
                blockMove.EndLocation = (uint)patchRange.EndOffset + fileToRamOffset;
                blockMove.Offset      = patchRange.MoveOffset;
                blockMoves.Add(blockMove);
            }

            List <PatchedByteArray> allPatchList = GetAllPatches();

            //foreach (PatchedByteArray patchedByteArray in innerList)
            foreach (PatchedByteArray patchedByteArray in allPatchList)
            {
                if (patchedByteArray is InputFilePatch)
                {
                    continue;
                }

                byte[] bytes = patchedByteArray.GetBytes();

                foreach (MovePatchRange patchRange in movePatchRanges)
                {
                    //PatchRange patchRange = movePair.Key;
                    //uint offset = movePair.Value;

                    //if ((patchedByteArray.RamOffset == blockMove.Location) && ((patchedByteArray.RamOffset + bytes.Length) == blockMove.EndLocation))
                    //if (patchedByteArray.RamOffset == blockMove.Location)
                    //PatchRange patchRange = new PatchRange(patchedByteArray);
                    //PatchRange patchRange2 = new PatchRange(
                    //if (
                    //if (blockMove.Key
                    if (patchRange.HasOverlap(patchedByteArray))
                    {
                        patchedByteArray.Offset    += patchRange.MoveOffset; // offset;
                        patchedByteArray.RamOffset += patchRange.MoveOffset; // offset;
                        //isSequentialAdd[patchRange] = true;
                    }

                    /*
                     * else if ((isSequentialAdd[patchRange]) && (patchedByteArray.IsSequentialOffset))
                     * {
                     *  patchedByteArray.Offset += offset;
                     *  patchedByteArray.RamOffset += offset;
                     *  //patchRange.EndOffset += (uint)bytes.Length;
                     * }
                     * else
                     * {
                     *  isSequentialAdd[patchRange] = false;
                     * }
                     */
                }

                if ((patchedByteArray.IsCheckedAsm) && (!patchedByteArray.IsMoveSimple))
                {
                    byte[] newBytes = utility.UpdateBlockReferences(bytes, (uint)patchedByteArray.RamOffset, true, blockMoves);
                    patchedByteArray.SetBytes(newBytes);
                }
            }

            foreach (ASMEncoding.Helpers.BlockMove blockMove in blockMoves)
            {
                bool isAlreadyPresent = false;
                foreach (ASMEncoding.Helpers.BlockMove listBlockMove in blockMoveList)
                {
                    if (blockMove.IsEqual(listBlockMove))
                    {
                        isAlreadyPresent = true;
                        break;
                    }
                }

                if (!isAlreadyPresent)
                {
                    blockMoveList.Add(blockMove);
                }
            }
        }
        public static ConflictResolveResult ResolveConflicts(IList <AsmPatch> patchList, ASMEncoding.ASMEncodingUtility asmUtility, int maxConflictResolveAttempts = MaxConflictResolveAttempts)
        {
            List <AsmPatch>            resultPatchList = new List <AsmPatch>();
            Dictionary <AsmPatch, int> patchIndexMap   = new Dictionary <AsmPatch, int>();
            StringBuilder sbMessage    = new StringBuilder();
            bool          hasConflicts = false;

            for (int index = 0; index < patchList.Count; index++)
            {
                resultPatchList.Add(patchList[index]);
                patchIndexMap.Add(patchList[index], index);
            }

            Context       context       = (asmUtility.EncodingMode == ASMEncoding.ASMEncodingMode.PSP) ? Context.US_PSP : Context.US_PSX;
            FreeSpaceMode mode          = FreeSpace.GetMode(context);
            FreeSpaceMaps freeSpaceMaps = FreeSpace.GetFreeSpaceMaps(resultPatchList, mode);

            foreach (PatchRange freeSpaceRange in freeSpaceMaps.PatchRangeMap.Keys)
            {
                List <PatchedByteArray> innerPatches  = freeSpaceMaps.PatchRangeMap[freeSpaceRange];
                FreeSpaceAnalyzeResult  analyzeResult = FreeSpace.Analyze(innerPatches, freeSpaceRange, true);
                int conflictResolveAttempts           = 0;

                /*
                 * Type sectorType = ISOHelper.GetSectorType(context);
                 * Enum sector = (Enum)Enum.ToObject(sectorType, freeSpaceRange.Sector);
                 * string strSector = (mode == FreeSpaceMode.PSP) ? PspIso.GetSectorName((PspIso.Sectors)sector) : PsxIso.GetSectorName((PsxIso.Sectors)sector);
                 */

                string strSector = ISOHelper.GetSectorName(freeSpaceRange.Sector, context);

                while ((analyzeResult.HasConflicts) && (conflictResolveAttempts < maxConflictResolveAttempts))
                {
                    bool isStatic   = false;
                    bool stayStatic = false;

                    int endIndex = innerPatches.Count - 1;
                    for (int index = 0; index < endIndex; index++)
                    {
                        PatchedByteArray innerPatch = innerPatches[index];
                        isStatic   = innerPatch.IsStatic || stayStatic;
                        stayStatic = (innerPatch.IsStatic) && (innerPatch.IsPatchEqual(innerPatches[index + 1]));

                        if ((analyzeResult.ConflictIndexes.Contains(index)) && (!isStatic))
                        {
                            long           moveOffset     = analyzeResult.LargestGapOffset - innerPatch.Offset;
                            MovePatchRange movePatchRange = new MovePatchRange(new PatchRange(innerPatch), moveOffset);

                            AsmPatch asmPatch         = freeSpaceMaps.InnerPatchMap[innerPatch];
                            int      resultPatchIndex = patchIndexMap[asmPatch];
                            patchIndexMap.Remove(asmPatch);

                            asmPatch = asmPatch.Copy();
                            resultPatchList[resultPatchIndex] = asmPatch;
                            patchIndexMap.Add(asmPatch, resultPatchIndex);
                            asmPatch.MoveBlock(asmUtility, movePatchRange);
                            asmPatch.Update(asmUtility);

                            sbMessage.AppendLine("Conflict resolved by moving segment of patch \"" + asmPatch.Name + "\" in sector " + strSector + " from offset "
                                                 + innerPatch.Offset.ToString("X") + " to " + analyzeResult.LargestGapOffset.ToString("X") + ".");

                            freeSpaceMaps = FreeSpace.GetFreeSpaceMaps(resultPatchList, mode);
                            innerPatches  = freeSpaceMaps.PatchRangeMap[freeSpaceRange];
                            analyzeResult = FreeSpace.Analyze(innerPatches, freeSpaceRange, false);
                            conflictResolveAttempts++;
                            break;
                        }
                    }
                }

                if (analyzeResult.HasConflicts)
                {
                    hasConflicts = true;
                    int endIndex = innerPatches.Count - 1;
                    for (int index = 0; index < endIndex; index++)
                    {
                        if (analyzeResult.ConflictIndexes.Contains(index))
                        {
                            sbMessage.Length = 0;
                            sbMessage.AppendLine("Conflict in sector " + strSector + " at offset " + innerPatches[index].Offset.ToString("X") + "!");
                            break;
                        }
                    }
                }
            }

            return(new ConflictResolveResult(resultPatchList, hasConflicts, sbMessage.ToString()));
        }
Beispiel #15
0
 public static string[] GetRangeNames(FreeSpaceMode mode)
 {
     return(RangeNames[(int)mode]);
 }
Beispiel #16
0
 public static PatchRange[] GetRanges(FreeSpaceMode mode)
 {
     return(Ranges[(int)mode]);
 }
Beispiel #17
0
 private void InitFiles(FreeSpaceMode mode)
 {
     this.Filelistbox.Items.Clear();
     this.Filelistbox.Items.AddRange(FreeSpace.GetRangeNames(mode));
 }