Beispiel #1
0
        public void LogicProviderTest()
        {
            CSLogicProvider.AsyncFactory = scriptName => CSLogicProvider.CompileAsync(scriptName, code);
            CSLogicProvider provider = CSLogicProvider.CompileAsync("Test", code).Get();
            var             exported = new SerialCSLogicProvider(provider);
            var             imported = exported.DeserializeAsync().Result;

            Assert.AreEqual(provider, imported);

            DynamicCSLogic logic = new DynamicCSLogic(provider, null, null);

            var serialLogic = Helper.SerializeToArray(logic);

            var logic2 = (DynamicCSLogic)Helper.Deserialize(serialLogic);

            logic2.FinishLoading(new EntityID(), TimeSpan.FromSeconds(1));



            var serialProvider = Helper.SerializeToArray(provider);
            var provider2      = (CSLogicProvider)Helper.Deserialize(serialProvider);

            DB.LogicLoader = scriptName => Task.Run(() => provider2);
            var logic3 = (DynamicCSLogic)Helper.Deserialize(serialLogic);

            logic3.FinishLoading(new EntityID(), TimeSpan.FromSeconds(1));
        }
Beispiel #2
0
        public void AllowedLogicTest()
        {
            try
            {
                CSLogicProvider factory0 = CSLogicProvider.CompileAsync("DisallowedA", disallowedStructNotSerializable).Get();
                Assert.Fail("The specified code has a non-serializable struct field. Should have triggered an exception");
            }
            catch (CSLogicProvider.SerializationException)
            { }
            try
            {
                CSLogicProvider factory1 = CSLogicProvider.CompileAsync("DisallowedB", disallowedLogicNotSerializable).Get();
                Assert.Fail("The specified code has a non-serializable logic. Should have triggered an exception");
            }
            catch (CSLogicProvider.SerializationException)
            { }

            try
            {
                CSLogicProvider factory1 = CSLogicProvider.CompileAsync("DisallowedNested", disallowedClassNotSerializable).Get();
                Assert.Fail("The specified code has a non-serializable class field. Should have triggered an exception");
            }
            catch (CSLogicProvider.SerializationException)
            { }

            CSLogicProvider factory2 = CSLogicProvider.CompileAsync("Allowed", allowed).Get();
        }
Beispiel #3
0
        public void ScriptedRemoteLogicInstantiationTest()
        {
            CSLogicProvider providerA = CSLogicProvider.CompileAsync("RemoteA", remoteTestA).Result;
            CSLogicProvider providerB = CSLogicProvider.CompileAsync("RemoteB", remoteTestB).Result;

            DB.LogicLoader = CSLogicProvider.AsyncFactory = scriptName => Task.Run(() => scriptName == providerA.AssemblyName ? providerA : providerB);


            SimulationRun run = new SimulationRun(
                new BaseDB.ConfigContainer()
            {
                extent = Int3.One, r = 1f / 8, m = 1f / 16
            },
                new ShardID(Int3.Zero, 0),
                new Entity[]
            {
                new Entity(
                    new EntityID(Guid.NewGuid(), Simulation.MySpace.Center),
                    Vec3.Zero,
                    new DynamicCSLogic(providerA, "InstantiatorLogic", null),
                    null),
            }
                );


            const int NumIterations = 3;

            for (int i = 0; i < NumIterations; i++)
            {
                var rs             = run.AdvanceTLG(true, true);
                int instantiations = rs.IntermediateSDS.localChangeSet.NamedSets.Where(pair => pair.Key == "instantiations").First().Value.Size;
                Assert.AreEqual(instantiations, 1);
                Assert.AreEqual(rs.IntermediateSDS.entities.Count, Math.Min(i + 1, 2)); //can never be more than 2
                Assert.AreEqual(rs.SDS.FinalEntities.Length, 2);                        //previous clone self-destructed, so we are back to exactly 2
            }
            Assert.AreEqual(1, run.stack.Size);
            foreach (var e in run.stack.Last().SDS.FinalEntities)
            {
                var st = Helper.Deserialize(e.SerialLogicState);
                Assert.IsTrue(st is DynamicCSLogic, st.GetType().ToString());
            }
        }
Beispiel #4
0
        public void ReferencedProviderTest2()
        {
            CSLogicProvider baseP = CSLogicProvider.CompileAsync("base", derivationTest0).Get();

            CSLogicProvider.AsyncFactory = scriptName => Task.Run(() => baseP);
            CSLogicProvider usingSharedP = CSLogicProvider.CompileAsync("using", derivationTest1).Get();
            DynamicCSLogic  logic        = new DynamicCSLogic(usingSharedP, "Test", null);

            logic.FinishLoading(new EntityID(), TimeSpan.Zero);

            var serialProvider = Helper.SerializeToArray(usingSharedP);
            var provider2      = (CSLogicProvider)Helper.Deserialize(serialProvider);

            var serialLogic = Helper.SerializeToArray(logic);

            CSLogicProvider.AsyncFactory = scriptName => Task.Run(() => scriptName == "base" ? baseP : provider2);
            var logic3 = (DynamicCSLogic)Helper.Deserialize(serialLogic);

            logic3.FinishLoading(new EntityID(), TimeSpan.FromHours(1));
        }
Beispiel #5
0
            public void Compile(string name, string code)
            {
                CSLogicProvider provider = CSLogicProvider.CompileAsync(name, code).Result;

                providers.Add(provider);
            }