Skip to content

Commit

Permalink
Preliminary nodejs compatibility (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
njibhu authored Feb 7, 2021
1 parent e013cb4 commit 416662a
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 5 deletions.
3 changes: 2 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"prettier": "^2.1.2",
"vinyl-buffer": "^1.0.1",
"vinyl-source-stream": "^2.0.0"
}
},
"dependencies": {}
}
63 changes: 61 additions & 2 deletions experiments/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions experiments/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@
"prettier": "^2.1.2",
"vinyl-buffer": "^1.0.1",
"vinyl-source-stream": "^2.0.0"
},
"dependencies": {
"source-map-support": "^0.5.19",
"vblob": "^1.0.2",
"web-worker": "^1.0.0"
}
}
17 changes: 17 additions & 0 deletions experiments/src/node/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
env: {
node: true,
},
extends: ["eslint:recommended"],
parserOptions: {
ecmaVersion: 2018,
},
rules: {
"no-tabs": "error",
camelcase: 0,
"no-var": "error",
eqeqeq: "error",
semi: ["error", "always"],
"prefer-const": "error",
},
};
85 changes: 85 additions & 0 deletions experiments/src/node/generateGLTF.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* cli example:
* - node ./src/node/generateGLTF.js "path/to/Gw2.dat" "outputPath" mapId
* (186397 - snowden drifts)
*/

const DataStream = require("../../dist/static/DataStream");
global.DataStream = DataStream;

const { Blob, FileReader } = require("vblob");
global.Blob = Blob;
global.FileReader = FileReader;
global.window = {};
global.window.FileReader = FileReader;

const THREE = require("../../dist/static/three");
global.THREE = THREE;
require("../../dist/static/GLTFExporter");

const Worker = require("web-worker");
global.Worker = Worker;

const fs = require("fs");
global.fs = fs;

const T3D = require("../../../library/src/T3DLib");
global.T3D = T3D;

if (process.argv.length < 4) {
console.log("Missing arguments. Expected: filePath, outputFolder, mapId");
process.exit(1);
}

const filePath = process.argv[2];
const outputFolder = process.argv[3];
const mapId = Number.parseInt(process.argv[4]);
if (isNaN(mapId) || mapId < 0) {
console.log("The mapId is not valid");
process.exit(1);
}

T3D.getLocalReader(
filePath,
async (lr) => {
console.log("Loaded!");
const renderers = [
{
renderClass: T3D.EnvironmentRenderer,
settings: {},
},
{
renderClass: T3D.TerrainRenderer,
settings: {
export: true,
},
},
{
renderClass: T3D.HavokRenderer,
settings: {
export: true,
},
},
];
T3D.renderMapContentsAsync(lr, mapId, renderers, (context) => {
console.log("Map loaded !");
const scene = new THREE.Scene();
for (const elem of T3D.getContextValue(context, T3D.TerrainRenderer, "terrainTiles")) {
scene.add(elem);
}
for (const elem of T3D.getContextValue(context, T3D.HavokRenderer, "meshes")) {
scene.add(elem);
}

console.log("Scene loaded !");

const exporter = new THREE.GLTFExporter();
exporter.parse(scene, (gltf) => {
fs.writeFileSync(`${outputFolder}/${mapId}.gltf`, JSON.stringify(gltf));
process.exit(0);
});
});
},
"./dist/static/t3dworker.js",
false
);
9 changes: 9 additions & 0 deletions library/src/LocalReader/ArchiveParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ function parseMFTIndex(ds, size) {
* @returns {Promise<{ds: DataStream, len: number}>}
*/
function getFilePart(file, offset, length) {
// Node compatibility workaround
if (global.process && global.fs) {
const fd = global.fs.openSync(file);
const buffer = global.Buffer.alloc(length);
const readLen = global.fs.readSync(fd, buffer, 0, length, offset);
const ds = new DataStream(buffer);
ds.endianness = DataStream.LITTLE_ENDIAN;
return { ds, len: readLen };
}
return new Promise((resolve, reject) => {
const reader = new FileReader();

Expand Down
3 changes: 2 additions & 1 deletion library/src/T3DLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,14 @@ const T3D = (module.exports = {
* will not be fully initialized until the callback
* is fired.
*/
getLocalReader: function (file, callback, t3dtoolsWorker) {
getLocalReader: function (file, callback, t3dtoolsWorker, noIndexedDB) {
const path = t3dtoolsWorker || _settings.t3dtoolsWorker;

// Create the instance and init the threads
const lrInstance = new LocalReader({
workerPath: path,
workersNb: _settings.concurrentTasks,
noIndexedDB,
});

/// Callback with the lrInstance
Expand Down
2 changes: 1 addition & 1 deletion library/src/dataRenderer/HavokRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class HavokRenderer extends DataRenderer {
}

if (i < models.length) {
window.setTimeout(
setTimeout(
this.parseAllModels.bind(this, models, mat, title, chunkSize, offset + chunkSize, callback),
10 /* time in ms to next call */
);
Expand Down

0 comments on commit 416662a

Please sign in to comment.