Skip to content

Commit

Permalink
Improve plugin cache handling & fix event handler loop/spam in Source…
Browse files Browse the repository at this point in the history
…SelectorPanel
  • Loading branch information
serivesmejia committed Oct 27, 2024
1 parent 7ba7bda commit 74ec7f7
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ public static void createConfigDialog(EOCVSim eocvSim) {
invokeLater(() -> new Configuration(eocvSim.visualizer.frame, eocvSim));
}

public static void createPluginsDialog(EOCVSim eocvSim) {
invokeLater(() -> new Configuration(eocvSim.visualizer.frame, eocvSim));
}

public static void createAboutDialog(EOCVSim eocvSim) {
invokeLater(() -> new About(eocvSim.visualizer.frame, eocvSim));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.github.serivesmejia.eocvsim.EOCVSim
import com.github.serivesmejia.eocvsim.gui.DialogFactory
import com.github.serivesmejia.eocvsim.gui.Visualizer
import com.github.serivesmejia.eocvsim.gui.dialog.Output
import com.github.serivesmejia.eocvsim.gui.dialog.PluginOutput
import com.github.serivesmejia.eocvsim.gui.util.GuiUtil
import com.github.serivesmejia.eocvsim.input.SourceType
import com.github.serivesmejia.eocvsim.util.FileFilters
Expand Down Expand Up @@ -102,7 +103,9 @@ class TopMenuBar(visualizer: Visualizer, eocvSim: EOCVSim) : JMenuBar() {
mFileMenu.add(editSettings)

val filePlugins = JMenuItem("Plugins")
filePlugins.addActionListener { DialogFactory.createPluginsDialog(eocvSim) }
filePlugins.addActionListener { eocvSim.pluginManager.appender.append(PluginOutput.SPECIAL_OPEN)}

mFileMenu.add(filePlugins)

mFileMenu.addSeparator()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.coroutines.swing.Swing
import java.awt.FlowLayout
import java.awt.GridBagConstraints
import java.awt.GridBagLayout
import java.awt.event.MouseAdapter
import javax.swing.*

class SourceSelectorPanel(private val eocvSim: EOCVSim) : JPanel() {
Expand Down Expand Up @@ -81,6 +82,7 @@ class SourceSelectorPanel(private val eocvSim: EOCVSim) : JPanel() {

sourceSelectorButtonsContainer.add(sourceSelectorCreateBtt)
sourceSelectorButtonsContainer.add(sourceSelectorDeleteBtt)
sourceSelectorDeleteBtt.isEnabled = false

add(sourceSelectorButtonsContainer, GridBagConstraints().apply {
gridy = 1
Expand All @@ -92,41 +94,46 @@ class SourceSelectorPanel(private val eocvSim: EOCVSim) : JPanel() {

private fun registerListeners() {
//listener for changing input sources
sourceSelector.addListSelectionListener { evt ->
if(allowSourceSwitching && !evt.valueIsAdjusting) {
try {
if (sourceSelector.selectedIndex != -1) {
val model = sourceSelector.model
val source = model.getElementAt(sourceSelector.selectedIndex)

//enable or disable source delete button depending if source is default or not
eocvSim.visualizer.sourceSelectorPanel.sourceSelectorDeleteBtt
.isEnabled = !(eocvSim.inputSourceManager.sources[source]?.isDefault ?: true)

if (!evt.valueIsAdjusting && source != beforeSelectedSource) {
if (!eocvSim.pipelineManager.paused) {
eocvSim.inputSourceManager.requestSetInputSource(source)
beforeSelectedSource = source
beforeSelectedSourceIndex = sourceSelector.selectedIndex
} else {
//check if the user requested the pause or if it was due to one shoot analysis when selecting images
if (eocvSim.pipelineManager.pauseReason !== PipelineManager.PauseReason.IMAGE_ONE_ANALYSIS) {
sourceSelector.setSelectedIndex(beforeSelectedSourceIndex)
} else { //handling pausing
eocvSim.pipelineManager.requestSetPaused(false)
eocvSim.inputSourceManager.requestSetInputSource(source)
beforeSelectedSource = source
beforeSelectedSourceIndex = sourceSelector.selectedIndex
sourceSelector.addMouseListener(object: MouseAdapter() {
override fun mouseClicked(e: java.awt.event.MouseEvent) {
val index = (e.source as JList<*>).locationToIndex(e.point)

if (index != -1) {
if(allowSourceSwitching) {
try {
if (sourceSelector.selectedIndex != -1) {
val model = sourceSelector.model
val source = model.getElementAt(sourceSelector.selectedIndex)

//enable or disable source delete button depending if source is default or not
sourceSelectorDeleteBtt.isEnabled = eocvSim.inputSourceManager.sources[source]?.isDefault == false

if (source != beforeSelectedSource) {
if (!eocvSim.pipelineManager.paused) {
eocvSim.inputSourceManager.requestSetInputSource(source)
beforeSelectedSource = source
beforeSelectedSourceIndex = sourceSelector.selectedIndex
} else {
//check if the user requested the pause or if it was due to one shoot analysis when selecting images
if (eocvSim.pipelineManager.pauseReason !== PipelineManager.PauseReason.IMAGE_ONE_ANALYSIS) {
sourceSelector.setSelectedIndex(beforeSelectedSourceIndex)
} else { //handling pausing
eocvSim.pipelineManager.requestSetPaused(false)
eocvSim.inputSourceManager.requestSetInputSource(source)
beforeSelectedSource = source
beforeSelectedSourceIndex = sourceSelector.selectedIndex
}
}
}
} else {
sourceSelector.setSelectedIndex(1)
}
} catch (ignored: ArrayIndexOutOfBoundsException) {
}
} else {
sourceSelector.setSelectedIndex(1)
}
} catch (ignored: ArrayIndexOutOfBoundsException) {
}
}
}
})

// delete selected input source
sourceSelectorDeleteBtt.addActionListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,22 @@ class PluginOutput(
) : Appendable {

companion object {
const val SPECIAL_CLOSE = "[CLOSE]"
const val SPECIAL_CONTINUE = "[CONTINUE]"
const val SPECIAL_SILENT = "[SILENT]"
private const val SPECIAL = "13mck"

const val SPECIAL_OPEN = "$SPECIAL[OPEN]"
const val SPECIAL_CLOSE = "$SPECIAL[CLOSE]"
const val SPECIAL_CONTINUE = "$SPECIAL[CONTINUE]"
const val SPECIAL_FREE = "$SPECIAL[FREE]"
const val SPECIAL_SILENT = "$SPECIAL[SILENT]"

fun String.trimSpecials(): String {
return this
.replace(SPECIAL_OPEN, "")
.replace(SPECIAL_CLOSE, "")
.replace(SPECIAL_CONTINUE, "")
.replace(SPECIAL_SILENT, "")
.replace(SPECIAL_FREE, "")
}
}

private val output = JDialog()
Expand Down Expand Up @@ -130,26 +143,24 @@ class PluginOutput(

private fun handleSpecials(text: String): Boolean {
when(text) {
SPECIAL_FREE -> {
mavenBottomButtonsPanel.continueButton.isEnabled = false
mavenBottomButtonsPanel.closeButton.isEnabled = true
}
SPECIAL_CLOSE -> close()
SPECIAL_CONTINUE -> {
mavenBottomButtonsPanel.continueButton.isEnabled = true
mavenBottomButtonsPanel.closeButton.isEnabled = false
}
}

if(!text.startsWith(SPECIAL_SILENT) && text != SPECIAL_CLOSE) {
if(!text.startsWith(SPECIAL_SILENT) && text != SPECIAL_CLOSE && text != SPECIAL_FREE) {
SwingUtilities.invokeLater {
output.isVisible = true
}
}

return text == SPECIAL_CLOSE || text == SPECIAL_CONTINUE
}

private fun String.trimSpecials(): String {
return this.replace(SPECIAL_CLOSE, "")
.replace(SPECIAL_CONTINUE, "")
.replace(SPECIAL_SILENT, "")
return text == SPECIAL_OPEN || text == SPECIAL_CLOSE || text == SPECIAL_CONTINUE || text == SPECIAL_FREE
}

override fun append(csq: CharSequence?): java.lang.Appendable? {
Expand Down Expand Up @@ -201,6 +212,8 @@ class PluginOutput(
override fun create(panel: OutputPanel) {
layout = BoxLayout(this, BoxLayout.LINE_AXIS)

add(Box.createRigidArea(Dimension(4, 0)))

copyButton.addActionListener {
Toolkit.getDefaultToolkit().systemClipboard.setContents(StringSelection(outputTextSupplier()), null)
}
Expand All @@ -221,6 +234,8 @@ class PluginOutput(

add(closeButton)
closeButton.addActionListener { closeCallback() }

add(Box.createRigidArea(Dimension(4, 0)))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ public boolean setInputSource(String sourceName, boolean makeDefault) {
public boolean setInputSource(String sourceName) {
InputSource src = null;

SysUtil.debugLogCalled("setInputSource");

if(sourceName == null) {
src = new NullSource();
} else {
Expand Down Expand Up @@ -298,6 +300,8 @@ public void pauseIfImageTwoFrames() {
}

public void requestSetInputSource(String name) {
SysUtil.debugLogCalled("requestSetInputSource");

eocvSim.onMainUpdate.doOnce(() -> setInputSource(name));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.github.serivesmejia.eocvsim.pipeline.util.PipelineExceptionTracker
import com.github.serivesmejia.eocvsim.pipeline.util.PipelineSnapshot
import com.github.serivesmejia.eocvsim.util.ReflectUtil
import com.github.serivesmejia.eocvsim.util.StrUtil
import com.github.serivesmejia.eocvsim.util.SysUtil
import com.github.serivesmejia.eocvsim.util.event.EventHandler
import com.github.serivesmejia.eocvsim.util.fps.FpsCounter
import com.github.serivesmejia.eocvsim.util.loggerForThis
Expand Down Expand Up @@ -572,7 +573,7 @@ class PipelineManager(

logger.info("Changing to pipeline ${pipelineClass.name}")

debugLogCalled("forceChangePipeline")
SysUtil.debugLogCalled("forceChangePipeline")

val instantiator = getInstantiatorFor(pipelineClass)

Expand Down Expand Up @@ -664,7 +665,7 @@ class PipelineManager(
}

fun requestForceChangePipeline(index: Int) {
debugLogCalled("requestForceChangePipeline")
SysUtil.debugLogCalled("requestForceChangePipeline")

onUpdate.doOnce { forceChangePipeline(index) }
}
Expand Down Expand Up @@ -769,15 +770,6 @@ class PipelineManager(
forceChangePipeline(0) // default pipeline
}

private fun debugLogCalled(name: String) {
val builder = StringBuilder()
for (s in Thread.currentThread().stackTrace) {
builder.appendLine(s.toString())
}

logger.debug("$name called in: {}", builder.toString().trim())
}

}

enum class PipelineTimeout(val ms: Long, val coolName: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@ public static CommandResult runShellCommand(String command) {
return result;
}


public static void debugLogCalled(String name) {
StringBuilder builder = new StringBuilder();
for (StackTraceElement s : Thread.currentThread().getStackTrace()) {
builder.append(s.toString()).append("\n");
}

logger.debug("{} called in: {}", name, builder.toString().trim());
}

public enum OperatingSystem {
WINDOWS,
LINUX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,31 @@ class PluginLoader(

setupFs()

if(pluginToml.contains("min-api-version")) {
val parsedVersion = ParsedVersion(pluginToml.getString("min-api-version"))
if(pluginToml.contains("api-version")) {
val parsedVersion = ParsedVersion(pluginToml.getString("api-version"))

if(parsedVersion > EOCVSim.PARSED_VERSION)
throw UnsupportedPluginException("Plugin requires a minimum api version of v${parsedVersion}, EOCV-Sim is currently running at v${EOCVSim.PARSED_VERSION}")

logger.info("Plugin $pluginName requests min api version of v${parsedVersion}")
}

if(pluginToml.contains("max-api-version")) {
val parsedVersion = ParsedVersion(pluginToml.getString("max-api-version"))

if(parsedVersion < EOCVSim.PARSED_VERSION)
throw UnsupportedPluginException("Plugin requires a maximum api version of v${parsedVersion}, EOCV-Sim is currently running at v${EOCVSim.PARSED_VERSION}")

logger.info("Plugin $pluginName requests max api version of v${parsedVersion}")
}

if(pluginToml.contains("api-version")) {
val parsedVersion = ParsedVersion(pluginToml.getString("api-version"))
if(pluginToml.contains("exact-api-version")) {
val parsedVersion = ParsedVersion(pluginToml.getString("exact-api-version"))

if(parsedVersion != EOCVSim.PARSED_VERSION)
throw UnsupportedPluginException("Plugin requires an exact api version of v${parsedVersion}, EOCV-Sim is currently running at v${EOCVSim.PARSED_VERSION}")

if(parsedVersion == EOCVSim.PARSED_VERSION)
throw UnsupportedPluginException("Plugin request api version of v${parsedVersion}, EOCV-Sim is currently running at v${EOCVSim.PARSED_VERSION}")
logger.info("Plugin $pluginName requests exact api version of v${parsedVersion}")
}

if(pluginToml.getBoolean("super-access", false)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package io.github.deltacv.eocvsim.plugin.loader
import com.github.serivesmejia.eocvsim.EOCVSim
import com.github.serivesmejia.eocvsim.gui.DialogFactory
import com.github.serivesmejia.eocvsim.gui.dialog.PluginOutput
import com.github.serivesmejia.eocvsim.gui.dialog.PluginOutput.Companion.trimSpecials
import com.github.serivesmejia.eocvsim.util.JavaProcess
import com.github.serivesmejia.eocvsim.util.extension.plus
import com.github.serivesmejia.eocvsim.util.io.EOCVSimFolder
Expand Down Expand Up @@ -65,7 +66,11 @@ class PluginManager(val eocvSim: EOCVSim) {

appender.subscribe {
if(!it.isBlank()) {
logger.info(it)
val message = it.trimSpecials()

if(message.isNotBlank()) {
logger.info(message)
}
}
}

Expand Down Expand Up @@ -97,6 +102,10 @@ class PluginManager(val eocvSim: EOCVSim) {
* @see PluginLoader
*/
fun init() {
eocvSim.visualizer.onInitFinished {
appender.append(PluginOutput.SPECIAL_FREE)
}

repositoryManager.init()

val pluginFiles = mutableListOf<File>()
Expand Down
Loading

0 comments on commit 74ec7f7

Please sign in to comment.