1
1
#include " mnemonichelp.h"
2
2
#include " threading.h"
3
+ #include < atomic>
3
4
#include " jansson/jansson_x64dbg.h"
5
+ #include " debugger.h"
6
+ #include " filehelper.h"
4
7
5
8
static std::unordered_map<String, String> MnemonicMap;
6
9
static std::unordered_map<String, String> MnemonicBriefMap;
10
+ static std::atomic<bool > isMnemonicLoaded (false );
11
+ static bool loadFromText ();
7
12
8
- bool MnemonicHelp::loadFromText ( const char * json )
13
+ static inline void loadMnemonicHelp ( )
9
14
{
10
- EXCLUSIVE_ACQUIRE (LockMnemonicHelp);
15
+ if (isMnemonicLoaded.load ())
16
+ return ;
17
+ else
18
+ // Load mnemonic help database
19
+ loadFromText ();
20
+ }
21
+
22
+ static bool loadFromText ()
23
+ {
24
+ EXCLUSIVE_ACQUIRE (LockMnemonicHelp); // Protect the following code in a critical section
25
+ if (isMnemonicLoaded.load ())
26
+ return true ;
27
+ isMnemonicLoaded.store (true ); // Don't retry failed load(and spam log).
28
+ String json;
29
+ if (!FileHelper::ReadAllText (StringUtils::sprintf (" %s\\ ..\\ mnemdb.json" , szProgramDir), json))
30
+ {
31
+ dputs (QT_TRANSLATE_NOOP (" DBG" , " Failed to read mnemonic help database..." ));
32
+ return false ;
33
+ }
11
34
MnemonicMap.clear ();
12
- auto root = json_loads (json, 0 , 0 );
35
+ auto root = json_loads (json. c_str () , 0 , 0 );
13
36
if (root)
14
37
{
15
38
// Get a handle to the root object -> x86-64 subtree
@@ -50,7 +73,11 @@ bool MnemonicHelp::loadFromText(const char* json)
50
73
json_decref (root);
51
74
}
52
75
else
76
+ {
77
+ dputs (QT_TRANSLATE_NOOP (" DBG" , " Failed to load mnemonic help database..." ));
53
78
return false ;
79
+ }
80
+ dputs (QT_TRANSLATE_NOOP (" DBG" , " Mnemonic help database loaded!" ));
54
81
return true ;
55
82
}
56
83
@@ -86,6 +113,7 @@ String MnemonicHelp::getDescription(const char* mnem, int depth)
86
113
return GuiTranslateText (QT_TRANSLATE_NOOP (" DBG" , " Invalid mnemonic!" ));
87
114
if (depth == 10 )
88
115
return GuiTranslateText (QT_TRANSLATE_NOOP (" DBG" , " Too many redirections..." ));
116
+ loadMnemonicHelp ();
89
117
SHARED_ACQUIRE (LockMnemonicHelp);
90
118
auto mnemuni = getUniversalMnemonic (mnem);
91
119
auto found = MnemonicMap.find (mnemuni);
@@ -110,6 +138,7 @@ String MnemonicHelp::getBriefDescription(const char* mnem)
110
138
{
111
139
if (mnem == nullptr )
112
140
return GuiTranslateText (QT_TRANSLATE_NOOP (" DBG" , " Invalid mnemonic!" ));
141
+ loadMnemonicHelp ();
113
142
SHARED_ACQUIRE (LockMnemonicHelp);
114
143
auto mnemLower = StringUtils::ToLower (mnem);
115
144
if (mnemLower == " ???" )
0 commit comments