LaiTool/src/main/index.js

486 lines
14 KiB
JavaScript
Raw Normal View History

2024-07-13 15:44:13 +08:00
import fspromises from 'fs/promises'
import { v4 as uuidv4 } from 'uuid'
2024-05-24 13:46:19 +08:00
import { version } from '../../package.json'
2024-07-13 15:44:13 +08:00
import { graphics } from 'systeminformation'
2024-05-24 13:46:19 +08:00
2024-08-08 16:24:47 +08:00
import { app, shell, BrowserWindow, ipcMain, dialog, nativeTheme, session } from 'electron'
2024-05-15 12:57:15 +08:00
import path, { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.ico?asset'
import { define } from '../define/define.js'
import { func } from './func.js'
2024-08-03 12:46:12 +08:00
import { AsyncQueue } from './quene'
import { DEFINE_STRING } from '../define/define_string'
2024-05-15 12:57:15 +08:00
import { Tools } from './tools.js'
2024-06-24 13:11:19 +08:00
import { ImageGenerate } from './ReverseManage/imageGenerate.js'
2024-05-15 12:57:15 +08:00
import { Setting } from './setting/setting.js'
import { has, isEmpty } from 'lodash'
2024-07-13 15:44:13 +08:00
import { AutoSync } from './setting/autoSync.js'
2024-08-03 12:46:12 +08:00
import { TaskManager } from './Service/taskManage'
2024-05-15 12:57:15 +08:00
// ipc
import { DiscordIpc, RemoveDiscordIpc } from './IPCEvent/discordIpc.js'
2024-07-13 15:44:13 +08:00
import { Logger } from './logger.js'
import { RegisterIpc } from './IPCEvent/index.js'
2024-05-15 12:57:15 +08:00
2024-07-13 15:44:13 +08:00
let tools = new Tools()
let imageGenerate = new ImageGenerate(global)
let setting = new Setting(global)
2024-05-24 13:46:19 +08:00
2024-05-15 12:57:15 +08:00
async function InitData(gl) {
2024-07-13 15:44:13 +08:00
let res = await setting.getSettingDafultData()
gl.config = res
return res
2024-05-15 12:57:15 +08:00
}
function setIpcHandler(hash) {
2024-07-13 15:44:13 +08:00
if (hash == 'discord') {
DiscordIpc(global)
2024-05-15 12:57:15 +08:00
}
}
function removeIpcHandler(hash) {
2024-07-13 15:44:13 +08:00
if (hash == 'discord') {
RemoveDiscordIpc()
2024-05-15 12:57:15 +08:00
}
}
2024-07-13 15:44:13 +08:00
async function createWindow(hash = 'ShowMessage', data, url = null) {
2024-05-15 12:57:15 +08:00
// Create the browser window.
2024-07-13 15:44:13 +08:00
await InitData(global)
global.currentHash = hash
2024-05-24 13:46:19 +08:00
// 判断当前是不是有设置的宽高,用的话记忆
2024-07-13 15:44:13 +08:00
let isRe =
global.config.window_wh_bm_remember && hash == 'ShowMessage' && global.config.window_wh_bm
2024-08-08 16:24:47 +08:00
const ses = session.fromPartition('persist:my-session')
2024-05-15 12:57:15 +08:00
let mainWindow = new BrowserWindow({
2024-05-24 13:46:19 +08:00
width: isRe ? global.config.window_wh_bm.width : 900,
height: isRe ? global.config.window_wh_bm.height : 675,
x: isRe ? global.config.window_wh_bm.x : 100,
y: isRe ? global.config.window_wh_bm.y : 100,
2024-05-15 12:57:15 +08:00
title: 'LAITool',
icon: '../../resources/icon.ico',
show: false,
autoHideMenuBar: true,
...(process.platform === 'linux' ? { icon } : {}),
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false,
2024-07-13 15:44:13 +08:00
nodeIntegration: hash == 'discord' ? false : true, // 在网页中集成Node
2024-05-15 12:57:15 +08:00
nodeIntegrationInWorker: true,
webSecurity: false,
2024-08-08 16:24:47 +08:00
partition: 'persist:my-partition',
session: ses,
webviewTag : true,
2024-05-15 12:57:15 +08:00
}
})
2024-07-13 15:44:13 +08:00
mainWindow.myID = uuidv4()
2024-05-15 12:57:15 +08:00
mainWindow.on('ready-to-show', () => {
mainWindow.show()
})
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
})
// HMR for renderer base on electron-vite cli.
// Load the remote URL for development or the local html file for production.
// 判断是不是加载外部网页
if (url) {
mainWindow.loadURL(url)
} else {
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
2024-07-13 15:44:13 +08:00
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'] + '/#/' + hash)
2024-08-03 12:46:12 +08:00
// mainWindow.webContents.openDevTools()
2024-05-15 12:57:15 +08:00
} else {
2024-07-13 15:44:13 +08:00
if (hash != '') {
2024-05-15 12:57:15 +08:00
mainWindow.loadURL(`file://${path.join(__dirname, '../renderer/index.html')}#/${hash}`)
} else {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
}
}
}
2024-07-13 15:44:13 +08:00
mainWindow.on('close', async () => {
2024-05-15 12:57:15 +08:00
// 判断指定的窗口,移除指定的监听
2024-07-13 15:44:13 +08:00
removeIpcHandler(hash)
global.newWindow = global.newWindow.filter((item) => item.id != mainWindow.id)
2024-05-24 13:46:19 +08:00
// 判断当前的是不是开启了记录功能
2024-07-13 15:44:13 +08:00
if (global.config.window_wh_bm_remember && hash == 'ShowMessage') {
let window_wh_bm = mainWindow.getBounds()
2024-05-24 13:46:19 +08:00
// 记录到文件中
await setting.ModifySampleSetting(JSON.stringify({ window_wh_bm: window_wh_bm }))
}
2024-05-15 12:57:15 +08:00
})
// 创建一个新的窗口,添加对应的监听
global.newWindow.push({
hash,
id: mainWindow.id,
win: mainWindow,
init_folder: data
})
2024-07-13 15:44:13 +08:00
setIpcHandler(hash)
return mainWindow
2024-05-15 12:57:15 +08:00
}
2024-07-13 15:44:13 +08:00
let mainWindow
global.createWindow = createWindow
2024-05-15 12:57:15 +08:00
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(async () => {
// Set app user model id for windows
electronApp.setAppUserModelId('com.electron')
// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
//判断是不是又配置文件没有的话将temp中的基础配置加载
// 判断文件夹是不是存在
2024-07-13 15:44:13 +08:00
let config_p = path.dirname(define.config_path)
let isE = await tools.checkExists(config_p)
2024-05-15 12:57:15 +08:00
// 文件夹存在判断json文件数量
2024-07-13 15:44:13 +08:00
let ex_json = []
2024-05-15 12:57:15 +08:00
if (isE) {
2024-07-13 15:44:13 +08:00
let ex_json_path = await tools.getFilesWithExtensions(config_p, '.json')
2024-05-15 12:57:15 +08:00
for (let i = 0; i < ex_json_path.length; i++) {
2024-07-13 15:44:13 +08:00
const element = ex_json_path[i]
ex_json.push(path.basename(element))
2024-05-15 12:57:15 +08:00
}
} else {
2024-07-13 15:44:13 +08:00
await fspromises.mkdir(config_p, { recursive: true })
2024-05-15 12:57:15 +08:00
}
// 判断文件是不是存在,不存在添加
if (ex_json.length != 5) {
2024-07-13 15:44:13 +08:00
let temp_path = path.join(path.dirname(define.draft_temp_path), 'config')
let tmp_json_path = await tools.getFilesWithExtensions(temp_path, '.json')
2024-05-15 12:57:15 +08:00
for (let i = 0; i < tmp_json_path.length; i++) {
2024-07-13 15:44:13 +08:00
const element = tmp_json_path[i]
2024-05-15 12:57:15 +08:00
if (!ex_json.includes(path.basename(element))) {
await fspromises.copyFile(element, path.join(config_p, path.basename(element)))
}
}
}
// 判断动态文件是不是存在
2024-07-13 15:44:13 +08:00
tools.checkJsonFileExistsOrCreate(path.normalize(define.dynamic_setting))
2024-05-15 12:57:15 +08:00
// 判断标签文件是不是存在
2024-07-13 15:44:13 +08:00
tools.checkJsonFileExistsOrCreate(
path.normalize(define.tag_setting),
JSON.stringify({
character_tags: [],
style_tags: [],
scene_tags: [],
prefix_tags: [],
suffix_tags: []
})
)
2024-05-24 13:46:19 +08:00
// 判断SD图片缓存文件是不是存在不存在创建
2024-07-13 15:44:13 +08:00
tools.checkFolderExistsOrCreate(path.normalize(define.temp_sd_image))
tools.checkFolderExistsOrCreate(path.normalize(path.join(define.image_path, 'c_s')))
2024-05-15 12:57:15 +08:00
2024-05-26 12:12:15 +08:00
app.on('activate', async function () {
2024-05-15 12:57:15 +08:00
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
2024-05-26 12:12:15 +08:00
if (BrowserWindow.getAllWindows().length === 0) {
mainWindow = createWindow('ShowMessage', null)
}
2024-05-15 12:57:15 +08:00
})
2024-08-04 15:00:00 +08:00
global.logger = new Logger(define.logger_path)
// 同步数据
await AutoSync()
global.newWindow = []
mainWindow = await createWindow('ShowMessage', null)
global.requestQuene = new AsyncQueue(global, global.config.task_number)
global.fileQueue = new AsyncQueue(global, 1)
2024-05-15 12:57:15 +08:00
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
2024-07-13 15:44:13 +08:00
RegisterIpc(createWindow)
2024-05-15 12:57:15 +08:00
ipcMain.handle('dark-mode:toggle', (event, value) => {
if (value) {
2024-07-13 15:44:13 +08:00
nativeTheme.themeSource = value
2024-05-15 12:57:15 +08:00
} else {
2024-07-13 15:44:13 +08:00
nativeTheme.themeSource = 'system'
2024-05-15 12:57:15 +08:00
}
return nativeTheme.shouldUseDarkColors
})
// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.
ipcMain.handle(DEFINE_STRING.GET_SETTING_Dafault_DATA, async (event) => {
2024-07-13 15:44:13 +08:00
return await InitData(global)
2024-05-15 12:57:15 +08:00
})
ipcMain.handle(DEFINE_STRING.GET_DRAFT_FILE_LIST, async (event) => {
2024-07-13 15:44:13 +08:00
let res = await func.getDraftFileList()
return res
2024-05-15 12:57:15 +08:00
})
ipcMain.handle(DEFINE_STRING.SELECT_FOLDER, async (event, value = null) => {
2024-07-13 15:44:13 +08:00
let po = ['openDirectory']
2024-05-15 12:57:15 +08:00
if (value && !isEmpty(value.multi)) {
2024-07-13 15:44:13 +08:00
po.push('multiSelections')
2024-05-15 12:57:15 +08:00
}
let { filePaths } = await dialog.showOpenDialog({
properties: po,
2024-07-13 15:44:13 +08:00
defaultPath: value && !isEmpty(value.defaultPath) ? value.defaultPath : ''
2024-05-15 12:57:15 +08:00
})
2024-07-13 15:44:13 +08:00
return filePaths
2024-05-15 12:57:15 +08:00
})
ipcMain.handle(DEFINE_STRING.SELECT_FILE, async (event, value) => {
try {
let { filePaths } = await dialog.showOpenDialog({
properties: ['openFile'],
2024-07-13 15:44:13 +08:00
filters: [{ name: 'fileName', extensions: value }]
2024-05-15 12:57:15 +08:00
})
return {
code: 1,
value: filePaths[0]
}
} catch (error) {
return {
code: 0,
message: `Error Message ${error}`
}
}
})
ipcMain.handle(DEFINE_STRING.GET_DRAFT_TEXT_STYLE, async (event, value) => {
2024-07-13 15:44:13 +08:00
let res = await func.getDraftTextStyle(value)
return res
2024-05-15 12:57:15 +08:00
})
ipcMain.handle(DEFINE_STRING.GET_TEXT_STYLE_LIST, async (event) => {
2024-07-13 15:44:13 +08:00
let res = await func.getClipSetting('text_style')
return res
2024-05-15 12:57:15 +08:00
})
ipcMain.handle(DEFINE_STRING.GET_FRIENDLY_REMINDER_LIST, async (event) => {
2024-07-13 15:44:13 +08:00
let res = await func.getClipSetting('friendly_reminder_setting')
return res
2024-05-15 12:57:15 +08:00
})
ipcMain.handle(DEFINE_STRING.GET_FRIENDLY_REMINDER_DRAFT, async (event, value) => {
2024-07-13 15:44:13 +08:00
let res = await func.GetDraftFriendlyReminder(value)
return res
2024-05-15 12:57:15 +08:00
})
ipcMain.handle(DEFINE_STRING.ADD_DRAFT, async (event, value) => {
2024-07-13 15:44:13 +08:00
let res = await func.addDraft(value)
return res
2024-05-15 12:57:15 +08:00
})
// 获取当前版本
2024-06-01 15:08:22 +08:00
ipcMain.handle(DEFINE_STRING.GET_VERSION, async (event) => {
// 获取当前电脑的显卡信息
2024-07-13 15:44:13 +08:00
let da = await graphics()
2024-06-01 15:08:22 +08:00
for (let i = 0; i < da.controllers.length; i++) {
// 获取第一个英伟达或者是AMD的显卡信息
2024-07-13 15:44:13 +08:00
const element = da.controllers[i]
if (element.vendor.startsWith('NVIDIA')) {
global.gpu = element
global.gpu.type = 'NVIDIA'
break
} else if (element.vendor.startsWith('AMD') || element.vendor.startsWith('Advanced')) {
global.gpu = element
global.gpu.type = 'AMD'
break
2024-06-01 15:08:22 +08:00
} else {
global.gpu = {
2024-07-13 15:44:13 +08:00
name: 'OTHER'
}
global.gpu.type = 'OTHER'
2024-06-01 15:08:22 +08:00
}
}
2024-07-13 15:44:13 +08:00
return version + ' ' + (global.gpu?.name ? global.gpu.name : '')
})
2024-05-15 12:57:15 +08:00
// 监听保存SD配置
ipcMain.handle(DEFINE_STRING.SAVE_SD_CONFIG, async (event, value) => await func.SaveSDConfig(value))
// 监听保存生成视频的简单配置
2024-07-13 15:44:13 +08:00
ipcMain.handle(
DEFINE_STRING.SAVE_GENERAL_SETTING,
async (event, value) => await func.SaveGeneralSetting(value)
)
2024-05-15 12:57:15 +08:00
// 获取当前的视频合成配置信息
2024-07-13 15:44:13 +08:00
ipcMain.handle(
DEFINE_STRING.GET_VIDEO_CONFIG_MESSAGE,
async (event) => await func.GetVideoConfigMessage()
)
2024-05-15 12:57:15 +08:00
// 监听保存字幕的是指信息
2024-07-13 15:44:13 +08:00
ipcMain.handle(
DEFINE_STRING.SAVE_ASS_CONFIG,
async (event, value) => await func.SaveAssConfig(value)
)
2024-05-15 12:57:15 +08:00
// 监听获取当前系统安装的字体
2024-07-13 15:44:13 +08:00
ipcMain.handle(
DEFINE_STRING.GET_SYSTEM_INSTALL_FONTNAME,
async (event) => await func.GetSystemInstallFontName()
)
2024-05-15 12:57:15 +08:00
// 监听删除视频配置任务删除指定ID的值
2024-07-13 15:44:13 +08:00
ipcMain.handle(
DEFINE_STRING.DELETE_VIDEO_CONFIG,
async (event, value) => await func.DeleteVideoConfig(value)
)
2024-05-15 12:57:15 +08:00
// 监听添加生图任务信息
2024-07-13 15:44:13 +08:00
ipcMain.handle(
DEFINE_STRING.ADD_IMAGE_TASK_LIST,
async (event, value) => await func.AddImageTask(value)
)
2024-05-15 12:57:15 +08:00
// 监听删除生成图片列表中的信息
2024-07-13 15:44:13 +08:00
ipcMain.handle(
DEFINE_STRING.DELETE_IMAGE_TASK_LIST,
async (event, value) => await func.DeleteImageTaskList(value)
)
2024-05-15 12:57:15 +08:00
// 监听获取加密的机械码任务
2024-07-13 15:44:13 +08:00
ipcMain.handle(DEFINE_STRING.GET_MACHINE_ID, async (event, value) => await func.GetMachineId())
2024-05-15 12:57:15 +08:00
// 监听获取不想要的提示词任务
2024-07-13 15:44:13 +08:00
ipcMain.handle(DEFINE_STRING.GET_BAD_PROMPT, async (event) => await func.GetBadPrompt())
2024-05-15 12:57:15 +08:00
// 保存不想要的提示词
2024-07-13 15:44:13 +08:00
ipcMain.handle(
DEFINE_STRING.SAVE_BAD_PROMPT,
async (event, value) => await func.SaveBadPrompt(value)
)
2024-05-15 12:57:15 +08:00
// 一键删除不想要的值
2024-07-13 15:44:13 +08:00
ipcMain.handle(DEFINE_STRING.DELETE_BAD_PROMPT, async (event) => await func.DeleteBadPrompt())
2024-05-15 12:57:15 +08:00
// 监听反推任务
2024-07-13 15:44:13 +08:00
ipcMain.handle(DEFINE_STRING.PUSH_BACK_PROMPT, async (event) => await func.PushBackPrompt())
2024-05-15 12:57:15 +08:00
// 打开GPT购买界面
2024-07-13 15:44:13 +08:00
ipcMain.on(DEFINE_STRING.OPEN_GPT_BUY_URL, async (event, value) => await func.openGptBuyUrl(value))
2024-05-15 12:57:15 +08:00
// 监听打开任意网站的任务
2024-07-13 15:44:13 +08:00
ipcMain.on(DEFINE_STRING.OPEN_URL, async (event, value) => await func.OpenUrl(value))
2024-05-15 12:57:15 +08:00
// 监听抽帧任务
2024-07-13 15:44:13 +08:00
ipcMain.handle(DEFINE_STRING.GET_FRAME, async (event, value) => await func.getFrame(value))
2024-05-15 12:57:15 +08:00
// 监听获取ADtailer列表的任务
2024-07-13 15:44:13 +08:00
ipcMain.handle(DEFINE_STRING.GET_ADETAILER_LIST, async (event) => await func.GetADetailerList())
2024-05-15 12:57:15 +08:00
// 监听保存ADtailer数据
2024-07-13 15:44:13 +08:00
ipcMain.handle(
DEFINE_STRING.SAVE_DETAILER_CONFIG,
async (event, value) => await func.SaveADetailerConfig(value)
)
2024-05-15 12:57:15 +08:00
// 分镜识别。语音识别
2024-07-13 15:44:13 +08:00
ipcMain.handle(
DEFINE_STRING.START_STORY_BOARDING,
async (event, value) => await func.StartStoryboarding(value)
)
2024-05-15 12:57:15 +08:00
// 保存试用结束时间
ipcMain.on(DEFINE_STRING.SAVE_TRIAL_END_TIME, (event, value) => {
2024-07-13 15:44:13 +08:00
global.endTime = value.endTime
global.permissions = value.permissions
})
2024-05-15 12:57:15 +08:00
// 获取当前机器的权限
ipcMain.handle(DEFINE_STRING.GET_PERMISSION, async (event) => {
return {
code: 1,
endTime: global.endTime,
permissions: global.permissions
}
})
// 监听字幕的保存
ipcMain.handle(DEFINE_STRING.SAVE_NEW_WORD, async (event, value) => {
2024-07-13 15:44:13 +08:00
return await func.SaveNewWord(value)
2024-05-15 12:57:15 +08:00
})
/**
* 监听字幕对齐任务通过前端传过来的数据
*/
ipcMain.handle(DEFINE_STRING.ALIGN_DRAFT_IMG_TO_TEXT, async (event, value) => {
2024-07-13 15:44:13 +08:00
return await func.alginDraftImgToText(value)
2024-05-15 12:57:15 +08:00
})
/**
* 监听单张重绘任务
*/
ipcMain.handle(DEFINE_STRING.RE_GENERATE_IAMGE_ONE, async (event, value) => {
2024-07-13 15:44:13 +08:00
console.log(value[0])
let newWindow = global.newWindow.filter((item) => item.id == value[0])
2024-05-15 12:57:15 +08:00
let res = await func.ReGenerateImageOne(newWindow, value)
2024-07-13 15:44:13 +08:00
return res
2024-05-15 12:57:15 +08:00
})
/**
2024-07-13 15:44:13 +08:00
* 监听文件夹高清任务
2024-05-15 12:57:15 +08:00
*/
ipcMain.handle(DEFINE_STRING.IMPROVE_IMAGE_RESOULTION, async (event, value) => {
2024-07-13 15:44:13 +08:00
let res = await imageGenerate.ImproveResolution(value)
return res
2024-05-15 12:57:15 +08:00
})
/**
* 监听刷新窗口的事件
*/
ipcMain.handle(DEFINE_STRING.REFRASH_IMAGWE_DATA, async (event, value) => {
console.log(value)
2024-07-13 15:44:13 +08:00
let newWindow = global.newWindow.filter((item) => item.id == value[0])
let img_dir = path.join(global.config.project_path, 'tmp/' + value[1])
let res = await imageGenerate.getFolderImageList(newWindow, img_dir, true)
return res
2024-05-15 12:57:15 +08:00
})
/**
* 监听一个窗口返回窗口创建成功后需要的基础数据
*/
ipcMain.on(DEFINE_STRING.SHOW_NEW_WINDOW, async (event, value) => {
2024-07-13 15:44:13 +08:00
let newW = createWindow(value[0], value[1])
2024-05-15 12:57:15 +08:00
})
// 监听程序关闭
ipcMain.on(DEFINE_STRING.QUIT_APP, (event) => {
2024-07-13 15:44:13 +08:00
app.quit()
2024-05-15 12:57:15 +08:00
})
/**
* 监听图片对齐任务
*/
ipcMain.on(DEFINE_STRING.ALIGN_DRAFT_IMG, async (event, value) => {
func.alignDraftImage(mainWindow, value)
})