SerializedDiff.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /// 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. /// Returns true if the diff doesn't contain any changes between the two objects.
  18. /// </summary>
  19. public bool IsEmpty => Internal_IsEmpty(mCachedPtr);
  20. /// <summary>
  21. /// Creates a difference of properties that are different in <paramref name="newObj"/> compared to
  22. /// <paramref name="oldObj"/>. The difference can later be applied to <paramref name="oldObj"/> to restore it to
  23. /// the same state as <paramref name="newObj"/> .
  24. /// </summary>
  25. /// <param name="oldObj">Object to compare from.</param>
  26. /// <param name="newObj">Object to compare to.</param>
  27. /// <returns>A set of differences between the two objects.</returns>
  28. public static SerializedDiff Create(object oldObj, object newObj)
  29. {
  30. if (oldObj == null || newObj == null)
  31. return null;
  32. SerializedObject serializedOldObj = SerializedObject.Create(oldObj);
  33. SerializedObject serializedNewObj = SerializedObject.Create(newObj);
  34. return Create(serializedOldObj, serializedNewObj);
  35. }
  36. /// <summary>
  37. /// Creates a difference of properties that are different in <paramref name="newObj"/> compared to
  38. /// <paramref name="oldObj"/>. The difference can later be applied to <paramref name="oldObj"/> to restore it to
  39. /// the same state as <paramref name="newObj"/> .
  40. /// </summary>
  41. /// <param name="oldObj">Object to compare from.</param>
  42. /// <param name="newObj">Object to compare to.</param>
  43. /// <returns>A set of differences between the two objects.</returns>
  44. public static SerializedDiff Create(SerializedObject oldObj, object newObj)
  45. {
  46. if (newObj == null)
  47. return null;
  48. SerializedObject serializedNewObj = SerializedObject.Create(newObj);
  49. return Create(oldObj, serializedNewObj);
  50. }
  51. /// <summary>
  52. /// Creates a difference of properties that are different in <paramref name="newObj"/> compared to
  53. /// <paramref name="oldObj"/>. The difference can later be applied to <paramref name="oldObj"/> to restore it to
  54. /// the same state as <paramref name="newObj"/> .
  55. /// </summary>
  56. /// <param name="oldObj">Object to compare from.</param>
  57. /// <param name="newObj">Object to compare to.</param>
  58. /// <returns>A set of differences between the two objects.</returns>
  59. public static SerializedDiff Create(object oldObj, SerializedObject newObj)
  60. {
  61. if (oldObj == null)
  62. return null;
  63. SerializedObject serializedOldObj = SerializedObject.Create(oldObj);
  64. return Create(serializedOldObj, newObj);
  65. }
  66. /// <summary>
  67. /// Creates a difference of properties that are different in <paramref name="newObj"/> compared to
  68. /// <paramref name="oldObj"/>. The difference can later be applied to <paramref name="oldObj"/> to restore it to
  69. /// the same state as <paramref name="newObj"/> .
  70. /// </summary>
  71. /// <param name="oldObj">Object to compare from.</param>
  72. /// <param name="newObj">Object to compare to.</param>
  73. /// <returns>A set of differences between the two objects.</returns>
  74. public static SerializedDiff Create(SerializedObject oldObj, SerializedObject newObj)
  75. {
  76. if (oldObj == null || newObj == null)
  77. return null;
  78. IntPtr oldObjPtr = oldObj.GetCachedPtr();
  79. IntPtr newObjPtr = newObj.GetCachedPtr();
  80. return Internal_CreateDiff(oldObjPtr, newObjPtr);
  81. }
  82. /// <summary>
  83. /// Applies difference stored in this object to the provided object. The type of the object must be the same as the
  84. /// type of objects the difference was generated from.
  85. /// </summary>
  86. /// <param name="obj">Object to apply the difference to.</param>
  87. public void Apply(object obj)
  88. {
  89. if (obj == null)
  90. return;
  91. Internal_ApplyDiff(mCachedPtr, obj);
  92. }
  93. [MethodImpl(MethodImplOptions.InternalCall)]
  94. private static extern SerializedDiff Internal_CreateDiff(IntPtr oldObj, IntPtr newObj);
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. private static extern void Internal_ApplyDiff(IntPtr thisPtr, object obj);
  97. [MethodImpl(MethodImplOptions.InternalCall)]
  98. private static extern bool Internal_IsEmpty(IntPtr thisPtr);
  99. }
  100. /** @} */
  101. }