MPROMEditor now calls its columns "Opcode" and "muPC"
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / MachineRegistry.java
1 package net.mograsim.machine;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.Map;
7 import java.util.Objects;
8 import java.util.Set;
9
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IConfigurationElement;
12 import org.eclipse.core.runtime.IExtension;
13 import org.eclipse.core.runtime.IExtensionPoint;
14 import org.eclipse.core.runtime.IExtensionRegistry;
15 import org.eclipse.core.runtime.IRegistryEventListener;
16 import org.eclipse.core.runtime.Platform;
17
18 /**
19  * The MachineRegsitry is a static context registry for all {@link MachineDefinition}s known at any point during runtime.
20  * <p>
21  * It is updated automatically as mograsim machine definition extension points ({@link #MACHINE_EXT_ID}) are added or removed.
22  *
23  * @author Christian Femers
24  *
25  */
26 public class MachineRegistry
27 {
28
29         public static final String MACHINE_EXT_ID = "net.mograsim.machine.machine_definition";
30
31         private static final Map<String, MachineDefinition> installedMachines = Collections.synchronizedMap(new HashMap<>());
32         private static final Set<MachineRegistryListener> listeners = Collections.synchronizedSet(new HashSet<>());
33
34         private static void reload()
35         {
36                 installedMachines.clear();
37                 IExtensionRegistry registry = Platform.getExtensionRegistry();
38                 IConfigurationElement[] config = registry.getConfigurationElementsFor(MACHINE_EXT_ID);
39                 try
40                 {
41                         for (IConfigurationElement e : config)
42                         {
43                                 final Object o = e.createExecutableExtension("class");
44                                 final String id = e.getAttribute("unique_id");
45                                 if (o instanceof MachineDefinition)
46                                 {
47                                         MachineDefinition md = (MachineDefinition) o;
48                                         if (Objects.equals(id, md.getId()))
49                                                 installedMachines.put(id, md);
50                                         else
51                                                 System.err.println("Machine definition ids to not match: " + id + " and " + md.getId());
52                                 } else
53                                 {
54                                         System.err.println("Invalid machine definition: " + o + "(id=" + id + "");
55                                 }
56                         }
57                 }
58                 catch (CoreException ex)
59                 {
60                         System.err.println("An error occurred reloading the machines:");
61                         ex.printStackTrace();
62                 }
63                 notifyListeners();
64         }
65
66         public static void initialize()
67         {
68                 reload();
69                 Platform.getExtensionRegistry().addListener(new IRegistryEventListener()
70                 {
71
72                         @Override
73                         public void removed(IExtensionPoint[] extensionPoints)
74                         {
75                                 // nothing?
76                         }
77
78                         @Override
79                         public void removed(IExtension[] extensions)
80                         {
81                                 reload();
82                         }
83
84                         @Override
85                         public void added(IExtensionPoint[] extensionPoints)
86                         {
87                                 // nothing?
88                         }
89
90                         @Override
91                         public void added(IExtension[] extensions)
92                         {
93                                 reload();
94                         }
95                 }, MACHINE_EXT_ID);
96         }
97
98         public static Map<String, MachineDefinition> getInstalledMachines()
99         {
100                 return Collections.unmodifiableMap(installedMachines);
101         }
102
103         public static MachineDefinition getMachine(String id)
104         {
105                 return installedMachines.get(id);
106         }
107
108         private static void notifyListeners()
109         {
110                 Map<String, MachineDefinition> unmodMachines = getInstalledMachines();
111                 listeners.forEach(l -> l.onReload(unmodMachines));
112         }
113
114         public static void addMachineRegistryListener(MachineRegistryListener listener)
115         {
116                 listeners.add(listener);
117         }
118
119         public static void removeMachineRegistryListener(MachineRegistryListener listener)
120         {
121                 listeners.remove(listener);
122         }
123
124         @FunctionalInterface
125         public interface MachineRegistryListener
126         {
127                 void onReload(Map<String, MachineDefinition> installedMachines);
128         }
129 }