Imports System.Reflection
Namespace SimpleHistory
'''
''' Represent some Object States and Actions.
'''
Public Class HistoryPoint
'''
''' Represents the Name of this History Point
'''
'''
Public Property Name As String = ""
'''
''' A List of Object States and Actions.
'''
'''
Public ReadOnly Property Entries As New List(Of ObjectBase)
'''
''' Some data can be refered on this HistoryPoint. Don't know, in some situations this can be helpful.
'''
Public ReadOnly Tag As Object = Nothing
Public Function HasEntries(Of T As ObjectBase)() As Boolean
Return Entries.Where(Function(n) TypeOf n Is T).Count > 0
End Function
Friend Sub Undo()
For Each s As ObjectBase In Entries.OrderBy(Function(n) n.UndoPriority)
If TypeOf s Is ObjectState Then
CType(s, ObjectState).Patch()
ElseIf TypeOf s Is ObjectAction Then
CType(s, ObjectAction).Undo()
End If
Next
End Sub
Friend Sub Redo()
For Each s As ObjectBase In Entries.OrderBy(Function(n) n.RedoPriority)
If TypeOf s Is ObjectState Then
CType(s, ObjectState).Patch()
ElseIf TypeOf s Is ObjectAction Then
CType(s, ObjectAction).Redo()
End If
Next
End Sub
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify which members to include.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object(), whiteList As MemberWhiteList) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(whiteList), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify which members to exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object(), blackList As MemberBlackList) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(blackList), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' The member names to include.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object(), ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, True, memberName)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' If true, the memberName-Array has member names that should be included.
''' The member names to include/exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object(), isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberWhiteList(memberName)), BindingFlags.Default)
Else
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberBlackList(memberName)), BindingFlags.Default)
End If
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify what member types to include.
''' The member names to include.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object(), membersToStore As ObjectValueType, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, membersToStore, True, memberName)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify what member types to include.
''' If true, the memberName-Array has member names that should be included.
''' The member names to include/exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object(), membersToStore As ObjectValueType, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, membersToStore, CObj(New MemberWhiteList(memberName)), BindingFlags.Default)
Else
Return FromObject({obj}, membersToStore, CObj(New MemberBlackList(memberName)), BindingFlags.Default)
End If
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' The Binding Flags that the members should have.
''' The member names to include.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object(), flags As BindingFlags, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, flags, True, memberName)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' The Binding Flags that the members should have.
''' If true, the memberName-Array has member names that should be included.
''' The member names to include/exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object(), flags As BindingFlags, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberWhiteList(memberName)), flags)
Else
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberBlackList(memberName)), flags)
End If
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify what member types to include.
''' The Binding Flags that the members should have.
''' The member names to include.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object(), membersToStore As ObjectValueType, flags As BindingFlags, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, flags, True, memberName)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify what member types to include.
''' The Binding Flags that the members should have.
''' If true, the memberName-Array has member names that should be included.
''' The member names to include/exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object(), membersToStore As ObjectValueType, flags As BindingFlags, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, membersToStore, CObj(New MemberWhiteList(memberName)), flags)
Else
Return FromObject({obj}, membersToStore, CObj(New MemberBlackList(memberName)), flags)
End If
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify which members to include.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, whiteList As MemberWhiteList) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(whiteList), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify which members to exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, blackList As MemberBlackList) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(blackList), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' The member names to include/exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, True, memberName)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' If true, the memberName-Array has member names that should be included.
''' The member names to include/exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberWhiteList(memberName)), BindingFlags.Default)
Else
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberBlackList(memberName)), BindingFlags.Default)
End If
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify what member types to include.
''' The member names to include.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, membersToStore, True, memberName)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify what member types to include.
''' If true, the memberName-Array has member names that should be included.
''' The member names to include/exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, membersToStore, CObj(New MemberWhiteList(memberName)), BindingFlags.Default)
Else
Return FromObject({obj}, membersToStore, CObj(New MemberBlackList(memberName)), BindingFlags.Default)
End If
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' The Binding Flags that the members should have.
''' The member names to include.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, flags As BindingFlags, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, flags, True, memberName)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' The Binding Flags that the members should have.
''' If true, the memberName-Array has member names that should be included.
''' The member names to include/exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, flags As BindingFlags, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberWhiteList(memberName)), flags)
Else
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberBlackList(memberName)), flags)
End If
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify what member types to include.
''' The Binding Flags that the members should have.
''' The member names to include.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, flags As BindingFlags, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, flags, True, memberName)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify what member types to include.
''' The Binding Flags that the members should have.
''' If true, the memberName-Array has member names that should be included.
''' The member names to include/exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, flags As BindingFlags, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, membersToStore, CObj(New MemberWhiteList(memberName)), flags)
Else
Return FromObject({obj}, membersToStore, CObj(New MemberBlackList(memberName)), flags)
End If
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(Nothing), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify what member types to include.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(Nothing), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify what member types to include.
''' Specify which members to include.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, whiteList As MemberWhiteList) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(whiteList), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify what member types to include.
''' Specify which members to exclude.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, blackList As MemberBlackList) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(blackList), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' The Binding Flags that the members should have.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, flags As BindingFlags) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(Nothing), flags)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify what member types to include.
''' The Binding Flags that the members should have.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, flags As BindingFlags) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(Nothing), flags)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify what member types to include.
''' Specify which members to include.
''' The Binding Flags that the members should have.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, whiteList As MemberWhiteList, flags As BindingFlags) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(whiteList), flags)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The object that should be included.
''' Specify what member types to include.
''' Specify which members to exclude.
''' The Binding Flags that the members should have.
''' A History Point with Object States.
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, blackList As MemberBlackList, flags As BindingFlags) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(blackList), flags)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' A History Point with Object States.
Public Shared Function FromObject(objs As Object()) As HistoryPoint
Return FromObject(objs, ObjectValueType.None, CObj(Nothing), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify what member types to include.
''' A History Point with Object States.
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(Nothing), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify what member types to include.
''' Specify which members to include.
''' A History Point with Object States.
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, whiteList As MemberWhiteList) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(whiteList), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify what member types to include.
''' Specify which members to exclude.
''' A History Point with Object States.
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, blackList As MemberBlackList) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(blackList), BindingFlags.Default)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' The Binding Flags that the members should have.
''' A History Point with Object States.
Public Shared Function FromObject(objs As Object(), flags As BindingFlags) As HistoryPoint
Return FromObject(objs, ObjectValueType.None, CObj(Nothing), flags)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify what member types to include.
''' The Binding Flags that the members should have.
''' A History Point with Object States.
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, flags As BindingFlags) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(Nothing), flags)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify what member types to include.
''' Specify which members to include.
''' The Binding Flags that the members should have.
''' A History Point with Object States.
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, whiteList As MemberWhiteList, flags As BindingFlags) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(whiteList), flags)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify what member types to include.
''' Specify which members to exclude.
''' The Binding Flags that the members should have.
''' A History Point with Object States.
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, blackList As MemberBlackList, flags As BindingFlags) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(blackList), flags)
End Function
'''
''' Creates an History Point with Object States automaticly from input.
'''
''' The objects that should be included.
''' Specify what member types to include.
''' Specify which members to include.
''' The Binding Flags that the members should have.
''' A History Point with Object States.
Private Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, whiteOrBlackList As Object, flags As BindingFlags) As HistoryPoint
Dim hp As New HistoryPoint
If whiteOrBlackList Is Nothing Then whiteOrBlackList = New MemberBlackList
Dim isWhiteList As Boolean = TypeOf whiteOrBlackList Is MemberWhiteList
If flags = BindingFlags.Default Then
flags = BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic
End If
If membersToStore = ObjectValueType.None Then
membersToStore = ObjectValueType.Field Or ObjectValueType.Property
End If
For Each obj As Object In objs
If (membersToStore And ObjectValueType.Field) = ObjectValueType.Field Then
For Each fi As FieldInfo In obj.GetType.GetFields(flags)
Dim contains As Boolean = CType(whiteOrBlackList, List(Of String)).Contains(fi.Name)
If If(isWhiteList, contains, Not contains) Then
Dim os As New ObjectState
os.Object = obj
os.MemberName = fi.Name
os.MemberType = ObjectValueType.Field
os.MemberFlags = flags
os.ValueToPatch = fi.GetValue(obj)
hp.Entries.Add(os)
End If
Next
End If
If (membersToStore And ObjectValueType.Property) = ObjectValueType.Property Then
For Each pi As PropertyInfo In obj.GetType.GetProperties(flags)
Dim contains As Boolean = CType(whiteOrBlackList, List(Of String)).Contains(pi.Name)
If If(isWhiteList, contains, Not contains) Then
Dim os As New ObjectState
os.Object = obj
os.MemberName = pi.Name
os.MemberType = ObjectValueType.Property
os.MemberFlags = flags
os.ValueToPatch = pi.GetValue(obj)
hp.Entries.Add(os)
End If
Next
End If
Next
Return hp
End Function
'''
''' Combines some History Points to one.
'''
''' An array of History Points to combine.
''' One History Point that contains all Data of inputted History Points.
Public Shared Function Concat(ParamArray hps As HistoryPoint()) As HistoryPoint
Return Concat(hps.FirstOrDefault?.Name, hps)
End Function
'''
''' Combines some History Points to one.
'''
''' An array of History Points to combine.
''' One History Point that contains all Data of inputted History Points.
Public Shared Function Concat(newName As String, ParamArray hps As HistoryPoint()) As HistoryPoint
Dim hp As New HistoryPoint
For Each _hp As HistoryPoint In hps
hp.Entries.AddRange(_hp.Entries)
Next
Return hp
End Function
End Class
End Namespace