Beforing fixing bug of failing most API if disable vfs thread is on

This commit is contained in:
Berserker 2019-05-25 20:50:23 +03:00
parent 45d3b258b9
commit d67a8ae4f9
3 changed files with 53 additions and 11 deletions

View File

@ -8,7 +8,7 @@ unit VfsExport;
uses uses
Windows, Windows,
VfsDebug, VfsBase, VfsControl, DlgMes, Files, FilesEx; VfsDebug, VfsBase, VfsControl, VfsWatching;
exports exports
VfsDebug.SetLoggingProc, VfsDebug.SetLoggingProc,
@ -43,6 +43,11 @@ begin
result := VfsControl.MapModsFromList(WideString(RootDir), WideString(ModsDir), WideString(ModListFile), Flags); result := VfsControl.MapModsFromList(WideString(RootDir), WideString(ModsDir), WideString(ModListFile), Flags);
end; end;
function RunWatcher (const WatchDir: PWideChar; DebounceInterval: integer): LONGBOOL; stdcall;
begin
result := VfsWatching.RunWatcher(WatchDir, DebounceInterval);
end;
procedure ConsoleLoggingProc (Operation, Message: pchar); stdcall; procedure ConsoleLoggingProc (Operation, Message: pchar); stdcall;
begin begin
WriteLn('>> ', string(Operation), ': ', string(Message), #13#10); WriteLn('>> ', string(Operation), ': ', string(Message), #13#10);
@ -82,12 +87,7 @@ exports
MapDirA, MapDirA,
MapModsFromList, MapModsFromList,
MapModsFromListA, MapModsFromListA,
RunWatcher,
InstallConsoleLogger; InstallConsoleLogger;
// var s: string;
// begin
// assert(MapModsFromListA('D:\Heroes 3', 'D:\Heroes 3\Mods', 'D:\Heroes 3\Mods\list.txt'));
// VfsControl.RunVfs(SORT_FIFO);
// ReadFileContents('D:\Heroes 3\Data\s\pHoenix.erm', s);
// VarDump([GetFileList('D:\Heroes 3\Data\s\*', FILES_AND_DIRS).ToText(#13#10)]);
end. end.

View File

@ -28,6 +28,11 @@ function MapModsFromList (const RootDir, ModsDir, ModListFile: PWideChar; Flags:
(* Runs all VFS subsystems, unless VFS is already running *) (* Runs all VFS subsystems, unless VFS is already running *)
function RunVfs (DirListingOrder: TDirListingSortType): LONGBOOL; stdcall; external 'vfs.dll'; function RunVfs (DirListingOrder: TDirListingSortType): LONGBOOL; stdcall; external 'vfs.dll';
(* Spawns separate thread, which starts recursive monitoring for changes in specified directory.
VFS will be fully refreshed or smartly updated on any change. Debounce interval specifies
time in msec to wait after last change before running full VFS rescanning routine *)
function RunWatcher (const WatchDir: PWideChar; DebounceInterval: integer): LONGBOOL; stdcall; external 'vfs.dll';
(* Allocates console and install logger, writing messages to console *) (* Allocates console and install logger, writing messages to console *)
procedure InstallConsoleLogger; stdcall; external 'vfs.dll'; procedure InstallConsoleLogger; stdcall; external 'vfs.dll';

View File

@ -10,7 +10,13 @@ unit VfsWatching;
uses uses
Windows, SysUtils, Math, Windows, SysUtils, Math,
Utils, Concur, WinUtils, StrLib, WinNative, Utils, Concur, WinUtils, StrLib, WinNative,
VfsBase, VfsUtils; VfsBase, VfsUtils, {FIXME} DlgMes;
(* Spawns separate thread, which starts recursive monitoring for changes in specified directory.
VFS will be fully refreshed or smartly updated on any change. Debounce interval specifies
time in msec to wait after last change before running full VFS rescanning routine *)
function RunWatcher (const WatchDir: WideString; DebounceInterval: integer): boolean;
(***) implementation (***) (***) implementation (***)
@ -74,8 +80,8 @@ var
WatcherCritSection: Concur.TCritSection; WatcherCritSection: Concur.TCritSection;
AbsWatcherDir: WideString; AbsWatcherDir: WideString;
WatcherDebounceInterval: integer; WatcherDebounceInterval: integer;
WatcherStopEvent: THandle; WatcherStopEvent: THandle = 0;
WatcherIsRunning: boolean; WatcherIsRunning: boolean = false;
WatcherThreadHandle: THandle; WatcherThreadHandle: THandle;
WatcherThreadId: cardinal; WatcherThreadId: cardinal;
@ -247,6 +253,7 @@ begin
if NeedFullRescan and (PlannedRescanTime <= CurrentTime) then begin if NeedFullRescan and (PlannedRescanTime <= CurrentTime) then begin
VfsBase.RefreshVfs; VfsBase.RefreshVfs;
NeedFullRescan := false; NeedFullRescan := false;
VarDump(['Fully rescanned']);
end; end;
if DirChangesScanner = nil then begin if DirChangesScanner = nil then begin
@ -276,6 +283,7 @@ begin
end else if DirChange.Action = NOTIFY_FILE_MODIFIED then begin end else if DirChange.Action = NOTIFY_FILE_MODIFIED then begin
if not NeedFullRescan then begin if not NeedFullRescan then begin
VfsBase.RefreshMappedFile(DirChange.FilePath); VfsBase.RefreshMappedFile(DirChange.FilePath);
VarDump(['Updated ' + DirChange.FilePath]);
end; end;
LastChangeTime := WinUtils.GetMicroTime; LastChangeTime := WinUtils.GetMicroTime;
@ -313,7 +321,36 @@ begin
end; end;
Leave; Leave;
end; end; // .with
end; // .function RunWatcher end; // .function RunWatcher
function StopWatcher: LONGBOOL;
const
MANUAL_RESET = true;
begin
with WatcherCritSection do begin
Enter;
result := WatcherIsRunning;
if result then begin
Windows.SetEvent(WatcherStopEvent);
result := Windows.WaitForSingleObject(WatcherThreadHandle, Windows.INFINITE) = Windows.WAIT_OBJECT_0;
if result then begin
Windows.CloseHandle(WatcherThreadHandle);
Windows.CloseHandle(WatcherStopEvent);
WatcherThreadHandle := 0;
WatcherStopEvent := 0;
WatcherIsRunning := false;
end;
end;
Leave;
end; // .with
end; // .function StopWatcher
begin
WatcherCritSection.Init;
end. end.