Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gestalt v7 #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 20 additions & 32 deletions build.gradle
@@ -1,38 +1,26 @@
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
// Since SpotBugs and SonarQube in the legacy style have external dependencies we have to have this block here.
// Alternatively we untangle and update the common.gradle / Kotlin Gradle plugin stuff or just remove these two
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.net.minecrell:licenser:0.3"
}
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'net.minecrell.licenser'

repositories {
mavenCentral()
maven {
url "http://artifactory.terasology.org/artifactory/virtual-repo-live"
}
}
repositories {
// External libs - jcenter is Bintray and is supposed to be a superset of Maven Central, but do both just in case
jcenter()
mavenCentral()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
//Spotbugs
classpath("gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:4.0.0")

dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compile group: 'org.msgpack', name: 'msgpack-core', version: '0.8.16'
compile fileTree(dir: 'libs', include: '*.jar')
testCompile 'junit:junit:4.12'
compile group: 'org.terasology.jnlua', name: 'JNLua', version: '0.1.0-SNAPSHOT'
// SonarQube / Cloud scanning
classpath("org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.8")
}
}

license {
sourceSets = [project.sourceSets.main]
header = project.file("docs/NOTICE")
ignoreFailures = true
plugins {
id("terasology-module")
}
4 changes: 3 additions & 1 deletion docs/simulator_examples/oc_machine.json
Expand Up @@ -27,7 +27,9 @@
{
"id": "screen-address",
"type": "SimulatorFrameBufferWindow",
"connects": ["keyboard-address"],
"connects": [
"keyboard-address"
],
"windowName": "OpenComputers framebuffer"
},
{
Expand Down
10 changes: 10 additions & 0 deletions module.txt
@@ -0,0 +1,10 @@
{
"id": "Kallisti",
"version": "1.0.0",
"author": "asiekierka",
"displayName": "Kallisti",
"description": "Library for embedding fantasy computer virtual machines in game engines.",
"dependencies": [],
"serverSideOnly": false,
"isLibrary": true
}
@@ -1,28 +1,14 @@
/*
* Copyright 2018 Adrian Siekierka, MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.kallisti.base.component;

import org.terasology.kallisti.base.interfaces.Identifiable;

/**
* The context of a component, linking it to its in-game representation.
*
* Please note that a valid ComponentContext MUST implement equals() and
* hashCode().
* <p>
* Please note that a valid ComponentContext MUST implement equals() and hashCode().
*/
public interface ComponentContext extends Identifiable {
}
@@ -1,18 +1,5 @@
/*
* Copyright 2018 Adrian Siekierka, MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.kallisti.base.component;

Expand All @@ -29,34 +16,15 @@
* Handler for emitting ComponentEvents.
*/
public class ComponentEventHandler {
private static class Listener {
private final Object parent;
private final MethodHandle handle;

public Listener(Object parent, Method method) {
this.parent = parent;
try {
this.handle = MethodHandles.lookup().unreflect(method);
} catch (IllegalAccessException e) {
// Should be caught earlier!
throw new RuntimeException(e);
}
}

public void invoke(ComponentEvent event) throws Throwable {
this.handle.invoke(parent, event);
}
}

private final MultiValueMap<Class, Listener> listeners;

public ComponentEventHandler() {
listeners = new CollectionBackedMultiValueMap<>(new HashMap<>(), ArrayList::new);
}

/**
* Register an object's @ComponentEventListener-marked methods
* as event listeners.
* Register an object's @ComponentEventListener-marked methods as event listeners.
*
* @param o The object.
*/
public void register(Object o) {
Expand All @@ -72,6 +40,7 @@ public void register(Object o) {

/**
* Emit a given ComponentEvent to all matching listeners.
*
* @param event The event.
*/
public void emit(ComponentEvent event) {
Expand All @@ -84,4 +53,23 @@ public void emit(ComponentEvent event) {
}
}
}

private static class Listener {
private final Object parent;
private final MethodHandle handle;

public Listener(Object parent, Method method) {
this.parent = parent;
try {
this.handle = MethodHandles.lookup().unreflect(method);
} catch (IllegalAccessException e) {
// Should be caught earlier!
throw new RuntimeException(e);
}
}

public void invoke(ComponentEvent event) throws Throwable {
this.handle.invoke(parent, event);
}
}
}
@@ -1,18 +1,5 @@
/*
* Copyright 2018 Adrian Siekierka, MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.kallisti.base.component;

Expand All @@ -23,9 +10,8 @@

/**
* Interface used to mark event listening methods in components.
*
* The signature is as follows:
* void eventListener([? extends ComponentEvent] event) { ... }
* <p>
* The signature is as follows: void eventListener([? extends ComponentEvent] event) { ... }
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
Expand Down
@@ -1,18 +1,5 @@
/*
* Copyright 2018 Adrian Siekierka, MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.kallisti.base.component;

Expand All @@ -23,11 +10,10 @@

/**
* Marks an interface (or class) used in component queries.
*
* Any class implementing an interface annotated with this, extending a class
* annotated with this or being annotated with this itself will be counted as
* a component which may be queried using the Machine's lookup methods.
* As such, only one of its type will be able to exist per Context.
* <p>
* Any class implementing an interface annotated with this, extending a class annotated with this or being annotated
* with this itself will be counted as a component which may be queried using the Machine's lookup methods. As such,
* only one of its type will be able to exist per Context.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
Expand Down
@@ -1,51 +1,33 @@
/*
* Copyright 2018 Adrian Siekierka, MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.kallisti.base.component;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

/**
* Marks a method in an object-based peripheral which should be exposed
* to the end user.
* Marks a method in an object-based peripheral which should be exposed to the end user.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ComponentMethod {
/**
* @return The target name of the method. If empty, the Java method name
* is used.
* @return The target name of the method. If empty, the Java method name is used.
*/
String name() default "";

/**
* @return If this is true and the method's return type is an array,
* the array will be returned as multiple arguments on compatible
* platforms.
* @return If this is true and the method's return type is an array, the array will be returned as multiple
* arguments on compatible platforms.
*/
boolean returnsMultipleArguments() default false;

/**
* @return If the method execution requires synchronization with the
* in-game execution thread, as opposed to running on the
* computer's separate thread.
* @return If the method execution requires synchronization with the in-game execution thread, as opposed to running
* on the computer's separate thread.
*/
boolean synchronize() default false;
}
@@ -1,18 +1,5 @@
/*
* Copyright 2018 Adrian Siekierka, MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.kallisti.base.component;

Expand All @@ -22,24 +9,21 @@
import java.lang.annotation.Target;

/**
* Marks a method or constructor which, taking in multiple arguments, creates
* a new component.
*
* Arguments may be components or provided non-component objects, such
* as a Machine. All permutations of them will be evaluated with the Rule.
* All objects are required to be present at least once, unless Optional is
* utilized. The Contexts of all Components will be joined together.
*
* There are two types of rules:
* - creation rules - non-variable argument methods,
* - joining rules - variable argument methods returning the same type as
* their argument.
* Marks a method or constructor which, taking in multiple arguments, creates a new component.
* <p>
* Arguments may be components or provided non-component objects, such as a Machine. All permutations of them will be
* evaluated with the Rule. All objects are required to be present at least once, unless Optional is utilized. The
* Contexts of all Components will be joined together.
* <p>
* There are two types of rules: - creation rules - non-variable argument methods, - joining rules - variable argument
* methods returning the same type as their argument.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
public @interface ComponentRule {
/**
* The priority of the rule. Higher priorities are attempted first.
*
* @return The priority of the rule.
*/
int priority() default 0;
Expand Down