Advertisement
Guest User

Untitled

a guest
Sep 26th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.72 KB | None | 0 0
  1. module network.msguserinfo;
  2.  
  3. import client;
  4. import network.networkdef;
  5. import network.netmsg;
  6.  
  7. public struct MsgUserInfo
  8. {
  9. public:
  10.     NetMsg msg = NetMsg(20, MessageType.userInfo);
  11.     alias msg this;
  12.    
  13.     mixin MessageProperty!(4, uint, "timestamp");
  14.     mixin MessageProperty!(8, uint, "id");
  15.     mixin MessageProperty!(66, uint, "life");
  16.     mixin MessageProperty!(68, uint, "mana");
  17.     mixin MessageProperty!(122, string[], "strings");
  18.    
  19.     void handleReceived(Client client, out bool skip)
  20.     {
  21.  
  22.         string[] strs = strings;
  23.         string name = strs[0];
  24.        
  25.         client.player.id = this.id;
  26.         client.player.name = name;
  27.        
  28.     }
  29.    
  30.     void handleSent(Client client, out bool skip)
  31.     {
  32.        
  33.     }
  34.    
  35. }
  36.  
  37. public mixin template MessageProperty(int propertyOffset, propertyType, string propertyName)
  38. {
  39.    
  40.     static if (is(propertyType == string[]))
  41.     {
  42.        
  43.         mixin("@property string[]" ~ propertyName ~ "()
  44.        {
  45.            
  46.            ubyte stringAmount = buffer[propertyOffset - 4];
  47.            
  48.            char* pBuf = cast(char*)&buffer[propertyOffset - 4 + 1];
  49.            
  50.            string[] strings = new string[stringAmount];
  51.            
  52.            foreach (i; 0..stringAmount)
  53.            {
  54.                
  55.                ubyte strSize = *cast(ubyte*)pBuf;
  56.                pBuf++;
  57.                
  58.                string str = pBuf[0..strSize].idup;
  59.                pBuf += strSize;
  60.                
  61.                strings[i] = str;
  62.                
  63.            }
  64.            
  65.            return strings;
  66.            
  67.        }");
  68.        
  69.         mixin ("@property void " ~ propertyName ~ "(string[] value)
  70.        {
  71.            
  72.            char* startPointer = cast(char*)&buffer[propertyOffset - 4];
  73.            char* pBuf = cast(char*)&buffer[propertyOffset - 4 + 1];
  74.            
  75.            buffer[propertyOffset - 4] = cast(ubyte)value.length;
  76.            
  77.            foreach (i; 0..value.length)
  78.            {
  79.                
  80.                string str = value[i];
  81.                ubyte strSize = cast(ubyte)str.length;
  82.                
  83.                *cast(ubyte*)pBuf = strSize;
  84.                pBuf++;
  85.                
  86.                pBuf[0..strSize] = value[i][0..strSize];
  87.                pBuf += strSize;
  88.                
  89.            }
  90.            
  91.            size += cast(ushort)((pBuf - startPointer) + 2);
  92.            
  93.        }");
  94.        
  95.     }
  96.     else
  97.     {
  98.         mixin("@property " ~ propertyType.stringof ~ " " ~ propertyName ~ "() { return *cast(" ~ propertyType.stringof ~ "*)&buffer[" ~ (propertyOffset - 4).stringof ~ "]; }");
  99.         mixin("@property void " ~ propertyName ~ "(" ~ propertyType.stringof ~ " value) { *cast(" ~ propertyType.stringof ~ "*)&buffer[" ~ (propertyOffset - 4).stringof ~ "] = value;}");
  100.     }
  101.    
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement