Skip to content

Commit

Permalink
Update 4.0
Browse files Browse the repository at this point in the history
Performance Update
Less Code
  • Loading branch information
NyboTV committed Mar 4, 2024
1 parent ac2fbad commit 1cda476
Show file tree
Hide file tree
Showing 23 changed files with 2,847 additions and 2,930 deletions.
1,101 changes: 1,067 additions & 34 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tp_ets2_plugin",
"version": "3.4.3",
"version": "3.4.4",
"description": "Euro Truck Simulator 2 Plugin for TouchPortal",
"main": "src/bin/plugin.js",
"scripts": {
Expand Down Expand Up @@ -46,6 +46,7 @@
"homepage": "https://github.com/NyboTV/TP_ETS2_Plugin#readme",
"dependencies": {
"adm-zip": "^0.5.10",
"archiver": "^7.0.0",
"axios": "^1.4.0",
"check-internet-connected": "^2.0.6",
"current-currency": "^1.6.2",
Expand All @@ -57,14 +58,16 @@
"fs-extra": "^11.1.1",
"get-folder-size": "^2.0.0",
"jimp": "^0.22.8",
"moment-timezone": "^0.5.45",
"node-fetch": "^3.3.1",
"path": "^0.12.7",
"pidusage": "^3.0.2",
"prompt": "^1.3.0",
"replace-json-property": "^1.9.0",
"request": "^2.88.2",
"self-reload-json": "^0.4.0",
"touchportal-api": "^3.2.1"
"touchportal-api": "^3.2.1",
"winston-daily-rotate-file": "^5.0.0"
},
"devDependencies": {
"electron": "^25.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/bin/config/cfg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "3.4.3",
"refreshInterval": 500,
"version": "3.4.4",
"refreshInterval": 200,
"debug": false,
"firstInstall": false,
"UpdateCheck": true,
Expand Down
44 changes: 18 additions & 26 deletions src/bin/modules/script/autoupdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@ const { exec } = require(`child_process`)
const debugMode = process.argv.includes("--debugging")
const Testing = process.argv.includes("--testing")

const isRunning = (query, cb) => {
exec('tasklist', (err, stdout, stderr) => {
cb(stdout.toLowerCase().indexOf(query.toLowerCase()) > -1);
});
}

const autoupdate = async (UpdateCheck, PreReleaseAllowed, lastVersion, logIt, showDialog, timeout) => {
const AutoUpdate = async (UpdateCheck, PreReleaseAllowed, lastVersion, logger, showDialog, timeout) => {
return new Promise(async (resolve, reject) => {
let newversion = ""
let NeedUpdate = false

try {
if (UpdateCheck === true && debugMode === false || Testing === true) {
logIt("AUTOUPDATE", "INFO", "Checking for Updates... (Searching for PreRelease allowed: " + PreReleaseAllowed + ")")
logger.info("[AUTOUPDATE] Checking for Updates... (Searching for PreRelease allowed: " + PreReleaseAllowed + ")")

axios.get('https://api.github.com/repos/NyboTV/TP_ETS2_Plugin/releases')
.then(async function(response) {
Expand All @@ -54,7 +48,7 @@ const autoupdate = async (UpdateCheck, PreReleaseAllowed, lastVersion, logIt, sh

if (!response.assets[0]) {
NeedUpdate = false
logIt("AUTOUPDATE", "WARNING", "AutoUpdate Check skipped. Asset is not present.")
logger.info("[AUTOUPDATE] Check skipped. Asset is not present.")
}

if (NeedUpdate === true) {
Expand All @@ -74,20 +68,19 @@ const autoupdate = async (UpdateCheck, PreReleaseAllowed, lastVersion, logIt, sh

if (UpdateQuestion === 1) {
showDialog("warning", ["Okay"], `If the Import will not start after the ProgressBar Closed, then you need to go to your Downloads Folder and execute the ETS2_Dashboard.tpp File! \n\nIf Windows will ask you which Programm you want to use to open the File, search the TouchPortal.exe and select it!`)
logIt("AUTOUPDATE", "INFO", "Update starting...")
logger.info("[AUTOUPDATE] Update starting...")

let progressBar
var body = "";
var cur = 0;
var len = ""
var total = ""

if(fs.existsSync(`${download_path}/ETS2_Dashboard.tpp`)) {
try {
fs.rmSync(`${download_path}/ETS2_Dashboard.tpp`)
} catch (e) {
await showDialog("warning", ["Done"], "Due to AntiVirus issues we can not delete any Files outside this Plugin. Please delete the 'ETS2_Dashboard.tpp' File in your Downloads Folder")
logIt("AUTOUPDATE", "ERROR", e)
logger.error("[AUTOUPDATE] ERROR " + e)
}
}

Expand All @@ -111,7 +104,7 @@ const autoupdate = async (UpdateCheck, PreReleaseAllowed, lastVersion, logIt, sh

progressBar = new ProgressBar({
title: "ETS2 Dashboard Update",
text: 'AutoUpdate',
text: 'AUTOUPDATE',
indeterminate: false,
maxValue: 101
});
Expand All @@ -126,22 +119,22 @@ const autoupdate = async (UpdateCheck, PreReleaseAllowed, lastVersion, logIt, sh
progressBar.value = Math.round(Number((100.0 * cur / len)))
})
.on('error', async (error) => {
logIt("AUTOUPDATER", "INFO", "Error while Downloading Update... " + error)
logger.error("[AUTOUPDATE] Error while Downloading Update... " + error)
progressBar.detail = `Error while downloading Update... Closing in 5 Seconds...`
await timeout(5000)
exit()
})
.pipe(file)
.on('finish', async () => {
progressBar.detail = `Download Completed.`
logIt("AUTOUPDATE", "INFO", "Download Finished!")
logIt("AUTOUPDATE", "INFO", "Waiting 1.5 Seconds to let the Script write the zip Header...")
logger.info("[AUTOUPDATE] Download Finished!")
logger.info("[AUTOUPDATE] Waiting 1.5 Seconds to let the Script write the zip Header...")
await timeout(1500)
progressBar.detail = 'Finishing Download...';

await timeout(200)

logIt("AUTOUPDATE", "INFO", "Backup Config...")
logger.info("[AUTOUPDATE] Backup Config...")
progressBar.detail = 'Backup Config Files...';

fs.mkdirSync(`${download_path}/ETS2_Dashboard-Backup`)
Expand All @@ -155,39 +148,38 @@ const autoupdate = async (UpdateCheck, PreReleaseAllowed, lastVersion, logIt, sh
try{
exec(`${download_path}/ETS2_Dashboard.tpp`)
} catch (e) {
logIt("AUTOUPDATE", "ERROR", `${e}`)
logger.error(`[AUTOUPDATE] ERROR ${e}`)
}
await timeout(1500)
logIt("AUTOUPDATE", "INFO", "Exiting Plugin due to Update...")
logger.info("[AUTOUPDATE] Exiting Plugin due to Update...")
progressBar.close()
exit()
})

} else if (UpdateQuestion === 0) {
logIt("AUTOUPDATE", "INFO", "Update Skipped")
logger.info("[AUTOUPDATE] Update Skipped")
resolve()
}
} else {
logIt("AUTOUPDATE", "INFO", "No Update Found.")
logger.info("[AUTOUPDATE] No Update Found.")
resolve()
}

})
.catch(function(error) {
logIt("AUTOUPDATE", "Error", error)
logger.error("[AUTOUPDATE] Error " + error)
})

} else {
logIt("AUTOUPDATE", "INFO", "Disabled")
logger.info("[AUTOUPDATE] Disabled")
resolve()
}

} catch (e) {
logIt("AUTOUPDATER", "INFO", "AutoUpdater Failed!")
logIt("AUTOUPDATER", "INFO", e)
logger.error("[AUTOUPDATE] ERROR " + e)
//resolve()
}
})
}

module.exports = autoupdate
module.exports = AutoUpdate
9 changes: 9 additions & 0 deletions src/bin/modules/script/emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// eventsBroker.js

const EventEmitter = require('events');
const eventBroker = new EventEmitter();

module.exports = {
emit: eventBroker.emit.bind(eventBroker),
on: eventBroker.on.bind(eventBroker),
};
11 changes: 5 additions & 6 deletions src/bin/modules/script/filescheck.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const fs = require('fs')

let missing = 0

const filescheck = async (path, cfg_path, logIt, timeout) => {
const filescheck = async (cfg_path, logger) => {
return new Promise(async (resolve, reject) => {
logIt("FILESCHECK", "INFO", "Checking for missing Files/Folders...")
logger.info("[FILESCHECK] Checking for missing Files/Folders...")
let missing = 0

if(!fs.existsSync(`${cfg_path}/files.json`)) { missing = 1 } else {
let Files = JSON.parse(fs.readFileSync(`${cfg_path}/files.json`))
Expand All @@ -13,15 +12,15 @@ const filescheck = async (path, cfg_path, logIt, timeout) => {
if(fs.existsSync(element)) {
} else {
missing += 1
logIt("FILESCHECK", "MISSING", `${element}`)
logger.error(`[FILESCHECK] MISSING ${element}`)
}
});
}

if(missing > 0) {
resolve(missing)
} else {
logIt("FILESCHECK", "INFO", "Everything looks okay!")
logger.info("[FILESCHECK] Everything looks okay!")
resolve(missing)
}
})
Expand Down
26 changes: 0 additions & 26 deletions src/bin/modules/script/logIt.js

This file was deleted.

89 changes: 89 additions & 0 deletions src/bin/modules/script/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const winston = require('winston');
const DailyRotateFile = require('winston-daily-rotate-file');
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
const moment = require('moment-timezone')
moment.tz.setDefault('Europe/Berlin').locale('id');

const debugMode = process.argv.includes("--debugging")

const logDir = debugMode ? `src/bin/logs` : 'logs'
const isDebugMode = process.argv.includes('--debug');

if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}

const generalLoggerFormat = winston.format.printf(({ timestamp, level, message }) => {
return `${timestamp} [${level}]: ${message}`;
});

// Definieren Sie die Logger ohne Transports zunächst
const generalLogger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp({
format: () => moment().format('DD-MM-YYYY HH:mm:ss')
}),
generalLoggerFormat
),
transports: []
});

// Funktion zum Einrichten der Transports für die Logger
function setupLoggerTransports(callback) {
const errorTransport = new DailyRotateFile({
filename: `${logDir}/index/error-%DATE%.log`,
datePattern: 'YYYY-MM-DD',
level: 'error',
handleExceptions: false
});

const debugTransport = new DailyRotateFile({
filename: `${logDir}/index/debug-%DATE%.log`,
datePattern: 'YYYY-MM-DD',
level: 'debug',
handleExceptions: false,
});

const infoTransport = new DailyRotateFile({
filename: `${logDir}/index/info-%DATE%.log`,
datePattern: 'YYYY-MM-DD',
level: 'info',
handleExceptions: false,
});

generalLogger.clear();

generalLogger.add(errorTransport);
generalLogger.add(debugTransport);
generalLogger.add(infoTransport);

if (isDebugMode) {
generalLogger.add(new winston.transports.Console({
level: 'debug',
format: winston.format.combine(
winston.format.colorize(),
generalLoggerFormat
)
}));
} else {
generalLogger.add(new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
generalLoggerFormat
)
}));
}

// Rufen Sie die Callback-Funktion auf, um anzuzeigen, dass die Transports eingerichtet sind
if (typeof callback === 'function') {
callback();
}
}

setupLoggerTransports();

module.exports = {
logger: generalLogger
};

0 comments on commit 1cda476

Please sign in to comment.