Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: CLI build command (WIP) #175

Draft
wants to merge 9 commits into
base: development
Choose a base branch
from
Prev Previous commit
Next Next commit
fix: revert from dev
  • Loading branch information
brmassa committed Sep 17, 2024
commit 6d8bd02c59cd3fdea11cd40389756c52a874a4e2
247 changes: 123 additions & 124 deletions Prowl.Editor/Build/Desktop_Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,153 +7,152 @@
using Prowl.Runtime;
using Prowl.Runtime.Utils;

namespace Prowl.Editor.Build
namespace Prowl.Editor.Build;

public class Desktop_Player : ProjectBuilder
{
public class Desktop_Player : ProjectBuilder
public enum Target
{
public enum Target
{
[Text("Win x64")] win_x64,
[Text("Win ARM x64")] win_arm64,
[Text("Win x86")] win_x86,
[Text("Win x64")] win_x64,
[Text("Win ARM x64")] win_arm64,
[Text("Win x86")] win_x86,

[Text("Linux x64")] linux_x64,
[Text("Linux x86")] linux_x86,
[Text("Linux x64")] linux_x64,
[Text("Linux x86")] linux_x86,

[Text("OSX")] osx,
[Text("OSX x64")] osx_x64,
[Text("OSX ARM x64")] osx_arm64,
[Text("OSX")] osx,
[Text("OSX x64")] osx_x64,
[Text("OSX ARM x64")] osx_arm64,

Universal
}
public Target target = Target.win_x64;
Universal
}
public Target target = Target.win_x64;

public enum Configuration
public enum Configuration
{
Debug,
Release
}
public Configuration configuration = Configuration.Release;

public enum AssetPacking
{
[Text("All Assets")] All,
[Text("Used Assets")] Used
}
public AssetPacking assetPacking = AssetPacking.Used;


protected override void Build(AssetRef<Scene>[] scenes, DirectoryInfo output)
{
output.Create();
string BuildDataPath = Path.Combine(output.FullName, "GameData");
Directory.CreateDirectory(BuildDataPath);


BoundedLog($"Compiling project assembly to {output.FullName}...");

Check failure on line 53 in Prowl.Editor/Build/Desktop_Player.cs

View workflow job for this annotation

GitHub Actions / build

The name 'BoundedLog' does not exist in the current context

Check failure on line 53 in Prowl.Editor/Build/Desktop_Player.cs

View workflow job for this annotation

GitHub Actions / build

The name 'BoundedLog' does not exist in the current context
if (!Project.Compile(Project.Active.Assembly_Proj.FullName, output, configuration == Configuration.Release))
{
Debug,
Release
Debug.LogError($"Failed to compile Project assembly!");
return;
}
public Configuration configuration = Configuration.Release;

public enum AssetPacking
BoundedLog($"Exporting and Packing assets to {BuildDataPath}...");

Check failure on line 60 in Prowl.Editor/Build/Desktop_Player.cs

View workflow job for this annotation

GitHub Actions / build

The name 'BoundedLog' does not exist in the current context

Check failure on line 60 in Prowl.Editor/Build/Desktop_Player.cs

View workflow job for this annotation

GitHub Actions / build

The name 'BoundedLog' does not exist in the current context
if (assetPacking == AssetPacking.All)
{
[Text("All Assets")] All,
[Text("Used Assets")] Used
AssetDatabase.ExportAllBuildPackages(new DirectoryInfo(BuildDataPath));
}
public AssetPacking assetPacking = AssetPacking.Used;
else
{
HashSet<Guid> assets = [];
foreach (var scene in scenes)
AssetDatabase.GetDependenciesDeep(scene.AssetID, ref assets);

// Include all Shaders in the build for the time being
foreach (var shader in AssetDatabase.GetAllAssetsOfType<Shader>())
assets.Add(shader.Item2);

protected override void Build(AssetRef<Scene>[] scenes, DirectoryInfo output)
AssetDatabase.ExportBuildPackages(assets.ToArray(), new DirectoryInfo(BuildDataPath));
}


BoundedLog($"Packing scenes...");

Check failure on line 79 in Prowl.Editor/Build/Desktop_Player.cs

View workflow job for this annotation

GitHub Actions / build

The name 'BoundedLog' does not exist in the current context

Check failure on line 79 in Prowl.Editor/Build/Desktop_Player.cs

View workflow job for this annotation

GitHub Actions / build

The name 'BoundedLog' does not exist in the current context
for (int i = 0; i < scenes.Length; i++)
{
output.Create();
string BuildDataPath = Path.Combine(output.FullName, "GameData");
Directory.CreateDirectory(BuildDataPath);


Project.BoundedLog($"Compiling project assembly to {output.FullName}...");
if (!Project.Compile(Project.Active.Assembly_Proj.FullName, output, configuration == Configuration.Release))
{
Debug.LogError($"Failed to compile Project assembly!");
return;
}

Project.BoundedLog($"Exporting and Packing assets to {BuildDataPath}...");
if (assetPacking == AssetPacking.All)
{
AssetDatabase.ExportAllBuildPackages(new DirectoryInfo(BuildDataPath));
}
else
{
HashSet<Guid> assets = [];
foreach (var scene in scenes)
AssetDatabase.GetDependenciesDeep(scene.AssetID, ref assets);

// Include all Shaders in the build for the time being
foreach (var shader in AssetDatabase.GetAllAssetsOfType<Shader>())
assets.Add(shader.Item2);

AssetDatabase.ExportBuildPackages(assets.ToArray(), new DirectoryInfo(BuildDataPath));
}


Project.BoundedLog($"Packing scenes...");
for (int i = 0; i < scenes.Length; i++)
{
Project.BoundedLog($"Packing scene_{i}.prowl...");
var scene = scenes[i];
SerializedProperty tag = Serializer.Serialize(scene.Res!);
BinaryTagConverter.WriteToFile(tag, new FileInfo(Path.Combine(BuildDataPath, $"scene_{i}.prowl")));
}


Project.BoundedLog($"Preparing project settings...");
// Find all ScriptableSingletons with the specified location
foreach (var type in RuntimeUtils.GetTypesWithAttribute<FilePathAttribute>())
if (Attribute.GetCustomAttribute(type, typeof(FilePathAttribute)) is FilePathAttribute attribute)
if (attribute.FileLocation == FilePathAttribute.Location.Setting)
BoundedLog($"Packing scene_{i}.prowl...");

Check failure on line 82 in Prowl.Editor/Build/Desktop_Player.cs

View workflow job for this annotation

GitHub Actions / build

The name 'BoundedLog' does not exist in the current context

Check failure on line 82 in Prowl.Editor/Build/Desktop_Player.cs

View workflow job for this annotation

GitHub Actions / build

The name 'BoundedLog' does not exist in the current context
var scene = scenes[i];
SerializedProperty tag = Serializer.Serialize(scene.Res!);
BinaryTagConverter.WriteToFile(tag, new FileInfo(Path.Combine(BuildDataPath, $"scene_{i}.prowl")));
}


BoundedLog($"Preparing project settings...");

Check failure on line 89 in Prowl.Editor/Build/Desktop_Player.cs

View workflow job for this annotation

GitHub Actions / build

The name 'BoundedLog' does not exist in the current context
// Find all ScriptableSingletons with the specified location
foreach (var type in RuntimeUtils.GetTypesWithAttribute<FilePathAttribute>())
if (Attribute.GetCustomAttribute(type, typeof(FilePathAttribute)) is FilePathAttribute attribute)
if (attribute.FileLocation == FilePathAttribute.Location.Setting)
{
// Use Reflection to find the CopyTo method
MethodInfo copyTo = type.BaseType.GetMethod("CopyTo", BindingFlags.Static | BindingFlags.NonPublic);
if (copyTo is null)
{
// Use Reflection to find the CopyTo method
MethodInfo copyTo = type.BaseType.GetMethod("CopyTo", BindingFlags.Static | BindingFlags.NonPublic);
if (copyTo is null)
{
Debug.LogError($"Failed to find CopyTo method for {type.Name}");
continue;
}

// Invoke the CopyTo method
string? test = BuildDataPath;
copyTo.Invoke(null, [test]);
Debug.LogError($"Failed to find CopyTo method for {type.Name}");
continue;
}

// Invoke the CopyTo method
string? test = BuildDataPath;
copyTo.Invoke(null, [test]);
}


Project.BoundedLog($"Copying Desktop player to {output.FullName}...");
// Our executable folder contains "Players\Desktop" which we need to copy over the contents
string playerPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Players", "Desktop");
if (!Directory.Exists(playerPath))
{
Debug.LogError($"Failed to find Desktop player at {playerPath}");
return;
}
BoundedLog($"Copying Desktop player to {output.FullName}...");

Check failure on line 109 in Prowl.Editor/Build/Desktop_Player.cs

View workflow job for this annotation

GitHub Actions / build

The name 'BoundedLog' does not exist in the current context
// Our executable folder contains "Players\Desktop" which we need to copy over the contents
string playerPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Players", "Desktop");
if (!Directory.Exists(playerPath))
{
Debug.LogError($"Failed to find Desktop player at {playerPath}");
return;
}

// Copy the contents of the Desktop player to the output directory, Files and Directories
var allDirectories = Directory.GetDirectories(playerPath, "*", SearchOption.AllDirectories);
foreach (var directory in allDirectories)
Directory.CreateDirectory(directory.Replace(playerPath, output.FullName));
// Copy the contents of the Desktop player to the output directory, Files and Directories
var allDirectories = Directory.GetDirectories(playerPath, "*", SearchOption.AllDirectories);
foreach (var directory in allDirectories)
Directory.CreateDirectory(directory.Replace(playerPath, output.FullName));

var allFiles = Directory.GetFiles(playerPath, "*", SearchOption.AllDirectories);
foreach (var file in allFiles)
File.Copy(file, file.Replace(playerPath, output.FullName), true);
var allFiles = Directory.GetFiles(playerPath, "*", SearchOption.AllDirectories);
foreach (var file in allFiles)
File.Copy(file, file.Replace(playerPath, output.FullName), true);

// Strip files we dont need for our target
if (target != Target.Universal)
CleanupRuntimes(output);
// Strip files we dont need for our target
if (target != Target.Universal)
CleanupRuntimes(output);

Debug.Log("**********************************************************************************************************************");
Debug.Log($"Successfully built project!");
Debug.Log("**********************************************************************************************************************");
Debug.Log($"Successfully built project!");

// Open the Build folder
AssetDatabase.OpenPath(output);
}
// Open the Build folder
AssetDatabase.OpenPath(output);
}

private void CleanupRuntimes(DirectoryInfo output)
{
string runtimesPath = Path.Combine(output.FullName, "runtimes");
if (!Directory.Exists(runtimesPath))
return;

// Remove all runtimes except the one we need
string targetRuntime = target.ToString().ToLower().Replace("_", "-");
// Remove all but the target runtime
foreach (var runtime in Directory.GetDirectories(runtimesPath))
if (!runtime.Contains(targetRuntime))
Directory.Delete(runtime, true);

// Copy all remaining files into the root output directory
foreach (var file in Directory.GetFiles(runtimesPath, "*", SearchOption.AllDirectories))
File.Copy(file, Path.Combine(output.FullName, Path.GetFileName(file)), true);

// Remove the runtimes folder
Directory.Delete(runtimesPath, true);
}
private void CleanupRuntimes(DirectoryInfo output)
{
string runtimesPath = Path.Combine(output.FullName, "runtimes");
if (!Directory.Exists(runtimesPath))
return;

// Remove all runtimes except the one we need
string targetRuntime = target.ToString().ToLower().Replace("_", "-");
// Remove all but the target runtime
foreach (var runtime in Directory.GetDirectories(runtimesPath))
if (!runtime.Contains(targetRuntime))
Directory.Delete(runtime, true);

// Copy all remaining files into the root output directory
foreach (var file in Directory.GetFiles(runtimesPath, "*", SearchOption.AllDirectories))
File.Copy(file, Path.Combine(output.FullName, Path.GetFileName(file)), true);

// Remove the runtimes folder
Directory.Delete(runtimesPath, true);
}
}
Loading