update_pbjs.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* jshint esversion: 6 */
  2. /* eslint "indent": [ "error", 4, { "SwitchCase": 1 } ] */
  3. /* eslint "no-console": off */
  4. const fs = require('fs');
  5. const process = require('process');
  6. const pattern = process.argv[2];
  7. const file = process.argv[3];
  8. const variable = process.argv[4];
  9. const type = process.argv[5];
  10. const count = parseInt(process.argv[6]);
  11. let arrayType = '';
  12. let dataViewMethod = '';
  13. let shift = 0;
  14. switch (type) {
  15. case 'float':
  16. arrayType = 'Float32Array';
  17. dataViewMethod = 'getFloat32';
  18. shift = '2';
  19. break;
  20. case 'double':
  21. arrayType = 'Float64Array';
  22. dataViewMethod = 'getFloat64';
  23. shift = '3';
  24. break;
  25. default:
  26. console.log('ERROR: Type is not supported.');
  27. process.exit(1);
  28. break;
  29. }
  30. const source = fs.readFileSync(file, 'utf-8');
  31. let search = '';
  32. let replace = '';
  33. switch (pattern) {
  34. case 'array':
  35. search = `if ((tag & 7) === 2) {
  36. var end2 = reader.uint32() + reader.pos;
  37. while (reader.pos < end2)
  38. message.$(variable).push(reader.$(type)());
  39. } else`;
  40. replace = `if ((tag & 7) === 2) {
  41. var end2 = reader.uint32() + reader.pos;
  42. if (message.$(variable).length == 0 && (end2 - reader.pos) > 1048576) {
  43. var $(variable)Length = end2 - reader.pos;
  44. var $(variable)View = new DataView(reader.buf.buffer, reader.buf.byteOffset + reader.pos, $(variable)Length);
  45. $(variable)Length = $(variable)Length >>> $(shift);
  46. var $(variable) = new $(arrayType)($(variable)Length);
  47. for (var i = 0; i < $(variable)Length; i++) {
  48. $(variable)[i] = $(variable)View.$(dataViewMethod)(i << $(shift), true);
  49. }
  50. message.$(variable) = $(variable);
  51. reader.pos = end2;
  52. }
  53. else {
  54. while (reader.pos < end2)
  55. message.$(variable).push(reader.$(type)());
  56. }
  57. } else`;
  58. break;
  59. case 'enumeration':
  60. search = `if (!(message.$(variable) && message.$(variable).length))
  61. message.$(variable) = [];
  62. if ((tag & 7) === 2) {
  63. var end2 = reader.uint32() + reader.pos;
  64. while (reader.pos < end2)
  65. message.$(variable).push(reader.$(type)());
  66. } else
  67. message.$(variable).push(reader.$(type)());
  68. break;`;
  69. replace = `if (!(message.$(variable) && message.$(variable).length)) {
  70. if (message.$(variable) != -1) {
  71. message.$(variable) = [];
  72. message.$(variable)Count = 0;
  73. }
  74. }
  75. if (message.$(variable)Count < 1000000) {
  76. if ((tag & 7) === 2) {
  77. var end2 = reader.uint32() + reader.pos;
  78. while (reader.pos < end2) {
  79. message.$(variable).push(reader.$(type)());
  80. message.$(variable)Count++;
  81. }
  82. }
  83. else {
  84. message.$(variable).push(reader.$(type)());
  85. message.$(variable)Count++;
  86. }
  87. }
  88. else {
  89. message.$(variable) = -1;
  90. if ((tag & 7) === 2) {
  91. var endx = reader.uint32() + reader.pos;
  92. while (reader.pos < endx)
  93. reader.$(type)();
  94. }
  95. else {
  96. reader.$(type)();
  97. }
  98. }
  99. break;`;
  100. break;
  101. default:
  102. console.log('ERROR: Unknown pattern.')
  103. process.exit(1);
  104. }
  105. search = search.split('$(variable)').join(variable);
  106. search = search.split('$(type)').join(type);
  107. replace = replace.split('$(variable)').join(variable);
  108. replace = replace.split('$(type)').join(type);
  109. replace = replace.split('$(arrayType)').join(arrayType);
  110. replace = replace.split('$(dataViewMethod)').join(dataViewMethod);
  111. replace = replace.split('$(shift)').join(shift);
  112. for (let i = 0; i < 8; i++) {
  113. search = search.split('\n').map((line) => ' ' + line).join('\n');
  114. replace = replace.split('\n').map((line) => ' ' + line).join('\n');
  115. const parts = source.split(search);
  116. if (parts.length == (count + 1)) {
  117. const target = parts.join(replace);
  118. fs.writeFileSync(file, target, 'utf-8');
  119. process.exit(0);
  120. }
  121. }
  122. console.log('ERROR: Replace failed.');
  123. process.exit(1);