-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathSingleExePostProcessor.cs
More file actions
122 lines (101 loc) · 4.33 KB
/
Copy pathSingleExePostProcessor.cs
File metadata and controls
122 lines (101 loc) · 4.33 KB
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
// builds single standalone exe from your Unity build (using 7-Zip SFX module)
// Note: Change those hardcoded 7zip paths for you
// Note: Download SDK package for the 7zSD.sfx files https://www.7-zip.org/download.html
// Warning: This copies all build folder output there, you might not want debug symbols or other data
// Note: You could make it customize the exe icon also (with external tool calls to create .ico from project icon and assign it)
using UnityEditor;
using UnityEditor.Callbacks;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace UnityLibrary.BuildTools
{
public static class SingleExePostProcessor
{
private const string SevenZipExe = @"C:\Program Files\7-Zip\7z.exe";
private const string SevenZipSfx = @"C:\sdk\lzma2603\bin\7zSD.sfx";
[PostProcessBuild(1000)]
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.StandaloneWindows64)
return;
string buildExe = Path.GetFullPath(pathToBuiltProject);
string buildDirectory = Path.GetDirectoryName(buildExe);
string exeName = Path.GetFileName(buildExe);
string gameName = Path.GetFileNameWithoutExtension(buildExe);
if (buildDirectory == null)
return;
if (!File.Exists(SevenZipExe))
{
UnityEngine.Debug.LogError("7-Zip not found: " + SevenZipExe);
return;
}
if (!File.Exists(SevenZipSfx))
{
UnityEngine.Debug.LogError("7-Zip SFX module not found: " + SevenZipSfx);
return;
}
string tempDirectory = Path.Combine(Path.GetTempPath(), "UnitySingleExe_" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(tempDirectory);
string archivePath = Path.Combine(tempDirectory, "game.7z");
string configPath = Path.Combine(tempDirectory, "config.txt");
string outputExe = Path.Combine(Directory.GetParent(buildDirectory)?.FullName ?? buildDirectory, gameName + "_Standalone.exe");
try
{
CreateArchive(buildDirectory, archivePath);
string config = ";!@Install@!UTF-8!\r\n" + "Title=\"" + gameName + "\"\r\n" + "RunProgram=\"" + exeName + "\"\r\n" + "GUIMode=\"2\"\r\n" + ";!@InstallEnd@!\r\n";
File.WriteAllText(configPath, config, new UTF8Encoding(false));
using (FileStream output = File.Create(outputExe))
{
AppendFile(output, SevenZipSfx);
AppendFile(output, configPath);
AppendFile(output, archivePath);
}
UnityEngine.Debug.Log("Single EXE created: " + outputExe);
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
}
finally
{
try
{
Directory.Delete(tempDirectory, true);
}
catch
{
}
}
}
private static void CreateArchive(string buildDirectory, string archivePath)
{
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = SevenZipExe,
WorkingDirectory = buildDirectory,
Arguments = "a -t7z -mx=5 \"" + archivePath + "\" \"*\"",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using Process process = Process.Start(psi);
if (process == null)
throw new Exception("Could not start 7-Zip.");
string stdout = process.StandardOutput.ReadToEnd();
string stderr = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception("7-Zip failed.\n" + stdout + "\n" + stderr);
}
}
private static void AppendFile(Stream destination, string file)
{
using FileStream source = File.OpenRead(file);
source.CopyTo(destination);
}
}
}

