WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
TableNameMappings.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Xml.Serialization;
6 using FuzzyStrings;
7 namespace PinballX.Table2RomMapping
8 {
9  [XmlRoot(ElementName="TableNameMappings")]
10  public class TableNameMappings : List<Mapping>
11  {
12 
13 
14  public Mapping GetBestMatchingMapping(string TableName)
15  {
16  foreach (Mapping M in this)
17  {
18  if (M.TableName.Equals(TableName, StringComparison.InvariantCultureIgnoreCase))
19  {
20  return M;
21  }
22  }
23 
24 
25  Mapping BestMatch = null;
26  double BestScore=-1;
27  foreach (Mapping M in this)
28  {
29  double Score = FuzzyStrings.FuzzyText.DiceCoefficient(TableName, M.TableName);
30 
31  if (Score > BestScore)
32  {
33  BestMatch = M;
34  BestScore = Score;
35  }
36  }
37  if (BestScore > 0.3)
38  {
39  return BestMatch;
40  }
41  else
42  {
43  return null;
44  }
45  }
46 
47 
48 
49 
50  public static TableNameMappings LoadTableMappings(string Filename)
51  {
52  if (!string.IsNullOrEmpty(Filename))
53  {
54  if (new FileInfo(Filename).Exists)
55  {
56  string Data = null;
57  try
58  {
59  StreamReader streamReader = new StreamReader(Filename);
60  Data = streamReader.ReadToEnd();
61  streamReader.Close();
62  streamReader.Dispose();
63 
64  }
65  catch (Exception E)
66  {
67 
68  throw new Exception("Could not read data from TableMapping file: " + Filename, E);
69  }
70  byte[] xmlBytes = Encoding.Default.GetBytes(Data);
71  using (MemoryStream ms = new MemoryStream(xmlBytes))
72  {
73  try
74  {
75  return (TableNameMappings)new XmlSerializer(typeof(TableNameMappings)).Deserialize(ms);
76  }
77 catch (Exception E)
78  {
79 
80  Exception Ex = new Exception("Could not deserialize the TableNameMappings config from XML data: " + E.Message, E);
81  Ex.Data.Add("XML Data", Data);
82  throw Ex;
83  }
84  }
85 
86  }
87  else
88  {
89  throw new FileNotFoundException("File does not exist: " + Filename, Filename);
90  }
91  }
92  else
93  {
94  return new TableNameMappings();
95  }
96  }
97 
98 
99  public void SaveTableMappings(string Filename)
100  {
101  string Xml = "";
102  using (MemoryStream ms = new MemoryStream())
103  {
104  new XmlSerializer(typeof(TableNameMappings)).Serialize(ms, this);
105  ms.Position = 0;
106  using (StreamReader sr = new StreamReader(ms, Encoding.Default))
107  {
108  Xml = sr.ReadToEnd();
109  sr.Dispose();
110  }
111  }
112 
113  FileInfo FI = new FileInfo(Filename);
114  StreamWriter SW= FI.CreateText();
115  SW.Write(Xml);
116  SW.Close();
117  }
118 
119  }
120 }
static TableNameMappings LoadTableMappings(string Filename)
Simple class containing the mappinging between tablename and romname
Definition: Mapping.cs:10
Mapping GetBestMatchingMapping(string TableName)
string TableName
Gets or sets the name of the table. This is not necessarly the same as the name of the table file...
Definition: Mapping.cs:19