This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
tutorial:modding_architecture [2014/07/27 14:34] vexatos |
tutorial:modding_architecture [2015/01/14 23:41] 127.0.0.1 external edit |
||
---|---|---|---|
Line 12: | Line 12: | ||
To make the whole thing a little less abstract, here's how an architecture is actually *used*. This will hopefully provide a better understanding of where the architecture implementation fits into the OC ecosystem. To use an architecture, a machine using that architecture must be created. | To make the whole thing a little less abstract, here's how an architecture is actually *used*. This will hopefully provide a better understanding of where the architecture implementation fits into the OC ecosystem. To use an architecture, a machine using that architecture must be created. | ||
- | - *Note*: it's planned to allow addons to register architectures that can be provided by the standard CPUs, so you will not have to implement an actual computer block / item yourself, which is currently required (2014-07-24). | + | - *Note*: OC 1.4 allows addons to register architectures that can be provided by the standard CPUs, so you will not have to implement an actual computer block / item yourself. The architecture a CPU provides can be changed by sneak-activating the CPU while holding it in your hand. |
The machine encapsulates the architecture and takes care of thread scheduling, signal queuing, network connecticity and so on. All you have to do for the created machine is call `update()` each tick (and maybe forward some calls such as `start()` to it). | The machine encapsulates the architecture and takes care of thread scheduling, signal queuing, network connecticity and so on. All you have to do for the created machine is call `update()` each tick (and maybe forward some calls such as `start()` to it). | ||
Line 42: | Line 42: | ||
```java | ```java | ||
- | /** The VM itself. */ | + | /** The VM itself. This is just an example, it's not a "real" interface. */ |
public interface PseudoVM { | public interface PseudoVM { | ||
Object[] run(Object[] args) throws Exception; | Object[] run(Object[] args) throws Exception; | ||
Line 58: | Line 58: | ||
```java | ```java | ||
+ | /** This is the class you implement; Architecture is from the OC API. */ | ||
+ | @Architecture.Name("Pseudolang") | ||
public class PseudoArchitecture implements Architecture { | public class PseudoArchitecture implements Architecture { | ||
private final Machine machine; | private final Machine machine; | ||
Line 67: | Line 69: | ||
this.machine = machine; | this.machine = machine; | ||
} | } | ||
- | |||
- | public String name() { return "Pseudo"; } | ||
public boolean isInitialized() { return true; } | public boolean isInitialized() { return true; } | ||
Line 91: | Line 91: | ||
// (true, something) the call succeeded, if it returns (false) | // (true, something) the call succeeded, if it returns (false) | ||
// the limit was reached. | // the limit was reached. | ||
+ | // The script running in the VM is then supposed to return control | ||
+ | // to the caller initiating the current execution (e.g. by yielding | ||
+ | // if supported, or just returning, when in an event driven system). | ||
return new Object[]{false}; | return new Object[]{false}; | ||
} | } | ||
Line 101: | Line 104: | ||
final Node node = machine.node().network().node(address); | final Node node = machine.node().network().node(address); | ||
if (node instanceof Component) { | if (node instanceof Component) { | ||
- | // TODO forgot to add this to the API, will add in 1.4. | + | final Component component = (Component) node; |
- | // Use reflection for now. Sorry! | + | if (component.canBeSeenFrom(machine.node())) { |
- | return ((Component)node).isDirect(method); | + | final Callback callback = machine.methods(node.host()).get(method); |
+ | if (callback != null) { | ||
+ | return callback.direct(); | ||
+ | } | ||
+ | } | ||
} | } | ||
return false; | return false; | ||
Line 188: | Line 195: | ||
Some pseudo-code for handling synchronized calls in the VM: | Some pseudo-code for handling synchronized calls in the VM: | ||
- | ``` | + | ```scala |
private def invokeSynchronous(address, method, ...) { | private def invokeSynchronous(address, method, ...) { | ||
yield; // This is where it returns to runThreaded(). | yield; // This is where it returns to runThreaded(). | ||
Line 220: | Line 227: | ||
Registering an architecture | Registering an architecture | ||
--------------------------- | --------------------------- | ||
- | This one's pretty simple: just do `li.cil.oc.api.Machine.add(PseudoArchitecture.class)`. | + | This one's pretty simple: just do `li.cil.oc.api.Machine.add(PseudoArchitecture.class)`. This will allow CPUs to provide the registered architecture. If you do not wish OC CPUs to provide your architecture, you can either add your own `Processor` driver that can be used to get access to the architecture, or go one step further and add your own computer block, that will exclusively run your architecture, for example. |
- | Note that this doesn't really do anything *yet*, but will be used later on to provide architectures registered like this via the built-in CPUs. | + | |
Contents | Contents | ||
------------ | ------------ | ||
{{page>tutorial:contents&noheader&noeditbutton&nouser&nofooter}} | {{page>tutorial:contents&noheader&noeditbutton&nouser&nofooter}} |