dlc.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. var dlc = dlc || {};
  2. var text = text || require('./text');
  3. dlc.ModelFactory = class {
  4. match(context) {
  5. return dlc.Container.open(context);
  6. }
  7. open(context, match) {
  8. return context.require('./dlc-schema').then(() => {
  9. dlc.schema = flatbuffers.get('dlc').dlc;
  10. const container = match;
  11. return new dlc.Model(container);
  12. });
  13. }
  14. };
  15. dlc.Model = class {
  16. constructor(container) {
  17. if (container.metadata.size > 0) {
  18. const converter = container.metadata.get('converter-command');
  19. if (converter) {
  20. const source = converter.split(' ').shift().trim();
  21. if (source.length > 0) {
  22. this._source = source;
  23. const version = container.metadata.get('converter-version');
  24. if (version) {
  25. this._source = this._source + ' v' + version;
  26. }
  27. }
  28. }
  29. }
  30. container.model;
  31. container.params;
  32. this._graphs = [];
  33. }
  34. get format() {
  35. return 'DLC';
  36. }
  37. get source() {
  38. return this._source;
  39. }
  40. get graphs() {
  41. return this._graphs;
  42. }
  43. };
  44. dlc.Container = class {
  45. static open(context) {
  46. const entries = context.entries('zip');
  47. if (entries.size > 0) {
  48. const model = entries.get('model');
  49. const params = entries.get('model.params');
  50. if (model || params) {
  51. return new dlc.Container(model, params, entries.get('dlc.metadata'));
  52. }
  53. }
  54. const stream = context.stream;
  55. switch (dlc.Container._idenfitier(stream)) {
  56. case 'NETD':
  57. return new dlc.Container(stream, null, null);
  58. case 'NETP':
  59. return new dlc.Container(null, stream, null);
  60. default:
  61. break;
  62. }
  63. return null;
  64. }
  65. constructor(model, params, metadata) {
  66. this._model = model || null;
  67. this._params = params || null;
  68. this._metadata = metadata || new Uint8Array(0);
  69. }
  70. get model() {
  71. if (this._model && this._model.peek) {
  72. const stream = this._model;
  73. const reader = this._open(stream, 'NETD');
  74. stream.seek(0);
  75. this._model = dlc.schema.NetDefinition.decode(reader, reader.root);
  76. throw new dlc.Error("File contains undocumented '" + reader.identifier + "' data.");
  77. }
  78. return this._model;
  79. }
  80. get params() {
  81. if (this._params && this._params.peek) {
  82. const stream = this._params;
  83. const reader = this._open(stream, 'NETP');
  84. stream.seek(0);
  85. this._params = dlc.schema.NetParameter.decode(reader, reader.root);
  86. throw new dlc.Error("File contains undocumented '" + reader.identifier + "' data.");
  87. }
  88. return this._params;
  89. }
  90. get metadata() {
  91. if (this._metadata && this._metadata.peek) {
  92. const reader = text.Reader.open(this._metadata);
  93. const metadata = new Map();
  94. for (;;) {
  95. const line = reader.read();
  96. if (line === undefined) {
  97. break;
  98. }
  99. const index = line.indexOf('=');
  100. if (index === -1) {
  101. break;
  102. }
  103. const key = line.substring(0, index);
  104. const value = line.substring(index + 1);
  105. metadata.set(key, value);
  106. }
  107. this._metadata = metadata;
  108. }
  109. return this._metadata;
  110. }
  111. _open(stream, identifier) {
  112. const signature = [ 0xD5, 0x0A, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 ];
  113. if (signature.length <= stream.length && stream.peek(signature.length).every((value, index) => value === signature[index])) {
  114. stream.read(8);
  115. }
  116. const buffer = stream.read();
  117. const reader = flatbuffers.BinaryReader.open(buffer);
  118. if (identifier != reader.identifier) {
  119. throw new dlc.Error("File contains undocumented '" + reader.identifier + "' data.");
  120. }
  121. return reader;
  122. }
  123. static _idenfitier(stream) {
  124. const signature = [ 0xD5, 0x0A, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 ];
  125. if (stream.length > 16 && stream.peek(signature.length).every((value, index) => value === signature[index])) {
  126. const buffer = stream.peek(16).slice(8, 16);
  127. const reader = flatbuffers.BinaryReader.open(buffer);
  128. return reader.identifier;
  129. }
  130. else if (stream.length > 8) {
  131. const buffer = stream.peek(8);
  132. const reader = flatbuffers.BinaryReader.open(buffer);
  133. return reader.identifier;
  134. }
  135. return '';
  136. }
  137. };
  138. dlc.Error = class extends Error {
  139. constructor(message) {
  140. super(message);
  141. this.name = 'Error loading DLC model.';
  142. this.stack = undefined;
  143. }
  144. };
  145. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  146. module.exports.ModelFactory = dlc.ModelFactory;
  147. }