@@ -19,7 +19,7 @@ if (getCookie("addTimestamp") === "false") {
1919}
2020
2121initializeToggleButton ( false ) ;
22- initialzeDebugConsole ( ) ;
22+ initializeDebugConsole ( ) ;
2323
2424
2525function setCookie ( cname , cvalue , exdays ) {
@@ -44,7 +44,7 @@ function getCookie(cname) {
4444 return "" ;
4545}
4646
47- function initialzeDebugConsole ( ) {
47+ function initializeDebugConsole ( ) {
4848 let debugConsole = document . createElement ( 'div' ) ;
4949 debugConsole . id = 'debugConsole' ;
5050 document . body . appendChild ( debugConsole ) ;
@@ -279,52 +279,128 @@ function setupConsoleLogPipe() {
279279
280280 parseMessageAndLog = ( message , logLevel , consoleLogFunction ) => {
281281 updateLogCounter ( logLevel ) ;
282- let index = 0 ;
283- let consoleArgs = [ ] ;
284- let consoleMessage = "" ;
285- let divMessage = "" ;
286-
287- // parse unity color tags to render them nicely in the console
288- while ( index <= message . length ) {
289- let startTagStart = message . indexOf ( "<color=" , index ) ;
290- if ( startTagStart == - 1 ) {
291- // Parse the reset of the message without styling
292- let remainingMessage = message . substring ( index ) ;
293- consoleMessage += `%c${ remainingMessage } ` ;
294- consoleArgs . push ( "color:inherit" ) ;
295- divMessage += remainingMessage ;
296- break ;
297- }
282+ let styledTextParts = parseUnityRichText ( message ) ;
283+
284+ let consoleText = '' ;
285+ let consoleStyle = [ ] ;
286+ let htmlText = '' ;
287+ styledTextParts . forEach ( textPart => {
288+ consoleText += textPart . toConsoleTextString ( ) ;
289+ consoleStyle . push ( textPart . toConsoleStyleString ( ) ) ;
290+ htmlText += textPart . toHTML ( ) ;
291+ } ) ;
292+ htmlLog ( htmlText . trim ( ) , logLevel ) ;
293+ consoleLogFunction ( consoleText , ...consoleStyle ) ;
294+ } ;
295+ }
296+
297+ class StyledTextPart {
298+ constructor ( text , styles ) {
299+ this . text = text ;
300+ this . styles = styles ;
301+ }
302+
303+ toHTML ( ) {
304+ if ( this . styles . length > 0 ) {
305+ return `<span style="${ this . styles . join ( ';' ) } ">${ this . text } </span>` ;
306+ }
307+ return this . text ;
308+ }
309+
310+ toConsoleStyleString ( ) {
311+ if ( this . styles . length > 0 ) {
312+ return this . styles . join ( ';' ) ;
313+ }
314+ return 'color:inherit' ;
315+ }
298316
299- if ( startTagStart > index ) {
300- let textBeforeTag = message . substring ( index , startTagStart ) ;
301- consoleMessage += `%c${ textBeforeTag } ` ;
302- consoleArgs . push ( "color:inherit" ) ;
303- divMessage += textBeforeTag ;
317+ toConsoleTextString ( ) {
318+ return `%c${ this . text } ` ;
319+ }
320+ }
321+
322+ function parseUnityRichText ( message ) {
323+ // Mapping for unity tags to css style tags
324+ const tagMap = {
325+ 'color' : { startTag : '<color=' , closeTag : '</color>' , styleTag : 'color:' , postfix : '' , hasParameter : true } ,
326+ 'size' : { startTag : '<size=' , closeTag : '</size>' , styleTag : 'font-size:' , postfix : 'px' , hasParameter : true } ,
327+ 'bold' : { startTag : '<b' , closeTag : '</b>' , styleTag : 'font-weight:' , styleValue : 'bold' , hasParameter : false } ,
328+ 'italic' : { startTag : '<i' , closeTag : '</i>' , styleTag : 'font-style:' , styleValue : 'italic' , hasParameter : false }
329+ } ;
330+
331+ let index = 0 ;
332+ const styledTextParts = [ ] ;
333+
334+ // Stack of applied styles, so we can also nest styling
335+ let styleStack = [ ] ;
336+ // next start index for each tag
337+ let nextStartIndex = [ ] ;
338+ // next end index for each tag
339+ let nextEndIndex = [ ] ;
340+ Object . keys ( tagMap ) . forEach ( key => {
341+ nextStartIndex [ key ] = message . indexOf ( tagMap [ key ] . startTag , index ) ;
342+ nextEndIndex [ key ] = message . indexOf ( tagMap [ key ] . closeTag , index ) ;
343+ } ) ;
344+
345+ while ( index < message . length ) {
346+ let nextTagFound = false ;
347+ let nextKey ;
348+ let fromArray = [ ] ;
349+ let nextIndex = message . length ;
350+
351+ // find the next tag start or end
352+ Object . keys ( tagMap ) . forEach ( key => {
353+ if ( nextStartIndex [ key ] >= 0 && nextStartIndex [ key ] < nextIndex ) {
354+ nextIndex = nextStartIndex [ key ] ;
355+ nextKey = key ;
356+ fromArray = nextStartIndex ;
357+ nextTagFound = true ;
304358 }
305359
306- let startTagEnd = message . indexOf ( ">" , startTagStart ) ;
307- let closingTag = message . indexOf ( "</color>" , startTagStart ) ;
308- if ( startTagEnd == - 1 || closingTag == - 1 ) {
309- // Tag is set up in the wrong way, abort parsing
310- defaultConsoleError ( { error : "Could not parse color tag, since no end tag was found" , startStartIndex : startTagStart , startEndIndex : startTagEnd , closingIndex : closingTag } ) ;
311- let remainingMessage = message . substring ( index ) ;
312- consoleMessage += `%c${ remainingMessage } ` ;
313- consoleArgs . push ( "color:inherit" ) ;
314- divMessage += remainingMessage ;
315- break ;
360+ if ( nextEndIndex [ key ] >= 0 && nextEndIndex [ key ] < nextIndex ) {
361+ nextIndex = nextEndIndex [ key ] ;
362+ nextKey = key ;
363+ fromArray = nextEndIndex ;
364+ nextTagFound = true ;
316365 }
317- let color = message . substring ( startTagStart + "<color=" . length , startTagEnd ) ;
318- let text = message . substring ( startTagEnd + 1 , closingTag ) ;
319- consoleMessage += `%c${ text } ` ;
320- consoleArgs . push ( "color:" + color ) ;
321- divMessage += `<span style="color:${ color } ;">${ text } </span>` ;
322- index = closingTag + "</color>" . length ;
366+ } ) ;
367+
368+ // write the text in before the next tag to our style text part array
369+ if ( nextIndex > index ) {
370+ let messageToNextTag = message . substring ( index , nextIndex ) ;
371+ let styles = [ ...styleStack ] ;
372+ styledTextParts . push ( new StyledTextPart ( messageToNextTag , styles ) ) ;
323373 }
324374
325- htmlLog ( divMessage . trim ( ) , logLevel ) ;
326- consoleLogFunction ( consoleMessage , ...consoleArgs ) ;
327- } ;
375+ // end if no more tags exist
376+ if ( nextTagFound === false ) {
377+ index = message . length ;
378+ break ;
379+ }
380+
381+ // Process start tag
382+ if ( fromArray === nextStartIndex ) {
383+ nextStartIndex [ nextKey ] = message . indexOf ( tagMap [ nextKey ] . startTag , nextIndex + 1 ) ;
384+ let closeTagIndex = message . indexOf ( '>' , nextIndex + 1 ) ;
385+ let styleValue ;
386+ if ( tagMap [ nextKey ] . hasParameter ) {
387+ styleValue = message . substring ( nextIndex + tagMap [ nextKey ] . startTag . length , closeTagIndex ) ;
388+ styleValue += tagMap [ nextKey ] . postfix ;
389+ } else {
390+ styleValue = tagMap [ nextKey ] . styleValue ;
391+ }
392+ styleStack . push ( `${ tagMap [ nextKey ] . styleTag } ${ styleValue } ` ) ;
393+ index = closeTagIndex + 1 ;
394+ }
395+ // process end tag
396+ else if ( fromArray === nextEndIndex ) {
397+ nextEndIndex [ nextKey ] = message . indexOf ( tagMap [ nextKey ] . closeTag , nextIndex + 1 ) ;
398+ let closeTagIndex = message . indexOf ( '>' , nextIndex + 1 ) ;
399+ styleStack . pop ( ) ;
400+ index = closeTagIndex + 1 ;
401+ }
402+ }
403+ return styledTextParts ;
328404}
329405
330406function updateLogCounter ( logLevel ) {
@@ -352,8 +428,7 @@ function htmlLog(message, className) {
352428 hour : "numeric" ,
353429 minute : "numeric" ,
354430 second : "numeric"
355- }
356- ) ;
431+ } ) ;
357432 message = "<span class='timestamplog'>[" + time + "] </span>" + message ;
358433 message = message . replaceAll ( "\n" , "<br />" ) ;
359434 text . innerHTML = message ;
0 commit comments