Skip to content

Commit 33e4a13

Browse files
committed
Implement data backup for Store app reinstall
1 parent 381a974 commit 33e4a13

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

MCLauncher/MainWindow.xaml.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace MCLauncher {
1212
using System.IO.Compression;
1313
using System.Threading;
1414
using Windows.Foundation;
15+
using Windows.Management.Core;
1516
using Windows.Management.Deployment;
1617
using Windows.System;
1718
using WPFDataTypes;
@@ -99,6 +100,56 @@ private async Task DeploymentProgressWrapper(IAsyncOperationWithProgress<Deploym
99100
await src.Task;
100101
}
101102

103+
private string GetBackupMinecraftDataDir() {
104+
string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
105+
string tmpDir = Path.Combine(localAppData, "TmpMinecraftLocalState");
106+
return tmpDir;
107+
}
108+
109+
private void BackupMinecraftDataForRemoval() {
110+
var data = ApplicationDataManager.CreateForPackageFamily(MINECRAFT_PACKAGE_FAMILY);
111+
string tmpDir = GetBackupMinecraftDataDir();
112+
if (Directory.Exists(tmpDir)) {
113+
Debug.WriteLine("BackupMinecraftDataForRemoval error: " + tmpDir + " already exists");
114+
Process.Start("explorer.exe", tmpDir);
115+
MessageBox.Show("The temporary directory for backing up MC data already exists. This probably means that we failed last time backing up the data. Please back the directory up manually.");
116+
throw new Exception("Temporary dir exists");
117+
}
118+
Debug.WriteLine("Moving Minecraft data to: " + tmpDir);
119+
Directory.Move(data.LocalFolder.Path, tmpDir);
120+
}
121+
122+
private void RestoreMove(string from, string to) {
123+
foreach (var f in Directory.EnumerateFiles(from)) {
124+
string ft = Path.Combine(to, Path.GetFileName(f));
125+
if (File.Exists(ft)) {
126+
if (MessageBox.Show("The file " + ft + " already exists in the destination.\nDo you want to replace it? The old file will be lost otherwise.", "Restoring data directory from previous installation", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
127+
continue;
128+
File.Delete(ft);
129+
}
130+
File.Move(f, ft);
131+
}
132+
foreach (var f in Directory.EnumerateDirectories(from)) {
133+
string tp = Path.Combine(to, Path.GetFileName(f));
134+
if (!Directory.Exists(tp)) {
135+
if (File.Exists(tp) && MessageBox.Show("The file " + tp + " is not a directory. Do you want to remove it? The data from the old directory will be lost otherwise.", "Restoring data directory from previous installation", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
136+
continue;
137+
Directory.CreateDirectory(tp);
138+
}
139+
RestoreMove(f, tp);
140+
}
141+
}
142+
143+
private void RestoreMinecraftDataFromReinstall() {
144+
string tmpDir = GetBackupMinecraftDataDir();
145+
if (!Directory.Exists(tmpDir))
146+
return;
147+
var data = ApplicationDataManager.CreateForPackageFamily(MINECRAFT_PACKAGE_FAMILY);
148+
Debug.WriteLine("Moving backup Minecraft data to: " + data.LocalFolder.Path);
149+
RestoreMove(tmpDir, data.LocalFolder.Path);
150+
Directory.Delete(tmpDir, true);
151+
}
152+
102153
private async Task ReRegisterPackage(string gameDir) {
103154
foreach (var pkg in new PackageManager().FindPackages(MINECRAFT_PACKAGE_FAMILY)) {
104155
if (pkg.InstalledLocation.Path == gameDir) {
@@ -107,8 +158,7 @@ private async Task ReRegisterPackage(string gameDir) {
107158
}
108159
Debug.WriteLine("Removing package: " + pkg.Id.FullName + " " + pkg.InstalledLocation.Path);
109160
if (!pkg.IsDevelopmentMode) {
110-
if (MessageBox.Show("A non-Development Mode version is installed on this system. It will need to be removed in a way which does not preserve the data (including your saved worlds). Are you sure you want to continue?", "Warning", MessageBoxButton.YesNoCancel) != MessageBoxResult.Yes)
111-
return;
161+
BackupMinecraftDataForRemoval();
112162
await DeploymentProgressWrapper(new PackageManager().RemovePackageAsync(pkg.Id.FullName, 0));
113163
} else {
114164
Debug.WriteLine("Package is in development mode");
@@ -121,6 +171,7 @@ private async Task ReRegisterPackage(string gameDir) {
121171
string manifestPath = Path.Combine(gameDir, "AppxManifest.xml");
122172
await DeploymentProgressWrapper(new PackageManager().RegisterPackageAsync(new Uri(manifestPath), null, DeploymentOptions.DevelopmentMode));
123173
Debug.WriteLine("App re-register done!");
174+
RestoreMinecraftDataFromReinstall();
124175
}
125176

126177
private void InvokeDownload(Version v) {

0 commit comments

Comments
 (0)