mirror of https://github.com/GooseMod/OpenAsar
[AppSettings] Integrate Settings util within, make settings global to avoid unneeded reuse
parent
7e9ede7fc8
commit
dc908c54cc
@ -1,7 +1,47 @@
|
||||
let settings;
|
||||
const fs = require('fs');
|
||||
|
||||
exports.getSettings = () => {
|
||||
if (!settings) settings = new (require('./utils/Settings'))(require('./paths').getUserData());
|
||||
return settings;
|
||||
};
|
||||
exports.init = () => {};
|
||||
class Settings { // Heavily based on original for compat, but simplified and tweaked
|
||||
constructor(path) {
|
||||
this.path = path;
|
||||
|
||||
try {
|
||||
this.store = JSON.parse(fs.readFileSync(this.path));
|
||||
} catch (e) {
|
||||
this.store = {};
|
||||
}
|
||||
|
||||
this.mod = this.getMod();
|
||||
|
||||
log('AppSettings', this.path, this.store);
|
||||
}
|
||||
|
||||
getMod() { // Get when file was last modified
|
||||
try {
|
||||
return fs.statSync(this.path).mtime.getTime();
|
||||
} catch { }
|
||||
}
|
||||
|
||||
get(key, defaultValue) {
|
||||
return this.store[key] ?? defaultValue;
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
this.store[key] = value;
|
||||
}
|
||||
|
||||
save() {
|
||||
if (this.mod && this.mod !== this.getMod()) return; // File was last modified after Settings was made, so was externally edited therefore we don't save over
|
||||
|
||||
try {
|
||||
fs.writeFileSync(this.path, JSON.stringify(this.store, null, 2));
|
||||
this.mod = this.getMod();
|
||||
|
||||
log('AppSettings', 'Saved');
|
||||
} catch (e) {
|
||||
log('AppSettings', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let inst; // Instance of class
|
||||
exports.getSettings = () => inst = inst ?? new Settings(require('path').join(require('./paths').getUserData(), 'settings.json'));
|
@ -1,49 +0,0 @@
|
||||
const fs = require('fs');
|
||||
const { join } = require('path');
|
||||
|
||||
module.exports = class Settings { // Heavily based on original for compat, but simplified and tweaked
|
||||
constructor(root) {
|
||||
this.path = join(root, 'settings.json');
|
||||
|
||||
try {
|
||||
this.settings = JSON.parse(fs.readFileSync(this.path));
|
||||
} catch (e) {
|
||||
this.settings = {};
|
||||
}
|
||||
|
||||
this.mod = this.getMod();
|
||||
|
||||
log('AppSettings', this.path, this.settings);
|
||||
}
|
||||
|
||||
getMod() { // Get when file was last modified
|
||||
try {
|
||||
return fs.statSync(this.path).mtime.getTime();
|
||||
} catch (e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
get(key, defaultValue) {
|
||||
return this.settings[key] ?? defaultValue;
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
this.settings[key] = value;
|
||||
}
|
||||
|
||||
save() {
|
||||
if (this.mod && this.mod !== this.getMod()) return; // File was last modified after Settings was made, so was externally edited therefore we don't save over
|
||||
|
||||
try {
|
||||
const str = JSON.stringify(this.settings, null, 2);
|
||||
|
||||
fs.writeFileSync(this.path, str);
|
||||
this.mod = this.getMod();
|
||||
|
||||
log('AppSettings', 'Saved');
|
||||
} catch (e) {
|
||||
log('AppSettings', e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue