Skip to content

Commit

Permalink
chore(behavior): remove some CoreRegistry usage
Browse files Browse the repository at this point in the history
  • Loading branch information
keturn committed Jun 14, 2022
1 parent 558c841 commit a4e1b84
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.logic.behavior;

import com.google.common.collect.Lists;
import com.google.gson.GsonBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.terasology.engine.context.internal.ContextImpl;
import org.terasology.engine.logic.behavior.core.Actor;
import org.terasology.engine.logic.behavior.core.BehaviorNode;
import org.terasology.engine.logic.behavior.core.BehaviorState;
Expand All @@ -30,7 +31,7 @@ public void init() {
nextId2 = 1;

gsonBuilder = new GsonBuilder();
BehaviorTreeBuilder builder = new BehaviorTreeBuilder() {
BehaviorTreeBuilder builder = new BehaviorTreeBuilder(new ContextImpl()) {
@Override
public BehaviorNode createNode(BehaviorNode node) {
return new CountDelegate(node);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.logic.behavior;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.terasology.engine.context.internal.ContextImpl;
import org.terasology.engine.logic.behavior.actions.CounterAction;
import org.terasology.engine.logic.behavior.actions.Print;
import org.terasology.engine.logic.behavior.actions.TimeoutAction;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void test() {
@BeforeEach
public void setup() {

treeBuilder = new BehaviorTreeBuilder();
treeBuilder = new BehaviorTreeBuilder(new ContextImpl());
treeBuilder.registerAction("print", Print.class);
treeBuilder.registerDecorator("counter", CounterAction.class);
treeBuilder.registerDecorator("timeout", TimeoutAction.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.logic.behavior;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.context.internal.ContextImpl;
import org.terasology.engine.logic.behavior.actions.Print;
import org.terasology.engine.logic.behavior.core.Actor;
import org.terasology.engine.logic.behavior.core.BaseAction;
Expand All @@ -19,7 +20,7 @@ private Example() {
}

public static void main(String[] args) {
BehaviorTreeBuilder treeBuilder = new BehaviorTreeBuilder();
BehaviorTreeBuilder treeBuilder = new BehaviorTreeBuilder(new ContextImpl());

BehaviorNode node = treeBuilder.fromJson("{ sequence:[ success, success, failure ] }");
logger.info("{}", new DefaultBehaviorTreeRunner(node, null).step());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.core;

Expand Down Expand Up @@ -200,7 +200,7 @@ public void initialise() {
}

private void initialiseSystem(ComponentSystem system) {
InjectionHelper.inject(system);
InjectionHelper.inject(system, context);

if (console != null) {
MethodCommand.registerAvailable(system, console, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.logic.behavior;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.context.Context;
import org.terasology.engine.core.PathManager;
import org.terasology.engine.entitySystem.entity.EntityManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent;
import org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent;
import org.terasology.engine.entitySystem.prefab.PrefabManager;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
Expand Down Expand Up @@ -49,10 +49,11 @@ public class BehaviorSystem extends BaseComponentSystem implements UpdateSubscri

public static final Name BEHAVIORS = new Name("Behaviors");
private static final Logger logger = LoggerFactory.getLogger(BehaviorSystem.class);

@In
private EntityManager entityManager;
private Context context;
@In
private PrefabManager prefabManager;
private EntityManager entityManager;
@In
private AssetManager assetManager;

Expand Down Expand Up @@ -116,7 +117,7 @@ public void save(BehaviorTree tree) {
Path overridesPath = PathManager.getInstance().getHomeModPath().resolve(BEHAVIORS.toString()).resolve("overrides");
savePath = overridesPath.resolve(uri.getModuleName().toString()).resolve("behaviors");
}
BehaviorTreeFormat loader = new BehaviorTreeFormat();
BehaviorTreeFormat loader = new BehaviorTreeFormat(context);
try {
Files.createDirectories(savePath);
Path file = savePath.resolve(uri.getResourceName() + ".behavior");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.logic.behavior;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.audio.StaticSound;
import org.terasology.engine.context.Context;
import org.terasology.engine.core.PathManager;
import org.terasology.engine.entitySystem.entity.EntityManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent;
import org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent;
import org.terasology.engine.entitySystem.prefab.PrefabManager;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
Expand Down Expand Up @@ -52,10 +52,11 @@
public class CollectiveBehaviorSystem extends BaseComponentSystem implements UpdateSubscriberSystem {
public static final Name BEHAVIORS = new Name("Behaviors");
private static final Logger logger = LoggerFactory.getLogger(BehaviorSystem.class);

@In
private EntityManager entityManager;
private Context context;
@In
private PrefabManager prefabManager;
private EntityManager entityManager;
@In
private AssetManager assetManager;

Expand Down Expand Up @@ -117,7 +118,7 @@ public void save(BehaviorTree tree) {
Path overridesPath = PathManager.getInstance().getHomeModPath().resolve(BEHAVIORS.toString()).resolve("overrides");
savePath = overridesPath.resolve(uri.getModuleName().toString()).resolve("behaviors");
}
BehaviorTreeFormat loader = new BehaviorTreeFormat();
BehaviorTreeFormat loader = new BehaviorTreeFormat(context);
try {
Files.createDirectories(savePath);
Path file = savePath.resolve(uri.getResourceName() + ".behavior");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.logic.behavior.asset;

import com.google.common.base.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.engine.context.Context;
import org.terasology.engine.logic.behavior.core.BehaviorNode;
import org.terasology.engine.logic.behavior.core.BehaviorTreeBuilder;
import org.terasology.engine.registry.CoreRegistry;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.gestalt.assets.format.AbstractAssetFileFormat;
import org.terasology.gestalt.assets.format.AssetDataFile;
import org.terasology.gestalt.assets.module.annotations.RegisterAssetFileFormat;
Expand All @@ -29,15 +27,16 @@
*/
@RegisterAssetFileFormat
public class BehaviorTreeFormat extends AbstractAssetFileFormat<BehaviorTreeData> {
private static final Logger logger = LoggerFactory.getLogger(BehaviorTreeFormat.class);
private final Context context;


public BehaviorTreeFormat() {
public BehaviorTreeFormat(Context context) {
super("behavior");
this.context = context;
}

public void save(OutputStream stream, BehaviorTreeData data) throws IOException {
BehaviorTreeBuilder builder = CoreRegistry.get(BehaviorTreeBuilder.class);
BehaviorTreeBuilder builder = context.get(BehaviorTreeBuilder.class);
OutputStreamWriter writer = new OutputStreamWriter(stream, Charsets.UTF_8);
writer.write(builder.toJson(data.getRoot()));
writer.close();
Expand All @@ -46,10 +45,10 @@ public void save(OutputStream stream, BehaviorTreeData data) throws IOException

@Override
public BehaviorTreeData load(ResourceUrn resourceUrn, List<AssetDataFile> list) throws IOException {
BehaviorTreeBuilder builder = CoreRegistry.get(BehaviorTreeBuilder.class);
BehaviorTreeBuilder builder = context.getValue(BehaviorTreeBuilder.class);
if (builder == null) {
builder = new BehaviorTreeBuilder();
CoreRegistry.put(BehaviorTreeBuilder.class, builder);
builder = new BehaviorTreeBuilder(context);
context.put(BehaviorTreeBuilder.class, builder);
}
try (InputStream stream = list.get(0).openStream()) {
return load(stream);
Expand All @@ -58,11 +57,11 @@ public BehaviorTreeData load(ResourceUrn resourceUrn, List<AssetDataFile> list)


public BehaviorTreeData load(InputStream stream) {
BehaviorTreeBuilder builder = CoreRegistry.get(BehaviorTreeBuilder.class);
BehaviorTreeBuilder builder = context.getValue(BehaviorTreeBuilder.class);

if (builder == null) {
builder = new BehaviorTreeBuilder();
CoreRegistry.put(BehaviorTreeBuilder.class, builder);
builder = new BehaviorTreeBuilder(context);
context.put(BehaviorTreeBuilder.class, builder);
}
BehaviorNode node = builder.fromJson(stream);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.logic.behavior.core;

Expand All @@ -21,10 +21,10 @@
import org.apache.commons.codec.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.context.Context;
import org.terasology.engine.core.module.ModuleManager;
import org.terasology.engine.logic.behavior.BehaviorAction;
import org.terasology.engine.logic.behavior.asset.BehaviorTree;
import org.terasology.engine.registry.CoreRegistry;
import org.terasology.engine.registry.InjectionHelper;
import org.terasology.engine.utilities.gson.UriTypeAdapterFactory;
import org.terasology.gestalt.assets.ResourceUrn;
Expand Down Expand Up @@ -54,10 +54,12 @@ public class BehaviorTreeBuilder implements JsonDeserializer<BehaviorNode>, Json
private Gson gson;

private int nextId = 1;
private final Context context;

public BehaviorTreeBuilder() {
public BehaviorTreeBuilder(Context context) {
this.context = context;

ModuleManager moduleManager = CoreRegistry.get(ModuleManager.class);
ModuleManager moduleManager = context.getValue(ModuleManager.class);

if (moduleManager != null) {
ModuleEnvironment environment = moduleManager.getEnvironment();
Expand Down Expand Up @@ -127,7 +129,7 @@ public void write(JsonWriter out, BehaviorTree value) throws IOException {
@Override
public BehaviorTree read(JsonReader in) throws IOException {
String uri = in.nextString();
AssetManager assetManager = CoreRegistry.get(AssetManager.class);
AssetManager assetManager = context.getValue(AssetManager.class);
return assetManager.getAsset(new ResourceUrn(uri), BehaviorTree.class)
.orElse(assetManager.getAsset(new ResourceUrn("engine:default"), BehaviorTree.class).get());

Expand All @@ -138,37 +140,37 @@ public BehaviorTree read(JsonReader in) throws IOException {
}

@Override
public BehaviorNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
public BehaviorNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext jsonContext) throws JsonParseException {
BehaviorNode node;
if (json.isJsonPrimitive()) {
node = getPrimitiveNode(json, context);
node = getPrimitiveNode(json, jsonContext);
} else {
node = getCompositeNode(json, context);
node = getCompositeNode(json, jsonContext);
}
node = createNode(node);
return node;
}

@Override
public JsonElement serialize(BehaviorNode src, Type typeOfSrc, JsonSerializationContext context) {
public JsonElement serialize(BehaviorNode src, Type typeOfSrc, JsonSerializationContext jsonContext) {
JsonObject node = new JsonObject();
if (src instanceof DelegateNode) {
DelegateNode delegateNode = (DelegateNode) src;
return serialize(delegateNode.delegate, BehaviorNode.class, context);
return serialize(delegateNode.delegate, BehaviorNode.class, jsonContext);
}
if (src instanceof CompositeNode) {
String name = src.getName();
JsonArray array = new JsonArray();
for (int i = 0; i < src.getChildrenCount(); i++) {
array.add(serialize(src.getChild(i), BehaviorNode.class, context));
array.add(serialize(src.getChild(i), BehaviorNode.class, jsonContext));
}
node.add(name, array);
} else if (src instanceof ActionNode) {
ActionNode actionNode = (ActionNode) src;
JsonObject content;
String name;
if (actionNode.action != null) {
content = (JsonObject) context.serialize(actionNode.action);
content = (JsonObject) jsonContext.serialize(actionNode.action);
name = actionNode.action.getName();
} else {
content = new JsonObject();
Expand All @@ -178,7 +180,7 @@ public JsonElement serialize(BehaviorNode src, Type typeOfSrc, JsonSerialization
if (src instanceof DecoratorNode) {
DecoratorNode decoratorNode = (DecoratorNode) src;
if (decoratorNode.getChildrenCount() > 0) {
content.add("child", serialize(decoratorNode.getChild(0), BehaviorNode.class, context));
content.add("child", serialize(decoratorNode.getChild(0), BehaviorNode.class, jsonContext));
}
}

Expand All @@ -193,14 +195,14 @@ public BehaviorNode createNode(BehaviorNode node) {
return node;
}

private BehaviorNode getPrimitiveNode(JsonElement json, JsonDeserializationContext context) {
private BehaviorNode getPrimitiveNode(JsonElement json, JsonDeserializationContext jsonContext) {
String type = json.getAsString();
BehaviorNode node = createNode(type);
if (actions.containsKey(type)) {
Action action = context.deserialize(new JsonObject(), actions.get(type));
Action action = jsonContext.deserialize(new JsonObject(), actions.get(type));
addAction((ActionNode) node, action);
} else if (decorators.containsKey(type)) {
Action action = context.deserialize(new JsonObject(), decorators.get(type));
Action action = jsonContext.deserialize(new JsonObject(), decorators.get(type));
addAction((ActionNode) node, action);
}
return node;
Expand All @@ -210,11 +212,11 @@ private void addAction(ActionNode node, Action action) {
action.setId(nextId);
nextId++;
node.setAction(action);
InjectionHelper.inject(action);
InjectionHelper.inject(action, context);
action.setup();
}

private BehaviorNode getCompositeNode(JsonElement json, JsonDeserializationContext context) {
private BehaviorNode getCompositeNode(JsonElement json, JsonDeserializationContext jsonContext) {
String type;
JsonObject obj = json.getAsJsonObject();
Map.Entry<String, JsonElement> entry = obj.entrySet().iterator().next();
Expand All @@ -224,16 +226,16 @@ private BehaviorNode getCompositeNode(JsonElement json, JsonDeserializationConte
BehaviorNode node = createNode(type);

if (actions.containsKey(type)) {
Action action = context.deserialize(jsonElement, actions.get(type));
Action action = jsonContext.deserialize(jsonElement, actions.get(type));
addAction((ActionNode) node, action);
} else if (decorators.containsKey(type)) {
Action action = context.deserialize(jsonElement, decorators.get(type));
Action action = jsonContext.deserialize(jsonElement, decorators.get(type));
addAction((ActionNode) node, action);
JsonElement childJson = jsonElement.getAsJsonObject().get("child");
BehaviorNode child = context.deserialize(childJson, BehaviorNode.class);
BehaviorNode child = jsonContext.deserialize(childJson, BehaviorNode.class);
node.insertChild(0, child);
} else if (jsonElement.isJsonArray()) {
List<BehaviorNode> children = context.deserialize(jsonElement, new TypeToken<List<BehaviorNode>>() {
List<BehaviorNode> children = jsonContext.deserialize(jsonElement, new TypeToken<List<BehaviorNode>>() {
}.getType());
for (int i = 0; i < children.size(); i++) {
BehaviorNode child = children.get(i);
Expand Down
Loading

0 comments on commit a4e1b84

Please sign in to comment.