Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module network.msguserinfo;
- import client;
- import network.networkdef;
- import network.netmsg;
- public struct MsgUserInfo
- {
- public:
- NetMsg msg = NetMsg(20, MessageType.userInfo);
- alias msg this;
- mixin MessageProperty!(4, uint, "timestamp");
- mixin MessageProperty!(8, uint, "id");
- mixin MessageProperty!(66, uint, "life");
- mixin MessageProperty!(68, uint, "mana");
- mixin MessageProperty!(122, string[], "strings");
- void handleReceived(Client client, out bool skip)
- {
- string[] strs = strings;
- string name = strs[0];
- client.player.id = this.id;
- client.player.name = name;
- }
- void handleSent(Client client, out bool skip)
- {
- }
- }
- public mixin template MessageProperty(int propertyOffset, propertyType, string propertyName)
- {
- static if (is(propertyType == string[]))
- {
- mixin("@property string[]" ~ propertyName ~ "()
- {
- ubyte stringAmount = buffer[propertyOffset - 4];
- char* pBuf = cast(char*)&buffer[propertyOffset - 4 + 1];
- string[] strings = new string[stringAmount];
- foreach (i; 0..stringAmount)
- {
- ubyte strSize = *cast(ubyte*)pBuf;
- pBuf++;
- string str = pBuf[0..strSize].idup;
- pBuf += strSize;
- strings[i] = str;
- }
- return strings;
- }");
- mixin ("@property void " ~ propertyName ~ "(string[] value)
- {
- char* startPointer = cast(char*)&buffer[propertyOffset - 4];
- char* pBuf = cast(char*)&buffer[propertyOffset - 4 + 1];
- buffer[propertyOffset - 4] = cast(ubyte)value.length;
- foreach (i; 0..value.length)
- {
- string str = value[i];
- ubyte strSize = cast(ubyte)str.length;
- *cast(ubyte*)pBuf = strSize;
- pBuf++;
- pBuf[0..strSize] = value[i][0..strSize];
- pBuf += strSize;
- }
- size += cast(ushort)((pBuf - startPointer) + 2);
- }");
- }
- else
- {
- mixin("@property " ~ propertyType.stringof ~ " " ~ propertyName ~ "() { return *cast(" ~ propertyType.stringof ~ "*)&buffer[" ~ (propertyOffset - 4).stringof ~ "]; }");
- mixin("@property void " ~ propertyName ~ "(" ~ propertyType.stringof ~ " value) { *cast(" ~ propertyType.stringof ~ "*)&buffer[" ~ (propertyOffset - 4).stringof ~ "] = value;}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement