Skip to content

Commit

Permalink
WIP on PolyToolkitHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
mtschoen-unity committed Dec 7, 2019
1 parent 407ae3a commit 29eb3ff
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
124 changes: 124 additions & 0 deletions Editor/PolyToolkitHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
static class PolyToolkitHelper
{
// ReSharper disable InconsistentNaming
[Serializable]
class AssemblyDefinition
{
[SerializeField]
string name;

[SerializeField]
string[] references;

[SerializeField]
string[] optionalUnityReferences;

[SerializeField]
string[] includePlatforms;

[SerializeField]
string[] excludePlatforms;

[SerializeField]
bool allowUnsafeCode;

public string Name { get { return name; } set { name = value; } }
public string[] References { get { return references; } set { references = value; } }
public string[] IncludePlatforms { get { return includePlatforms; } set { includePlatforms = value; } }
public string[] ExcludePlatforms { get { return excludePlatforms; } set { excludePlatforms = value; } }
public bool AllowUnsafeCode { get { return allowUnsafeCode; } set { allowUnsafeCode = value; } }
}
// ReSharper restore InconsistentNaming

const string k_PolyApiGuid = "bffc1b9d57b39eb47a9ca38dcb349814";
const string k_PtUtilsGuid = "bffc1b9d57b39eb47a9ca38dcb349814";
const string k_AssemblyDefinitionExtension = ".asmdef";
const string k_RuntimeAssemblyDefinitionName = "PolyToolkit";
const string k_EditorAssemblyDefinitionName = "PolyToolkit.Editor";
const string k_RuntimeAssemblyDefinitionFileName = k_RuntimeAssemblyDefinitionName + k_AssemblyDefinitionExtension;
const string k_EditorAssemblyDefinitionFileName = k_EditorAssemblyDefinitionName + k_AssemblyDefinitionExtension;
const string k_IncludePolyToolkitDefine = "INCLUDE_POLY_TOOLKIT";
static readonly string[] k_IncludePlatformsEditorOnly = { "Editor" };

static PolyToolkitHelper()
{
var polyApiPath = AssetDatabase.GUIDToAssetPath(k_PolyApiGuid);
if (string.IsNullOrEmpty(polyApiPath))
{
Cleanup();
return;
}

var runtimeFolder = Directory.GetParent(polyApiPath).Parent;
if (runtimeFolder == null)
{
Cleanup();
return;
}

var runtimeAssemblyDefinitionPath = Path.Combine(runtimeFolder.ToString(), k_RuntimeAssemblyDefinitionFileName);
if (File.Exists(runtimeAssemblyDefinitionPath))
return;

var runtimeAssemblyDefinition = new AssemblyDefinition
{
Name = k_RuntimeAssemblyDefinitionName,
AllowUnsafeCode = true
};

File.WriteAllText(runtimeAssemblyDefinitionPath, JsonUtility.ToJson(runtimeAssemblyDefinition));

var ptUtilsPath = AssetDatabase.GUIDToAssetPath(k_PtUtilsGuid);
if (string.IsNullOrEmpty(ptUtilsPath))
{
Cleanup();
return;
}

var editorFolder = Directory.GetParent(ptUtilsPath);
if (editorFolder == null)
{
Cleanup();
return;
}

var editorAssemblyDefinitionPath = Path.Combine(editorFolder.ToString(), k_EditorAssemblyDefinitionFileName);
if (File.Exists(editorAssemblyDefinitionPath))
{
Cleanup();
return;
}

var editorAssemblyDefinition = new AssemblyDefinition
{
Name = k_EditorAssemblyDefinitionName,
References = new[] { k_RuntimeAssemblyDefinitionName },
IncludePlatforms = k_IncludePlatformsEditorOnly
};

File.WriteAllText(editorAssemblyDefinitionPath, JsonUtility.ToJson(editorAssemblyDefinition));
}

/// <summary>
/// Attempt to remove the INCLUDE_POLY_TOOLKIT define if it exists, but PolyToolkit cannot be found
/// </summary>
static void Cleanup()
{
var currentGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
var defineString = PlayerSettings.GetScriptingDefineSymbolsForGroup(currentGroup);
if (string.IsNullOrEmpty(defineString) || !defineString.Contains(k_IncludePolyToolkitDefine))
return;

var defines = defineString.Split(';').ToList();
defines.Remove(k_IncludePolyToolkitDefine);
defineString = string.Join(";", defines);
PlayerSettings.SetScriptingDefineSymbolsForGroup(currentGroup, defineString);
}
}
11 changes: 11 additions & 0 deletions Editor/PolyToolkitHelper.cs.meta

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

1 change: 0 additions & 1 deletion Runtime/Scripts/Core/Contexts/EditorXRContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.Labs.EditorXR.Utilities;
using Unity.Labs.ModuleLoader;
Expand Down

0 comments on commit 29eb3ff

Please sign in to comment.