Refactoring: Settings.
This commit is contained in:
parent
5f374f0657
commit
de2819eab3
1 changed files with 60 additions and 56 deletions
|
@ -1,16 +1,16 @@
|
||||||
import {EliteMatrix} from 'elite-matrix';
|
import { EliteMatrix } from 'elite-matrix';
|
||||||
import {PathLike} from 'fs';
|
import type { PathLike } from 'fs';
|
||||||
|
|
||||||
const EventEmitter = require('node:events');
|
const EventEmitter = require('node:events');
|
||||||
// Jest can't parse 'node:fs' so this has to be 'fs' for testing.
|
// Jest can't parse 'node:fs' so this has to be 'fs' for testing.
|
||||||
const fs = require('fs/promises');
|
const fs = require('fs/promises');
|
||||||
const {statSync, writeFileSync, readFileSync} = require('fs');
|
const { statSync, writeFileSync, readFileSync } = require('fs');
|
||||||
const ini = require('ini');
|
const ini = require('ini');
|
||||||
const os = require('node:os');
|
const os = require('node:os');
|
||||||
const path = require('node:path');
|
const path = require('node:path');
|
||||||
const xmlJS = require('xml-js');
|
const xmlJS = require('xml-js');
|
||||||
|
|
||||||
import {Log} from './Log';
|
import { Log } from './Log';
|
||||||
|
|
||||||
interface settingsFile {
|
interface settingsFile {
|
||||||
minValue: number,
|
minValue: number,
|
||||||
|
@ -19,14 +19,14 @@ interface settingsFile {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Settings extends EventEmitter {
|
export class Settings extends EventEmitter {
|
||||||
static #instance: Settings | undefined;
|
static #instance: Settings|undefined;
|
||||||
|
|
||||||
readonly #file: string;
|
readonly #file: string;
|
||||||
|
|
||||||
minValue: number;
|
minValue: number;
|
||||||
maxDistance: number;
|
maxDistance: number;
|
||||||
|
|
||||||
#matrixFile: null | string;
|
#matrixFile: null|string;
|
||||||
matrix?: EliteMatrix;
|
matrix?: EliteMatrix;
|
||||||
|
|
||||||
private constructor(isPackaged: boolean) {
|
private constructor(isPackaged: boolean) {
|
||||||
|
@ -55,7 +55,7 @@ export class Settings extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initial reading of settings file done in sync for same reasons as above.
|
// Initial reading of settings file done in sync for same reasons as above.
|
||||||
const contents: settingsFile = JSON.parse(readFileSync(this.#file, {encoding: 'utf8'}));
|
const contents: settingsFile = JSON.parse(readFileSync(this.#file, { encoding: 'utf8' }));
|
||||||
this.minValue = contents.minValue;
|
this.minValue = contents.minValue;
|
||||||
this.maxDistance = contents.maxDistance;
|
this.maxDistance = contents.maxDistance;
|
||||||
this.#matrixFile = contents.matrixFile;
|
this.#matrixFile = contents.matrixFile;
|
||||||
|
@ -105,7 +105,7 @@ export class Settings extends EventEmitter {
|
||||||
|
|
||||||
async #read(): Promise<boolean> {
|
async #read(): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
const file: string = await fs.readFile(this.#file, {encoding: 'utf8'});
|
const file: string = await fs.readFile(this.#file, { encoding: 'utf8' });
|
||||||
const contents: settingsFile = JSON.parse(file);
|
const contents: settingsFile = JSON.parse(file);
|
||||||
|
|
||||||
this.minValue = contents.minValue;
|
this.minValue = contents.minValue;
|
||||||
|
@ -127,15 +127,21 @@ export class Settings extends EventEmitter {
|
||||||
/* -------------------------------------------------------------------------- #setMatrix ---- */
|
/* -------------------------------------------------------------------------- #setMatrix ---- */
|
||||||
|
|
||||||
async #setMatrix(): Promise<void> {
|
async #setMatrix(): Promise<void> {
|
||||||
const file: string = await fs.readFile((
|
const file: string = await fs.readFile((this.#matrixFile as PathLike), { encoding: 'utf8' });
|
||||||
this.#matrixFile as PathLike
|
|
||||||
), {encoding: 'utf8'});
|
|
||||||
|
|
||||||
let matrixRed: [number, number, number];
|
|
||||||
let matrixGreen: [number, number, number];
|
|
||||||
let matrixBlue: [number, number, number];
|
|
||||||
|
|
||||||
if (this.#matrixFile && path.basename(this.#matrixFile) === 'GraphicsConfiguration.xml') {
|
if (this.#matrixFile && path.basename(this.#matrixFile) === 'GraphicsConfiguration.xml') {
|
||||||
|
this.matrix = await this.#getMatrixFromXml(file);
|
||||||
|
|
||||||
|
} else if (this.#matrixFile && path.basename(this.#matrixFile) === 'XML-Profile.ini') {
|
||||||
|
this.matrix = await this.#getMatrixFromIni(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.emit('CUSTOM_COLORS_SET');
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- #getMatrixFromXml ---- */
|
||||||
|
|
||||||
|
async #getMatrixFromXml(file: string): Promise<EliteMatrix> {
|
||||||
const options = {
|
const options = {
|
||||||
trim: true,
|
trim: true,
|
||||||
ignoreDeclaration: true,
|
ignoreDeclaration: true,
|
||||||
|
@ -153,24 +159,22 @@ export class Settings extends EventEmitter {
|
||||||
|
|
||||||
matrix = matrix.map(v => v.replace(/\s/g, '').split(','));
|
matrix = matrix.map(v => v.replace(/\s/g, '').split(','));
|
||||||
|
|
||||||
matrixRed = matrix[0].length === 3 ? matrix[0] : [1, 0, 0];
|
const matrixRed: [number, number, number] = matrix[0].length === 3 ? matrix[0] : [1, 0, 0];
|
||||||
matrixGreen = matrix[1].length === 3 ? matrix[1] : [0, 1, 0];
|
const matrixGreen: [number, number, number] = matrix[1].length === 3 ? matrix[1] : [0, 1, 0];
|
||||||
matrixBlue = matrix[2].length === 3 ? matrix[2] : [0, 0, 1];
|
const matrixBlue: [number, number, number] = matrix[2].length === 3 ? matrix[2] : [0, 0, 1];
|
||||||
|
|
||||||
this.matrix = new EliteMatrix(matrixRed, matrixGreen, matrixBlue);
|
return new EliteMatrix(matrixRed, matrixGreen, matrixBlue);
|
||||||
|
|
||||||
} else if (this.#matrixFile && path.basename(this.#matrixFile) === 'XML-Profile.ini') {
|
|
||||||
const contents = (
|
|
||||||
ini.parse(file)
|
|
||||||
).constants;
|
|
||||||
|
|
||||||
matrixRed = [contents.x150, contents.y150, contents.z150];
|
|
||||||
matrixGreen = [contents.x151, contents.y151, contents.z151];
|
|
||||||
matrixBlue = [contents.x152, contents.y152, contents.z152];
|
|
||||||
|
|
||||||
this.matrix = new EliteMatrix(matrixRed, matrixGreen, matrixBlue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('CUSTOM_COLORS_SET');
|
/* --------------------------------------------------------------------- #getMatrixFromIni ---- */
|
||||||
|
|
||||||
|
async #getMatrixFromIni(file: string): Promise<EliteMatrix> {
|
||||||
|
const contents = (ini.parse(file)).constants;
|
||||||
|
|
||||||
|
const matrixRed: [number, number, number] = [contents.x150, contents.y150, contents.z150];
|
||||||
|
const matrixGreen: [number, number, number] = [contents.x151, contents.y151, contents.z151];
|
||||||
|
const matrixBlue: [number, number, number] = [contents.x152, contents.y152, contents.z152];
|
||||||
|
|
||||||
|
return new EliteMatrix(matrixRed, matrixGreen, matrixBlue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue