Example #1
0
        private void ShowConflicts(AsmPatch patch)
        {
            List <PatchRangeConflict> conflictList = conflictPatchData.ConflictMap[patch];

            foreach (PatchRangeConflict conflict in conflictList)
            {
                //PsxIso.Sectors sector = (PsxIso.Sectors)(conflict.ConflictRange.Sector);
                //string strSector = Enum.GetName(typeof(PsxIso.Sectors), sector);
                //string strSector = PatcherLib.Iso.PsxIso.GetSectorName(sector);

                /*
                 * Type sectorType = (mode == FreeSpaceMode.PSP) ? typeof(PspIso.Sectors) : typeof(PsxIso.Sectors);
                 * Enum sector = (Enum)Enum.ToObject(sectorType, conflict.ConflictRange.Sector);
                 * string strSector = (mode == FreeSpaceMode.PSP) ? PspIso.GetSectorName((PspIso.Sectors)sector) : PsxIso.GetSectorName((PsxIso.Sectors)sector);
                 */

                string strSector = ISOHelper.GetSectorName(conflict.ConflictRange.Sector, FreeSpace.GetContext(mode));

                // Patch #, Sector, Location, Conflict Location
                ListViewItem item = new ListViewItem();
                item.Text = conflict.ConflictPatchNumber.ToString();
                item.SubItems.Add(strSector);
                item.SubItems.Add(conflict.Range.StartOffset.ToString("X"));
                item.SubItems.Add(conflict.ConflictRange.StartOffset.ToString("X"));
                item.BackColor = conflict.IsInFreeSpace ? backColor_Conflict_FreeSpace : backColor_Conflict_NonFreeSpace;
                lv_Conflicts.Items.Add(item);
            }
        }
Example #2
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());
        }
Example #3
0
        public static string CreatePatchXML(IEnumerable <PatcherLib.Datatypes.PatchedByteArray> patchedByteArrays, Datatypes.Context context,
                                            bool includePatchTag = false, bool includeRootTag = false, string name = null, string description = null)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

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

            if ((includePatchTag) && (!string.IsNullOrEmpty(name)))
            {
                sb.AppendFormat("    <Patch name=\"{0}\">{1}", name, Environment.NewLine);

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

            foreach (PatcherLib.Datatypes.PatchedByteArray patchedByteArray in patchedByteArrays)
            {
                byte[] bytes     = patchedByteArray.GetBytes();
                int    byteCount = bytes.Length;

                if (byteCount > 0)
                {
                    List <uint> fourByteSets = new List <uint>(GetUintArrayFromBytes(bytes, false));

                    //string file = PatcherLib.Iso.PsxIso.GetSectorName(patchedByteArray.Sector);

                    /*
                     * Type sectorType = (context == Datatypes.Context.US_PSP) ? typeof(PatcherLib.Iso.PspIso.Sectors) : typeof(PatcherLib.Iso.PsxIso.Sectors);
                     * Enum sector = (Enum)Enum.ToObject(sectorType, patchedByteArray.Sector);
                     * string file = (context == Datatypes.Context.US_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);

                    sb.AppendFormat("        <Location file=\"{0}\"{1}{2}>{3}", file,
                                    (patchedByteArray.IsSequentialOffset ? "" : String.Format(" offset=\"{0}\"", patchedByteArray.Offset.ToString("X"))),
                                    (patchedByteArray.MarkedAsData ? " mode=\"DATA\"" : ""), Environment.NewLine);

                    foreach (uint fourByteSet in fourByteSets)
                    {
                        sb.AppendFormat("            {0}{1}", fourByteSet.ToString("X8"), Environment.NewLine);
                    }

                    int remainingBytes = byteCount % 4;
                    if (remainingBytes > 0)
                    {
                        int remainingBytesIndex = byteCount - remainingBytes;
                        System.Text.StringBuilder sbRemainingBytes = new System.Text.StringBuilder(remainingBytes * 2);
                        for (int index = remainingBytesIndex; index < byteCount; index++)
                        {
                            sbRemainingBytes.Append(bytes[index].ToString("X2"));
                        }
                        sb.AppendFormat("            {0}{1}", sbRemainingBytes.ToString(), Environment.NewLine);
                    }

                    sb.AppendLine("        </Location>");
                }
            }

            if (includePatchTag)
            {
                sb.AppendLine("    </Patch>");
            }
            if (includeRootTag)
            {
                sb.AppendLine("</Patches>");
            }

            return(sb.ToString());
        }
        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()));
        }