summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/ui/KBCommands.java
blob: 22f8c658167a43068eb7a81638a1705f146c7caa (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package com.keuin.kbackupfabric.ui;

import com.keuin.kbackupfabric.backup.BackupFilesystemUtil;
import com.keuin.kbackupfabric.backup.name.IncrementalBackupFileNameEncoder;
import com.keuin.kbackupfabric.backup.name.PrimitiveBackupFileNameEncoder;
import com.keuin.kbackupfabric.backup.suggestion.BackupNameSuggestionProvider;
import com.keuin.kbackupfabric.metadata.MetadataHolder;
import com.keuin.kbackupfabric.operation.BackupOperation;
import com.keuin.kbackupfabric.operation.DeleteOperation;
import com.keuin.kbackupfabric.operation.RestoreOperation;
import com.keuin.kbackupfabric.operation.abstracts.i.Invokable;
import com.keuin.kbackupfabric.operation.backup.method.ConfiguredBackupMethod;
import com.keuin.kbackupfabric.operation.backup.method.ConfiguredIncrementalBackupMethod;
import com.keuin.kbackupfabric.operation.backup.method.ConfiguredPrimitiveBackupMethod;
import com.keuin.kbackupfabric.util.DateUtil;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static com.keuin.kbackupfabric.backup.BackupFilesystemUtil.*;
import static com.keuin.kbackupfabric.util.PrintUtil.*;

public final class KBCommands {


    private static final int SUCCESS = 1;
    private static final int FAILED = -1;
    private static final String DEFAULT_BACKUP_NAME = "noname";
    private static boolean notifiedPreviousRestoration = false;

    // don't access it directly
    private static MinecraftServer server;
    private static BackupManager backupManager;
    private static final Object managerCreatorLock = new Object();

    //private static final Logger LOGGER = LogManager.getLogger();

    private static final List<BackupInfo> backupList = new ArrayList<>(); // index -> backupName
    private static Invokable pendingOperation = null;
    //private static BackupMethod activatedBackupMethod = new PrimitiveBackupMethod(); // The backup method we currently using

    public static void setServer(MinecraftServer server) {
        KBCommands.server = server;
    }

    private static MinecraftServer getServer() {
        if (server != null)
            return server;
        throw new IllegalStateException("server is not initialized.");
    }

    private static BackupManager getBackupManager() {
        synchronized (managerCreatorLock) {
            if (backupManager == null)
                backupManager = new BackupManager(getBackupSaveDirectory(getServer()));
            return backupManager;
        }
    }

    /**
     * Print the help menu.
     *
     * @param context the context.
     * @return stat code.
     */
    public static int help(CommandContext<ServerCommandSource> context) {
        msgInfo(context, "==== KBackup Manual ====");
        msgInfo(context, "/kb , /kb help - Print help menu.");
        msgInfo(context, "/kb list - Show all backups.");
        msgInfo(context, "/kb backup [incremental/zip] [backup_name] - Backup the whole level to backup_name. The default name is current system time.");
        msgInfo(context, "/kb restore <backup_name> - Delete the whole current level and restore from given backup. /kb restore is identical with /kb list.");
        msgInfo(context, "/kb confirm - Confirm and start restoring.");
        msgInfo(context, "/kb cancel - Cancel the restoration to be confirmed. If cancelled, /kb confirm will not run.");
        return SUCCESS;
    }

    /**
     * Print the help menu. (May show extra info during the first run after restoring)
     *
     * @param context the context.
     * @return stat code.
     */
    public static int kb(CommandContext<ServerCommandSource> context) {
        int statCode = list(context);
        if (MetadataHolder.hasMetadata() && !notifiedPreviousRestoration) {
            // Output metadata info
            notifiedPreviousRestoration = true;
            msgStress(context, "Restored from backup "
                    + MetadataHolder.getMetadata().getBackupName() + " (created at " +
                    DateUtil.fromEpochMillis(MetadataHolder.getMetadata().getBackupTime())
                    + ")");
        }
        return statCode;
    }

    private static void updateBackupList() {
        synchronized (backupList) {
            backupList.clear();
            List<BackupInfo> list = new ArrayList<>();
            getBackupManager().getAllBackups().forEach(list::add);
            list.sort(Comparator.comparing(BackupInfo::getCreationTime).reversed());
            backupList.addAll(list);
        }
    }

    /**
     * List all existing backups.
     *
     * @param context the context.
     * @return stat code.
     */
    public static int list(CommandContext<ServerCommandSource> context) {
        // lazy: it just works as expected. Don't try to refactor, it's a waste of time. Just improve display and
        //       that's enough.
        // TODO: Show real name and size and etc info for incremental backup
        // TODO: Show concrete info from metadata for `.zip` backup
//        MinecraftServer server = context.getSource().getMinecraftServer();
        // TODO: refactor this to use {@link ObjectCollectionSerializer#fromDirectory}
//        File[] files = getBackupSaveDirectory(server).listFiles(
//                (dir, name) -> dir.isDirectory() &&
//                        (name.toLowerCase().endsWith(".zip") && name.toLowerCase().startsWith(getBackupFileNamePrefix())
//                                || name.toLowerCase().endsWith(".kbi"))
//        );

//        Function<File, String> backupInformationProvider = file -> {
//            Objects.requireNonNull(file);
//            if (file.getName().toLowerCase().endsWith(".zip"))
//                return getPrimitiveBackupInformationString(file.getName(), file.length());
//                // TODO: refactor this to use {@link ObjectCollectionSerializer#fromDirectory}
//            else if (file.getName().toLowerCase().endsWith(".kbi"))
//                return getIncrementalBackupInformationString(file);
//            return file.getName();
//        };

        updateBackupList();
        synchronized (backupList) {
            if (backupList.isEmpty())
                msgInfo(context, "There is no backup available. To make a new backup, use `/kb backup`.");
            else
                msgInfo(context, "Available backups:");
            for (int i = backupList.size() - 1; i >= 0; --i) {
                BackupInfo info = backupList.get(i);
                printBackupInfo(context, info, i);
            }
//            if (files != null) {
//                if (files.length != 0) {
//                    msgInfo(context, "Available backups: (file is not checked, manipulation may affect this plugin)");
//                } else {
//                    msgInfo(context, "There are no available backups. To make a new backup, run /kb backup.");
//                }
//                int i = 0;
//                for (File file : files) {
//                    ++i;
//                    String backupFileName = file.getName();
//                    msgInfo(context, String.format("[%d] %s", i, backupInformationProvider.apply(file)));
//                    backupFileNameList.add(backupFileName);
//                }
//            } else {
//                msgErr(context, "Error: failed to list files in backup folder.");
//            }
        }
        return SUCCESS;
    }

    /**
     * Print backup information.
     *
     * @param context the context.
     * @param info    the info.
     * @param i       the index, starting from 0.
     */
    private static void printBackupInfo(CommandContext<ServerCommandSource> context, BackupInfo info, int i) {
        msgInfo(context, String.format(
                "[%d] (%s) %s (%s) %s",
                i + 1,
                info.getType(),
                info.getName(),
                DateUtil.getPrettyString(info.getCreationTime()),
                (info.getSizeBytes() > 0) ? BackupFilesystemUtil.getFriendlyFileSizeString(info.getSizeBytes()) : ""
        ));
    }

    /**
     * Backup with context parameter backupName.
     *
     * @param context the context.
     * @return stat code.
     */
    public static int primitiveBackup(CommandContext<ServerCommandSource> context) {
        //KBMain.backup("name")
        String customBackupName = StringArgumentType.getString(context, "backupName");
        if (customBackupName.matches("[0-9]*")) {
            // Numeric param is not allowed
            customBackupName = String.format("a%s", customBackupName);
            msgWarn(context, String.format("Pure numeric name is not allowed. Renaming to %s", customBackupName));
        }
        return doBackup(context, customBackupName, false);
    }

    /**
     * Backup with default name.
     *
     * @param context the context.
     * @return stat code.
     */
    public static int primitiveBackupWithDefaultName(CommandContext<ServerCommandSource> context) {
        return doBackup(context, DEFAULT_BACKUP_NAME, false);
    }

    public static int incrementalBackup(CommandContext<ServerCommandSource> context) {
        String customBackupName = StringArgumentType.getString(context, "backupName");
        if (customBackupName.matches("[0-9]*")) {
            // Numeric param is not allowed
            customBackupName = String.format("a%s", customBackupName);
            msgWarn(context, String.format("Pure numeric name is not allowed. Renaming to %s", customBackupName));
        }
        return doBackup(context, customBackupName, true);
    }

    public static int incrementalBackupWithDefaultName(CommandContext<ServerCommandSource> context) {
        return doBackup(context, DEFAULT_BACKUP_NAME, true);
    }


//    public static int incrementalBackup(CommandContext<ServerCommandSource> context) {
//        //KBMain.backup("name")
//        String backupName = StringArgumentType.getString(context, "backupName");
//        if (backupName.matches("[0-9]*")) {
//            // Numeric param is not allowed
//            backupName = String.format("a%s", backupName);
//            msgWarn(context, String.format("Pure numeric name is not allowed. Renaming to %s", backupName));
//        }
//        return doBackup(context, backupName, IncrementalBackupMethod.getInstance());
//    }
//
//    public static int incrementalBackupWithDefaultName(CommandContext<ServerCommandSource> context) {
//        return doBackup(context, DEFAULT_BACKUP_NAME, IncrementalBackupMethod.getInstance());
//    }

    /**
     * Delete an existing backup with context parameter backupName.
     * Simply set the pending backupName to given backupName, for the second confirmation.
     *
     * @param context the context.
     * @return stat code.
     */
    public static int delete(CommandContext<ServerCommandSource> context) {

        String backupFileName = parseBackupFileName(context, StringArgumentType.getString(context, "backupName"));
        MinecraftServer server = context.getSource().getMinecraftServer();

        if (backupFileName == null)
            return list(context); // Show the list and return

        // Validate backupName
        if (!isBackupFileExists(backupFileName, server)) {
            // Invalid backupName
            msgErr(context, "Invalid backup name! Please check your input. The list index number is also valid.");
            return FAILED;
        }

        // Update pending task
        //pendingOperation = AbstractConfirmableOperation.createDeleteOperation(context, backupName);
        pendingOperation = new DeleteOperation(context, backupFileName);

        msgWarn(context, String.format("DELETION WARNING: The deletion is irreversible! You will lose the backup %s permanently. Use /kb confirm to start or /kb cancel to abort.", backupFileName), true);
        return SUCCESS;
    }


    /**
     * Restore with context parameter backupName.
     * Simply set the pending backupName to given backupName, for the second confirmation.
     *
     * @param context the context.
     * @return stat code.
     */
    public static int restore(CommandContext<ServerCommandSource> context) {
        try {
            //KBMain.restore("name")
            MinecraftServer server = context.getSource().getMinecraftServer();
            String backupFileName = parseBackupFileName(context, StringArgumentType.getString(context, "backupName"));
//            backupFileName = parseBackupFileName(context, backupFileName);

            if (backupFileName == null)
                return list(context); // Show the list and return

            // Validate backupName
            if (!isBackupFileExists(backupFileName, server)) {
                // Invalid backupName
                msgErr(context, "Invalid backup name! Please check your input. The list index number is also valid.", false);
                return FAILED;
            }

            // Detect backup type

            // Update pending task
            //pendingOperation = AbstractConfirmableOperation.createRestoreOperation(context, backupName);
//        File backupFile = new File(getBackupSaveDirectory(server), getBackupFileName(backupName));
            // TODO: improve this
            ConfiguredBackupMethod method = backupFileName.endsWith(".zip") ?
                    new ConfiguredPrimitiveBackupMethod(
                            backupFileName, getLevelPath(server), getBackupSaveDirectory(server).getAbsolutePath()
                    ) : new ConfiguredIncrementalBackupMethod(
                    backupFileName, getLevelPath(server),
                    getBackupSaveDirectory(server).getAbsolutePath(),
                    getIncrementalBackupBaseDirectory(server).getAbsolutePath()
            );
            // String backupSavePath, String levelPath, String backupFileName
//        getBackupSaveDirectory(server).getAbsolutePath(), getLevelPath(server), backupFileName
            pendingOperation = new RestoreOperation(context, method);

            msgWarn(context, String.format("RESET WARNING: You will LOSE YOUR CURRENT WORLD PERMANENTLY! The worlds will be replaced with backup %s . Use /kb confirm to start or /kb cancel to abort.", backupFileName), true);
            return SUCCESS;
        } catch (IOException e) {
            msgErr(context, String.format("An I/O exception occurred while making backup: %s", e));
        }
        return FAILED;
    }

    private static int doBackup(CommandContext<ServerCommandSource> context, String customBackupName, boolean incremental) {
        try {
            // Real backup name (compatible with legacy backup): date_name, such as 2020-04-23_21-03-00_test
            //KBMain.backup("name")
//        String backupName = BackupNameTimeFormatter.getTimeString() + "_" + customBackupName;

            // Validate file name
            final char[] ILLEGAL_CHARACTERS = {'/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':'};
            for (char c : ILLEGAL_CHARACTERS) {
                if (customBackupName.contains(String.valueOf(c))) {
                    msgErr(context, String.format("Name cannot contain special character \"%c\".", c));
                    return FAILED;
                }
            }

            PrintUtil.info("Start backup...");

            // configure backup method
            MinecraftServer server = context.getSource().getMinecraftServer();
            ConfiguredBackupMethod method = !incremental ? new ConfiguredPrimitiveBackupMethod(
                    PrimitiveBackupFileNameEncoder.INSTANCE.encode(customBackupName, LocalDateTime.now()),
                    getLevelPath(server),
                    getBackupSaveDirectory(server).getCanonicalPath()
            ) : new ConfiguredIncrementalBackupMethod(
                    IncrementalBackupFileNameEncoder.INSTANCE.encode(customBackupName, LocalDateTime.now()),
                    getLevelPath(server),
                    getBackupSaveDirectory(server).getCanonicalPath(),
                    getIncrementalBackupBaseDirectory(server).getCanonicalPath()
            );

            // dispatch to operation worker
            BackupOperation operation = new BackupOperation(context, method);
            if (operation.invoke()) {
                return SUCCESS;
            } else if (operation.isBlocked()) {
                msgWarn(context, "Another task is running, cannot start making backup at this time.");
                return FAILED;
            }
        } catch (IOException e) {
            msgErr(context, String.format("An I/O exception occurred while making backup: %s", e));
        }
        return FAILED;
    }

    /**
     * Restore with context parameter backupName.
     *
     * @param context the context.
     * @return stat code.
     */
    public static int confirm(CommandContext<ServerCommandSource> context) {
        if (pendingOperation == null) {
            msgWarn(context, "Nothing to confirm.");
            return FAILED;
        }

        Invokable operation = pendingOperation;
        pendingOperation = null;

        boolean returnValue = operation.invoke();

        // By the way, update suggestion list.
        BackupNameSuggestionProvider.updateCandidateList();

        return returnValue ? SUCCESS : FAILED; // block compiler's complain.
    }

    /**
     * Cancel the execution to be confirmed.
     *
     * @param context the context.
     * @return stat code.
     */
    public static int cancel(CommandContext<ServerCommandSource> context) {
        if (pendingOperation != null) {
            PrintUtil.msgInfo(context, String.format("The %s has been cancelled.", pendingOperation.toString()), true);
            pendingOperation = null;
            return SUCCESS;
        } else {
            msgErr(context, "Nothing to cancel.");
            return FAILED;
        }
    }

    /**
     * Show the most recent backup.
     * If there is no available backup, print specific info.
     *
     * @param context the context.
     * @return stat code.
     */
    public static int prev(CommandContext<ServerCommandSource> context) {
        // FIXME: This breaks after adding incremental backup
        try {
            // List all backups
            updateBackupList();
//            MinecraftServer server = context.getSource().getMinecraftServer();
//            List<File> files = Arrays.asList(Objects.requireNonNull(getBackupSaveDirectory(server).listFiles()));
//            files.removeIf(f -> !f.getName().startsWith(BackupFilesystemUtil.getBackupFileNamePrefix()));
//            files.sort((x, y) -> (int) (BackupFilesystemUtil.getBackupTimeFromBackupFileName(y.getName()) - BackupFilesystemUtil.getBackupTimeFromBackupFileName(x.getName())));
//            File prevBackupFile = files.get(0);
//            String backupFileName = prevBackupFile.getName();
//            int i;
//            synchronized (backupList) {
//                i = backupList.indexOf(backupFileName);
//                if (i == -1) {
//                    backupList.add(backupFileName);
//                    i = backupList.size();
//                } else {
//                    ++i;
//                }
//            }
            synchronized (backupList) {
                if (!backupList.isEmpty()) {
                    BackupInfo info = backupList.get(0);
                    msgInfo(context, "The most recent backup:");
                    printBackupInfo(context, info, 0);
                } else {
                    msgInfo(context, "There is no backup available.");
                }
            }
        } catch (SecurityException ignored) {
            msgErr(context, "Failed to read file.");
            return FAILED;
        }
        return SUCCESS;
    }

//    private static String getPrimitiveBackupInformationString(String backupFileName, long backupFileSizeBytes) {
//        return String.format(
//                "(ZIP) %s , size: %s",
//                PrimitiveBackupFileNameEncoder.INSTANCE.decode(backupFileName),
//                getFriendlyFileSizeString(backupFileSizeBytes)
//        );
//    }

//    private static String getIncrementalBackupInformationString(File backupFile) {
//        try {
//            SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile(backupFile);
//            return "(Incremental) " + info.getBackupName()
//                    + ", " + DateUtil.getString(info.getBackupTime())
//                    + ((info.getTotalSizeBytes() > 0) ?
//                    (" size: " + BackupFilesystemUtil.getFriendlyFileSizeString(info.getTotalSizeBytes())) : "");
//        } catch (IOException e) {
//            e.printStackTrace();
//            return "(Incremental) " + backupFile.getName();
//        }
//    }

//    /**
//     * Select the backup method we use.
//     * @param context the context.
//     * @return stat code.
//     */
//    public static int setMethod(CommandContext<ServerCommandSource> context) {
//        String desiredMethodName = StringArgumentType.getString(context, "backupMethod");
//        List<BackupType> backupMethods = Arrays.asList(BackupType.PRIMITIVE_ZIP_BACKUP, BackupType.OBJECT_TREE_BACKUP);
//        for (BackupType method : backupMethods) {
//            if(method.getName().equals(desiredMethodName)) {
//                // Incremental backup
////                activatedBackupMethod =
//                msgInfo(context, String.format("Backup method is set to: %s", desiredMethodName));
//                return SUCCESS;
//            }
//        }
//
//        return SUCCESS;
//    }


    private static String parseBackupFileName(CommandContext<ServerCommandSource> context, String userInput) {
        try {
            String backupName = StringArgumentType.getString(context, "backupName");

            if (backupName.matches("[0-9]*")) {
                // treat numeric input as backup index number in list
                int index = Integer.parseInt(backupName) - 1;
                synchronized (backupList) {
                    return backupList.get(index).getBackupFileName(); // Replace input number with real backup file name.
                }
            }
        } catch (NumberFormatException | IndexOutOfBoundsException ignored) {
        }
        return userInput;
    }
}