Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 deletions Basis/Packages/com.cnlohr.cilbox/Cilbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2409,14 +2409,14 @@ public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
}
public class CilboxScenePostprocessor {
//[PostProcessSceneAttribute (2)] This is actually called by IProcessSceneWithReport
public static void OnPostprocessScene(UnityEngine.SceneManagement.Scene scene) {
public static void OnPostprocessScene(UnityEngine.SceneManagement.Scene? scene) {

ProfilerMarker perf = new ProfilerMarker("Initial Setup"); perf.Begin();

MonoBehaviour [] allBehavioursThatNeedCilboxing = CilboxUtil.GetAllBehavioursThatNeedCilboxing();

MonoBehaviour [] allBehavioursThatNeedCilboxing = CilboxUtil.GetAllBehavioursThatNeedCilboxing(scene);
Debug.Log( $"Postprocessing scene. Cilbox scripts to do: {allBehavioursThatNeedCilboxing.Length}" );
if( allBehavioursThatNeedCilboxing.Length == 0 ) return;
if( allBehavioursThatNeedCilboxing.Length == 0 )
return;


Dictionary< String, Serializee > assemblyMetadata = new Dictionary< String, Serializee >();
Expand All @@ -2441,7 +2441,7 @@ public static void OnPostprocessScene(UnityEngine.SceneManagement.Scene scene) {

if( scene != null )
{
GameObject[] rootObjects = scene.GetRootGameObjects();
GameObject[] rootObjects =((UnityEngine.SceneManagement.Scene) scene).GetRootGameObjects();
foreach (GameObject root in rootObjects)
{
MonoBehaviour[] components = root.GetComponentsInChildren<MonoBehaviour>(true);
Expand All @@ -2463,7 +2463,6 @@ public static void OnPostprocessScene(UnityEngine.SceneManagement.Scene scene) {
else
{
// Collect ALL cilboxable classes if no scene active.

foreach( System.Reflection.Assembly proxyAssembly in assys )
{
foreach (Type t in proxyAssembly.GetTypes())
Expand All @@ -2476,7 +2475,6 @@ public static void OnPostprocessScene(UnityEngine.SceneManagement.Scene scene) {
}
}
}

{
for( int typeIndex = 0; typeIndex < TypesInUseInSceneList.Count; typeIndex++ )
{
Expand Down Expand Up @@ -3003,24 +3001,19 @@ public static void OnPostprocessScene(UnityEngine.SceneManagement.Scene scene) {
perf.End(); perf = new ProfilerMarker( "Updating Game Objects" ); perf.Begin();

// Iterate over all GameObjects, and find the ones that have Cilboxable scripts.
object[] obj = GameObject.FindObjectsByType<GameObject>(FindObjectsInactive.Include, FindObjectsSortMode.None);
foreach (object o in obj)
foreach (MonoBehaviour m in allBehavioursThatNeedCilboxing)
{
GameObject g = (GameObject) o;
MonoBehaviour [] scripts = g.GetComponents<MonoBehaviour>();
foreach (MonoBehaviour m in scripts )
{
// Skip null objects.
if (m == null)
continue;
if( !CilboxUtil.HasCilboxableAttribute( m.GetType() ) )
continue;

CilboxProxy p = g.AddComponent<CilboxProxy>();
refProxies.Add( p );
refProxiesOrig.Add( m );
refToProxyMap[m] = p;
}
GameObject g = m.gameObject;
// Skip null objects.
if (m == null)
continue;
if( !CilboxUtil.HasCilboxableAttribute( m.GetType() ) )
continue;

CilboxProxy p = g.AddComponent<CilboxProxy>();
refProxies.Add( p );
refProxiesOrig.Add( m );
refToProxyMap[m] = p;
}
perf.End(); perf = new ProfilerMarker( "Setting Up Proxies" ); perf.Begin();

Expand Down
63 changes: 49 additions & 14 deletions Basis/Packages/com.cnlohr.cilbox/CilboxUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,26 +1008,61 @@ public static void AssemblyLoggerTask( String fileName, String assemblyData, Cil
///////////////////////////////////////////////////////////////////////////
// REFLECTION HELPERS ///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
public static MonoBehaviour [] GetAllBehavioursThatNeedCilboxing()
public static MonoBehaviour [] GetAllBehavioursThatNeedCilboxing(UnityEngine.SceneManagement.Scene? scene = null)
{
List<MonoBehaviour> ret = new List<MonoBehaviour>();

object[] objToCheck = GameObject.FindObjectsByType<GameObject>(FindObjectsInactive.Include, FindObjectsSortMode.None);
foreach (object o in objToCheck)
if (scene != null)
{
GameObject g = (GameObject) o;
MonoBehaviour [] scripts = g.GetComponents<MonoBehaviour>();
foreach (MonoBehaviour m in scripts )
UnityEngine.SceneManagement.Scene _scene = (UnityEngine.SceneManagement.Scene)scene;
if( !_scene.IsValid())
{
// Skip null objects.
if (m == null)
continue;
if( !HasCilboxableAttribute( m.GetType() ) )
Debug.LogWarning($"Scene {_scene.name} is not valid. Returning empty MonoBehaviour array.");
return Array.Empty<MonoBehaviour>();
}

List<MonoBehaviour> ret = new List<MonoBehaviour>();
GameObject[] roots = _scene.GetRootGameObjects();
int rootLength = roots.Length;
for( int i = 0; i < rootLength; i++ )
{
GameObject root = roots[i];
if( root == null )
continue;
ret.Add(m);

MonoBehaviour[] scripts = root.GetComponentsInChildren<MonoBehaviour>(true);
int scriptLength = scripts.Length;
for( int scriptIndex = 0; scriptIndex < scriptLength; scriptIndex++ )
{
MonoBehaviour script = scripts[scriptIndex];
if( script == null )
continue;
if( !HasCilboxableAttribute( script.GetType() ) )
continue;
ret.Add( script );
}
}
return ret.ToArray();
}
else
{
List<MonoBehaviour> ret = new List<MonoBehaviour>();

object[] objToCheck = GameObject.FindObjectsByType<GameObject>(FindObjectsInactive.Include, FindObjectsSortMode.None);
foreach (object o in objToCheck)
{
GameObject g = (GameObject) o;
MonoBehaviour [] scripts = g.GetComponents<MonoBehaviour>();
foreach (MonoBehaviour m in scripts )
{
// Skip null objects.
if (m == null)
continue;
if( !HasCilboxableAttribute( m.GetType() ) )
continue;
ret.Add(m);
}
}
return ret.ToArray();
}
return ret.ToArray();
}

// Checks if the type is nested within a type with the CilboxableAttribute.
Expand Down
Loading