2021-05-03 20:13:03 +01:00
using Mono.Cecil ;
namespace QNetWeaver
{
internal class MonoBehaviourProcessor
{
2021-05-03 20:27:52 +01:00
public MonoBehaviourProcessor ( TypeDefinition td ) = > m_td = td ;
2021-05-03 20:13:03 +01:00
public void Process ( )
{
2021-05-03 20:27:52 +01:00
ProcessSyncVars ( ) ;
ProcessMethods ( ) ;
2021-05-03 20:13:03 +01:00
}
private void ProcessSyncVars ( )
{
2021-05-03 20:27:52 +01:00
foreach ( var fieldDefinition in m_td . Fields )
2021-05-03 20:13:03 +01:00
{
foreach ( var customAttribute in fieldDefinition . CustomAttributes )
{
if ( customAttribute . AttributeType . FullName = = Weaver . SyncVarType . FullName )
{
Log . Error ( string . Concat ( new string [ ]
{
"Script " ,
2021-05-03 20:27:52 +01:00
m_td . FullName ,
2021-05-03 20:13:03 +01:00
" uses [SyncVar] " ,
fieldDefinition . Name ,
" but is not a NetworkBehaviour."
} ) ) ;
Weaver . fail = true ;
}
}
2021-06-18 22:40:38 +01:00
2021-05-03 20:13:03 +01:00
if ( Helpers . InheritsFromSyncList ( fieldDefinition . FieldType ) )
{
2021-05-03 20:27:52 +01:00
Log . Error ( string . Format ( "Script {0} defines field {1} with type {2}, but it's not a NetworkBehaviour" , m_td . FullName , fieldDefinition . Name , Helpers . PrettyPrintType ( fieldDefinition . FieldType ) ) ) ;
2021-05-03 20:13:03 +01:00
Weaver . fail = true ;
}
}
}
private void ProcessMethods ( )
{
2021-05-03 20:27:52 +01:00
foreach ( var methodDefinition in m_td . Methods )
2021-05-03 20:13:03 +01:00
{
foreach ( var customAttribute in methodDefinition . CustomAttributes )
{
if ( customAttribute . AttributeType . FullName = = Weaver . CommandType . FullName )
{
Log . Error ( string . Concat ( new string [ ]
{
"Script " ,
2021-05-03 20:27:52 +01:00
m_td . FullName ,
2021-05-03 20:13:03 +01:00
" uses [Command] " ,
methodDefinition . Name ,
" but is not a NetworkBehaviour."
} ) ) ;
Weaver . fail = true ;
}
2021-06-18 22:40:38 +01:00
2021-05-03 20:13:03 +01:00
if ( customAttribute . AttributeType . FullName = = Weaver . ClientRpcType . FullName )
{
Log . Error ( string . Concat ( new string [ ]
{
"Script " ,
2021-05-03 20:27:52 +01:00
m_td . FullName ,
2021-05-03 20:13:03 +01:00
" uses [ClientRpc] " ,
methodDefinition . Name ,
" but is not a NetworkBehaviour."
} ) ) ;
Weaver . fail = true ;
}
2021-06-18 22:40:38 +01:00
2021-05-03 20:13:03 +01:00
if ( customAttribute . AttributeType . FullName = = Weaver . TargetRpcType . FullName )
{
Log . Error ( string . Concat ( new string [ ]
{
"Script " ,
2021-05-03 20:27:52 +01:00
m_td . FullName ,
2021-05-03 20:13:03 +01:00
" uses [TargetRpc] " ,
methodDefinition . Name ,
" but is not a NetworkBehaviour."
} ) ) ;
Weaver . fail = true ;
}
2021-06-18 22:40:38 +01:00
2021-05-03 20:13:03 +01:00
var text = customAttribute . Constructor . DeclaringType . ToString ( ) ;
if ( text = = "UnityEngine.Networking.ServerAttribute" )
{
Log . Error ( string . Concat ( new string [ ]
{
"Script " ,
2021-05-03 20:27:52 +01:00
m_td . FullName ,
2021-05-03 20:13:03 +01:00
" uses the attribute [Server] on the method " ,
methodDefinition . Name ,
" but is not a NetworkBehaviour."
} ) ) ;
Weaver . fail = true ;
}
else if ( text = = "UnityEngine.Networking.ServerCallbackAttribute" )
{
Log . Error ( string . Concat ( new string [ ]
{
"Script " ,
2021-05-03 20:27:52 +01:00
m_td . FullName ,
2021-05-03 20:13:03 +01:00
" uses the attribute [ServerCallback] on the method " ,
methodDefinition . Name ,
" but is not a NetworkBehaviour."
} ) ) ;
Weaver . fail = true ;
}
else if ( text = = "UnityEngine.Networking.ClientAttribute" )
{
Log . Error ( string . Concat ( new string [ ]
{
"Script " ,
2021-05-03 20:27:52 +01:00
m_td . FullName ,
2021-05-03 20:13:03 +01:00
" uses the attribute [Client] on the method " ,
methodDefinition . Name ,
" but is not a NetworkBehaviour."
} ) ) ;
Weaver . fail = true ;
}
else if ( text = = "UnityEngine.Networking.ClientCallbackAttribute" )
{
Log . Error ( string . Concat ( new string [ ]
{
"Script " ,
2021-05-03 20:27:52 +01:00
m_td . FullName ,
2021-05-03 20:13:03 +01:00
" uses the attribute [ClientCallback] on the method " ,
methodDefinition . Name ,
" but is not a NetworkBehaviour."
} ) ) ;
Weaver . fail = true ;
}
}
}
}
private TypeDefinition m_td ;
}
}