我用的是论坛里面早期的一个protobufjs版本,我的加载方式用的是ProtoBuf.protoFromString(proto, builder,cc.url.raw(paths[0].import))
cc.url.raw(paths[0].import 是被引用的.proto文件(定义proto的时候复用另外一个proto里面的结构是经常的事情)
但是这里我在调试进入到protobufjs里面的时候,源码里面的这个函数:
BuilderPrototype[“import”] = function(json, filename) {
var delim = ‘/’;
// Make sure to skip duplicate imports
if (typeof filename === 'string') {
if (ProtoBuf.Util.IS_NODE)
filename = require("path")['resolve'](filename);
if (this.files[filename] === true)
return this.reset();
**this.files[filename] = true;**
} else if (typeof filename === 'object') { // Object with root, file.
var root = filename.root;
if (ProtoBuf.Util.IS_NODE)
root = require("path")['resolve'](root);
if (root.indexOf("\\") >= 0 || filename.file.indexOf("\\") >= 0)
delim = '\\';
var fname = root + delim + filename.file;
if (this.files[fname] === true)
return this.reset();
this.files[fname] = true;
}
// Import imports
if (json['imports'] && json['imports'].length > 0) {
var importRoot,
resetRoot = false;
if (typeof filename === 'object') { // If an import root is specified, override
this.importRoot = filename["root"]; resetRoot = true; // ... and reset afterwards
importRoot = this.importRoot;
filename = filename["file"];
if (importRoot.indexOf("\\") >= 0 || filename.indexOf("\\") >= 0)
delim = '\\';
} else if (typeof filename === 'string') {
if (this.importRoot) // If import root is overridden, use it
importRoot = this.importRoot;
else { // Otherwise compute from filename
if (filename.indexOf("/") >= 0) { // Unix
importRoot = filename.replace(/\/[^\/]*$/, "");
if (/* /file.proto */ importRoot === "")
importRoot = "/";
} else if (filename.indexOf("\\") >= 0) { // Windows
importRoot = filename.replace(/\\[^\\]*$/, "");
delim = '\\';
} else
importRoot = ".";
}
} else
importRoot = null;
for (var i=0; i<json['imports'].length; i++) {
if (typeof json['imports'][i] === 'string') { // Import file
if (!importRoot)
throw Error("cannot determine import root");
var importFilename = json['imports'][i];
if (importFilename === "google/protobuf/descriptor.proto")
continue; // Not needed and therefore not used
importFilename = importRoot + delim + importFilename;
** if (this.files[importFilename] === true)**
** continue; // Already imported**
if (/.proto$/i.test(importFilename) && !ProtoBuf.DotProto) // If this is a light build
importFilename = importFilename.replace(/.proto$/, “.json”); // always load the JSON file
var contents = ProtoBuf.Util.fetch(importFilename);
if (contents === null)
throw Error(“failed to import '”+importFilename+"’ in ‘"+filename+"’: file not found");
if (/.json$/i.test(importFilename)) // Always possible
this[“import”](JSON.parse(contents+""), importFilename); // May throw
else
this[“import”](ProtoBuf.DotProto.Parser.parse(contents), importFilename); // May throw
} else // Import structure
if (!filename)
this"import";
else if (/.(\w+)/.test(filename)) // With extension: Append _importN to the name portion to make it unique
this["import"](json['imports'][i], filename.replace(/^(.+)\.(\w+)/, function($0, $1, $2) { return $1+"_import"+i+"."+$2; }));
else // Without extension: Append _importN to make it unique
this[“import”](json[‘imports’][i], filename+"_import"+i);
}
if (resetRoot) // Reset import root override when all imports are done
this.importRoot = null;
}
发现有个问题,图片中我圈出了红色的部分,上面把文件路径添加到数组,下面去读取import的proto的时候直接判断了刚才添加进去的proto的路径,然后就continue了。。。。没有走下面的var contents = ProtoBuf.Util.fetch(importFilename); 这段加载代码,这是为嘛?这代码逻辑问题么?还是我加载方式不对?求大神解惑下。
