aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/loader/LilleroLoader.java
blob: 8999c49153f799b0d68191df44d4df47c1896127 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package ftbsc.lll.loader;

import cpw.mods.modlauncher.serviceapi.ILaunchPluginService;

import ftbsc.lll.IInjector;
import ftbsc.lll.exceptions.InjectionException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;

public class LilleroLoader implements ILaunchPluginService {
   private static final Logger LOGGER = LogManager.getLogger(LilleroLoader.class.getCanonicalName());
   private static final Marker INIT     = MarkerManager.getMarker("INIT");
   private static final Marker RESOURCE = MarkerManager.getMarker("RESOURCE");
   private static final Marker HANDLER  = MarkerManager.getMarker("HANDLER");
   private static final Marker PATCHER  = MarkerManager.getMarker("PATCHER");

   public static final String NAME = "lll-loader";

   private final Set<IInjector> injectors = new HashSet<>();
   private final Set<String> targetClasses = new HashSet<>();

   public LilleroLoader() {
      LOGGER.info(INIT, "Patch Loader initialized");
   }

   @Override
   public String name() {
      return NAME;
   }


   // Load mods requesting patches from resources

   @Override
   public void offerResource(Path resource, String name) {
      LOGGER.warn(RESOURCE, "Resource offered to us ({}@{}) but no action was taken", name, resource.toString());
   }

   @Override
   public void addResources(List<Map.Entry<String, Path>> resources) {
      LOGGER.debug(RESOURCE, "Resources being added:");
      for (Map.Entry<String, Path> row : resources) {
         LOGGER.debug(RESOURCE, "> {} ({})", row.getKey(), row.getValue().toString());
         try {
            URL jarUrl = new URL("file:" + row.getValue().toString());
            URLClassLoader loader = new URLClassLoader(new URL[] { jarUrl });
            for (IInjector inj : ServiceLoader.load(IInjector.class, loader)) {
               LOGGER.info(RESOURCE, "Registering injector {}", inj.name());
               this.injectors.add(inj);
               this.targetClasses.add(inj.targetClass());
            }
         } catch (MalformedURLException e) {
            LOGGER.error(RESOURCE, "Malformed URL for resource {} - 'file:{}'", row.getKey(), row.getValue().toString());
         }
      }
   }


   // Filter only classes we need to patch

   @Override
   public EnumSet<Phase> handlesClass(Type classType, final boolean isEmpty) {
      throw new IllegalStateException("Outdated ModLauncher"); //mixin does it
   }

   private static final EnumSet<Phase> YAY = EnumSet.of(Phase.BEFORE);
   private static final EnumSet<Phase> NAY = EnumSet.noneOf(Phase.class);

   @Override
   public EnumSet<Phase> handlesClass(Type classType, final boolean isEmpty, final String reason) {
      if (isEmpty) return NAY;
      LOGGER.debug(HANDLER, "Inspecting class {}", classType.getClassName());
      if(targetClasses.contains(classType.getClassName())) {
         LOGGER.info(HANDLER, "Marked class {} as handled by {}", classType.getClassName(), LilleroLoader.NAME);
         return YAY;
      }
      return NAY;
   }


   // Process classes and inject methods

   @Override
   public int processClassWithFlags(Phase phase, ClassNode classNode, Type classType, String reason) {
      LOGGER.debug(PATCHER, "Processing class {} in phase {} of {}", classType.getClassName(), phase.name(), reason);
      List<IInjector> relevantInjectors = this.injectors.stream()
         .filter(i -> i.targetClass().equals(classType.getClassName()))
         .collect(Collectors.toList());
      boolean modified = false;
      for (MethodNode method : classNode.methods) {
         for (IInjector inj : relevantInjectors) {
            if (
               inj.methodName().equals(method.name) &&
               inj.methodDesc().equals(method.desc)
            ) {
               LOGGER.info(PATCHER, "Patching {}.{} with {} ({})", classType.getClassName(), method.name, inj.name(), inj.reason());
               try {
                  inj.inject(classNode, method);
                  modified = true;
               } catch (InjectionException e) {
                  LOGGER.error(PATCHER, "Error applying patch '{}' : {}", inj.name(), e.toString());
               }
            }
         }
      }

      return modified ? ComputeFlags.COMPUTE_FRAMES | ComputeFlags.COMPUTE_MAXS : ComputeFlags.NO_REWRITE;
   }
}