com.amanotes.gdk 0.2.49 → 0.2.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,906 @@
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Diagnostics;
4
+ using System.IO;
5
+ using System.Linq;
6
+ using UnityEditor;
7
+ using UnityEngine;
8
+ using UnityEngine.Events;
9
+ using Debug = UnityEngine.Debug;
10
+ using UnityObject = UnityEngine.Object;
11
+
12
+ namespace Amanotes.Editor
13
+ {
14
+ [CreateAssetMenu(menuName = "Ama GDK/Build Config", fileName = "LocalBuild")]
15
+ public class GDKLocalBuild : ScriptableObject
16
+ {
17
+ private const string DEFAULT_BUILD_FOLDER = "../../_Build";
18
+
19
+ public KeystoreInfo keystore;
20
+ [SerializeField] private string relativeBuildFolder = DEFAULT_BUILD_FOLDER;
21
+
22
+ private string _buildFolder;
23
+ public string buildFolder
24
+ {
25
+ get
26
+ {
27
+ if (!string.IsNullOrEmpty(_buildFolder)) return _buildFolder;
28
+ _buildFolder = new DirectoryInfo(Path.Combine(Application.dataPath, relativeBuildFolder ?? DEFAULT_BUILD_FOLDER)).FullName;
29
+ return _buildFolder;
30
+ }
31
+ }
32
+
33
+ public Texture2D icon;
34
+
35
+ public List<BuildInfo> listBuild = new List<BuildInfo>();
36
+
37
+ public List<UnityObject> listUnused;
38
+ public BuildRequestTS buildRQ;
39
+ public UnityEvent beforeBuild;
40
+ public TextAsset shTemplate;
41
+
42
+ [NonSerialized] private BuildInfo _buildInfo;
43
+ [NonSerialized] private int _lockCount;
44
+
45
+ public int nBuildPending
46
+ {
47
+ get
48
+ {
49
+ if ((buildRQ != null) && !buildRQ.isDifferentSession) return listBuild.Count(t => t.enable);
50
+
51
+ StopBuild();
52
+ return 0;
53
+ }
54
+ }
55
+
56
+ internal static int SecondsTS => (int)(DateTime.Now - new DateTime(2010, 1, 1)).TotalSeconds;
57
+ public void Lock()
58
+ {
59
+ _lockCount++;
60
+ }
61
+ public void Unlock()
62
+ {
63
+ _lockCount--;
64
+ if ((_lockCount == 0) && (_buildInfo != null)) ExportBuild(_buildInfo);
65
+ }
66
+
67
+ public void StopBuild()
68
+ {
69
+ for (var i = 0; i < listBuild.Count; i++)
70
+ {
71
+ listBuild[i].enable = false;
72
+ }
73
+ }
74
+
75
+ public void ProcessBuild()
76
+ {
77
+ BuildTarget cTarget = EditorUserBuildSettings.activeBuildTarget;
78
+
79
+ for (var i = 0; i < listBuild.Count; i++)
80
+ {
81
+ BuildInfo item = listBuild[i];
82
+ if (!item.enable) continue;
83
+
84
+ if (item.target != cTarget) continue;
85
+
86
+ item.enable = false; // already build
87
+ Build(item);
88
+ return; // only do one build at a time
89
+ }
90
+
91
+ for (var i = 0; i < listBuild.Count; i++)
92
+ {
93
+ BuildInfo item = listBuild[i];
94
+ if (!item.enable) continue;
95
+
96
+ switch (item.target)
97
+ {
98
+ case BuildTarget.Android:
99
+ EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android);
100
+ return; // only do one build at a time
101
+
102
+ case BuildTarget.iOS:
103
+ EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.iOS, BuildTarget.iOS);
104
+ return; // only do one build at a time
105
+ }
106
+
107
+ }
108
+ }
109
+
110
+ private void RemoveUnusedAssets()
111
+ {
112
+ AssetDatabase.StartAssetEditing();
113
+ for (var i = 0; i < listUnused.Count; i++)
114
+ {
115
+ if (listUnused[i] == null) continue;
116
+ string path = AssetDatabase.GetAssetPath(listUnused[i]);
117
+ AssetDatabase.DeleteAsset(path);
118
+ }
119
+ AssetDatabase.StopAssetEditing();
120
+ }
121
+
122
+ public void Build(BuildInfo info)
123
+ {
124
+ DryRun(info);
125
+
126
+ if (_lockCount == 0)
127
+ {
128
+ ExportBuild(info);
129
+ return;
130
+ }
131
+
132
+ _buildInfo = info;
133
+ }
134
+
135
+ public void EnableBuildForTarget(BuildTarget target, bool updateBuildNumber = true)
136
+ {
137
+ for (var i = 0; i < listBuild.Count; i++)
138
+ {
139
+ BuildInfo b = listBuild[i];
140
+ b.enable = b.target == target;
141
+
142
+ if (updateBuildNumber && (b.target == target)) b.buildNumber++;
143
+ }
144
+
145
+ EditorUtility.SetDirty(this);
146
+ }
147
+
148
+ public void DryRun(BuildInfo info)
149
+ {
150
+ var buildDir = new DirectoryInfo(buildFolder);
151
+ if (!buildDir.Exists) Directory.CreateDirectory(buildDir.FullName);
152
+
153
+ // Check for existed build with the same name
154
+ if (info.overwrite == false)
155
+ {
156
+ if (info.target == BuildTarget.Android)
157
+ {
158
+ string[] apkNames = info.GetAPKNames(buildFolder);
159
+ for (var i = 0; i < apkNames.Length; i++)
160
+ {
161
+ string item = apkNames[i];
162
+ if (!File.Exists(item)) continue;
163
+ Debug.LogWarning("Exporting file existed! " + item);
164
+
165
+ // return;
166
+ }
167
+ } else
168
+ {
169
+ string fullBuildPath = info.fullBuildPath(buildFolder);
170
+ if (Directory.Exists(fullBuildPath))
171
+ {
172
+ Debug.LogWarning("Exporting target existed! " + fullBuildPath);
173
+ EditorUtility.RevealInFinder(fullBuildPath);
174
+
175
+ // return;
176
+ }
177
+ }
178
+ }
179
+
180
+ RemoveUnusedAssets();
181
+ if (info.target == BuildTarget.Android) keystore.Write();
182
+
183
+ info.Write();
184
+ info.PreprocessBuild(buildFolder);
185
+
186
+ // write customized icon, if any
187
+ if ((icon != null) && (info.buildIcon != null) && (info.buildIcon != icon))
188
+ {
189
+ string p1 = AssetDatabase.GetAssetPath(info.buildIcon);
190
+ string p2 = AssetDatabase.GetAssetPath(icon);
191
+ byte[] bytes = File.ReadAllBytes(p1);
192
+ File.WriteAllBytes(p2, bytes);
193
+
194
+ // force refresh
195
+ AssetDatabase.ImportAsset(p2, ImportAssetOptions.ForceSynchronousImport);
196
+ RepaintAll();
197
+ }
198
+
199
+ _lockCount = 0;
200
+ if (beforeBuild != null) beforeBuild.Invoke();
201
+ }
202
+
203
+ private static void RepaintAll()
204
+ {
205
+ UnityObject[] windows = Resources.FindObjectsOfTypeAll(typeof(EditorWindow));
206
+ if (windows == null || windows.Length <= 0) return;
207
+
208
+ for (var i = 0; i < windows.Length; i++)
209
+ {
210
+ var w = windows[i] as EditorWindow;
211
+ if (w != null) w.Repaint();
212
+ }
213
+ }
214
+
215
+ private void RunPostBuild()
216
+ {
217
+ listBuild[0].buildNumber++;
218
+ EditorUtility.SetDirty(this);
219
+ AssetDatabase.SaveAssets();
220
+ // RunPostBuildSH(listBuild[0]);
221
+ }
222
+
223
+ // private void RunPostBuildSH(BuildInfo info)
224
+ // {
225
+ // if (shTemplate == null) return;
226
+ //
227
+ // string shPath = ExternalProcessUtils.FileFromTemplate(
228
+ // "git-post-build.sh", shTemplate.text,
229
+ // "{{TEMPLATE_PROJECT_PATH}}", Application.dataPath.Replace("/Assets", string.Empty),
230
+ // "{{TEMPLATE_TAG}}", $"{info.projectId}/v{info.buildVersion}_b{info.buildNumber}",
231
+ // "{{TEMPLATE_BRANCH}}", $"build/v{info.buildVersion}_b{info.buildNumber}",
232
+ // "{{TEMPLATE_BUILD_ASSET}}", $"{AssetDatabase.GetAssetPath(this)}"
233
+ // );
234
+ //
235
+ // // Run & Lock Unity until git finish
236
+ // ExternalProcessUtils.Run(shPath, true);
237
+ // }
238
+
239
+ private void ExportBuild(BuildInfo info)
240
+ {
241
+ string fullBuildPath = info.fullBuildPath(buildFolder);
242
+ BuildPipeline.BuildPlayer(GetScenes(), fullBuildPath, info.target, BuildOptions.None);
243
+ info.PostProcessBuild(buildFolder);
244
+
245
+ // if (buildRQ.willRunSHPostBuild)
246
+ // {
247
+ // // Run once per build request
248
+ // buildRQ.willRunSHPostBuild = false;
249
+ // RunPostBuildSH(info);
250
+ // }
251
+
252
+ if (nBuildPending > 0)
253
+ {
254
+ ProcessBuild();
255
+ return;
256
+ }
257
+
258
+ EditorUtility.RevealInFinder(fullBuildPath);
259
+ }
260
+
261
+ internal static string[] GetScenes()
262
+ {
263
+ return EditorBuildSettings.scenes.Where(s => s.enabled).Select(s => s.path).ToArray();
264
+ }
265
+
266
+ internal static void DrawRect(Rect rect, Color c)
267
+ {
268
+ Color bgColor = GUI.color;
269
+ GUI.color = c;
270
+ GUI.DrawTexture(rect, Texture2D.whiteTexture, ScaleMode.StretchToFill, false);
271
+ GUI.color = bgColor;
272
+ }
273
+ [Serializable] public class BuildRequestTS
274
+ {
275
+ public int processId;
276
+ public string machineId;
277
+ public string projectPath;
278
+ public bool willRunSHPostBuild = true;
279
+
280
+ public bool isDifferentSession
281
+ {
282
+ get
283
+ {
284
+ if (machineId != SystemInfo.deviceUniqueIdentifier)
285
+
286
+ //Debug.LogWarning("Different device");
287
+ return true;
288
+
289
+ var currentProcess = Process.GetCurrentProcess();
290
+ if (processId != currentProcess.Id)
291
+
292
+ //Debug.LogWarning("Different process ID");
293
+ return true; // Changed process id
294
+
295
+ if (projectPath != Application.dataPath)
296
+
297
+ //Debug.LogWarning("Different project path");
298
+ return true; // Changed project
299
+
300
+ return false;
301
+ }
302
+ }
303
+
304
+ public static BuildRequestTS Generate() //bool runGit
305
+ {
306
+ var currentProcess = Process.GetCurrentProcess();
307
+
308
+ return new BuildRequestTS
309
+ {
310
+ machineId = SystemInfo.deviceUniqueIdentifier,
311
+ processId = currentProcess.Id,
312
+ projectPath = Application.dataPath,
313
+ // willRunSHPostBuild = runGit
314
+ };
315
+ }
316
+ }
317
+ [Serializable] public class BuildInfo
318
+ {
319
+ public string projectId;
320
+ public string productName;
321
+ public string packageName;
322
+
323
+ public Texture2D buildIcon;
324
+ public BuildTarget target;
325
+
326
+ public bool isAAB;
327
+ public bool splitAPK = true;
328
+ public bool overwrite = true;
329
+ public string buildVersion;
330
+ public int buildNumber;
331
+
332
+ // Android options
333
+ public bool enable;
334
+
335
+ public bool isAndroid => target == BuildTarget.Android;
336
+ public bool isIOS => target == BuildTarget.iOS;
337
+
338
+ public string buildName => $"{projectId}_v{buildVersion}_b{buildNumber}";
339
+
340
+ public void PreprocessBuild(string buildFolder)
341
+ {
342
+ AssetDatabase.SaveAssets();
343
+ GC.Collect();
344
+ Resources.UnloadUnusedAssets();
345
+ EditorUtility.UnloadUnusedAssetsImmediate();
346
+
347
+ PlayerSettings.bundleVersion = buildVersion;
348
+
349
+ if (isAndroid)
350
+ {
351
+ PlayerSettings.Android.bundleVersionCode = buildNumber;
352
+ PlayerSettings.Android.buildApkPerCpuArchitecture = splitAPK;
353
+ PlayerSettings.Android.targetArchitectures = AndroidArchitecture.All;
354
+
355
+ EditorUserBuildSettings.buildAppBundle = isAAB;
356
+ EditorUserBuildSettings.exportAsGoogleAndroidProject = false;
357
+
358
+ #if UNITY_2021_1_OR_NEWER
359
+ EditorUserBuildSettings.androidCreateSymbols = AndroidCreateSymbols.Disabled;
360
+ #else
361
+ EditorUserBuildSettings.androidCreateSymbolsZip = false;
362
+ #endif
363
+
364
+ string[] listAPKNames = GetAPKNames(buildFolder);
365
+
366
+ for (var i = 0; i < listAPKNames.Length; i++)
367
+ {
368
+ string apkName = listAPKNames[i];
369
+ string tempName = apkName.Replace(buildName, Application.productName);
370
+
371
+ if (File.Exists(tempName)) File.Delete(tempName); // always delete temp file no matter what
372
+ if (overwrite && File.Exists(apkName)) File.Delete(apkName);
373
+ }
374
+
375
+ return;
376
+ }
377
+
378
+ if (isIOS)
379
+ {
380
+ PlayerSettings.iOS.buildNumber = buildNumber.ToString();
381
+ if (!overwrite) return;
382
+
383
+ string path = fullBuildPath(buildFolder);
384
+ if (Directory.Exists(path)) Directory.Delete(path, true);
385
+ return;
386
+ }
387
+
388
+ Debug.LogWarning("Unsupported platform: " + target);
389
+ }
390
+
391
+ public string[] GetAPKNames(string buildFolder)
392
+ {
393
+ var buildDir = new DirectoryInfo(buildFolder);
394
+ string fullName = buildDir.FullName;
395
+
396
+ if (isAAB)
397
+ return new[]
398
+ {
399
+ Path.Combine(fullName, buildName + ".aab")
400
+ };
401
+
402
+ if (splitAPK)
403
+ return new[]
404
+ {
405
+ Path.Combine(fullName, buildName + ".arm64-v8a.apk"), Path.Combine(fullName, buildName + ".armeabi-v7a.apk")
406
+ };
407
+
408
+ return new[]
409
+ {
410
+ Path.Combine(fullName, buildName + ".apk")
411
+ };
412
+ }
413
+
414
+ public void PostProcessBuild(string buildFolder)
415
+ {
416
+ if (target != BuildTarget.Android) return;
417
+ if (isAAB) return;
418
+
419
+ // var productName = Application.productName;
420
+ string[] listAPKNames = GetAPKNames(buildFolder);
421
+
422
+ for (var i = 0; i < listAPKNames.Length; i++)
423
+ {
424
+ string apkName = listAPKNames[i];
425
+ string tempName = apkName.Replace(buildName, productName);
426
+
427
+ if (!File.Exists(tempName)) continue;
428
+ File.Move(tempName, apkName);
429
+ }
430
+ }
431
+
432
+ public string fullBuildPath(string buildFolder)
433
+ {
434
+ if (buildFolder.EndsWith("/"))
435
+
436
+ // remove last slash
437
+ buildFolder = buildFolder.Substring(0, buildFolder.Length - 1);
438
+
439
+ var buildDir = new DirectoryInfo(buildFolder);
440
+
441
+ if (isAndroid)
442
+ {
443
+ if (isAAB) return Path.Combine(buildDir.FullName, buildName + ".aab");
444
+ if (splitAPK) return buildDir.FullName;
445
+ return Path.Combine(buildDir.FullName, buildName + ".apk");
446
+ }
447
+
448
+ return Path.Combine(buildDir.FullName, "XCode_" + buildName);
449
+ }
450
+
451
+ public BuildInfo Read()
452
+ {
453
+ buildVersion = PlayerSettings.bundleVersion;
454
+ productName = PlayerSettings.productName;
455
+
456
+ if (isAndroid)
457
+ {
458
+ buildNumber = PlayerSettings.Android.bundleVersionCode;
459
+ packageName = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.Android);
460
+ }
461
+
462
+ if (isIOS)
463
+ {
464
+ int.TryParse(PlayerSettings.iOS.buildNumber, out buildNumber);
465
+ packageName = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS);
466
+ }
467
+
468
+ return this;
469
+ }
470
+
471
+ public void Write()
472
+ {
473
+ PlayerSettings.bundleVersion = buildVersion;
474
+ PlayerSettings.productName = productName;
475
+
476
+ if (isAndroid)
477
+ {
478
+ PlayerSettings.Android.bundleVersionCode = buildNumber;
479
+ PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, packageName);
480
+ }
481
+
482
+ if (isIOS)
483
+ {
484
+ PlayerSettings.iOS.buildNumber = buildNumber.ToString();
485
+ PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.iOS, packageName);
486
+ }
487
+
488
+ EditorApplication.RepaintProjectWindow();
489
+ }
490
+
491
+ public void IncreaseVersion()
492
+ {
493
+ buildNumber++;
494
+ if (string.IsNullOrEmpty(buildVersion)) buildVersion = "1.0.0";
495
+
496
+ string[] arr = buildVersion.Split('.');
497
+ int last = int.Parse(arr[2]);
498
+ last++;
499
+
500
+ arr[2] = last.ToString();
501
+ buildVersion = string.Join(".", arr);
502
+ }
503
+ }
504
+
505
+ [Serializable] public class KeystoreInfo
506
+ {
507
+ public string path = "AmaGDK/Store/project.keystore";
508
+ public string alias = "alias";
509
+ public string keystorePassword = "123456789";
510
+ public string aliasPassword = "123456789";
511
+
512
+ public void Write()
513
+ {
514
+ // Must use absolute path or else gradle build fail
515
+ var keystoreFI = new FileInfo(path);
516
+ PlayerSettings.Android.keystoreName = keystoreFI.FullName;
517
+ PlayerSettings.keystorePass = keystorePassword;
518
+ PlayerSettings.Android.keyaliasPass = aliasPassword;
519
+ PlayerSettings.Android.keyaliasName = alias;
520
+ }
521
+ }
522
+ }
523
+
524
+
525
+ [InitializeOnLoad]
526
+ internal class GDKLocalBuildHelper
527
+ {
528
+ private static GDKLocalBuild build;
529
+ static GDKLocalBuildHelper()
530
+ {
531
+ EditorApplication.update -= Update;
532
+ EditorApplication.update += Update;
533
+ }
534
+
535
+ private static void Update()
536
+ {
537
+ if (EditorApplication.isCompiling || EditorApplication.isUpdating) return;
538
+
539
+ if (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)
540
+ {
541
+ if (build != null) build.StopBuild();
542
+ EditorApplication.update -= Update;
543
+ return;
544
+ }
545
+
546
+ if (build == null)
547
+ {
548
+ string[] guids = AssetDatabase.FindAssets("t:" + nameof(GDKLocalBuild)); //FindAssets uses tags check documentation for more info
549
+ if (guids.Length == 0)
550
+ {
551
+ EditorApplication.update -= Update;
552
+ return;
553
+ }
554
+
555
+ string path = AssetDatabase.GUIDToAssetPath(guids[0]);
556
+ build = AssetDatabase.LoadAssetAtPath<GDKLocalBuild>(path);
557
+ if (build == null)
558
+ {
559
+ EditorApplication.update -= Update;
560
+ return;
561
+ }
562
+ }
563
+
564
+ EditorApplication.update -= Update;
565
+
566
+ //check if timeStamp is valid
567
+ if (build.nBuildPending == 0) return;
568
+
569
+ build.ProcessBuild();
570
+ }
571
+
572
+
573
+ // [MenuItem("Window/AmaGDK/Build/Select Build Asset &#b", false, 10)]
574
+ public static void SelectBuild()
575
+ {
576
+ string[] arr = AssetDatabase.FindAssets("t:" + nameof(GDKLocalBuild));
577
+
578
+ if (arr.Length == 0)
579
+ {
580
+ Debug.LogWarning("There are no Build asset in the project!");
581
+ return;
582
+ }
583
+
584
+ // if (arr.Length > 1)
585
+ // {
586
+ // Debug.LogWarning("There are [" + arr.Length + "] Build assets in the project!");
587
+ // }
588
+
589
+ string path = AssetDatabase.GUIDToAssetPath(arr[0]);
590
+ var obj = AssetDatabase.LoadAssetAtPath<UnityObject>(path);
591
+ Selection.activeObject = obj;
592
+ EditorGUIUtility.PingObject(obj);
593
+ }
594
+
595
+ private static GDKLocalBuild FindLocalBuildAssets(string name = null, bool allowFallback = false)
596
+ {
597
+ string[] arr = AssetDatabase.FindAssets("t:" + nameof(GDKLocalBuild));
598
+ if (arr.Length == 0)
599
+ {
600
+ Debug.LogWarning("There are no Build asset in the project!");
601
+ return null;
602
+ }
603
+
604
+ for (var i = 0; i < arr.Length; i++)
605
+ {
606
+ string path = AssetDatabase.GUIDToAssetPath(arr[i]).Replace("\\", "/");
607
+ if (string.IsNullOrEmpty(name)) return AssetDatabase.LoadAssetAtPath<GDKLocalBuild>(path);
608
+
609
+ string fileName = Path.GetFileName(path);
610
+ if (fileName.ToLower().Contains(name.ToLower())) return AssetDatabase.LoadAssetAtPath<GDKLocalBuild>(path);
611
+ }
612
+
613
+ if (!allowFallback) return null;
614
+
615
+ // Fall back to the first one
616
+ string path0 = AssetDatabase.GUIDToAssetPath(arr[0]).Replace("\\", "/");
617
+ return AssetDatabase.LoadAssetAtPath<GDKLocalBuild>(path0);
618
+ }
619
+
620
+ // [MenuItem("Window/AmaGDK/Build/Android", false, 50)] public static void BuildAndroid() { BuildAndroid(false); }
621
+ // [MenuItem("Window/AmaGDK/Build/iOS", false, 51)] public static void BuildIOS() { BuildIOS(false); }
622
+ // [MenuItem("Window/AmaGDK/Build/Android - Release", false, 90)] public static void BuildAndroidRelease() { BuildAndroid(true); }
623
+ // [MenuItem("Window/AmaGDK/Build/iOS - Release", false, 91)] public static void BuildIOSRelease() { BuildIOS(true); }
624
+
625
+
626
+ private static void BuildAndroid(bool isRelease)
627
+ {
628
+ GDKLocalBuild buildAsset = FindLocalBuildAssets("android");
629
+ if (buildAsset == null)
630
+ {
631
+ Debug.LogWarning("Build Asset for Android not found!");
632
+ return;
633
+ }
634
+
635
+ buildAsset.EnableBuildForTarget(BuildTarget.Android);
636
+ buildAsset.buildRQ = GDKLocalBuild.BuildRequestTS.Generate();// isRelease
637
+ buildAsset.ProcessBuild();
638
+ }
639
+
640
+ private static void BuildIOS(bool isRelease)
641
+ {
642
+ GDKLocalBuild buildAsset = FindLocalBuildAssets("ios");
643
+ if (buildAsset == null)
644
+ {
645
+ Debug.LogWarning("Build Asset for iOS not found!");
646
+ return;
647
+ }
648
+ buildAsset.EnableBuildForTarget(BuildTarget.iOS);
649
+ buildAsset.buildRQ = GDKLocalBuild.BuildRequestTS.Generate(); //isRelease
650
+ buildAsset.ProcessBuild();
651
+ }
652
+ }
653
+
654
+ [CustomEditor(typeof(GDKLocalBuild))]
655
+ public class GDKLocalBuildEditor : UnityEditor.Editor
656
+ {
657
+ public int bIndex;
658
+ public string path;
659
+ public bool drawDefault;
660
+
661
+ public GUIStyle productNameStyle;
662
+ public GUIStyle projectStyle;
663
+
664
+ private string helpInfo;
665
+
666
+ private void Init()
667
+ {
668
+ productNameStyle = new GUIStyle(EditorStyles.largeLabel)
669
+ {
670
+ alignment = TextAnchor.MiddleCenter,
671
+ fontSize = 32
672
+ };
673
+
674
+ projectStyle = new GUIStyle(EditorStyles.miniLabel)
675
+ {
676
+ alignment = TextAnchor.MiddleCenter
677
+ };
678
+
679
+ path = Application.dataPath;
680
+ List<string> arr = path.Split('/').ToList();
681
+ arr.RemoveAt(arr.Count - 1);
682
+
683
+ if (arr.Count > 3) arr.RemoveRange(0, arr.Count - 3);
684
+
685
+ path = string.Join("/", arr);
686
+ }
687
+ public override void OnInspectorGUI()
688
+ {
689
+ var ab = (GDKLocalBuild)target;
690
+ if (ab == null) return;
691
+
692
+ if (productNameStyle == null) Init();
693
+
694
+ GUILayout.Label(PlayerSettings.productName, productNameStyle);
695
+
696
+ Rect r0 = GUILayoutUtility.GetRect(0, Screen.width, 16f, 16f);
697
+ r0.xMin = 0;
698
+ r0.xMax += 16f;
699
+ GDKLocalBuild.DrawRect(r0, new Color32(20, 20, 20, 255));
700
+
701
+ if ((Event.current.type == EventType.MouseUp) && r0.Contains(Event.current.mousePosition))
702
+ {
703
+ var fullPath = new DirectoryInfo(ab.buildFolder);
704
+ EditorUtility.RevealInFinder(fullPath.FullName);
705
+ Event.current.Use();
706
+ }
707
+
708
+ GUI.Label(r0, path, projectStyle);
709
+ EditorGUILayout.Space();
710
+
711
+ if (string.IsNullOrEmpty(helpInfo)) helpInfo = $"\nPackageName:\t{PlayerSettings.applicationIdentifier}\nTargetPlatform:\t{EditorUserBuildSettings.activeBuildTarget}\n";
712
+ EditorGUILayout.HelpBox(helpInfo, MessageType.Info);
713
+ Rect rect = GUILayoutUtility.GetLastRect();
714
+ if ((Event.current.type == EventType.MouseUp) && rect.Contains(Event.current.mousePosition))
715
+ {
716
+ Event.current.Use();
717
+ helpInfo = null;
718
+ Repaint();
719
+ }
720
+
721
+ drawDefault = GUILayout.Toggle(drawDefault, "Draw default");
722
+ if (drawDefault) DrawDefaultInspector();
723
+
724
+ EditorGUILayout.Space();
725
+
726
+ int max = ab.listBuild.Count;
727
+ if (max == 0)
728
+ {
729
+ EditorGUILayout.HelpBox("No BuildInfo!", MessageType.Warning);
730
+
731
+ if (GUILayout.Button("Create Default"))
732
+ {
733
+ new GDKLocalBuild.BuildInfo().Read();
734
+
735
+ ab.listBuild = new List<GDKLocalBuild.BuildInfo>
736
+ {
737
+ new GDKLocalBuild.BuildInfo
738
+ {
739
+ target = BuildTarget.Android, isAAB = false, splitAPK = true
740
+ }.Read(),
741
+ new GDKLocalBuild.BuildInfo
742
+ {
743
+ target = BuildTarget.Android, isAAB = true, splitAPK = true
744
+ }.Read(),
745
+ new GDKLocalBuild.BuildInfo
746
+ {
747
+ target = BuildTarget.iOS, isAAB = true, splitAPK = true
748
+ }.Read()
749
+ };
750
+ }
751
+ return;
752
+ }
753
+
754
+ serializedObject.Update();
755
+ SerializedProperty prop = serializedObject.FindProperty($"listBuild.Array.data[{bIndex}]");
756
+ prop.isExpanded = true;
757
+
758
+ GDKLocalBuild.BuildInfo info = ab.listBuild[bIndex];
759
+
760
+ EditorGUI.BeginChangeCheck();
761
+ {
762
+ EditorGUILayout.PropertyField(prop, true);
763
+ }
764
+ if (EditorGUI.EndChangeCheck()) serializedObject.ApplyModifiedProperties();
765
+
766
+ Rect r = GUILayoutUtility.GetLastRect();
767
+ r.x = 0;
768
+ r.width = Screen.width;
769
+ r.height = 16f;
770
+
771
+ GDKLocalBuild.DrawRect(r, new Color32(20, 20, 20, 255));
772
+
773
+ r.xMin += 12f;
774
+ GUI.Label(r, info.buildName);
775
+
776
+ if ((ab.icon != null) && (ab.icon == info.buildIcon)) EditorGUILayout.HelpBox("Build Icon must be different with the template icon (which will be overwritten)", MessageType.Error);
777
+
778
+ GUILayout.BeginHorizontal();
779
+ {
780
+ if (GUILayout.Button("Read")) info.Read();
781
+
782
+ if (GUILayout.Button("Write")) info.Write();
783
+
784
+ if (GUILayout.Button("+")) info.IncreaseVersion();
785
+ }
786
+ GUILayout.EndHorizontal();
787
+
788
+ if (GUILayout.Button("Dry Run"))
789
+ {
790
+ ab.DryRun(info);
791
+ helpInfo = null;
792
+ Repaint();
793
+ }
794
+
795
+ bIndex = EditorGUILayout.IntSlider(bIndex, 0, max - 1);
796
+
797
+ var nBuilds = 0;
798
+ for (var i = 0; i < ab.listBuild.Count; i++)
799
+ {
800
+ GDKLocalBuild.BuildInfo item = ab.listBuild[i];
801
+
802
+ if (!item.enable) continue;
803
+ nBuilds++;
804
+
805
+ string hint = item.target == BuildTarget.Android
806
+ ? $"{string.Join("\n", item.GetAPKNames(ab.buildFolder))}"
807
+ : item.fullBuildPath(ab.buildFolder);
808
+
809
+ EditorGUILayout.HelpBox(hint, MessageType.Info);
810
+ }
811
+
812
+ if (nBuilds == 0) return;
813
+
814
+ GUILayout.BeginHorizontal();
815
+ {
816
+ if (GUILayout.Button("Build"))
817
+ {
818
+ ab.buildRQ = GDKLocalBuild.BuildRequestTS.Generate(); //ab.runGitPostBuild
819
+ ab.ProcessBuild();
820
+ }
821
+
822
+ if (GUILayout.Button("Stop")) ab.StopBuild();
823
+ }
824
+ GUILayout.EndHorizontal();
825
+ }
826
+ }
827
+
828
+
829
+ public class ExternalProcessUtils
830
+ {
831
+ public static string FileFromTemplate(string fileName, string source, params string[] tokens)
832
+ {
833
+ for (var i = 0; i < tokens.Length; i += 2)
834
+ {
835
+ source = source.Replace(tokens[i], tokens[i + 1]);
836
+ }
837
+
838
+ string filePath = Application.dataPath.Replace("/Assets", "/" + fileName);
839
+
840
+ // Debug.LogWarning("FilePath: " + filePath);
841
+ File.WriteAllText(filePath, source);
842
+ return filePath;
843
+ }
844
+
845
+ // public static void GitResetRepo()
846
+ // {
847
+ // RunCMD("git reset --hard | git clean -df");
848
+ // }
849
+
850
+ public static void RunCMD(string cmd)
851
+ {
852
+ try
853
+ {
854
+ var startInfo = new ProcessStartInfo
855
+ {
856
+ FileName = "/bin/bash",
857
+ Arguments = $"-c \" {cmd} \" ",
858
+ CreateNoWindow = true
859
+ };
860
+
861
+ var proc = new Process
862
+ {
863
+ StartInfo = startInfo
864
+ };
865
+
866
+ proc.Start();
867
+ proc.WaitForExit();
868
+ } catch (Exception e)
869
+ {
870
+ Debug.LogWarning(e);
871
+ }
872
+ }
873
+
874
+ public static void ChmodX(string filePath)
875
+ {
876
+ RunCMD($"chmod +x {filePath}");
877
+ }
878
+
879
+ public static void Run(string filePath, bool waitExit = false)
880
+ {
881
+ ChmodX(filePath);
882
+
883
+ try
884
+ {
885
+ var process = new Process
886
+ {
887
+ EnableRaisingEvents = false,
888
+ StartInfo =
889
+ {
890
+ FileName = filePath,
891
+ UseShellExecute = false,
892
+ RedirectStandardOutput = true,
893
+ RedirectStandardInput = true,
894
+ RedirectStandardError = true
895
+ }
896
+ };
897
+
898
+ process.Start();
899
+ if (waitExit) process.WaitForExit();
900
+ } catch (Exception e)
901
+ {
902
+ Debug.LogWarning(e);
903
+ }
904
+ }
905
+ }
906
+ }