Ejemplo n.º 1
0
 /// <summary>Constructor.</summary>
 internal GameLibrary()
 {
     if (File.Exists(libraryFileName))
     {
         XmlDocument xml = new XmlDocument();
         using (Stream stream = File.OpenRead(libraryFileName)) {
             xml.Load(stream);
         }
         XmlElement rootNode = xml.DocumentElement;
         foreach (XmlElement gameBoxNode in rootNode.SelectNodes("game-box"))
         {
             if (gameBoxNode.SelectSingleNode("name") != null)
             {
                 // old format
                 XmlNode          descriptionNode = gameBoxNode.SelectSingleNode("description");
                 XmlNode          copyrightNode   = gameBoxNode.SelectSingleNode("copyright");
                 XmlNode          imageFileNode   = gameBoxNode.SelectSingleNode("image-file");
                 GameBoxReference reference       = new GameBoxReference(
                     gameBoxNode.SelectSingleNode("name").InnerText,
                     (descriptionNode != null ? descriptionNode.InnerText : null),
                     (copyrightNode != null ? copyrightNode.InnerText : null),
                     gameBoxNode.SelectSingleNode("file").InnerText,
                     Convert.FromBase64String(gameBoxNode.SelectSingleNode("hash").InnerText),
                     null);
                 string name = reference.Name.ToUpper();
                 List <GameBoxReference> referenceList;
                 if (!gameBoxes.TryGetValue(name, out referenceList))
                 {
                     referenceList = new List <GameBoxReference>(1);
                     gameBoxes.Add(name, referenceList);
                 }
                 referenceList.Add(reference);
             }
             else
             {
                 // new format
                 GameBoxReference reference = new GameBoxReference(
                     gameBoxNode.GetAttribute("name"),
                     gameBoxNode.GetAttribute("description"),
                     gameBoxNode.GetAttribute("copyright"),
                     gameBoxNode.GetAttribute("file"),
                     Convert.FromBase64String(gameBoxNode.GetAttribute("hash")),
                     (gameBoxNode.HasAttribute("icon") ? Convert.FromBase64String(gameBoxNode.GetAttribute("icon")) : null));
                 string name = reference.Name.ToUpper();
                 List <GameBoxReference> referenceList;
                 if (!gameBoxes.TryGetValue(name, out referenceList))
                 {
                     referenceList = new List <GameBoxReference>(1);
                     gameBoxes.Add(name, referenceList);
                 }
                 referenceList.Add(reference);
             }
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>Removes a game box from this library without updating the file.</summary>
        /// <param name="gameBoxReference">A game box reference.</param>
        private void removeReference(IGameBoxReference gameBoxReference)
        {
            List <GameBoxReference> referenceList;

            if (gameBoxes.TryGetValue(gameBoxReference.Name.ToUpper(), out referenceList))
            {
                for (int i = 0; i < referenceList.Count; ++i)
                {
                    if (GameBoxReference.HashAreIdentical(referenceList[i].Hash, gameBoxReference.Hash))
                    {
                        referenceList.RemoveAt(i);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>Adds a game box reference to this library.</summary>
 /// <param name="gameBoxReference">A game box reference.</param>
 public void AddReference(IGameBoxReference gameBoxReference)
 {
     Debug.Assert(gameBoxReference.Name != null);
     if (gameBoxReference != defaultGameBox)
     {
         string name = gameBoxReference.Name.ToUpper();
         List <GameBoxReference> referenceList;
         if (!gameBoxes.TryGetValue(name, out referenceList))
         {
             referenceList = new List <GameBoxReference>(1);
             gameBoxes.Add(name, referenceList);
         }
         else
         {
             foreach (GameBoxReference reference in referenceList)
             {
                 if (GameBoxReference.HashAreIdentical(reference.Hash, gameBoxReference.Hash))
                 {
                     // update file path?
                     if (reference.FileName != gameBoxReference.FileName)
                     {
                         removeReference(reference);
                         break;
                     }
                     else
                     {
                         return;
                     }
                 }
             }
         }
         // remove other references pointing to the same file path (at most one normally)
         foreach (IGameBoxReference otherReference in GameBoxes)
         {
             if (otherReference.FileName == gameBoxReference.FileName)
             {
                 removeReference(otherReference);
                 break;
             }
         }
         referenceList.Add((GameBoxReference)gameBoxReference);
         updateLibraryFile();
     }
 }
Ejemplo n.º 4
0
 /// <summary>Looks for a game box file in the library.</summary>
 /// <param name="hash">Hash value of the game box.</param>
 /// <returns>A game box reference, or null if not found.</returns>
 public IGameBoxReference FindGameBox(byte[] hash)
 {
     Debug.Assert(hash != null);
     if (GameBoxReference.HashAreIdentical(hash, defaultGameBox.Hash))
     {
         return(defaultGameBox);
     }
     else
     {
         foreach (List <GameBoxReference> referenceList in gameBoxes.Values)
         {
             foreach (GameBoxReference reference in referenceList)
             {
                 if (GameBoxReference.HashAreIdentical(hash, reference.Hash))
                 {
                     return(reference);
                 }
             }
         }
         return(null);
     }
 }