HexdumpScreen.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import {FsUtils} from "../lib/FsUtils";
  2. class HexdumpScreen {
  3. constructor(data) {
  4. this.path = data;
  5. this.offset = 0;
  6. }
  7. start() {
  8. this.size = FsUtils.stat(this.path)[0].size;
  9. this.file = FsUtils.open(this.path);
  10. this.header = hmUI.createWidget(hmUI.widget.TEXT, {
  11. x: 0,
  12. y: 0,
  13. w: 192,
  14. h: 72,
  15. align_h: hmUI.align.CENTER_H,
  16. align_v: hmUI.align.CENTER_V,
  17. text: "hello",
  18. color: 0x999999
  19. });
  20. this.columns = [];
  21. this.textColumns = [];
  22. for(let i = 0; i < 4; i++) {
  23. this.columns.push(hmUI.createWidget(hmUI.widget.TEXT, {
  24. x: 4 + (26*i),
  25. y: 96,
  26. w: 30,
  27. h: 320,
  28. text_size: 18,
  29. text: "00\n00\n00",
  30. color: 0xFFFFFF,
  31. align_h: hmUI.align.CENTER_H
  32. }));
  33. this.textColumns.push(hmUI.createWidget(hmUI.widget.TEXT, {
  34. x: 116 + (18*i),
  35. y: 96,
  36. w: 18,
  37. h: 320,
  38. text_size: 18,
  39. text: "a",
  40. color: 0xAAAAAA,
  41. align_h: hmUI.align.CENTER_H
  42. }))
  43. }
  44. hmUI.createWidget(hmUI.widget.IMG, {
  45. x: 0,
  46. y: 0,
  47. w: 192,
  48. h: 245,
  49. src: ""
  50. }).addEventListener(hmUI.event.CLICK_UP, () => {
  51. this.refresh(this.offset - 64);
  52. })
  53. hmUI.createWidget(hmUI.widget.IMG, {
  54. x: 0,
  55. y: 245,
  56. w: 192,
  57. h: 245,
  58. src: ""
  59. }).addEventListener(hmUI.event.CLICK_UP, () => {
  60. this.refresh(this.offset + 64);
  61. })
  62. this.refresh(this.offset);
  63. }
  64. refresh(newOffset) {
  65. const lines = 16;
  66. if(newOffset > this.size) return;
  67. if(newOffset < 0) return;
  68. const headerText = newOffset.toString(16) + " / " + this.size.toString(16);
  69. this.header.setProperty(hmUI.prop.TEXT, headerText.toUpperCase());
  70. const buffer = new ArrayBuffer(4 * lines);
  71. const view = new Uint8Array(buffer);
  72. hmFS.seek(this.file, newOffset, hmFS.SEEK_SET);
  73. hmFS.read(this.file, buffer, 0, 4 * lines);
  74. // Update byte columns
  75. for(let i = 0; i < 4; i++) {
  76. let data = "", text = "";
  77. for(let j = 0; j < lines; j++) {
  78. if(newOffset + (4*j) + i > this.size) break;
  79. let charCode = view[(4*j) + i];
  80. if(charCode < 32 || charCode > 126)
  81. charCode = 46; // .
  82. data += view[(4*j) + i].toString(16).padStart(2, "0").toUpperCase() + "\n";
  83. text += String.fromCharCode(charCode) + "\n";
  84. }
  85. this.columns[i].setProperty(hmUI.prop.TEXT, data);
  86. this.textColumns[i].setProperty(hmUI.prop.TEXT, text);
  87. }
  88. this.offset = newOffset;
  89. }
  90. }
  91. let __$$app$$__ = __$$hmAppManager$$__.currentApp;
  92. let __$$module$$__ = __$$app$$__.current;
  93. __$$module$$__.module = DeviceRuntimeCore.Page({
  94. onInit(p) {
  95. new HexdumpScreen(p).start();
  96. }
  97. });