internal bool UpdateSync(NameSpace xNS, long ID, SyncType xType)
 {
     if (xType == SyncType.None)
         return true;
     int idx1 = -1, idx2 = -1;
     for (int i = 0; i < xIndexRecords.Count; i++)
     {
         if (xIndexRecords[i].NS != xNS)
             continue;
         idx1 = i;
         break;
     }
     for (int i = 0; i < xSyncs.Count; i++)
     {
         if (xSyncs[i].NS != xNS)
             continue;
         idx2 = i;
         break;
     }
     if (idx1 == -1 || idx2 == -1)
     {
         RecordEntry xidx = null;
         SyncEntry xsync = null;
         if (idx1 == -1)
         {
             int xsize;
             if (xNS != NameSpace.Achievement)
                 xsize = 0x10;
             else
                 xsize = (0x10 * xAchievements.Count);
             int pos = AllocateData(xsize);
             if (pos == -1)
                 return false;
             xidx = new RecordEntry(new XDBFEntry(xNS, (long)GPDIDs.IndexRecord, pos, xsize, this));
             if (xNS == NameSpace.Achievement)
             {
                 for (int i = 0; i < xAchievements.Count; i++)
                     xidx.xpairs.Add(new SyncPair(xAchievements[i].ID, i + 1));
             }
         }
         if (idx2 == -1)
         {
             int pos = AllocateData(0x18);
             if (pos == -1)
                 return false;
             xsync = new SyncEntry(new XDBFEntry(xNS, (long)GPDIDs.SyncRecord, pos, 0x18, this));
             if (xNS == NameSpace.Achievement)
             {
                 xsync.xLastSync = 0;
                 xsync.xNext = xAchievements.Count + 1;
             }
         }
         if (xidx != null)
         {
             xIndexRecords.Add(xidx);
             xIndexRecords.Sort(new Comparison<RecordEntry>(sortns));
             idx1 = xIndexRecords.IndexOf(xidx);
         }
         if (xsync != null)
         {
             xSyncs.Add(xsync);
             xSyncs.Sort(new Comparison<SyncEntry>(sortns));
             idx2 = xSyncs.IndexOf(xsync);
         }
     }
     long curnext = xSyncs[idx2].Next;
     for (int i = 0; i < xIndexRecords[idx1].xpairs.Count; i++)
     {
         if (xIndexRecords[idx1].xpairs[i].ID == ID)
             xIndexRecords[idx1].xpairs.RemoveAt(i--);
     }
     if (xType == SyncType.Server)
         xIndexRecords[idx1].xpairs.Add(new SyncPair(ID, curnext++));
     else
         xIndexRecords[idx1].xpairs.Add(new SyncPair(ID, 0));
     if (xIndexRecords[idx1].xUpdate())
     {
         xSyncs[idx2].xNext = curnext;
         return xSyncs[idx2].xUpdate();
     }
     return false;
 }
        /* All functions are self explanitory, here's a general structure:
         * Header
         * - Info
         * - XDBF Entries (Data entries)
         * - Free space entries (old unused data that can be reused)
         * Entries
         *
         * To comment everything out in GPD's would be useless and repetitive
         *
         * Achievements, Settings, and TitlesPlayed have syncs associated with them.
         * This is used when updating information to the servers without having to
         * constantly update every single item, it would be a bad system, uneffecient.
         * Each syncable NameSpace has a Sync Entry and an Index Record (I don't know
         * why they wouldn't just combine them, maybe it's a spacial issue).  Sync Entry
         * contains the last synced entry and the next sync ID to be used when needing to
         * assign a sync ID.  To update a sync, you just get the next SyncID, assign it to
         * the respected SyncPair in the IndexRecord, move it to the bottom, and increase
         * the next sync by one.  The Xbox checks it and will sync all ID's that are
         * between the last sync and the next sync.  One trick is you have to make sure you
         * order the entries in the header properly. */
        internal GPD(string GPDLocale, uint xTitleID)
        {
            xActive = true;
            xIO = new DJsIO(GPDLocale, DJFileMode.Open, true);
            if (!xIO.Accessed)
                return;
            TitleID = xTitleID;
            xIO.IsBigEndian = true;
            xIO.Position = 0;
            if (xIO.ReadUInt32() != 0x58444246)
                throw GPDExcepts.IsntXDBF;
            xIO.Position += 4; // Version
            xEntryMax = xIO.ReadInt32();
            xEntryCurrent = xIO.ReadInt32();
            xFreeMax = xIO.ReadInt32();
            xFreeCurrent = xIO.ReadInt32();
            List<XDBFEntry> xEntries = new List<XDBFEntry>();
            try
            {
                for (int i = 0; i < xEntryCurrent; i++)
                    xEntries.Add(new XDBFEntry(this));
                xIO.Position = (0x18 + (xEntryMax * 0x12));
                for (int i = 0; i < xFreeCurrent - 1; i++)
                {
                    FreeSpaceEntry x = new FreeSpaceEntry(this);
                    xFreeEnts.Add(x);
                }
                PatchFree();
                for (int i = 0; i < xEntries.Count; i++)
                {
                    XDBFEntry x = xEntries[i];
                    switch (x.ID)
                    {
                        case (long)GPDIDs.IndexRecord:
                            {
                                RecordEntry xThisRec = new RecordEntry(x);
                                xIndexRecords.Add(xThisRec);
                                if (xThisRec.NS == NameSpace.Achievement && xAchievements.Count == 0)
                                    xIsInErase = true;
                            }
                            break;
                        case (long)GPDIDs.SyncRecord:
                            {
                                SyncEntry xThisSync = new SyncEntry(x);
                                xSyncs.Add(xThisSync);
                            }
                            break;
                        default:
                            {
                                switch (x.NS)
                                {
                                    case NameSpace.Nothing:
                                        xEntries.RemoveAt(i--);
                                        break;

                                    case NameSpace.Achievement:
                                        {
                                            AchievementEntry xCurrentAchievment = new AchievementEntry(x);
                                            xAchievements.Add(xCurrentAchievment);
                                        }
                                        break;

                                    case NameSpace.Image:
                                        {
                                            if (!ContainsEntry(x))
                                            {
                                                ImageEntry xCurrentImage = new ImageEntry(x);
                                                xImages.Add(xCurrentImage);
                                            }
                                        }
                                        break;

                                    case NameSpace.Setting:
                                        {
                                            if (!ContainsEntry(x))
                                            {
                                                Setting xThisSetting = new Setting(x);
                                                xUserSettings.Add(xThisSetting);
                                            }
                                        }
                                        break;

                                    case NameSpace.Title:
                                        {
                                            if (!ContainsEntry(x))
                                            {
                                                TitlePlayedEntry xTitle = new TitlePlayedEntry(x);
                                                xTitlesPlayed.Add(xTitle);
                                            }
                                        }
                                        break;

                                    case NameSpace.String:
                                        {
                                            if (!ContainsEntry(x))
                                            {
                                                StringEntry x_String = new StringEntry(x);
                                                xStrings.Add(x_String);
                                            }
                                        }
                                        break;

                                    default:
                                        xEntries.RemoveAt(i--);
                                        xEntryCurrent--;
                                        break;
                                }
                            }
                            break;
                    }
                }
                for (int i = 0; i < xUserSettings.Count; i++)
                {
                    if (!xUserSettings[i].LoadDetails())
                    {
                        OtherEntry xUnknown = new OtherEntry(xUserSettings[i]);
                        xUnknownData.Add(xUnknown);
                        xUserSettings.RemoveAt(i--);
                    }
                }
                for (int i = 0; i < xUnknownData.Count; i++)
                {
                    if (!xUnknownData[i].LoadData())
                    {
                        xUnknownData.RemoveAt(i--);
                        xEntryCurrent--;
                    }
                }
                for (int i = 0; i < xImages.Count; i++)
                {
                    if (!xImages[i].LoadImage())
                    {
                        xImages.RemoveAt(i--);
                        xEntryCurrent--;
                    }
                }
                for (int i = 0; i < xStrings.Count; i++)
                {
                    if (!xStrings[i].LoadString())
                    {
                        xStrings.RemoveAt(i--);
                        xEntryCurrent--;
                    }
                }
                for (int i = 0; i < xIndexRecords.Count; i++)
                {
                    if (!xIndexRecords[i].xLoadDetails())
                    {
                        xIndexRecords.RemoveAt(i--);
                        xEntryCurrent--;
                    }
                }
                for (int i = 0; i < xSyncs.Count; i++)
                {
                    if (!xSyncs[i].LoadSyncs())
                    {
                        xSyncs.RemoveAt(i--);
                        xEntryCurrent--;
                    }
                }
                for (int i = 0; i < xFreeEnts.Count; i++)
                {
                    if (xFreeEnts[i].Size == 0)
                    {
                        xFreeEnts.RemoveAt(i--);
                        xEntryCurrent--;
                    }
                }
                xUserSettings.Sort(sortbyid);
                xAchievements.Sort(sortbyid);
            }
            catch (Exception x) { xIO = null; throw x; }
        }