/// <summary> /// Loads an C# assembly and return encapulsated script Type. /// </summary> public static CryScript[] LoadAssembly(Assembly assembly) { var assemblyTypes = assembly.GetTypes().Where(type => type.Implements(typeof(CryScriptInstance))).ToArray(); var scripts = new CryScript[assemblyTypes.Length]; //Extract the types and load everything that implements IEntity, IGameRules and IFlowNode System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); //for(var i = 0; i < scripts.Length; i++) Parallel.For(0, scripts.Length, i => { var type = assemblyTypes[i]; if(!type.ContainsAttribute<ExcludeFromCompilationAttribute>()) { var scriptType = MonoScriptType.Null; if(type.Implements(typeof(BaseGameRules))) scriptType = MonoScriptType.GameRules; else if(type.Implements(typeof(BasePlayer))) scriptType = MonoScriptType.Actor; else if(type.Implements(typeof(Entity))) scriptType = MonoScriptType.Entity; else if(type.Implements(typeof(StaticEntity))) scriptType = MonoScriptType.StaticEntity; else if(type.Implements(typeof(FlowNode))) scriptType = MonoScriptType.FlowNode; scripts[i] = new CryScript(type, scriptType); // This is done after CryScript construction to avoid calling Type.name several times if(scriptType == MonoScriptType.GameRules) { GameRulesSystem._RegisterGameMode(scripts[i].className); if(type.ContainsAttribute<DefaultGamemodeAttribute>()) GameRulesSystem._SetDefaultGameMode(scripts[i].className); } else if(scriptType == MonoScriptType.Actor) ActorSystem._RegisterActorClass(scripts[i].className, false); else if(scriptType == MonoScriptType.Entity || scriptType == MonoScriptType.StaticEntity) LoadEntity(type, scripts[i], scriptType == MonoScriptType.StaticEntity); else if(scriptType == MonoScriptType.FlowNode) LoadFlowNode(type, scripts[i].className); } }); return scripts; }
private static void LoadEntity(Type type, CryScript script, bool staticEntity) { EntityConfig config = default(EntityConfig); StaticEntity entity = null; if(staticEntity) entity = Activator.CreateInstance(type) as StaticEntity; else entity = Activator.CreateInstance(type) as Entity; config = entity.GetEntityConfig(); entity = null; if(config.registerParams.Name.Length <= 0) config.registerParams.Name = script.className; if(config.registerParams.Category.Length <= 0) config.registerParams.Category = ""; // TODO: Use the folder structure in Scripts/Entities. (For example if the entity is in Scripts/Entities/Multiplayer, the category should become "Multiplayer") EntitySystem.RegisterEntityClass(config); LoadFlowNode(type, config.registerParams.Name, true); }
private void AddScripts(CryScript[] scripts) { if(scripts == null || scripts.Length < 1) return; foreach(var tempScript in scripts) { if(tempScript.Type != null) m_compiledScripts.Add(tempScript); } }