dlc.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. import * as flatbuffers from './flatbuffers.js';
  2. import * as text from './text.js';
  3. const dlc = {};
  4. dlc.ModelFactory = class {
  5. async match(context) {
  6. const container = await dlc.Container.open(context);
  7. if (container) {
  8. return context.set('dlc', container);
  9. }
  10. return null;
  11. }
  12. async open(context) {
  13. dlc.schema = await context.require('./dlc-schema');
  14. dlc.schema = dlc.schema.dlc;
  15. await context.value.read();
  16. const metadata = await context.metadata('dlc-metadata.json');
  17. return new dlc.Model(metadata, context.value);
  18. }
  19. };
  20. dlc.Model = class {
  21. constructor(metadata, target) {
  22. this.format = target.format;
  23. this.metadata = [];
  24. if (target.metadata.size > 0) {
  25. const version = target.metadata.get('model-version');
  26. if (version) {
  27. this.version = version;
  28. }
  29. const converter = target.metadata.get('converter-command');
  30. if (converter) {
  31. const source = converter.split(' ').shift().trim();
  32. if (source.length > 0) {
  33. const version = target.metadata.get('converter-version');
  34. this.source = version ? `${source} v${version}` : source;
  35. }
  36. }
  37. const license = target.metadata.get('model-copyright');
  38. if (license && license !== 'N/A') {
  39. this.metadata.push(new dlc.Argument('license', license));
  40. }
  41. }
  42. for (const graph of target.graphs) {
  43. this.modules = [new dlc.Graph(metadata, target.version, graph)];
  44. }
  45. }
  46. };
  47. dlc.Graph = class {
  48. constructor(metadata, version, graph) {
  49. this.name = graph.name;
  50. this.inputs = [];
  51. this.outputs = [];
  52. const values = new Map();
  53. switch (version.major) {
  54. case 3: {
  55. for (const node of graph.nodes) {
  56. for (const name of node.inputs) {
  57. if (!values.has(name)) {
  58. values.set(name, {});
  59. }
  60. }
  61. for (const name of node.outputs) {
  62. if (!values.has(name)) {
  63. values.set(name, {});
  64. }
  65. }
  66. let shapes = new Array(node.outputs.length);
  67. for (const attribute of node.attributes) {
  68. if (attribute.name === 'OutputDims' &&
  69. Array.isArray(attribute.attributes) && attribute.attributes.length > 0) {
  70. shapes = attribute.data;
  71. break;
  72. }
  73. }
  74. for (let i = 0; i < node.outputs.length; i++) {
  75. const name = node.outputs[i];
  76. const value = values.get(name);
  77. if (!value.shape && i < shapes.length) {
  78. value.shape = shapes[i];
  79. }
  80. }
  81. }
  82. break;
  83. }
  84. case 4: {
  85. for (const tensor of graph.tensors) {
  86. values.set(tensor.name, tensor);
  87. }
  88. break;
  89. }
  90. default: {
  91. break;
  92. }
  93. }
  94. for (const [name, tensor] of values) {
  95. const type = tensor.shape ? new dlc.TensorType(tensor.dtype, tensor.shape) : null;
  96. const initializer = tensor.data && tensor.data ? new dlc.Tensor(tensor.name, type, tensor.data) : null;
  97. const value = new dlc.Value(name, type, initializer);
  98. values.set(name, value);
  99. }
  100. const value = (name) => {
  101. if (!values.has(name)) {
  102. values.set(name, new dlc.Value(name));
  103. }
  104. return values.get(name);
  105. };
  106. this.nodes = [];
  107. for (const node of graph.nodes) {
  108. if (node.type === 'Input') {
  109. this.inputs.push(new dlc.Argument(node.name, node.inputs.map((input) => value(input))));
  110. continue;
  111. }
  112. this.nodes.push(new dlc.Node(metadata, version, node, value));
  113. }
  114. }
  115. };
  116. dlc.Argument = class {
  117. constructor(name, value, type = null) {
  118. this.name = name;
  119. this.value = value;
  120. this.type = type;
  121. }
  122. };
  123. dlc.Value = class {
  124. constructor(name, type, initializer) {
  125. if (typeof name !== 'string') {
  126. throw new dlc.Error(`Invalid value identifier '${JSON.stringify(name)}'.`);
  127. }
  128. this.name = name;
  129. this.type = type;
  130. this.initializer = initializer;
  131. }
  132. };
  133. dlc.Node = class {
  134. constructor(metadata, version, obj, value) {
  135. this.type = metadata.type(obj.type);
  136. this.name = obj.name;
  137. this.inputs = [];
  138. this.outputs = [];
  139. this.attributes = [];
  140. const inputs = Array.isArray(obj.inputs) ? Array.from(obj.inputs).map((name) => value(name)) : [];
  141. if (version !== 3 && Array.isArray(this.type.inputs) && inputs.length === this.type.inputs.length) {
  142. for (let i = 0; i < inputs.length; i++) {
  143. const argument = new dlc.Argument(this.type.inputs[i].name, [inputs[i]]);
  144. this.inputs.push(argument);
  145. }
  146. } else if (inputs.length > 0) {
  147. const argument = new dlc.Argument(inputs.length === 1 ? 'input' : 'inputs', inputs);
  148. this.inputs.push(argument);
  149. }
  150. const outputs = Array.isArray(obj.outputs) ? Array.from(obj.outputs).map((name) => value(name)) : [];
  151. if (Array.isArray(this.type.outputs) && outputs.length === this.type.outputs.length) {
  152. for (let i = 0; i < outputs.length; i++) {
  153. const argument = new dlc.Argument(this.type.outputs[i].name, [outputs[i]]);
  154. this.outputs.push(argument);
  155. }
  156. } else if (outputs.length > 0) {
  157. const argument = new dlc.Argument(outputs.length === 1 ? 'output' : 'outputs', outputs);
  158. this.outputs.push(argument);
  159. }
  160. if (obj.attributes) {
  161. for (const attr of obj.attributes) {
  162. if (attr.name === 'OutputDims') {
  163. continue;
  164. }
  165. const schema = metadata.attribute(obj.type, attr.name);
  166. let type = attr.type;
  167. switch (type) {
  168. case 'tensor': {
  169. const tensor = attr.data;
  170. const type = new dlc.TensorType(tensor.dtype, tensor.shape);
  171. value = new dlc.Tensor(tensor.name, type, tensor.data);
  172. break;
  173. }
  174. default: {
  175. value = attr.data;
  176. }
  177. }
  178. if (schema && schema.type) {
  179. type = schema.type;
  180. value = dlc.Utility.enum(version, type, value);
  181. }
  182. const attribute = new dlc.Argument(attr.name, value, type);
  183. this.attributes.push(attribute);
  184. }
  185. }
  186. if (obj.weights) {
  187. for (const tensor of obj.weights) {
  188. const type = new dlc.TensorType(tensor.data.dtype, tensor.shape);
  189. const value = new dlc.Value('', type, new dlc.Tensor(tensor.name, type, tensor.data));
  190. this.inputs.push(new dlc.Argument(tensor.name, [value]));
  191. }
  192. }
  193. }
  194. };
  195. dlc.TensorType = class {
  196. constructor(dataType, shape) {
  197. this.dataType = dataType || '?';
  198. this.shape = new dlc.TensorShape(shape);
  199. }
  200. toString() {
  201. return this.dataType + this.shape.toString();
  202. }
  203. };
  204. dlc.TensorShape = class {
  205. constructor(dimensions) {
  206. this.dimensions = Array.from(dimensions);
  207. }
  208. toString() {
  209. if (Array.isArray(this.dimensions) && this.dimensions.length > 0) {
  210. return `[${this.dimensions.map((dimension) => dimension.toString()).join(',')}]`;
  211. }
  212. return '';
  213. }
  214. };
  215. dlc.Tensor = class {
  216. constructor(name, type, data) {
  217. this.name = name;
  218. this.type = type;
  219. if (data instanceof Uint8Array) {
  220. this.encoding = '<';
  221. this.values = data;
  222. } else {
  223. this.encoding = '|';
  224. switch (type.dataType) {
  225. case 'uint8': this.values = data.bytes; break;
  226. case 'float32': this.values = data.floats; break;
  227. default: throw new dlc.Error(`Unsupported tensor data type '${type.dataType}'.`);
  228. }
  229. }
  230. }
  231. };
  232. dlc.Container = class {
  233. static async open(context) {
  234. const entries = await context.peek('zip');
  235. if (entries instanceof Map) {
  236. const model = entries.get('model');
  237. const params = entries.get('model.params');
  238. const metadata = entries.get('dlc.metadata');
  239. if (model) {
  240. const signature = dlc.Container._signature(model);
  241. if (signature && (signature.identifier === 'NETD' || signature.major === 2)) {
  242. return new dlc.Container(context, model, params, metadata);
  243. }
  244. }
  245. if (params) {
  246. const signature = dlc.Container._signature(params);
  247. if (signature && signature.identifier === 'NETP') {
  248. return new dlc.Container(context, model, params, metadata);
  249. }
  250. }
  251. return null;
  252. }
  253. const stream = context.stream;
  254. const signature = dlc.Container._signature(stream);
  255. switch (signature.identifier) {
  256. case 'NETD':
  257. return new dlc.Container(context, stream, undefined, undefined);
  258. case 'NETP':
  259. case 'NR64':
  260. return new dlc.Container(context, undefined, stream, undefined);
  261. default:
  262. return null;
  263. }
  264. }
  265. constructor(context, model, params, metadata) {
  266. this.context = context;
  267. this._model = model;
  268. this._params = params;
  269. this._metadata = metadata;
  270. }
  271. async read() {
  272. if (this._model === undefined) {
  273. this._model = await this._fetch('model');
  274. }
  275. if (this._params === undefined) {
  276. this._params = await this._fetch('model.params');
  277. }
  278. if (this._metadata === undefined) {
  279. this._metadata = await this._fetch('dlc.metadata');
  280. }
  281. delete this.context;
  282. this.graphs = [];
  283. this.metadata = new Map();
  284. if (this._model) {
  285. this.format = 'DLC';
  286. const stream = this._model;
  287. delete this._model;
  288. const signature = dlc.Container._signature(stream);
  289. if (signature.major === 2) {
  290. throw new dlc.Error("File contains undocumented DLC v2 data.");
  291. } else if (signature.identifier === 'NETD' && (signature.major === 3 || signature.major === undefined)) {
  292. this.version = { major: signature.major || 3, minor: signature.minor || 0 };
  293. this.graph = dlc.Container._model3(stream, signature.offset);
  294. this.graphs = [this.graph];
  295. } else if (signature.identifier === 'NETD' && signature.major === 4) {
  296. this.version = { major: signature.major, minor: signature.minor };
  297. this.graphs = dlc.Container._model4(stream);
  298. } else {
  299. const buffer = stream.peek(Math.min(stream.length, 16));
  300. const content = Array.from(buffer).map((c) => (c < 16 ? '0' : '') + c.toString(16)).join('');
  301. throw new dlc.Error(`File contains undocumented '${content}' data.`);
  302. }
  303. }
  304. if (this._params) {
  305. this.format = this.format || 'DLC Weights';
  306. const stream = this._params;
  307. delete this._params;
  308. const signature = dlc.Container._signature(stream);
  309. if (signature.major === 2) {
  310. throw new dlc.Error("File contains undocumented DLC v2 data.");
  311. } else if (signature.identifier === 'NETP' && (signature.major === 3 || signature.major === undefined)) {
  312. this.version = this.graphs.length > 0 ? this.version : { major: signature.major || 3, minor: signature.minor || 0 };
  313. this.graph = dlc.Container._params3(stream, signature, this.graph);
  314. this.graphs = [this.graph];
  315. } else if ((signature.identifier === 'NETP' || signature.identifier === 'NR64') && signature.major === 4) {
  316. dlc.Container._params4(stream, this.graphs, signature);
  317. } else {
  318. const buffer = stream.peek(Math.min(stream.length, 16));
  319. const content = Array.from(buffer).map((c) => (c < 16 ? '0' : '') + c.toString(16)).join('');
  320. throw new dlc.Error(`File contains undocumented '${content}' data.`);
  321. }
  322. }
  323. if (this._metadata) {
  324. const stream = this._metadata;
  325. delete this._metadata;
  326. const reader = text.Reader.open(stream);
  327. for (;;) {
  328. const line = reader.read('\n');
  329. if (line === undefined) {
  330. break;
  331. }
  332. const index = line.indexOf('=');
  333. if (index === -1) {
  334. break;
  335. }
  336. const key = line.substring(0, index);
  337. const value = line.substring(index + 1);
  338. this.metadata.set(key, value);
  339. }
  340. }
  341. }
  342. static _model3(stream, offset) {
  343. let model = null;
  344. try {
  345. const buffer = new Uint8Array(offset > 0 ? stream.peek().subarray(offset) : stream.peek());
  346. const reader = flatbuffers.BinaryReader.open(buffer);
  347. model = dlc.schema.v3.Model.decode(reader, reader.root);
  348. } catch (error) {
  349. const message = error && error.message ? error.message : error.toString();
  350. throw new dlc.Error(`File format is not dlc.v3.NETD (${message.replace(/\.$/, '')}).`);
  351. }
  352. model.tensors = [];
  353. const updateAttribute = (attr) => {
  354. switch (attr.type) {
  355. case 1: return ['boolean', attr.bool_value];
  356. case 2: return ['int32', attr.int32_value];
  357. case 3: return ['uint32', attr.uint32_value];
  358. case 4: return ['float32', attr.float32_value];
  359. case 5: return ['string', attr.string_value];
  360. case 7: return ['byte[]', Array.from(attr.byte_list)];
  361. case 8: return ['int32[]', Array.from(attr.int32_list)];
  362. case 9: return ['float32[]', Array.from(attr.float32_list)];
  363. case 11: {
  364. const obj = {};
  365. let index = 0;
  366. let list = true;
  367. for (const attribute of attr.attributes) {
  368. const name = attribute.name;
  369. const [, data] = updateAttribute(attribute);
  370. obj[name] = data;
  371. list = list && index.toString() === attribute.name;
  372. index++;
  373. }
  374. return list ? ['', Object.values(obj)] : ['', obj];
  375. }
  376. default:
  377. throw new dlc.Error(`Unsupported attribute type '${attr.type}'.`);
  378. }
  379. };
  380. for (const node of model.nodes) {
  381. for (const attribute of node.attributes) {
  382. const [type, data] = updateAttribute(attribute);
  383. attribute.type = type;
  384. attribute.data = data;
  385. }
  386. }
  387. return model;
  388. }
  389. static _model4(stream) {
  390. let model = null;
  391. try {
  392. const buffer = new Uint8Array(stream.peek().subarray(8));
  393. const reader = flatbuffers.BinaryReader.open(buffer);
  394. model = dlc.schema.v4.Model.decode(reader, reader.root);
  395. } catch (error) {
  396. const message = error && error.message ? error.message : error.toString();
  397. throw new dlc.Error(`File format is not dlc.v4.NETD (${message.replace(/\.$/, '')}).`);
  398. }
  399. const dataType = (value) => {
  400. switch (value) {
  401. case 0x0008: return 'int8';
  402. case 0x0016: return 'int16';
  403. case 0x0032: return 'int32';
  404. case 0x0064: return 'int64';
  405. case 0x0108: return 'uint8';
  406. case 0x0116: return 'uint16';
  407. case 0x0132: return 'uint32';
  408. case 0x0164: return 'uint64';
  409. case 0x0216: return 'float16';
  410. case 0x0232: return 'float32';
  411. case 0x0304: return 'qint4';
  412. case 0x0308: return 'qint8';
  413. case 0x0316: return 'qint16';
  414. case 0x0332: return 'qint32';
  415. case 0x0404: return 'quint4';
  416. case 0x0408: return 'quint8';
  417. case 0x0416: return 'quint16';
  418. case 0x0432: return 'quint32';
  419. case 0x0508: return 'boolean';
  420. case 0x0608: return 'string';
  421. case 0x7fffffff: return 'undefined';
  422. default: throw new dlc.Error(`Unsupported data type '${JSON.stringify(value)}'.`);
  423. }
  424. };
  425. const updateTensor = (tensor) => {
  426. tensor.dtype = dataType(tensor.dtype);
  427. tensor.output_dtype = dataType(tensor.output_dtype);
  428. };
  429. for (const graph of model.graphs) {
  430. for (const node of graph.nodes) {
  431. for (const attribute of node.attributes) {
  432. switch (attribute.kind) {
  433. case 0: {
  434. const value = attribute.value;
  435. switch (value.kind) {
  436. case 0x7fffffff:
  437. attribute.data = value.string_value;
  438. attribute.type = 'string';
  439. break;
  440. case 0x0032:
  441. attribute.data = value.int32_value;
  442. break;
  443. case 0x0108:
  444. attribute.data = value.int32_value;
  445. attribute.type = 'int8';
  446. break;
  447. case 0x0132:
  448. attribute.data = value.int32_value;
  449. attribute.type = 'int32';
  450. break;
  451. case 0x0232:
  452. attribute.data = value.float32_value;
  453. attribute.type = 'float32';
  454. break;
  455. case 0x0508:
  456. attribute.data = value.int32_value !== 0;
  457. attribute.type = 'boolean';
  458. break;
  459. case 0x0608:
  460. attribute.data = value.string_value;
  461. attribute.type = 'string';
  462. break;
  463. default:
  464. throw new dlc.Error(`Unknown attribute value kind '${value.kind}'.`);
  465. }
  466. break;
  467. }
  468. case 1: {
  469. const tensor = attribute.tensor;
  470. updateTensor(tensor);
  471. attribute.type = 'tensor';
  472. attribute.data = tensor;
  473. break;
  474. }
  475. default: {
  476. throw new dlc.Error(`Unknown attribute kind '${attribute.kind}'.`);
  477. }
  478. }
  479. }
  480. }
  481. for (const tensor of graph.tensors) {
  482. updateTensor(tensor);
  483. }
  484. }
  485. return model.graphs;
  486. }
  487. static _params3(stream, signature, graph) {
  488. let params = null;
  489. try {
  490. const buffer = new Uint8Array(signature === 'NETP' ? stream.peek() : stream.peek().subarray(8));
  491. const reader = flatbuffers.BinaryReader.open(buffer);
  492. params = dlc.schema.v3.ModelParameters.decode(reader, reader.root);
  493. } catch (error) {
  494. const message = error && error.message ? error.message : error.toString();
  495. throw new dlc.Error(`File format is not dlc.v3.NETP (${message.replace(/\.$/, '')}).`);
  496. }
  497. if (!graph) {
  498. graph = new dlc.schema.v3.ModelParameters();
  499. graph.nodes = new Array(params.nodes.length);
  500. graph.tensors = [];
  501. for (let i = 0; i < graph.nodes.length; i++) {
  502. const node = new dlc.schema.v3.Node();
  503. node.type = 'Weights';
  504. node.name = params.nodes[i].name;
  505. node.inputs = [];
  506. node.outputs = [];
  507. node.attributes = [];
  508. graph.nodes[i] = node;
  509. }
  510. }
  511. const dataType = (value) => {
  512. switch (value) {
  513. case null: return '?';
  514. case 6: return 'uint8';
  515. case 9: return 'float32';
  516. default:
  517. throw new dlc.Error(`Unsupported data type '${JSON.stringify(value)}'.`);
  518. }
  519. };
  520. const weights = new Map(params.nodes.map((node) => [node.name, node.weights]));
  521. for (const node of graph.nodes) {
  522. if (weights.has(node.name)) {
  523. const tensors = weights.get(node.name);
  524. for (const tensor of tensors) {
  525. tensor.data.dtype = dataType(tensor.data.dtype);
  526. }
  527. node.weights = tensors;
  528. }
  529. }
  530. return graph;
  531. }
  532. static _params4(stream, graphs, signature) {
  533. let buffer = stream.peek().subarray(8);
  534. let buffers = null;
  535. if (signature.major === 4 && signature.identifier === 'NR64') {
  536. try {
  537. const reader = flatbuffers.BinaryReader.open(buffer);
  538. const nr64 = dlc.schema.v4.ModelParameters64.decode(reader, reader.root);
  539. buffers = nr64.buffers;
  540. buffer = nr64.params;
  541. } catch (error) {
  542. const message = error && error.message ? error.message : error.toString();
  543. throw new dlc.Error(`File format is not dlc.v4.NR64 (${message.replace(/\.$/, '')}).`);
  544. }
  545. }
  546. let params = null;
  547. try {
  548. const reader = flatbuffers.BinaryReader.open(buffer);
  549. params = dlc.schema.v4.ModelParameters.decode(reader, reader.root);
  550. } catch (error) {
  551. const message = error && error.message ? error.message : error.toString();
  552. throw new dlc.Error(`File format is not dlc.v4.NETP (${message.replace(/\.$/, '')}).`);
  553. }
  554. if (graphs.length === 0) {
  555. throw new dlc.Error('Model definition not available.');
  556. }
  557. const weights = new Map(params.graphs.map((graph) => [graph.name, graph]));
  558. for (const graph of graphs) {
  559. const params = weights.get(graph.name);
  560. const tensors = new Map(params.tensors.map((tensor) => [tensor.name, tensor]));
  561. let index = 0;
  562. graph.tensors.sort((a, b) => a.name.localeCompare(b.name));
  563. for (const tensor of graph.tensors) {
  564. if (tensor.location === 4) {
  565. if (buffers && index < buffers.length) {
  566. tensor.data = buffers[index++].bytes;
  567. } else if (tensors.has(tensor.name)) {
  568. tensor.data = tensors.get(tensor.name).bytes;
  569. } else {
  570. throw new dlc.Error(`Unknown tensor `);
  571. }
  572. }
  573. }
  574. for (let i = 0; i < graph.nodes.length; i++) {
  575. const node = graph.nodes[i];
  576. const tensors = new Map(params.nodes[i].tensors.map((tensor) => [tensor.name, tensor]));
  577. for (const attribute of node.attributes) {
  578. const tensor = attribute.tensor;
  579. if (tensor) {
  580. if (buffers && index < buffers.length) {
  581. tensor.data = buffers[index++].bytes;
  582. } else if (tensors.has(tensor.name)) {
  583. tensor.data = tensors.get(tensor.name).bytes;
  584. } else {
  585. throw new dlc.Error(`Unknown tensor `);
  586. }
  587. }
  588. }
  589. }
  590. }
  591. }
  592. async _fetch(name) {
  593. try {
  594. const context = await this.context.fetch(name);
  595. return context.stream;
  596. } catch {
  597. return null;
  598. }
  599. }
  600. static _signature(stream) {
  601. const signature = {};
  602. signature.identifier = '?';
  603. signature.offset = 0;
  604. if (stream) {
  605. const buffer = stream.peek(Math.min(stream.length, 16));
  606. if (buffer[0] === 0xD5 && buffer[1] === 0x0A) {
  607. delete signature.identifier;
  608. if (buffer[3] === 0x00 && buffer[5] === 0x00 && buffer[6] === 0x00) {
  609. signature.major = buffer[2] | buffer[3] << 8;
  610. signature.minor = buffer[4] | buffer[5] << 8;
  611. if (signature.major > 2) {
  612. signature.identifier = '?';
  613. }
  614. }
  615. }
  616. if (signature.identifier === '?') {
  617. const offset = signature.major === undefined ? 0 : 8;
  618. const reader = flatbuffers.BinaryReader.open(stream, offset);
  619. if (reader) {
  620. signature.identifier = reader.identifier;
  621. signature.offset = offset;
  622. }
  623. }
  624. }
  625. return signature;
  626. }
  627. };
  628. dlc.Utility = class {
  629. static enum(version, name, value) {
  630. switch (version) {
  631. case 3: version = 'v3'; break;
  632. case 4: version = 'v4'; break;
  633. default: version = '';
  634. }
  635. const schema = dlc.schema[version];
  636. if (schema && name) {
  637. const type = schema[name];
  638. if (type) {
  639. dlc.Utility[version] = dlc.Utility[version] || new Map();
  640. const enums = dlc.Utility[version];
  641. if (!enums.has(name)) {
  642. const entries = new Map(Object.entries(type).map(([key, value]) => [value, key]));
  643. enums.set(name, entries);
  644. }
  645. const values = enums.get(name);
  646. if (values.has(value)) {
  647. return values.get(value);
  648. }
  649. }
  650. }
  651. return value;
  652. }
  653. };
  654. dlc.Error = class extends Error {
  655. constructor(message) {
  656. super(message);
  657. this.name = 'Error loading DLC model.';
  658. }
  659. };
  660. export const ModelFactory = dlc.ModelFactory;