SerializedDiff.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /** @addtogroup Utility-Editor
  9. * @{
  10. */
  11. /// <summary>
  12. /// Container and functionality to creating and storing differences between two managed objects of the same type.
  13. /// </summary>
  14. public class SerializedDiff : ScriptObject
  15. {
  16. /// <summary>
  17. /// Creates a difference of properties that are different in <paramref name="newObj"/> compared to
  18. /// <paramref name="oldObj"/>. The difference can later be applied to <paramref name="oldObj"/> to restore it to
  19. /// the same state as <paramref name="newObj"/> .
  20. /// </summary>
  21. /// <param name="oldObj">Object to compare from.</param>
  22. /// <param name="newObj">Object to compare to.</param>
  23. /// <returns>A set of differences between the two objects.</returns>
  24. public static SerializedDiff Create(object oldObj, object newObj)
  25. {
  26. SerializedObject serializedOldObj = SerializedObject.Create(oldObj);
  27. return Create(serializedOldObj, newObj);
  28. }
  29. /// <summary>
  30. /// Creates a difference of properties that are different in <paramref name="newObj"/> compared to
  31. /// <paramref name="oldObj"/>. The difference can later be applied to <paramref name="oldObj"/> to restore it to
  32. /// the same state as <paramref name="newObj"/> .
  33. /// </summary>
  34. /// <param name="oldObj">Object to compare from.</param>
  35. /// <param name="newObj">Object to compare to.</param>
  36. /// <returns>A set of differences between the two objects.</returns>
  37. public static SerializedDiff Create(SerializedObject oldObj, object newObj)
  38. {
  39. if (oldObj == null || newObj == null)
  40. return null;
  41. IntPtr oldObjPtr = oldObj.GetCachedPtr();
  42. return Internal_CreateDiff(oldObjPtr, newObj);
  43. }
  44. /// <summary>
  45. /// Applies difference stored in this object to the provided object. The type of the object must be the same as the
  46. /// type of objects the difference was generated from.
  47. /// </summary>
  48. /// <param name="obj">Object to apply the difference to.</param>
  49. public void Apply(object obj)
  50. {
  51. if (obj == null)
  52. return;
  53. Internal_ApplyDiff(mCachedPtr, obj);
  54. }
  55. [MethodImpl(MethodImplOptions.InternalCall)]
  56. private static extern SerializedDiff Internal_CreateDiff(IntPtr oldObj, object newObj);
  57. [MethodImpl(MethodImplOptions.InternalCall)]
  58. private static extern void Internal_ApplyDiff(IntPtr thisPtr, object obj);
  59. }
  60. /** @} */
  61. }