Repository navigation
Expand file tree
/
Copy pathBuildTools.cs
More file actions
473 lines (398 loc) · 17.1 KB
/
Copy pathBuildTools.cs
File metadata and controls
473 lines (398 loc) · 17.1 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using System.Threading;
public class BuildTools
{
public static void CopyDirectory(string SourcePath, string DestinationPath)
{
//Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);
}
public static UnityEditor.Build.Reporting.BuildReport BuildGame(string buildPath, string exeName, BuildTarget target,
BuildOptions opts, string buildId, bool il2cpp)
{
var levels = new string[]
{
"Assets/Scenes/bootstrapper.unity",
"Assets/Scenes/empty.unity"
};
var exePathName = buildPath + "/" + exeName;
Debug.Log("Building: " + exePathName);
Directory.CreateDirectory(buildPath);
// Set all files to be writeable (As Unity 2017.1 sets them to read only)
string fullBuildPath = Directory.GetCurrentDirectory() + "/" + buildPath;
string[] fileNames = Directory.GetFiles(fullBuildPath, "*.*", SearchOption.AllDirectories);
//Contentpipeline compile player scripts
foreach (var fileName in fileNames)
{
FileAttributes attributes = File.GetAttributes(fileName);
attributes &= ~FileAttributes.ReadOnly;
File.SetAttributes(fileName, attributes);
}
string bundlePathSrc = buildPath + "/" + SimpleBundleManager.assetBundleFolder;
string bundlePathDst = "Assets/StreamingAssets/" + SimpleBundleManager.assetBundleFolder;
if (target == BuildTarget.PS4)
{
if (!Directory.Exists(bundlePathSrc))
{
EditorUtility.DisplayDialog("No bundles found", "No Asset Bundles found. Please build them first",
"Ok");
return null;
}
CopyDirectory(bundlePathSrc, bundlePathDst);
}
var monoDirs = Directory.GetDirectories(fullBuildPath).Where(s => s.Contains("MonoBleedingEdge"));
var il2cppDirs = Directory.GetDirectories(fullBuildPath).Where(s => s.Contains("BackUpThisFolder_ButDontShipItWithYourGame"));
var clearFolder = (il2cpp && monoDirs.Count() > 0) || (!il2cpp && il2cppDirs.Count() > 0);
if (clearFolder)
{
Debug.Log(" deleting old folders ..");
foreach(var file in Directory.GetFiles(fullBuildPath))
File.Delete(file);
foreach(var dir in monoDirs)
Directory.Delete(dir,true);
foreach(var dir in il2cppDirs)
Directory.Delete(dir,true);
foreach(var dir in Directory.GetDirectories(fullBuildPath).Where(s => s.EndsWith("_Data")))
Directory.Delete(dir,true);
}
if (il2cpp)
{
UnityEditor.PlayerSettings.SetScriptingBackend(BuildTargetGroup.Standalone, ScriptingImplementation.IL2CPP);
UnityEditor.PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.Standalone, Il2CppCompilerConfiguration.Release);
}
else
{
UnityEditor.PlayerSettings.SetScriptingBackend(BuildTargetGroup.Standalone, ScriptingImplementation.Mono2x);
}
/// Colossal hack to work around build postprocessing expecting everything to be writable in the unity
/// installation, but if people have unity in p4 it will be readonly.
var editorHome = EditorApplication.applicationPath.BeforeLast("/") + "/Data/PlaybackEngines/windowsstandalonesupport";
Debug.Log("Checking for read/only files in standalone players");
if (Directory.Exists(editorHome))
{
var files = Directory.GetFiles(editorHome, "*.*", SearchOption.AllDirectories);
foreach(var f in files)
{
var attr = File.GetAttributes(f);
if((attr & FileAttributes.ReadOnly) != 0)
{
attr = attr & ~FileAttributes.ReadOnly;
Debug.Log("Setting " + f + " to read/write");
File.SetAttributes(f, attr);
}
}
}
Debug.Log("Done.");
Environment.SetEnvironmentVariable("BUILD_ID", buildId, EnvironmentVariableTarget.Process);
var result = BuildPipeline.BuildPlayer(levels, exePathName, target, opts);
Environment.SetEnvironmentVariable("BUILD_ID", "", EnvironmentVariableTarget.Process);
if (target == BuildTarget.PS4)
{
Directory.Delete(bundlePathDst, true);
}
Debug.Log(" ==== Build Done =====");
var stepCount = result.steps.Count();
Debug.Log(" Steps:"+ stepCount);
for(var i=0;i<stepCount;i++)
{
var step = result.steps[i];
Debug.Log("-- " + (i+1) + "/" + stepCount + " " + step.name + " " + step.duration.Seconds + "s --");
foreach (var msg in step.messages)
Debug.Log(msg.content);
}
return result;
}
public static List<LevelInfo> LoadLevelInfos()
{
return LoadAssetsOfType<LevelInfo>();
}
static void AddKeys(Dictionary<string, int> dictionary, string[] keys)
{
foreach (var key in keys)
{
if (dictionary.ContainsKey(key))
{
dictionary[key]++;
}
else
{
dictionary[key] = 1;
}
}
}
public static List<T> LoadAssetsOfType<T>() where T : UnityEngine.Object
{
var result = new List<T>();
var assets = AssetDatabase.FindAssets("t:" + typeof(T).Name);
foreach (var a in assets)
{
var path = AssetDatabase.GUIDToAssetPath(a);
result.Add(AssetDatabase.LoadAssetAtPath<T>(path));
}
return result;
}
static AssetBundleBuild MakeSceneBundleBuild(UnityEngine.Object mainScene, string name)
{
var build = new AssetBundleBuild();
build.assetBundleName = name;
build.assetBundleVariant = "";
var path = AssetDatabase.GetAssetPath(mainScene);
var scenes = new List<string>();
scenes.Add(path.ToLower());
if (EditorLevelManager.IsLayeredLevel(path))
{
foreach (var l in EditorLevelManager.GetLevelLayers(path))
{
scenes.Add(l.ToLower());
}
}
build.assetNames = scenes.ToArray();
return build;
}
static AssetBundleBuild MakeAssetBundleBuild(List<string> assets, string name)
{
var build = new AssetBundleBuild();
build.assetBundleName = name;
build.assetBundleVariant = "";
build.assetNames = assets.ToArray();
return build;
}
static List<string> FindSharedDependencies(List<AssetBundleBuild> builds)
{
var dependenciesCount = new Dictionary<string, int>();
foreach (var build in builds)
{
foreach (var asset in build.assetNames)
{
var dependencies = AssetDatabase.GetDependencies(asset, true);
AddKeys(dependenciesCount, dependencies);
}
}
var shared = new List<string>();
foreach (var dependency in dependenciesCount)
{
if (dependency.Key.EndsWith(".unity"))
continue;
if (dependency.Value > 1)
shared.Add(dependency.Key);
}
return shared;
}
public static void BuildBundles(string bundlePath, BuildTarget target, bool buildBundledAssets, bool buildBundledLevels, bool force = false, List<LevelInfo> buildOnlyLevels = null)
{
Debug.Log("Scene cooking started");
var path = bundlePath + "/" + SimpleBundleManager.assetBundleFolder;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.UncompressedAssetBundle;
if (force)
{
Debug.Log("Forcing rebuild");
assetBundleOptions |= BuildAssetBundleOptions.ForceRebuildAssetBundle;
}
if (buildBundledLevels)
BuildLevelBundles(path, target, assetBundleOptions, buildOnlyLevels);
if (buildBundledAssets)
BundledResourceBuilder.BuildBundles(path, target, assetBundleOptions);
}
public static void BuildLevelBundles(string path, BuildTarget target, BuildAssetBundleOptions assetBundleOptions, List<LevelInfo> buildOnlyLevels = null)
{
var builds = new List<AssetBundleBuild>();
foreach (var levelInfo in LoadLevelInfos())
{
if (buildOnlyLevels != null && !buildOnlyLevels.Contains(levelInfo))
continue;
Debug.Log(" - adding level: " + AssetDatabase.GetAssetPath(levelInfo.main_scene));
var build = MakeSceneBundleBuild(levelInfo.main_scene, levelInfo.name);
builds.Add(build);
}
BuildPipeline.BuildAssetBundles(path, builds.ToArray(), assetBundleOptions, EditorUserBuildSettings.activeBuildTarget);
Debug.Log("Scene cooking done");
}
static string GetBuildName()
{
var buildNumber = System.Environment.GetEnvironmentVariable("BUILD_NUMBER");
if (buildNumber == null)
{
buildNumber = "Dev";
}
var changeSet = System.Environment.GetEnvironmentVariable("P4_CHANGELIST");
if (changeSet == null)
{
changeSet = "0";
}
var now = System.DateTime.Now;
var name = now.ToString("yyyyMMdd") + "." + buildNumber + "." + changeSet;
return name;
}
static string GetBuildPath(BuildTarget target, string buildName)
{
return "Builds/" + target.ToString() + "/" + Application.productName + "_" + target.ToString() + "_" + buildName;
}
[MenuItem("Assets/ResirializeAssets")]
public static void ReserializeProject()
{
if (Selection.assetGUIDs.Length == 0)
return;
List<string> paths = new List<string>();
foreach (var g in Selection.assetGUIDs)
paths.Add(AssetDatabase.GUIDToAssetPath(g));
if (EditorUtility.DisplayDialog("Reserialize " + paths.Count + " assets", "Do you want to reserialize " + paths.Count + " assets?", "Yes, I do!"))
{
foreach(var p in paths)
{
var a = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(p);
EditorUtility.SetDirty(a);
}
AssetDatabase.SaveAssets();
}
}
[MenuItem("FPS Sample/BuildSystem/Win64/OpenBuildFolder")]
public static void OpenBuildFolder()
{
var target = BuildTarget.StandaloneWindows64;
var buildName = GetBuildName();
var buildPath = GetBuildPath(target, buildName);
if (Directory.Exists(buildPath))
{
Debug.Log("Opening " + buildPath);
var p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo("explorer.exe", Path.GetFullPath(buildPath));
p.Start();
}
}
[MenuItem("FPS Sample/BuildSystem/Win64/Deploy")]
public static void Deploy()
{
Debug.Log("Window64 Deploying...");
var target = BuildTarget.StandaloneWindows64;
var buildName = GetBuildName();
var buildPath = GetBuildPath(target, buildName);
//string executableName = Application.productName + ".exe";
var platform = target.ToString();
var clientApi = "b5176262e35ba4aa8280a68aae7b0492";
// TODO: Figure out if it's possible to initialize cloud / unityconnect here instead
var projectId = CloudProjectSettings.projectId;
var orgId = CloudProjectSettings.organizationId;
var projectName = CloudProjectSettings.projectId;
var accessToken = CloudProjectSettings.accessToken;
var deploy = new ConnectedGames.Build.DeployTools(OnProgressUpdate, clientApi, projectId, orgId, projectName, accessToken);
var dstPath = buildName + ".zip";
Debug.Log("Starting upload src=" + buildPath + " platform=" + platform + " isClient=N/A" +
" clientApi=" + clientApi + " projectId=" + projectId + " orgId=" + orgId +
" projectName=" + projectName + " accessToken=" + accessToken);
deploy.CompressAndUpload(buildPath, buildPath+"/"+dstPath, platform, buildName);
while (!deploy.Done)
{
deploy.UpdateLoop();
Thread.Sleep(100);
}
}
private static void OnProgressUpdate(string fileName, double progress)
{
Debug.Log(fileName + ":" + progress);
return;
}
[MenuItem("FPS Sample/BuildSystem/Win64/PostProcess")]
public static void PostProcess()
{
Debug.Log("Window64 build postprocessing...");
var target = BuildTarget.StandaloneWindows64;
var buildName = GetBuildName();
var buildPath = GetBuildPath(target, buildName);
string executableName = Application.productName + ".exe";
if (!Directory.Exists(buildPath) || !File.Exists(buildPath + "/" + Application.productName + ".exe"))
{
Debug.Log("No build here: " + buildPath);
}
Debug.Log("Writing config files");
// Build server bat
var serverBat = new string[]
{
"REM start game server on level_01",
executableName + " -nographics -batchmode +serve level_01 +game.modename assault"
};
File.WriteAllLines(buildPath + "/server.bat", serverBat);
Debug.Log(" server.bat");
// Build empty user.cfg
File.WriteAllLines(buildPath + "/user.cfg", new string[] { });
Debug.Log(" user.cfg");
// Build game.cfg
var gameCfg = new string[]
{
"client",
"load level_menu"
};
File.WriteAllLines(buildPath + "/game.cfg", gameCfg);
Debug.Log(" game.cfg");
Debug.Log("Window64 build postprocessing done.");
}
[MenuItem("FPS Sample/BuildSystem/Win64/CreateBuildWindows64")]
public static void CreateBuildWindows64()
{
CreateBuildWindows64(false);
}
[MenuItem("FPS Sample/BuildSystem/Win64/CreateBuildWindows64-IL2CPP")]
public static void CreateBuildWindows64IL2CPP()
{
CreateBuildWindows64(true);
}
static void CreateBuildWindows64(bool useIL2CPP)
{
Debug.Log("Window64 build started. (" + (useIL2CPP ? "IL2CPP" : "Mono") + ")");
var target = BuildTarget.StandaloneWindows64;
var buildName = GetBuildName();
var buildPath = GetBuildPath(target, buildName);
string executableName = Application.productName + ".exe";
Directory.CreateDirectory(buildPath);
BuildBundles(buildPath, target, true, true, true);
var res = BuildGame(buildPath, executableName, target, BuildOptions.None, buildName, useIL2CPP);
if (!res)
throw new Exception("BuildPipeline.BuildPlayer failed");
if (res.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
throw new Exception("BuildPipeline.BuildPlayer failed: " + res.ToString());
Debug.Log("Window64 build completed...");
PostProcess();
}
[MenuItem("FPS Sample/BuildSystem/PS4/CreateBuildPS4")]
public static void CreateBuildPS4()
{
var target = BuildTarget.PS4;
var buildName = GetBuildName();
var buildPath = GetBuildPath(target, buildName);
string executableName = Application.productName;
Directory.CreateDirectory(buildPath);
BuildBundles(buildPath, target, true, true, true);
var res = BuildGame(buildPath, executableName, target, BuildOptions.None, buildName, false);
if (!res)
throw new Exception("BuildPipeline.BuildPlayer failed");
if (res.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
throw new Exception("BuildPipeline.BuildPlayer failed: " + res.ToString());
}
[MenuItem("FPS Sample/BuildSystem/Linux64/CreateBuildLinux64")]
public static void CreateBuildLinux64()
{
var target = BuildTarget.StandaloneLinux64;
var buildName = GetBuildName();
var buildPath = GetBuildPath(target, buildName);
string executableName = Application.productName;
Directory.CreateDirectory(buildPath);
BuildBundles(buildPath, target, true, true, true);
var res = BuildGame(buildPath, executableName, target, BuildOptions.EnableHeadlessMode, buildName, false);
if (!res)
throw new Exception("BuildPipeline.BuildPlayer failed");
if (res.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
throw new Exception("BuildPipeline.BuildPlayer failed: " + res.ToString());
}
}