Autodetect CD instead of prompting the user. Fixes #911. Requires testing under Windows and Linux.

This commit is contained in:
Paul Chote
2011-06-24 19:08:29 +12:00
parent dc9b0adba9
commit 24266ebc93
5 changed files with 203 additions and 138 deletions

View File

@@ -11,6 +11,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using ICSharpCode.SharpZipLib; using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Zip;
@@ -27,6 +28,15 @@ namespace OpenRA.FileFormats
} }
} }
public static string GetMountedDisk(string[] volumeNames)
{
var volumes = DriveInfo.GetDrives()
.Where(v => v.DriveType == DriveType.CDRom && v.IsReady)
.Select(v => v.RootDirectory.FullName);
return volumes.FirstOrDefault(v => volumeNames.Contains(Path.GetFileName(v)));
}
// TODO: The package should be mounted into its own context to avoid name collisions with installed files // TODO: The package should be mounted into its own context to avoid name collisions with installed files
public static bool ExtractFromPackage(string srcPath, string package, string[] files, string destPath, Action<string> onProgress, Action<string> onError) public static bool ExtractFromPackage(string srcPath, string package, string[] files, string destPath, Action<string> onProgress, Action<string> onError)
{ {

View File

@@ -24,6 +24,8 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
ProgressBarWidget progressBar; ProgressBarWidget progressBar;
LabelWidget statusLabel; LabelWidget statusLabel;
Action continueLoading; Action continueLoading;
ButtonWidget retryButton, backButton;
Widget installingContainer, insertDiskContainer;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public CncInstallFromCDLogic([ObjectCreator.Param] Widget widget, public CncInstallFromCDLogic([ObjectCreator.Param] Widget widget,
@@ -34,41 +36,43 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
progressBar = panel.GetWidget<ProgressBarWidget>("PROGRESS_BAR"); progressBar = panel.GetWidget<ProgressBarWidget>("PROGRESS_BAR");
statusLabel = panel.GetWidget<LabelWidget>("STATUS_LABEL"); statusLabel = panel.GetWidget<LabelWidget>("STATUS_LABEL");
var backButton = panel.GetWidget<ButtonWidget>("BACK_BUTTON"); backButton = panel.GetWidget<ButtonWidget>("BACK_BUTTON");
backButton.OnClick = Widget.CloseWindow; backButton.OnClick = Widget.CloseWindow;
backButton.IsVisible = () => false;
var retryButton = panel.GetWidget<ButtonWidget>("RETRY_BUTTON"); retryButton = panel.GetWidget<ButtonWidget>("RETRY_BUTTON");
retryButton.OnClick = PromptForCD; retryButton.OnClick = CheckForDisk;
retryButton.IsVisible = () => false;
// TODO: Search obvious places (platform dependent) for CD installingContainer = panel.GetWidget("INSTALLING");
PromptForCD(); insertDiskContainer = panel.GetWidget("INSERT_DISK");
CheckForDisk();
} }
void PromptForCD() void CheckForDisk()
{ {
if (Game.Settings.Graphics.Mode == WindowMode.Fullscreen) var path = InstallUtils.GetMountedDisk(new [] { "GDI95", "NOD95" });
if (path != null)
Install(path);
else
{ {
statusLabel.GetText = () => "Error: Installing from Fullscreen mode is not supported"; insertDiskContainer.IsVisible = () => true;
panel.GetWidget("BACK_BUTTON").IsVisible = () => true; installingContainer.IsVisible = () => false;
return; }
} }
progressBar.SetIndeterminate(true); void Install(string source)
Game.Utilities.PromptFilepathAsync("Select CONQUER.MIX on the C&C CD", path => Game.RunAfterTick(() => Install(path)));
}
void Install(string path)
{ {
backButton.IsDisabled = () => true;
retryButton.IsDisabled = () => true;
insertDiskContainer.IsVisible = () => false;
installingContainer.IsVisible = () => true;
var dest = new string[] { Platform.SupportDir, "Content", "cnc" }.Aggregate(Path.Combine); var dest = new string[] { Platform.SupportDir, "Content", "cnc" }.Aggregate(Path.Combine);
var copyFiles = new string[] { "CONQUER.MIX", "DESERT.MIX", var copyFiles = new string[] { "CONQUER.MIX", "DESERT.MIX",
"GENERAL.MIX", "SCORES.MIX", "SOUNDS.MIX", "TEMPERAT.MIX", "WINTER.MIX" }; "GENERAL.MIX", "SCORES.MIX", "SOUNDS.MIX", "TEMPERAT.MIX", "WINTER.MIX" };
var extractPackage = "INSTALL/SETUP.Z"; var extractPackage = "INSTALL/SETUP.Z";
var extractFiles = new string[] { "cclocal.mix", "speech.mix", "tempicnh.mix", "updatec.mix" }; var extractFiles = new string[] { "cclocal.mix", "speech.mix", "tempicnh.mix", "updatec.mix", "transit.mix" };
progressBar.SetIndeterminate(false);
var installCounter = 0; var installCounter = 0;
var installTotal = copyFiles.Count() + extractFiles.Count(); var installTotal = copyFiles.Count() + extractFiles.Count();
@@ -83,22 +87,13 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
var onError = (Action<string>)(s => Game.RunAfterTick(() => var onError = (Action<string>)(s => Game.RunAfterTick(() =>
{ {
statusLabel.GetText = () => "Error: "+s; statusLabel.GetText = () => "Error: "+s;
panel.GetWidget("RETRY_BUTTON").IsVisible = () => true; backButton.IsDisabled = () => false;
panel.GetWidget("BACK_BUTTON").IsVisible = () => true; retryButton.IsDisabled = () => false;
})); }));
string source;
try
{
source = Path.GetDirectoryName(path);
}
catch (ArgumentException)
{
onError("Invalid path selected");
return;
}
var t = new Thread( _ => var t = new Thread( _ =>
{
try
{ {
if (!InstallUtils.CopyFiles(source, copyFiles, dest, onProgress, onError)) if (!InstallUtils.CopyFiles(source, copyFiles, dest, onProgress, onError))
return; return;
@@ -111,6 +106,11 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
Widget.CloseWindow(); Widget.CloseWindow();
continueLoading(); continueLoading();
}); });
}
catch
{
onError("Installation failed");
}
}) { IsBackground = true }; }) { IsBackground = true };
t.Start(); t.Start();
} }

View File

@@ -47,12 +47,14 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
panel.GetWidget<ButtonWidget>("BACK_BUTTON").OnClick = () => { Widget.CloseWindow(); onExit(); }; panel.GetWidget<ButtonWidget>("BACK_BUTTON").OnClick = () => { Widget.CloseWindow(); onExit(); };
Action<string> afterInstall = path => Action afterInstall = () =>
{ {
// Mount the new mixfile and rebuild the scores list // Mount the new mixfile and rebuild the scores list
try try
{ {
FileSystem.Mount(path); var path = new string[] { Platform.SupportDir, "Content", "cnc" }.Aggregate(Path.Combine);
FileSystem.Mount(Path.Combine(path, "scores.mix"));
FileSystem.Mount(Path.Combine(path, "transit.mix"));
Rules.Music.Do(m => m.Value.Reload()); Rules.Music.Do(m => m.Value.Reload());
} }
catch (Exception) { } catch (Exception) { }
@@ -189,94 +191,95 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
Widget panel; Widget panel;
ProgressBarWidget progressBar; ProgressBarWidget progressBar;
LabelWidget statusLabel; LabelWidget statusLabel;
Action<string> afterInstall; Action afterInstall;
ButtonWidget retryButton, backButton;
Widget installingContainer, insertDiskContainer;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public CncInstallMusicLogic([ObjectCreator.Param] Widget widget, public CncInstallMusicLogic([ObjectCreator.Param] Widget widget,
[ObjectCreator.Param] Action<string> afterInstall) [ObjectCreator.Param] Action afterInstall)
{ {
this.afterInstall = afterInstall; this.afterInstall = afterInstall;
panel = widget.GetWidget("INSTALL_MUSIC_PANEL"); panel = widget.GetWidget("INSTALL_MUSIC_PANEL");
progressBar = panel.GetWidget<ProgressBarWidget>("PROGRESS_BAR"); progressBar = panel.GetWidget<ProgressBarWidget>("PROGRESS_BAR");
statusLabel = panel.GetWidget<LabelWidget>("STATUS_LABEL"); statusLabel = panel.GetWidget<LabelWidget>("STATUS_LABEL");
var backButton = panel.GetWidget<ButtonWidget>("BACK_BUTTON"); backButton = panel.GetWidget<ButtonWidget>("BACK_BUTTON");
backButton.OnClick = Widget.CloseWindow; backButton.OnClick = Widget.CloseWindow;
backButton.IsVisible = () => false;
var retryButton = panel.GetWidget<ButtonWidget>("RETRY_BUTTON"); retryButton = panel.GetWidget<ButtonWidget>("RETRY_BUTTON");
retryButton.OnClick = PromptForCD; retryButton.OnClick = CheckForDisk;
retryButton.IsVisible = () => false;
// TODO: Search obvious places (platform dependent) for CD installingContainer = panel.GetWidget("INSTALLING");
PromptForCD(); insertDiskContainer = panel.GetWidget("INSERT_DISK");
CheckForDisk();
} }
void PromptForCD() void CheckForDisk()
{ {
if (Game.Settings.Graphics.Mode == WindowMode.Fullscreen) var path = InstallUtils.GetMountedDisk(new [] { "GDI95", "NOD95" });
if (path != null)
Install(path);
else
{ {
statusLabel.GetText = () => "Error: Installing from Fullscreen mode is not supported"; insertDiskContainer.IsVisible = () => true;
panel.GetWidget("BACK_BUTTON").IsVisible = () => true; installingContainer.IsVisible = () => false;
return; }
} }
progressBar.SetIndeterminate(true); void Install(string source)
statusLabel.GetText = () => "Waiting for file"; {
Game.Utilities.PromptFilepathAsync("Select SCORES.MIX on the C&C CD", path => Install(path)); backButton.IsDisabled = () => true;
} retryButton.IsDisabled = () => true;
insertDiskContainer.IsVisible = () => false;
installingContainer.IsVisible = () => true;
public void OnError(string message)
{
Game.RunAfterTick(() =>
{
progressBar.SetIndeterminate(false);
statusLabel.GetText = () => "Error: "+message;
panel.GetWidget("RETRY_BUTTON").IsVisible = () => true;
panel.GetWidget("BACK_BUTTON").IsVisible = () => true;
});
}
void Install(string path)
{
var dest = new string[] { Platform.SupportDir, "Content", "cnc" }.Aggregate(Path.Combine); var dest = new string[] { Platform.SupportDir, "Content", "cnc" }.Aggregate(Path.Combine);
Game.RunAfterTick(() => statusLabel.GetText = () => "Installing"); var copyFiles = new string[] { "SCORES.MIX" };
// Mount the package and check that it contains the correct files var extractPackage = "INSTALL/SETUP.Z";
try var extractFiles = new string[] { "transit.mix" };
{
var mixFile = new MixFile(path, 0);
if (!mixFile.Exists("aoi.aud")) var installCounter = 0;
var installTotal = copyFiles.Count() + extractFiles.Count();
var onProgress = (Action<string>)(s => Game.RunAfterTick(() =>
{ {
OnError("Not the C&C SCORES.MIX"); progressBar.Percentage = installCounter*100/installTotal;
return; installCounter++;
}
statusLabel.GetText = () => s;
}));
var onError = (Action<string>)(s => Game.RunAfterTick(() =>
{
statusLabel.GetText = () => "Error: "+s;
backButton.IsDisabled = () => false;
retryButton.IsDisabled = () => false;
}));
var t = new Thread( _ => var t = new Thread( _ =>
{ {
var destPath = Path.Combine(dest, "scores.mix");
try try
{ {
File.Copy(path, destPath, true); if (!InstallUtils.CopyFiles(source, copyFiles, dest, onProgress, onError))
return;
if (!InstallUtils.ExtractFromPackage(source, extractPackage, extractFiles, dest, onProgress, onError))
return;
Game.RunAfterTick(() => Game.RunAfterTick(() =>
{ {
Widget.CloseWindow(); // Progress panel Widget.CloseWindow();
afterInstall(destPath); afterInstall();
}); });
} }
catch (Exception e) catch
{ {
OnError("File copy failed"); onError("Installation failed");
Log.Write("debug", e.Message);
} }
}) { IsBackground = true }; }) { IsBackground = true };
t.Start(); t.Start();
} }
catch
{
OnError("Invalid mix file");
}
}
} }
} }

View File

@@ -57,13 +57,13 @@ Container@INSTALL_PANEL:
Height:25 Height:25
Text:OpenRA requires the original C&C content files. Text:OpenRA requires the original C&C content files.
Font:Bold Font:Bold
Label@INFO: Label@INFO2:
X:170 X:170
Y:70 Y:70
Width:PARENT_RIGHT-185 Width:PARENT_RIGHT-185
Height:25 Height:25
WordWrap:true WordWrap:true
Text:OpenRA can download these files (excluding music and videos) from the internet, or you can install from the original C&C CD. Text:OpenRA can download these files (excluding music and videos) from the internet, or you can install from the original C&C CDs.
Font:Bold Font:Bold
Button@QUIT_BUTTON: Button@QUIT_BUTTON:
Id:QUIT_BUTTON Id:QUIT_BUTTON
@@ -118,6 +118,12 @@ Container@INSTALL_FROMCD_PANEL:
Y:11 Y:11
ImageCollection:logos ImageCollection:logos
ImageName:install ImageName:install
Container@INSTALLING:
Id:INSTALLING
Width:PARENT_RIGHT
Height:PARENT_BOTTOM
Visible: false
Children:
ProgressBar@PROGRESS_BAR: ProgressBar@PROGRESS_BAR:
Id:PROGRESS_BAR Id:PROGRESS_BAR
X:170 X:170
@@ -131,7 +137,27 @@ Container@INSTALL_FROMCD_PANEL:
Width:PARENT_RIGHT - 185 Width:PARENT_RIGHT - 185
Height:25 Height:25
Align:Left Align:Left
Text:Waiting for CD Container@INSERT_DISK:
Id:INSERT_DISK
Width:PARENT_RIGHT
Height:PARENT_BOTTOM
Visible: false
Children:
Label@INFO:
X:170
Y:35
Width:PARENT_RIGHT-30
Height:25
Text:Disk not found
Font:Bold
Label@INFO2:
X:170
Y:70
Width:PARENT_RIGHT-185
Height:25
WordWrap:true
Text:Please insert one of the Command & Conquer install CDs then click Retry.
Font:Bold
Button@BACK_BUTTON: Button@BACK_BUTTON:
Id:BACK_BUTTON Id:BACK_BUTTON
Key:escape Key:escape

View File

@@ -225,6 +225,12 @@ Container@INSTALL_MUSIC_PANEL:
Y:11 Y:11
ImageCollection:logos ImageCollection:logos
ImageName:install ImageName:install
Container@INSTALLING:
Id:INSTALLING
Width:PARENT_RIGHT
Height:PARENT_BOTTOM
Visible: false
Children:
ProgressBar@PROGRESS_BAR: ProgressBar@PROGRESS_BAR:
Id:PROGRESS_BAR Id:PROGRESS_BAR
X:170 X:170
@@ -238,7 +244,27 @@ Container@INSTALL_MUSIC_PANEL:
Width:PARENT_RIGHT - 185 Width:PARENT_RIGHT - 185
Height:25 Height:25
Align:Left Align:Left
Text:Waiting for file Container@INSERT_DISK:
Id:INSERT_DISK
Width:PARENT_RIGHT
Height:PARENT_BOTTOM
Visible: false
Children:
Label@INFO:
X:170
Y:35
Width:PARENT_RIGHT-30
Height:25
Text:Disk not found
Font:Bold
Label@INFO2:
X:170
Y:70
Width:PARENT_RIGHT-185
Height:25
WordWrap:true
Text:Please insert one of the Command & Conquer install CDs then click Retry.
Font:Bold
Button@BACK_BUTTON: Button@BACK_BUTTON:
Id:BACK_BUTTON Id:BACK_BUTTON
Key:escape Key:escape