I'm trying to implement a way to clone elements in my application. These elements are custom and designed by me (and so far, they aren't really that complicated). Each item as a property called Children
that has many more instances listed inside of it (imagine it as a tree view with multiple sub branches).
I have an object called "Environment" that contained, among other things, a reference to all of these children. Think of it like a orchard that holds all of my trees. When I want to copy everything, I want to be able to just call "clone" on my "Environment" class and have it give me a copy of everything.
It all worked for a while, but after some changes and code refactoring, it doesn't seem to work anymore and I have no clue as to why. I can follow a stack trace pretty well, but in this case that isn't possible. Here is a screen capture of my error.
I will also be using serialization for the actual saving my my "Environment" to a file. Before I can do that, however, I have to get serialization in general to work.
Anyways, I've checked my individual tree/branch/limb objects individually and they all clone successfully. The only thing I can't seem to get to clone is the "Environment" class (my orchard). Here is the source of my "Environment" class:
Imports gE.gEngine
Imports LuaInterface
Imports gE.gEngine.Generic
Imports Microsoft.Xna.Framework
Imports System.Runtime.Serialization
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports gE.gEngine.WorldObjects
Imports System.Windows.Forms
Imports Microsoft.Xna.Framework.Input
Imports System.Threading.Tasks
Imports System.Threading
<Serializable()> _
Public Class Environment
Public World As WorldServices.World
Public Players As WorldServices.Players
Public Lighting As WorldServices.Lighting
Public Environment As WorldServices.Environment
Public GenericsLibrary As WorldServices.GenericsLibrary
<NonSerialized()> _
Public Lua As New LuaInterface.Lua
<NonSerialized()> _
Public SafetyContext As CommonEnum.SafetyContext = CommonEnum.SafetyContext.Safe
Public Event EnvironmentChanged(ByVal sender As Object, ByVal e As GenericEventArgument)
Public Sub call_EnvironmentChanged(ByVal sender As Object, ByVal e As GenericEventArgument)
RaiseEvent EnvironmentChanged(sender, e)
End Sub
Public Sub New()
GlobalShare.Environment = Me
InitializeLua()
Lighting = New WorldServices.Lighting()
World = New WorldServices.World()
Players = New WorldServices.Players()
Environment = New WorldServices.Environment()
GenericsLibrary = New WorldServices.GenericsLibrary()
Dim ConstraintesGroup As New WorldObjects.Group With {
.Name = "Constraintes",
.Parent = GenericsLibrary
}
Dim ordsc As List(Of gEngine.WorldObjects.Lua) = Environment.GetScripts()
ordsc.Sort(Function(x, y) x.RunIndex.CompareTo(y.RunIndex))
For Each script As gEngine.WorldObjects.Lua In ordsc
GlobalShare.Environment.RunScript(script.Source, script.Name)
Next
End Sub
Public Sub SetSafetyContext(ByVal e As CommonEnum.SafetyContext)
If e <> CommonEnum.SafetyContext.Unlocked Then
SafetyContext = e
Exit Sub
End If
Throw New Exception("Attempt to unlock safety context from unsafe Lua script!")
End Sub
Public Sub InitializeLua()
Lua("Game") = GlobalShare.Game
Lua("__Environment") = Me
Lua("gel") = New gELua(Me)
Lua.RegisterFunction("Print", Lua("gel"), Lua("gel").GetType().GetMethod("Print"))
Lua.RegisterFunction("print", Lua("gel"), Lua("gel").GetType().GetMethod("Print"))
Lua.RegisterFunction("SetSafetyContext", Lua("gel"), Me.GetType().GetMethod("SetSafetyContext"))
Lua("Vector2") = New Lua_.Lua_Vector2
Lua("Vector3") = New Lua_.Lua_Vector3
Lua("Mouse") = New Lua_.Lua_Mouse
Lua.NewTable("Helpers")
Lua("Helpers.EnumHelper") = New Helpers.EnumHelper
Dim GameTimer As New Stopwatch
Lua("Time") = GameTimer
GameTimer.Start()
Lua.NewTable("__X")
End Sub
Public Function Clone() As Environment
If [Object].ReferenceEquals(Me, Nothing) Then
Return Nothing
End If
Dim formatter As IFormatter = New BinaryFormatter()
Dim stream As IO.Stream = New MemoryStream()
Using stream
formatter.Serialize(stream, Me)
stream.Seek(0, SeekOrigin.Begin)
Return DirectCast(formatter.Deserialize(stream), [Environment])
End Using
End Function
Public Sub RunScript(ByVal scr As String, ByVal name As String)
Try
Lua.DoString(scr, name)
Catch ex As Exception
Game.CallLogEvent(Me, New GenericEventArgument("LogEvent", "Error", ex.Message))
End Try
End Sub
End Class
The code is a bit messy because I haven't messed with this particular class very much because of this particular issue (I want to know why it won't serialize before I continue to change it around - I don't want to cause any more issues in the meantime).
According to the error, it appears as if it's trying to serialize my applications window (StudioWindow), however, I can't find any references to it in my application.
So I'm not really asking for you guys to find the reference for me (for all I know, it could be hidden deep in some other parts of my project), but I would love to find a way to narrow down the issue. I've been commenting parts out and trying to eliminate certain parts of my project as being the problem, but it's kind of hard because some aspects of my application won't run without other parts running.
Any help is appreciated. If you would like to know more information, just ask. I can probably get you any information you ask for. My code is mostly VB.NET (although, it's a hybrid project so many other parts are in C#) so any .NET examples/solutions/help are very much appreciated and equally helpful (I can read both so don't hesitate).