forked from PlayFab/XPlatCoreTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake.js
More file actions
302 lines (265 loc) · 15.4 KB
/
make.js
File metadata and controls
302 lines (265 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
var path = require("path");
// Making resharper less noisy - These are defined in Generate.js
if (typeof getCompiledTemplate === "undefined") getCompiledTemplate = function () { };
if (typeof templatizeTree === "undefined") templatizeTree = function () { };
exports.makeCombinedAPI = function (apis, sourceDir, apiOutputDir) {
console.log("Generating Combined api from: " + sourceDir + " to: " + apiOutputDir);
var removeStatic = ""; // "DISABLE_PLAYFAB_STATIC_API;";
var libDefines = "ENABLE_PLAYFABADMIN_API;ENABLE_PLAYFABSERVER_API;" + removeStatic;
var clientDefines = "" + removeStatic;
var serverDefines = "ENABLE_PLAYFABADMIN_API;ENABLE_PLAYFABSERVER_API;DISABLE_PLAYFABCLIENT_API;" + removeStatic;
var locals = {
apis: apis,
buildIdentifier: sdkGlobals.buildIdentifier,
clientDefines: clientDefines,
libDefines: libDefines,
serverDefines: serverDefines,
sdkVersion: sdkGlobals.sdkVersion,
sdkDate: sdkGlobals.sdkVersion.split(".")[2],
sdkYear: sdkGlobals.sdkVersion.split(".")[2].substr(0, 2),
vsVer: "v141", // As C++ versions change, we may need to update this
vsYear: "2017", // As VS versions change, we may need to update this
getVerticalNameDefault: getVerticalNameDefault
};
templatizeTree(locals, path.resolve(sourceDir, "source"), apiOutputDir);
for (var a = 0; a < apis.length; a++)
makeApiFiles(apis[a], sourceDir, apiOutputDir);
};
function makeApiFiles(api, sourceDir, apiOutputDir) {
var remStaticDefine = ""; // " && !defined(DISABLE_PLAYFAB_STATIC_API)";
var locals = {
api: api,
enumtypes: getEnumTypes(api.datatypes),
getApiDefine: getApiDefine,
getAuthParams: getAuthParams,
getBaseType: getBaseType,
getPropertyDefinition: getPropertyDefinition,
getPropertyFromJson: getPropertyFromJson,
getPropertyToJson: getPropertyToJson,
getPropertySafeName: getPropertySafeName,
getRequestActions: getRequestActions,
getResultActions: getResultActions,
hasClientOptions: getAuthMechanisms([api]).includes("SessionTicket"),
hasAuthParams: hasAuthParams,
ifHasProps: ifHasProps,
remStaticDefine: remStaticDefine,
sdkVersion: sdkGlobals.sdkVersion,
sortedClasses: getSortedClasses(api.datatypes)
};
var apihTemplate = getCompiledTemplate(path.resolve(sourceDir, "templates/PlayFab_Api.h.ejs"));
writeFile(path.resolve(apiOutputDir, "code/include/playfab", "PlayFab" + api.name + "Api.h"), apihTemplate(locals));
var iapihTemplate = getCompiledTemplate(path.resolve(sourceDir, "templates/PlayFab_InstanceApi.h.ejs"));
writeFile(path.resolve(apiOutputDir, "code/include/playfab", "PlayFab" + api.name + "InstanceApi.h"), iapihTemplate(locals));
var apiCppTemplate = getCompiledTemplate(path.resolve(sourceDir, "templates/PlayFab_Api.cpp.ejs"));
writeFile(path.resolve(apiOutputDir, "code/source/playfab", "PlayFab" + api.name + "Api.cpp"), apiCppTemplate(locals));
var iapiCppTemplate = getCompiledTemplate(path.resolve(sourceDir, "templates/PlayFab_InstanceApi.cpp.ejs"));
writeFile(path.resolve(apiOutputDir, "code/source/playfab", "PlayFab" + api.name + "InstanceApi.cpp"), iapiCppTemplate(locals));
var dataModelTemplate = getCompiledTemplate(path.resolve(sourceDir, "templates/PlayFab_DataModels.h.ejs"));
writeFile(path.resolve(apiOutputDir, "code/include/playfab", "PlayFab" + api.name + "DataModels.h"), dataModelTemplate(locals));
}
// *************************** Internal utility methods ***************************
function getEnumTypes(datatypes) {
var enumtypes = [];
for (var typeIdx in datatypes) // Add all types and their dependencies
if (datatypes[typeIdx].isenum)
enumtypes.push(datatypes[typeIdx]);
return enumtypes;
}
function getSortedClasses(datatypes) {
var sortedClasses = [];
var addedTypes = new Set();
var addType = function (datatype) {
if (addedTypes.has(datatype.name) || datatype.isenum)
return;
// In C++, dependent types must be defined first
if (datatype.properties) {
for (var propIdx = 0; propIdx < datatype.properties.length; propIdx++) {
var property = datatype.properties[propIdx];
if (property.isclass || property.isenum)
addType(datatypes[property.actualtype]);
}
}
addedTypes.add(datatype.name);
sortedClasses.push(datatype);
};
for (var typeIdx in datatypes) // Add all types and their dependencies
addType(datatypes[typeIdx]);
return sortedClasses;
}
// *************************** ejs-exposed methods ***************************
function getApiDefine(api) {
if (api.name === "Client")
return "#if !defined(DISABLE_PLAYFABCLIENT_API)";
if (api.name === "Matchmaker")
return "#if defined(ENABLE_PLAYFABSERVER_API)"; // Matchmaker is bound to server, which is just a legacy design decision at this point
if (api.name === "Admin" || api.name === "Server")
return "#if defined(ENABLE_PLAYFAB" + api.name.toUpperCase() + "_API)";
// For now, everything else is considered ENTITY
return "#if !defined(DISABLE_PLAYFABENTITY_API)";
}
function hasAuthParams(apiCall) {
try {
getAuthParams(apiCall, true);
} catch (err) {
return false;
}
return true;
}
function getAuthParams(apiCall, isInstanceApi) {
if (apiCall.url === "/Authentication/GetEntityToken")
return "authKey, authValue";
switch (apiCall.auth) {
case "EntityToken": return "\"X-EntityToken\", context->entityToken";
case "SessionTicket": return "\"X-Authorization\", context->clientSessionTicket";
case "SecretKey": return "\"X-SecretKey\", settings->developerSecretKey";
}
throw Error("getAuthParams: Unknown auth type: " + apiCall.auth + " for " + apiCall.name);
}
function getBaseType(datatype) {
if (datatype.className.toLowerCase().endsWith("request"))
return "PlayFabRequestCommon";
if (datatype.className.toLowerCase().endsWith("loginresult"))
return "PlayFabLoginResultCommon";
if (datatype.className.toLowerCase().endsWith("response") || datatype.className.toLowerCase().endsWith("result"))
return "PlayFabResultCommon";
return "PlayFabBaseModel";
}
function getPropertyCppType(property, datatype, needOptional) {
var isOptional = property.optional && needOptional;
if (property.actualtype === "String")
return "std::string";
else if (property.isclass)
return isOptional ? ("Boxed<" + property.actualtype + ">") : property.actualtype;
else if (property.jsontype === "Object" && property.actualtype === "object")
return "Json::Value";
else if (property.actualtype === "Boolean")
return isOptional ? "Boxed<bool>" : "bool";
else if (property.actualtype === "int16")
return isOptional ? "Boxed<Int16>" : "Int16";
else if (property.actualtype === "uint16")
return isOptional ? "Boxed<Uint16>" : "Uint16";
else if (property.actualtype === "int32")
return isOptional ? "Boxed<Int32>" : "Int32";
else if (property.actualtype === "uint32")
return isOptional ? "Boxed<Uint32>" : "Uint32";
else if (property.actualtype === "int64")
return isOptional ? "Boxed<Int64>" : "Int64";
else if (property.actualtype === "uint64")
return isOptional ? "Boxed<Uint64>" : "Uint64";
else if (property.actualtype === "float")
return isOptional ? "Boxed<float>" : "float";
else if (property.actualtype === "double")
return isOptional ? "Boxed<double>" : "double";
else if (property.actualtype === "DateTime")
return isOptional ? "Boxed<time_t>" : "time_t";
else if (property.isenum)
return isOptional ? ("Boxed<" + property.actualtype + ">") : property.actualtype;
throw Error("getPropertyCppType: Unknown property type: " + property.actualtype + " for " + property.name + " in " + datatype.name);
}
function getPropertyDefinition(tabbing, property, datatype) {
var cppType = getPropertyCppType(property, datatype, !property.collection);
var safePropName = getPropertySafeName(property);
if (!property.collection) {
return tabbing + cppType + " " + safePropName + ";";
} else if (property.jsontype === "Object" && property.actualtype === "object") {
return tabbing + cppType + " " + safePropName + "; // Not truly arbitrary. See documentation for restrictions on format";
} else if (property.collection === "array") {
return tabbing + "std::list<" + cppType + "> " + safePropName + ";";
} else if (property.collection === "map") {
return tabbing + "std::map<std::string, " + cppType + "> " + safePropName + ";";
}
throw Error("getPropertyDefinition: Unknown property type: " + property.actualtype + " for " + property.name + " in " + datatype.name);
}
function getPropertyFromJson(tabbing, property, datatype) {
var safePropName = getPropertySafeName(property);
if (property.jsontype === "Object" && property.actualtype === "object")
return tabbing + safePropName + " = input[\"" + property.name + "\"];";
if (property.jsontype === "Object")
return tabbing + "FromJsonUtilO(input[\"" + property.name + "\"], " + safePropName + ");";
if (property.isenum && (property.collection || property.optional))
return tabbing + "FromJsonUtilE(input[\"" + property.name + "\"], " + safePropName + ");";
if (property.isenum)
return tabbing + "FromJsonEnum(input[\"" + property.name + "\"], " + safePropName + ");";
if (property.actualtype === "DateTime")
return tabbing + "FromJsonUtilT(input[\"" + property.name + "\"], " + safePropName + ");";
if (property.actualtype === "String")
return tabbing + "FromJsonUtilS(input[\"" + property.name + "\"], " + safePropName + ");";
var primitives = new Set(["Boolean", "int16", "uint16", "int32", "uint32", "int64", "uint64", "float", "double"]);
if (primitives.has(property.actualtype))
return tabbing + "FromJsonUtilP(input[\"" + property.name + "\"], " + safePropName + ");";
throw Error("getPropertyFromJson: Unknown property type: " + property.actualtype + " for " + property.name + " in " + datatype.name);
}
function getPropertyToJson(tabbing, property, datatype) {
var safePropName = getPropertySafeName(property);
if (property.jsontype === "Object" && property.actualtype === "object")
return tabbing + "output[\"" + property.name + "\"] = " + safePropName + ";";
if (property.jsontype === "Object")
return tabbing + "Json::Value each_" + safePropName + "; ToJsonUtilO(" + safePropName + ", each_" + safePropName + "); output[\"" + property.name + "\"] = each_" + safePropName + ";";
if (property.isenum && (property.collection || property.optional))
return tabbing + "Json::Value each_" + safePropName + "; ToJsonUtilE(" + safePropName + ", each_" + safePropName + "); output[\"" + property.name + "\"] = each_" + safePropName + ";";
if (property.isenum)
return tabbing + "Json::Value each_" + safePropName + "; ToJsonEnum(" + safePropName + ", each_" + safePropName + "); output[\"" + property.name + "\"] = each_" + safePropName + ";";
if (property.actualtype === "DateTime")
return tabbing + "Json::Value each_" + safePropName + "; ToJsonUtilT(" + property.name + ", each_" + safePropName + "); output[\"" + property.name + "\"] = each_" + safePropName + ";";
if (property.actualtype === "String")
return tabbing + "Json::Value each_" + safePropName + "; ToJsonUtilS(" + safePropName + ", each_" + safePropName + "); output[\"" + property.name + "\"] = each_" + safePropName + ";";
var primitives = new Set(["Boolean", "int16", "uint16", "int32", "uint32", "int64", "uint64", "float", "double"]);
if (primitives.has(property.actualtype))
return tabbing + "Json::Value each_" + safePropName + "; ToJsonUtilP(" + safePropName + ", each_" + safePropName + "); output[\"" + property.name + "\"] = each_" + safePropName + ";";
throw Error("getPropertyToJson: Unknown property type: " + property.actualtype + " for " + property.name + " in " + datatype.name);
}
function getPropertySafeName(property) {
return (property.actualtype === property.name) ? "pf" + property.name : property.name;
}
function getRequestActions(tabbing, apiCall, isInstanceApi) {
//TODO Bug 6594: add to this titleId check.
// If this titleId does not exist we should be throwing an error informing the user MUST have a titleId.
if (apiCall.result === "LoginResult" || apiCall.result === "RegisterPlayFabUserResult")
return tabbing + "if (request.TitleId.empty())\n"
+ tabbing + "{\n"
+ tabbing + " request.TitleId = settings->titleId;\n"
+ tabbing + "}\n";
if (apiCall.url === "/Authentication/GetEntityToken")
return tabbing + "std::string authKey, authValue;\n" +
tabbing + "if (context->entityToken.length() > 0)\n" +
tabbing + "{\n" +
tabbing + " authKey = \"X-EntityToken\"; authValue = context->entityToken;\n" +
tabbing + "}\n" +
tabbing + "else if (context->clientSessionTicket.length() > 0)\n" +
tabbing + "{\n" +
tabbing + " authKey = \"X-Authorization\"; authValue = context->clientSessionTicket;\n" +
tabbing + "}\n" +
"#if defined(ENABLE_PLAYFABSERVER_API) || defined(ENABLE_PLAYFABADMIN_API)\n" +
tabbing + "else if (settings->developerSecretKey.length() > 0)\n" +
tabbing + "{\n" +
tabbing + " authKey = \"X-SecretKey\"; authValue = settings->developerSecretKey;\n" +
tabbing + "}\n" +
"#endif\n";
return "";
}
function getResultActions(tabbing, apiCall, isInstanceApi) {
if (apiCall.url === "/Authentication/GetEntityToken")
return tabbing + "context->HandlePlayFabLogin(\"\", \"\", outResult.Entity->Id, outResult.Entity->Type, outResult.EntityToken);\n";
if (apiCall.result === "LoginResult")
return tabbing + "outResult.authenticationContext = std::make_shared<PlayFabAuthenticationContext>();\n" +
tabbing + "outResult.authenticationContext->HandlePlayFabLogin(outResult.PlayFabId, outResult.SessionTicket, outResult.EntityToken->Entity->Id, outResult.EntityToken->Entity->Type, outResult.EntityToken->EntityToken);\n" +
tabbing + "context->HandlePlayFabLogin(outResult.PlayFabId, outResult.SessionTicket, outResult.EntityToken->Entity->Id, outResult.EntityToken->Entity->Type, outResult.EntityToken->EntityToken);\n" +
tabbing + "MultiStepClientLogin(context, outResult.SettingsForUser->NeedsAttribution);\n";
if (apiCall.result === "RegisterPlayFabUserResult")
return tabbing + "context->HandlePlayFabLogin(outResult.PlayFabId, outResult.SessionTicket, outResult.EntityToken->Entity->Id, outResult.EntityToken->Entity->Type, outResult.EntityToken->EntityToken);\n"
+ tabbing + "MultiStepClientLogin(context, outResult.SettingsForUser->NeedsAttribution);\n";
if (apiCall.result === "AttributeInstallResult")
return tabbing + "context->advertisingIdType += \"_Successful\";\n";
return "";
}
function ifHasProps(datatype, displayText) {
if (datatype.properties.length === 0)
return "";
return displayText;
}
function getVerticalNameDefault() {
if (sdkGlobals.verticalName) {
return sdkGlobals.verticalName;
}
return "";
}