chore: replace json config with database
This commit is contained in:
1 parent
2ba094f07e
commit
20e8389409
16 files changed
+761
-226
No files matched your search
@@ -1,6 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { config } from '$lib/config';
|
||||
import { custom_theme_audio_playing, custom_theme_volume } from '$lib/global';
|
||||
import { reduceAnimations, userSettings } from '$lib/userSettings';
|
||||
import { reduceAnimations } from '$lib/userSettings';
|
||||
import { Volume, Volume1, VolumeOff } from '@lucide/svelte';
|
||||
import Minimize from '@lucide/svelte/icons/minus';
|
||||
import Volume2 from '@lucide/svelte/icons/volume-2';
|
||||
@@ -62,8 +63,7 @@
|
||||
step="0.01"
|
||||
bind:value={$custom_theme_volume}
|
||||
on:input={() => {
|
||||
$userSettings.value('volume').set($custom_theme_volume);
|
||||
$userSettings.save();
|
||||
config.value<number>('volume').set($custom_theme_volume);
|
||||
}}
|
||||
class="volume-slider"
|
||||
/>
|
||||
|
||||
+31
-79
@@ -1,87 +1,39 @@
|
||||
import { Crypto } from './crypto';
|
||||
import { getHWID } from './osuUtil';
|
||||
import * as path from '@tauri-apps/api/path';
|
||||
import { exists, mkdir, readTextFile, writeFile } from '@tauri-apps/plugin-fs';
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
|
||||
export class Config {
|
||||
private fileName: string;
|
||||
private config: Record<string, unknown> = {};
|
||||
private crypto: Crypto | undefined;
|
||||
private configFilePath: string | undefined;
|
||||
private encrypt: boolean;
|
||||
interface ConfigValue<T> {
|
||||
set(value: T, opts?: { encrypt?: boolean }): Promise<void>;
|
||||
get(fallback: T): Promise<T>;
|
||||
exists(): Promise<boolean>;
|
||||
delete(): Promise<void>;
|
||||
}
|
||||
|
||||
constructor(fileName: string, encrypt?: boolean) {
|
||||
this.fileName = fileName;
|
||||
this.encrypt = encrypt ?? false;
|
||||
}
|
||||
|
||||
async init(): Promise<boolean> {
|
||||
const hwid = (await getHWID()) ?? 'recorderinsandybridge';
|
||||
|
||||
this.crypto = new Crypto(hwid);
|
||||
|
||||
const homeDir = await path.homeDir();
|
||||
const folderPath = await path.join(homeDir, '.ezpplauncher');
|
||||
this.configFilePath = await path.join(folderPath, this.fileName);
|
||||
|
||||
const createFolder = !(await exists(folderPath));
|
||||
if (createFolder) await mkdir(folderPath);
|
||||
|
||||
const configExists = await exists(this.configFilePath);
|
||||
if (!configExists) return true;
|
||||
|
||||
await this.load();
|
||||
return false;
|
||||
}
|
||||
|
||||
private async load() {
|
||||
if (!this.configFilePath) throw Error('configFilePath not set');
|
||||
if (this.encrypt && !this.crypto) throw Error('crypto not initialized');
|
||||
|
||||
const fileStream = await readTextFile(this.configFilePath);
|
||||
try {
|
||||
const decryptedJSON = JSON.parse(
|
||||
this.encrypt && this.crypto ? this.crypto.decrypt(fileStream) : fileStream
|
||||
) as Record<string, unknown>;
|
||||
this.config = decryptedJSON;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
this.config = {};
|
||||
await this.save();
|
||||
}
|
||||
}
|
||||
|
||||
async save() {
|
||||
if (!this.configFilePath) throw Error('configFilePath not set');
|
||||
if (this.encrypt && !this.crypto) throw Error('crypto not initialized');
|
||||
const encryptedJSON =
|
||||
this.encrypt && this.crypto
|
||||
? this.crypto.encrypt(JSON.stringify(this.config))
|
||||
: JSON.stringify(this.config);
|
||||
|
||||
await writeFile(this.configFilePath, Buffer.from(encryptedJSON), {
|
||||
append: false,
|
||||
});
|
||||
}
|
||||
|
||||
values() {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
value(key: string) {
|
||||
export const config = {
|
||||
value<T = unknown>(key: string): ConfigValue<T> {
|
||||
return {
|
||||
set: <T>(val: T) => {
|
||||
this.config[key] = val;
|
||||
set(value: T, opts?: { encrypt?: boolean }): Promise<void> {
|
||||
return invoke('config_set', { key, value, encrypt: opts?.encrypt ?? false });
|
||||
},
|
||||
exists: () => {
|
||||
return this.config[key] !== undefined;
|
||||
|
||||
async get(fallback: T): Promise<T> {
|
||||
const result = await invoke<T | null>('config_get', { key });
|
||||
return result ?? fallback;
|
||||
},
|
||||
get: <T>(fallback: T): T => {
|
||||
return (this.config[key] as T) ?? fallback;
|
||||
|
||||
exists(): Promise<boolean> {
|
||||
return invoke<boolean>('config_exists', { key });
|
||||
},
|
||||
del: () => {
|
||||
delete this.config[key];
|
||||
|
||||
delete(): Promise<void> {
|
||||
return invoke('config_delete', { key });
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
all(): Promise<[string, unknown][]> {
|
||||
return invoke<[string, unknown][]>('config_all');
|
||||
},
|
||||
|
||||
clear(): Promise<void> {
|
||||
return invoke('config_clear');
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,94 @@
|
||||
import { Crypto } from './crypto';
|
||||
import { getHWID } from './osuUtil';
|
||||
import * as path from '@tauri-apps/api/path';
|
||||
import { exists, mkdir, readTextFile, writeFile, remove } from '@tauri-apps/plugin-fs';
|
||||
|
||||
export class Config {
|
||||
private fileName: string;
|
||||
private config: Record<string, unknown> = {};
|
||||
private crypto: Crypto | undefined;
|
||||
private configFilePath: string | undefined;
|
||||
private encrypt: boolean;
|
||||
|
||||
constructor(fileName: string, encrypt?: boolean) {
|
||||
this.fileName = fileName;
|
||||
this.encrypt = encrypt ?? false;
|
||||
}
|
||||
|
||||
async init(): Promise<boolean> {
|
||||
const hwid = (await getHWID()) ?? 'recorderinsandybridge';
|
||||
|
||||
this.crypto = new Crypto(hwid);
|
||||
|
||||
const homeDir = await path.homeDir();
|
||||
const folderPath = await path.join(homeDir, '.ezpplauncher');
|
||||
this.configFilePath = await path.join(folderPath, this.fileName);
|
||||
|
||||
const createFolder = !(await exists(folderPath));
|
||||
if (createFolder) await mkdir(folderPath);
|
||||
|
||||
const configExists = await exists(this.configFilePath);
|
||||
if (configExists) {
|
||||
await this.load();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private async load() {
|
||||
if (!this.configFilePath) throw Error('configFilePath not set');
|
||||
if (this.encrypt && !this.crypto) throw Error('crypto not initialized');
|
||||
|
||||
const fileStream = await readTextFile(this.configFilePath);
|
||||
try {
|
||||
const decryptedJSON = JSON.parse(
|
||||
this.encrypt && this.crypto ? this.crypto.decrypt(fileStream) : fileStream
|
||||
) as Record<string, unknown>;
|
||||
this.config = decryptedJSON;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
this.config = {};
|
||||
await this.save();
|
||||
}
|
||||
}
|
||||
|
||||
async delete() {
|
||||
if (!this.configFilePath) throw Error('configFilePath not set');
|
||||
await remove(this.configFilePath);
|
||||
}
|
||||
|
||||
async save() {
|
||||
if (!this.configFilePath) throw Error('configFilePath not set');
|
||||
if (this.encrypt && !this.crypto) throw Error('crypto not initialized');
|
||||
const encryptedJSON =
|
||||
this.encrypt && this.crypto
|
||||
? this.crypto.encrypt(JSON.stringify(this.config))
|
||||
: JSON.stringify(this.config);
|
||||
|
||||
await writeFile(this.configFilePath, Buffer.from(encryptedJSON), {
|
||||
append: false,
|
||||
});
|
||||
}
|
||||
|
||||
values() {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
value(key: string) {
|
||||
return {
|
||||
set: <T>(val: T) => {
|
||||
this.config[key] = val;
|
||||
},
|
||||
exists: () => {
|
||||
return this.config[key] !== undefined;
|
||||
},
|
||||
get: <T>(fallback: T): T => {
|
||||
return (this.config[key] as T) ?? fallback;
|
||||
},
|
||||
del: () => {
|
||||
delete this.config[key];
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -262,7 +262,6 @@ export const loadTheme = async (theme: Theme, themeContainer: HTMLElement, volum
|
||||
};
|
||||
|
||||
export const deleteTheme = async (themeToUninstall: Theme) => {
|
||||
console.log('Uninstall', themeToUninstall);
|
||||
if (themeToUninstall.status !== 'installed') {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { Config } from './config';
|
||||
import type { EZPPUser } from './types';
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
export const userAuth = writable<Config>(new Config('user_auth', true));
|
||||
export const currentUser = writable<EZPPUser | undefined>(undefined);
|
||||
@@ -1,8 +1,5 @@
|
||||
import { Config } from './config';
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
export const userSettings = writable<Config>(new Config('user_settings', false));
|
||||
|
||||
export const customCursor = writable<boolean>(false);
|
||||
export const cursorSmoothening = writable<boolean>(false);
|
||||
export const cursorSmoothness = writable<number>(180);
|
||||
|
||||
Reference in new issue
Block a user