PlayerOutput.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import EditorUI = require("../EditorUI");
  23. import Preferences = require("../../editor/Preferences");
  24. class PlayerOutput extends Atomic.UIWindow {
  25. output: Atomic.UIEditField;
  26. closeOnStop: Atomic.UICheckBox;
  27. errorsFileLine: Array<string>;
  28. constructor() {
  29. super();
  30. var view = EditorUI.getView();
  31. view.addChild(this);
  32. this.text = "Player Output";
  33. this.load("AtomicEditor/editor/ui/playeroutput.tb.txt");
  34. this.output = <Atomic.UIEditField> this.getWidget("output");
  35. this.closeOnStop = <Atomic.UICheckBox> this.getWidget("closeonstop");
  36. this.closeOnStop.value = Preferences.getInstance().editorFeatures.closePlayerLog ? 1 : 0;
  37. this.errorsFileLine = new Array();
  38. (<Atomic.UIButton>this.getWidget("closebutton")).onClick = () => {
  39. this.close();
  40. };
  41. this.subscribeToEvent(this, Atomic.UIWidgetEvent((data) => this.handleWidgetEvent(data)));
  42. this.subscribeToEvent(Editor.EditorPlayerLogEvent((ev: Editor.EditorPlayerLogEvent) => this.handlePlayerLog(ev)));
  43. this.resizeToFitContent();
  44. this.center();
  45. this.setFocus();
  46. }
  47. handlePlayerLog(ev: Editor.EditorPlayerLogEvent) {
  48. var text = this.output.text;
  49. if (text.length > 32768)
  50. this.output.text = "";
  51. this.output.appendText( this.wakeOnError(ev.message) + "\n");
  52. this.output.scrollTo(0, 0xffffff);
  53. }
  54. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  55. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  56. var id = ev.target.id;
  57. if (id == "closeonstop") {
  58. Preferences.getInstance().editorFeatures.closePlayerLog = this.closeOnStop.value > 0 ? true : false;
  59. Preferences.getInstance().write();
  60. return true;
  61. } else {
  62. if ( id.indexOf("ERR") > -1 ) { // ERRxxx this is an encoded error index id
  63. var indx = parseInt( id.replace ("ERR", "")); // remove the ERR string so it will parse
  64. var colonpos = this.errorsFileLine[indx].indexOf( ":"); // split the filename and line
  65. var fn = ToolCore.toolSystem.project.projectPath + "Resources/" + this.errorsFileLine[indx].substr(0, colonpos) + ".js";
  66. var ln = this.errorsFileLine[indx].substr(colonpos + 1, (this.errorsFileLine[indx].length - colonpos));
  67. var line = parseInt(ln);
  68. this.sendEvent(Editor.EditorEditResourceEventData({ path: fn, lineNumber: line }));
  69. }
  70. }
  71. }
  72. }
  73. wakeOnError ( message: string ) { // look for Errors in the transcript, insert links to bring up the offending file
  74. var ecnum = message.indexOf("ERROR");
  75. if ( ecnum == -1 ) return message; // send it back if no "Error" string is present
  76. if ( this.errorsFileLine.length > 100 ) return message; // limit the number of unique errors, geez
  77. var linenum;
  78. var linepos = message.indexOf("Line", ecnum); // find the Line token to harvest the line number
  79. if ( linepos > -1 ) { // we found it
  80. var lnsrt = message.indexOf(" ", linepos + 1 ); // find the next 2 spaces, and substr it
  81. var lnstp = message.indexOf(" ", lnsrt + 1 );
  82. linenum = ":" + message.substr(lnsrt, lnstp - lnsrt).trim() + ")"; // create our next search string
  83. }
  84. else return message; // send it back original if the Line signature is not present
  85. var fnpos = message.indexOf( linenum ); // find the :linenumber) position
  86. if ( fnpos == -1 ) return message; // more checking
  87. var fnsrt = fnpos - 1; // find the start of the string, the hard way.
  88. while (message.charAt(fnsrt) != "(" && message.charAt(fnsrt) != " " && message.charAt(fnsrt) != "\n" && fnsrt > 0)
  89. fnsrt--;
  90. if (message.charAt(fnsrt) == "(") fnsrt++; // get rid of the open paren that is there now with Duk 2.x
  91. var fnstp = fnpos + 1; // find the end of the string
  92. while (message.charAt(fnstp) != ")" && message.charAt(fnstp) != " " && message.charAt(fnstp) != "\n" && fnstp < message.length - 1 )
  93. fnstp++;
  94. var errFnLn = message.substr(fnsrt, fnstp - fnsrt).trim(); // and this is your problem, right there.
  95. var link = this.errorsFileLine.indexOf(errFnLn); // see if we already have this signature
  96. if ( link == -1 ) { // if not logged yet, do so, otherwise use an existing link
  97. this.errorsFileLine.push(errFnLn);
  98. link = this.errorsFileLine.length - 1;
  99. }
  100. var lnkwidget = ` <widget TBSkinImage: skin: MagnifierBitmap, text: "..." id: ERR` + link.toString() + ` > `;
  101. var newMessage = message.replace(errFnLn, errFnLn + lnkwidget);
  102. return newMessage;
  103. };
  104. }
  105. export = PlayerOutput;