/// <summary>
 /// Handles intercepted file access.
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="desiredAccess"></param>
 /// <param name="shareMode"></param>
 /// <param name="securityAttributes"></param>
 /// <param name="creationDisposition"></param>
 /// <param name="flagsAndAttributes"></param>
 /// <param name="templateFile"></param>
 /// <returns></returns>
 public IntPtr CreateFile(string fileName, FileAccessRightFlags desiredAccess, FileShareModeFlags shareMode,
                            IntPtr securityAttributes, FileCreationDisposition creationDisposition,
                            FileFlagsAndAttributes flagsAndAttributes, IntPtr templateFile)
 {
   var request = new FileRequest
   {
     CreationDisposition = creationDisposition,
     Path = fileName,
     ResourceType = ResourceType.File
   };
   using (EngineCore.Engine.GetEngineProcessingSpace())
   {
     var virtualPath = _fileSystem.GetVirtualPath(request);
     return HostFileSystem.NativeMethods.CreateFile(
       virtualPath, desiredAccess, shareMode, securityAttributes,
       creationDisposition, flagsAndAttributes, templateFile);
   }
 }
 /// <summary>
 /// Returns the replacement path for the specified <paramref name="request"/>.
 /// The result is relative to the virtual file system's root directory.
 /// </summary>
 /// <param name="request">The <see cref="FileRequest"/> to redirect.</param>
 /// <param name="root"></param>
 /// <returns>The replacement path, used for redirection.</returns>
 public FileRequestResult Redirect(FileRequest request, string root)
 {
   var result = new FileRequestResult {Request = request};
   string systemFolder;
   if (request.Path.StartsWithAny(_systemVariables.Keys, out systemFolder, true))
   {
     result.SystemFolder = _systemVariables[systemFolder];
     result.Path = _systemVariables[systemFolder].ToPath() +
                             (request.Path.Length > systemFolder.Length
                                ? request.Path.Substring(systemFolder.Length + 1).ToLowerInvariant()
                                : "");
   }
   else
   {
     result.SystemFolder = VirtualFolder.Other;
     result.Path = RedirectToDefaultFolder(request.Path);
   }
   result.Path = Path.Combine(root, result.Path);
   return result;
 }
 public string GetVirtualPath(FileRequest request)
 {
   if (string.IsNullOrEmpty(request.Path)
       || _virtualEnvironment.IsLocatedInVirtualFileSystem(request.Path)
       || !_virtualEnvironment.IsVirtualizable(request.Path))
     return request.Path;
   // Make sure to have a full path AND the path's long file name.
   // BUT only if the requested resource is NOT a library!
   if (request.ResourceType != ResourceType.Library
       && (!Path.IsPathRooted(request.Path) || request.Path.Contains("~1")))
     request.Path = Path.GetFullPath(request.Path);
   // Get the type of virtualization that needs to be applied.
   VirtualizationType virtualizationType;
   if (!_engineRules.HasRule(request.Path, out virtualizationType))
     EngineCore.Log.Warning("No known engine rule for \"{0}\"", request.Path);
   if (virtualizationType == VirtualizationType.Transparent)
     return request.Path;
   // Redirect the call.
   var redirectedPath = _virtualEnvironment.RedirectRequest(request);
   EngineCore.Log.Debug("FileSystem Redirection: \"{0}\" => \"{1}\"", request.Path, redirectedPath);
   return redirectedPath;
 }
 /// <summary>
 /// Redirects the given <see cref="FileRequest"/> to the virtual environment.
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public string RedirectRequest(FileRequest request)
 {
   // Redirect the path to the virtual environment.
   var result = _redirector.Redirect(request, _root);
   // Verify the result.
   result = GetVerifiedRequestResult(result);
   return result.Path;
 }
 /// <summary>
 /// Handles intercepted requests to delete a file.
 /// </summary>
 /// <param name="fileName"></param>
 /// <returns></returns>
 public bool DeleteFile(string fileName)
 {
   var request = new FileRequest
   {
     CreationDisposition = FileCreationDisposition.OpenExisting,
     Path = fileName,
     ResourceType = ResourceType.File
   };
   using (EngineCore.Engine.GetEngineProcessingSpace())
   {
     var virtualPath = _fileSystem.GetVirtualPath(request);
     return HostFileSystem.NativeMethods.DeleteFile(virtualPath);
   }
 }
 /// <summary>
 /// Retrieves the path of the directory designated for temporary files.
 /// </summary>
 /// <param name="bufferSize">The size of the string buffer identified by lpBuffer, in TCHARs.</param>
 /// <param name="tempPath">
 /// A pointer to a string buffer that receives the null-terminated string specifying the temporary file path.
 /// The returned string ends with a backslash, for example, C:\TEMP\.
 /// </param>
 /// <returns>
 /// If the function succeeds, the return value is the length, in TCHARs, of the string copied to lpBuffer,
 /// not including the terminating null character. If the return value is greater than nBufferLength,
 /// the return value is the length, in TCHARs, of the buffer required to hold the path.
 /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
 /// </returns>
 public int GetTempPath(int bufferSize, ref string tempPath)
 {
   if (tempPath == null)
     return 0;
   string path;
   using (EngineCore.Engine.GetEngineProcessingSpace())
   {
     var request = new FileRequest { Path = System.IO.Path.GetTempPath() };
     path = _fileSystem.GetVirtualPath(request);
   }
   if (!path.EndsWith(@"\"))
     path = path + @"\";
   if (bufferSize >= path.Length + 1) // + terminating null character
   {
     try
     {
       tempPath = path;
     }
     catch (AccessViolationException)
     {
       return 0;
     }
   }
   return path.Length;
 }
 /// <summary>
 /// Handles intercepted library access.
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="file"></param>
 /// <param name="flags"></param>
 /// <returns></returns>
 public IntPtr LoadLibraryEx(string fileName, IntPtr file, ModuleLoadFlags flags)
 {
   var request = new FileRequest
   {
     CreationDisposition = FileCreationDisposition.OpenExisting,
     Path = fileName,
     ResourceType = ResourceType.Library
   };
   using (EngineCore.Engine.GetEngineProcessingSpace())
   {
     var virtualPath = _fileSystem.GetVirtualPath(request);
     return HostFileSystem.NativeMethods.LoadLibraryEx(virtualPath, file, flags);
   }
 }
 /// <summary>
 /// Handles intercepted requests to remove a directory.
 /// </summary>
 /// <param name="pathName"></param>
 /// <returns></returns>
 public bool RemoveDirectory(string pathName)
 {
   var request = new FileRequest
   {
     CreationDisposition = FileCreationDisposition.OpenExisting,
     Path = pathName,
     ResourceType = ResourceType.Directory
   };
   using (EngineCore.Engine.GetEngineProcessingSpace())
   {
     var virtualPath = _fileSystem.GetVirtualPath(request);
     return HostFileSystem.NativeMethods.RemoveDirectory(virtualPath);
   }
 }
 /// <summary>
 /// Handles intercepted directory access.
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="securityAttributes"></param>
 /// <returns></returns>
 public bool CreateDirectory(string fileName, NativeSecurityAttributes securityAttributes)
 {
   var request = new FileRequest
   {
     CreationDisposition = FileCreationDisposition.OpenAlways,
     Path = fileName,
     ResourceType = ResourceType.Directory
   };
   using (EngineCore.Engine.GetEngineProcessingSpace())
   {
     var virtualPath = _fileSystem.GetVirtualPath(request);
     return HostFileSystem.NativeMethods.CreateDirectory(virtualPath, securityAttributes);
   }
 }