Files
2026-09-11 23:30:03 +08:00

59 lines
2.3 KiB
JavaScript

'use strict';
// Eagle's item API has no arbitrary custom fields. Keep plugin metadata in
// userData, keyed by library + item ID, without editing Eagle metadata.json.
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
function identity(sourcePath, itemId) {
const directory = path.dirname(path.resolve(sourcePath));
const match = path.basename(directory).match(/^(.+)\.info$/i);
return match
? path.dirname(directory) + ':' + (itemId || match[1])
: path.resolve(sourcePath);
}
function createStore(userDataPath, sourcePath, itemId) {
const key = crypto.createHash('sha256').update(identity(sourcePath, itemId)).digest('hex');
const directory = path.join(userDataPath, 'SpineViewer', 'preview-settings');
const file = path.join(directory, key + '.json');
return {
file,
read() {
try {
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
return typeof data.premultipliedAlpha === 'boolean' ? data.premultipliedAlpha : undefined;
} catch (e) {
if (e.code === 'ENOENT') return undefined;
throw e;
}
},
// This method is called only by the user's checkbox change event.
write(premultipliedAlpha) {
if (typeof premultipliedAlpha !== 'boolean') throw new TypeError('预乘设置必须为布尔值');
fs.mkdirSync(directory, { recursive: true });
const temp = file + '.' + crypto.randomBytes(8).toString('hex') + '.tmp';
try {
fs.writeFileSync(temp, JSON.stringify({ premultipliedAlpha }, null, 2) + '\n', { flag: 'wx' });
fs.renameSync(temp, file);
} finally {
try { fs.unlinkSync(temp); } catch (e) { if (e.code !== 'ENOENT') throw e; }
}
}
};
}
async function resolveDataPath(app) {
try {
const value = app && (app.userDataPath || (typeof app.getPath === 'function' && await app.getPath('userData')));
if (typeof value === 'string' && value) return value;
} catch (e) { /* Some Eagle format-view windows reject app.getPath IPC. */ }
const home = require('os').homedir();
if (process.platform === 'darwin') return path.join(home, 'Library', 'Application Support', 'Eagle');
if (process.platform === 'win32') return path.join(process.env.APPDATA || path.join(home, 'AppData', 'Roaming'), 'Eagle');
return path.join(process.env.XDG_CONFIG_HOME || path.join(home, '.config'), 'Eagle');
}
module.exports = { createStore, resolveDataPath };