InjectionStrategy.java
3.84 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.launch;
/**
* Encapsulates a strategy for injecting a URL into the classpath
*
* @author Adam Mummery-Smith
*/
public final class InjectionStrategy
{
/**
* Defines a position for a classpath injection strategy
*
* @author Adam Mummery-Smith
*/
public enum InjectionPosition
{
/**
* Inject the URL at the bottom (end) of the classpath, lowest priority
* - this is the default.
*/
Bottom,
/**
* Inject the URL at the base of the classpath (directly above the
* minecraft jar but below all other libs).
*/
Base,
/**
* Inject the URL at the top (start) of the classpath, highest priority
* above all other libs.
*/
Top,
/**
* Inject the URL above the entry which matches the URL defined by param
*/
Above;
/**
* Parse an InjectionPosition from the specified "injectAt" string
*
* @param injectAt
*/
public static InjectionPosition parsePosition(String injectAt)
{
if ("top".equalsIgnoreCase(injectAt)) return InjectionPosition.Top;
if ("base".equalsIgnoreCase(injectAt)) return InjectionPosition.Base;
if (injectAt != null && injectAt.toLowerCase().startsWith("above:")) return InjectionPosition.Above;
return InjectionPosition.Bottom;
}
/**
* Parse InjectionPosition params from the specified "injectAt" string
*
* @param injectAt
*/
public String[] parseParams(String injectAt)
{
if (this == InjectionPosition.Above && injectAt != null) return injectAt.substring(6).split(",");
return null;
}
}
/**
* Top strategy
*/
public static final InjectionStrategy TOP = new InjectionStrategy(InjectionPosition.Top, null);
/**
* Default strategy
*/
public static final InjectionStrategy DEFAULT = new InjectionStrategy(InjectionPosition.Bottom, null);
/**
* Position for this strategy
*/
private final InjectionPosition position;
/**
* Params for the strategy (if supported by the specified position)
*/
private final String[] params;
/**
* Private constructor because strategy should be created from a string
* using parseStrategy()
*
* @param injectAt
*/
private InjectionStrategy(String injectAt)
{
this.position = InjectionPosition.parsePosition(injectAt);
this.params = this.position.parseParams(injectAt);
}
/**
* Private constructor for the pre-defined public strategies TOP and DEFAULT
*
* @param position
* @param params
*/
private InjectionStrategy(InjectionPosition position, String[] params)
{
this.position = position;
this.params = params;
}
/**
* Get the position
*/
public InjectionPosition getPosition()
{
return this.position;
}
/**
* Get the parameters
*/
public String[] getParams()
{
return this.params;
}
/**
* Parse an injection strategy from the specified injectAt string
*
* @param injectAt
*/
public static InjectionStrategy parseStrategy(String injectAt)
{
return InjectionStrategy.parseStrategy(injectAt, null);
}
/**
* Parse an injection strategy from the specified injectAt string
*
* @param injectAt
*/
public static InjectionStrategy parseStrategy(String injectAt, InjectionStrategy defaultStrategy)
{
if (injectAt == null) return defaultStrategy;
return new InjectionStrategy(injectAt);
}
}