11using System ;
22using System . IO ;
3+ using System . Linq ;
34using System . Reflection ;
45using System . Runtime . InteropServices ;
56using FastText . NetWrapper . Logging ;
@@ -39,7 +40,13 @@ private static void LoadLibrary(string path)
3940 {
4041 _log . Info ( $ "Directly loading { path } ...") ;
4142 var result = LoadLibraryEx ( path , IntPtr . Zero , LoadLibraryFlags . LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LoadLibraryFlags . LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LoadLibraryFlags . LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LoadLibraryFlags . LOAD_LIBRARY_SEARCH_SYSTEM32 | LoadLibraryFlags . LOAD_LIBRARY_SEARCH_USER_DIRS ) ;
42- _log . Info ( result == IntPtr . Zero ? "FAILED!" : "Success" ) ;
43+ if ( result == IntPtr . Zero )
44+ {
45+ var error = Marshal . GetLastWin32Error ( ) ;
46+ _log . Error ( $ "FAILED! Last Win32 error is: { error } ") ;
47+ throw new Exception ( $ "Failed to load library with path \" { path } \" ") ;
48+ }
49+ _log . Info ( "Successfully loaded library." ) ;
4350 }
4451
4552 private static string UnpackResources ( )
@@ -66,9 +73,46 @@ private static void UnpackFile(string curDir, string fileName, byte[] bytes)
6673 {
6774 var path = ! string . IsNullOrEmpty ( curDir ) ? Path . Combine ( curDir , fileName ) : fileName ;
6875
69- _log . Info ( $ "Unpacking { fileName } .") ;
76+ try
77+ {
78+ if ( File . Exists ( path ) )
79+ {
80+ var existingFileContents = File . ReadAllBytes ( path ) ;
81+ if ( existingFileContents . Length == bytes . Length )
82+ {
83+ if ( existingFileContents . SequenceEqual ( bytes ) )
84+ {
85+ _log . Info ( $ "File { path } already exists and is the same (length and contents)") ;
86+ return ;
87+ }
88+ }
89+ }
90+ }
91+ catch ( Exception e )
92+ {
93+ _log . Warn ( $ "Unable to determine size of existing file, will replace with our version. Message: { e . Message } ") ;
94+ try
95+ {
96+ File . Delete ( path ) ;
97+ }
98+ catch ( Exception deleteException )
99+ {
100+ _log . Error ( $ "Unable to delete existing file: { path } . Message: { deleteException . Message } ") ;
101+ return ;
102+ }
103+ }
104+
105+ _log . Info ( $ "Unpacking { fileName } to path { curDir } .") ;
70106
71- File . WriteAllBytes ( path , bytes ) ;
107+ try
108+ {
109+ File . WriteAllBytes ( path , bytes ) ;
110+ }
111+ catch ( Exception writeException )
112+ {
113+ _log . Error ( $ "Unable to write: { path } . Message: { writeException . Message } ") ;
114+ throw ;
115+ }
72116 }
73117
74118 #region LoadLibraryEx
0 commit comments