Skip to content

Commit

Permalink
Unity: build
Browse files Browse the repository at this point in the history
  • Loading branch information
Kryptos-FR committed Feb 25, 2022
1 parent 2436c5a commit 1536f0c
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Unity/Packages/com.fragcolor.chainblocks/Editor/Build.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* SPDX-License-Identifier: BUSL-1.1 */
/* Copyright © 2022 Fragcolor Pte. Ltd. */

#nullable enable

using Fragcolor.Chainblocks.UnityEditor.Settings;

using UnityEngine;

namespace Fragcolor.Chainblocks.UnityEditor.Build
{
internal abstract class BuilderBase : ScriptableObject, IBuilder
{
public abstract string Name { get; }

protected static ChainblocksSettings? Settings
{
get { return ChainblocksSettingsDefaultObject.Settings; }
}

internal abstract void Build();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* SPDX-License-Identifier: BUSL-1.1 */
/* Copyright © 2022 Fragcolor Pte. Ltd. */

#nullable enable

using System.Collections.Generic;
using System.IO;
using System.Linq;

using Fragcolor.Chainblocks.UnityEditor.Settings;

using Newtonsoft.Json;

using UnityEditor;
using UnityEditor.Build.Reporting;

using UnityEngine;

namespace Fragcolor.Chainblocks.UnityEditor.Build
{
internal sealed class DefaultBuilder : BuilderBase
{
public override string Name => "Default";

[InitializeOnLoadMethod]
private static void Initialize()
{
BuildPlayerWindow.RegisterBuildPlayerHandler(BuidWithOptions);
}

[MenuItem("Chainblocks/Build/Default")]
private static void BuildFromMenu()
{
var builder = Settings!.builders!.Find(x => x.Name == "Default");
builder.Build();
}

[MenuItem("Chainblocks/Build/Default", true)]
private static bool CanBuildFromMenu()
{
return Settings != null;
}

internal override void Build()
{
if (Settings == null) return;

BuidWithOptions(BuildPlayerWindow.DefaultBuildMethods.GetBuildPlayerOptions(default));
}

private static void BuidWithOptions(BuildPlayerOptions options)
{
if (Settings == null)
{
// Do a regular build
BuildPlayerWindow.DefaultBuildMethods.BuildPlayer(options);
return;
}

#if false
// HACK: artificially mark all scenes as dirty to force a rebuild
foreach (var scenePath in options.scenes)
{
var scene = AssetDatabase.LoadAssetAtPath<SceneAsset>(scenePath);
EditorUtility.SetDirty(scene);
}
AssetDatabase.SaveAssets();
#endif
options.options |= BuildOptions.DetailedBuildReport | BuildOptions.StrictMode;
#if UNITY_2021_2_OR_NEWER
options.options |= BuildOptions.CleanBuildCache
#endif
var report = BuildPipeline.BuildPlayer(options);
var summary = report.summary;
switch (summary.result)
{
case BuildResult.Succeeded:
Debug.Log("Build succeeded: " + summary.totalSize + " bytes");
var foundEntries = new List<ChainblocksAssetEntry>();
foreach (var asset in report.scenesUsingAssets.SelectMany(s => s.list))
{
var guid = AssetDatabase.AssetPathToGUID(asset.assetPath);
if (Settings.TryFindAssetEntry(guid, out var entry))
{
foundEntries.Add(entry);
}
}

foreach (var entry in foundEntries)
{
Debug.Log($"{entry.guid} - {entry.localPath}");
}

if (foundEntries.Count > 0)
{
var json = JsonConvert.SerializeObject(foundEntries, Formatting.Indented);
var basePath = Path.GetDirectoryName(options.locationPathName);
File.WriteAllText(Path.Combine(basePath, "FragInfo.data"), json);
}

break;

case BuildResult.Failed:
case BuildResult.Cancelled:
Debug.Log("Build failed or cancelled.");
break;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Unity/Packages/com.fragcolor.chainblocks/Editor/Build/IBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* SPDX-License-Identifier: BUSL-1.1 */
/* Copyright © 2022 Fragcolor Pte. Ltd. */

#nullable enable

namespace Fragcolor.Chainblocks.UnityEditor.Build
{
internal interface IBuilder
{
string Name { get; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* SPDX-License-Identifier: BUSL-1.1 */
/* SPDX-License-Identifier: BUSL-1.1 */
/* Copyright © 2022 Fragcolor Pte. Ltd. */

#nullable enable
Expand Down Expand Up @@ -59,6 +59,9 @@ private void OnGUI()
return;
}

var contentRect = new Rect(0, 0, position.width, position.height);
TopToolbar(contentRect);

_hash = EditorGUILayout.TextField("Fragment hash", _hash);
EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(_hash));
if (GUILayout.Button(!_requests.ContainsKey(_hash) ? "Get Fragment" : "Cancel"))
Expand All @@ -78,6 +81,37 @@ private void OnGUI()

EditorGUI.EndDisabledGroup();
}

private static void TopToolbar(Rect _)
{
if (Settings == null) return;

GUILayout.BeginHorizontal(EditorStyles.toolbar);

var cBuild = new GUIContent("Build");
var rBuild = GUILayoutUtility.GetRect(cBuild, EditorStyles.toolbarDropDown);
if (EditorGUI.DropdownButton(rBuild, cBuild, FocusType.Passive, EditorStyles.toolbarDropDown))
{
var menu = new GenericMenu();
for (var i = 0; i < Settings.builders!.Count; i++)
{
var builder = Settings.builders[i];
menu.AddItem(new GUIContent(builder.Name), false, OnBuild, i);
}

menu.DropDown(rBuild);
}

GUILayout.EndHorizontal();

static void OnBuild(object boxed)
{
var i = (int) boxed;
var builder = Settings!.builders![i];
builder.Build();
}
}

private static void OnUpdate()
{
// tick all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.IO;
using System.Linq;

using Fragcolor.Chainblocks.UnityEditor.Build;

using UnityEditor;

using UnityEngine;
Expand All @@ -20,6 +22,9 @@ namespace Fragcolor.Chainblocks.UnityEditor.Settings
/// </summary>
internal sealed class ChainblocksSettings : ScriptableObject
{
[SerializeField]
internal List<BuilderBase>? builders = new List<BuilderBase>();

/// <summary>
/// The list of asset registries.
/// </summary>
Expand Down Expand Up @@ -138,6 +143,11 @@ internal string ConfigFolder
get { return Path.GetDirectoryName(AssetPath); }
}

private string BuilderFolder
{
get { return ConfigFolder + "/Builders"; }
}

/// <summary>
/// The folder for the registry assets.
/// </summary>
Expand Down Expand Up @@ -424,12 +434,35 @@ private static ChainblocksAssetEntry CreateAndAddEntryToRegistry(string guid, Ch
return entry;
}

private T CreateBuilderAsset<T>()
where T : BuilderBase
{
if (!Directory.Exists(BuilderFolder))
Directory.CreateDirectory(BuilderFolder);

var path = $"{BuilderFolder}/{typeof(T).Name}.asset";
if (!File.Exists(path))
{
AssetDatabase.CreateAsset(CreateInstance<T>(), path);
}

return AssetDatabase.LoadAssetAtPath<T>(path);
}

/// <summary>
/// Ensures the state of this object is consistent.
/// </summary>
private void Validate()
{
// TODO: sanity checks

if (builders == null || builders.Count == 0)
{
builders = new List<BuilderBase>
{
CreateBuilderAsset<DefaultBuilder>()
};
}
}
}
}

0 comments on commit 1536f0c

Please sign in to comment.