1 /* 2 * Copyright (c) 2017-2019 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 /** 24 * Copyright: Copyright (c) 2017-2019 sel-project 25 * License: MIT 26 * Authors: Kripth 27 * Source: $(HTTP github.com/sel-project/selery/source/selery/entity/metadata.d, selery/entity/metadata.d) 28 */ 29 module selery.entity.metadata; 30 31 import std.conv : to; 32 import std..string : join, startsWith; 33 import std.typetuple : TypeTuple; 34 35 import selery.about; 36 37 // still named "minecraft" in sel-utils 38 mixin("alias Games = TypeTuple!(" ~ (){ string[] ret;foreach(g,pr;["bedrock":supportedBedrockProtocols,"java":supportedJavaProtocols]){foreach(p;pr){ret~="\""~g~p.to!string~"\"";}}return ret.join(","); }() ~ ");"); 39 40 mixin((){ 41 string ret; 42 foreach(immutable game ; Games) { 43 ret ~= "static import sul.metadata." ~ game ~ ";"; 44 } 45 return ret; 46 }()); 47 48 class Metadata { 49 50 mixin((){ 51 string ret; 52 foreach(immutable game ; Games) { 53 ret ~= "public sul.metadata." ~ game ~ ".Metadata " ~ game ~ ";"; 54 } 55 return ret; 56 }()); 57 58 public bool changed = false; 59 60 public pure nothrow @safe this() { 61 foreach(immutable game ; Games) { 62 mixin("this." ~ game ~ " = new sul.metadata." ~ game ~ ".Metadata();"); 63 } 64 } 65 66 T get(string m, T)() { 67 enum string game = (){ 68 string ret = ""; 69 foreach(immutable g ; Games) { 70 mixin("alias T = sul.metadata." ~ g ~ ".Metadata;"); 71 static if(__traits(hasMember, T, m)) ret = g; 72 } 73 return ret; 74 }(); 75 static if(game.length) { 76 mixin("return this." ~ game ~ "." ~ m ~ ";"); 77 } else { 78 return T.init; 79 } 80 } 81 82 T set(string m, string filter, T)(T value) { 83 foreach(immutable game ; Games) { 84 static if(!filter.length || game.startsWith(filter)) { 85 mixin("alias T = sul.metadata." ~ game ~ ".Metadata;"); 86 static if(__traits(hasMember, T, m)) { 87 this.changed = true; 88 mixin("this." ~ game ~ "." ~ m ~ " = value;"); 89 } 90 } 91 } 92 return value; 93 } 94 95 T set(string m, T)(T value) { 96 return this.set!(m, "")(value); 97 } 98 99 }