open-dis / open-dis-javascript

Javascript implementation of the IEEE-1278.1 Distributed Interactive Simulation (DIS) application protocol v6 and v7
BSD 2-Clause "Simplified" License
11 stars 12 forks source link

Enable use of DIS 6 and DIS 7 in same library #25

Open B1Dobbs opened 1 year ago

B1Dobbs commented 1 year ago

With the way functions are declared on a global variable 'dis', it makes it fairly difficult to use both DIS 6 and DIS 7 in the same module. We have a need to support both versions and potentially DIS 8 when it comes along. Instead of declaring everything to one variable 'dis', could each version assign it to 'disX' where 'X' is the version number?

Example of issue:

const dis6 = require('open-dis') // don't need relative import because module 'main' points to dis6.min.js
const dis7 = require("../../../../node_modules/open-dis/dist/dis7.min.js"); // overwrites all 'dis' functions to dis7 version

var entityStatePdu_dis6 = new dis6.EntityStatePdu();
entityStatePdu_dis6.entityID.entity = 1;
var entityStatePdu_dis7 = new dis7.EntityStatePdu();
entityStatePdu_dis6.entityID.entityID = 1;

 var ab = new ArrayBuffer(1500);
 var outputstream = new dis.OutputStream(ab)
entityStatePdu_dis6.encodeToBinary(outputStream) // would encode pdu.entityID.entityID instead of pdu.entityID.entity
entityStatePdu_dis7.encodeToBinary(outputStream) // encodes pdu.entityID.entityID as expected

A way I got around this was to reset the global dis.EntityID function before any pdu creation but this would get very unwieldy for a more complex pdu.

const dis6 = require('open-dis');
const dis7 = require("../../../../node_modules/open-dis/dist/dis7.min.js");

var pdu; // dummy variable for example, would be an dis.Pdu object
if(pdu.protocolVersion == 7) {
     global.dis.EntityID = dis7.EntityID;
     pdu = new dis7.EntityStatePdu();
} else {
     global.dis.EntityID = dis6.EntityID;
     pdu = new dis6.EntityStatePdu();
}

Would definitely appreciate any advice if there is a better way to get around this!