This commit is contained in:
MakerYang
2024-08-06 18:30:21 +08:00
parent f363be1fc8
commit a902dd3de7
1870 changed files with 496402 additions and 6524 deletions

0
desktop/tools/.gitkeep Normal file
View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 870 B

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

View File

@@ -0,0 +1,5 @@
import {build} from "vite";
await build({configFile: "tools/script/main/vite.config.ts"});
await build({configFile: "tools/script/preload/vite.config.ts"});
await build({configFile: "template/vite.config.ts"});

View File

@@ -0,0 +1,25 @@
import * as FileAPI from "fs";
import PathAPI from "path";
FileAPI.writeFileSync("release/.gitkeep", "");
const Index = FileAPI.readFileSync("release/dist/template/index.html", {encoding: "utf8"});
const NewIndex = Index.replace(/.\/monacoeditorwork/g, '"monacoeditorwork');
FileAPI.writeFileSync("release/dist/template/index.html", NewIndex);
const icons = FileAPI.readdirSync("tools/icons", {withFileTypes: true});
FileAPI.mkdirSync("release/dist/icons", {recursive: true});
for (let item of icons) {
let srcPath = PathAPI.join("tools/icons", item.name);
let destPath = PathAPI.join("release/dist/icons", item.name);
FileAPI.copyFileSync(srcPath, destPath);
}
FileAPI.mkdirSync("release/dist/software", {recursive: true});
const nets = FileAPI.readdirSync("tools/software", {withFileTypes: true});
for (let item of nets) {
let srcPath = PathAPI.join("tools/software", item.name);
let destPath = PathAPI.join("release/dist/software", item.name);
FileAPI.copyFileSync(srcPath, destPath);
}

View File

@@ -0,0 +1,228 @@
import os from "os";
import path from "path";
import * as FileAPI from "fs";
import * as Electron from "electron";
import ElectronDebug from "electron-debug";
import Package from "../../../package.json";
// Language Environment Configuration
import Store from "electron-store";
const store = new Store();
// Initialize the application window
let Windows: any = {
Main: false,
UserData: {
Lang: store.get("mir2:electron:language", ""),
Sleep: false,
Quit: true,
}
};
// Prevent the application from opening multiple times
if (!Electron.app.requestSingleInstanceLock()){
Electron.app.quit();
}else{
Electron.app.on("second-instance", ()=>{
if (Windows.Main) {
Windows.Main.show();
Windows.Main.setAlwaysOnTop(true);
Windows.Main.setAlwaysOnTop(false);
}
});
}
// Configuration related to environment variables and startup parameters
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
ElectronDebug({showDevTools: false, devToolsMode: "bottom"});
Electron.app.commandLine.appendSwitch("ignore-certificate-errors", "true");
Electron.app.commandLine.appendSwitch("disable-gpu", "false");
if(Windows.UserData.Lang !== ""){
Electron.app.commandLine.appendSwitch("--lang", Windows.UserData.Lang);
}
// Initialize the application's root domain and path
const application_url: string = Electron.app.isPackaged ? `file://${path.join(__dirname, "../template/index.html")}` : `http://${Package.env.VITE_DEV_SERVER_HOST}:${Package.env.VITE_DEV_SERVER_PORT}`;
const user_agent: string = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.215 Safari/537.36";
function onWindowMain(){
console.log("[main:onWindowMain]");
Windows.Main = new Electron.BrowserWindow({
frame: false,
center: true,
width: 1200,
height: 750,
minWidth: 1200,
minHeight: 750,
useContentSize: false,
hasShadow: os.platform() === "darwin",
webPreferences: {
javascript: true,
spellcheck: true,
webviewTag: true,
webSecurity: false,
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, "../preload/index.cjs")
},
fullscreen: false,
show: false,
backgroundColor: "#ffffff",
titleBarStyle: "hidden"
});
Windows.Main.loadURL(application_url + "#/?preload=" + path.join(__dirname, "../preload/index.cjs"),{
"userAgent": user_agent
}).then((res: any) => {
console.log("[main:load]", res);
});
Windows.Main.on("ready-to-show", function () {
Windows.Main.show();
});
// Implement right-click menu
Windows.Main.webContents.on("context-menu", (e: any, params: any) => {
e.preventDefault();
})
Electron.globalShortcut.register("Shift+Alt+H", () => {
Windows.Main.webContents.openDevTools({mode: "bottom", activate: false});
});
Electron.globalShortcut.register("Shift+Alt+L", () => {
Windows.Main.webContents.send("message", {type: "switch:language"});
});
Electron.globalShortcut.register("Shift+Alt+K", () => {
store.set("mir2:electron:language", "zh-CN"); // zh-CN or en-US
});
Electron.globalShortcut.register("Shift+Alt+V", () => {
Windows.Main.webContents.send("message", {type: "open:browser:tools"});
});
Electron.globalShortcut.register("Shift+Alt+S", () => {
Windows.Main.webContents.send("message", {type: "display-sleep", status: !Windows.UserData.Sleep});
});
}
// When the application is ready
Electron.app.on("ready", () => {
console.log("[main:ready]");
// Copy Net Files
FileAPI.mkdirSync(path.join(__dirname, Electron.app.isPackaged ? "../../../../temp" : "../../temp"), {recursive: true});
const net_files = FileAPI.readdirSync(path.join(__dirname, "../software"), {withFileTypes: true});
for (let item of net_files) {
if(item.name !== "chainnet.js"){
let srcPath = path.join(path.join(__dirname, "../software/"), item.name);
let destPath = path.join(path.join(__dirname, Electron.app.isPackaged ? "../../../../temp/" : "../../temp/"), item.name);
FileAPI.copyFileSync(srcPath, destPath);
}
}
});
// When the application is ready
Electron.app.whenReady().then(() => {
console.log("[main:whenready]");
onWindowMain();
// Switch the sleep configuration once to ensure the status is not 0
Windows.UserData.Sleep = Electron.powerSaveBlocker.start("prevent-display-sleep");
Electron.powerSaveBlocker.stop(Windows.UserData.Sleep);
Windows.UserData.Sleep = false;
// Active state monitoring
Electron.app.on("activate", () => {
console.log("[main:activate]");
if (Electron.BrowserWindow.getAllWindows().length === 0){
console.log("[main:activate:length]", 0);
onWindowMain();
}
});
// Request Listening and Interception Handling
Electron.session.defaultSession.webRequest.onBeforeSendHeaders((details: any, callback: any) =>{
details.requestHeaders["User-Agent"] = user_agent;
if (details.url.includes("google.com")) {
details.requestHeaders["User-Agent"] = "Chrome";
}
if (details.url.includes("cdn.nanjing.geekros.com")) {
details.requestHeaders["Referer"] = "https://makeryang.com";
}
callback({cancel: false, requestHeaders: details.requestHeaders});
});
// listens for power suspend and screen lock
Electron.powerMonitor.on("suspend", () => {
console.log("[main:powerMonitor:suspend]");
if (Windows.Main) {
Windows.Main.webContents.send("message", {type: "power-lock"});
}
});
Electron.powerMonitor.on("resume", () => {
console.log("[main:powerMonitor:resume]");
if (Windows.Main) {
Windows.Main.webContents.send("message", {type: "power-unlock"});
}
});
Electron.powerMonitor.on("lock-screen", () => {
console.log("[main:powerMonitor:lock-screen]");
if (Windows.Main) {
Windows.Main.webContents.send("message", {type: "screen-lock"});
}
});
Electron.powerMonitor.on("unlock-screen", () => {
console.log("[main:powerMonitor:unlock-screen]");
if (Windows.Main) {
Windows.Main.webContents.send("message", {type: "screen-unlock"});
}
});
});
// Listen for main process messages
Electron.ipcMain.on("message", (event: any, args: any) => {
if(args.type === "header:right:button"){
if(args.data === "close"){
if(!Windows.UserData.Quit){
Windows.Main.hide();
}else{
Windows.Main.close();
Electron.app.quit();
}
}
if(args.data === "min"){
Windows.Main.minimize();
}
if(args.data === "size"){
if(Windows.Main.isMaximized()){
Windows.Main.unmaximize();
}else{
Windows.Main.maximize();
}
}
if(args.data === "resize"){
if(Windows.Main.isMaximized()){
event.sender.send("message", {type: "header:right:button", data: "max"});
}else{
event.sender.send("message", {type: "header:right:button", data: "restore"});
}
}
}
if(args.type === "on:login"){
event.sender.send("message", {type: "on:login"});
}
if(args.type === "display:sleep"){
if(args.status){
Windows.UserData.Sleep = Electron.powerSaveBlocker.start("prevent-display-sleep");
}else{
if(Windows.UserData.Sleep){
Electron.powerSaveBlocker.stop(Windows.UserData.Sleep);
Windows.UserData.Sleep = false;
}
}
console.log("[main:display:sleep]", Windows.UserData.Sleep);
}
if(args.type === "updater"){
console.log("[main:updater]");
}
if(args.type === "quit"){
console.log("[main:quit]");
}
});

View File

@@ -0,0 +1,26 @@
import {defineConfig} from "vite";
import {builtinModules} from "module";
import Package from "../../../package.json";
export default defineConfig({
root: __dirname,
build: {
outDir: "../../../release/dist/main",
emptyOutDir: true,
minify: "terser",
sourcemap: false,
lib: {
entry: "index.ts",
formats: ["cjs"],
fileName: () => "[name].cjs",
},
rollupOptions: {
external: [
"electron",
...builtinModules,
...builtinModules.map(e => `node:${e}`),
...Object.keys(Package.dependencies || {}),
]
},
}
})

View File

@@ -0,0 +1,45 @@
import os from "os";
import Path from "path";
import File from "fs";
import {ipcRenderer} from "electron";
import Shell from "./shell";
import * as Config from "../../../package.json";
(window as any).base = {
os: os,
path: Path,
process: process,
platform: os.platform(), //darwin、linux、win32
config: Config,
file: File,
ipc: ipcRenderer,
lang: {
t: false,
locale: false
},
window: {
max: false
},
tools: {
shell: Shell,
crypto: require("crypto"),
navigator: navigator
},
app_path: (process: any)=> {
return Path.join(__dirname, (process.env["VITE_DEV_SERVER_HOST"] !== "127.0.0.1" ? "./../../../../" : "../../"));
},
app_data_path: (process: any)=> {
const path_temp= (os.platform() === "win32" ? process.env["APPDATA"] + "" : process.env["HOME"] + "");
return Path.join(path_temp, "./");
},
app_home_path: (process: any)=> {
const path_temp= (os.platform() === "win32" ? (process.env["HOMEDRIVE"] + "" + process.env["HOMEPATH"]) : process.env["HOME"] + "");
return Path.join(path_temp, "./");
},
app_temp_path: (process: any)=> {
return Path.join(os.tmpdir(), "./");
},
environment: (process: any) => {
return process.env["VITE_DEV_SERVER_HOST"] !== "127.0.0.1" ? "produce" : "develop"
},
}

View File

@@ -0,0 +1,61 @@
const Shell = {
command: (command: any, success: any, onerror: any, stderr: any, stdout: any, close: any) => {
const child_process = require("child_process");
const spawn: any = child_process.spawn(command.path, command.args, command.options ? command.options : {});
if(typeof success == "function"){
success(spawn);
}
spawn.on("error", (err: any) => {
if(typeof onerror == "function"){
onerror(err);
}
});
spawn.stderr.on("data", (data: any) => {
if(typeof stderr == "function"){
stderr(data);
}
});
spawn.stdout.on("data", (data: any) => {
if(typeof stdout == "function"){
stdout(data);
}
});
spawn.on("close", (code: any) => {
if(typeof close == "function"){
close(code);
}
});
},
commands: (commands: any, callback: any)=>{
return new Promise((resolve: any, reject: any)=> {
let command_index = 0;
const NextCommand = () => {
if (command_index >= commands.length) {
return resolve();
}
const item = commands[command_index];
Shell.command(item, (spawn: any)=>{
callback({type: "spawn", data: spawn, index: command_index});
}, (error: any)=>{
callback({type: "error", data: error.toString(), index: command_index});
reject(false);
}, (stderr: any)=>{
callback({type: "stderr", data: stderr.toString(), index: command_index});
reject(false);
}, (stdout: any)=>{
callback({type: "stdout", data: stdout.toString(), index: command_index});
}, (code: any)=>{
callback({type: "code", data: code, index: command_index});
if(code !== 0){
reject(false);
}else{
command_index++;
NextCommand();
}
});
}
});
}
}
export default Shell;

View File

@@ -0,0 +1,31 @@
import {defineConfig} from "vite";
import {join} from "path";
import {builtinModules} from "module";
import Package from "../../../package.json";
export default defineConfig({
root: __dirname,
plugins: [],
build: {
outDir: "../../../release/dist/preload",
emptyOutDir: true,
minify: "terser",
sourcemap: false,
rollupOptions: {
input: {
index: join(__dirname, "index.ts")
},
output: {
format: "cjs",
entryFileNames: "[name].cjs",
manualChunks: {},
},
external: [
"electron",
...builtinModules,
...builtinModules.map(e => `node:${e}`),
...Object.keys(Package.dependencies || {}),
],
},
}
})

View File

@@ -0,0 +1,52 @@
import {spawn} from "child_process";
import {build, createServer} from "vite";
import electron from "electron";
function watchMain(server) {
const address = server.httpServer.address();
const env = Object.assign(process.env, {
VITE_DEV_SERVER_HOST: address.address,
VITE_DEV_SERVER_PORT: address.port,
});
return build({
configFile: "tools/script/main/vite.config.ts",
mode: "development",
plugins: [{
name: "electron-main-watcher",
writeBundle(command, options) {
if (process.electronApp) {
process.electronApp.removeAllListeners()
process.electronApp.kill()
}
process.electronApp = spawn(electron, [".", "--no-sandbox"], {stdio: "inherit", env})
process.electronApp.once("exit", process.exit)
},
}],
build: {
watch: {},
},
})
}
function watchPreload(server) {
return build({
configFile: "tools/script/preload/vite.config.ts",
mode: "development",
plugins: [{
name: "electron-preload-watcher",
writeBundle() {
server.ws.send({type: "full-reload"})
}
}],
build: {
watch: {},
},
})
}
const server = await createServer({configFile: "template/vite.config.ts"});
await server.listen();
await watchPreload(server);
await watchMain(server);

View File