Skip to content

Commit

Permalink
-Cleaned up unused type converters
Browse files Browse the repository at this point in the history
-Added resource lock to ChatManager.ChatHistory
-Fixed dead-locking bug with GameEntityManager
-Converted Id and Definition properties to use built-in definitions manager and types
-Improved inventory performance by removing old API code and using new Id and Definition properties
  • Loading branch information
chessmaster42 committed Sep 1, 2014
1 parent 8999b4e commit cb9c42b
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 359 deletions.
4 changes: 4 additions & 0 deletions SEConfigTool/SEConfigTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\GameLibraries\Sandbox.Common.XmlSerializers.dll</HintPath>
</Reference>
<Reference Include="Sandbox.Game">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\GameLibraries\Sandbox.Game.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down
156 changes: 0 additions & 156 deletions SEModAPI/API/TypeConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,160 +103,4 @@ public override object CreateInstance(ITypeDescriptorContext context, System.Col
return new Vector3I((int)propertyValues["X"], (int)propertyValues["Y"], (int)propertyValues["Z"]);
}
}

public class ItemSerializableDefinitionIdTypeConverter : TypeConverter
{
private static PhysicalItemDefinitionsManager m_physicalItemsManager;
private static ComponentDefinitionsManager m_componentsManager;
private static AmmoMagazinesDefinitionsManager m_ammoManager;

private static List<SerializableDefinitionId> m_idList;

public ItemSerializableDefinitionIdTypeConverter()
{
//Load up the static item managers
if (m_physicalItemsManager == null)
{
m_physicalItemsManager = new PhysicalItemDefinitionsManager();
m_physicalItemsManager.Load(PhysicalItemDefinitionsManager.GetContentDataFile("PhysicalItems.sbc"));
}
if (m_componentsManager == null)
{
m_componentsManager = new ComponentDefinitionsManager();
m_componentsManager.Load(ComponentDefinitionsManager.GetContentDataFile("Components.sbc"));
}
if (m_ammoManager == null)
{
m_ammoManager = new AmmoMagazinesDefinitionsManager();
m_ammoManager.Load(AmmoMagazinesDefinitionsManager.GetContentDataFile("AmmoMagazines.sbc"));
}

//Populate the static list with the ids from the items
if (m_idList == null)
{
m_idList = new List<SerializableDefinitionId>();
foreach (var def in m_physicalItemsManager.Definitions)
{
m_idList.Add(def.Id);
}
foreach (var def in m_componentsManager.Definitions)
{
m_idList.Add(def.Id);
}
foreach (var def in m_ammoManager.Definitions)
{
m_idList.Add(def.Id);
}
}
}

public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}

public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(m_idList);
}

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{/*
if (value.GetType() == typeof(string))
{
string source = (string)value;
source = source.Replace(" ", "");
string[] sourceParts = source.Split('/');
if (sourceParts.Length == 2)
{
MyObjectBuilderTypeEnum typeId;
if (Enum.TryParse<MyObjectBuilderTypeEnum>(sourceParts[0], out typeId))
{
SerializableDefinitionId result = new SerializableDefinitionId(typeId, sourceParts[1]);
return result;
}
}
}
*/
return base.ConvertFrom(context, culture, value);
}
}

public class ObjectSerializableDefinitionIdTypeConverter : TypeConverter
{
private static CubeBlockDefinitionsManager m_blocksManager;
private static List<SerializableDefinitionId> m_idList;

public ObjectSerializableDefinitionIdTypeConverter()
{
//Load up the static item managers
if (m_blocksManager == null)
{
m_blocksManager = new CubeBlockDefinitionsManager();
m_blocksManager.Load(CubeBlockDefinitionsManager.GetContentDataFile("CubeBlocks.sbc"));
}

//Populate the static list with the ids from the items
if (m_idList == null)
{
m_idList = new List<SerializableDefinitionId>();
foreach (var def in m_blocksManager.Definitions)
{
m_idList.Add(def.Id);
}

//m_idList.Add(new SerializableDefinitionId(MyObjectBuilderTypeEnum.CubeGrid, ""));
//m_idList.Add(new SerializableDefinitionId(MyObjectBuilderTypeEnum.VoxelMap, ""));
//m_idList.Add(new SerializableDefinitionId(MyObjectBuilderTypeEnum.FloatingObject, ""));
//m_idList.Add(new SerializableDefinitionId(MyObjectBuilderTypeEnum.Meteor, ""));
}
}

public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}

public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(m_idList);
}

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{/*
if (value.GetType() == typeof(string))
{
string source = (string)value;
source = source.Replace(" ", "");
string[] sourceParts = source.Split('/');
if (sourceParts.Length == 2)
{
MyObjectBuilderTypeEnum typeId;
if (Enum.TryParse<MyObjectBuilderTypeEnum>(sourceParts[0], out typeId))
{
SerializableDefinitionId result = new SerializableDefinitionId(typeId, sourceParts[1]);
return result;
}
}
}
*/
return base.ConvertFrom(context, culture, value);
}
}
}
30 changes: 18 additions & 12 deletions SEModAPIExtensions/API/ChatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,26 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;

using Sandbox.Common.ObjectBuilders;

using SEModAPI.API.Definitions;

using SEModAPIExtensions.API.IPC;

using SEModAPIInternal.API.Server;
using SEModAPIInternal.API.Common;
using SEModAPIInternal.API.Entity;
using SEModAPIInternal.API.Entity.Sector.SectorObject;
using SEModAPIInternal.API.Entity.Sector.SectorObject.CubeGrid;
using SEModAPIInternal.API.Entity.Sector.SectorObject.CubeGrid.CubeBlock;
using SEModAPIInternal.API.Utility;
using SEModAPIInternal.Support;

using VRageMath;
using VRage;
using VRage.Common.Utils;
using System.Threading;
using VRageMath;

namespace SEModAPIExtensions.API
{
Expand Down Expand Up @@ -102,6 +94,7 @@ public struct ChatEvent
private static List<string> m_chatMessages;
private static List<ChatEvent> m_chatHistory;
private static bool m_chatHandlerSetup;
private static FastResourceLock m_resourceLock;

private List<ChatEvent> m_chatEvents;
private Dictionary<ChatCommand, Guid> m_chatCommands;
Expand All @@ -124,6 +117,7 @@ protected ChatManager()
m_chatMessages = new List<string>();
m_chatHistory = new List<ChatEvent>();
m_chatHandlerSetup = false;
m_resourceLock = new FastResourceLock();
m_chatEvents = new List<ChatEvent>();
m_chatCommands = new Dictionary<ChatCommand, Guid>();

Expand Down Expand Up @@ -289,7 +283,13 @@ public List<ChatEvent> ChatHistory
}
}

return m_chatHistory;
m_resourceLock.AcquireShared();

List<ChatEvent> history = new List<ChatEvent>(m_chatHistory);

m_resourceLock.ReleaseShared();

return history;
}
}

Expand Down Expand Up @@ -390,7 +390,9 @@ protected void ReceiveChatMessage(ulong remoteUserId, string message, ChatEntryT
chatEvent.priority = 0;
ChatManager.Instance.AddEvent(chatEvent);

m_resourceLock.AcquireExclusive();
m_chatHistory.Add(chatEvent);
m_resourceLock.ReleaseExclusive();
}

public void SendPrivateChatMessage(ulong remoteUserId, string message)
Expand Down Expand Up @@ -421,7 +423,9 @@ public void SendPrivateChatMessage(ulong remoteUserId, string message)
chatEvent.priority = 0;
ChatManager.Instance.AddEvent(chatEvent);

m_resourceLock.AcquireExclusive();
m_chatHistory.Add(chatEvent);
m_resourceLock.ReleaseExclusive();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -471,7 +475,9 @@ public void SendPublicChatMessage(string message)
selfChatEvent.priority = 0;
ChatManager.Instance.AddEvent(selfChatEvent);

m_resourceLock.AcquireExclusive();
m_chatHistory.Add(selfChatEvent);
m_resourceLock.ReleaseExclusive();
}
catch (Exception ex)
{
Expand Down
8 changes: 4 additions & 4 deletions SEModAPIInternal/API/Common/GameEntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,26 @@ public static BaseObject GetEntity(long entityId)

internal static void AddEntity(long entityId, BaseObject entity)
{
m_resourceLock.AcquireExclusive();
//m_resourceLock.AcquireExclusive();

if (m_entityMap.ContainsKey(entityId))
return;

m_entityMap.Add(entityId, entity);

m_resourceLock.ReleaseExclusive();
//m_resourceLock.ReleaseExclusive();
}

internal static void RemoveEntity(long entityId)
{
m_resourceLock.AcquireExclusive();
//m_resourceLock.AcquireExclusive();

if (!m_entityMap.ContainsKey(entityId))
return;

m_entityMap.Remove(entityId);

m_resourceLock.ReleaseExclusive();
//m_resourceLock.ReleaseExclusive();
}

internal static Object GetGameEntity(long entityId, Type entityType)
Expand Down
Loading

0 comments on commit cb9c42b

Please sign in to comment.