Skip to content

Commit

Permalink
Use working directory for petep.json file, add WORKING_DIR param to s…
Browse files Browse the repository at this point in the history
…h/bat files, minor fixes and refactors
  • Loading branch information
Warxim committed Oct 1, 2023
1 parent b75bdea commit f61f5d6
Show file tree
Hide file tree
Showing 18 changed files with 320 additions and 153 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ All notable changes to this project will are documented in this changelog file.

## Unreleased
### Added
- A bit more convenient Hex Editor
- Support for keeping selection when switching between BytesEditor tabs
- A bit more convenient Hex Editor (smart backspace/delete, support undo/redo, support keeping selection between tabs)
- Use working directory for petep.json file
- Fixed copy&paste behaviour of Text editor for bytes
- Small refactors and fixes

## [2.1.0] - 2023-05-10
### Added
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ chmod +x petep.sh
petep.bat
```

***Note:** These run scripts contain useful variables, including path to Java executable.
***Note:** These run scripts contain useful variables,
including working directory (for `petep.json` file), and path to Java executable.
You might need to change it if you do not have it in PATH or you use multiple Java versions.*

## Supported functionality
Expand Down
11 changes: 8 additions & 3 deletions scripts/petep.bat
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
@echo off

rem Useful params, change if needed
set JAVA_EXE=java.exe
set JAVAW_EXE=javaw.exe
set DIRNAME=%~dp0
set APP_HOME=%DIRNAME%
set JAVAW_EXE=java.exe
set APP_HOME=%~dp0
set WORKING_DIR=%APP_HOME%
set CMD_LINE_ARGS=%*
set DEFAULT_JVM_OPTS=
set CLASSPATH=%APP_HOME%\lib\*
set MAIN_CLASS="com.warxim.petep.Main"

rem Set working directory (important for petep.json), by default the startup directory is used
cd /D "%WORKING_DIR%"

if "%~2"=="--nogui" (
goto NOGUI
)
Expand Down
14 changes: 10 additions & 4 deletions scripts/petep.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#!/bin/bash

# Useful params, change if needed
JAVA="java"
APP_HOME="`pwd`"
APP_HOME=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)
WORKING_DIR=$APP_HOME
DEFAULT_JVM_OPTS=
CMD_LINE_ARGS=$@
CLASSPATH=$APP_HOME/lib/*
MAIN_CLASS="com.warxim.petep.Main"
LOG_FILE=petep.log

# Set working directory (important for petep.json), by default the startup directory is used
cd $WORKING_DIR

# Run PETEP
if [[ $2 == '--nogui' ]];
then
$JAVA -cp "$CLASSPATH" $MAIN_CLASS $CMD_LINE_ARGS
$JAVA $DEFAULT_JVM_OPTS -cp "$CLASSPATH" $MAIN_CLASS $CMD_LINE_ARGS
else
nohup $JAVA -cp "$CLASSPATH" $MAIN_CLASS $CMD_LINE_ARGS > $LOG_FILE &
fi
nohup $JAVA $DEFAULT_JVM_OPTS -cp "$CLASSPATH" $MAIN_CLASS $CMD_LINE_ARGS > $LOG_FILE &
fi
36 changes: 21 additions & 15 deletions src/main/java/com/warxim/petep/Bundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
import com.warxim.petep.bootstrap.CommandLineArguments;
import com.warxim.petep.common.Constant;
import com.warxim.petep.common.ContextType;
import com.warxim.petep.configuration.*;
import com.warxim.petep.configuration.ExtensionsLoader;
import com.warxim.petep.configuration.ExtensionsSaver;
import com.warxim.petep.configuration.ModulesLoader;
import com.warxim.petep.configuration.ModulesSaver;
import com.warxim.petep.configuration.ProjectLoader;
import com.warxim.petep.configuration.ProjectSaver;
import com.warxim.petep.core.PetepManager;
import com.warxim.petep.core.listener.PetepListenerManager;
import com.warxim.petep.exception.ConfigurationException;
Expand All @@ -34,7 +39,7 @@
import com.warxim.petep.util.FileUtils;
import lombok.Getter;

import java.io.File;
import java.nio.file.Path;

/**
* Singleton for PETEP assets.
Expand Down Expand Up @@ -91,13 +96,15 @@ public void load(CommandLineArguments arguments) throws ConfigurationException {
contextType = arguments.getContextType();
projectPath = arguments.getProjectPath();

var configDirectory = FileUtils.getProjectFileAbsolutePath(Constant.PROJECT_CONFIG_DIRECTORY) + File.separator;
var configDirectory = Path.of(FileUtils.getProjectFileAbsolutePath(Constant.PROJECT_CONFIG_DIRECTORY));

project = ProjectLoader.load(configDirectory + Constant.PROJECT_CONFIG_FILE);
project = ProjectLoader.load(configDirectory.resolve(Constant.PROJECT_CONFIG_FILE).toString());

receiverManager = new ReceiverManager();
// Create managers.
extensionManager = new ExtensionManager(ExtensionsLoader.load(configDirectory + Constant.EXTENSIONS_CONFIG_FILE));
extensionManager = new ExtensionManager(ExtensionsLoader.load(
configDirectory.resolve(Constant.EXTENSIONS_CONFIG_FILE).toString()
));
proxyModuleFactoryManager = new ProxyFactoryModuleManager();
interceptorModuleFactoryManager = new InterceptorModuleFactoryManager();
petepListenerManager = new PetepListenerManager();
Expand All @@ -107,16 +114,16 @@ public void load(CommandLineArguments arguments) throws ConfigurationException {

// Load proxies.
var proxyModuleContainer = new ProxyModuleContainer(ModulesLoader.load(
configDirectory + Constant.PROXIES_CONFIG_FILE,
configDirectory.resolve(Constant.PROXIES_CONFIG_FILE).toString(),
proxyModuleFactoryManager));

// Load interceptor module managers.
var interceptorModuleContainerC2S = new InterceptorModuleContainer(ModulesLoader.load(
configDirectory + Constant.INTERCEPTORS_C2S_CONFIG_FILE,
configDirectory.resolve(Constant.INTERCEPTORS_C2S_CONFIG_FILE).toString(),
interceptorModuleFactoryManager));

var interceptorModuleContainerS2C = new InterceptorModuleContainer(ModulesLoader.load(
configDirectory + Constant.INTERCEPTORS_S2C_CONFIG_FILE,
configDirectory.resolve(Constant.INTERCEPTORS_S2C_CONFIG_FILE).toString(),
interceptorModuleFactoryManager));

// Create PETEP manager.
Expand All @@ -132,27 +139,26 @@ public void load(CommandLineArguments arguments) throws ConfigurationException {
* @throws ConfigurationException if the bundle could not be saved because of configuration error
*/
public void save() throws ConfigurationException {
var configDirectory =
FileUtils.getProjectFileAbsolutePath(Constant.PROJECT_CONFIG_DIRECTORY) + File.separator;
var configDirectory = Path.of(FileUtils.getProjectFileAbsolutePath(Constant.PROJECT_CONFIG_DIRECTORY));

ProjectSaver.save(
configDirectory + Constant.PROJECT_CONFIG_FILE,
configDirectory.resolve(Constant.PROJECT_CONFIG_FILE).toString(),
project);

ExtensionsSaver.save(
configDirectory + Constant.EXTENSIONS_CONFIG_FILE,
configDirectory.resolve(Constant.EXTENSIONS_CONFIG_FILE).toString(),
extensionManager.getList());

ModulesSaver.save(
configDirectory + Constant.PROXIES_CONFIG_FILE,
configDirectory.resolve(Constant.PROXIES_CONFIG_FILE).toString(),
petepManager.getProxyModuleContainer().getList());

ModulesSaver.save(
configDirectory + Constant.INTERCEPTORS_C2S_CONFIG_FILE,
configDirectory.resolve(Constant.INTERCEPTORS_C2S_CONFIG_FILE).toString(),
petepManager.getInterceptorModuleContainerC2S().getList());

ModulesSaver.save(
configDirectory + Constant.INTERCEPTORS_S2C_CONFIG_FILE,
configDirectory.resolve(Constant.INTERCEPTORS_S2C_CONFIG_FILE).toString(),
petepManager.getInterceptorModuleContainerS2C().getList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void start() throws BootstrapException {
try {
// Initialize utils.
FileUtils.setProjectDirectory(
FileUtils.getApplicationFileAbsolutePath(arguments.getProjectPath()));
FileUtils.getWorkingDirectoryFileAbsolutePath(arguments.getProjectPath()));

// Load PETEP bundle.
Bundle.getInstance().load(arguments);
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/com/warxim/petep/gui/PetepGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.sun.javafx.css.StyleManager;
import com.warxim.petep.Bundle;
import com.warxim.petep.common.Constant;
import com.warxim.petep.exception.ConfigurationException;
import com.warxim.petep.gui.common.GuiConstant;
import com.warxim.petep.gui.common.PetepApplication;
import com.warxim.petep.gui.dialog.Dialogs;
Expand Down Expand Up @@ -74,20 +73,9 @@ public void start(Stage stage) {
* <p>Shows dialog to ask user, whether the project should be saved or not.</p>
*/
private void onClose(WindowEvent event) {
var decision = Dialogs.createYesOrNoOrCancelDialog("Save project", "Do you want to save project before closing PETEP?");
if (decision.isEmpty()) {
var closeProject = Dialogs.createCloseProjectDialog();
if (!closeProject) {
event.consume();
return;
}

if (Boolean.TRUE.equals(decision.get())) {
try {
Bundle.getInstance().save();
} catch (ConfigurationException e) {
event.consume();
Logger.getGlobal().log(Level.SEVERE, "Could not save project!", e);
Dialogs.createExceptionDialog("Could not save project", "Could not save project!", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
import com.warxim.petep.gui.guide.GuideDialog;
import com.warxim.petep.helper.DefaultGuiHelper;
import com.warxim.petep.util.GuiUtils;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
Expand Down Expand Up @@ -133,7 +133,12 @@ private void onProjectInfoMenuClick(ActionEvent event) {
*/
@FXML
private void onExitMenuClick(ActionEvent event) {
Platform.exit();
var closeProject = Dialogs.createCloseProjectDialog();
if (!closeProject) {
return;
}
var stage = (Stage) tabs.getScene().getWindow();
stage.close();
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/warxim/petep/gui/dialog/Dialogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package com.warxim.petep.gui.dialog;

import com.warxim.petep.Bundle;
import com.warxim.petep.common.Constant;
import com.warxim.petep.exception.ConfigurationException;
import com.warxim.petep.extension.PetepAPI;
import com.warxim.petep.gui.GuiBundle;
import javafx.event.ActionEvent;
Expand Down Expand Up @@ -357,6 +359,28 @@ public static void createNewVersionDialog(String newVersion) {
alert.showAndWait();
}

/**
* Create close project dialog
* @return {@code} true if project should be closed
*/
public static boolean createCloseProjectDialog() {
var decision = Dialogs.createYesOrNoOrCancelDialog("Save project", "Do you want to save project before closing PETEP?");
if (decision.isEmpty()) {
return false;
}

if (Boolean.TRUE.equals(decision.get())) {
try {
Bundle.getInstance().save();
} catch (ConfigurationException e) {
Logger.getGlobal().log(Level.SEVERE, "Could not save project!", e);
Dialogs.createExceptionDialog("Could not save project", "Could not save project!", e);
return false;
}
}
return true;
}

/**
* Opens PETEP website.
*/
Expand Down
Loading

0 comments on commit f61f5d6

Please sign in to comment.