using FishNet.CodeGenerating.Helping;
using FishNet.CodeGenerating.ILCore;
using FishNet.CodeGenerating.Processing;
using FishNet.CodeGenerating.Processing.Rpc;
using MonoFN.Cecil;
using System.Collections.Generic;
using System.Linq;
using Unity.CompilationPipeline.Common.Diagnostics;
#if !UNITY_2020_1_OR_NEWER
using UnityEngine;
#endif
using SR = System.Reflection;
namespace FishNet.CodeGenerating
{
internal class CodegenSession
{
///
/// Current module for this session.
///
internal ModuleDefinition Module;
///
/// Outputs errors when codegen fails.
///
internal List Diagnostics;
///
/// SyncVars that are being accessed from an assembly other than the currently being processed one.
///
internal List DifferentAssemblySyncVars = new List();
///
/// CodegenBase classes for processing a module.
///
private List _bases;
///
/// Quick lookup of base classes.
///
private Dictionary _basesCache = new Dictionary();
///
/// Returns class of type if found within CodegenBase classes.
///
///
///
internal T GetClass() where T : CodegenBase
{
string tName = typeof(T).Name;
return (T)_basesCache[tName];
}
///
/// Resets all helpers while importing any information needed by them.
///
///
///
internal bool Initialize(ModuleDefinition module)
{
Module = module;
Diagnostics = new List();
_bases = new List()
{
new ReaderImports(), new ReaderProcessor()
,new WriterImports(), new WriterProcessor()
, new PhysicsHelper(), new TimeManagerHelper(), new AttributeHelper(), new GeneralHelper()
, new ObjectHelper(), new NetworkBehaviourHelper()
, new CreatedSyncVarGenerator(), new TransportHelper()
, new NetworkConnectionImports(), new PredictedObjectHelper(), new GeneratorHelper()
, new CustomSerializerProcessor()
, new NetworkBehaviourProcessor()
, new QolAttributeProcessor()
, new RpcProcessor()
, new NetworkBehaviourSyncProcessor()
, new PredictionProcessor()
};
//Add all to dictionary first, then import.
foreach (CodegenBase item in _bases)
{
string tName = item.GetType().Name;
_basesCache.Add(tName, item);
}
//Initialize.
foreach (CodegenBase item in _bases)
{
item.Initialize(this);
if (!item.ImportReferences())
return false;
}
return true;
}
#region Logging.
///
/// Logs a warning.
///
///
internal void LogWarning(string msg)
{
#if UNITY_2020_1_OR_NEWER
Diagnostics.AddWarning(msg);
#else
Debug.LogWarning(msg);
#endif
}
///
/// Logs an error.
///
///
internal void LogError(string msg)
{
#if UNITY_2020_1_OR_NEWER
Diagnostics.AddError(msg);
#else
Debug.LogError(msg);
#endif
}
#endregion
#region ImportReference.
public MethodReference ImportReference(SR.MethodBase method)
{
return Module.ImportReference(method);
}
public MethodReference ImportReference(SR.MethodBase method, IGenericParameterProvider context)
{
return Module.ImportReference(method, context);
}
public TypeReference ImportReference(TypeReference type)
{
return Module.ImportReference(type);
}
public TypeReference ImportReference(TypeReference type, IGenericParameterProvider context)
{
return Module.ImportReference(type, context);
}
public FieldReference ImportReference(FieldReference field)
{
return Module.ImportReference(field);
}
public FieldReference ImportReference(FieldReference field, IGenericParameterProvider context)
{
return Module.ImportReference(field, context);
}
public MethodReference ImportReference(MethodReference method)
{
return Module.ImportReference(method);
}
public MethodReference ImportReference(MethodReference method, IGenericParameterProvider context)
{
return Module.ImportReference(method, context);
}
public TypeReference ImportReference(System.Type type)
{
return ImportReference(type, null);
}
public TypeReference ImportReference(System.Type type, IGenericParameterProvider context)
{
return Module.ImportReference(type, context);
}
public FieldReference ImportReference(SR.FieldInfo field)
{
return Module.ImportReference(field);
}
public FieldReference ImportReference(SR.FieldInfo field, IGenericParameterProvider context)
{
return Module.ImportReference(field, context);
}
#endregion
}
}