|
30 | 30 | import java.util.function.BiPredicate; |
31 | 31 | import java.util.function.Function; |
32 | 32 | import java.util.stream.Collectors; |
33 | | -import java.util.stream.StreamSupport; |
34 | 33 |
|
35 | 34 | import net.minecraftforge.fml.network.NetworkRegistry; |
36 | 35 | import org.apache.commons.lang3.tuple.Pair; |
@@ -137,67 +136,97 @@ private List<String> validateChannels(final Function<Identifier, String> incomin |
137 | 136 | final String incomingVersion = incoming.apply(channel.getChannelName()); |
138 | 137 | final boolean accepted = predicate.test(channel, incomingVersion != null ? incomingVersion : NetworkRegistry.ABSENT); |
139 | 138 |
|
140 | | - if (origin == Origin.VANILLA) { |
141 | | - LOGGER.debug(NETREGISTRY, "Channel '{}' : Vanilla acceptance test: {}", channel.getChannelName(), accepted ? "ACCEPTED" : "REJECTED"); |
142 | | - } else { |
143 | | - LOGGER.debug(NETREGISTRY, "Channel '{}' : Version test of '{}' from {} : {}", channel.getChannelName(), incomingVersion, origin, accepted ? "ACCEPTED" : "REJECTED"); |
144 | | - } |
145 | | - |
146 | 139 | if (!accepted) { |
147 | 140 | rejected.add(channel.getChannelName().toString()); |
148 | 141 | } |
| 142 | + |
| 143 | + final String status = accepted ? "ACCEPTED" : "REJECTED"; |
| 144 | + |
| 145 | + switch (origin) { |
| 146 | + case VANILLA: |
| 147 | + LOGGER.debug(NETREGISTRY, "Channel '{}' : Vanilla acceptance test: {}", channel.getChannelName(), status); |
| 148 | + break; |
| 149 | + case PING: |
| 150 | + LOGGER.debug(NETREGISTRY, "Channel '{}' : Version test of '{}' during listping : {}", channel.getChannelName(), incomingVersion, status); |
| 151 | + break; |
| 152 | + case SERVER: |
| 153 | + case CLIENT: |
| 154 | + LOGGER.debug(NETREGISTRY, "Channel '{}' : Version test of '{}' from {} : {}", channel.getChannelName(), incomingVersion, origin, status); |
| 155 | + } |
149 | 156 | } |
150 | 157 |
|
151 | 158 | if (!rejected.isEmpty()) { |
152 | | - if (origin == Origin.VANILLA) { |
| 159 | + switch (origin) { |
| 160 | + case VANILLA: |
153 | 161 | LOGGER.error(NETREGISTRY, "Channels {} rejected vanilla connections", rejected); |
154 | | - } else { |
| 162 | + break; |
| 163 | + case PING: |
| 164 | + LOGGER.error(NETREGISTRY, "Channels {} rejected their server side version number during listping", rejected); |
| 165 | + break; |
| 166 | + case SERVER: |
| 167 | + case CLIENT: |
155 | 168 | LOGGER.error(NETREGISTRY, "Channels {} rejected their {} version number", rejected, origin); |
156 | 169 | } |
157 | 170 |
|
158 | 171 | return rejected; |
159 | 172 | } |
160 | 173 |
|
161 | | - LOGGER.debug(NETREGISTRY, "Accepting channel list from {}", origin); |
| 174 | + if (origin != Origin.PING) { |
| 175 | + LOGGER.debug(NETREGISTRY, "Accepting channel list from {}", origin); |
| 176 | + } |
162 | 177 |
|
163 | 178 | return Collections.emptyList(); |
164 | 179 | } |
165 | 180 |
|
166 | 181 | public boolean checkListPingCompatibilityForClient(Map<Identifier, Pair<String, Boolean>> incoming) { |
167 | | - Set<Identifier> handled = new HashSet<>(); |
168 | | - final List<Pair<Identifier, Boolean>> results = StreamSupport.stream(channels.spliterator(), false) |
169 | | - .filter(p -> !p.getChannelName().getNamespace().equals("fml")) |
170 | | - .map(ni -> { |
171 | | - final Pair<String, Boolean> incomingVersion = incoming.getOrDefault(ni.getChannelName(), Pair.of(ABSENT, true)); |
172 | | - final boolean test = ni.tryServerVersionOnClient(incomingVersion.getLeft()); |
173 | | - handled.add(ni.getChannelName()); |
174 | | - LOGGER.debug(NETREGISTRY, "Channel '{}' : Version test of '{}' during listping : {}", ni.getChannelName(), incomingVersion, test ? "ACCEPTED" : "REJECTED"); |
175 | | - return Pair.of(ni.getChannelName(), test); |
176 | | - }).filter(p -> !p.getRight()).collect(Collectors.toList()); |
177 | | - final List<Identifier> missingButRequired = incoming.entrySet().stream() |
178 | | - .filter(p -> !p.getKey().getNamespace().equals("fml")) |
179 | | - .filter(p -> !p.getValue().getRight()) |
180 | | - .filter(p -> !handled.contains(p.getKey())) |
181 | | - .map(Map.Entry::getKey) |
182 | | - .collect(Collectors.toList()); |
183 | | - |
184 | | - if (!results.isEmpty()) { |
185 | | - LOGGER.error(NETREGISTRY, "Channels [{}] rejected their server side version number during listping", |
186 | | - results.stream().map(Pair::getLeft).map(Object::toString).collect(Collectors.joining(","))); |
| 182 | + // TODO: don't mutate map here, modify the call site instead |
| 183 | + incoming.keySet().removeIf(name -> name.getNamespace().equals("fml")); |
| 184 | + |
| 185 | + List<String> rejected = validateChannels(identifier -> { |
| 186 | + Pair<String, Boolean> entry = incoming.get(identifier); |
| 187 | + |
| 188 | + return entry != null ? entry.getLeft() : null; |
| 189 | + }, Origin.PING, VersionedChannel::tryServerVersionOnClient); |
| 190 | + |
| 191 | + if (!rejected.isEmpty()) { |
187 | 192 | return false; |
188 | 193 | } |
189 | 194 |
|
| 195 | + Set<Identifier> handled = new HashSet<>(); |
| 196 | + List<Identifier> missingButRequired = new ArrayList<>(); |
| 197 | + |
| 198 | + for (NamedChannel channel: channels) { |
| 199 | + handled.add(channel.getChannelName()); |
| 200 | + } |
| 201 | + |
| 202 | + for (Map.Entry<Identifier, Pair<String, Boolean>> entry : incoming.entrySet()) { |
| 203 | + Identifier channelName = entry.getKey(); |
| 204 | + boolean required = entry.getValue().getRight(); |
| 205 | + |
| 206 | + if (!required || handled.contains(channelName)) { |
| 207 | + continue; |
| 208 | + } |
| 209 | + |
| 210 | + missingButRequired.add(channelName); |
| 211 | + } |
| 212 | + |
190 | 213 | if (!missingButRequired.isEmpty()) { |
191 | | - LOGGER.error(NETREGISTRY, "The server is likely to require channel [{}] to be present, yet we don't have it", |
| 214 | + LOGGER.error(NETREGISTRY, "The server is likely to require the channels {} to be present, yet we don't have them", |
192 | 215 | missingButRequired); |
193 | 216 | return false; |
194 | 217 | } |
195 | 218 |
|
196 | 219 | LOGGER.debug(NETREGISTRY, "Accepting channel list during listping"); |
| 220 | + |
197 | 221 | return true; |
198 | 222 | } |
199 | 223 |
|
200 | 224 | public enum Origin { |
201 | | - CLIENT, SERVER, VANILLA |
| 225 | + CLIENT, SERVER, VANILLA, |
| 226 | + |
| 227 | + /** |
| 228 | + * Server list ping response from server. |
| 229 | + */ |
| 230 | + PING |
202 | 231 | } |
203 | 232 | } |
0 commit comments