|
| 1 | +/* |
| 2 | + * FastBack - Fast, incremental Minecraft backups powered by Git. |
| 3 | + * Copyright (C) 2022 pcal.net |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU General Public License |
| 7 | + * as published by the Free Software Foundation; either version 2 |
| 8 | + * of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package net.pcal.fastback.mod.neoforge.mixins; |
| 19 | + |
| 20 | +import net.minecraft.server.MinecraftServer; |
| 21 | +import net.pcal.fastback.mod.neoforge.MixinGateway; |
| 22 | +import org.spongepowered.asm.mixin.Mixin; |
| 23 | +import org.spongepowered.asm.mixin.injection.At; |
| 24 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 25 | +import org.spongepowered.asm.mixin.injection.Redirect; |
| 26 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 27 | + |
| 28 | +import static net.pcal.fastback.logging.SystemLogger.syslog; |
| 29 | + |
| 30 | +/** |
| 31 | + * Allows us to disable vanilla saving during 'git add' to avoid coherency problems in the backup snapshots. Also |
| 32 | + * sends notifications when autosaving completes so we can follow them with automated backups. |
| 33 | + * |
| 34 | + * @author pcal |
| 35 | + * @since 0.0.1 |
| 36 | + */ |
| 37 | +@Mixin(MinecraftServer.class) |
| 38 | +public class MinecraftServerMixin { |
| 39 | + |
| 40 | + /** |
| 41 | + * Intercept the call to saveAll that triggers on autosave, pass it through and then send out notification that |
| 42 | + * the autosave is done. |
| 43 | + */ |
| 44 | + @Redirect(method = "autoSave()V", |
| 45 | + at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;saveEverything(ZZZ)Z")) |
| 46 | + public boolean fastback_saveAll(MinecraftServer instance, boolean suppressLogs, boolean flush, boolean force) { |
| 47 | + boolean result = instance.saveEverything(suppressLogs, flush, force); |
| 48 | + MixinGateway.get().autoSaveCompleted(); |
| 49 | + return result; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Intercept save so we can hard-disable saving during critical parts of the backup. |
| 54 | + */ |
| 55 | + @Inject(at = @At("HEAD"), method = "saveAllChunks(ZZZ)Z", cancellable = true) |
| 56 | + public void fastback_save(boolean suppressLogs, boolean flush, boolean force, CallbackInfoReturnable<Boolean> ci) { |
| 57 | + synchronized (this) { |
| 58 | + if (MixinGateway.get().isWorldSaveEnabled()) { |
| 59 | + syslog().debug("world saves are enabled, doing requested save"); |
| 60 | + } else { |
| 61 | + syslog().warn("Skipping requested save because a backup is in progress."); |
| 62 | + ci.setReturnValue(false); |
| 63 | + ci.cancel(); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Intercept saveAll so we can hard-disable saving during critical parts of the backup. |
| 70 | + */ |
| 71 | + @Inject(at = @At("HEAD"), method = "saveEverything(ZZZ)Z", cancellable = true) |
| 72 | + public void fastback_saveAll(boolean suppressLogs, boolean flush, boolean force, CallbackInfoReturnable<Boolean> ci) { |
| 73 | + synchronized (this) { |
| 74 | + if (MixinGateway.get().isWorldSaveEnabled()) { |
| 75 | + syslog().debug("world saves are enabled, doing requested saveAll"); |
| 76 | + //TODO should call save here to ensure all synced? |
| 77 | + } else { |
| 78 | + syslog().warn("Skipping requested saveAll because a backup is in progress."); |
| 79 | + ci.setReturnValue(false); |
| 80 | + ci.cancel(); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments