1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Diagnostics ;
4+ using System . Linq ;
25using System . Xml ;
36using SPCode . Utils ;
47
@@ -52,18 +55,51 @@ public class HotkeyControl
5255 } ;
5356
5457 /// <summary>
55- /// Buffers the hotkeys to an array in memory.
58+ /// Checks if there are new Hotkeys that haven't been added to the Hotkeys file <br>
59+ /// and buffers all Hotkeys to an array in memory.
5660 /// </summary>
57- public static void BufferHotkeys ( )
61+ public static void CheckAndBufferHotkeys ( )
5862 {
59- // Buffer hotkeys in global HotkeyInfo list
60- Program . HotkeysList = new ( ) ;
61- var document = new XmlDocument ( ) ;
62- document . Load ( Constants . HotkeysFile ) ;
63+ try
64+ {
65+ // Load the current Hotkeys file
66+ Program . HotkeysList = new ( ) ;
67+ var document = new XmlDocument ( ) ;
68+ document . Load ( Constants . HotkeysFile ) ;
69+
70+ // Compare with the default hotkeys to check for new commands
71+ var xmlNodes = document . ChildNodes [ 0 ] . ChildNodes . Cast < XmlNode > ( ) . ToList ( ) ;
72+ var defaultHksCount = DefaultHotkeys . Count ;
73+
74+ // If the count is not equal, there's a new hotkey to be added to the file
75+ if ( xmlNodes . Count != defaultHksCount )
76+ {
77+ // Regular for to get the index
78+ for ( var i = 0 ; i < defaultHksCount ; i ++ )
79+ {
80+ var currentHk = DefaultHotkeys . ElementAt ( i ) ;
81+ if ( ! xmlNodes . Any ( x => x . Name . Equals ( DefaultHotkeys . ElementAt ( i ) . Key ) ) )
82+ {
83+ var element = document . CreateElement ( string . Empty , currentHk . Key , string . Empty ) ;
84+ var text = document . CreateTextNode ( currentHk . Value ) ;
85+ element . AppendChild ( text ) ;
86+ document . ChildNodes [ 0 ] . InsertBefore ( element , document . ChildNodes [ 0 ] . ChildNodes [ i ] ) ;
87+ }
88+ }
89+ document . Save ( Constants . HotkeysFile ) ;
90+ xmlNodes = document . ChildNodes [ 0 ] . ChildNodes . Cast < XmlNode > ( ) . ToList ( ) ;
91+ }
6392
64- foreach ( XmlNode node in document . ChildNodes [ 0 ] . ChildNodes )
93+ xmlNodes . ForEach ( x =>
94+ {
95+ var hki = new HotkeyInfo ( new Hotkey ( x . InnerText ) , x . Name ) ;
96+ Program . HotkeysList . Add ( hki ) ;
97+ } ) ;
98+
99+ }
100+ catch ( Exception ex )
65101 {
66- Program . HotkeysList . Add ( new HotkeyInfo ( new Hotkey ( node . InnerText ) , node . Name ) ) ;
102+ throw new Exception ( "Error while checking and buffering the hotkeys" , ex ) ;
67103 }
68104 }
69105
@@ -72,29 +108,36 @@ public static void BufferHotkeys()
72108 /// </summary>
73109 public static void CreateDefaultHotkeys ( )
74110 {
75- // Create the XML document
76- var document = new XmlDocument ( ) ;
111+ try
112+ {
113+ // Create the XML document
114+ var document = new XmlDocument ( ) ;
77115
78- var rootElement = document . CreateElement ( string . Empty , "Hotkeys" , string . Empty ) ;
79- document . AppendChild ( rootElement ) ;
116+ var rootElement = document . CreateElement ( string . Empty , "Hotkeys" , string . Empty ) ;
117+ document . AppendChild ( rootElement ) ;
80118
81- // Fill it with the default hotkeys from the dictionary
82- foreach ( var item in DefaultHotkeys )
83- {
84- var element = document . CreateElement ( string . Empty , item . Key , string . Empty ) ;
85- var text = document . CreateTextNode ( item . Value ) ;
86- element . AppendChild ( text ) ;
87- rootElement . AppendChild ( element ) ;
88- }
119+ // Fill it with the default hotkeys from the dictionary
120+ foreach ( var item in DefaultHotkeys )
121+ {
122+ var element = document . CreateElement ( string . Empty , item . Key , string . Empty ) ;
123+ var text = document . CreateTextNode ( item . Value ) ;
124+ element . AppendChild ( text ) ;
125+ rootElement . AppendChild ( element ) ;
126+ }
127+
128+ // Buffer hotkeys in global HotkeyInfo list
129+ Program . HotkeysList = new List < HotkeyInfo > ( ) ;
130+ foreach ( XmlNode node in document . ChildNodes [ 0 ] . ChildNodes )
131+ {
132+ Program . HotkeysList . Add ( new HotkeyInfo ( new Hotkey ( node . InnerText ) , node . Name ) ) ;
133+ }
89134
90- // Buffer hotkeys in global HotkeyInfo list
91- Program . HotkeysList = new List < HotkeyInfo > ( ) ;
92- foreach ( XmlNode node in document . ChildNodes [ 0 ] . ChildNodes )
135+ document . Save ( Constants . HotkeysFile ) ;
136+ }
137+ catch ( Exception ex )
93138 {
94- Program . HotkeysList . Add ( new HotkeyInfo ( new Hotkey ( node . InnerText ) , node . Name ) ) ;
139+ throw new Exception ( "Error while creating the default hotkeys" , ex ) ;
95140 }
96-
97- document . Save ( Constants . HotkeysFile ) ;
98141 }
99142 }
100143}
0 commit comments