lnobad / lidgren-network-gen3

Automatically exported from code.google.com/p/lidgren-network-gen3
0 stars 0 forks source link

Non integral types stored inside classes aren't read properly when using reflection #96

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Using XNA:
have a class such as:

        private class EntityNetworkData
        {
            public long Time;
            public Vector2 Position;

            public EntityNetworkData()
            {

            }

            public EntityNetworkData( long time, Vector2 position )
            {
                Time = time;
                Position = position;
            }
        };

Attempt to pass this class over the network using:
        public virtual void UpdateToNetwork(NetOutgoingMessage message)
        {
            //Sample data
            message.WriteAllFields(new EntityNetworkData(100, new Vector2(42, 42));   
        }

        public virtual networkData UpdateFromNetwork(NetIncomingMessage message, long gameTime)
        {
            var networkData = new EntityNetworkData();
            message.ReadAllFields(networkData);
            return networkData;
        }

What is the expected output? What do you see instead?
Expected: a class containing 100 and {42, 42}
Actual: a class with default values.

Please provide any additional information below.
I merged in the XNA extensions to the main project. This may or may not be 
neccessary, I'm not sure if Reflection would have picked up on the additional 
read\write functions otherwise.

Changing NetIncomingMessage::NetIncomingMessage() to 

        static NetIncomingMessage()
        {
            s_readMethods = new Dictionary<Type, MethodInfo>();
            MethodInfo[] methods = typeof(NetIncomingMessage).GetMethods(BindingFlags.Instance | BindingFlags.Public);
            foreach (MethodInfo mi in methods)
            {
                if (mi.GetParameters().Length == 0 && mi.Name.StartsWith("Read", StringComparison.InvariantCulture) && mi.Name.Substring(4) == mi.ReturnType.Name)
                {                    
                    s_readMethods[mi.ReturnType] = mi;
                }
            }
        }

would make adding new types into the library easier.

Original issue reported on code.google.com by Gumby...@gmail.com on 30 Nov 2011 at 7:51

GoogleCodeExporter commented 9 years ago
Thanks for information :)

Original comment by yoskele...@msn.com on 30 Dec 2011 at 3:27

GoogleCodeExporter commented 9 years ago
Thanks, good change! Fixed in 292

Original comment by lidg...@gmail.com on 26 May 2012 at 8:33

GoogleCodeExporter commented 9 years ago
Woo, my patch got used. Always glad to help!

Original comment by Gumby...@gmail.com on 26 May 2012 at 8:57

GoogleCodeExporter commented 9 years ago
Sigh, just found out that this doesn't play nicely with reading array types as 
you cant have [] in a name.

Original comment by Gumby...@gmail.com on 2 Jun 2012 at 3:01