-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (160 loc) · 5.22 KB
/
index.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
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
"use strict";
const protocPlugin = require('protoc-plugin');
function isDefine(a){return void 0!==a};
function mk_one_message(msgname, fieldList)
{
var content = `
//if (jspb.Message.GENERATE_FROM_OBJECT) {
/**
* Loads data from an object into a new instance of this proto.
* @param {!Object} obj The object representation of this proto to
* load the data from.
* @return {!proto.${msgname}}
*/
proto.${msgname}.prototype.fromObject = function(obj) {
return proto.${msgname}.fromObject(obj, this);
};
/**
* Loads data from an object into a new instance of this proto.
* @param {!Object} obj The object representation of this proto to
* load the data from.
* @return {!proto.${msgname}}
*/
proto.${msgname}.fromObject = function(obj, msg) {
var f;`;
fieldList.forEach(function(field) {
// console.error(field);
if (field.type == 11) {
if (field.label == 3) {
content += `
isArray(obj.${field.jsonName}) && jspb.Message.setRepeatedWrapperField(msg, ${field.number}, obj.${field.jsonName}.map(function(i){return (new proto${field.typeName}()).fromObject(i);}));`;
} else {
content += `
isDef(obj.${field.jsonName}) && (f = new proto${field.typeName}()) && f.fromObject(obj.${field.jsonName}) && jspb.Message.setWrapperField(msg, ${field.number}, f);`;
}
} else {
content += `
isDef(obj.${field.jsonName}) && jspb.Message.setField(msg, ${field.number}, obj.${field.jsonName});`;
}
});
content += `
return msg;
};
//}
`;
return content;
}
protocPlugin(function(protos) {
var rets = [];
var service_request = '';
var service_response = '';
var service_functions = '';
var requires = '';
var namespace = '';
protos.forEach(function(proto) {
// console.error(proto);
if (namespace == '')
namespace = proto.pb_package;
var content = '';
content += `
/**
* @fileoverview
* @enhanceable
* @suppress {messageConventions} JS Compiler reports an error if a variable or
* field starts with 'MSG_' and isn't a translatable message.
* @public
*/
// GENERATED CODE -- DO NOT EDIT!
var jspb = require('google-protobuf');
function isDef(a){return void 0!==a};
function isArray(a){return void 0!==a && a instanceof Array};
`;
if (isDefine(proto.messageTypeList)) {
proto.messageTypeList.forEach(function(message) {
message.nestedTypeList.forEach(function(nested) {
// console.error(nested);
var msgname = proto.pb_package + '.' + message.name + '.' + nested.name;
content += mk_one_message(msgname, nested.fieldList)
});
var msgname = proto.pb_package + '.' + message.name;
content += mk_one_message(msgname, message.fieldList);
})
}
if (isDefine(proto.serviceList)) {
proto.serviceList.forEach(function(service) {
// console.error(service);
service.methodList.forEach(function(method) {
var url = ('/' + service.name.replace(/Service$/, '') + '/' + method.name).toLowerCase();
url = url.replace(/\/create$/, '');
if (method.options && method.options.http && method.options.http.post) {
url = method.options.http.post;
}
service_request += `
case '${url}': return proto${method.inputType};`;
service_response += `
case '${url}': return proto${method.outputType};`;
var fname = (service.name.replace(/Service$/, '') + method.name);
service_functions += `
public delegate void Def${fname}Callback(${method.outputType.replace('.'+proto.pb_package+'.', '')} response);
public virtual void ${fname}(${method.inputType.replace('.'+proto.pb_package+'.', '')} req, Def${fname}Callback cb)
{
StartCoroutine(Send(m_current_url + "${url}", req, delegate (byte[] buff)
{
var res = new ${method.outputType.replace('.'+proto.pb_package+'.', '')}();
res.MergeFrom(buff);
if (cb != null)
cb(res);
}));
}
`;
})
});
}
var ret = {};
ret.name = proto.name.replace('.proto', '_fromobject.js');
ret.content = content;
rets.push(ret);
requires += `
require('../proto/${proto.name.replace('.proto','_pb.js')}');
require('../proto/${proto.name.replace('.proto','_fromobject.js')}');`;
})
var ret = {};
ret.name = 'services.js';
ret.content = `
var protobuf = ` + requires + `
module.exports = function() {
};
module.exports.request = function(url) {
switch(url)
{` + service_request + `
}
return null;
};
module.exports.response = function(url) {
switch(url)
{` + service_response + `
}
return null;
};
`;
rets.push(ret);
ret = {};
ret.name = 'Services.cs';
ret.content = `
using UnityEngine;
using System.Collections;
using Google.Protobuf;
namespace ${namespace.charAt(0).toUpperCase() + namespace.slice(1)}
{
public abstract class Services : MonoBehaviour
{
protected static string m_current_url {get; set;}
protected delegate void DefCallback(byte[] res);
protected abstract IEnumerator Send(string url, IMessage req, DefCallback cb);
${service_functions}
}
}
`;
rets.push(ret);
return rets;
});