Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
201 lines (170 loc) · 6.05 KB
/
Copy pathmain.js
File metadata and controls
201 lines (170 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* EXE 图标替换工具 & PNG↔ICO 转换器 — Electron 主进程
*/
const { app, BrowserWindow, ipcMain, dialog, Menu } = require('electron');
const path = require('path');
const fs = require('fs');
const os = require('os');
const {
NtExecutable,
NtExecutableResource,
Data,
Resource,
} = require('resedit');
let mainWindow = null;
// ============================================================
// 窗口创建
// ============================================================
function createWindow() {
mainWindow = new BrowserWindow({
width: 1000,
height: 720,
minWidth: 800,
minHeight: 600,
title: '图标工具箱 v1.10.1 — PNG↔ICO & EXE图标替换',
icon: path.join(__dirname, 'app-icon.ico'),
backgroundColor: '#0f172a',
show: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
sandbox: false,
},
});
const menuTemplate = [
{
label: '文件',
submenu: [
{ label: '打开 EXE...', click: () => mainWindow.webContents.send('menu-open-exe') },
{ label: '打开 ICO...', click: () => mainWindow.webContents.send('menu-open-ico') },
{ type: 'separator' },
{ role: 'quit', label: '退出' },
],
},
{
label: '视图',
submenu: [
{ role: 'reload', label: '刷新' },
{ role: 'toggleDevTools', label: '开发者工具' },
{ type: 'separator' },
{ role: 'resetZoom', label: '重置缩放' },
{ role: 'zoomIn', label: '放大' },
{ role: 'zoomOut', label: '缩小' },
],
},
];
Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplate));
mainWindow.loadFile('index.html');
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
mainWindow.on('closed', () => {
mainWindow = null;
});
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
app.quit();
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// ============================================================
// 辅助:写临时文件 + 弹出保存对话框
// ============================================================
async function saveViaDialog(buf, defaultName, filters) {
const tmpDir = os.tmpdir();
const tmpPath = path.join(tmpDir, defaultName);
fs.writeFileSync(tmpPath, buf);
const result = await dialog.showSaveDialog(mainWindow, {
defaultPath: defaultName,
filters: filters || [],
});
if (!result.canceled && result.filePath) {
try {
fs.copyFileSync(tmpPath, result.filePath);
try { fs.unlinkSync(tmpPath); } catch {}
return { success: true, savedPath: result.filePath };
} catch (err) {
return { success: false, error: '保存文件失败: ' + err.message };
}
}
try { fs.unlinkSync(tmpPath); } catch {}
return { success: false, cancelled: true };
}
// ============================================================
// IPC: 替换图标(使用文件路径,避免大文件 IPC 传输)
// ============================================================
ipcMain.handle('replace-icon', async (_event, { exePath, icoPath }) => {
// 直接用 Buffer(.buffer 是 pool-backed 含垃圾数据,会破坏 ICO 解析)
const exeBuf = fs.readFileSync(exePath);
const icoBuf = fs.readFileSync(icoPath);
// 解析 EXE
const ntExecutable = NtExecutable.from(exeBuf, { ignoreCert: true });
const resource = NtExecutableResource.from(ntExecutable);
// 解析 ICO
const iconFile = Data.IconFile.from(icoBuf);
if (!iconFile.icons || iconFile.icons.length === 0) {
throw new Error('ICO 文件无效:未找到图标数据。请确认拖入的是 .ico 图标文件(非 PNG 直接改名)');
}
const iconItems = iconFile.icons.map((item) => item.data);
// 查找并替换所有图标组
const groupIconType = 14;
const iconGroupIds = resource.entries
.filter((e) => e.type === groupIconType)
.map((e) => e.id);
if (iconGroupIds.length === 0) {
Resource.IconGroupEntry.replaceIconsForResource(
resource.entries, 1, 0, iconItems
);
} else {
for (const gid of iconGroupIds) {
Resource.IconGroupEntry.replaceIconsForResource(
resource.entries, gid, 0, iconItems
);
}
}
// 写回
resource.outputResource(ntExecutable);
const resultBuffer = Buffer.from(ntExecutable.generate());
// 弹出保存对话框
const outName = path.basename(exePath, '.exe') + '_new.exe';
return await saveViaDialog(resultBuffer, outName, [
{ name: '可执行文件', extensions: ['exe'] },
]);
});
// ============================================================
// IPC: 提取图标(使用文件路径,避免大文件 IPC 传输)
// ============================================================
ipcMain.handle('extract-icon', async (_event, { exePath }) => {
const exeBuf = fs.readFileSync(exePath);
const ntExecutable = NtExecutable.from(exeBuf, { ignoreCert: true });
const resource = NtExecutableResource.from(ntExecutable);
const groupIconType = 14;
const iconGroups = resource.entries.filter((e) => e.type === groupIconType);
if (iconGroups.length === 0) {
throw new Error('该 EXE 文件中没有图标资源');
}
const numericIds = iconGroups
.filter((e) => typeof e.id === 'number')
.map((e) => e.id);
const mainGroupId = numericIds.length > 0
? Math.min(...numericIds)
: iconGroups[0].id;
const entries = Resource.IconGroupEntry.fromEntries(resource.entries);
const targetGroup = entries.find((g) => g.id === mainGroupId);
if (!targetGroup) {
throw new Error('无法读取图标组数据');
}
const iconItems = targetGroup.getIconItemsFromEntries(resource.entries);
const icoFile = new Data.IconFile();
icoFile.icons = iconItems.map((item) => ({ data: item }));
const resultBuffer = Buffer.from(icoFile.generate());
const outName = path.basename(exePath, '.exe') + '_icon.ico';
return await saveViaDialog(resultBuffer, outName, [
{ name: '图标文件', extensions: ['ico'] },
]);
});