1 /*
2  * Copyright (c) 2016-2018 sel-project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  *
22  */
23 module soupply.util;
24 
25 import std.string : split, join;
26 import std.uuid : _UUID = UUID;
27 
28 import packetmaker.attributes : Exclude;
29 import packetmaker.packet : Packet;
30 
31 import xbuffer.buffer : Buffer;
32 
33 template Pad(size_t padding, T:Packet) {
34 
35 	private ubyte[] __padding = new ubyte[padding];
36 
37 	class Pad : T {
38 	
39 		override void encode(Buffer buffer) {
40 			encodeId(buffer);
41 			buffer.writeData(__padding);
42 			encodeBody(buffer);
43 		}
44 		
45 		override void decode(Buffer buffer) {
46 			decodeId(buffer);
47 			buffer.readData(padding);
48 			decodeBody(buffer);
49 		}
50 	
51 	}
52 
53 }
54 
55 struct Vector(T, string variables) if(variables.length)
56 {
57 
58 	union
59 	{
60 	
61 		T[variables.length] array;
62 		struct
63 		{
64 			mixin("@Exclude T " ~ variables.split("").join(";@Exclude T ") ~ ";");
65 		}
66 	
67 	}
68 	
69 	this(T[] values...)
70 	{
71 		this.array = values;
72 	}
73 
74 }
75 
76 struct UUID {
77 
78 	_UUID uuid;
79 	
80 	void encodeBody(Buffer buffer) {
81 		buffer.writeData(uuid.data);
82 	}
83 	
84 	void decodeBody(Buffer buffer) {
85 		ubyte[16] data = cast(ubyte[])buffer.readData(16);
86 		uuid = _UUID(data);
87 	}
88 	
89 	alias uuid this;
90 
91 }