mirror of
https://git.sanhost.net/sanasol/hytale-f2p.git
synced 2026-02-26 06:41:47 -03:00
Added a full profile system and fixed a few critical mod loading issues. What changed Profiles — Implemented proper profile management (create, switch, delete). Each profile now has its own isolated mod list. Mod Isolation — Fixed ModManager so mods are strictly scoped to the active profile. Browsing and installing only affects the selected profile. Critical Fix — Fixed a path bug where mods were being saved to ~/AppData/Local on macOS (Windows path) instead of ~/Library/Application Support. Mods now save to the correct location and load correctly in-game. Stability — Added an auto-sync step before every launch to make sure the physical mods folder always matches the active profile. UI — Added a profile selector dropdown and a profile management modal.
317 lines
7.6 KiB
JavaScript
317 lines
7.6 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const os = require('os');
|
|
|
|
|
|
// Default auth domain - can be overridden by env var or config
|
|
const DEFAULT_AUTH_DOMAIN = 'sanasol.ws';
|
|
|
|
// Get auth domain from env, config, or default
|
|
function getAuthDomain() {
|
|
// First check environment variable
|
|
if (process.env.HYTALE_AUTH_DOMAIN) {
|
|
return process.env.HYTALE_AUTH_DOMAIN;
|
|
}
|
|
// Then check config file
|
|
const config = loadConfig();
|
|
if (config.activeProfileId && config.profiles && config.profiles[config.activeProfileId]) {
|
|
// Allow profile to override auth domain if ever needed
|
|
// but for now stick to global or env
|
|
}
|
|
if (config.authDomain) {
|
|
return config.authDomain;
|
|
}
|
|
// Fall back to default
|
|
return DEFAULT_AUTH_DOMAIN;
|
|
}
|
|
|
|
// Get full auth server URL
|
|
function getAuthServerUrl() {
|
|
const domain = getAuthDomain();
|
|
return `https://sessions.${domain}`;
|
|
}
|
|
|
|
// Save auth domain to config
|
|
function saveAuthDomain(domain) {
|
|
saveConfig({ authDomain: domain || DEFAULT_AUTH_DOMAIN });
|
|
}
|
|
|
|
function getAppDir() {
|
|
const home = os.homedir();
|
|
if (process.platform === 'win32') {
|
|
return path.join(home, 'AppData', 'Local', 'HytaleF2P');
|
|
} else if (process.platform === 'darwin') {
|
|
return path.join(home, 'Library', 'Application Support', 'HytaleF2P');
|
|
} else {
|
|
return path.join(home, '.hytalef2p');
|
|
}
|
|
}
|
|
|
|
const CONFIG_FILE = path.join(getAppDir(), 'config.json');
|
|
|
|
function loadConfig() {
|
|
try {
|
|
if (fs.existsSync(CONFIG_FILE)) {
|
|
return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
|
|
}
|
|
} catch (err) {
|
|
console.log('Notice: could not load config:', err.message);
|
|
}
|
|
return {};
|
|
}
|
|
|
|
function saveConfig(update) {
|
|
try {
|
|
const configDir = path.dirname(CONFIG_FILE);
|
|
if (!fs.existsSync(configDir)) {
|
|
fs.mkdirSync(configDir, { recursive: true });
|
|
}
|
|
const config = loadConfig();
|
|
const next = { ...config, ...update };
|
|
fs.writeFileSync(CONFIG_FILE, JSON.stringify(next, null, 2), 'utf8');
|
|
} catch (err) {
|
|
console.log('Notice: could not save config:', err.message);
|
|
}
|
|
}
|
|
|
|
function saveUsername(username) {
|
|
saveConfig({ username: username || 'Player' });
|
|
}
|
|
|
|
function loadUsername() {
|
|
const config = loadConfig();
|
|
return config.username || 'Player';
|
|
}
|
|
|
|
function saveChatUsername(chatUsername) {
|
|
saveConfig({ chatUsername: chatUsername || '' });
|
|
}
|
|
|
|
function loadChatUsername() {
|
|
const config = loadConfig();
|
|
return config.chatUsername || '';
|
|
}
|
|
|
|
function getUuidForUser(username) {
|
|
const { v4: uuidv4 } = require('uuid');
|
|
const config = loadConfig();
|
|
const userUuids = config.userUuids || {};
|
|
|
|
if (userUuids[username]) {
|
|
return userUuids[username];
|
|
}
|
|
|
|
const newUuid = uuidv4();
|
|
userUuids[username] = newUuid;
|
|
saveConfig({ userUuids });
|
|
|
|
return newUuid;
|
|
}
|
|
|
|
function saveJavaPath(javaPath) {
|
|
const trimmed = (javaPath || '').trim();
|
|
saveConfig({ javaPath: trimmed });
|
|
}
|
|
|
|
function loadJavaPath() {
|
|
const config = loadConfig();
|
|
|
|
// Prefer Active Profile's Java Path
|
|
if (config.activeProfileId && config.profiles && config.profiles[config.activeProfileId]) {
|
|
const profile = config.profiles[config.activeProfileId];
|
|
if (profile.javaPath && profile.javaPath.trim().length > 0) {
|
|
return profile.javaPath;
|
|
}
|
|
}
|
|
|
|
// Fallback to global setting
|
|
return config.javaPath || '';
|
|
}
|
|
|
|
function saveInstallPath(installPath) {
|
|
const trimmed = (installPath || '').trim();
|
|
saveConfig({ installPath: trimmed });
|
|
}
|
|
|
|
function loadInstallPath() {
|
|
const config = loadConfig();
|
|
return config.installPath || '';
|
|
}
|
|
|
|
function saveDiscordRPC(enabled) {
|
|
saveConfig({ discordRPC: !!enabled });
|
|
}
|
|
|
|
function loadDiscordRPC() {
|
|
const config = loadConfig();
|
|
return config.discordRPC !== undefined ? config.discordRPC : true;
|
|
}
|
|
|
|
function saveModsToConfig(mods) {
|
|
try {
|
|
const config = loadConfig();
|
|
|
|
// Config migration to profiles handles the structure,
|
|
// but here we ensure that if we are saving mods, we save them to the ACTIVE profile
|
|
// OR we save to the global pool if that's the design.
|
|
// Based on the plan: Profile has "Enabled Mods".
|
|
// This function seems to be used to save the list of *Installed* mods.
|
|
|
|
// We'll update the global config for "installedMods" mostly for reference/migration,
|
|
// but primarily we should be updating the active profile's mod list.
|
|
|
|
if (config.activeProfileId && config.profiles && config.profiles[config.activeProfileId]) {
|
|
config.profiles[config.activeProfileId].mods = mods;
|
|
} else {
|
|
// Fallback for legacy or no-profile state
|
|
config.installedMods = mods;
|
|
}
|
|
|
|
const configDir = path.dirname(CONFIG_FILE);
|
|
if (!fs.existsSync(configDir)) {
|
|
fs.mkdirSync(configDir, { recursive: true });
|
|
}
|
|
|
|
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
|
|
console.log('Mods saved to config.json');
|
|
} catch (error) {
|
|
console.error('Error saving mods to config:', error);
|
|
}
|
|
}
|
|
|
|
function loadModsFromConfig() {
|
|
try {
|
|
const config = loadConfig();
|
|
|
|
// Prefer Active Profile
|
|
if (config.activeProfileId && config.profiles && config.profiles[config.activeProfileId]) {
|
|
return config.profiles[config.activeProfileId].mods || [];
|
|
}
|
|
|
|
return config.installedMods || [];
|
|
} catch (error) {
|
|
console.error('Error loading mods from config:', error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function isFirstLaunch() {
|
|
const config = loadConfig();
|
|
|
|
if ('hasLaunchedBefore' in config) {
|
|
return !config.hasLaunchedBefore;
|
|
}
|
|
|
|
const hasUserData = config.installPath || config.username || config.javaPath ||
|
|
config.chatUsername || config.userUuids ||
|
|
Object.keys(config).length > 0;
|
|
|
|
if (!hasUserData) {
|
|
return true;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
function markAsLaunched() {
|
|
saveConfig({ hasLaunchedBefore: true, firstLaunchDate: new Date().toISOString() });
|
|
}
|
|
|
|
// UUID Management Functions
|
|
function getCurrentUuid() {
|
|
const username = loadUsername();
|
|
return getUuidForUser(username);
|
|
}
|
|
|
|
function getAllUuidMappings() {
|
|
const config = loadConfig();
|
|
return config.userUuids || {};
|
|
}
|
|
|
|
function setUuidForUser(username, uuid) {
|
|
const { v4: uuidv4, validate: validateUuid } = require('uuid');
|
|
|
|
// Validate UUID format
|
|
if (!validateUuid(uuid)) {
|
|
throw new Error('Invalid UUID format');
|
|
}
|
|
|
|
const config = loadConfig();
|
|
const userUuids = config.userUuids || {};
|
|
userUuids[username] = uuid;
|
|
saveConfig({ userUuids });
|
|
|
|
return uuid;
|
|
}
|
|
|
|
function generateNewUuid() {
|
|
const { v4: uuidv4 } = require('uuid');
|
|
return uuidv4();
|
|
}
|
|
|
|
function deleteUuidForUser(username) {
|
|
const config = loadConfig();
|
|
const userUuids = config.userUuids || {};
|
|
|
|
if (userUuids[username]) {
|
|
delete userUuids[username];
|
|
saveConfig({ userUuids });
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function resetCurrentUserUuid() {
|
|
const username = loadUsername();
|
|
const { v4: uuidv4 } = require('uuid');
|
|
const newUuid = uuidv4();
|
|
|
|
return setUuidForUser(username, newUuid);
|
|
}
|
|
|
|
function saveChatColor(color) {
|
|
const config = loadConfig();
|
|
config.chatColor = color;
|
|
saveConfig(config);
|
|
}
|
|
|
|
function loadChatColor() {
|
|
const config = loadConfig();
|
|
return config.chatColor || '#3498db';
|
|
}
|
|
|
|
module.exports = {
|
|
loadConfig,
|
|
saveConfig,
|
|
saveUsername,
|
|
loadUsername,
|
|
saveChatUsername,
|
|
loadChatUsername,
|
|
saveChatColor,
|
|
loadChatColor,
|
|
getUuidForUser,
|
|
saveJavaPath,
|
|
loadJavaPath,
|
|
saveInstallPath,
|
|
loadInstallPath,
|
|
saveDiscordRPC,
|
|
loadDiscordRPC,
|
|
saveModsToConfig,
|
|
loadModsFromConfig,
|
|
isFirstLaunch,
|
|
markAsLaunched,
|
|
CONFIG_FILE,
|
|
// Auth server exports
|
|
getAuthServerUrl,
|
|
getAuthDomain,
|
|
saveAuthDomain,
|
|
// UUID Management exports
|
|
getCurrentUuid,
|
|
getAllUuidMappings,
|
|
setUuidForUser,
|
|
generateNewUuid,
|
|
deleteUuidForUser,
|
|
resetCurrentUserUuid
|
|
};
|