2011-01-11 02:55:51 +00:00
|
|
|
|
using System;
|
2011-01-28 06:46:33 +00:00
|
|
|
|
using System.Collections.Generic;
|
2011-01-29 04:52:50 +00:00
|
|
|
|
using System.Globalization;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
|
|
|
|
|
namespace BizHawk.MultiClient
|
|
|
|
|
{
|
|
|
|
|
public class RomGame : IGame
|
|
|
|
|
{
|
|
|
|
|
public byte[] RomData;
|
2011-02-28 06:16:20 +00:00
|
|
|
|
public byte[] FileData;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
public string System;
|
|
|
|
|
|
|
|
|
|
private string name;
|
2011-01-28 06:46:33 +00:00
|
|
|
|
private List<string> options;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
private const int BankSize = 4096;
|
|
|
|
|
|
2011-03-07 01:07:49 +00:00
|
|
|
|
public RomGame(HawkFile file) : this(file, null){}
|
2011-01-11 02:55:51 +00:00
|
|
|
|
|
2011-03-07 01:07:49 +00:00
|
|
|
|
public RomGame(HawkFile file, string patch)
|
2011-01-11 02:55:51 +00:00
|
|
|
|
{
|
2011-03-07 01:07:49 +00:00
|
|
|
|
if (!file.Exists)
|
|
|
|
|
throw new Exception("The file needs to exist, yo.");
|
|
|
|
|
|
|
|
|
|
var stream = file.GetStream();
|
|
|
|
|
|
|
|
|
|
FileData = Util.ReadAllBytes(stream);
|
|
|
|
|
|
|
|
|
|
int header = (int)(stream.Length % BankSize);
|
|
|
|
|
stream.Position = header;
|
|
|
|
|
int length = (int)stream.Length - header;
|
|
|
|
|
|
|
|
|
|
RomData = new byte[length];
|
|
|
|
|
stream.Read(RomData, 0, length);
|
|
|
|
|
|
|
|
|
|
if (file.Extension == "SMD")
|
|
|
|
|
RomData = DeInterleaveSMD(RomData);
|
|
|
|
|
|
|
|
|
|
var info = Database.GetGameInfo(RomData, file.Name);
|
|
|
|
|
name = info.Name;
|
|
|
|
|
System = info.System;
|
|
|
|
|
options = new List<string>(info.GetOptions());
|
|
|
|
|
CheckForPatchOptions();
|
|
|
|
|
|
|
|
|
|
if (patch != null)
|
|
|
|
|
{
|
|
|
|
|
using (var patchFile = new HawkFile(patch))
|
|
|
|
|
{
|
|
|
|
|
patchFile.BindFirstOf("IPS");
|
2011-03-07 02:44:30 +00:00
|
|
|
|
if(patchFile.IsBound)
|
2011-03-07 01:07:49 +00:00
|
|
|
|
IPS.Patch(RomData, patchFile.GetStream());
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-11 02:55:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-01-28 06:46:33 +00:00
|
|
|
|
public void AddOptions(params string[] options)
|
|
|
|
|
{
|
|
|
|
|
this.options.AddRange(options);
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-11 02:55:51 +00:00
|
|
|
|
private byte[] DeInterleaveSMD(byte[] source)
|
|
|
|
|
{
|
|
|
|
|
// SMD files are interleaved in pages of 16k, with the first 8k containing all
|
|
|
|
|
// odd bytes and the second 8k containing all even bytes.
|
|
|
|
|
|
|
|
|
|
int size = source.Length;
|
|
|
|
|
if (size > 0x400000) size = 0x400000;
|
|
|
|
|
int pages = size / 0x4000;
|
|
|
|
|
byte[] output = new byte[size];
|
|
|
|
|
|
|
|
|
|
for (int page = 0; page < pages; page++)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < 0x2000; i++)
|
|
|
|
|
{
|
|
|
|
|
output[(page * 0x4000) + (i * 2) + 0] = source[(page * 0x4000) + 0x2000 + i];
|
|
|
|
|
output[(page * 0x4000) + (i * 2) + 1] = source[(page * 0x4000) + 0x0000 + i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-29 04:52:50 +00:00
|
|
|
|
private void CheckForPatchOptions()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var opt in options)
|
|
|
|
|
{
|
|
|
|
|
if (opt.StartsWith("PatchBytes"))
|
|
|
|
|
{
|
|
|
|
|
var split1 = opt.Split('=');
|
|
|
|
|
foreach (var val in split1[1].Split(','))
|
|
|
|
|
{
|
|
|
|
|
var split3 = val.Split(':');
|
|
|
|
|
int offset = int.Parse(split3[0], NumberStyles.HexNumber);
|
|
|
|
|
byte value = byte.Parse(split3[1], NumberStyles.HexNumber);
|
|
|
|
|
RomData[offset] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception) { } // No need for errors in patching to propagate.
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-11 02:55:51 +00:00
|
|
|
|
public byte[] GetRomData() { return RomData; }
|
2011-02-28 06:16:20 +00:00
|
|
|
|
public byte[] GetFileData() { return FileData; }
|
2011-01-28 06:46:33 +00:00
|
|
|
|
public IList<string> GetOptions() { return options; }
|
2011-03-07 10:41:46 +00:00
|
|
|
|
public string Name { get { return name; } set { name = value; } }
|
2011-01-11 02:55:51 +00:00
|
|
|
|
|
|
|
|
|
public string SaveRamPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
switch (System)
|
|
|
|
|
{
|
|
|
|
|
case "SMS": return "SMS/SaveRAM/" + Name + ".SaveRAM";
|
|
|
|
|
case "GG": return "Game Gear/SaveRAM/" + Name + ".SaveRAM";
|
2011-01-19 07:06:14 +00:00
|
|
|
|
case "SG": return "SG-1000/SaveRAM/" + Name + ".SaveRAM";
|
|
|
|
|
case "SGX": return "TurboGrafx/SaveRAM/" + Name + ".SaveRAM";
|
2011-01-11 02:55:51 +00:00
|
|
|
|
case "PCE": return "TurboGrafx/SaveRAM/" + Name + ".SaveRAM";
|
|
|
|
|
case "GB": return "Gameboy/SaveRAM/" + Name + ".SaveRAM";
|
|
|
|
|
case "GEN": return "Genesis/SaveRAM/" + Name + ".SaveRAM";
|
|
|
|
|
case "NES": return "NES/SaveRAM/" + Name + ".SaveRAM";
|
2011-04-21 01:41:37 +00:00
|
|
|
|
case "TI83": return "TI83/SaveRAM/" + "TI83" + ".SaveRAM";
|
2011-01-11 02:55:51 +00:00
|
|
|
|
default: return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string SaveStatePrefix
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
switch (System)
|
|
|
|
|
{
|
|
|
|
|
case "SMS": return "SMS/State/" + Name;
|
|
|
|
|
case "GG": return "Game Gear/State/" + Name;
|
2011-01-19 07:06:14 +00:00
|
|
|
|
case "SG": return "SG-1000/State/" + Name;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
case "PCE": return "TurboGrafx/State/" + Name;
|
2011-01-19 07:06:14 +00:00
|
|
|
|
case "SGX": return "TurboGrafx/State/" + Name;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
case "GB": return "Gameboy/State/" + Name;
|
|
|
|
|
case "GEN": return "Genesis/State/" + Name;
|
|
|
|
|
case "NES": return "NES/State/" + Name;
|
2011-04-21 01:41:37 +00:00
|
|
|
|
case "TI83": return "TI83/State/" + "TI83";
|
2011-01-11 02:55:51 +00:00
|
|
|
|
default: return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string MoviePrefix
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
switch (System)
|
|
|
|
|
{
|
|
|
|
|
case "SMS": return "SMS/Movie/" + Name;
|
|
|
|
|
case "GG": return "Game Gear/Movie/" + Name;
|
2011-01-19 07:06:14 +00:00
|
|
|
|
case "SG": return "SG-1000/Movie/" + Name;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
case "PCE": return "TurboGrafx/Movie/" + Name;
|
2011-01-19 07:06:14 +00:00
|
|
|
|
case "SGX": return "TurboGrafx/Movie/" + Name;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
case "GB": return "Gameboy/Movie/" + Name;
|
|
|
|
|
case "GEN": return "Genesis/Movie/" + Name;
|
|
|
|
|
case "NES": return "NES/Movie/" + Name;
|
2011-04-21 01:41:37 +00:00
|
|
|
|
case "TI83": return "TI83/Movie/" + Name;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
default: return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ScreenshotPrefix
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
switch (System)
|
|
|
|
|
{
|
|
|
|
|
case "SMS": return "SMS/Screenshot/" + Name;
|
|
|
|
|
case "GG": return "Game Gear/Screenshot/" + Name;
|
2011-01-19 07:06:14 +00:00
|
|
|
|
case "SG": return "SG-1000/Screenshot/" + Name;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
case "PCE": return "TurboGrafx/Screenshot/" + Name;
|
2011-01-19 07:06:14 +00:00
|
|
|
|
case "SGX": return "TurboGrafx/Screenshot/" + Name;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
case "GB": return "Gameboy/Screenshot/" + Name;
|
|
|
|
|
case "GEN": return "Genesis/Screenshot/" + Name;
|
|
|
|
|
case "NES": return "NES/Screenshot/" + Name;
|
2011-04-21 01:41:37 +00:00
|
|
|
|
case "TI83": return "TI83/Screenshot/" + "TI83";
|
2011-01-11 02:55:51 +00:00
|
|
|
|
default: return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|