FolderMonitor.cs 3.8 KB

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