From dd67527c64c8f7889e2c88bda51fe839adc7f284 Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Sat, 12 Dec 2020 14:18:50 +0100 Subject: [PATCH] Some minor changes, bugfixes and improvements --- shared/js/connection/CommandHandler.ts | 2 +- shared/js/connection/rtc/Connection.ts | 33 +++++++++++++++++------ shared/js/log.ts | 37 +++++++++++++++----------- shared/js/proto.ts | 26 +++++++++--------- shared/js/tree/Channel.ts | 6 ++--- shared/js/tree/ChannelTree.tsx | 12 ++++++--- 6 files changed, 74 insertions(+), 42 deletions(-) diff --git a/shared/js/connection/CommandHandler.ts b/shared/js/connection/CommandHandler.ts index 06c8ec7e..85613bdd 100644 --- a/shared/js/connection/CommandHandler.ts +++ b/shared/js/connection/CommandHandler.ts @@ -746,7 +746,7 @@ export class ConnectionCommandHandler extends AbstractCommandHandler { return 0; } - tree.moveChannel(channel, prev, parent, true); + tree.moveChannel(channel, prev, parent, false); } handleNotifyChannelEdited(json) { diff --git a/shared/js/connection/rtc/Connection.ts b/shared/js/connection/rtc/Connection.ts index be808098..7314067a 100644 --- a/shared/js/connection/rtc/Connection.ts +++ b/shared/js/connection/rtc/Connection.ts @@ -1,7 +1,7 @@ import {AbstractServerConnection, ServerCommand, ServerConnectionEvents} from "tc-shared/connection/ConnectionBase"; import {ConnectionState} from "tc-shared/ConnectionHandler"; import * as log from "tc-shared/log"; -import {LogCategory, logDebug, logError, logTrace, logWarn} from "tc-shared/log"; +import {group, LogCategory, logDebug, logError, logGroupNative, logTrace, LogType, logWarn} from "tc-shared/log"; import {AbstractCommandHandler} from "tc-shared/connection/AbstractCommandHandler"; import {CommandResult} from "tc-shared/connection/ServerConnectionDeclaration"; import {tr, tra} from "tc-shared/i18n/localize"; @@ -11,7 +11,6 @@ import {SdpCompressor, SdpProcessor} from "./SdpUtils"; import {ErrorCode} from "tc-shared/connection/ErrorCode"; import {WhisperTarget} from "tc-shared/voice/VoiceWhisper"; import {globalAudioContext} from "tc-backend/audio/player"; -import * as sdpTransform from "sdp-transform"; const kSdpCompressionMode = 1; @@ -185,7 +184,10 @@ class CommandHandler extends AbstractCommandHandler { return; } if(RTCConnection.kEnableSdpTrace) { - logTrace(LogCategory.WEBRTC, tr("Received remote %s:\n%s"), data.mode, data.sdp); + const gr = logGroupNative(LogType.TRACE, LogCategory.WEBRTC, tra("Original remote SDP ({})", data.mode as string)); + gr.collapsed(true); + gr.log("%s", data.sdp); + gr.end(); } try { sdp = this.sdpProcessor.processIncomingSdp(sdp, data.mode); @@ -195,7 +197,10 @@ class CommandHandler extends AbstractCommandHandler { return; } if(RTCConnection.kEnableSdpTrace) { - logTrace(LogCategory.WEBRTC, tr("Patched remote %s:\n%s"), data.mode, sdp); + const gr = logGroupNative(LogType.TRACE, LogCategory.WEBRTC, tra("Patched remote SDP ({})", data.mode as string)); + gr.collapsed(true); + gr.log("%s", sdp); + gr.end(); } if(data.mode === "answer") { this.handle["peer"].setRemoteDescription({ @@ -217,7 +222,10 @@ class CommandHandler extends AbstractCommandHandler { }).then(() => this.handle["peer"].createAnswer()) .then(async answer => { if(RTCConnection.kEnableSdpTrace) { - logTrace(LogCategory.WEBRTC, tr("Generated local answer due to remote %s:\n%s"), data.mode, answer.sdp); + const gr = logGroupNative(LogType.TRACE, LogCategory.WEBRTC, tra("Original local SDP ({})", data.mode as string)); + gr.collapsed(true); + gr.log("%s", answer.sdp); + gr.end(); } answer.sdp = this.sdpProcessor.processOutgoingSdp(answer.sdp, "answer"); @@ -227,7 +235,10 @@ class CommandHandler extends AbstractCommandHandler { .then(answer => { answer.sdp = SdpCompressor.compressSdp(answer.sdp, kSdpCompressionMode); if(RTCConnection.kEnableSdpTrace) { - logTrace(LogCategory.WEBRTC, tr("Patched answer to remote %s:\n%s"), data.mode, answer.sdp); + const gr = logGroupNative(LogType.TRACE, LogCategory.WEBRTC, tra("Patched local SDP ({})", data.mode as string)); + gr.collapsed(true); + gr.log("%s", answer.sdp); + gr.end(); } return this.connection.send_command("rtcsessiondescribe", { @@ -898,11 +909,17 @@ export class RTCConnection { if(this.peer !== peer) { return; } if(RTCConnection.kEnableSdpTrace) { - logTrace(LogCategory.WEBRTC, tr("Generated initial local offer:\n%s"), offer.sdp); + const gr = logGroupNative(LogType.TRACE, LogCategory.WEBRTC, tra("Original initial local SDP (offer)")); + gr.collapsed(true); + gr.log("%s", offer.sdp); + gr.end(); } try { offer.sdp = this.sdpProcessor.processOutgoingSdp(offer.sdp, "offer"); - logTrace(LogCategory.WEBRTC, tr("Patched initial local offer:\n%s"), offer.sdp); + const gr = logGroupNative(LogType.TRACE, LogCategory.WEBRTC, tra("Patched initial local SDP (offer)")); + gr.collapsed(true); + gr.log("%s", offer.sdp); + gr.end(); } catch (error) { logError(LogCategory.WEBRTC, tr("Failed to preprocess outgoing initial offer: %o"), error); this.handleFatalError(tr("Failed to preprocess outgoing initial offer"), true); diff --git a/shared/js/log.ts b/shared/js/log.ts index c3fc19af..92e45e1a 100644 --- a/shared/js/log.ts +++ b/shared/js/log.ts @@ -33,7 +33,7 @@ export enum LogType { ERROR } -let category_mapping = new Map([ +let categoryMapping = new Map([ [LogCategory.CHANNEL, "Channel "], [LogCategory.CHANNEL_PROPERTIES, "Channel "], [LogCategory.CLIENT, "Client "], @@ -92,7 +92,7 @@ enum GroupMode { NATIVE, PREFIX } -const group_mode: GroupMode = GroupMode.PREFIX; +const defaultGroupMode: GroupMode = GroupMode.PREFIX; //Category Example: ?log.i18n.enabled=0 //Level Example A: ?log.level.trace.enabled=0 @@ -134,7 +134,7 @@ function logDirect(type: LogType, message: string, ...optionalParams: any[]) { export function log(type: LogType, category: LogCategory, message: string, ...optionalParams: any[]) { if(!enabled_mapping.get(category)) return; - optionalParams.unshift(category_mapping.get(category)); + optionalParams.unshift(categoryMapping.get(category)); message = "[%s] " + message; logDirect(type, message, ...optionalParams); } @@ -182,13 +182,20 @@ export function logError(category: LogCategory, message: string, ...optionalPara export function group(level: LogType, category: LogCategory, name: string, ...optionalParams: any[]) : Group { name = "[%s] " + name; - optionalParams.unshift(category_mapping.get(category)); + optionalParams.unshift(categoryMapping.get(category)); - return new Group(group_mode, level, category, name, optionalParams); + return new Group(defaultGroupMode, level, category, name, optionalParams); +} + +export function logGroupNative(level: LogType, category: LogCategory, name: string, ...optionalParams: any[]) : Group { + name = "[%s] " + name; + optionalParams.unshift(categoryMapping.get(category)); + + return new Group(GroupMode.NATIVE, level, category, name, optionalParams); } export function table(level: LogType, category: LogCategory, title: string, args: any) { - if(group_mode == GroupMode.NATIVE) { + if(defaultGroupMode == GroupMode.NATIVE) { console.groupCollapsed(title); console.table(args); console.groupEnd(); @@ -209,9 +216,9 @@ export class Group { private readonly name: string; private readonly optionalParams: any[][]; - private _collapsed: boolean = false; + private isCollapsed: boolean = false; private initialized = false; - private _log_prefix: string; + private logPrefix: string; constructor(mode: GroupMode, level: LogType, category: LogCategory, name: string, optionalParams: any[][], owner: Group = undefined) { this.level = level; @@ -227,7 +234,7 @@ export class Group { } collapsed(flag: boolean = true) : this { - this._collapsed = flag; + this.isCollapsed = flag; return this; } @@ -238,17 +245,17 @@ export class Group { if(!this.initialized) { if(this.mode == GroupMode.NATIVE) { - if(this._collapsed && console.groupCollapsed) { + if(this.isCollapsed && console.groupCollapsed) { console.groupCollapsed(this.name, ...this.optionalParams); } else { console.group(this.name, ...this.optionalParams); } } else { - this._log_prefix = " "; + this.logPrefix = " "; let parent = this.owner; while(parent) { if(parent.mode == GroupMode.PREFIX) { - this._log_prefix = this._log_prefix + parent._log_prefix; + this.logPrefix = this.logPrefix + parent.logPrefix; } else { break; } @@ -259,7 +266,7 @@ export class Group { if(this.mode == GroupMode.NATIVE) { logDirect(this.level, message, ...optionalParams); } else { - logDirect(this.level, "[%s] " + this._log_prefix + message, category_mapping.get(this.category), ...optionalParams); + logDirect(this.level, "[%s] " + this.logPrefix + message, categoryMapping.get(this.category), ...optionalParams); } return this; } @@ -272,11 +279,11 @@ export class Group { } get prefix() : string { - return this._log_prefix; + return this.logPrefix; } set prefix(prefix: string) { - this._log_prefix = prefix; + this.logPrefix = prefix; } } diff --git a/shared/js/proto.ts b/shared/js/proto.ts index 94889bd3..2973aaa4 100644 --- a/shared/js/proto.ts +++ b/shared/js/proto.ts @@ -131,22 +131,24 @@ if(!JSON.map_to) { if(!JSON.map_field_to) { JSON.map_field_to = function(object: T, value: any, field: string) : boolean { - let field_type = typeof(object[field]); - let new_object; - if(field_type == "string" || field_type == "object" || field_type == "undefined") - new_object = value; - else if(field_type == "number") - new_object = parseFloat(value); - else if(field_type == "boolean") - new_object = value == "1" || value == "true"; - else { - console.warn(tr("Invalid object type %s for entry %s"), field_type, field); + let fieldType = typeof object[field]; + let newValue; + if(fieldType == "string" || fieldType == "object" || fieldType == "undefined") { + newValue = value; + } else if(fieldType == "number") { + newValue = parseFloat(value); + } else if(fieldType == "boolean") { + newValue = typeof value === "boolean" && value || value === "1" || value === "true"; + } else { + console.warn(tr("Invalid object type %s for entry %s"), fieldType, field); return false; } - if(new_object === object[field as string]) return false; + if(newValue === object[field]) { + return false; + } - object[field as string] = new_object; + object[field] = newValue; return true; } } diff --git a/shared/js/tree/Channel.ts b/shared/js/tree/Channel.ts index f6a4e78d..2885d2b2 100644 --- a/shared/js/tree/Channel.ts +++ b/shared/js/tree/Channel.ts @@ -194,8 +194,7 @@ export class ChannelEntry extends ChannelTreeEntry { this.properties = new ChannelProperties(); this.channelId = channelId; this.properties.channel_name = channelName; - - this.parsed_channel_name = new ParsedChannelName("undefined", false); + this.parsed_channel_name = new ParsedChannelName(channelName, false); this.clientPropertyChangedListener = (event: ClientEvents["notify_properties_updated"]) => { if("client_nickname" in event.updated_properties || "client_talk_power" in event.updated_properties) { @@ -575,6 +574,7 @@ export class ChannelEntry extends ChannelTreeEntry { for(const variable of variables) { let key = variable.key; let value = variable.value; + if(!JSON.map_field_to(this.properties, value, variable.key)) { /* no update */ continue; @@ -584,7 +584,7 @@ export class ChannelEntry extends ChannelTreeEntry { this.parsed_channel_name = new ParsedChannelName(value, this.hasParent()); } else if(key == "channel_order") { let order = this.channelTree.findChannel(this.properties.channel_order); - this.channelTree.moveChannel(this, order, this.parent, true); + this.channelTree.moveChannel(this, order, this.parent, false); } else if(key === "channel_icon_id") { this.properties.channel_icon_id = variable.value as any >>> 0; /* unsigned 32 bit number! */ } else if(key == "channel_description") { diff --git a/shared/js/tree/ChannelTree.tsx b/shared/js/tree/ChannelTree.tsx index caa8cb9d..486c3b85 100644 --- a/shared/js/tree/ChannelTree.tsx +++ b/shared/js/tree/ChannelTree.tsx @@ -251,7 +251,7 @@ export class ChannelTree { handleChannelCreated(previous: ChannelEntry, parent: ChannelEntry, channelId: number, channelName: string) : ChannelEntry { const channel = new ChannelEntry(this, channelId, channelName); this.channels.push(channel); - this.moveChannel(channel, previous, parent, false); + this.moveChannel(channel, previous, parent, true); this.events.fire("notify_channel_created", { channel: channel }); return channel; } @@ -300,12 +300,16 @@ export class ChannelTree { channel.parent = undefined; } - moveChannel(channel: ChannelEntry, channelPrevious: ChannelEntry, parent: ChannelEntry, triggerMoveEvent: boolean) { + moveChannel(channel: ChannelEntry, channelPrevious: ChannelEntry, parent: ChannelEntry, isInsertMove: boolean) { if(channelPrevious != null && channelPrevious.parent != parent) { console.error(tr("Invalid channel move (different parents! (%o|%o)"), channelPrevious.parent, parent); return; } + if(!isInsertMove && channel.channel_previous === channelPrevious && channel.parent === parent) { + return; + } + const previousParent = channel.parent_channel(); const previousOrder = channel.channel_previous; @@ -355,13 +359,15 @@ export class ChannelTree { debugger; } - if(triggerMoveEvent) { + if(!isInsertMove) { this.events.fire("notify_channel_moved", { channel: channel, previousOrder: previousOrder, previousParent: previousParent }); } + + channel.properties.channel_order = previousOrder ? previousOrder.channelId : 0; } deleteClient(client: ClientEntry, reason: { reason: ViewReasonId, message?: string, serverLeave: boolean }) {