Exemplo n.º 1
0
        public void Serialize_CryScript_List()
        {
            var serializer = new CrySerializer();

            using (var stream = new MemoryStream())
            {
                var list = new List <CryScript>();

                CryScript script;
                if (CryScript.TryCreate(typeof(NativeActor), out script))
                {
                    script.ScriptInstances = new List <CryScriptInstance>();
                    script.ScriptInstances.Add(new NativeActor(759));
                    script.ScriptInstances.Add(new NativeActor(5));

                    list.Add(script);
                }

                if (CryScript.TryCreate(typeof(NativeEntity), out script))
                {
                    script.ScriptInstances = new List <CryScriptInstance>();
                    script.ScriptInstances.Add(new NativeEntity(987, IntPtr.Zero));
                    script.ScriptInstances.Add(new NativeEntity(8, IntPtr.Zero));
                    script.ScriptInstances.Add(null);

                    list.Add(script);
                }

                serializer.Serialize(stream, list);

                serializer = new CrySerializer();
                list       = serializer.Deserialize(stream) as List <CryScript>;

                Assert.NotNull(list);
                Assert.NotEmpty(list);

                Assert.Equal(2, list.Count);

                var nativeActorScript = list.ElementAt(0);
                Assert.NotNull(nativeActorScript.ScriptInstances);
                Assert.Equal(2, nativeActorScript.ScriptInstances.Count);

                Assert.NotNull(nativeActorScript.ScriptInstances.ElementAt(0));
                Assert.Equal <int>(759, (nativeActorScript.ScriptInstances.ElementAt(0) as EntityBase).Id);

                Assert.NotNull(nativeActorScript.ScriptInstances.ElementAt(1));
                Assert.Equal <int>(5, (nativeActorScript.ScriptInstances.ElementAt(1) as EntityBase).Id);

                var nativeEntityScript = list.ElementAt(1);

                Assert.NotNull(nativeEntityScript.ScriptInstances);
                Assert.Equal(3, nativeEntityScript.ScriptInstances.Count);

                Assert.NotNull(nativeEntityScript.ScriptInstances.ElementAt(0));
                Assert.Equal <int>(987, (nativeEntityScript.ScriptInstances.ElementAt(0) as EntityBase).Id);

                Assert.NotNull(nativeEntityScript.ScriptInstances.ElementAt(1));
                Assert.Equal <int>(8, (nativeEntityScript.ScriptInstances.ElementAt(1) as EntityBase).Id);

                Assert.Null(nativeEntityScript.ScriptInstances.ElementAt(2));
            }
        }
Exemplo n.º 2
0
        IEnumerable <CryScript> ProcessAssembly(Assembly assembly)
        {
            if (assembly == null)
            {
                yield break;
            }

            foreach (var type in assembly.GetTypes())
            {
                IScriptRegistrationParams registrationParams = null;

                CryScript script;
                if (!type.ContainsAttribute <ExcludeFromCompilationAttribute>() && CryScript.TryCreate(type, out script))
                {
                    if (script.ScriptType.ContainsFlag(ScriptType.Actor))
                    {
                        TryGetActorParams(ref registrationParams, script.Type);
                    }
                    else if (script.ScriptType.ContainsFlag(ScriptType.GameRules))
                    {
                        TryGetGamemodeParams(ref registrationParams, script.Type);
                    }
                    else if (script.ScriptType.ContainsFlag(ScriptType.Entity))
                    {
                        TryGetEntityParams(ref registrationParams, script.Type);
                    }
                    else if (script.ScriptType.ContainsFlag(ScriptType.EntityFlowNode))
                    {
                        if (!TryGetEntityFlowNodeParams(ref registrationParams, script.Type))
                        {
                            continue;
                        }
                    }
                    else if (script.ScriptType.ContainsFlag(ScriptType.FlowNode))
                    {
                        if (!TryGetFlowNodeParams(ref registrationParams, script.Type))
                        {
                            continue;
                        }
                    }

                    if (script.ScriptType.ContainsFlag(ScriptType.CryScriptInstance))
                    {
                        foreach (var member in type.GetMethods(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public))
                        {
                            ConsoleCommandAttribute attribute;
                            if (member.TryGetAttribute(out attribute))
                            {
                                ConsoleCommand.Register(attribute.Name ?? member.Name, Delegate.CreateDelegate(typeof(ConsoleCommandDelegate), member as MethodInfo) as ConsoleCommandDelegate, attribute.Comment, attribute.Flags);
                            }
                        }
                    }

                    script.RegistrationParams = registrationParams;

                    yield return(script);
                }

                if (type.ContainsAttribute <TestCollectionAttribute>())
                {
                    var ctor = type.GetConstructor(Type.EmptyTypes);
                    if (ctor != null)
                    {
                        var collection = new TestCollection
                        {
                            Instance = ctor.Invoke(Type.EmptyTypes),
                            Tests    = from method in type.GetMethods()
                                       where method.ContainsAttribute <TestAttribute>() &&
                                       method.GetParameters().Length == 0
                                       select method
                        };

                        TestManager.TestCollections.Add(collection);
                    }
                }
            }
        }