1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.IO;
7 using System.Linq;
8 using System.Text;
9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 using System.Xml;
12 using Mesen.GUI.Config;
13 
14 namespace Mesen.GUI.Forms.Cheats
15 {
16 	public partial class frmCheatImport : BaseForm
17 	{
18 		private string _gameCrc = "";
19 		private string _gameName = "";
20 		private string _cheatFile = null;
21 		private bool _isMesenCheatFile = false;
22 
23 		public List<CheatInfo> ImportedCheats { get; internal set; }
24 
frmCheatImport()25 		public frmCheatImport()
26 		{
27 			InitializeComponent();
28 
29 			UpdateImportButton();
30 
31 			RomInfo romInfo = InteropEmu.GetRomInfo();
32 			_gameCrc = romInfo.GetPrgCrcString();
33 			_gameName = romInfo.GetRomName();
34 			txtGameName.Text = _gameName;
35 		}
36 
UpdateImportButton()37 		private void UpdateImportButton()
38 		{
39 			btnImport.Enabled = !string.IsNullOrWhiteSpace(_cheatFile) && (!string.IsNullOrWhiteSpace(_gameName) || _isMesenCheatFile);
40 		}
41 
LoadGame(string romPath)42 		private void LoadGame(string romPath)
43 		{
44 			ResourcePath resource = romPath;
45 			if(frmSelectRom.SelectRom(ref resource)) {
46 				RomInfo romInfo = InteropEmu.GetRomInfo(resource);
47 				_gameCrc = romInfo.GetPrgCrcString();
48 				_gameName = romInfo.GetRomName();
49 				txtGameName.Text = _gameName;
50 				UpdateImportButton();
51 			}
52 		}
53 
btnBrowseGame_Click(object sender, EventArgs e)54 		private void btnBrowseGame_Click(object sender, EventArgs e)
55 		{
56 			OpenFileDialog ofd = new OpenFileDialog();
57 			ofd.SetFilter(ResourceHelper.GetMessage("FilterRom"));
58 			if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
59 				LoadGame(ofd.FileName);
60 			}
61 		}
62 
btnBrowseCheat_Click(object sender, EventArgs e)63 		private void btnBrowseCheat_Click(object sender, EventArgs e)
64 		{
65 			OpenFileDialog ofd = new OpenFileDialog();
66 			ofd.SetFilter(ResourceHelper.GetMessage("FilterCheat"));
67 			if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
68 				_cheatFile = ofd.FileName;
69 				_isMesenCheatFile = NestopiaCheatLoader.IsMesenCheatFile(_cheatFile);
70 				if(_isMesenCheatFile) {
71 					txtGameName.Text = "";
72 					_gameName = "";
73 					_gameCrc = "";
74 				}
75 				txtCheatFile.Text = Path.GetFileName(_cheatFile);
76 				UpdateImportButton();
77 			}
78 		}
79 
btnImport_Click(object sender, EventArgs e)80 		private void btnImport_Click(object sender, EventArgs e)
81 		{
82 			if(File.Exists(_cheatFile)) {
83 				switch(Path.GetExtension(_cheatFile).ToLowerInvariant().Substring(1)) {
84 					default:
85 					case "xml":
86 						ImportedCheats = NestopiaCheatLoader.Load(_cheatFile, _gameName, _gameCrc);
87 						break;
88 
89 					case "cht":
90 						ImportedCheats = FceuxCheatLoader.Load(_cheatFile, _gameName, _gameCrc);
91 						break;
92 				}
93 			}
94 
95 			if(ImportedCheats != null) {
96 				this.DialogResult = DialogResult.OK;
97 				this.Close();
98 			}
99 		}
100 
btnCancel_Click(object sender, EventArgs e)101 		private void btnCancel_Click(object sender, EventArgs e)
102 		{
103 			DialogResult = DialogResult.Cancel;
104 			this.Close();
105 		}
106 	}
107 }
108