<commit_before>/*
 * Project Scelight
 * 
 * Copyright (c) 2013 <NAME> <<EMAIL>>
 * 
 * This software is the property of <NAME>.
 * Copying, modifying, distributing, refactoring without the author's permission
 * is prohibited and protected by Law.
 */
package hu.sllauncher.service.env;

import hu.sllauncher.LConsts;
import hu.sllauncher.ScelightLauncher;
import hu.sllauncher.bean.BuildInfoBean;
import hu.sllauncher.bean.settings.SettingsBean;
import hu.sllauncher.gui.LauncherFrame;
import hu.sllauncher.gui.comp.XFrame;
import hu.sllauncher.r.LR;
import hu.sllauncher.service.env.bootsettings.BootSettings;
import hu.sllauncher.service.env.bootsettings.WorkspaceLocationType;
import hu.sllauncher.service.job.Jobs;
import hu.sllauncher.service.lang.Language;
import hu.sllauncher.service.log.Logger;
import hu.sllauncher.service.registration.RegManager;
import hu.sllauncher.service.settings.LSettings;
import hu.sllauncher.util.LUtils;
import hu.sllauncher.util.SkillLevel;
import hu.sllauncher.util.gui.LGuiUtils;

import java.awt.Cursor;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.Socket;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.xml.bind.JAXB;

/**
 * Launcher environment.
 * 
 * @author <NAME>
 */
public class LEnv {
	
	/** {@link LUtils} implementation. */
	public static final AtomicReference< LUtils > UTILS_IMPL         = new AtomicReference<>( new LUtils() );
	
	
	/** UTF-8 charset used generally. */
	public static final Charset                   UTF8               = StandardCharsets.UTF_8;
	
	
	/** Application path. */
	public static final Path                      PATH_APP           = Paths.get( "mod" ).toAbsolutePath().getParent();
	
	
	/** Boot settings path. */
	public static final Path                      PATH_BOOT_SETTINGS = PATH_APP.resolve( "boot-settings.xml" );
	
	
	/** Boot settings bean. */
	public static final SettingsBean              BOOT_SETTINGS;
	static {
		// First backup settings
		if ( Files.exists( PATH_BOOT_SETTINGS ) )
			try {
				Files.copy( PATH_BOOT_SETTINGS, PATH_BOOT_SETTINGS.resolveSibling( PATH_BOOT_SETTINGS.getFileName() + "_" ),
				        StandardCopyOption.REPLACE_EXISTING );
			} catch ( final IOException ie ) {
				System.out.println( "Failed to backup Boot settings!" );
			}
		
		// Ensure the class listing the boot settings is loaded and initialized, so those settings will be found (by their full id).
		try {
			Class.forName( BootSettings.class.getName() );
		} catch ( final ClassNotFoundException cnfe ) {
			// Never to happen.
		}
		
		SettingsBean settings = null;
		try {
			settings = JAXB.unmarshal( PATH_BOOT_SETTINGS.toFile(), SettingsBean.class );
		} catch ( final Exception e ) {
			System.out.println( "Warning! Could not read Boot settings, the default settings will be used: " + PATH_BOOT_SETTINGS );
			settings = new SettingsBean();
		}
		BOOT_SETTINGS = settings;
		BOOT_SETTINGS.configureSave( LConsts.LAUNCHER_NAME, LConsts.LAUNCHER_VERSION, PATH_BOOT_SETTINGS );
		BOOT_SETTINGS.setValidSettingList( SettingsBean.createSettingListFromFields( BootSettings.class ) );
		
		// Save settings when logger is up (when saving fails, it uses the logger!)
	}
	
	
	/** Workspace location root path. */
	public static final Path                      PATH_WORKSPACE;
	
	/** Workspace meta data location root path (e.g. lock files). */
	public static final Path                      PATH_WORKSPACE_META;
	
	/** Workspace lock file to detect if workspace is already in use. */
	protected static final RandomAccessFile       WORKSPACE_LOCK;
	
	/** Instance Monitor info bean path. */
	public static final Path                      PATH_INSTANCE_MONITOR_INFO;
	
	static {
		Path PATH_WORKSPACE_ = null;
		Path PATH_WORKSPACE_META_ = null;
		RandomAccessFile WORKSPACE_LOCK_ = null;
		
		while ( WORKSPACE_LOCK_ == null ) {
			final WorkspaceLocationType wlt = BOOT_SETTINGS.get( BootSettings.WS_LOCATION_TYPE );
			switch ( wlt ) {
				case USER_HOME :
					String snapDataDir = System.getenv("SNAP_USER_DATA");
					Path configDirectory;
					if (snapDataDir != null) {
						configDirectory = Paths.get( snapDataDir );
					} else {
						configDirectory = Paths.get( System.getProperty( "user.home" ), ".Scelight/workspace" );
					}
					PATH_WORKSPACE_ = configDirectory;
					break;
				case APP_FOLDER :
					PATH_WORKSPACE_ = PATH_APP.resolve( "workspace" );
					break;
				case CUSTOM_FOLDER :
					PATH_WORKSPACE_ = BOOT_SETTINGS.get( BootSettings.WS_CUSTOM_FOLDER );
					break;
				default :<commit_msg>Change the workspace path on Linux into the standard user data path.<commit_after>/*
 * Project Scelight
 * 
 * Copyright (c) 2013 <NAME> <<EMAIL>>
 * 
 * This software is the property of <NAME>.
 * Copying, modifying, distributing, refactoring without the author's permission
 * is prohibited and protected by Law.
 */
package hu.sllauncher.service.env;

import hu.sllauncher.LConsts;
import hu.sllauncher.ScelightLauncher;
import hu.sllauncher.bean.BuildInfoBean;
import hu.sllauncher.bean.settings.SettingsBean;
import hu.sllauncher.gui.LauncherFrame;
import hu.sllauncher.gui.comp.XFrame;
import hu.sllauncher.r.LR;
import hu.sllauncher.service.env.bootsettings.BootSettings;
import hu.sllauncher.service.env.bootsettings.WorkspaceLocationType;
import hu.sllauncher.service.job.Jobs;
import hu.sllauncher.service.lang.Language;
import hu.sllauncher.service.log.Logger;
import hu.sllauncher.service.registration.RegManager;
import hu.sllauncher.service.settings.LSettings;
import hu.sllauncher.util.LUtils;
import hu.sllauncher.util.SkillLevel;
import hu.sllauncher.util.gui.LGuiUtils;

import java.awt.Cursor;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.Socket;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.xml.bind.JAXB;

/**
 * Launcher environment.
 * 
 * @author <NAME>
 */
public class LEnv {
	
	/** {@link LUtils} implementation. */
	public static final AtomicReference< LUtils > UTILS_IMPL         = new AtomicReference<>( new LUtils() );
	
	
	/** UTF-8 charset used generally. */
	public static final Charset                   UTF8               = StandardCharsets.UTF_8;
	
	
	/** Application path. */
	public static final Path                      PATH_APP           = Paths.get( "mod" ).toAbsolutePath().getParent();
	
	
	/** Boot settings path. */
	public static final Path                      PATH_BOOT_SETTINGS = PATH_APP.resolve( "boot-settings.xml" );
	
	
	/** Boot settings bean. */
	public static final SettingsBean              BOOT_SETTINGS;
	static {
		// First backup settings
		if ( Files.exists( PATH_BOOT_SETTINGS ) )
			try {
				Files.copy( PATH_BOOT_SETTINGS, PATH_BOOT_SETTINGS.resolveSibling( PATH_BOOT_SETTINGS.getFileName() + "_" ),
				        StandardCopyOption.REPLACE_EXISTING );
			} catch ( final IOException ie ) {
				System.out.println( "Failed to backup Boot settings!" );
			}
		
		// Ensure the class listing the boot settings is loaded and initialized, so those settings will be found (by their full id).
		try {
			Class.forName( BootSettings.class.getName() );
		} catch ( final ClassNotFoundException cnfe ) {
			// Never to happen.
		}
		
		SettingsBean settings = null;
		try {
			settings = JAXB.unmarshal( PATH_BOOT_SETTINGS.toFile(), SettingsBean.class );
		} catch ( final Exception e ) {
			System.out.println( "Warning! Could not read Boot settings, the default settings will be used: " + PATH_BOOT_SETTINGS );
			settings = new SettingsBean();
		}
		BOOT_SETTINGS = settings;
		BOOT_SETTINGS.configureSave( LConsts.LAUNCHER_NAME, LConsts.LAUNCHER_VERSION, PATH_BOOT_SETTINGS );
		BOOT_SETTINGS.setValidSettingList( SettingsBean.createSettingListFromFields( BootSettings.class ) );
		
		// Save settings when logger is up (when saving fails, it uses the logger!)
	}
	
	
	/** Workspace location root path. */
	public static final Path                      PATH_WORKSPACE;
	
	/** Workspace meta data location root path (e.g. lock files). */
	public static final Path                      PATH_WORKSPACE_META;
	
	/** Workspace lock file to detect if workspace is already in use. */
	protected static final RandomAccessFile       WORKSPACE_LOCK;
	
	/** Instance Monitor info bean path. */
	public static final Path                      PATH_INSTANCE_MONITOR_INFO;
	
	static {
		Path PATH_WORKSPACE_ = null;
		Path PATH_WORKSPACE_META_ = null;
		RandomAccessFile WORKSPACE_LOCK_ = null;
		
		while ( WORKSPACE_LOCK_ == null ) {
			final WorkspaceLocationType wlt = BOOT_SETTINGS.get( BootSettings.WS_LOCATION_TYPE );
			switch ( wlt ) {
				case USER_HOME :
					String snapDataDir = System.getenv( "SNAP_USER_DATA" );
					Path configPath = Paths.get( System.getProperty("user.home"), ".config" );
					if (snapDataDir != null) {
						PATH_WORKSPACE_ = Paths.get( snapDataDir );
					} else if (Files.exists(configPath)) {
						PATH_WORKSPACE_ = configPath.resolve("scelight");
					} else {
						PATH_WORKSPACE_ = Paths.get( System.getProperty( "user.home" ), ".Scelight/workspace" );
					}
					break;
				case APP_FOLDER :
					PATH_WORKSPACE_ = PATH_APP.resolve( "workspace" );
					break;
				case CUSTOM_FOLDER :
					PATH_WORKSPACE_ = BOOT_SETTINGS.get( BootSettings.WS_CUSTOM_FOLDER );
					break;
				default :
{'fraction_non_alphanumeric': 0.10037842951750237, 'fraction_numerical': 0.0013245033112582781, 'mean_word_length': 3.56236512731981, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3863766', 'n_tokens_mistral': 3664, 'n_tokens_neox': 3255, 'n_words': 786}