BizHawk/BizHawk.Client.Common/XmlGame.cs

120 lines
2.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
2014-05-11 17:48:17 +00:00
using System.Linq;
using System.Xml;
using BizHawk.Common;
2014-07-03 19:20:34 +00:00
using BizHawk.Common.BufferExtensions;
using BizHawk.Common.IOExtensions;
using BizHawk.Emulation.Common;
2013-10-27 07:54:00 +00:00
namespace BizHawk.Client.Common
{
public class XmlGame
{
2014-01-08 03:53:53 +00:00
public XmlDocument Xml { get; set; }
public GameInfo GI = new GameInfo();
public Dictionary<string, byte[]> Assets = new Dictionary<string, byte[]>();
public static XmlGame Create(HawkFile f)
{
try
{
var x = new XmlDocument();
x.Load(f.GetStream());
var y = x.SelectSingleNode("./BizHawk-XMLGame");
if (y == null)
2014-01-08 03:53:53 +00:00
{
return null;
2014-01-08 03:53:53 +00:00
}
var ret = new XmlGame
{
GI =
{
System = y.Attributes["System"].Value,
Name = y.Attributes["Name"].Value,
Status = RomStatus.Unknown
},
Xml = x
};
var n = y.SelectSingleNode("./LoadAssets");
if (n != null)
{
2014-01-08 03:53:53 +00:00
var HashStream = new MemoryStream();
int? OriginalIndex = null;
foreach (XmlNode a in n.ChildNodes)
{
string name = a.Name;
string filename = a.Attributes["FileName"].Value;
byte[] data;
if (filename[0] == '|')
{
// in same archive
var ai = f.FindArchiveMember(filename.Substring(1));
if (ai != null)
{
if (OriginalIndex == null)
2014-01-08 03:53:53 +00:00
{
OriginalIndex = f.GetBoundIndex();
2014-01-08 03:53:53 +00:00
}
2013-04-27 17:24:23 +00:00
f.Unbind();
f.BindArchiveMember(ai);
data = f.GetStream().ReadAllBytes();
}
else
{
throw new Exception("Couldn't load XMLGame LoadAsset \"" + name + "\"");
}
}
else
{
// relative path
2014-05-11 17:48:17 +00:00
var fullpath = Path.GetDirectoryName(f.CanonicalFullPath.Split('|').First()) ?? string.Empty;
fullpath = Path.Combine(fullpath, filename.Split('|').First());
try
{
2014-05-11 17:48:17 +00:00
data = File.ReadAllBytes(fullpath.Split('|').First());
}
catch
{
throw new Exception("Couldn't load XMLGame LoadAsset \"" + name + "\"");
}
}
2014-01-08 03:53:53 +00:00
ret.Assets[name] = data;
using (var sha1 = System.Security.Cryptography.SHA1.Create())
{
sha1.TransformFinalBlock(data, 0, data.Length);
HashStream.Write(sha1.Hash, 0, sha1.Hash.Length);
}
}
2014-01-08 03:53:53 +00:00
2014-07-03 19:20:34 +00:00
ret.GI.Hash = HashStream.GetBuffer().HashSHA1(0, (int)HashStream.Length);
HashStream.Close();
if (OriginalIndex != null)
{
f.Unbind();
f.BindArchiveMember((int)OriginalIndex);
}
}
else
{
ret.GI.Hash = "0000000000000000000000000000000000000000";
}
2014-01-08 03:53:53 +00:00
return ret;
}
2014-01-08 03:53:53 +00:00
catch (Exception ex)
{
2013-10-27 07:54:00 +00:00
throw new InvalidOperationException(ex.ToString());
}
}
}
}