1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Text;
6 using System.Text.RegularExpressions;
7 using System.Threading.Tasks;
8 
9 namespace Mesen.GUI.Debugger
10 {
11 	public class TblLoader
12 	{
13 		public struct TblKey
14 		{
15 			public UInt64 Key;
16 			public int Length;
17 
GetBytesMesen.GUI.Debugger.TblLoader.TblKey18 			public byte[] GetBytes()
19 			{
20 				byte[] bytes = new byte[this.Length];
21 				UInt64 value = this.Key;
22 				for(int i = 0; i < this.Length; i++) {
23 					bytes[i] = (byte)value;
24 					value >>= 8;
25 				}
26 				return bytes;
27 			}
28 		}
29 
ToDictionary(string[] fileContents)30 		public static Dictionary<TblKey, string> ToDictionary(string[] fileContents)
31 		{
32 			try {
33 				Dictionary<TblKey, string> dict = new Dictionary<TblKey, string>();
34 
35 				for(int i = 0; i < fileContents.Length; i++) {
36 					if(!string.IsNullOrWhiteSpace(fileContents[i])) {
37 						string[] data = fileContents[i].Split('=');
38 						if(data.Length == 2) {
39 							data[0] = data[0].Replace(" ", "");
40 							if(data[0].Length % 2 == 0 && Regex.IsMatch(data[0], "[0-9A-Fa-f]+")) {
41 								TblKey key = new TblKey();
42 
43 								for(int j = 0; j < data[0].Length; j+=2) {
44 									byte result = byte.Parse(data[0].Substring(j, 2), System.Globalization.NumberStyles.HexNumber);
45 									key.Key |= (UInt64)result << (8 * j / 2);
46 									key.Length++;
47 								}
48 
49 								dict[key] = string.IsNullOrEmpty(data[1]) ? " " : data[1];
50 							}
51 						}
52 					}
53 				}
54 
55 				return dict;
56 			} catch {
57 				return null;
58 			}
59 		}
60 	}
61 }
62