FolderMonitor.cs 3.6 KB

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