summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/launcher/minecraft/BackupManager.h
blob: ec7b42df2f3d147b4e4181e5d331251b1aca186b (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
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team
/*
 *  ProjT Launcher - Minecraft Launcher
 *  Copyright (C) 2026 Project Tick
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 3.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#pragma once

#include <QDateTime>
#include <QFutureWatcher>
#include <QObject>
#include <QString>
#include <QStringList>
#include "BaseInstance.h"

struct BackupOptions
{
	bool includeSaves		  = true;
	bool includeConfig		  = true;
	bool includeMods		  = false;
	bool includeResourcePacks = false;
	bool includeShaderPacks	  = false;
	bool includeScreenshots	  = false;
	bool includeOptions		  = true;
	QStringList customPaths; // Relative paths to include (e.g., "logs", "crash-reports")

	qint64 estimateSize() const;
};

class InstanceBackup
{
  public:
	InstanceBackup() = default;
	InstanceBackup(const QString& path);

	QString name() const
	{
		return m_name;
	}
	QString backupPath() const
	{
		return m_backupPath;
	}
	QDateTime createdAt() const
	{
		return m_createdAt;
	}
	qint64 size() const
	{
		return m_size;
	}
	QString description() const
	{
		return m_description;
	}
	QStringList includedPaths() const
	{
		return m_includedPaths;
	}

	void setName(const QString& name)
	{
		m_name = name;
	}
	void setDescription(const QString& desc)
	{
		m_description = desc;
	}

	bool isValid() const;
	QString displaySize() const;

  private:
	QString m_name;
	QString m_backupPath;
	QString m_description;
	QDateTime m_createdAt;
	qint64 m_size = 0;
	QStringList m_includedPaths;

	friend class BackupManager;
};

class BackupManager : public QObject
{
	Q_OBJECT

  public:
	explicit BackupManager(QObject* parent = nullptr);

	// Create a new backup (async)
	void createBackupAsync(InstancePtr instance, const QString& backupName, const BackupOptions& options);

	// Create a new backup (blocking)
	bool createBackup(InstancePtr instance, const QString& backupName, const BackupOptions& options);

	// Restore from backup (async)
	void restoreBackupAsync(InstancePtr instance, const InstanceBackup& backup, bool createBackupBeforeRestore = true);

	// Restore from backup (blocking)
	bool restoreBackup(InstancePtr instance, const InstanceBackup& backup, bool createBackupBeforeRestore = true);

	// List all backups for an instance
	QList<InstanceBackup> listBackups(InstancePtr instance) const;

	// Delete old backups (keep only maxCount newest)
	int deleteOldBackups(InstancePtr instance, int maxCount);

	// Delete specific backup
	bool deleteBackup(const InstanceBackup& backup);

	// Get backup directory for instance
	static QString getBackupDirectory(InstancePtr instance);

	// Auto backup before launch
	bool autoBackupBeforeLaunch(InstancePtr instance);

  signals:
	void backupCreated(const QString& instanceId, const QString& backupName);
	void backupRestored(const QString& instanceId, const QString& backupName);
	void backupDeleted(const QString& instanceId, const QString& backupName);
	void backupProgress(int current, int total, const QString& currentFile);
	void backupStarted(const QString& instanceId, const QString& backupName);
	void backupFailed(const QString& instanceId, const QString& error);
	void restoreStarted(const QString& instanceId, const QString& backupName);
	void restoreFailed(const QString& instanceId, const QString& error);

  private:
	bool compressBackup(const QString& sourcePath, const QString& backupPath, const BackupOptions& options);
	bool extractBackup(const QString& backupPath, const QString& targetPath);
	QList<InstanceBackup> scanBackupDirectory(const QString& backupDir) const;
};