mirror of
https://github.com/ZZZure/ZZZ-Plugin.git
synced 2025-12-16 21:27:47 +00:00
refactor: 重构lib代码(无实质性功能更新,可不更新)
This commit is contained in:
parent
a7f06d404b
commit
aa3b7928ec
26 changed files with 547 additions and 483 deletions
171
lib/download.js
171
lib/download.js
|
|
@ -1,72 +1,8 @@
|
|||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import * as convert from './convert.js';
|
||||
import {
|
||||
ZZZ_SQUARE_AVATAR,
|
||||
ZZZ_SQUARE_BANGBOO,
|
||||
NEW_ZZZ_SQUARE_BANGBOO,
|
||||
NEW_ZZZ_SQUARE_AVATAR,
|
||||
} from './mysapi/api.js';
|
||||
import { imageResourcesPath } from './path.js';
|
||||
import { char, equip, weapon } from './convert.js';
|
||||
import { getResourceRemotePath } from './assets.js';
|
||||
import request from '../utils/request.js';
|
||||
|
||||
const ZZZ_SQUARE_AVATAR_PATH = path.join(imageResourcesPath, 'square_avatar');
|
||||
const ZZZ_SMALL_SQUARE_AVATAR_PATH = path.join(
|
||||
imageResourcesPath,
|
||||
'role_general'
|
||||
);
|
||||
const ZZZ_SQUARE_BANGBOO_PATH = path.join(
|
||||
imageResourcesPath,
|
||||
'bangboo_square_avatar'
|
||||
);
|
||||
const ZZZ_WEAPON_PATH = path.join(imageResourcesPath, 'weapon');
|
||||
const ZZZ_ROLE_PATH = path.join(imageResourcesPath, 'role');
|
||||
const ZZZ_ROLE_CIRCLE_PATH = path.join(imageResourcesPath, 'role_circle');
|
||||
const ZZZ_SUIT_3D_PATH = path.join(imageResourcesPath, 'suit_3d');
|
||||
const ZZZ_SUIT_PATH = path.join(imageResourcesPath, 'suit');
|
||||
// const ZZZ_GUIDES_PATH = path.join(imageResourcesPath, 'guides');
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
* @param {string} url 下载地址
|
||||
* @param {string} savePath 保存路径
|
||||
* @returns {Promise<string | null>} 保存路径
|
||||
*/
|
||||
export const downloadFile = async (url, savePath) => {
|
||||
// 下载文件
|
||||
try {
|
||||
const download = await request(url, {}, 5);
|
||||
const arrayBuffer = await download.arrayBuffer();
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
// 保存文件
|
||||
if (!fs.existsSync(path.dirname(savePath))) {
|
||||
fs.mkdirSync(path.dirname(savePath), { recursive: true });
|
||||
}
|
||||
fs.writeFileSync(savePath, buffer);
|
||||
// 返回保存路径
|
||||
return savePath;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 查看文件是否存在,如果存在则返回路径,否则下载文件
|
||||
* @param {string} url 下载地址
|
||||
* @param {string} savePath 保存路径
|
||||
* @returns {Promise<string | null>} 保存路径
|
||||
*/
|
||||
export const checkFile = async (url, savePath) => {
|
||||
if (fs.existsSync(savePath)) {
|
||||
const stats = fs.statSync(savePath);
|
||||
if (stats.size > 0) {
|
||||
return savePath;
|
||||
}
|
||||
}
|
||||
const download = await downloadFile(url, savePath);
|
||||
return download;
|
||||
};
|
||||
downloadMysImage,
|
||||
downloadResourceImage,
|
||||
} from './download/download.js';
|
||||
|
||||
/**
|
||||
* 获取角色头像(方形)
|
||||
|
|
@ -75,96 +11,94 @@ export const checkFile = async (url, savePath) => {
|
|||
*/
|
||||
export const getSquareAvatar = async charID => {
|
||||
const filename = `role_square_avatar_${charID}.png`;
|
||||
const avatarPath = path.join(ZZZ_SQUARE_AVATAR_PATH, filename);
|
||||
let url = `${ZZZ_SQUARE_AVATAR}/${filename}`;
|
||||
let result = await checkFile(url, avatarPath);
|
||||
if (!result) {
|
||||
url = `${NEW_ZZZ_SQUARE_AVATAR}/${filename}`;
|
||||
result = await checkFile(url, avatarPath);
|
||||
}
|
||||
const result = await downloadMysImage(
|
||||
'ZZZ_SQUARE_AVATAR',
|
||||
'ZZZ_SQUARE_AVATAR_PATH',
|
||||
filename,
|
||||
'NEW_ZZZ_SQUARE_AVATAR'
|
||||
);
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取角色头像(小方形)
|
||||
* @param {string | number} charID
|
||||
* @returns Promise<string>
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
export const getSmallSquareAvatar = async charID => {
|
||||
const sprite = char.IDToCharSprite(charID);
|
||||
const sprite = convert.char.IDToCharSprite(charID);
|
||||
if (!sprite) return null;
|
||||
const filename = `IconRoleGeneral${sprite}.png`;
|
||||
const avatarPath = path.join(ZZZ_SMALL_SQUARE_AVATAR_PATH, filename);
|
||||
const url = await getResourceRemotePath('role_general', filename);
|
||||
const result = await checkFile(url, avatarPath);
|
||||
const result = await downloadResourceImage(
|
||||
'role_general',
|
||||
'ZZZ_SMALL_SQUARE_AVATAR_PATH',
|
||||
filename
|
||||
);
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取邦布头像(方形)
|
||||
* @param {string | number} bangbooId
|
||||
* @returns Promise<string>
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
export const getSquareBangboo = async bangbooId => {
|
||||
const filename = `bangboo_rectangle_avatar_${bangbooId}.png`;
|
||||
const bangbooPath = path.join(ZZZ_SQUARE_BANGBOO_PATH, filename);
|
||||
let url = `${ZZZ_SQUARE_BANGBOO}/${filename}`;
|
||||
let result = await checkFile(url, bangbooPath);
|
||||
if (!result) {
|
||||
url = `${NEW_ZZZ_SQUARE_BANGBOO}/${filename}`;
|
||||
result = await checkFile(url, bangbooPath);
|
||||
}
|
||||
const result = await downloadMysImage(
|
||||
'ZZZ_SQUARE_BANGBOO',
|
||||
'ZZZ_SQUARE_BANGBOO_PATH',
|
||||
filename,
|
||||
'NEW_ZZZ_SQUARE_BANGBOO'
|
||||
);
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取武器图片
|
||||
* @param {string} id
|
||||
* @returns Promise<string>
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
export const getWeaponImage = async id => {
|
||||
const name = weapon.IDToWeaponFileName(id);
|
||||
let filename = `${name}_High.png`;
|
||||
const weaponPath = path.join(ZZZ_WEAPON_PATH, filename);
|
||||
const url = await getResourceRemotePath('weapon', filename);
|
||||
let result = await checkFile(url, weaponPath);
|
||||
if (!result) {
|
||||
filename = `${name}.png`;
|
||||
const weaponPath = path.join(ZZZ_WEAPON_PATH, filename);
|
||||
const url = await getResourceRemotePath('weapon', filename);
|
||||
result = await checkFile(url, weaponPath);
|
||||
}
|
||||
const name = convert.weapon.IDToWeaponFileName(id);
|
||||
if (!name) return null;
|
||||
const filename = `${name}_High.png`;
|
||||
const replaceFilename = `${name}_High.png`;
|
||||
const result = await downloadResourceImage(
|
||||
'weapon',
|
||||
'ZZZ_WEAPON_PATH',
|
||||
filename,
|
||||
replaceFilename
|
||||
);
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取角色图片
|
||||
* @param {string | number} id
|
||||
* @returns Promise<string>
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
export const getRoleImage = async id => {
|
||||
const sprite = char.IDToCharSprite(id);
|
||||
const sprite = convert.char.IDToCharSprite(id);
|
||||
if (!sprite) return null;
|
||||
const filename = `IconRole${sprite}.png`;
|
||||
const rolePath = path.join(ZZZ_ROLE_PATH, filename);
|
||||
const url = await getResourceRemotePath('role', filename);
|
||||
const result = await checkFile(url, rolePath);
|
||||
const result = await downloadResourceImage('role', 'ZZZ_ROLE_PATH', filename);
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取角色圆形图片
|
||||
* @param {string | number} id
|
||||
* @returns Promise<string>
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
export const getRoleCircleImage = async id => {
|
||||
const sprite = char.IDToCharSprite(id);
|
||||
const sprite = convert.char.IDToCharSprite(id);
|
||||
if (!sprite) return null;
|
||||
const filename = `IconRoleCircle${sprite}.png`;
|
||||
const roleCirclePath = path.join(ZZZ_ROLE_CIRCLE_PATH, filename);
|
||||
const url = await getResourceRemotePath('role_circle', filename);
|
||||
const result = await checkFile(url, roleCirclePath);
|
||||
const result = await downloadResourceImage(
|
||||
'role_circle',
|
||||
'ZZZ_ROLE_CIRCLE_PATH',
|
||||
filename
|
||||
);
|
||||
return result;
|
||||
};
|
||||
|
||||
|
|
@ -174,11 +108,10 @@ export const getRoleCircleImage = async id => {
|
|||
* @returns Promise<string>
|
||||
*/
|
||||
export const getSuitImage = async suitId => {
|
||||
const suitName = equip.equipIdToSprite(suitId);
|
||||
const suitName = convert.equip.equipIdToSprite(suitId);
|
||||
if (!suitName) return null;
|
||||
const filename = `${suitName}.png`;
|
||||
const suitPath = path.join(ZZZ_SUIT_PATH, filename);
|
||||
const url = await getResourceRemotePath('suit', filename);
|
||||
const result = await checkFile(url, suitPath);
|
||||
const result = await downloadResourceImage('suit', 'ZZZ_SUIT_PATH', filename);
|
||||
return result;
|
||||
};
|
||||
|
||||
|
|
@ -188,10 +121,12 @@ export const getSuitImage = async suitId => {
|
|||
* @returns Promise<string>
|
||||
*/
|
||||
export const getSuit3DImage = async suitId => {
|
||||
const suitName = equip.equipIdToSprite(suitId);
|
||||
const suitName = convert.equip.equipIdToSprite(suitId);
|
||||
const filename = `${suitName}_3d.png`;
|
||||
const suitPath = path.join(ZZZ_SUIT_3D_PATH, filename);
|
||||
const url = await getResourceRemotePath('suit_3d', filename);
|
||||
const result = await checkFile(url, suitPath);
|
||||
const result = await downloadResourceImage(
|
||||
'suit_3d',
|
||||
'ZZZ_SUIT_3D_PATH',
|
||||
filename
|
||||
);
|
||||
return result;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue