Skip to content

Commit

Permalink
Added GameGenie for SNES but it glitchy and commented out
Browse files Browse the repository at this point in the history
  • Loading branch information
ClusterM committed Oct 10, 2017
1 parent d54bdf4 commit c69fe81
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 95 deletions.
2 changes: 0 additions & 2 deletions Apps/ISupportsGameGenie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace com.clusterrr.hakchi_gui
{
interface ISupportsGameGenie
{
string GameGeniePath { get; }
string GameGenie { get; set; }
void ApplyGameGenie();
}
}
65 changes: 18 additions & 47 deletions Apps/NesGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ namespace com.clusterrr.hakchi_gui
public class NesGame : NesMiniApplication, ICloverAutofill, ISupportsGameGenie
{
public const char Prefix = 'H';
public string GameGeniePath { private set; get; }
public static bool? IgnoreMapper;
const string DefaultArgs = "--guest-overscan-dimensions 0,0,9,3 --initial-fadein-durations 10,2 --volume 75 --enable-armet";
private static Dictionary<uint, CachedGameInfo> gameInfoCache = null;

public const string GameGenieFileName = "gamegenie.txt";
private static byte[] supportedMappers = new byte[] { 0, 1, 2, 3, 4, 5, 7, 9, 10, 86, 87, 184 };

public override string GoogleSuffix
Expand All @@ -32,23 +30,9 @@ public override string GoogleSuffix
}
}

private string gameGenie = "";
public string GameGenie
{
get { return gameGenie; }
set
{
if (gameGenie != value) hasUnsavedChanges = true;
gameGenie = value;
}
}

public NesGame(string path, bool ignoreEmptyConfig = false)
: base(path, ignoreEmptyConfig)
{
GameGeniePath = System.IO.Path.Combine(path, GameGenieFileName);
if (File.Exists(GameGeniePath))
gameGenie = File.ReadAllText(GameGeniePath);
}

public static bool Patch(string inputFileName, ref byte[] rawRomData, ref char prefix, ref string application, ref string outputFileName, ref string args, ref Image cover, ref byte saveCount, ref uint crc32)
Expand Down Expand Up @@ -149,37 +133,6 @@ public bool TryAutofill(uint crc32)
return false;
}

public override bool Save()
{
var old = hasUnsavedChanges;
if (hasUnsavedChanges)
{
if (!string.IsNullOrEmpty(gameGenie))
File.WriteAllText(GameGeniePath, gameGenie);
else
File.Delete(GameGeniePath);
}
return base.Save() || old;
}

public void ApplyGameGenie()
{
if (!string.IsNullOrEmpty(GameGenie))
{
var codes = GameGenie.Split(new char[] { ',', '\t', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
var nesFiles = Directory.GetFiles(this.GamePath, "*.nes", SearchOption.TopDirectoryOnly);
foreach (var f in nesFiles)
{
var nesFile = new NesFile(f);
foreach (var code in codes)
{
nesFile.PRG = GameGeniePatcher.Patch(nesFile.PRG, code.Trim());
}
nesFile.Save(f);
}
}
}

private struct CachedGameInfo
{
public string Name;
Expand Down Expand Up @@ -232,6 +185,24 @@ public static void LoadCache()
Debug.WriteLine(ex.Message + ex.StackTrace);
}
}

public void ApplyGameGenie()
{
if (!string.IsNullOrEmpty(GameGenie))
{
var codes = GameGenie.Split(new char[] { ',', '\t', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
var nesFiles = Directory.GetFiles(this.GamePath, "*.nes", SearchOption.TopDirectoryOnly);
foreach (var f in nesFiles)
{
var nesFile = new NesFile(f);
foreach (var code in codes)
{
nesFile.PRG = GameGeniePatcherNes.Patch(nesFile.PRG, code.Trim());
}
nesFile.Save(f);
}
}
}
}
}

25 changes: 25 additions & 0 deletions Apps/NesMiniApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public virtual string GoogleSuffix
get { return "game"; }
}


public const string GameGenieFileName = "gamegenie.txt";
public string GameGeniePath { private set; get; }
private string gameGenie = "";
public string GameGenie
{
get { return gameGenie; }
set
{
if (gameGenie != value) hasUnsavedChanges = true;
gameGenie = value;
}
}

public readonly string GamePath;
public readonly string ConfigPath;
public readonly string IconPath;
Expand Down Expand Up @@ -324,6 +338,11 @@ protected NesMiniApplication(string path, bool ignoreEmptyConfig = false)
break;
}
}

GameGeniePath = Path.Combine(path, GameGenieFileName);
if (File.Exists(GameGeniePath))
gameGenie = File.ReadAllText(GameGeniePath);

hasUnsavedChanges = false;
}

Expand All @@ -350,6 +369,12 @@ public virtual bool Save()
$"SortRawTitle={(Name ?? Code).ToLower()}\n" +
$"SortRawPublisher={(Publisher ?? DefaultPublisher).ToUpper()}\n" +
$"Copyright=hakchi2 ©2017 Alexey 'Cluster' Avdyukhin\n");

if (!string.IsNullOrEmpty(gameGenie))
File.WriteAllText(GameGeniePath, gameGenie);
else if (File.Exists(GameGeniePath))
File.Delete(GameGeniePath);

hasUnsavedChanges = false;
return true;
}
Expand Down
43 changes: 40 additions & 3 deletions Apps/SnesGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace com.clusterrr.hakchi_gui
{
public class SnesGame : NesMiniApplication, ICloverAutofill
public class SnesGame : NesMiniApplication, ICloverAutofill /*, ISupportsGameGenie*/
{
public enum SnesRomType { LoRom = 0x14, HiRom = 0x15 };

Expand Down Expand Up @@ -213,7 +213,7 @@ public static bool Patch(string inputFileName, ref byte[] rawRomData, ref char p
return true;
}

private static SnesRomHeader GetCorrectHeader(byte[] rawRomData, out SnesRomType romType, out string gameTitle)
public static SnesRomHeader GetCorrectHeader(byte[] rawRomData, out SnesRomType romType, out string gameTitle)
{
var romHeaderLoRom = SnesRomHeader.Read(rawRomData, 0x7FC0);
var romHeaderHiRom = SnesRomHeader.Read(rawRomData, 0xFFC0);
Expand Down Expand Up @@ -382,7 +382,7 @@ public void WriteSfromHeader2(SfromHeader2 sfromHeader2)


[StructLayout(LayoutKind.Sequential)]
private struct SnesRomHeader
public struct SnesRomHeader
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 21)]
public byte[] GameTitleArr;
Expand Down Expand Up @@ -712,6 +712,43 @@ public bool TryAutofill(uint crc32)
}
return false;
}

public void ApplyGameGenie()
{
if (!string.IsNullOrEmpty(GameGenie))
{
var codes = GameGenie.Split(new char[] { ',', '\t', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
var nesFiles = Directory.GetFiles(this.GamePath, "*.*", SearchOption.TopDirectoryOnly);
foreach (var f in nesFiles)
{
byte[] data;
var ext = Path.GetExtension(f).ToLower();
int offset;
if (ext == ".sfrom")
{
data = File.ReadAllBytes(f);
offset = 48;
} else if (ext == ".sfc" || ext == ".smc")
{
data = File.ReadAllBytes(f);
if ((data.Length % 1024) != 0)
offset = 512;
else
offset = 0;
}
else continue;

var rawData = new byte[data.Length - offset];
Array.Copy(data, offset, rawData, 0, rawData.Length);

foreach (var code in codes)
rawData = GameGeniePatcherSnes.Patch(rawData, code);

Array.Copy(rawData, 0, data, offset, rawData.Length);
File.WriteAllBytes(f, data);
}
}
}
}
}

2 changes: 1 addition & 1 deletion ConfigIni.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ public static void Save()
public static Dictionary<string, string> GetConfigDictionary()
{
var config = new Dictionary<string, string>();
config["clovercon_home_combination"] = ConfigIni.ResetHack ? string.Format("0x{0:X4}", ConfigIni.ResetCombination) : "0xFFFF";
config["clovercon_home_combination"] = ConfigIni.ResetHack ? string.Format("0x{0:X4}", ConfigIni.ResetCombination) : "0x7FFF";
config["clovercon_autofire"] = ConfigIni.AutofireHack ? "1" : "0";
config["clovercon_autofire_xy"] = ConfigIni.AutofireXYHack && (ConfigIni.ConsoleType == MainForm.ConsoleType.NES || ConfigIni.ConsoleType == MainForm.ConsoleType.Famicom) ? "1" : "0";
config["clovercon_fc_start"] = ConfigIni.FcStart && (ConfigIni.ConsoleType == MainForm.ConsoleType.Famicom) ? "1" : "0";
Expand Down
2 changes: 1 addition & 1 deletion GameGenieDataBase.cs → GameGenie/GameGenieDataBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void ImportCodes(string AFileName, bool AQuiet = false)
NesFile lGame = new NesFile(lGameFileName);
try
{
lGame.PRG = GameGeniePatcher.Patch(lGame.PRG, lCurCode["genie"].InnerText);
lGame.PRG = GameGeniePatcherNes.Patch(lGame.PRG, lCurCode["genie"].InnerText);

lCodeNode = FXml.CreateElement("gamegenie");
GameNode.AppendChild(lCodeNode);
Expand Down
17 changes: 17 additions & 0 deletions GameGenie/GameGenieFormatException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.clusterrr.hakchi_gui
{
public class GameGenieFormatException : Exception
{
public readonly string Code;
public GameGenieFormatException(string code)
: base(string.Format("Invalid code \"{0}\"", code))
{
Code = code;
}
}
}
17 changes: 17 additions & 0 deletions GameGenie/GameGenieNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.clusterrr.hakchi_gui
{
public class GameGenieNotFoundException : Exception
{
public readonly string Code;
public GameGenieNotFoundException(string code)
: base(string.Format("Invalid code \"{0}\"", code))
{
Code = code;
}
}
}
30 changes: 5 additions & 25 deletions GameGeniePatcher.cs → GameGenie/GameGeniePatcherNes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Collections.Generic;
using System.Text;

namespace com.clusterrr.Famicom
namespace com.clusterrr.hakchi_gui
{
public static class GameGeniePatcher
public static class GameGeniePatcherNes
{
public static byte[] Patch(byte[] data, string code)
{
Expand All @@ -17,7 +17,7 @@ public static byte[] Patch(byte[] data, string code)
binaryCode.Replace(l.ToString(), letterValues[l]);

byte value, compare;
Int16 address;
UInt16 address;

if (binaryCode.Length == 24)
{
Expand All @@ -26,7 +26,7 @@ public static byte[] Patch(byte[] data, string code)
try
{
value = Convert.ToByte(new string(new char[] { binaryCode[0], binaryCode[5], binaryCode[6], binaryCode[7], binaryCode[20], binaryCode[1], binaryCode[2], binaryCode[3] }), 2);
address = Convert.ToInt16(new string(new char[] { binaryCode[13], binaryCode[14], binaryCode[15], binaryCode[16], binaryCode[21], binaryCode[22], binaryCode[23], binaryCode[4], binaryCode[9], binaryCode[10], binaryCode[11], binaryCode[12], binaryCode[17], binaryCode[18], binaryCode[19] }), 2);
address = Convert.ToUInt16(new string(new char[] { binaryCode[13], binaryCode[14], binaryCode[15], binaryCode[16], binaryCode[21], binaryCode[22], binaryCode[23], binaryCode[4], binaryCode[9], binaryCode[10], binaryCode[11], binaryCode[12], binaryCode[17], binaryCode[18], binaryCode[19] }), 2);
}
catch
{
Expand Down Expand Up @@ -54,7 +54,7 @@ public static byte[] Patch(byte[] data, string code)
try
{
value = Convert.ToByte(new string(new char[] { binaryCode[0], binaryCode[5], binaryCode[6], binaryCode[7], binaryCode[28], binaryCode[1], binaryCode[2], binaryCode[3] }), 2);
address = Convert.ToInt16(new string(new char[] { binaryCode[13], binaryCode[14], binaryCode[15], binaryCode[16], binaryCode[21], binaryCode[22], binaryCode[23], binaryCode[4], binaryCode[9], binaryCode[10], binaryCode[11], binaryCode[12], binaryCode[17], binaryCode[18], binaryCode[19] }), 2);
address = Convert.ToUInt16(new string(new char[] { binaryCode[13], binaryCode[14], binaryCode[15], binaryCode[16], binaryCode[21], binaryCode[22], binaryCode[23], binaryCode[4], binaryCode[9], binaryCode[10], binaryCode[11], binaryCode[12], binaryCode[17], binaryCode[18], binaryCode[19] }), 2);
compare = Convert.ToByte(new string(new char[] { binaryCode[24], binaryCode[29], binaryCode[30], binaryCode[31], binaryCode[20], binaryCode[25], binaryCode[26], binaryCode[27] }), 2);
}
catch
Expand Down Expand Up @@ -100,24 +100,4 @@ public static byte[] Patch(byte[] data, string code)
{ 'N', "1111" }
};
}

public class GameGenieFormatException : Exception
{
public readonly string Code;
public GameGenieFormatException(string code)
: base(string.Format("Invalid code \"{0}\"", code))
{
Code = code;
}
}

public class GameGenieNotFoundException : Exception
{
public readonly string Code;
public GameGenieNotFoundException(string code)
: base(string.Format("Invalid code \"{0}\"", code))
{
Code = code;
}
}
}
Loading

0 comments on commit c69fe81

Please sign in to comment.