dlc.js 26 KB

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