Skip to content

Commit

Permalink
[SDK-558] feat: refactored symbol edit logic and added symbol remove …
Browse files Browse the repository at this point in the history
…logic (#133)

- remove define symbols when core is removed via package manager
- refactor define symbol edit logic into separate class
  • Loading branch information
HarrisonHough authored Oct 18, 2023
1 parent 3fc5d86 commit f43e3ee
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 39 deletions.
56 changes: 18 additions & 38 deletions Editor/Module Management/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public static class ModuleInstaller

private const int THREAD_SLEEP_TIME = 100;
private const string PROGRESS_BAR_TITLE = "Ready Player Me";
private const string GLTFAST_SYMBOL = "GLTFAST";
private const string READY_PLAYER_ME_SYMBOL = "READY_PLAYER_ME";
private const string GLTFAST_NAME = "com.atteneder.gltfast";
private const string WEBVIEW_NAME = "webview";

Expand All @@ -39,6 +37,8 @@ public static class ModuleInstaller

static ModuleInstaller()
{
Events.registeringPackages -= OnRegisteringPackages;
Events.registeringPackages += OnRegisteringPackages;
# if RPM_DEVELOPMENT
modulesInstalled = true;
#endif
Expand All @@ -47,16 +47,29 @@ static ModuleInstaller()
InstallModules();
EditorApplication.delayCall += DelayCreateCoreSettings;
}

#if !GLTFAST
if (IsModuleInstalled(GLTFAST_NAME))
{
AddGltfastSymbol();
AddScriptingDefineSymbolToAllBuildTargetGroups(READY_PLAYER_ME_SYMBOL);
DefineSymbolHelper.AddSymbols();
}
#endif

}

/// <summary>
/// Called when a package is about to be added, removed or changed.
/// </summary>
/// <param name="args">Describes the <c>PackageInfo</c> entries of packages currently registering.</param>
private static void OnRegisteringPackages(PackageRegistrationEventArgs args)
{
// Core module uninstalled
if (args.removed != null && args.removed.Any(p => p.name == ModuleList.Core.name))
{
DefineSymbolHelper.RemoveSymbols();
ProjectPrefs.SetBool(ProjectPrefs.FIRST_TIME_SETUP_DONE, false);
}
}

private static void DelayCreateCoreSettings()
{
Expand Down Expand Up @@ -158,39 +171,6 @@ public static PackageInfo[] GetPackageList()
return listRequest.Result.ToArray();
}

public static void AddGltfastSymbol()
{
AddScriptingDefineSymbolToAllBuildTargetGroups(GLTFAST_SYMBOL);
}

private static void AddScriptingDefineSymbolToAllBuildTargetGroups(string defineSymbol)
{
foreach (BuildTarget target in Enum.GetValues(typeof(BuildTarget)))
{
BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target);

if (group == BuildTargetGroup.Unknown)
{
continue;
}

List<string> defineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';').Select(d => d.Trim()).ToList();

if (defineSymbols.Contains(defineSymbol)) continue;
defineSymbols.Add(defineSymbol);
try
{
PlayerSettings.SetScriptingDefineSymbolsForGroup(group, string.Join(";", defineSymbols.ToArray()));
}
catch (Exception e)
{
Debug.LogWarning("Could not set RPM " + defineSymbol + " defines for build target: " + target + " group: " + group + " " + e);
}
}

CompilationPipeline.RequestScriptCompilation();
}

/// <summary>
/// Check all modules installed successfully
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class AvatarLoaderEditor : EditorWindow
public static void ShowWindow()
{
#if !GLTFAST
ModuleInstaller.AddGltfastSymbol();
DefineSymbolHelper.AddSymbols();
#endif
var window = GetWindow<AvatarLoaderEditor>();
window.titleContent = new GUIContent(AVATAR_LOADER);
Expand Down
72 changes: 72 additions & 0 deletions Editor/Utils/DefineSymbolHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditor.Compilation;

namespace ReadyPlayerMe.Core.Editor
{
public static class DefineSymbolHelper
{
private const string GLTFAST_SYMBOL = "GLTFAST";
private const string READY_PLAYER_ME_SYMBOL = "READY_PLAYER_ME";

private static void ModifyScriptingDefineSymbolInAllBuildTargetGroups(string defineSymbol, bool addSymbol)
{
foreach (BuildTarget target in Enum.GetValues(typeof(BuildTarget)))
{
BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target);

if (group == BuildTargetGroup.Unknown)
{
continue;
}

List<string> defineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';').Select(d => d.Trim()).ToList();

if (addSymbol && !defineSymbols.Contains(defineSymbol))
{
defineSymbols.Add(defineSymbol);
}
else if (!addSymbol && defineSymbols.Contains(defineSymbol))
{
defineSymbols.Remove(defineSymbol);
}
else
{
continue;
}

try
{
PlayerSettings.SetScriptingDefineSymbolsForGroup(group, string.Join(";", defineSymbols.ToArray()));
}
catch (Exception e)
{
var actionWord = addSymbol ? "set" : "remove";
Debug.LogWarning($"Could not {actionWord} {defineSymbol} defines for build target: {target} group: {group} {e}");
}
}

if (addSymbol)
{
CompilationPipeline.RequestScriptCompilation();
}
}

public static void AddSymbols()
{
ModifyScriptingDefineSymbolInAllBuildTargetGroups(GLTFAST_SYMBOL, true);
ModifyScriptingDefineSymbolInAllBuildTargetGroups(READY_PLAYER_ME_SYMBOL, true);
CompilationPipeline.RequestScriptCompilation();
}

public static void RemoveSymbols()
{
ModifyScriptingDefineSymbolInAllBuildTargetGroups(GLTFAST_SYMBOL, false);
ModifyScriptingDefineSymbolInAllBuildTargetGroups(READY_PLAYER_ME_SYMBOL, false);
}

}
}
11 changes: 11 additions & 0 deletions Editor/Utils/DefineSymbolHelper.cs.meta

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

0 comments on commit f43e3ee

Please sign in to comment.