Represents a given class.
Exemplo n.º 1
0
        public void AddScriptInstance(CryScriptInstance instance, ScriptType scriptType)
        {
#if !(RELEASE && RELEASE_DISABLE_CHECKS)
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (!Enum.IsDefined(typeof(ScriptType), scriptType))
            {
                throw new ArgumentException(string.Format("scriptType: value {0} was not defined in the enum", scriptType));
            }
#endif

            var script = FindScript(scriptType, x => x.Type == instance.GetType());
            if (script == null)
            {
                if (CryScript.TryCreate(instance.GetType(), out script))
                {
                    Scripts.Add(script);
                }
                else
                {
                    return;
                }
            }

            AddScriptInstance(script, instance);
        }
Exemplo n.º 2
0
        void AddScriptInstance(CryScript script, CryScriptInstance instance, int scriptId = -1)
        {
#if !(RELEASE && RELEASE_DISABLE_CHECKS)
            if (script == default(CryScript))
            {
                throw new ArgumentException("script");
            }
#endif

            var index = Scripts.IndexOf(script);

#if !(RELEASE && RELEASE_DISABLE_CHECKS)
            if (index == -1)
            {
                throw new ArgumentException("Provided CryScript object was not present in the script collection", "script");
            }
#endif

            instance.ScriptId = (scriptId != -1) ? scriptId : LastScriptId++;

            if (script.ScriptInstances == null)
            {
                script.ScriptInstances = new List <CryScriptInstance>();
            }

            script.ScriptInstances.Add(instance);

            Scripts[index] = script;
        }
Exemplo n.º 3
0
        public CryScriptInstance CreateScriptInstance(CryScript script, object[] constructorParams = null, bool throwOnFail = true)
        {
#if !(RELEASE && RELEASE_DISABLE_CHECKS)
            if (script == null)
            {
                throw new ArgumentNullException("script");
            }
#endif

            var scriptInstance = Activator.CreateInstance(script.Type, constructorParams) as CryScriptInstance;
#if !(RELEASE && RELEASE_DISABLE_CHECKS)
            if (scriptInstance == null)
            {
                if (throwOnFail)
                {
                    throw new ArgumentException("Failed to create instance, make sure type derives from CryScriptInstance", "scriptName");
                }
                else
                {
                    return(null);
                }
            }
#endif
            AddScriptInstance(script, scriptInstance);

            scriptInstance.Script = script;

            return(scriptInstance);
        }
Exemplo n.º 4
0
        public static bool TryCreate(Type type, out CryScript script)
        {
            if (type.IsAbstract || type.IsEnum)
            {
                script = default(CryScript);
                return(false);
            }

            var scriptType = ScriptType.Any;

            if (type.Implements <CryScriptInstance>())
            {
                scriptType |= ScriptType.CryScriptInstance;

                if (type.Implements <EntityBase>())
                {
                    scriptType |= ScriptType.Entity;

                    if (type.Implements <ActorBase>())
                    {
                        scriptType |= ScriptType.Actor;
                    }
                    else if (type.Implements <GameRules>())
                    {
                        scriptType |= ScriptType.GameRules;
                    }
                }
                else if (type.Implements <FlowNode>())
                {
                    if (type.ImplementsGeneric(typeof(EntityFlowNode <>)))
                    {
                        scriptType |= ScriptType.EntityFlowNode;
                    }

                    scriptType |= ScriptType.FlowNode;
                }
            }

            if ((scriptType & (scriptType - 1)) == 0) // only had Any set.
            {
                script = default(CryScript);
                return(false);
            }

            script = new CryScript(type, scriptType);
            return(true);
        }
Exemplo n.º 5
0
        void AddScriptInstance(CryScript script, CryScriptInstance instance, int scriptId = -1)
        {
#if !(RELEASE && RELEASE_DISABLE_CHECKS)
            if (script == null)
            {
                throw new ArgumentException("script");
            }
#endif

            instance.ScriptId = (scriptId != -1) ? scriptId : LastScriptId++;

            if (script.ScriptInstances == null)
            {
                script.ScriptInstances = new List <CryScriptInstance>();
            }

            script.ScriptInstances.Add(instance);
        }
Exemplo n.º 6
0
        public static bool TryCreate(Type type, out CryScript script)
        {
            if (type.IsAbstract || type.IsEnum)
            {
                script = null;
                return(false);
            }

            var scriptType = GetScriptType(type);

            if ((scriptType & (scriptType - 1)) == 0) // only had Any set.
            {
                script = null;
                return(false);
            }

            script = new CryScript(type, scriptType);
            return(true);
        }
Exemplo n.º 7
0
        public void ReplaceScriptInstance(CryScriptInstance newInstance, int scriptId, ScriptType scriptType)
        {
            RemoveInstance(scriptId, scriptType);

            var script = FindScript(scriptType, x => x.Type == newInstance.GetType());

            if (script == null)
            {
                if (CryScript.TryCreate(newInstance.GetType(), out script))
                {
                    Scripts.Add(script);
                }
                else
                {
                    return;
                }
            }

            AddScriptInstance(script, newInstance, scriptId);
        }
Exemplo n.º 8
0
        void RegisterInternalTypes()
        {
            CryScript script;

            if (CryScript.TryCreate(typeof(NativeActor), out script))
            {
                Scripts.Add(script);
            }

            if (CryScript.TryCreate(typeof(NativeEntity), out script))
            {
                var entityRegistrationParams = new EntityRegistrationParams();

                entityRegistrationParams.name  = script.ScriptName;
                entityRegistrationParams.flags = EntityClassFlags.Default | EntityClassFlags.Invisible;

#if !UNIT_TESTING
                NativeEntityMethods.RegisterEntityClass(entityRegistrationParams);
#endif

                Scripts.Add(script);
            }
        }
Exemplo n.º 9
0
        public static bool TryCreate(Type type, out CryScript script)
        {
            if (type.IsAbstract || type.IsEnum)
            {
                script = default(CryScript);
                return false;
            }

            var scriptType = ScriptType.Any;
            if (type.Implements<CryScriptInstance>())
            {
                scriptType |= ScriptType.CryScriptInstance;

                if (type.Implements<EntityBase>())
                {
                    scriptType |= ScriptType.Entity;

                    if (type.Implements<ActorBase>())
                        scriptType |= ScriptType.Actor;
                    else if (type.Implements<GameRules>())
                        scriptType |= ScriptType.GameRules;
                }
                else if (type.Implements<FlowNode>())
                {
                    if (type.ImplementsGeneric(typeof(EntityFlowNode<>)))
                        scriptType |= ScriptType.EntityFlowNode;

                    scriptType |= ScriptType.FlowNode;
                }
            }

            if ((scriptType & (scriptType - 1)) == 0) // only had Any set.
            {
                script = default(CryScript);
                return false;
            }

            script = new CryScript(type, scriptType);
            return true;
        }
Exemplo n.º 10
0
        void ProcessWaitingScripts(bool initialLoad)
        {
            bool hasDefaultGameRules = false;

            foreach (var pluginPair in PluginTypes)
            {
                ICryMonoPlugin plugin = pluginPair.Key;

                foreach (Type type in pluginPair.Value)
                {
                    var script = FindScript(ScriptType.Any, x => x.Type == type);
                    if (script == null)
                    {
                        if (!CryScript.TryCreate(type, out script))
                        {
                            continue;
                        }
                    }

                    script.RegistrationParams = plugin.GetRegistrationParams(script.ScriptType, type);

                    if (!script.Registered)
                    {
                        if (script == null || script.RegistrationParams == null)
                        {
                            continue;
                        }

                        // Contain types that can only be registered at startup here.
                        if (initialLoad)
                        {
                            if (script.RegistrationParams is ActorRegistrationParams)
                            {
                                var registrationParams = (ActorRegistrationParams)script.RegistrationParams;

                                NativeActorMethods.RegisterActorClass(script.ScriptName, script.Type.Implements(typeof(NativeActor)), registrationParams.isAI);
                            }
                            else if (script.RegistrationParams is EntityRegistrationParams)
                            {
                                var registrationParams = (EntityRegistrationParams)script.RegistrationParams;

                                if (registrationParams.name == null)
                                {
                                    registrationParams.name = script.ScriptName;
                                }
                                if (registrationParams.category == null)
                                {
                                    registrationParams.category = "Default";
                                }

                                NativeEntityMethods.RegisterEntityClass(registrationParams);

                                script.RegistrationParams = registrationParams;
                            }
                        }

                        if (script.RegistrationParams is GameRulesRegistrationParams)
                        {
                            var registrationParams = (GameRulesRegistrationParams)script.RegistrationParams;

                            if (registrationParams.name == null)
                            {
                                registrationParams.name = script.ScriptName;
                            }

                            NativeGameRulesMethods.RegisterGameMode(registrationParams.name);

                            if (registrationParams.defaultGamemode || !hasDefaultGameRules)
                            {
                                NativeGameRulesMethods.SetDefaultGameMode(registrationParams.name);

                                hasDefaultGameRules = true;
                            }

                            script.RegistrationParams = registrationParams;
                        }
                        else if (script.RegistrationParams is FlowNodeRegistrationParams)
                        {
                            var registrationParams = (FlowNodeRegistrationParams)script.RegistrationParams;

                            if (registrationParams.name == null)
                            {
                                registrationParams.name = script.ScriptName;
                            }
                            if (registrationParams.category == null)
                            {
                                registrationParams.category = script.Type.Namespace;
                            }
                            if (registrationParams.filter == 0)
                            {
                                registrationParams.filter = FlowNodeFilter.Approved;
                            }

                            script.RegistrationParams = registrationParams;

                            script.ScriptName = registrationParams.category + ":" + registrationParams.name;
                        }
                        else if (script.RegistrationParams is EntityFlowNodeRegistrationParams)
                        {
                            var registrationParams = (EntityFlowNodeRegistrationParams)script.RegistrationParams;

                            script.ScriptName = "entity" + ":" + registrationParams.entityName;
                        }

                        script.Registered = true;
                        Scripts.Add(script);
                    }
                }
            }
        }
Exemplo n.º 11
0
        public static bool TryCreate(Type type, out CryScript script)
        {
            if (type.IsAbstract || type.IsEnum)
            {
                script = null;
                return false;
            }

            var scriptType = GetScriptType(type);
            if ((scriptType & (scriptType - 1)) == 0) // only had Any set.
            {
                script = null;
                return false;
            }

            script = new CryScript(type, scriptType);
            return true;
        }