diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/AppDirectoryProvider.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/AppDirectoryProvider.java new file mode 100644 index 000000000..7d69e9a99 --- /dev/null +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/AppDirectoryProvider.java @@ -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; + } +} diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java index de91655b4..9427ffe24 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java @@ -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 snapAlignCenterAlignmentVertical = null; - + @ConfigurationDefaultBoolean(true) @ConfigurationName("warning.linkTypes") @ConfigurationCategory("script") public static ConfigurationItem warningLinkTypes = null; - + @ConfigurationDefaultBoolean(true) @ConfigurationCategory("script") public static ConfigurationItem showCodeCompletionOnDot = null; - + @ConfigurationDefaultBoolean(false) @ConfigurationCategory("script") public static ConfigurationItem skipDetectionOfUnitializedClassFields = null; - + @ConfigurationDefaultBoolean(false) @ConfigurationInternal public static ConfigurationItem showHeapStatusWidget = null; - + @ConfigurationDefaultBoolean(false) @ConfigurationCategory("script") public static ConfigurationItem autoDeobfuscateIdentifiers = null; @@ -1166,10 +1162,6 @@ public final class Configuration { private static Map configurationDescriptions = new LinkedHashMap<>(); private static Map configurationTitles = new LinkedHashMap<>(); - private enum OSId { - WINDOWS, OSX, UNIX - } - public static void setConfigurationDescriptions(Map 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 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(); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/TomlConfigurationStorage.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/TomlConfigurationStorage.java index 8cb9e1c76..c02b8097c 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/TomlConfigurationStorage.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/TomlConfigurationStorage.java @@ -61,6 +61,8 @@ public class TomlConfigurationStorage implements ConfigurationStorage { @Override public Map loadFromFile(String file) { + Logger.getLogger(TomlConfigurationStorage.class.getName()).log(Level.FINE, "Loading TOML file {0}", file); + Map 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; } diff --git a/src/com/jpexs/decompiler/flash/gui/Main.java b/src/com/jpexs/decompiler/flash/gui/Main.java index 545954b31..ee9a02936 100644 --- a/src/com/jpexs/decompiler/flash/gui/Main.java +++ b/src/com/jpexs/decompiler/flash/gui/Main.java @@ -38,6 +38,7 @@ import com.jpexs.decompiler.flash.abc.ABC; import com.jpexs.decompiler.flash.abc.ABCInputStream; import com.jpexs.decompiler.flash.abc.ABCOpenException; import com.jpexs.decompiler.flash.abc.avm2.AVM2Code; +import com.jpexs.decompiler.flash.configuration.AppDirectoryProvider; import com.jpexs.decompiler.flash.configuration.Configuration; import com.jpexs.decompiler.flash.configuration.CustomConfigurationKeys; import com.jpexs.decompiler.flash.configuration.SwfSpecificConfiguration; @@ -228,6 +229,8 @@ public class Main { private static SWF runningSWF = null; private static SwfPreparation runningPreparation = null; + + private static boolean loggingFileLoaded = false; public static SWF getRunningSWF() { return runningSWF; @@ -3167,10 +3170,11 @@ public class Main { */ public static void main(String[] args) throws IOException { decodeLaunch5jArgs(args); + preInitLogging(); setSessionLoaded(false); - System.setProperty("sun.io.serialization.extendedDebugInfo", "true"); - + System.setProperty("sun.io.serialization.extendedDebugInfo", "true"); + clearTemp(); try { @@ -3293,9 +3297,9 @@ public class Main { } public static String tempFile(String url) throws IOException { - File f = new File(Configuration.getFFDecHome() + "saved" + File.separator); + File f = new File(AppDirectoryProvider.getFFDecHome() + "saved" + File.separator); Path.createDirectorySafe(f); - return Configuration.getFFDecHome() + "saved" + File.separator + "asdec_" + Integer.toHexString(url.hashCode()) + ".tmp"; + return AppDirectoryProvider.getFFDecHome() + "saved" + File.separator + "asdec_" + Integer.toHexString(url.hashCode()) + ".tmp"; } public static void removeTrayIcon() { @@ -3534,7 +3538,7 @@ public class Main { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); try { - fileName = Configuration.getFFDecHome() + "logs" + File.separator; + fileName = AppDirectoryProvider.getFFDecHome() + "logs" + File.separator; if (Configuration.useDetailedLogging.get()) { fileName += "log-" + sdf.format(new Date()) + ".txt"; } else { @@ -3594,17 +3598,24 @@ public class Main { System.getProperty("java.version"), System.getProperty("java.vendor"), System.getProperty("os.arch")}); } - public static void initLogging(boolean debug) { - File loggingFile = new File(Configuration.getFFDecHome() + "/logging.properties"); + public static void preInitLogging() { + File loggingFile = new File(AppDirectoryProvider.getFFDecHome() + "/logging.properties"); if (loggingFile.exists()) { //use manual configuration file final LogManager logManager = LogManager.getLogManager(); try { logManager.readConfiguration(new FileInputStream(loggingFile)); + loggingFileLoaded = true; return; } catch (IOException ex) { //ignore } } + } + + public static void initLogging(boolean debug) { + if (loggingFileLoaded) { + return; + } try { Logger logger = Logger.getLogger(""); logger.setLevel(Configuration.logLevel); diff --git a/src/com/jpexs/decompiler/flash/gui/SearchResultsStorage.java b/src/com/jpexs/decompiler/flash/gui/SearchResultsStorage.java index 0d70db596..d3d2f57e8 100644 --- a/src/com/jpexs/decompiler/flash/gui/SearchResultsStorage.java +++ b/src/com/jpexs/decompiler/flash/gui/SearchResultsStorage.java @@ -17,6 +17,7 @@ package com.jpexs.decompiler.flash.gui; import com.jpexs.decompiler.flash.SWF; +import com.jpexs.decompiler.flash.configuration.AppDirectoryProvider; import com.jpexs.decompiler.flash.configuration.Configuration; import com.jpexs.decompiler.flash.search.ABCSearchResult; import com.jpexs.decompiler.flash.search.ActionSearchResult; @@ -54,7 +55,7 @@ public class SearchResultsStorage { private static final int DATA_ACTION = 2; private static String getConfigFile() throws IOException { - return Configuration.getFFDecHome() + SEARCH_RESULTS_FILE; + return AppDirectoryProvider.getFFDecHome() + SEARCH_RESULTS_FILE; } List openableIds = new ArrayList<>(); diff --git a/src/com/jpexs/decompiler/flash/gui/translator/Translator.java b/src/com/jpexs/decompiler/flash/gui/translator/Translator.java index 2267a49bf..d2fd8cd39 100644 --- a/src/com/jpexs/decompiler/flash/gui/translator/Translator.java +++ b/src/com/jpexs/decompiler/flash/gui/translator/Translator.java @@ -16,6 +16,7 @@ */ package com.jpexs.decompiler.flash.gui.translator; +import com.jpexs.decompiler.flash.configuration.AppDirectoryProvider; import com.jpexs.decompiler.flash.configuration.Configuration; import com.jpexs.decompiler.flash.gui.View; import com.jpexs.decompiler.flash.gui.ViewMessages; @@ -899,11 +900,11 @@ public class Translator extends JFrame implements ItemListener { } private String getStorageFile() { - return Configuration.getFFDecHome() + "translated.zip"; + return AppDirectoryProvider.getFFDecHome() + "translated.zip"; } private String getWindowFile() { - return Configuration.getFFDecHome() + "translated.ini"; + return AppDirectoryProvider.getFFDecHome() + "translated.ini"; } private String escapeUnicode(String s) { @@ -1137,7 +1138,7 @@ public class Translator extends JFrame implements ItemListener { } private void saveAll() throws FileNotFoundException, IOException { - String storageFile = Configuration.getFFDecHome() + "alltranslated.zip"; + String storageFile = AppDirectoryProvider.getFFDecHome() + "alltranslated.zip"; save(storageFile, resourceValues); }