Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. package com.artemis;
  2.  
  3. import java.util.HashMap;
  4.  
  5. /**
  6. * @author Daan van Yperen
  7. */
  8. public class DebugWorld extends World {
  9. final DebugLogStrategy logStrategy;
  10. final HashMap<Integer, DebugComponent> debugComponents;
  11.  
  12. public DebugWorld(WorldConfiguration configuration, DebugLogStrategy logStrategy, HashMap<Integer, DebugComponent> debugComponents) {
  13. super(injectDebugSystems(configuration, logStrategy, this.debugComponents = debugComponents));
  14. this.debugComponents = debugComponents;
  15. this.logStrategy = logStrategy;
  16. }
  17.  
  18. private static WorldConfiguration injectDebugSystems(WorldConfiguration configuration, DebugLogStrategy logStrategy, HashMap<Integer, DebugComponent> debugComponents) {
  19. configuration.systems.set(WorldConfiguration.ENTITY_MANAGER_IDX, new DebugEntityManager(configuration.expectedEntityCount(), logStrategy, debugComponents));
  20. return configuration;
  21. }
  22.  
  23. public DebugWorld(WorldConfiguration configuration, DebugLogStrategy logStrategy) {
  24. this(configuration, logStrategy, new HashMap<>());
  25. }
  26.  
  27. public DebugWorld(WorldConfiguration build) {
  28. this(build, new DefaultDebugLogStrategy(), new HashMap<>());
  29. }
  30.  
  31. public DebugWorld() {
  32. this(new WorldConfiguration(), new DefaultDebugLogStrategy(), new HashMap<>());
  33. }
  34.  
  35. @Override
  36. public void delete(int entityId) {
  37. DebugComponent debugComponent = debugComponents.get(entityId);
  38. if (debugComponent.deletionStacktrace != null) {
  39. logStrategy.log("*********************");
  40. logStrategy.log(new MutationStacktrace(MutationStacktrace.Type.ERROR_ATTEMPT_TO_DELETE_DELETED_ENTITY, entityId, debugComponent.name, Thread.currentThread().getStackTrace()));
  41. logStrategy.log("Cause (Already deleted at):");
  42. logStrategy.log(debugComponent.deletionStacktrace);
  43. logStrategy.log("*********************");
  44.  
  45. }
  46. debugComponent.deletionStacktrace = new MutationStacktrace(MutationStacktrace.Type.DELETE, entityId, debugComponent.name, Thread.currentThread().getStackTrace());
  47. logStrategy.log(debugComponent.deletionStacktrace);
  48. super.delete(entityId);
  49. }
  50.  
  51. public interface DebugLogStrategy {
  52. void log(String s);
  53.  
  54. void log(MutationStacktrace site);
  55. }
  56.  
  57. public static class DefaultDebugLogStrategy implements DebugLogStrategy {
  58.  
  59. private final String[] packages;
  60.  
  61. public DefaultDebugLogStrategy(String... packages) {
  62. this.packages = packages;
  63. }
  64.  
  65. @Override
  66. public void log(String s) {
  67. System.out.println(s);
  68. }
  69.  
  70. @Override
  71. public void log(MutationStacktrace site) {
  72. boolean matched = false;
  73. String prefix = site.entityDebugName + "(" + site.entityId + ") " + site.type;
  74.  
  75. for (StackTraceElement stackTraceElement : site.stacktrace) {
  76. String callsiteSummary = stackTraceElement.toString();
  77. for (String includePackage : packages) {
  78. if (callsiteSummary.contains(includePackage)) {
  79. // we found a first match! keep reporting as long as we are inside the reported package scope
  80. // because we might be inside utility logic and more elements are needed to provide context.
  81. if (!matched) {
  82. // first line
  83. log(prefix + " @ " + callsiteSummary);
  84. } else {
  85. // beyond first line.
  86. log(" .. " + callsiteSummary);
  87. }
  88. matched = true;
  89. } else if (matched) {
  90. // no longer matching packages, so done.
  91. return;
  92. }
  93. }
  94. }
  95. if (!matched) {
  96. log(prefix + " @ excluded package.");
  97. }
  98. }
  99. }
  100.  
  101.  
  102. public static class DebugEntityManager extends EntityManager {
  103. private final DebugLogStrategy logStrategy;
  104. private final HashMap<Integer, DebugComponent> debugComponents;
  105.  
  106. protected DebugEntityManager(int initialContainerSize, DebugLogStrategy logStrategy, HashMap<Integer, DebugComponent> debugComponents) {
  107. super(initialContainerSize);
  108. this.logStrategy = logStrategy;
  109. this.debugComponents = debugComponents;
  110. }
  111.  
  112. protected Entity createEntityInstance() {
  113. Entity result = super.createEntityInstance();
  114. beginTracking(result.id);
  115. return result;
  116. }
  117.  
  118. @Override
  119. protected int create() {
  120. int entityId = super.create();
  121. beginTracking(entityId);
  122. return entityId;
  123. }
  124.  
  125. @Override
  126. protected Entity getEntity(int entityId) {
  127. DebugComponent debugComponent = debugComponents.get(entityId);
  128. if (debugComponent != null && debugComponent.deletionStacktrace != null) {
  129. logStrategy.log("*********************");
  130. logStrategy.log(new MutationStacktrace(MutationStacktrace.Type.ERROR_ATTEMPT_TO_ACCESS_DELETED_ENTITY, entityId, debugComponent.name, Thread.currentThread().getStackTrace()));
  131. logStrategy.log("Cause (Deleted at):");
  132. logStrategy.log(debugComponent.deletionStacktrace);
  133. logStrategy.log("*********************");
  134. }
  135. return super.getEntity(entityId);
  136. }
  137.  
  138. private void beginTracking(int entityId) {
  139. // create component for user debug sake.
  140. debugComponents.remove(entityId);
  141. DebugComponent debugComponent = getEntity(entityId).edit().create(DebugComponent.class);
  142. debugComponent.creationStacktrace = new MutationStacktrace(MutationStacktrace.Type.CREATE, entityId, debugComponent.name, Thread.currentThread().getStackTrace());
  143.  
  144. // the world lifecycle is delayed such that we need our own way to resolve debug components in case of create/destroy within the same system;
  145. debugComponents.put(entityId, debugComponent);
  146.  
  147. logStrategy.log(debugComponent.creationStacktrace);
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement