Donuts from Microsoft
//-----------------------------------------------------------------------------
// File: filewatch. cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
# Include "stdafx. H"
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
Cfilewatch: cfilewatch ()
{
M_nfilestowatch = 0;
M_hfilechangethread = NULL;
M_dwfilechangethreadid = 0;
M_bfilechanged = false;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
Cfilewatch ::~ Cfilewatch ()
{
Cleanup ();
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
Hresult cfilewatch: addfiletowatch (tchar * strwatchfile)
{
Strcpy (m_strfilestowatch [m_nfilestowatch ++], strwatchfile );
Return s_ OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
Hresult cfilewatch: Start ()
{
M_hfilechangethread = createthread (null, 0, staticfilechangethreadfunc,
This, 0, & m_dwfilechangethreadid );
Return s_ OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
Bool cfilewatch: havefileschanged (bool bresetchangeflag)
{
Bool bfilechanged = m_bfilechanged;
If (bresetchangeflag)
M_bfilechanged = false;
Return bfilechanged;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
DWORD winapi cfilewatch: staticfilechangethreadfunc (lpvoid lpparam)
{
Cfilewatch * PAPP = (cfilewatch *) lpparam;
Return PAPP-> filechangethreadfunc ();
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
DWORD cfilewatch: filechangethreadfunc ()
{
Handle hfilehandles [max_watch_files];
Filetime alastwritetime [max_watch_files];
Tchar astrfiledir [max_watch_files] [max_path];
Tchar * strfilepart;
Filetime lastwritetime;
DWORD dwresult;
Int I;
Int J;
Handle hwatchhandles [max_watch_files];
Int nwatchhandles = 0;
For (I = 0; I <m_nfilestowatch; I ++)
{
Hfilehandles [I] = createfile (m_strfilestowatch [I], generic_read,
File_pai_read | file_pai_write,
Null, open_existing, 0, null );
Getfiletime (hfilehandles [I], null, null, & alastwritetime [I]);
Getfullpathname (m_strfilestowatch [I], max_path,
Astrfiledir [I], & strfilepart );
If (strfilepart)
* Strfilepart = 0;
Bool bfound = false;
For (j = 0; j <I; j ++)
{
If (strcmp (astrfiledir [I], astrfiledir [J]) = 0)
{
Bfound = true;
Break;
}
}
If (! Bfound)
{
Hwatchhandles [nwatchhandles ++] = findfirstchangenotification (
Astrfiledir [I], false,
File_policy_change_last_write );
}
}
Bool bdone = false;
While (! Bdone)
{
Dwresult = msgwaitformultipleobjects (nwatchhandles, hwatchhandles, false, infinite, qs_allevents );
If (dwresult = wait_object_0 + nwatchhandles)
{
MSG;
If (peekmessage (& MSG, null, 0, 0, pm_remove ))
{
If (msg. Message = wm_quit)
Bdone = true;
}
}
Else
{
Int nindex = dwresult-wait_object_0;
Assert (nindex> = 0 | nindex <10 );
For (I = 0; I <m_nfilestowatch; I ++)
{
Getfiletime (hfilehandles [I], null, null, & lastwritetime );
If (memcmp (& lastwritetime, & alastwritetime [I], sizeof (filetime )))
{
M_bfilechanged = true;
}
}
Findnextchangenotification (hwatchhandles [nindex]);
Findnextchangenotification (hwatchhandles [nindex]);
}
}
For (I = 0; I <nwatchhandles; I ++)
Findclosechangenotification (hwatchhandles [I]);
For (I = 0; I <m_nfilestowatch; I ++)
Closehandle (hfilehandles [I]);
Return 0;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
Hresult cfilewatch: cleanup ()
{
If (m_hfilechangethread)
{
Postthreadmessage (m_dwfilechangethreadid, wm_quit, 0, 0 );
Waitforsingleobject (m_hfilechangethread, infinite );
Closehandle (m_hfilechangethread );
M_hfilechangethread = NULL;
}
Return s_ OK;
}
Header
//-----------------------------------------------------------------------------
// File: filewatch. h
//
// Desc: class to watch a set of file to see if any change. If they do
// Then havefileschanged () will return true.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
# Pragma once
# Define max_watch_files 10
Class cfilewatch
{
Public:
Cfilewatch ();
Virtual ~ Cfilewatch ();
Hresult addfiletowatch (tchar * strwatchfile );
Hresult start ();
Bool havefileschanged (bool bresetchangeflag );
Hresult cleanup ();
Public:
Protected:
DWORD m_dwfilechangethreadid;
Handle m_hfilechangethread;
Tchar m_strfilestowatch [max_watch_files] [max_path];
Int m_nfilestowatch;
Bool m_bfilechanged;
Static DWORD winapi staticfilechangethreadfunc (lpvoid lpparam );
DWORD filechangethreadfunc ();
};
However, when the game is running, a thread is created to monitor the specified directory and file. If the file content changes or the folder name changes, the thread will be blocked, but it seems that donuts has not processed it, so what should we do? The game may need to be used during its operation
Change the path in the file or re-load the file in the folder? Currently, I can only guess this, but for me,
This function has no practical significance for the moment.