#define ENV_SET_INCLUDED_SHADERS
using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using UnityEditor.Build;
using UnityEditor.PackageManager.Requests;
using UnityEditor.SceneManagement;
using UnityEditor.XR.Management;
using UnityEditor.XR.Management.Metadata;
using UnityEngine.Rendering;
using UnityEngine.XR;
using UnityEngine.XR.Management;
using VRC.SDKBase.Validation.Performance.Stats;
using Object = UnityEngine.Object;
namespace VRC.Editor
{
///
/// Setup up SDK env on editor launch
///
[InitializeOnLoad]
public class EnvConfig
{
private static readonly BuildTarget[] relevantBuildTargets =
{
BuildTarget.Android,
BuildTarget.iOS,
BuildTarget.StandaloneLinux64,
BuildTarget.StandaloneWindows, BuildTarget.StandaloneWindows64,
BuildTarget.StandaloneOSX
};
private static readonly BuildTarget[] allowedBuildtargets = {
BuildTarget.StandaloneWindows64,
BuildTarget.Android,
BuildTarget.iOS,
};
private static readonly Dictionary allowedGraphicsAPIs = new Dictionary()
{
{ BuildTarget.Android, new[] { GraphicsDeviceType.OpenGLES3, /* GraphicsDeviceType.Vulkan */ } },
{ BuildTarget.iOS, new[] { GraphicsDeviceType.Metal } },
{ BuildTarget.StandaloneLinux64, null },
{ BuildTarget.StandaloneWindows, new[] { GraphicsDeviceType.Direct3D11 } },
{ BuildTarget.StandaloneWindows64, new[] { GraphicsDeviceType.Direct3D11 } },
{ BuildTarget.StandaloneOSX, new[] { GraphicsDeviceType.Metal } }
};
private struct SDKInfo
{
public string Name;
public string LoaderType;
}
private static readonly List xrSDKs = new List
{
new SDKInfo { Name = "Oculus", LoaderType = "Unity.XR.Oculus.OculusLoader" },
new SDKInfo { Name = "OpenVR", LoaderType = "Unity.XR.OpenVR.OpenVRLoader" },
new SDKInfo { Name = "MockHMD", LoaderType = "Unity.XR.MockHMD.MockHMDLoader" },
new SDKInfo { Name = "OpenXR", LoaderType = "UnityEngine.XR.OpenXR.OpenXRLoader" },
};
private static readonly List loadersThatNeedsRestart = new List
{
"UnityEngine.XR.OpenXR.OpenXRLoader",
};
private static bool _requestConfigureSettings = true;
private static readonly Lazy _debugCategoryName = new Lazy(InitializeLogging);
private static string DebugCategoryName => _debugCategoryName.Value;
private static string InitializeLogging()
{
const string categoryName = "EnvConfig";
VRC.Core.Logger.DescribeCategory(categoryName, "EC", VRC.Core.Logger.Color.cyan);
//VRC.Core.Logger.EnableCategory(categoryName);
return categoryName;
}
static EnvConfig()
{
EditorApplication.update += EditorUpdate;
}
private static void EditorUpdate()
{
try
{
if(!_requestConfigureSettings || EditorApplication.isPlayingOrWillChangePlaymode)
{
_requestConfigureSettings = false;
return;
}
if(ConfigureSettings())
{
_requestConfigureSettings = false;
}
}
catch(Exception e)
{
Debug.LogException(e);
_requestConfigureSettings = false;
}
}
private static void RequestConfigureSettings()
{
_requestConfigureSettings = true;
}
[UnityEditor.Callbacks.DidReloadScripts(int.MaxValue)]
private static void DidReloadScripts()
{
RequestConfigureSettings();
}
private static bool ConfigureSettings()
{
CheckForFirstInit();
if(EditorApplication.isPlayingOrWillChangePlaymode || EditorApplication.isUpdating)
{
return false;
}
ConfigurePlayerSettings();
if(!VRC.Core.ConfigManager.RemoteConfig.IsInitialized())
{
VRC.Core.API.SetOnlineMode(true);
VRC.Core.ConfigManager.RemoteConfig.Init();
}
LoadEditorResources();
return true;
}
private static void SetDLLPlatforms(string dllName, bool active, bool isPreloaded = false)
{
string[] assetGuids = AssetDatabase.FindAssets(dllName);
foreach(string guid in assetGuids)
{
string dllPath = AssetDatabase.GUIDToAssetPath(guid);
if(string.IsNullOrEmpty(dllPath) || dllPath.ToLower().EndsWith(".dll") == false)
{
continue;
}
PluginImporter importer = AssetImporter.GetAtPath(dllPath) as PluginImporter;
if(importer == null)
{
continue;
}
bool allCorrect = true;
if(importer.GetCompatibleWithAnyPlatform() != active)
{
allCorrect = false;
}
else
{
if(importer.GetCompatibleWithAnyPlatform())
{
if(importer.GetExcludeEditorFromAnyPlatform() != !active ||
importer.GetExcludeFromAnyPlatform(BuildTarget.StandaloneWindows) != !active)
{
allCorrect = false;
}
}
else
{
if(importer.GetCompatibleWithEditor() != active ||
importer.GetCompatibleWithPlatform(BuildTarget.StandaloneWindows) != active)
{
allCorrect = false;
}
}
if(importer.isPreloaded != isPreloaded && isPreloaded)
{
allCorrect = false;
}
}
if(allCorrect)
{
continue;
}
if(active)
{
importer.SetCompatibleWithAnyPlatform(true);
importer.SetExcludeEditorFromAnyPlatform(false);
importer.SetExcludeFromAnyPlatform(BuildTarget.Android, false);
importer.SetExcludeFromAnyPlatform(BuildTarget.iOS, false);
importer.SetExcludeFromAnyPlatform(BuildTarget.StandaloneWindows, false);
importer.SetExcludeFromAnyPlatform(BuildTarget.StandaloneWindows64, false);
importer.SetExcludeFromAnyPlatform(BuildTarget.StandaloneLinux64, false);
importer.isPreloaded = isPreloaded;
}
else
{
importer.SetCompatibleWithAnyPlatform(false);
importer.SetCompatibleWithEditor(false);
importer.SetCompatibleWithPlatform(BuildTarget.Android, false);
importer.SetCompatibleWithPlatform(BuildTarget.iOS, false);
importer.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, false);
importer.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, false);
importer.SetCompatibleWithPlatform(BuildTarget.StandaloneLinux64, false);
importer.isPreloaded = isPreloaded;
}
importer.SaveAndReimport();
return;
}
}
[MenuItem("VRChat SDK/Utilities/Force Configure Player Settings")]
public static void ConfigurePlayerSettings()
{
VRC.Core.Logger.Log("Setting required PlayerSettings...", DebugCategoryName);
SetBuildTarget();
// Needed for Microsoft.CSharp namespace in DLLMaker
// Doesn't seem to work though
NamedBuildTarget namedBuildTarget = NamedBuildTarget.FromBuildTargetGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
if (PlayerSettings.GetApiCompatibilityLevel(namedBuildTarget) != ApiCompatibilityLevel.NET_4_6)
{
PlayerSettings.SetApiCompatibilityLevel(namedBuildTarget, ApiCompatibilityLevel.NET_4_6);
}
if(!PlayerSettings.runInBackground)
{
PlayerSettings.runInBackground = true;
}
SetDLLPlatforms("VRCCore-Standalone", false);
SetDLLPlatforms("VRCCore-Editor", true);
SetSpatializerPluginSettings();
SetDefaultGraphicsAPIs();
SetGraphicsSettings();
SetQualitySettings();
SetAudioSettings();
SetPlayerSettings();
SetVRSDKs(EditorUserBuildSettings.selectedBuildTargetGroup, new string[] { "None", "Oculus" });
SetTextureSettings();
}
internal static void EnableBatching(bool enable)
{
PlayerSettings[] playerSettings = Resources.FindObjectsOfTypeAll();
if(playerSettings == null)
{
return;
}
SerializedObject playerSettingsSerializedObject = new SerializedObject(playerSettings.Cast