diff --git a/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs b/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs index 0ab5f46e73..a5ac8bf1ab 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs @@ -203,7 +203,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo /// /// sets the provided palette as current /// - void SetPalette(int[,] pal) + public void SetPalette(int[,] pal) { Array.Copy(pal,palette,64*3); for(int i=0;i<64*8;i++) diff --git a/BizHawk.Emulation/Consoles/Nintendo/NES/Palettes.cs b/BizHawk.Emulation/Consoles/Nintendo/NES/Palettes.cs index d3ac0de16c..f3a2929872 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/NES/Palettes.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/NES/Palettes.cs @@ -10,7 +10,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo { partial class NES { - static class Palettes + public static class Palettes { static float[] rtmul = { 1.239f, 0.794f, 1.019f, 0.905f, 1.023f, 0.741f, 0.75f }; static float[] gtmul = { 0.915f, 1.086f, 0.98f, 1.026f, 0.908f, 0.987f, 0.75f }; @@ -29,6 +29,23 @@ namespace BizHawk.Emulation.Consoles.Nintendo if (b > 0xFF) b = 0xFF; } + /// + /// Loads a simple 192 byte (64 entry RGB888) palette which is FCEUX format (and probably other emulators as well) + /// + /// 192 bytes, the contents of the palette file + public static int[,] Load_FCEUX_Palette(byte[] fileContents) + { + if (fileContents.Length != 192) return null; + int[,] ret = new int[64, 3]; + int i=0; + for (int c = 0; c < 64; c++) + { + for(int z=0;z<3;z++) + ret[c,z] = fileContents[i++]; + } + return ret; + } + const int SHIFT = 2; public static int[,] FCEUX_Standard = new int[,] { diff --git a/BizHawk.Emulation/Util.cs b/BizHawk.Emulation/Util.cs index b32efd8e8f..71b88cd30e 100644 --- a/BizHawk.Emulation/Util.cs +++ b/BizHawk.Emulation/Util.cs @@ -43,6 +43,39 @@ namespace BizHawk public static class Extensions { + public static void CopyTo(this Stream src, Stream dest) + { + int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000; + byte[] buffer = new byte[size]; + int n; + do + { + n = src.Read(buffer, 0, buffer.Length); + dest.Write(buffer, 0, n); + } while (n != 0); + } + + public static void CopyTo(this MemoryStream src, Stream dest) + { + dest.Write(src.GetBuffer(), (int)src.Position, (int)(src.Length - src.Position)); + } + + public static void CopyTo(this Stream src, MemoryStream dest) + { + if (src.CanSeek) + { + int pos = (int)dest.Position; + int length = (int)(src.Length - src.Position) + pos; + dest.SetLength(length); + + while (pos < length) + pos += src.Read(dest.GetBuffer(), pos, length - pos); + } + else + src.CopyTo((Stream)dest); + } + + public static bool IsBinary(this string str) { for (int i = 0; i < str.Length; i++) diff --git a/BizHawk.MultiClient/HawkFile.cs b/BizHawk.MultiClient/HawkFile.cs index f75522c0b6..50d72dc47d 100644 --- a/BizHawk.MultiClient/HawkFile.cs +++ b/BizHawk.MultiClient/HawkFile.cs @@ -11,6 +11,29 @@ namespace BizHawk.MultiClient public class HawkFile : IDisposable { + public static bool ExistsAt(string path) + { + using (var file = new HawkFile(path)) + { + return file.Exists; + } + } + + public static byte[] ReadAllBytes(string path) + { + using (var file = new HawkFile(path)) + { + if (!file.Exists) throw new FileNotFoundException(path); + using (Stream stream = file.GetStream()) + { + MemoryStream ms = new MemoryStream((int)stream.Length); + stream.CopyTo(ms); + return ms.GetBuffer(); + } + } + } + + /// /// returns whether a bound file exists. if there is no bound file, it can't exist /// @@ -90,7 +113,8 @@ namespace BizHawk.MultiClient public HawkFile(string path) { string autobind = null; - if (IsCanonicalArchivePath(path)) + bool isArchivePath = IsCanonicalArchivePath(path); + if (isArchivePath) { string[] parts = path.Split('|'); path = parts[0]; @@ -114,7 +138,13 @@ namespace BizHawk.MultiClient //bind it later with the desired extensions. } - if (autobind != null) + if (autobind == null) + { + //non-archive files can be automatically bound this way + if (!isArchivePath) + BindRoot(); + } + else { autobind = autobind.ToUpperInvariant(); for (int i = 0; i < extractor.ArchiveFileData.Count; i++) @@ -189,7 +219,7 @@ namespace BizHawk.MultiClient } /// - /// causes the root to be bound (in the case of non-archive files + /// causes the root to be bound (in the case of non-archive files) /// void BindRoot() { diff --git a/BizHawk.MultiClient/MainForm.cs b/BizHawk.MultiClient/MainForm.cs index ef69ac7e82..b8f3f1e991 100644 --- a/BizHawk.MultiClient/MainForm.cs +++ b/BizHawk.MultiClient/MainForm.cs @@ -740,7 +740,15 @@ namespace BizHawk.MultiClient LoadTI83KeyPad(); break; case "NES": - nextEmulator = new NES(); + { + NES nes = new NES(); + nextEmulator = nes; + string palette_file = @"C:\svn\fceux\fceu\output\palettes\FCEU-15-nitsuja_new.pal"; + if (HawkFile.ExistsAt(palette_file)) + { + nes.SetPalette(NES.Palettes.Load_FCEUX_Palette(HawkFile.ReadAllBytes(palette_file))); + } + } break; case "GB": nextEmulator = new Gameboy();