浏览代码

Refactor : Messages as integer keys (Performance improvement)

/main
GitHub 3 年前
当前提交
44b44345
共有 7 个文件被更改,包括 75 次插入35 次删除
  1. 6
      Editor/IngredientsExplorer/IngredientsExplorerWindow.cs
  2. 51
      Runtime/Ingredients/Messager/Messager.cs
  3. 8
      Runtime/Ingredients/Messager/Playables/SendMessage/SendMessageBehaviour.cs
  4. 4
      Runtime/LevelScripting/Actions/GameManagerSendStartupMessageAction.cs
  5. 17
      Runtime/LevelScripting/Actions/SendMessageAction.cs
  6. 16
      Runtime/LevelScripting/Events/OnGameManagerLevelStart.cs
  7. 8
      Runtime/Managers/Implementations/GameManager.cs

6
Editor/IngredientsExplorer/IngredientsExplorerWindow.cs


if (!stack.Contains(msg))
{
stack.Push(msg);
var rootNode = new CallTreeNode(msg, CallTreeNodeType.Message, $"{msg.MessageToSend} : ({msg.gameObject.name}.{msg.Name})");
var all = Resources.FindObjectsOfTypeAll<OnMessageEvent>().Where(o=> o.MessageName == msg.MessageToSend).ToList();
var rootNode = new CallTreeNode(msg, CallTreeNodeType.Message, $"{msg.message} : ({msg.gameObject.name}.{msg.Name})");
var all = Resources.FindObjectsOfTypeAll<OnMessageEvent>().Where(o=> o.MessageName == msg.message).ToList();
foreach(var evt in all)
{

}
else
{
return new CallTreeNode(msg, GetType(msg), $"RECURSED :{msg.MessageToSend} : ({msg.gameObject.name}.{msg.Name})");
return new CallTreeNode(msg, GetType(msg), $"RECURSED :{msg.message} : ({msg.gameObject.name}.{msg.Name})");
}
}

51
Runtime/Ingredients/Messager/Messager.cs


{
public delegate void Message(GameObject instigator = null);
private static Dictionary<string, List<Message>> m_RegisteredMessages;
private static Dictionary<int, List<Message>> m_RegisteredMessages;
m_RegisteredMessages = new Dictionary<string, List<Message>>();
m_RegisteredMessages = new Dictionary<int, List<Message>>();
if (!m_RegisteredMessages.ContainsKey(messageName))
m_RegisteredMessages.Add(messageName, new List<Message>());
int messageID = Shader.PropertyToID(messageName);
RegisterMessage(messageID, message);
}
if (!m_RegisteredMessages[messageName].Contains(message))
m_RegisteredMessages[messageName].Add(message);
public static void RegisterMessage(int messageID, Message message)
{
if (!m_RegisteredMessages.ContainsKey(messageID))
m_RegisteredMessages.Add(messageID, new List<Message>());
if (!m_RegisteredMessages[messageID].Contains(message))
m_RegisteredMessages[messageID].Add(message);
Debug.LogWarning(string.Format("Messager : {0} entry already contains reference to message.", messageName));
Debug.LogError(string.Format("Messager : {0} entry already contains reference to message."));
var currentEvent = m_RegisteredMessages[messageName];
int messageID = Shader.PropertyToID(messageName);
RemoveMessage(messageID, message);
}
public static void RemoveMessage(int messageID, Message message)
{
var currentEvent = m_RegisteredMessages[messageID];
m_RegisteredMessages.Remove(messageName);
m_RegisteredMessages.Remove(messageID);
}
public static void Send(string messageName, GameObject instigator = null)
{
int messageID = Shader.PropertyToID(messageName);
Send(messageID, instigator);
public static void Send(string eventName, GameObject instigator = null)
public static void Send(int messageID, GameObject instigator = null)
if(GameplayIngredientsSettings.currentSettings.verboseCalls)
Debug.Log(string.Format("[MessageManager] Broadcast: {0}", eventName));
if (GameplayIngredientsSettings.currentSettings.verboseCalls)
Debug.Log(string.Format("[MessageManager] Broadcast: {0}", messageID));
if (m_RegisteredMessages.ContainsKey(eventName))
if (m_RegisteredMessages.ContainsKey(messageID))
var messages = m_RegisteredMessages[eventName].ToArray();
var messages = m_RegisteredMessages[messageID].ToArray();
foreach (var message in messages)
{
if(message != null)

catch (Exception e)
{
Debug.LogError(string.Format("Messager : Caught {0} while sending Message {1}", e.GetType().Name, eventName));
Debug.LogError(string.Format("Messager : Caught {0} while sending Message {1}", e.GetType().Name, messageID));
Debug.LogException(e);
}
}

Debug.Log("[MessageManager] could not find any listeners for event : " + eventName);
Debug.Log("[MessageManager] could not find any listeners for event : " + messageID);
}
}
}

8
Runtime/Ingredients/Messager/Playables/SendMessage/SendMessageBehaviour.cs


public class SendMessageBehaviour : PlayableBehaviour
{
public string StartMessage;
public int _messageID = int.MinValue;
if(StartMessage != "" )
if(StartMessage != "")
if (_messageID == int.MinValue)
_messageID = Shader.PropertyToID(StartMessage);
Messager.Send(StartMessage, Instigator);
Messager.Send(_messageID, Instigator);
else
Debug.Log("[SendMessageBehaviour] Would have sent broadcast message : '" + StartMessage + "'");
}

4
Runtime/LevelScripting/Actions/GameManagerSendStartupMessageAction.cs


switch(messageType)
{
case MessageType.GameLevelStart:
Messager.Send(GameManager.GameLevelStartMessage);
Messager.Send(GameManager.GameLevelStartMessageID);
Messager.Send(GameManager.MainMenuStartMessage);
Messager.Send(GameManager.MainMenuStartMessageID);
break;
}
}

17
Runtime/LevelScripting/Actions/SendMessageAction.cs


[Callable("Game", "Actions/ic-action-message.png")]
public class SendMessageAction : ActionBase
{
public string MessageToSend = "Message";
public string message => MessageToSend;
[SerializeField]
string MessageToSend = "Message";
int _messageID = int.MinValue;
Messager.Send(MessageToSend, instigator);
if (_messageID == int.MinValue)
_messageID = Shader.PropertyToID(MessageToSend);
Messager.Send(_messageID, instigator);
}
public void SetMessageName(string messageName)
{
MessageToSend = messageName;
_messageID = Shader.PropertyToID(MessageToSend);
}
public override string GetDefaultName()

16
Runtime/LevelScripting/Events/OnGameManagerLevelStart.cs


[SerializeField]
protected GameManagerLevelType m_LevelType = GameManagerLevelType.GameLevel;
string m_Message;
int m_MessageID;
m_Message = GetMessage(m_LevelType);
Messager.RegisterMessage(m_Message, Execute);
m_MessageID = GetMessageID(m_LevelType);
Messager.RegisterMessage(m_MessageID, Execute);
Messager.RemoveMessage(m_Message, Execute);
Messager.RemoveMessage(m_MessageID, Execute);
static string GetMessage(GameManagerLevelType type)
static int GetMessageID(GameManagerLevelType type)
case GameManagerLevelType.MainMenu: return GameManager.MainMenuStartMessage;
case GameManagerLevelType.MainMenu: return GameManager.MainMenuStartMessageID;
case GameManagerLevelType.GameLevel: return GameManager.GameLevelStartMessage;
case GameManagerLevelType.GameLevel: return GameManager.GameLevelStartMessageID;
}
}

}
catch (System.Exception e)
{
UnityEngine.Debug.LogError(string.Format("OnMessageEvent : Exception Caught while catching message '{0}' on Object '{1}'", m_Message, gameObject.name));
UnityEngine.Debug.LogError(string.Format("OnMessageEvent : Exception Caught while catching message '{0}' on Object '{1}'", m_MessageID, gameObject.name));
UnityEngine.Debug.LogException(e);
}
}

8
Runtime/Managers/Implementations/GameManager.cs


public string ProgressSaveName = "Progress";
public static string MainMenuStartMessage = "GAME_MANAGER_MAINMENU_START";
public static string GameLevelStartMessage = "GAME_MANAGER_GAME_START";
public static string GameLevelStartMessage ="GAME_MANAGER_GAME_START";
public static int MainMenuStartMessageID = Shader.PropertyToID(MainMenuStartMessage);
public static int GameLevelStartMessageID = Shader.PropertyToID(GameLevelStartMessage);
public int currentLevel { get; private set; } = -2;

string message = targetLevel == -1 ? MainMenuStartMessage : GameLevelStartMessage;
sendMessage.Name = $"Send {message}";
sendMessage.MessageToSend = message;
sendMessage.SetMessageName(message);
destroy.ObjectsToDestroy = new GameObject[] { go };

正在加载...
取消
保存