|
| 1 | +// builds single standalone exe from your Unity build (using 7-Zip SFX module) |
| 2 | +// Note: Change those hardcoded 7zip paths for you |
| 3 | +// Note: Download SDK package for the 7zSD.sfx files https://www.7-zip.org/download.html |
| 4 | + |
| 5 | +using UnityEditor; |
| 6 | +using UnityEditor.Callbacks; |
| 7 | +using System; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.IO; |
| 10 | +using System.Text; |
| 11 | + |
| 12 | +namespace UnityLibrary.BuildTools |
| 13 | +{ |
| 14 | + public static class SingleExePostProcessor |
| 15 | + { |
| 16 | + private const string SevenZipExe = @"D:\Program Files\7-Zip\7z.exe"; |
| 17 | + private const string SevenZipSfx = @"D:\sdk\lzma2603\bin\7zSD.sfx"; |
| 18 | + |
| 19 | + [PostProcessBuild(1000)] |
| 20 | + public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject) |
| 21 | + { |
| 22 | + if (target != BuildTarget.StandaloneWindows64) |
| 23 | + return; |
| 24 | + |
| 25 | + string buildExe = Path.GetFullPath(pathToBuiltProject); |
| 26 | + string buildDirectory = Path.GetDirectoryName(buildExe); |
| 27 | + string exeName = Path.GetFileName(buildExe); |
| 28 | + string gameName = Path.GetFileNameWithoutExtension(buildExe); |
| 29 | + |
| 30 | + if (buildDirectory == null) |
| 31 | + return; |
| 32 | + |
| 33 | + if (!File.Exists(SevenZipExe)) |
| 34 | + { |
| 35 | + UnityEngine.Debug.LogError("7-Zip not found: " + SevenZipExe); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + if (!File.Exists(SevenZipSfx)) |
| 40 | + { |
| 41 | + UnityEngine.Debug.LogError("7-Zip SFX module not found: " + SevenZipSfx); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + string tempDirectory = Path.Combine(Path.GetTempPath(), "UnitySingleExe_" + Guid.NewGuid().ToString("N")); |
| 46 | + Directory.CreateDirectory(tempDirectory); |
| 47 | + |
| 48 | + string archivePath = Path.Combine(tempDirectory, "game.7z"); |
| 49 | + string configPath = Path.Combine(tempDirectory, "config.txt"); |
| 50 | + string outputExe = Path.Combine(Directory.GetParent(buildDirectory)?.FullName ?? buildDirectory, gameName + "_Standalone.exe"); |
| 51 | + |
| 52 | + try |
| 53 | + { |
| 54 | + CreateArchive(buildDirectory, archivePath); |
| 55 | + |
| 56 | + string config = ";!@Install@!UTF-8!\r\n" + "Title=\"" + gameName + "\"\r\n" + "RunProgram=\"" + exeName + "\"\r\n" + "GUIMode=\"2\"\r\n" + ";!@InstallEnd@!\r\n"; |
| 57 | + |
| 58 | + File.WriteAllText(configPath, config, new UTF8Encoding(false)); |
| 59 | + |
| 60 | + using (FileStream output = File.Create(outputExe)) |
| 61 | + { |
| 62 | + AppendFile(output, SevenZipSfx); |
| 63 | + AppendFile(output, configPath); |
| 64 | + AppendFile(output, archivePath); |
| 65 | + } |
| 66 | + |
| 67 | + UnityEngine.Debug.Log("Single EXE created: " + outputExe); |
| 68 | + } |
| 69 | + catch (Exception e) |
| 70 | + { |
| 71 | + UnityEngine.Debug.LogException(e); |
| 72 | + } |
| 73 | + finally |
| 74 | + { |
| 75 | + try |
| 76 | + { |
| 77 | + Directory.Delete(tempDirectory, true); |
| 78 | + } |
| 79 | + catch |
| 80 | + { |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static void CreateArchive(string buildDirectory, string archivePath) |
| 86 | + { |
| 87 | + ProcessStartInfo psi = new ProcessStartInfo |
| 88 | + { |
| 89 | + FileName = SevenZipExe, |
| 90 | + WorkingDirectory = buildDirectory, |
| 91 | + Arguments = "a -t7z -mx=5 \"" + archivePath + "\" \"*\"", |
| 92 | + UseShellExecute = false, |
| 93 | + CreateNoWindow = true, |
| 94 | + RedirectStandardOutput = true, |
| 95 | + RedirectStandardError = true |
| 96 | + }; |
| 97 | + |
| 98 | + using Process process = Process.Start(psi); |
| 99 | + |
| 100 | + if (process == null) |
| 101 | + throw new Exception("Could not start 7-Zip."); |
| 102 | + |
| 103 | + string stdout = process.StandardOutput.ReadToEnd(); |
| 104 | + string stderr = process.StandardError.ReadToEnd(); |
| 105 | + |
| 106 | + process.WaitForExit(); |
| 107 | + |
| 108 | + if (process.ExitCode != 0) |
| 109 | + { |
| 110 | + throw new Exception("7-Zip failed.\n" + stdout + "\n" + stderr); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static void AppendFile(Stream destination, string file) |
| 115 | + { |
| 116 | + using FileStream source = File.OpenRead(file); |
| 117 | + source.CopyTo(destination); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments