Sync
This commit is contained in:
68
modules/gameOptions.js
Normal file
68
modules/gameOptions.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const fs = require("node:fs")
|
||||
const path = require("node:path")
|
||||
let optionsFile
|
||||
|
||||
function initOptions($optionFile) {
|
||||
optionsFile = $optionFile
|
||||
const gameDir = path.parse($optionFile).dir
|
||||
if (!fs.existsSync(gameDir)) {
|
||||
try {
|
||||
fs.mkdirSync(gameDir)
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
if (!fs.existsSync($optionFile)) {
|
||||
try {
|
||||
fs.copyFileSync(path.join(__dirname, "..", "options.txt"), $optionFile)
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parseOptions() {
|
||||
const options = fs.readFileSync(optionsFile, "utf-8").toString().split("\n").filter(line => line.trim() !== "")
|
||||
const parsedOptions = {}
|
||||
for (let i = 0; i < options.length; i++) {
|
||||
const line = options[i].trim()
|
||||
const [key, value] = line.split(":")
|
||||
if (key && value) {
|
||||
const trimmedValue = value.trim().toLowerCase()
|
||||
if (trimmedValue === "true" || trimmedValue === "false") {
|
||||
parsedOptions[key.trim()] = trimmedValue === "true"
|
||||
} else {
|
||||
parsedOptions[key.trim()] = isNaN(value) ? value.trim() : parseInt(value, 10)
|
||||
}
|
||||
}
|
||||
}
|
||||
return parsedOptions
|
||||
}
|
||||
|
||||
function stringfyOptions(optionsObject) {
|
||||
const optionsArray = []
|
||||
if (typeof optionsObject !== "object" || optionsObject === null) {
|
||||
throw new Error("Invalid options object")
|
||||
}
|
||||
for (const settingKey in optionsObject) {
|
||||
if (Object.prototype.hasOwnProperty.call(optionsObject, settingKey)) {
|
||||
const settingValue = optionsObject[settingKey]
|
||||
if (typeof settingValue === "object" || typeof settingValue === "function" || typeof settingValue === "symbol") {
|
||||
throw new Error(`Invalid value for setting "${settingKey}": ${settingValue}`)
|
||||
}
|
||||
optionsArray.push(`${settingKey}:${settingValue}`)
|
||||
}
|
||||
}
|
||||
return optionsArray.join("\n")
|
||||
}
|
||||
|
||||
function saveOptions(options) {
|
||||
fs.writeFileSync(optionsFile, options, "utf8")
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
initOptions,
|
||||
saveOptions,
|
||||
parseOptions,
|
||||
stringfyOptions
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
const fs = require("node:fs")
|
||||
const path = require("node:path")
|
||||
|
||||
function parseSettings(settingsFile) {
|
||||
const settings = fs.readFileSync(settingsFile, "utf-8").toString().split("\n").filter(line => line.trim() !== "")
|
||||
const parseSettings = {}
|
||||
for (let i = 0; i < settings.length; i++) {
|
||||
const line = settings[i].trim()
|
||||
const [key, value] = line.split(":")
|
||||
if (key && value) {
|
||||
const trimmedValue = value.trim().toLowerCase()
|
||||
if (trimmedValue === "true" || trimmedValue === "false") {
|
||||
parseSettings[key.trim()] = trimmedValue === "true"
|
||||
} else {
|
||||
parseSettings[key.trim()] = isNaN(value) ? value.trim() : parseInt(value, 10)
|
||||
}
|
||||
}
|
||||
}
|
||||
return parseSettings
|
||||
}
|
||||
|
||||
function stringfySettings(settingsFile) {
|
||||
const settingsObject = JSON.parse(fs.readFileSync(settingsFile, "utf-8").toString())
|
||||
const settingsArray = []
|
||||
if (typeof settingsObject !== "object" || settingsObject === null) {
|
||||
throw new Error("Invalid settings object")
|
||||
}
|
||||
for (const settingKey in settingsObject) {
|
||||
if (Object.prototype.hasOwnProperty.call(settingsObject, settingKey)) {
|
||||
const settingValue = settingsObject[settingKey]
|
||||
if (typeof settingValue === "object" || typeof settingValue === "function" || typeof settingValue === "symbol") {
|
||||
throw new Error(`Invalid value for setting "${settingKey}": ${settingValue}`)
|
||||
}
|
||||
settingsArray.push(`${settingKey}:${settingValue}`)
|
||||
}
|
||||
}
|
||||
return settingsArray.join("\n")
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parseSettings,
|
||||
stringfySettings
|
||||
}
|
||||
116
modules/launcherSettings.js
Normal file
116
modules/launcherSettings.js
Normal file
@@ -0,0 +1,116 @@
|
||||
const fs = require("node:fs")
|
||||
const path = require("node:path")
|
||||
|
||||
let launcherDataPath = path.join(__dirname, ".catboat")
|
||||
|
||||
const baseSettings = {
|
||||
ram: {
|
||||
max: 1024
|
||||
}
|
||||
}
|
||||
|
||||
function initSettings($launcherDataPath) {
|
||||
launcherDataPath = $launcherDataPath
|
||||
const gameDir = path.parse($launcherDataPath).dir
|
||||
if (!fs.existsSync(gameDir)) {
|
||||
try {
|
||||
fs.mkdirSync(gameDir)
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
if (!fs.existsSync($launcherDataPath)) {
|
||||
try {
|
||||
fs.writeFileSync($launcherDataPath, JSON.stringify(baseSettings, null, 4))
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getLauncherDataPath() {
|
||||
return launcherDataPath
|
||||
}
|
||||
|
||||
function writeSettings(settings) {
|
||||
try {
|
||||
fs.writeFileSync(path.join(launcherDataPath, "launcher_settings.json"), JSON.stringify(settings, null, 4))
|
||||
return
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
function get(key) {
|
||||
try {
|
||||
const settings = JSON.parse(fs.readFileSync(path.join(launcherDataPath, "launcher_settings.json")))
|
||||
|
||||
const keys = key.split(".")
|
||||
let value = settings
|
||||
|
||||
for (const k of keys) {
|
||||
if (value[k] !== undefined) {
|
||||
value = value[k]
|
||||
} else {
|
||||
throw new Error(`La clé '${key}' n'existe pas.`)
|
||||
}
|
||||
}
|
||||
|
||||
return value
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de l'accès aux paramètres :", error.message)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
function set(key, newValue) {
|
||||
try {
|
||||
const filePath = path.join(launcherDataPath, "launcher_settings.json")
|
||||
const settings = JSON.parse(fs.readFileSync(filePath))
|
||||
|
||||
const keys = key.split(".")
|
||||
let obj = settings
|
||||
|
||||
for (let i = 0; i < keys.length - 1; i++) {
|
||||
const k = keys[i]
|
||||
if (!obj[k]) {
|
||||
obj[k] = {}
|
||||
}
|
||||
obj = obj[k]
|
||||
}
|
||||
|
||||
obj[keys[keys.length - 1]] = newValue
|
||||
|
||||
fs.writeFileSync(filePath, JSON.stringify(settings, null, 2))
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la mise à jour des paramètres :", error.message)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
function clean() {
|
||||
try {
|
||||
fs.writeFileSync(path.join(launcherDataPath, "launcher_settings.json"), JSON.stringify(baseSettings, null, 4))
|
||||
return
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
function readSettings() {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(path.join(launcherDataPath, "launcher_settings.json")))
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getLauncherDataPath,
|
||||
writeSettings,
|
||||
readSettings,
|
||||
initSettings,
|
||||
clean,
|
||||
get,
|
||||
set,
|
||||
}
|
||||
Reference in New Issue
Block a user