-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerateTypes.js
79 lines (77 loc) · 2.02 KB
/
generateTypes.js
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
/**
* Generates a mapping of ids to protobuf message names
**/
var ProtoBuf = require('protobufjs');
var builder = ProtoBuf.newBuilder();
ProtoBuf.loadJsonFile("./build/protos.json", builder);
var dota = builder.build();
//maintain a mapping for PacketTypes of id to string so we can emit events for different packet types.
//we want to generate them automatically from the protobufs
var packetEnums = {
"NET_Messages": {
abbr: "net_",
full: "CNETMsg_"
},
"SVC_Messages": {
abbr: "svc_",
full: "CSVCMsg_"
},
"EBaseUserMessages": {
abbr: "UM_",
full: "CUserMessage"
},
"EBaseEntityMessages": {
abbr: "EM_",
full: "CEntityMessage"
},
"EBaseGameEvents": {
abbr: "GE_",
full: "CMsg"
},
"EDotaUserMessages": {
abbr: "DOTA_UM_",
full: "CDOTAUserMsg_"
}
};
var demoEnums = {
"EDemoCommands": {
abbr: "DEM_",
full: "CDemo",
}
};
var types = {
packets: generate(packetEnums),
dems: generate(demoEnums)
};
for (var key in dota) {
//don't try to build mapping if function
//otherwise reverse the enum to help interpret ids
if (typeof dota[key][Object.keys(dota[key])[0]] !== "function") {
types[key] = reverse(dota[key]);
}
}
process.stdout.write(JSON.stringify(types, null, 2));
function generate(enums) {
//using the dota object, each dota[enumName] is an object mapping an internal name to its packet number
//enum EBaseUserMessages {
//UM_AchievementEvent = 101;
//process into -> CUserMessageAchievementEvent
var types = {};
for (var key in enums) {
var obj = dota[key];
for (var key2 in obj) {
var protoName = key2.replace(enums[key].abbr, enums[key].full);
types[obj[key2]] = protoName;
}
}
return types;
}
function reverse(en) {
//accepts an object
//flip the mapping to id->string
var ret = {};
for (var key in en) {
ret[en[key]] = key;
}
return ret;
}