using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

namespace Networking
{

    public static class ExtensionMethods
    {

        public static bool TryRead<T>(this NetworkMessage msg, out T outMsg, bool logError = true) where T : MessageBase, new()
        {
            try
            {
                outMsg = msg.ReadMessage<T>();
                return true;
            }
            catch
            {
                outMsg = default(T);
                if (logError)
                    Debug.LogError("Recieved unknown NetworkMessage. Expected typeof " + typeof(T).Name);
                return false;
            }
        }

        public static T TryRead<T>(this NetworkMessage msg, bool logError = true) where T : MessageBase, new()
        {
            T retval;
            if (msg.TryRead(out retval,logError))
                return retval;
            return null;
        }

    }

}
