From b5ad2917d4cdc74d521bb727c1f254ed36f9840e Mon Sep 17 00:00:00 2001 From: Stefano Brilli Date: Fri, 8 May 2020 00:25:24 +0200 Subject: [PATCH] Fix disc renaming function. --- src/services/netmd.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/services/netmd.ts b/src/services/netmd.ts index f2d9b95..9aa6eb5 100644 --- a/src/services/netmd.ts +++ b/src/services/netmd.ts @@ -91,9 +91,28 @@ export class NetMDUSBService implements NetMDService { } async renameDisc(newName: string) { + // TODO: This whole function should be moved in netmd-js const oldName = await this.netmdInterface!.getDiscTitle(); - let newNameWithGroups = await this.netmdInterface!._getDiscTitle(); - newNameWithGroups = newNameWithGroups.replace(oldName, newName); + const oldRawName = await this.netmdInterface!._getDiscTitle(); + const hasGroups = oldRawName.indexOf('//') >= 0; + const hasGroupsAndTitle = oldRawName.startsWith('0;'); + + if (newName === oldName) { + return; + } + + let newNameWithGroups; + + if (hasGroups) { + if (hasGroupsAndTitle) { + newNameWithGroups = oldRawName.replace(/^0;.*?\/\//, newName !== '' ? `0;${newName}//` : ``); // Replace or delete the old title + } else { + newNameWithGroups = `0;${newName}//${oldRawName}`; // Add the new title + } + } else { + newNameWithGroups = newName; + } + await this.netmdInterface!.cacheTOC(); await this.netmdInterface!.setDiscTitle(newNameWithGroups); await this.netmdInterface!.syncTOC();