一个方案是修改源码,一个方案是重写其中关键函数,绕开nodejs api的调用,例如fetch方法
let protobuf = require('protobufjs');
let fetch = protobuf.Util.fetch;
protobuf.Util.fetch = function myfetch(path, callbcak) {
if (cc.sys.isNative) {
let str = jsb.fileUtils.getStringFromFile(path);
if (callbcak) {
callbcak(str);
return null;
}
return str;
}
return fetch.call(this, path, callbcak);
};
你预编译proto文件成js,上面函数不会被调用到,还需要重写,Builder的import方法
let protobuf = require('protobufjs');
function propagateSyntax(parent) {
if (parent['messages']) {
parent['messages'].forEach(function(child) {
child["syntax"] = parent["syntax"];
propagateSyntax(child);
});
}
if (parent['enums']) {
parent['enums'].forEach(function(child) {
child["syntax"] = parent["syntax"];
});
}
}
protobuf.Builder.prototype['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;
if (ProtoBuf.Util.IS_NODE)
fname = cc.path.join(root, filename.file);
else
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
if (ProtoBuf.Util.IS_NODE)
importFilename = require("path")['join'](importRoot, importFilename);
else
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"](json['imports'][i]);
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 structures
if (json['package'])
this.define(json['package']);
if (json['syntax'])
propagateSyntax(json);
var base = this.ptr;
if (json['options'])
Object.keys(json['options']).forEach(function(key) {
base.options[key] = json['options'][key];
});
if (json['messages'])
this.create(json['messages']),
this.ptr = base;
if (json['enums'])
this.create(json['enums']),
this.ptr = base;
if (json['services'])
this.create(json['services']),
this.ptr = base;
if (json['extends'])
this.create(json['extends']);
return this.reset();
};
通过上面重写的函数可以绕开nodejs 的API了。
关于你说的,6.8我了解到的是不需要bytebuffer.js和long.js了,具体情况你可以参考另一位网友的分享
http://www.jianshu.com/p/1b90dd4bcb0a