+
+ private void resourceChanged(IResourceChangeEvent event)
+ {
+ if (event.getType() == IResourceChangeEvent.POST_CHANGE)
+ {
+ tryHotReplaceIfChanged(event, mpmFile, this::assignMicroInstructionMemory, "MPM");
+
+ if (mpromFile.isPresent())
+ tryHotReplaceIfChanged(event, mpromFile.get(), this::assignMPROM, "MPROM");
+ }
+ }
+
+ private static void tryHotReplaceIfChanged(IResourceChangeEvent event, IFile memFile, RunnableThrowingCoreException assign, String type)
+ {
+ IResourceDelta mpmDelta = event.getDelta().findMember(memFile.getFullPath());
+ if (mpmDelta != null && (mpmDelta.getKind() & CHANGED) == CHANGED && memFile.exists())
+ tryHotReplace(memFile, assign, type);
+ }
+
+ private static void tryHotReplace(IFile memFile, RunnableThrowingCoreException assign, String type)
+ {
+ AtomicBoolean doHotReplace = new AtomicBoolean();
+ PlatformUI.getWorkbench().getDisplay().syncExec(() ->
+ {
+ if (MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Hot Replace " + type + "?",
+ String.format("The " + type + " %s has been modified on the file system. Replace simulated " + type
+ + " with modified contents?", memFile.getName())))
+ doHotReplace.set(true);
+ });
+ if (doHotReplace.get())
+ {
+ try
+ {
+ assign.run();
+ }
+ catch (CoreException e)
+ {
+ PlatformUI.getWorkbench().getDisplay()
+ .asyncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
+ "Failed Hot Replace!",
+ "An error occurred trying to read the modified " + type + " from the file system: " + e.getMessage()));
+ }
+ }
+ }
+
+ private static interface RunnableThrowingCoreException
+ {
+ public void run() throws CoreException;
+ }
+
+ private void assignMicroInstructionMemory() throws CoreException
+ {
+ try (InputStream mpmStream = mpmFile.getContents())
+ {
+ machine.getMicroInstructionMemory().bind(
+ MicroInstructionMemoryParser.parseMemory(machine.getDefinition().getMicroInstructionMemoryDefinition(), mpmStream));
+ }
+ catch (IOException e)
+ {
+ throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
+ }
+ }
+
+ private void assignMPROM() throws CoreException
+ {
+ assignMemory(mpromFile, machine.getMPROM(), machine.getDefinition().getMPROMDefinition(), StandardMPROM::new, "MPROM");
+ }
+
+ private void assignMainMemory() throws CoreException
+ {
+ assignMemory(memFile, machine.getMainMemory(), machine.getDefinition().getMainMemoryDefinition(), StandardMainMemory::new,
+ "initial RAM");
+ }
+
+ private static <D extends BitVectorMemoryDefinition, M extends BitVectorMemory> void assignMemory(Optional<IFile> memFile,
+ AbstractAssignableBitVectorMemory<M> memoryToAssign, D definition, Function<D, M> newMemory, String type) throws CoreException
+ {
+ if (memFile.isPresent())
+ {
+ try (InputStream initialRAMStream = memFile.get().getContents())
+ {
+ M mem = newMemory.apply(definition);
+ BitVectorBasedMemoryParser.parseMemory(mem, initialRAMStream);
+ memoryToAssign.bind(mem);
+ }
+ catch (IOException e)
+ {
+ throw new CoreException(
+ new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading " + type + " file", e));
+ }
+ }
+ }
+
+ public IFile getMPMFile()
+ {
+ return mpmFile;
+ }
+
+ public Optional<IFile> getMPROMFile()
+ {
+ return mpromFile;
+ }
+
+ public Optional<IFile> getMEMFile()
+ {
+ return memFile;
+ }