| 1 | package net.minecraft.client.telemetry;
|
| 2 |
|
| 3 | import com.google.common.base.Suppliers;
|
| 4 | import com.mojang.authlib.minecraft.TelemetrySession;
|
| 5 | import com.mojang.authlib.minecraft.UserApiService;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.time.Duration;
|
| 8 | import java.time.Instant;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.UUID;
|
| 11 | import java.util.concurrent.CompletableFuture;
|
| 12 | import java.util.concurrent.Executor;
|
| 13 | import java.util.concurrent.Executors;
|
| 14 | import java.util.concurrent.atomic.AtomicInteger;
|
| 15 | import java.util.function.Supplier;
|
| 16 | import net.fabricmc.api.EnvType;
|
| 17 | import net.fabricmc.api.Environment;
|
| 18 | import net.minecraft.SharedConstants;
|
| 19 | import net.minecraft.client.Minecraft;
|
| 20 | import net.minecraft.client.User;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class ClientTelemetryManager implements AutoCloseable {
|
| 26 | private static final AtomicInteger THREAD_COUNT = new AtomicInteger(1);
|
| 27 | private static final Executor EXECUTOR = Executors.newSingleThreadExecutor(r -> {
|
| 28 | Thread result = new Thread(r, "Telemetry-Sender-#" + THREAD_COUNT.getAndIncrement());
|
| 29 | result.setDaemon(true);
|
| 30 | return result;
|
| 31 | });
|
| 32 | private final Minecraft minecraft;
|
| 33 | private final UserApiService userApiService;
|
| 34 | private final TelemetryPropertyMap deviceSessionProperties;
|
| 35 | private final Path logDirectory;
|
| 36 | private final CompletableFuture<Optional<TelemetryLogManager>> logManager;
|
| 37 | private final Supplier<TelemetryEventSender> outsideSessionSender = Suppliers.memoize(this::createEventSender);
|
| 38 |
|
| 39 | public ClientTelemetryManager(final Minecraft minecraft, final UserApiService userApiService, final User user) {
|
| 40 | this.minecraft = minecraft;
|
| 41 | this.userApiService = userApiService;
|
| 42 | TelemetryPropertyMap.Builder properties = TelemetryPropertyMap.builder();
|
| 43 | user.getXuid().ifPresent(id -> properties.put(TelemetryProperty.USER_ID, id));
|
| 44 | user.getClientId().ifPresent(id -> properties.put(TelemetryProperty.CLIENT_ID, id));
|
| 45 | properties.put(TelemetryProperty.MINECRAFT_SESSION_ID, UUID.randomUUID());
|
| 46 | properties.put(TelemetryProperty.GAME_VERSION, SharedConstants.getCurrentVersion().id());
|
| 47 | properties.put(TelemetryProperty.OPERATING_SYSTEM, Util.getPlatform().telemetryName());
|
| 48 | properties.put(TelemetryProperty.PLATFORM, System.getProperty("os.name"));
|
| 49 | properties.put(TelemetryProperty.CLIENT_MODDED, Minecraft.checkModStatus().shouldReportAsModified());
|
| 50 | properties.putIfNotNull(TelemetryProperty.LAUNCHER_NAME, Minecraft.getLauncherBrand());
|
| 51 | this.deviceSessionProperties = properties.build();
|
| 52 | this.logDirectory = minecraft.gameDirectory.toPath().resolve("logs/telemetry");
|
| 53 | this.logManager = TelemetryLogManager.open(this.logDirectory);
|
| 54 | }
|
| 55 |
|
| 56 | public WorldSessionTelemetryManager createWorldSessionManager(
|
| 57 | final boolean newWorld, final @Nullable Duration worldLoadDuration, final @Nullable String minigameName, final UUID sessionId
|
| 58 | ) {
|
| 59 | return new WorldSessionTelemetryManager(this.createEventSender(), newWorld, worldLoadDuration, minigameName, sessionId);
|
| 60 | }
|
| 61 |
|
| 62 | public TelemetryEventSender getOutsideSessionSender() {
|
| 63 | return this.outsideSessionSender.get();
|
| 64 | }
|
| 65 |
|
| 66 | private TelemetryEventSender createEventSender() {
|
| 67 | if (!this.minecraft.allowsTelemetry()) {
|
| 68 | return TelemetryEventSender.DISABLED;
|
| 69 | }
|
| 70 |
|
| 71 | TelemetrySession telemetrySession = this.userApiService.newTelemetrySession(EXECUTOR);
|
| 72 | if (!telemetrySession.isEnabled()) {
|
| 73 | return TelemetryEventSender.DISABLED;
|
| 74 | }
|
| 75 |
|
| 76 | CompletableFuture<Optional<TelemetryEventLogger>> loggerFuture = this.logManager
|
| 77 | .thenCompose(manager -> manager.map(TelemetryLogManager::openLogger).orElseGet(() -> CompletableFuture.completedFuture(Optional.empty())));
|
| 78 | return (type, buildFunction) -> {
|
| 79 | if (!type.isOptIn() || Minecraft.getInstance().telemetryOptInExtra()) {
|
| 80 | TelemetryPropertyMap.Builder properties = TelemetryPropertyMap.builder();
|
| 81 | properties.putAll(this.deviceSessionProperties);
|
| 82 | properties.put(TelemetryProperty.EVENT_TIMESTAMP_UTC, Instant.now());
|
| 83 | properties.put(TelemetryProperty.OPT_IN, type.isOptIn());
|
| 84 | buildFunction.accept(properties);
|
| 85 | TelemetryEventInstance event = new TelemetryEventInstance(type, properties.build());
|
| 86 | loggerFuture.thenAccept(logger -> {
|
| 87 | if (!logger.isEmpty()) {
|
| 88 | logger.get().log(event);
|
| 89 | if (!SharedConstants.IS_RUNNING_IN_IDE || !SharedConstants.DEBUG_DONT_SEND_TELEMETRY_TO_BACKEND) {
|
| 90 | event.export(telemetrySession).send();
|
| 91 | }
|
| 92 | }
|
| 93 | });
|
| 94 | }
|
| 95 | };
|
| 96 | }
|
| 97 |
|
| 98 | public Path getLogDirectory() {
|
| 99 | return this.logDirectory;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public void close() {
|
| 104 | this.logManager.thenAccept(manager -> manager.ifPresent(TelemetryLogManager::close));
|
| 105 | }
|
| 106 | }
|
| 107 |
|