context: namespace SEModAPIInternal.API.Entity.Sector.SectorObject
  {
  	using System;
  	using System.Collections.Generic;
  	using System.ComponentModel;
  	using System.IO;
  	using System.Runtime.Serialization;
  	using Microsoft.Xml.Serialization.GeneratedAssembly;
  	using Sandbox.Common.ObjectBuilders;
  	using Sandbox.Definitions;
  	using SEModAPIInternal.API.Common;
  	using SEModAPIInternal.API.Entity.Sector.SectorObject.CubeGrid;
  	using SEModAPIInternal.API.Utility;
  	using SEModAPIInternal.Support;
  	using VRageMath;
  
  	[DataContract( Name = "CubeGridEntityProxy" )]
  	[KnownType( "KnownTypes" )]
  	public class CubeGridEntity : BaseEntity
  	{
  		#region "Attributes"
  
  		private readonly CubeBlockManager _cubeBlockManager;
  		private readonly CubeGridNetworkManager _networkManager;
  		private readonly CubeGridManagerManager _managerManager;
  
  		private static Type _internalType;
  		private string _name;
  		private DateTime _lastNameRefresh;
  
  		private CubeBlockEntity _cubeBlockToAddRemove;
  
  		public static string CubeGridNamespace = "";
  		public static string CubeGridClass = "Sandbox.Game.Entities.MyCubeGrid";
  
  		public static string CubeGridGetCubeBlocksHashSetMethod = "GetBlocks";
  		public static string CubeGridAddCubeBlockMethod = "AddCubeBlock";
  		public static string CubeGridRemoveCubeBlockMethod = "RemoveBlock";
  		public static string CubeGridGetManagerManagerMethod = "get_GridSystems";
  
  		//public static string CubeGridBlockGroupsField = "=Pzc3AyrJQWUIjBgew6TwHdil5t=";
  
  		//////////////////////////////////////////////////////////////
  
  		public static string CubeGridPackedCubeBlockClass = ".MyBlockBuildArea";
  
  		#endregion "Attributes"
  
  		#region "Constructors and Initializers"
  
  		protected CubeGridEntity( )
  			: base( new MyObjectBuilder_CubeGrid( ) )
  		{
  			_cubeBlockManager = new CubeBlockManager( this );
  			_lastNameRefresh = DateTime.Now;
  			_name = "Cube Grid";
  		}
  
  		public CubeGridEntity( FileInfo prefabFile )
  			: base( BaseObjectManager.LoadContentFile<MyObjectBuilder_CubeGrid, MyObjectBuilder_CubeGridSerializer>( prefabFile ) )
  		{
  			EntityId = 0;
  			ObjectBuilder.EntityId = 0;
  			if ( ObjectBuilder.PositionAndOrientation != null )
  				PositionAndOrientation = ObjectBuilder.PositionAndOrientation.GetValueOrDefault( );
  
  			_cubeBlockManager = new CubeBlockManager( this );
  			List<CubeBlockEntity> cubeBlockList = new List<CubeBlockEntity>( );
  			foreach ( MyObjectBuilder_CubeBlock cubeBlock in ObjectBuilder.CubeBlocks )
  			{
  				cubeBlock.EntityId = 0;
  				cubeBlockList.Add( new CubeBlockEntity( this, cubeBlock ) );
  			}
  			_cubeBlockManager.Load( cubeBlockList );
  
  			_lastNameRefresh = DateTime.Now;
  			_name = "Cube Grid";
  		}
  
  		public CubeGridEntity( MyObjectBuilder_CubeGrid definition )
  			: base( definition )
  		{
  			_cubeBlockManager = new CubeBlockManager( this );
  			List<CubeBlockEntity> cubeBlockList = new List<CubeBlockEntity>( );
  			foreach ( MyObjectBuilder_CubeBlock cubeBlock in definition.CubeBlocks )
  			{
  				cubeBlock.EntityId = 0;
  				cubeBlockList.Add( new CubeBlockEntity( this, cubeBlock ) );
  			}
  			_cubeBlockManager.Load( cubeBlockList );
  
  			_lastNameRefresh = DateTime.Now;
  			_name = "Cube Grid";
  		}
  
  		public CubeGridEntity( MyObjectBuilder_CubeGrid definition, Object backingObject )
  			: base( definition, backingObject )
  		{
  			_cubeBlockManager = new CubeBlockManager( this, backingObject, CubeGridGetCubeBlocksHashSetMethod );
  			_cubeBlockManager.Refresh( );
  
  			_networkManager = new CubeGridNetworkManager( this );
  			_managerManager = new CubeGridManagerManager( this, GetManagerManager( ) );
  
  			EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent
  			                                          {
  				                                          type = EntityEventManager.EntityEventType.OnCubeGridCreated,
  				                                          timestamp = DateTime.Now,
  				                                          entity = this,
  				                                          priority = 1
  			                                          };
  			EntityEventManager.Instance.AddEvent( newEvent );
  
  			_lastNameRefresh = DateTime.Now;
  			_name = "Cube Grid";
  		}
  
  		#endregion "Constructors and Initializers"
  
  		#region "Properties"
  
  		[IgnoreDataMember]
  		[Category( "Cube Grid" )]
  		[Browsable( false )]
  		[ReadOnly( true )]
  		new internal static Type InternalType
  		{
  			get
  			{
  				if ( _internalType == null )
  					_internalType = SandboxGameAssemblyWrapper.Instance.GetAssemblyType( CubeGridNamespace, CubeGridClass );
  				return _internalType;
  			}
  		}
  
  		[DataMember]
  		[Category( "Cube Grid" )]
  		[ReadOnly( true )]
  		public override string Name
  		{
  			get
  			{
  				string name = string.Empty;
  				TimeSpan timeSinceLastNameRefresh = DateTime.Now - _lastNameRefresh;
  				if ( timeSinceLastNameRefresh.TotalSeconds < 2 )
  				{
  					name = _name;
  				}
  				else
  				{
  					_lastNameRefresh = DateTime.Now;
  
  					List<MyObjectBuilder_CubeBlock> blocks = new List<MyObjectBuilder_CubeBlock>( ObjectBuilder.CubeBlocks );
  					foreach ( MyObjectBuilder_CubeBlock cubeBlock in blocks )
  					{
  						try
  						{
  							if ( cubeBlock == null )
  								continue;
  							if ( cubeBlock.TypeId != typeof ( MyObjectBuilder_Beacon ) )
  							{
  								continue;
  							}
  							if ( name.Length > 0 )
  								name += "|";
  
  							string customName = ( (MyObjectBuilder_Beacon)cubeBlock ).CustomName;
  							if ( customName == string.Empty )
  								customName = "Beacon";
  							name += customName;
  						}
  						catch ( Exception ex )
  						{
  							LogManager.ErrorLog.WriteLine( ex );
  						}
  					}
  				}
  
  				if ( name.Length == 0 )
  					name = DisplayName;
  
  				if ( name.Length == 0 )
  					name = ObjectBuilder.EntityId.ToString( );
  
  				_name = name;
  
  				return name;
  			}
  		}
  
  		[DataMember]
  		[Category( "Cube Grid" )]
  		[ReadOnly( true )]
  		public override string DisplayName
  		{
  			get { return ObjectBuilder.DisplayName; }
  			set
  			{
  				if ( ObjectBuilder.DisplayName == value ) return;
  				ObjectBuilder.DisplayName = value;
  				Changed = true;
  
  				base.DisplayName = value;
  			}
  		}
  
  		[IgnoreDataMember]
  		[Category( "Cube Grid" )]
  		[Browsable( false )]
  		[ReadOnly( true )]
  		internal new MyObjectBuilder_CubeGrid ObjectBuilder
  		{
  			get
  			{
  				MyObjectBuilder_CubeGrid objectBuilder = (MyObjectBuilder_CubeGrid)base.ObjectBuilder;
  				if ( objectBuilder == null )
  				{
  
gt: 					objectBuilder = new MyObjectBuilder_CubeGrid( );