From ad5010e7d5a18b0c385cef5ab2cab34f1b66a2dc Mon Sep 17 00:00:00 2001 From: ChenCMD Date: Sun, 15 Nov 2020 16:54:43 +0900 Subject: [PATCH] :sparkles: Fix not support other than latest ver --- package.json | 5 ++ package.nls.ja.json | 1 + package.nls.json | 1 + src/commands/createDatapackTemplate/main.ts | 23 +++-- .../types/QuickPickFiles.ts | 9 +- .../createDatapackTemplate/utils/data.ts | 8 +- src/extension.ts | 6 ++ src/types/AskGitHubData.ts | 6 ++ src/types/Config.ts | 2 + src/types/VersionInformation.ts | 35 ++++++++ src/utils/downloader.ts | 20 +---- src/utils/vanillaData.ts | 90 +++++++++++++++++++ 12 files changed, 171 insertions(+), 35 deletions(-) create mode 100644 src/types/AskGitHubData.ts create mode 100644 src/types/VersionInformation.ts create mode 100644 src/utils/vanillaData.ts diff --git a/package.json b/package.json index c429c61..9c4f7ad 100644 --- a/package.json +++ b/package.json @@ -243,6 +243,11 @@ "markdownDescription": "%mcdutil.config.scoreOperation.valueScale%", "default": 1 }, + "mcdutil.createDatapackTemplate.dataVersion": { + "type": "string", + "markdownDescription": "%mcdutil.config.createDatapackTemplate.dataVersion%", + "default": "Latest release" + }, "mcdutil.createDatapackTemplate.customTemplate": { "type": "array", "items": { diff --git a/package.nls.ja.json b/package.nls.ja.json index ad9ae8c..6f2043e 100644 --- a/package.nls.ja.json +++ b/package.nls.ja.json @@ -33,5 +33,6 @@ "mcdutil.config.createDatapackTemplate.customTemplate.generates.relativeFilePath": "DatapackName以下の相対ファイルパス (ファイルの場合拡張子まで記載が必要です)", "mcdutil.config.createDatapackTemplate.customTemplate.generates.relativeFilePath.error": "\\\\ : * ? \" < > | はファイル/フォルダ名に使えません", "mcdutil.config.createDatapackTemplate.customTemplate.generates.content": "ファイルの内容 (基本的に一つのアイテムは1行として解釈されます)", + "mcdutil.config.createDatapackTemplate.dataVersion": "使用するバニラデータのバージョン", "mcdutil.config.createFile.fileTemplate": "**データパックファイルを作成する**: ファイルの生成時に記載されている内容 (ファイル種毎に設定が可能です)" } \ No newline at end of file diff --git a/package.nls.json b/package.nls.json index 4898896..cba9992 100644 --- a/package.nls.json +++ b/package.nls.json @@ -33,5 +33,6 @@ "mcdutil.config.createDatapackTemplate.customTemplate.generates.relativeFilePath": "Relative file path under DatapackName (In case of file, extension is required)", "mcdutil.config.createDatapackTemplate.customTemplate.generates.relativeFilePath.error": "\\\\ : * ? \" < > | cannot be used for file/folder names", "mcdutil.config.createDatapackTemplate.customTemplate.generates.content": "File contents (Basically one item is interpreted as one line)", + "mcdutil.config.createDatapackTemplate.dataVersion": "Version of vanilla data to use", "mcdutil.config.createFile.fileTemplate": "**Create Datapack file**: Contents described when the file is generated (can be set for each file type)" } diff --git a/src/commands/createDatapackTemplate/main.ts b/src/commands/createDatapackTemplate/main.ts index cc8ef87..8df6af9 100644 --- a/src/commands/createDatapackTemplate/main.ts +++ b/src/commands/createDatapackTemplate/main.ts @@ -9,9 +9,10 @@ import { locale } from '../../locales'; import { createMessageItemsHasId } from './types/MessageItems'; import { resolveVars, VariableContainer } from '../../types/VariableContainer'; import { getFileType } from '../../types/FileTypes'; -import { codeConsole, config } from '../../extension'; +import { codeConsole, config, versionInformation } from '../../extension'; import rfdc from 'rfdc'; -import { getGitHubData } from '../../utils/downloader'; +import { GenerateFileData } from './types/QuickPickFiles'; +import { getVanillaData } from '../../utils/vanillaData'; export async function createDatapack(): Promise { // フォルダ選択 @@ -109,18 +110,24 @@ async function create(dir: Uri): Promise { createItemData.push(rfdc()(packMcMetaData)); try { - for (const func of funcs.map((v, i) => ({ index: i + 1, value: v }))) { + for (const func of funcs.map((value, index) => ({ value, index }))) { await window.withProgress({ location: ProgressLocation.Notification, cancellable: false, title: locale('create-datapack-template.progress.title') }, async progress => { - progress.report({ increment: 0, message: locale('create-datapack-template.progress.download', func.index, funcs.length) }); - - const data = await getGitHubData(func.value, (_, m) => - progress.report({ increment: 100 / m, message: locale('create-datapack-template.progress.download', func.index, funcs.length) }) + const message = locale('create-datapack-template.progress.download', func.index + 1, funcs.length); + progress.report({ increment: 0, message }); + + const datas = await getVanillaData( + config.createDatapackTemplate.dataVersion, + versionInformation, + func.value, + func.value.rel, + (_, m) => progress.report({ increment: 100 / m, message }) ); - createItemData.push(...data); + for (const data of datas) + createItemData.push({ type: 'file', ...data } as GenerateFileData); }); } } catch (error) { diff --git a/src/commands/createDatapackTemplate/types/QuickPickFiles.ts b/src/commands/createDatapackTemplate/types/QuickPickFiles.ts index 80f3fd5..ee008ac 100644 --- a/src/commands/createDatapackTemplate/types/QuickPickFiles.ts +++ b/src/commands/createDatapackTemplate/types/QuickPickFiles.ts @@ -1,6 +1,7 @@ import { QuickPickItem } from 'vscode'; import { ReposGetContentResponseData } from '@octokit/types/dist-types/generated/Endpoints'; import { FileData } from '../../../types/FileData'; +import { AskGitHubData } from '../../../types/AskGitHubData'; export interface QuickPickFiles extends QuickPickItem { generates: GenerateFileData[], @@ -11,10 +12,6 @@ export interface GenerateFileData extends FileData { type: 'file' | 'folder' } -export interface GetGitHubDataFunc { - owner: string, - repo: string, - ref: string, - path: string, - relativeFilePath: (data: ReposGetContentResponseData) => string +export interface GetGitHubDataFunc extends AskGitHubData { + rel: (data: ReposGetContentResponseData) => string } \ No newline at end of file diff --git a/src/commands/createDatapackTemplate/utils/data.ts b/src/commands/createDatapackTemplate/utils/data.ts index c49ee7d..3f762c6 100644 --- a/src/commands/createDatapackTemplate/utils/data.ts +++ b/src/commands/createDatapackTemplate/utils/data.ts @@ -68,7 +68,7 @@ export const pickItems: QuickPickFiles[] = [ repo: 'vanilla-datapack', ref: '%version%-data', path: 'data/minecraft/tags/blocks', - relativeFilePath: (data: ReposGetContentResponseData): string => data.path + rel: (data: ReposGetContentResponseData): string => data.path } ] }, @@ -81,7 +81,7 @@ export const pickItems: QuickPickFiles[] = [ repo: 'vanilla-datapack', ref: '%version%-data', path: 'data/minecraft/tags/entity_types', - relativeFilePath: (data: ReposGetContentResponseData): string => data.path + rel: (data: ReposGetContentResponseData): string => data.path } ] }, @@ -94,7 +94,7 @@ export const pickItems: QuickPickFiles[] = [ repo: 'vanilla-datapack', ref: '%version%-data', path: 'data/minecraft/tags/fluids', - relativeFilePath: (data: ReposGetContentResponseData): string => data.path + rel: (data: ReposGetContentResponseData): string => data.path } ] }, @@ -107,7 +107,7 @@ export const pickItems: QuickPickFiles[] = [ repo: 'vanilla-datapack', ref: '%version%-data', path: 'data/minecraft/tags/items', - relativeFilePath: (data: ReposGetContentResponseData): string => data.path + rel: (data: ReposGetContentResponseData): string => data.path } ] }, diff --git a/src/extension.ts b/src/extension.ts index e399f70..7fbc6d1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,15 +2,21 @@ import { ExtensionContext, commands, window, workspace, ConfigurationChangeEvent import { copyResourcePath, createDatapack, createFile, scoreOperation } from './commands'; import { loadLocale } from './locales'; import { constructConfig } from './types/Config'; +import { VersionInformation } from './types/VersionInformation'; +import { getLatestVersions } from './utils/vanillaData'; export const codeConsole = window.createOutputChannel('MC Commander Util'); export let config = constructConfig(workspace.getConfiguration('mcdutil')); +export let versionInformation: VersionInformation | undefined; const vscodeLanguage = getVSCodeLanguage(); /** * @param {vscode.ExtensionContext} context */ export function activate(context: ExtensionContext): void { + + getLatestVersions().then(info => versionInformation = info); + loadLocale(config.language, vscodeLanguage); const disposable = []; diff --git a/src/types/AskGitHubData.ts b/src/types/AskGitHubData.ts new file mode 100644 index 0000000..f21a109 --- /dev/null +++ b/src/types/AskGitHubData.ts @@ -0,0 +1,6 @@ +export interface AskGitHubData { + owner: string + repo: string + ref: string + path: string +} \ No newline at end of file diff --git a/src/types/Config.ts b/src/types/Config.ts index a312a5c..87d8735 100644 --- a/src/types/Config.ts +++ b/src/types/Config.ts @@ -16,6 +16,7 @@ export interface Config { valueScale: number }, createDatapackTemplate: { + dataVersion: string customTemplate: QuickPickFiles[] }, createFile: { @@ -36,6 +37,7 @@ export const defaultConfig: Config = { valueScale: 1 }, createDatapackTemplate: { + dataVersion: 'Latest release', customTemplate: [] }, createFile: { diff --git a/src/types/VersionInformation.ts b/src/types/VersionInformation.ts new file mode 100644 index 0000000..7d73cad --- /dev/null +++ b/src/types/VersionInformation.ts @@ -0,0 +1,35 @@ +/** + * @license + * MIT License + * + * Copyright (c) 2019-2020 SPGoding + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +export interface VersionInformation { + latestSnapshot: string, + latestRelease: string, + processedVersions: string[] +} + +export const dummyVersionInformation: VersionInformation = { + latestRelease: '', + latestSnapshot: '', + processedVersions: [] +}; \ No newline at end of file diff --git a/src/utils/downloader.ts b/src/utils/downloader.ts index 9c90372..37b1d68 100644 --- a/src/utils/downloader.ts +++ b/src/utils/downloader.ts @@ -3,7 +3,7 @@ import { Octokit } from '@octokit/rest'; import { ReposGetContentResponseData } from '@octokit/types/dist-types/generated/Endpoints'; import { OctokitResponse } from '@octokit/types/dist-types/OctokitResponse'; import { setTimeOut } from './common'; -import { GenerateFileData, GetGitHubDataFunc } from '../commands/createDatapackTemplate/types/QuickPickFiles'; +import { AskGitHubData } from '../types/AskGitHubData'; export async function download(uri: string): Promise { return await new Promise((resolve, reject) => { @@ -20,7 +20,7 @@ export async function download(uri: string): Promise { }); } -export async function getGitHubData(data: GetGitHubDataFunc, elementFunc: (index: number, max: number) => void): Promise { +export async function getGitHubData(data: AskGitHubData): Promise { const octokit = new Octokit(); const files = await Promise.race([ octokit.repos.getContent({ @@ -31,19 +31,5 @@ export async function getGitHubData(data: GetGitHubDataFunc, elementFunc: (index }) as unknown as OctokitResponse, setTimeOut>(7000) ]); - // コンパイラが雑魚 - const result: GenerateFileData[] = []; - for (const file of files.data.map((v, i) => ({ index: i, value: v }))) { - const content = await Promise.race([ - download(file.value.download_url), - setTimeOut(7000) - ]); - result.push({ - type: 'file', - relativeFilePath: data.relativeFilePath(file.value), - content: content.split('\n') - }); - elementFunc(file.index, files.data.length); - } - return result; + return files.data; } \ No newline at end of file diff --git a/src/utils/vanillaData.ts b/src/utils/vanillaData.ts new file mode 100644 index 0000000..15388db --- /dev/null +++ b/src/utils/vanillaData.ts @@ -0,0 +1,90 @@ +/** + * @license + * MIT License + * + * Copyright (c) 2019-2020 SPGoding + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import { ReposGetContentResponseData } from '@octokit/types/dist-types/generated/Endpoints'; +import { codeConsole } from '../extension'; +import { AskGitHubData } from '../types/AskGitHubData'; +import { FileData } from '../types/FileData'; +import { VersionInformation } from '../types/VersionInformation'; +import { setTimeOut } from './common'; +import { download, getGitHubData } from './downloader'; + +export async function getVanillaData( + versionOrLiteral: string, + versionInfo: VersionInformation | undefined, + askGitHubdata: AskGitHubData, + relProcessingFunc: (data: ReposGetContentResponseData) => string, + elementFunc: (index: number, max: number) => void +): Promise { + askGitHubdata.ref = askGitHubdata.ref.replace(/%version%/, resolveVersion(versionOrLiteral, versionInfo)); + + const files = await getGitHubData(askGitHubdata); + const ans: FileData[] = []; + + for (const file of files.map((value, index) => ({ index, value }))) { + const content = await Promise.race([ + download(file.value.download_url), + setTimeOut(7000) + ]); + ans.push({ + rel: relProcessingFunc(file.value), + content: content.split('\n') + }); + elementFunc(file.index, files.length); + } + return ans; +} + +function resolveVersion(versionOrLiteral: string, versionInformation: VersionInformation | undefined) { + if (!versionInformation) + return '1.16.4'; + switch (versionOrLiteral.toLowerCase()) { + case 'latest snapshot': + return versionInformation.latestSnapshot; + case 'latest release': + return versionInformation.latestRelease; + default: + return versionOrLiteral; + } +} + +export async function getLatestVersions(): Promise { + let ans: VersionInformation | undefined; + try { + codeConsole.appendLine('[LatestVersions] Fetching the latest versions...'); + const str = await Promise.race([ + download('https://launchermeta.mojang.com/mc/game/version_manifest.json'), + setTimeOut(7000) + ]); + const { latest: { release, snapshot }, versions }: { latest: { release: string, snapshot: string }, versions: { id: string }[] } = JSON.parse(str); + const processedVersion = '1.16.2'; + const processedVersionIndex = versions.findIndex(v => v.id === processedVersion); + const processedVersions = processedVersionIndex >= 0 ? versions.slice(0, processedVersionIndex + 1).map(v => v.id) : []; + ans = (release && snapshot) ? { latestRelease: release, latestSnapshot: snapshot, processedVersions } : undefined; + } catch (e) { + codeConsole.appendLine(`[LatestVersions] ${e}`); + } + return ans; +} \ No newline at end of file