1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-23 15:52:55 +01:00

Create custom task for 7z

This commit is contained in:
Ted John
2016-08-22 18:31:52 +01:00
parent 5cb50f6925
commit 8c00ad0e4f
2 changed files with 105 additions and 2 deletions

View File

@@ -1,5 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Main msbuild project for OpenRCT2 -->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Import custom build tasks -->
<Import Project="openrct2.props" />
<!-- Import the C++ project for OpenRCT2 -->
<Import Project="openrct2.vcxproj" />
@@ -53,10 +57,9 @@
<!-- Create zip -->
<Message Importance="high" Text="Creating openrct2.zip..." />
<Exec Command="7z a -tzip -mx9 -mtc=off $(OutZip) $(TempDir)\*" StandardOutputImportance="normal" />
<_7z Output="$(OutZip)" Inputs="$(TempDir)\*" />
<!-- Delete the temporary directory -->
<RemoveDir Directories="$(TempDir)" />
</Target>
</Project>

100
openrct2.props Normal file
View File

@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- 7z Task -->
<UsingTask TaskName="_7z"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<Inputs Required="true" ParameterType="System.String" />
<Output Required="true" ParameterType="System.String" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.Diagnostics"/>
<Using Namespace="System.IO"/>
<Code Type="Method" Language="cs">
<![CDATA[
public override bool Execute()
{
string appPath = Find7zPath();
if (appPath == null)
{
Log.LogError("Unable to find 7z.exe.");
return false;
}
string args = "a -tzip -mx9 -mtc=off \"" + Output + "\" \"" + Inputs + "\"";
Log.LogMessage(MessageImportance.Normal, "7z " + args);
var psi = new ProcessStartInfo(appPath, args);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
var process = Process.Start(psi);
process.WaitForExit();
if (process.ExitCode != 0)
{
string appError = process.StandardError.ReadToEnd();
appError = appError.Replace("\r\n", "\n");
appError = appError.Replace("\r", "\n");
appError = appError.Replace("\n", " ");
int colonIndex = appError.IndexOf(":");
if (colonIndex != -1)
{
appError = appError.Substring(colonIndex + 1);
}
appError = appError.Trim();
Log.LogError(appError);
return false;
}
string appOutput = process.StandardOutput.ReadToEnd();
Log.LogMessage(MessageImportance.Normal, appOutput);
return true;
}
private string Find7zPath()
{
const string DefaultAppFileName = "7z.exe";
string DefaultAppPath = Path.Combine("7-Zip", DefaultAppFileName);
// HACK needed as SpecialFolder.ProgramFiles returns x86 for a 32-bit process
string programFiles = Path.Combine(Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)), "Program Files");
string appPath = Path.Combine(programFiles, DefaultAppPath);
if (File.Exists(appPath))
{
return appPath;
}
programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
appPath = Path.Combine(programFiles, DefaultAppPath);
if (File.Exists(appPath))
{
return appPath;
}
programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
appPath = Path.Combine(programFiles, DefaultAppPath);
if (File.Exists(appPath))
{
return appPath;
}
string[] envPaths = Environment.GetEnvironmentVariable("PATH").Split(';');
foreach (string envPath in envPaths)
{
appPath = Path.Combine(envPath, DefaultAppFileName);
if (File.Exists(appPath))
{
return appPath;
}
}
return null;
}
]]>
</Code>
</Task>
</UsingTask>
</Project>