Files
SpineViewer/SpineThumbnailGenerateServer/js/settings.js
T
2026-09-20 21:22:03 +08:00

44 lines
1.6 KiB
JavaScript

// 插件设置持久化。默认关闭自动生成——Eagle 审核标准要求「批量或不可逆的变更
// 必须事先明确说明并获得确认」,所以必须由用户主动打开这个开关。
(function () {
'use strict';
const fs = require('fs');
const path = require('path');
// mode: 'animated' = 有动画就出动态 WebP(没有动画回退静帧),'static' = 一律 setup pose 静帧。
// strategy: 'size' = 十档全生成取体积最小的(慢、最省体积),'speed' = 只生成粗估最小的那几档。
const DEFAULTS = { enabled: false, mode: 'animated', strategy: 'size' };
/** 存放在 Eagle 的 userData 目录,不写进素材库,卸载时可直接删掉。 */
async function resolveFile() {
const userData = await eagle.app.getPath('userData');
return path.join(userData, 'SpineThumbnailGenerateServer', 'settings.json');
}
async function read() {
const file = await resolveFile();
try {
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
return { ...DEFAULTS, ...data };
} catch (error) {
if (error && error.code === 'ENOENT') return { ...DEFAULTS };
throw error;
}
}
async function write(next) {
const file = await resolveFile();
fs.mkdirSync(path.dirname(file), { recursive: true });
const body = JSON.stringify({ ...DEFAULTS, ...next }, null, 2) + '\n';
const temp = file + '.' + process.pid + '.tmp';
fs.writeFileSync(temp, body, 'utf8');
fs.renameSync(temp, file);
}
const api = { read, write, resolveFile, DEFAULTS };
if (typeof window !== 'undefined') window.SpineThumbSettings = api;
else if (typeof module !== 'undefined' && module.exports) module.exports = api;
})();