|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | import * as fs from "fs";
|
| 18 | +import * as glob from "glob"; |
18 | 19 | import * as path from "path";
|
19 | 20 | import * as vscode from "vscode";
|
20 | 21 |
|
@@ -74,6 +75,40 @@ export class MPQEditorProvider implements vscode.CustomTextEditorProvider {
|
74 | 75 | return undefined;
|
75 | 76 | }
|
76 | 77 |
|
| 78 | + /** |
| 79 | + * @brief A helper function to find unoccupied mpq file-name |
| 80 | + * @returns valid file name for mpq configuration or undefined on failure |
| 81 | + * @throw Error, when input is invalid (e.g. baseMPQName is empty) |
| 82 | + */ |
| 83 | + public static findMPQName( |
| 84 | + baseMPQName: string, |
| 85 | + dirPath: string |
| 86 | + ): string | undefined { |
| 87 | + if (baseMPQName.length === 0) { |
| 88 | + throw new Error("Invalid mixed precision quantization file name"); |
| 89 | + } |
| 90 | + |
| 91 | + const baseName = baseMPQName; |
| 92 | + let mpqName: string | undefined = undefined; |
| 93 | + |
| 94 | + const options = { cwd: dirPath }; |
| 95 | + // set maximal trials as maximal quantity of files + 1 |
| 96 | + const files = glob.sync("*" + MPQEditorProvider.fileExtension, options); |
| 97 | + const maxMPQIndex = files.length + 1; |
| 98 | + |
| 99 | + for (let i = 0; i < maxMPQIndex; i++) { |
| 100 | + mpqName = baseMPQName + MPQEditorProvider.fileExtension; |
| 101 | + const mpqPath: string = path.join(dirPath, mpqName); |
| 102 | + if (!fs.existsSync(mpqPath)) { |
| 103 | + break; |
| 104 | + } |
| 105 | + baseMPQName = baseName + `(${i + 1})`; |
| 106 | + mpqName = undefined; |
| 107 | + } |
| 108 | + |
| 109 | + return mpqName; |
| 110 | + } |
| 111 | + |
77 | 112 | constructor(private readonly context: vscode.ExtensionContext) {}
|
78 | 113 |
|
79 | 114 | /**
|
|
0 commit comments