FolderMonitor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup Utility-Editor
  9. * @{
  10. */
  11. /// <summary>
  12. /// Allows monitoring a file system folder for changes. Depending on the flags set this monitor can notify you when
  13. /// file is changed/moved/renamed and similar.
  14. /// </summary>
  15. public class FolderMonitor : ScriptObject
  16. {
  17. /// <summary>
  18. /// Triggers when a file in the monitored folder is modified. Provides absolute path to the modified file.
  19. /// </summary>
  20. public Action<string> OnModified;
  21. /// <summary>
  22. /// Triggers when a file/folder is added in the monitored folder. Provides absolute path to the added file/folder.
  23. /// </summary>
  24. public Action<string> OnAdded;
  25. /// <summary>
  26. /// Triggers when a file/folder is removed from the monitored folder. Provides absolute path to the removed
  27. /// file/folder.
  28. /// </summary>
  29. public Action<string> OnRemoved;
  30. /// <summary>
  31. /// Triggers when a file/folder is renamed in the monitored folder. Provides absolute path with old and new names.
  32. /// </summary>
  33. public Action<string, string> OnRenamed;
  34. /// <summary>
  35. /// Constructs a new folder monitor instance that immediately starts monitor the provided folder.
  36. /// </summary>
  37. /// <param name="folderToMonitor">Absolute path to the folder to monitor.</param>
  38. public FolderMonitor(string folderToMonitor)
  39. {
  40. Internal_CreateInstance(this, folderToMonitor);
  41. }
  42. /// <summary>
  43. /// Stops monitoring the folder.
  44. /// </summary>
  45. public void Destroy()
  46. {
  47. Internal_Destroy(mCachedPtr);
  48. }
  49. /// <summary>
  50. /// Triggered by the runtime when a file in the monitored folder is modified.
  51. /// </summary>
  52. /// <param name="path">Absolute path to the modified file/folder.</param>
  53. private void Internal_DoOnModified(string path)
  54. {
  55. if (OnModified != null)
  56. OnModified(path);
  57. }
  58. /// <summary>
  59. /// Triggered by the runtime when a file in the monitored folder is added.
  60. /// </summary>
  61. /// <param name="path">Absolute path to the added file/folder.</param>
  62. private void Internal_DoOnAdded(string path)
  63. {
  64. if (OnAdded != null)
  65. OnAdded(path);
  66. }
  67. /// <summary>
  68. /// Triggered by the runtime when a file in the monitored folder is removed.
  69. /// </summary>
  70. /// <param name="path">Absolute path to the removed file/folder.</param>
  71. private void Internal_DoOnRemoved(string path)
  72. {
  73. if (OnRemoved != null)
  74. OnRemoved(path);
  75. }
  76. /// <summary>
  77. /// Triggered by the runtime when a file in the monitored folder is renamed.
  78. /// </summary>
  79. /// <param name="from">Absolute path to the original file/folder location.</param>
  80. /// <param name="to">Absolute path to the new file/folder location.</param>
  81. private void Internal_DoOnRenamed(string from, string to)
  82. {
  83. if (OnRenamed != null)
  84. OnRenamed(from, to);
  85. }
  86. [MethodImpl(MethodImplOptions.InternalCall)]
  87. private static extern void Internal_CreateInstance(FolderMonitor instance, string folder);
  88. [MethodImpl(MethodImplOptions.InternalCall)]
  89. private static extern void Internal_Destroy(IntPtr thisPtr);
  90. }
  91. /** @} */
  92. }