Skip to content

Commit

Permalink
tweak(clrcore-v2): shared library update
Browse files Browse the repository at this point in the history
`ServerScript` & `ClientScript` are now present in both client and server environments, but will only load in their respective environment.
  • Loading branch information
thorium-cfx committed Jun 15, 2023
1 parent 9fb1c81 commit 0b8a301
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
23 changes: 19 additions & 4 deletions code/client/clrcore-v2/BaseScript.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Security;

Expand Down Expand Up @@ -64,7 +65,13 @@ internal enum State : byte
[SecuritySafeCritical]
internal void Initialize()
{
if (m_state != State.Uninitialized)
if (m_state != State.Uninitialized
#if IS_FXSERVER
|| this is ClientScript // shared-lib support: disallow client scripts to be loaded in server environments
#else
|| this is ServerScript // shared-lib support: disallow server scripts to be loaded in client environments
#endif
)
return;

var scriptMethods = this.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
Expand Down Expand Up @@ -323,9 +330,17 @@ public void RemoveRemoteHandler(RemoteHandler deleg)
#endif
}

#if !IS_FXSERVER
/// <inheritdoc cref="BaseScript"/>
/// <remarks>Will and can only be activated in client environments</remarks>
#if IS_FXSERVER
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public abstract class ClientScript : BaseScript { }
#else
public abstract class ServerScript : BaseScript { }

/// <inheritdoc cref="BaseScript"/>
/// <remarks>Will and can only be activated in server environments</remarks>
#if !IS_FXSERVER
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public abstract class ServerScript : BaseScript { }
}
65 changes: 25 additions & 40 deletions code/client/clrcore-v2/ScriptManager.cs
Expand Up @@ -60,48 +60,33 @@ internal static void RemoveScript(BaseScript script)

private static void LoadScripts(Assembly assembly)
{
IEnumerable<Type> definedTypes;

foreach (var module in assembly.GetModules(false))
{
// We have ClientScript and ServerScript defined only in the respective environments.
// Handle type load exceptions and keep going.
// See https://stackoverflow.com/a/11915414

Func<Type, bool> typesPredicate = t =>
t != null
&& !t.IsAbstract
&& t.IsSubclassOf(typeof(BaseScript))
&& t.GetConstructor(Type.EmptyTypes) != null
&& !(t.GetCustomAttribute<EnableOnLoadAttribute>()?.Enable == false);

/*foreach (var type in assembly.GetTypes())
{
DevDebug.WriteLine(type.BaseType.BaseType.Assembly.Location.ToString());
}*/

try
{
definedTypes = assembly.GetTypes().Where(typesPredicate);
}
catch (ReflectionTypeLoadException e)
foreach (var type in module.GetTypes())
{
definedTypes = e.Types.Where(typesPredicate);
}
}

foreach (var type in definedTypes)
{
try
{
var derivedScript = Activator.CreateInstance(type) as BaseScript;

Debug.WriteLine("Instantiated instance of script {0}.", type.FullName);

AddScript(derivedScript);
}
catch (Exception e)
{
Debug.WriteLine("Failed to instantiate instance of script {0}: {1}", type.FullName, e.ToString());
if (!type.IsAbstract
&& type.IsSubclassOf(typeof(BaseScript))
#if IS_FXSERVER
&& !type.IsSubclassOf(typeof(ClientScript)) // shared-lib support: disallow client scripts to be loaded in server environments
#else
&& !type.IsSubclassOf(typeof(ServerScript)) // shared-lib support: disallow server scripts to be loaded in client environments
#endif
&& type.GetConstructor(Type.EmptyTypes) != null
&& !(type.GetCustomAttribute<EnableOnLoadAttribute>()?.Enable == false))
{
try
{
var derivedScript = Activator.CreateInstance(type) as BaseScript;

Debug.WriteLine("Instantiated instance of script {0}.", type.FullName);

AddScript(derivedScript);
}
catch (Exception e)
{
Debug.WriteLine("Failed to instantiate instance of script {0}: {1}", type.FullName, e.ToString());
}
}
}
}
}
Expand Down

0 comments on commit 0b8a301

Please sign in to comment.