Better logging intialization. Separate class for providing app directory.

This commit is contained in:
Jindra Petřík
2025-07-27 19:57:20 +02:00
parent 0fa8701cd9
commit 84185ce4bb
6 changed files with 176 additions and 128 deletions

View File

@@ -0,0 +1,131 @@
/*
* Copyright (C) 2010-2025 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.configuration;
import com.jpexs.decompiler.flash.ApplicationInfo;
import java.io.File;
import java.util.Locale;
/**
* App directory provider
* @author JPEXS
*/
public class AppDirectoryProvider {
private static final File UNSPECIFIED_FILE = new File("unspecified");
private static File directory = UNSPECIFIED_FILE;
private enum OSId {
WINDOWS, OSX, UNIX
}
private static OSId getOSId() {
String OS = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
if ((OS.indexOf("mac") >= 0) || (OS.indexOf("darwin") >= 0)) {
return OSId.OSX;
} else if (OS.indexOf("win") >= 0) {
return OSId.WINDOWS;
} else {
return OSId.UNIX;
}
}
/**
* Get FFDec home directory
*
* @return FFDec home directory
*/
public static String getFFDecHome() {
if (directory == UNSPECIFIED_FILE) {
directory = null;
String userHome = null;
try {
userHome = System.getProperty("user.home");
} catch (SecurityException ignore) {
//ignored
}
if (userHome != null) {
String applicationId = ApplicationInfo.SHORT_APPLICATION_NAME;
OSId osId = getOSId();
if (osId == OSId.WINDOWS) {
File appDataDir = null;
try {
String appDataEV = System.getenv("APPDATA");
if ((appDataEV != null) && (appDataEV.length() > 0)) {
appDataDir = new File(appDataEV);
}
} catch (SecurityException ignore) {
//ignored
}
String vendorId = ApplicationInfo.VENDOR;
if ((appDataDir != null) && appDataDir.isDirectory()) {
// ${APPDATA}\{vendorId}\${applicationId}
String path = vendorId + "\\" + applicationId + "\\";
directory = new File(appDataDir, path);
} else {
// ${userHome}\Application Data\${vendorId}\${applicationId}
String path = "Application Data\\" + vendorId + "\\" + applicationId + "\\";
directory = new File(userHome, path);
}
} else if (osId == OSId.OSX) {
// ${userHome}/Library/Application Support/${applicationId}
String path = "Library/Application Support/" + applicationId + "/";
directory = new File(userHome, path);
} else {
File xdgConfigHome = null;
File oldConfigDir = new File(userHome, "." + applicationId + "/");
try {
String xdgConfigHomeEV = System.getenv("XDG_CONFIG_HOME");
if ((xdgConfigHomeEV != null) && (xdgConfigHomeEV.length() > 0)) {
xdgConfigHome = new File(xdgConfigHomeEV);
}
} catch (SecurityException ignore) {
//ignored
}
if ((xdgConfigHome != null) && xdgConfigHome.isDirectory()) {
// ${xdgConfigHome}/${applicationId}
String path = applicationId + "/";
directory = new File(xdgConfigHome, path);
} else if (oldConfigDir.isDirectory()) {
// ${userHome}/.${applicationId}
directory = oldConfigDir;
} else {
// ${userHome}/.config/${applicationId}
String path = ".config/" + applicationId + "/";
directory = new File(userHome, path);
}
}
} else {
//no home, then use application directory
directory = new File(".");
}
}
if (!directory.exists()) {
if (!directory.mkdirs()) {
if (!directory.exists()) {
directory = new File("."); //fallback to current directory
}
}
}
String ret = directory.getAbsolutePath();
if (!ret.endsWith(File.separator)) {
ret += File.separator;
}
return ret;
}
}

View File

@@ -61,10 +61,6 @@ public final class Configuration {
private static final ConfigurationStorage storage = new TomlConfigurationStorage();
private static final File UNSPECIFIED_FILE = new File("unspecified");
private static File directory = UNSPECIFIED_FILE;
/**
* Log level
*/
@@ -1141,24 +1137,24 @@ public final class Configuration {
@ConfigurationDefaultBoolean(false)
@ConfigurationCategory("display")
public static ConfigurationItem<Boolean> snapAlignCenterAlignmentVertical = null;
@ConfigurationDefaultBoolean(true)
@ConfigurationName("warning.linkTypes")
@ConfigurationCategory("script")
public static ConfigurationItem<Boolean> warningLinkTypes = null;
@ConfigurationDefaultBoolean(true)
@ConfigurationCategory("script")
public static ConfigurationItem<Boolean> showCodeCompletionOnDot = null;
@ConfigurationDefaultBoolean(false)
@ConfigurationCategory("script")
public static ConfigurationItem<Boolean> skipDetectionOfUnitializedClassFields = null;
@ConfigurationDefaultBoolean(false)
@ConfigurationInternal
public static ConfigurationItem<Boolean> showHeapStatusWidget = null;
@ConfigurationDefaultBoolean(false)
@ConfigurationCategory("script")
public static ConfigurationItem<Boolean> autoDeobfuscateIdentifiers = null;
@@ -1166,10 +1162,6 @@ public final class Configuration {
private static Map<String, String> configurationDescriptions = new LinkedHashMap<>();
private static Map<String, String> configurationTitles = new LinkedHashMap<>();
private enum OSId {
WINDOWS, OSX, UNIX
}
public static void setConfigurationDescriptions(Map<String, String> configurationDescriptions) {
Configuration.configurationDescriptions = configurationDescriptions;
}
@@ -1186,101 +1178,6 @@ public final class Configuration {
return configurationDescriptions.get(confirationName);
}
private static OSId getOSId() {
String OS = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
if ((OS.indexOf("mac") >= 0) || (OS.indexOf("darwin") >= 0)) {
return OSId.OSX;
} else if (OS.indexOf("win") >= 0) {
return OSId.WINDOWS;
} else {
return OSId.UNIX;
}
}
/**
* Get FFDec home directory
*
* @return FFDec home directory
*/
public static String getFFDecHome() {
if (directory == UNSPECIFIED_FILE) {
directory = null;
String userHome = null;
try {
userHome = System.getProperty("user.home");
} catch (SecurityException ignore) {
//ignored
}
if (userHome != null) {
String applicationId = ApplicationInfo.SHORT_APPLICATION_NAME;
OSId osId = getOSId();
if (osId == OSId.WINDOWS) {
File appDataDir = null;
try {
String appDataEV = System.getenv("APPDATA");
if ((appDataEV != null) && (appDataEV.length() > 0)) {
appDataDir = new File(appDataEV);
}
} catch (SecurityException ignore) {
//ignored
}
String vendorId = ApplicationInfo.VENDOR;
if ((appDataDir != null) && appDataDir.isDirectory()) {
// ${APPDATA}\{vendorId}\${applicationId}
String path = vendorId + "\\" + applicationId + "\\";
directory = new File(appDataDir, path);
} else {
// ${userHome}\Application Data\${vendorId}\${applicationId}
String path = "Application Data\\" + vendorId + "\\" + applicationId + "\\";
directory = new File(userHome, path);
}
} else if (osId == OSId.OSX) {
// ${userHome}/Library/Application Support/${applicationId}
String path = "Library/Application Support/" + applicationId + "/";
directory = new File(userHome, path);
} else {
File xdgConfigHome = null;
File oldConfigDir = new File(userHome, "." + applicationId + "/");
try {
String xdgConfigHomeEV = System.getenv("XDG_CONFIG_HOME");
if ((xdgConfigHomeEV != null) && (xdgConfigHomeEV.length() > 0)) {
xdgConfigHome = new File(xdgConfigHomeEV);
}
} catch (SecurityException ignore) {
//ignored
}
if ((xdgConfigHome != null) && xdgConfigHome.isDirectory()) {
// ${xdgConfigHome}/${applicationId}
String path = applicationId + "/";
directory = new File(xdgConfigHome, path);
} else if (oldConfigDir.isDirectory()) {
// ${userHome}/.${applicationId}
directory = oldConfigDir;
} else {
// ${userHome}/.config/${applicationId}
String path = ".config/" + applicationId + "/";
directory = new File(userHome, path);
}
}
} else {
//no home, then use application directory
directory = new File(".");
}
}
if (!directory.exists()) {
if (!directory.mkdirs()) {
if (!directory.exists()) {
directory = new File("."); //fallback to current directory
}
}
}
String ret = directory.getAbsolutePath();
if (!ret.endsWith(File.separator)) {
ret += File.separator;
}
return ret;
}
public static Font getSourceFont() {
return FontHelper.stringToFont(sourceFontString.get());
}
@@ -1408,7 +1305,7 @@ public final class Configuration {
return map.get(fileName);
}
/**
* Get per-swf custom configuration.
*
@@ -1434,7 +1331,7 @@ public final class Configuration {
return swfConf;
}
/**
* Get or create per-swf custom configuration.
*
@@ -1446,11 +1343,11 @@ public final class Configuration {
}
private static String getConfigFile() throws IOException {
return getFFDecHome() + storage.getConfigName();
return AppDirectoryProvider.getFFDecHome() + storage.getConfigName();
}
private static String getLegacyConfigFile() throws IOException {
return getFFDecHome() + legacyStorage.getConfigName();
return AppDirectoryProvider.getFFDecHome() + legacyStorage.getConfigName();
}
/**
@@ -1460,15 +1357,15 @@ public final class Configuration {
Logger.getLogger(Configuration.class.getName()).fine("Saving configuration...");
try {
storage.saveToFile(getConfigFile());
Logger.getLogger(Configuration.class.getName()).fine("TOML configuration saved.");
Logger.getLogger(Configuration.class.getName()).fine("TOML configuration saved.");
} catch (IOException ex) {
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, "Cannot save TOML configuration", ex);
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, "Cannot save TOML configuration", ex);
}
try {
legacyStorage.saveToFile(getLegacyConfigFile());
Logger.getLogger(Configuration.class.getName()).fine("Legacy configuration saved.");
Logger.getLogger(Configuration.class.getName()).fine("Legacy configuration saved.");
} catch (IOException ex) {
Logger.getLogger(Configuration.class.getName()).log(Level.WARNING, "Cannot save legacy configuration", ex);
Logger.getLogger(Configuration.class.getName()).log(Level.WARNING, "Cannot save legacy configuration", ex);
}
}
@@ -1555,14 +1452,18 @@ public final class Configuration {
* Set configuration fields.
*/
public static void setConfigurationFields() {
Logger.getLogger(Configuration.class.getName()).log(Level.FINE, "Setting configuration fields...");
try {
Map<String, Object> config;
if (new File(getConfigFile()).exists()) {
Logger.getLogger(Configuration.class.getName()).log(Level.FINE, "Using TOML file.");
config = storage.loadFromFile(getConfigFile());
} else {
Logger.getLogger(Configuration.class.getName()).log(Level.FINE, "Using legacy file.");
config = legacyStorage.loadFromFile(getLegacyConfigFile());
}
loadFromMap(config);
Logger.getLogger(Configuration.class.getName()).log(Level.FINE, "Configuration fields set.");
} catch (IOException ex) {
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
}
@@ -1702,7 +1603,7 @@ public final class Configuration {
* @return Folder
*/
public static File getPath(String folder) {
String home = getFFDecHome();
String home = AppDirectoryProvider.getFFDecHome();
File dir = new File(home + folder);
if (!dir.exists()) {
dir.mkdirs();

View File

@@ -61,6 +61,8 @@ public class TomlConfigurationStorage implements ConfigurationStorage {
@Override
public Map<String, Object> loadFromFile(String file) {
Logger.getLogger(TomlConfigurationStorage.class.getName()).log(Level.FINE, "Loading TOML file {0}", file);
Map<String, Object> result = new LinkedHashMap<>();
TomlParseResult tomlResult;
try {
@@ -209,6 +211,7 @@ public class TomlConfigurationStorage implements ConfigurationStorage {
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, "Exception during loading TOML configuration", ex);
}
}
Logger.getLogger(TomlConfigurationStorage.class.getName()).log(Level.FINE, "TOML file loaded.");
return result;
}