Exemplo n.º 1
0
        public NetMessage3(
            NM3MessageType messageType,
            params object[] param
            )
        {
            NM3Sig signature = NM3Sig.Get(messageType);

            setup(
                signature,
                new List <object>(param)
                );
        }
Exemplo n.º 2
0
        //setup the netmessage from a formatted string,
        //usually received from the server.
        //e.g. "move-unit:0,10,10"
        //called from the NetMessage3(string) ctor.
        private void setup(string formatted)
        {
            string        command;
            List <string> arguments;

            if (formatted.Contains(':'))
            {
                command   = formatted.Split(':')[0];
                arguments =
                    formatted
                    .Split(':')[1]
                    .Split(',')
                    .ToList();
            }
            else
            {
                command   = formatted;
                arguments = new List <string>();
            }

            arguments.RemoveAll(s => s == "");

            Signature = NM3Sig.Get(FromString(command));

            if (Signature.Arguments.Count != arguments.Count)
            {
                throw new ArgumentException();
            }

            messageArguments = new List <object>();

            var handlers = new Dictionary <Type, Func <string, object> >
            {
                { typeof(int), s => Int32.Parse(s) },
                { typeof(string), s => s },
                { typeof(bool), s => bool.Parse(s) },
            };

            for (int i = 0; i < arguments.Count; i++)
            {
                messageArguments.Add(
                    handlers
                    [Signature.ArgumentTypes[i]]
                        (arguments[i])
                    );
            }
        }