Refactoring: Settings.

This commit is contained in:
marley 2023-05-17 18:30:39 -07:00
parent 5f374f0657
commit de2819eab3

View file

@ -1,5 +1,5 @@
import { EliteMatrix } from 'elite-matrix';
import {PathLike} from 'fs';
import type { PathLike } from 'fs';
const EventEmitter = require('node:events');
// Jest can't parse 'node:fs' so this has to be 'fs' for testing.
@ -127,15 +127,21 @@ export class Settings extends EventEmitter {
/* -------------------------------------------------------------------------- #setMatrix ---- */
async #setMatrix(): Promise<void> {
const file: string = await fs.readFile((
this.#matrixFile as PathLike
), {encoding: 'utf8'});
let matrixRed: [number, number, number];
let matrixGreen: [number, number, number];
let matrixBlue: [number, number, number];
const file: string = await fs.readFile((this.#matrixFile as PathLike), { encoding: 'utf8' });
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 = {
trim: true,
ignoreDeclaration: true,
@ -153,24 +159,22 @@ export class Settings extends EventEmitter {
matrix = matrix.map(v => v.replace(/\s/g, '').split(','));
matrixRed = matrix[0].length === 3 ? matrix[0] : [1, 0, 0];
matrixGreen = matrix[1].length === 3 ? matrix[1] : [0, 1, 0];
matrixBlue = matrix[2].length === 3 ? matrix[2] : [0, 0, 1];
const matrixRed: [number, number, number] = matrix[0].length === 3 ? matrix[0] : [1, 0, 0];
const matrixGreen: [number, number, number] = matrix[1].length === 3 ? matrix[1] : [0, 1, 0];
const matrixBlue: [number, number, number] = matrix[2].length === 3 ? matrix[2] : [0, 0, 1];
this.matrix = 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);
return 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);
}
}