Skip to content

Commit

Permalink
Damn Stop Button (#75)
Browse files Browse the repository at this point in the history
* Bring back that damn stop button.
* Add new lib procedure for stopping the build.
* Handle thread interruption while populating resources.
* Disable the build menu after cthread is constructed.
* Enable the build menu in a finally block to ensure it gets enabled again.
  • Loading branch information
RobertBColton authored Jul 2, 2020
1 parent c305acc commit 03093f8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 47 deletions.
89 changes: 45 additions & 44 deletions org/enigma/EnigmaRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ public void run()
});

setMenuEnabled(false);
//stop.setVisible(false);
//stopb.setVisible(false);

final Thread initthread = new Thread()
{
Expand Down Expand Up @@ -397,27 +395,28 @@ public EnigmaSettings getInstance()

public void populateMenu()
{
//stopb = new JButton(); //$NON-NLS-1$
//stopb.addActionListener(this);
//stopb.setToolTipText(Messages.getString("EnigmaRunner.MENU_STOP"));
//stopb.setIcon(LGM.getIconForKey("EnigmaPlugin.STOP"));
//LGM.tool.add(stopb, 5);
int toolPos = 4; // <- After "Save As" separator.
stopb = new JButton(); //$NON-NLS-1$
stopb.addActionListener(this);
stopb.setToolTipText(Messages.getString("EnigmaRunner.MENU_STOP"));
stopb.setIcon(LGM.getIconForKey("EnigmaPlugin.STOP"));
LGM.tool.add(stopb, ++toolPos);
runb = new JButton(); //$NON-NLS-1$
runb.addActionListener(this);
runb.setToolTipText(Messages.getString("EnigmaRunner.MENU_RUN"));
runb.setIcon(LGM.getIconForKey("EnigmaPlugin.EXECUTE"));
LGM.tool.add(runb, 5);
LGM.tool.add(runb, ++toolPos);
debugb = new JButton(); //$NON-NLS-1$
debugb.addActionListener(this);
debugb.setToolTipText(Messages.getString("EnigmaRunner.MENU_DEBUG"));
debugb.setIcon(LGM.getIconForKey("EnigmaPlugin.DEBUG"));
LGM.tool.add(debugb, 6);
LGM.tool.add(debugb, ++toolPos);
compileb = new JButton(); //$NON-NLS-1$
compileb.addActionListener(this);
compileb.setToolTipText(Messages.getString("EnigmaRunner.MENU_COMPILE"));
compileb.setIcon(LGM.getIconForKey("EnigmaPlugin.COMPILE"));
LGM.tool.add(compileb, 7);
LGM.tool.add(new JToolBar.Separator(), 8);
LGM.tool.add(compileb, ++toolPos);
LGM.tool.add(new JToolBar.Separator(), ++toolPos);

JMenu menu = new GmMenu(Messages.getString("EnigmaRunner.MENU_BUILD")); //$NON-NLS-1$
menu.setMnemonic('B');
Expand Down Expand Up @@ -445,11 +444,11 @@ public void populateMenu()
compile.setAccelerator(KeyStroke.getKeyStroke(Messages.getKeyboardString("EnigmaRunner.COMPILE")));
menu.add(compile);
menu.addSeparator();
//stop = addItem(Messages.getString("EnigmaRunner.MENU_STOP")); //$NON-NLS-1$
//stop.addActionListener(this);
//stop.setIcon(LGM.getIconForKey("EnigmaPlugin.STOP"));
//stop.setAccelerator(KeyStroke.getKeyStroke(Messages.getKeyboardString("EnigmaRunner.STOP")));
//menu.add(stop);
stop = addItem(Messages.getString("EnigmaRunner.MENU_STOP")); //$NON-NLS-1$
stop.addActionListener(this);
stop.setIcon(LGM.getIconForKey("EnigmaPlugin.STOP"));
stop.setAccelerator(KeyStroke.getKeyStroke(Messages.getKeyboardString("EnigmaRunner.STOP")));
menu.add(stop);
rebuild = addItem(Messages.getString("EnigmaRunner.MENU_REBUILD_ALL")); //$NON-NLS-1$
rebuild.addActionListener(this);
rebuild.setIcon(LGM.getIconForKey("EnigmaPlugin.REBUILD_ALL"));
Expand Down Expand Up @@ -631,10 +630,10 @@ public void setMenuEnabled(boolean en)
run.setEnabled(en);
debug.setEnabled(en);
design.setEnabled(en);
//stop.setEnabled(!en);
stop.setEnabled(!en);
compile.setEnabled(en);
rebuild.setEnabled(en);
//stopb.setEnabled(!en);
stopb.setEnabled(!en);
runb.setEnabled(en);
debugb.setEnabled(en);
compileb.setEnabled(en);
Expand All @@ -653,19 +652,22 @@ class CompilerThread extends Thread {
}

public void run() {
EnigmaRunner.addDefaultExceptionHandler();
ef.open();
ef.progress(10,Messages.getString("EnigmaRunner.POPULATING")); //$NON-NLS-1$
EnigmaStruct es = EnigmaWriter.prepareStruct(LGM.currentFile,LGM.root);
ef.progress(20,Messages.getString("EnigmaRunner.CALLING")); //$NON-NLS-1$
System.out.println("Plugin: Delegating to ENIGMA (out of my hands now)");
System.out.println(DRIVER.compileEGMf(es,efi == null ? null : getUnixPath(efi.getAbsolutePath()),mode));
setupBaseKeywords();
populateKeywords();

setMenuEnabled(true);
//stop.setEnabled(false);
//stopb.setEnabled(false);
try {
EnigmaRunner.addDefaultExceptionHandler();
ef.open();
ef.progress(10,Messages.getString("EnigmaRunner.POPULATING")); //$NON-NLS-1$
EnigmaStruct es = EnigmaWriter.prepareStruct(LGM.currentFile,LGM.root);
if (Thread.currentThread().isInterrupted()) return;
ef.progress(20,Messages.getString("EnigmaRunner.CALLING")); //$NON-NLS-1$
System.out.println("Plugin: Delegating to ENIGMA (out of my hands now)");
System.out.println(DRIVER.compileEGMf(es,efi == null ? null : getUnixPath(efi.getAbsolutePath()),mode));
setupBaseKeywords();
populateKeywords();
} finally {
if (!stop.isEnabled())
ef.progress(0, Messages.getString("EnigmaRunner.BUILD_STOPPED"));
setMenuEnabled(true);
}
}
}

Expand All @@ -674,8 +676,6 @@ public void compile(final int mode)
{
if (!assertReady()) return;

//stop.setEnabled(true);
//stopb.setEnabled(true);
EnigmaSettings es = LGM.currentFile.resMap.get(EnigmaSettings.class).getResource();

if (es.targets.get(TargetHandler.COMPILER) == null)
Expand Down Expand Up @@ -723,7 +723,6 @@ else if (mode < MODE_DESIGN) //run/debug
outname = new File(outname.getPath());
}

setMenuEnabled(false);
LGM.commitAll();
//TODO: commit changes, have to do it because it's attached to game settings frame
//and doesn't get told.
Expand All @@ -732,12 +731,7 @@ else if (mode < MODE_DESIGN) //run/debug
//System.out.println("Compiling with " + enigma);

cthread = new CompilerThread(mode, outname);
try {
cthread.join();
} catch (InterruptedException e1) {
EnigmaRunner.showDefaultExceptionHandler(e1);
}
//cthread.run(outname);
setMenuEnabled(false); // <- stop button needs cthread instance
cthread.start();

if (mode == MODE_DESIGN) //design
Expand Down Expand Up @@ -768,10 +762,17 @@ public void actionPerformed(ActionEvent e)
{
if (!assertReady()) return;
Object s = e.getSource();
//if (s == stop || s == stopb) {
// cthread.interrupt();
// setMenuEnabled(true);
//}
if (s == stop || s == stopb) {
if (cthread != null) cthread.interrupt();
try {
DRIVER.libStopBuild();
} catch (UnsatisfiedLinkError err) {
// enigma only added this recently
// sink the error for now
}
stop.setEnabled(false);
stopb.setEnabled(false);
}
if (s == run || s == runb) compile(MODE_RUN);
if (s == debug || s == debugb) compile(MODE_DEBUG);
if (s == design) compile(MODE_DESIGN);
Expand Down
35 changes: 32 additions & 3 deletions org/enigma/EnigmaWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ protected void populateStruct() {
populateTimelines();
populateObjects();
populateRooms();

// leave if we were interrupted while writing any
// resource group above
if (Thread.currentThread().isInterrupted()) return;

// triggers not implemented
o.triggerCount = 0;
Expand Down Expand Up @@ -371,6 +375,7 @@ protected void populateSprites() {
org.lateralgm.resources.Sprite.class).toArray(
new org.lateralgm.resources.Sprite[0]);
for (int s = 0; s < size; s++) {
if (Thread.currentThread().isInterrupted()) return;
Sprite os = osl[s];
org.lateralgm.resources.Sprite is = isl[s];

Expand Down Expand Up @@ -467,6 +472,7 @@ protected void populateSounds() {
org.lateralgm.resources.Sound.class).toArray(
new org.lateralgm.resources.Sound[0]);
for (int s = 0; s < size; s++) {
if (Thread.currentThread().isInterrupted()) return;
Sound os = osl[s];
org.lateralgm.resources.Sound is = isl[s];

Expand Down Expand Up @@ -507,6 +513,7 @@ protected void populateBackgrounds() {
org.lateralgm.resources.Background.class).toArray(
new org.lateralgm.resources.Background[0]);
for (int s = 0; s < size; s++) {
if (Thread.currentThread().isInterrupted()) return;
Background ob = obl[s];
org.lateralgm.resources.Background ib = ibl[s];

Expand Down Expand Up @@ -542,6 +549,7 @@ protected void populatePaths() {
org.lateralgm.resources.Path.class).toArray(
new org.lateralgm.resources.Path[0]);
for (int p = 0; p < size; p++) {
if (Thread.currentThread().isInterrupted()) return;
Path op = opl[p];
org.lateralgm.resources.Path ip = ipl[p];

Expand Down Expand Up @@ -585,6 +593,7 @@ protected void populateScripts() {
org.lateralgm.resources.Script.class).toArray(
new org.lateralgm.resources.Script[0]);
for (int s = 0; s < isl.length; s++) {
if (Thread.currentThread().isInterrupted()) return;
Script oo = osl[s];
org.lateralgm.resources.Script io = isl[s];

Expand All @@ -594,6 +603,7 @@ protected void populateScripts() {
}

for (int s = 0; s < qs.size(); s++) {
if (Thread.currentThread().isInterrupted()) return;
Script oo = osl[s + isl.length];
oo.name = "lib" + qs.get(s).parentId + "_action" + qs.get(s).id; //$NON-NLS-1$ //$NON-NLS-2$
oo.id = -s - 2;
Expand All @@ -614,6 +624,7 @@ protected void populateShaders() {
org.lateralgm.resources.Shader.class).toArray(
new org.lateralgm.resources.Shader[0]);
for (int s = 0; s < isl.length; s++) {
if (Thread.currentThread().isInterrupted()) return;
Shader oo = osl[s];
org.lateralgm.resources.Shader io = isl[s];

Expand Down Expand Up @@ -667,6 +678,7 @@ protected void populateFonts() {
org.lateralgm.resources.Font.class).toArray(
new org.lateralgm.resources.Font[0]);
for (int f = 1; f < size; f++) {
if (Thread.currentThread().isInterrupted()) return;
Font of = ofl[f];
org.lateralgm.resources.Font ifont = ifl[f - 1];

Expand Down Expand Up @@ -725,9 +737,21 @@ private static void populateGlyph(Glyph og, java.awt.Font fnt, int c, int aa) {
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB };
if (aa < 0 || aa >= aaHints.length)
aa = 0;
GlyphVector gv = fnt.createGlyphVector(new FontRenderContext(null,
aaHints[aa], RenderingHints.VALUE_FRACTIONALMETRICS_OFF),
new String(Character.toChars(c)));

// This is a workaround to an unreported JDK bug for
// createGlyphVector sinking the interrupted status of the
// calling thread causing the ENIGMA build to continue
// until it causes native memory exceptions because the
// resources were only half written.
GlyphVector gv = null;
synchronized (Thread.currentThread()) {
if (Thread.currentThread().isInterrupted()) return;
gv = fnt.createGlyphVector(new FontRenderContext(null,
aaHints[aa], RenderingHints.VALUE_FRACTIONALMETRICS_OFF),
new String(Character.toChars(c)));
}
if (gv == null) return;

Rectangle2D r = gv.getPixelBounds(null, 0, 0); // don't know why it
// needs coordinates
if (r.getWidth() <= 0 || r.getHeight() <= 0)
Expand Down Expand Up @@ -778,6 +802,7 @@ protected void populateTimelines() {
org.lateralgm.resources.Timeline.class).toArray(
new org.lateralgm.resources.Timeline[0]);
for (int t = 0; t < size; t++) {
if (Thread.currentThread().isInterrupted()) return;
Timeline ot = otl[t];
org.lateralgm.resources.Timeline it = itl[t];

Expand Down Expand Up @@ -810,6 +835,7 @@ protected void populateObjects() {
org.lateralgm.resources.GmObject.class).toArray(
new org.lateralgm.resources.GmObject[0]);
for (int s = 0; s < size; s++) {
if (Thread.currentThread().isInterrupted()) return;
GmObject oo = ool[s];
org.lateralgm.resources.GmObject io = iol[s];

Expand Down Expand Up @@ -888,6 +914,7 @@ protected void populateRooms() {
org.lateralgm.resources.Room[] irl = irooms
.toArray(new org.lateralgm.resources.Room[0]);
for (int s = 0; s < size; s++) {
if (Thread.currentThread().isInterrupted()) return;
Room or = orly[s];
org.lateralgm.resources.Room is = irl[s];

Expand Down Expand Up @@ -989,6 +1016,7 @@ protected void populateRooms() {

or.instanceCount = ri.size();
if (or.instanceCount != 0) {
if (Thread.currentThread().isInterrupted()) return;
or.instances = new Instance.ByReference();
Instance[] oil = (Instance[]) or.instances.toArray(ri.size());
int i = 0;
Expand Down Expand Up @@ -1051,6 +1079,7 @@ protected void populateRooms() {

or.tileCount = is.tiles.size();
if (or.tileCount != 0) {
if (Thread.currentThread().isInterrupted()) return;
or.tiles = new Tile.ByReference();
Tile[] otl = (Tile[]) or.tiles.toArray(or.tileCount);
for (int t = 0; t < otl.length; t++) {
Expand Down
2 changes: 2 additions & 0 deletions org/enigma/backend/EnigmaDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ protected List<String> getFieldOrder()
}
}

public void libStopBuild();

public String libInit(EnigmaCallbacks ef);

public SyntaxError definitionsModified(String wscode, String yaml);
Expand Down
1 change: 1 addition & 0 deletions org/enigma/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ EnigmaPlugin.SELALL=Select All
EnigmaReader.UNKNOWN_VERSION=Unsupported version {0}
EnigmaReader.UNKNOWN_ACTION= Unsupported action type {0}

EnigmaRunner.BUILD_STOPPED=Build Stopped
EnigmaRunner.BUTTON_SYNTAX=Syntax
EnigmaRunner.BUTTON_SYNTAX_TIP=Syntax
EnigmaRunner.CALLING=Calling compiler.
Expand Down

0 comments on commit 03093f8

Please sign in to comment.