public void Run()
        {
            var context = new PofContext();

            context.MergeContext(new CustomPofContext(1000, dummyRemoteService));

            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, new LogicController(10));
                    serializer.Serialize(writer, new LogicController(20));
                }
                ms.Position = 0;
                VerifyNoMoreInteractions();
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    var instance1 = serializer.Deserialize <LogicController>(reader);
                    var instance2 = serializer.Deserialize <LogicController>(reader);
                    VerifyNoMoreInteractions();

                    instance1.Execute();
                    Verify(dummyRemoteService, Once()).DoSomething(10);
                    VerifyNoMoreInteractions();

                    instance2.Execute();
                    Verify(dummyRemoteService, Once()).DoSomething(20);
                    VerifyNoMoreInteractions();
                }
            }
        }
Exemple #2
0
        public void Run()
        {
            var nowUtc   = DateTime.UtcNow;
            var nowLocal = DateTime.Now;

            Console.WriteLine(nowUtc);
            Console.WriteLine(nowLocal);

            var context    = new PofContext().With(x => x.RegisterPortableObjectType(0, typeof(DummyType)));
            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, new DummyType(nowUtc, new [] { nowUtc, nowUtc, nowUtc }));
                    serializer.Serialize(writer, new DummyType(nowLocal, new[] { nowUtc, nowUtc, nowUtc }));
                }
                ms.Position = 0;
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    var nowUtcWrapper   = serializer.Deserialize <DummyType>(reader);
                    var nowLocalWrapper = serializer.Deserialize <DummyType>(reader);
                    Console.WriteLine(nowUtcWrapper.Time);
                    Console.WriteLine(nowLocalWrapper.Time);

                    AssertEquals(nowUtc.ToUniversalTime(), nowUtcWrapper.Time.ToUniversalTime());
                    AssertEquals(nowUtc.ToUniversalTime(), nowLocalWrapper.Time.ToUniversalTime());

                    AssertEquals(nowUtc.ToUniversalTime(), nowUtcWrapper.AlsoTime.ToUniversalTime());
                    AssertEquals(nowUtc.ToUniversalTime(), nowLocalWrapper.AlsoTime.ToUniversalTime());

                    AssertTrue(nowUtcWrapper.Times.All(time => time.ToUniversalTime().Equals(nowUtc.ToUniversalTime())));
                    AssertTrue(nowLocalWrapper.Times.All(time => time.ToUniversalTime().Equals(nowUtc.ToUniversalTime())));
                }
            }
        }
        public void Run()
        {
            var context = new PofContext();

            context.RegisterPortableObjectType(1, typeof(TestClass));

            var serializer = new PofSerializer(context);
            var testObj1   = new TestClass(EnumerateValues());
            var testObj2   = new TestClass(new object[] { null, null });
            var testObj3   = new TestClass(new List <object> {
                2, "string", null
            });

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, testObj1);
                    serializer.Serialize(writer, testObj2);
                    serializer.Serialize(writer, testObj3);
                }
                ms.Position = 0;
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    var readObj1 = serializer.Deserialize <TestClass>(reader);
                    var a        = ((IEnumerable <object>)testObj1.Value).ToArray();
                    var b        = ((IEnumerable <object>)readObj1.Value).ToArray();
                    AssertEquals(5, a.Length);
                    AssertEquals(5, b.Length);
                    for (var i = 0; i < b.Length; i++)
                    {
                        var ai = a[i];
                        var bi = b[i];
                        if (ai is IEnumerable && !(ai is string))
                        {
                            AssertTrue(((IEnumerable <object>)ai).SequenceEqual((IEnumerable <object>)bi));
                        }
                        else
                        {
                            AssertEquals(ai, bi);
                        }
                    }

                    var readObj2 = serializer.Deserialize <TestClass>(reader);
                    AssertTrue(((IEnumerable <object>)testObj2.Value).SequenceEqual((IEnumerable <object>)readObj2.Value));

                    var readObj3 = serializer.Deserialize <TestClass>(reader);
                    AssertTrue(((IEnumerable <object>)testObj3.Value).SequenceEqual((IEnumerable <object>)readObj3.Value));
                }
            }
        }
Exemple #4
0
        public void Run()
        {
            const string rootName           = "root";
            const string rootLeftName       = "root_left";
            const string rootRightName      = "root_right";
            const string rootRightRightName = "root_right_right";

            var context = new PofContext();

            context.RegisterPortableObjectType(0, typeof(Node));
            var serializer     = new PofSerializer(context);
            var rootRightRight = new Node(rootRightName, null, null);
            var rootRight      = new Node(rootRightName, null, rootRightRight);
            var rootLeft       = new Node(rootLeftName, null, null);
            var root           = new Node("root", rootLeft, rootRight);

            using (var ms = new MemoryStream())
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true))
                    using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                        serializer.Serialize(writer, root);
                        ms.Position = 0;
                        var newRoot = serializer.Deserialize <Node>(reader);
                        AssertEquals(root, newRoot);
                    }
        }
        public void PolymorphismTest()
        {
            var testObj1 = new TestClass(new object[] { 0xCDCDCDCD, "herp", new byte[] { 0xEE, 0xDD, 0xCC, 0xDD, 0xEE, 0xCC, 0xFF } });

            var context = new PofContext();

            context.RegisterPortableObjectType(0xFF, typeof(TestClass));

            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, testObj1);
                }

                Console.WriteLine(ms.ToArray().ToHex());

                ms.Position = 0;
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    var readObj1 = serializer.Deserialize <TestClass>(reader);
                    AssertTrue(testObj1.Value.Take(2).SequenceEqual(readObj1.Value.Take(2)));
                    AssertTrue(((IEnumerable <byte>)testObj1.Value[2]).SequenceEqual((IEnumerable <byte>)readObj1.Value[2]));
                }
            }
        }
Exemple #6
0
        public void RunTest()
        {
            const string name1 = "Henry has a first name!";
            const string name2 = "Larry doesn't have a last name!";
            var          key1  = new PersonKey(name1);
            var          key2  = new PersonKey(name2);

            var personEntry1 = new PersonEntry(key1, 10);
            var personEntry2 = new PersonEntry(key2, 5);

            var thresholdsByKey = new Dictionary <PersonKey, int>();

            thresholdsByKey.Add(key1, 30);
            thresholdsByKey.Add(key2, 2);

            var levelRemovalProcessor   = new RemovalByLevelThresholdProcessor(thresholdsByKey);
            var friendClearingProcessor = new FriendClearingProcessor();

            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, levelRemovalProcessor);
                    serializer.Serialize(writer, friendClearingProcessor);
                }
                ms.Position = 0;
                Console.WriteLine(ms.ToArray().ToHex());
                using (var reader = new BinaryReader(ms)) {
                    levelRemovalProcessor   = serializer.Deserialize <RemovalByLevelThresholdProcessor>(reader);
                    friendClearingProcessor = serializer.Deserialize <FriendClearingProcessor>(reader);
                }
            }

            var entry1 = new Entry <PersonKey, PersonEntry>(key1, personEntry1);
            var entry2 = new Entry <PersonKey, PersonEntry>(key2, personEntry2);

            friendClearingProcessor.Process(entry1);
            friendClearingProcessor.Process(entry2);

            levelRemovalProcessor.Process(entry1);
            levelRemovalProcessor.Process(entry2);

            assertTrue(entry1.IsPresent());
            assertFalse(entry2.IsPresent());
        }
        public void ImportingTaskStart()
        {
            var temporaryDirectory = TemporaryFileService.AllocateTemporaryDirectory(TimeSpan.FromHours(1));

            DirectoryHelpers.DirectoryCopy(Modification.RepositoryPath, temporaryDirectory, true);
            Directory.Move(temporaryDirectory, finalRepositoryPath);
            Modification = ModificationLoader.FromPath(finalRepositoryPath);
            PhaseFactory.SetModification(Modification);
            ViewModel.SetModification(Modification);

            var enabledComponent = Modification.GetComponent <EnabledComponent>();

            enabledComponent.IsEnabled = true;

            var thumbnailDirectory = Path.Combine(finalRepositoryPath, "thumbnails");

            FileSystemProxy.PrepareDirectory(thumbnailDirectory);
            var thumbnailGenerationTask = Task.Factory.StartNew(() => {
                using (var ms = new MemoryStream())
                    using (var writer = new BinaryWriter(ms)) {
                        PofSerializer.Serialize(writer, new ThumbnailGenerationParameters {
                            DestinationDirectory = thumbnailDirectory,
                            SourceDirectory      = importedDirectoryPath,
                            ThumbnailsToGenerate = 3
                        });
                        ExeggutorService.SpawnHatchling(
                            "thumbnail-generator",
                            new SpawnConfiguration {
                            InstanceName = "thumbnail-generator-" + DateTime.UtcNow.GetUnixTime(),
                            Arguments    = ms.GetBuffer()
                        });
                        var thumbnailComponent = Modification.GetComponent <ThumbnailComponent>();
                        thumbnailComponent.SelectThumbnailIfUnselected();
                    }
            }, TaskCreationOptions.LongRunning);

            var contentDirectory = Path.Combine(finalRepositoryPath, "content");

            FileSystemProxy.PrepareDirectory(contentDirectory);
            for (var i = 0; i < relativeImportedFilePaths.Length; i++)
            {
                var sourceFile      = Path.Combine(importedDirectoryPath, relativeImportedFilePaths[i]);
                var destinationFile = Path.Combine(contentDirectory, relativeImportedFilePaths[i]);
                FileSystemProxy.PrepareParentDirectory(destinationFile);
                FileSystemProxy.CopyFile(sourceFile, destinationFile);
                UpdateProgress(0.333 * ((double)i / relativeImportedFilePaths.Length));
            }

            LeagueBuildUtilities.ResolveModification(Modification, CancellationToken.None);
            UpdateProgress(0.666);

            LeagueBuildUtilities.CompileModification(Modification, CancellationToken.None);
            UpdateProgress(1);
            thumbnailGenerationTask.Wait();
            PhaseManager.Transition(PhaseFactory.Idle());
        }
Exemple #8
0
        public static void CheckConfiguration <TPortableObject>(IPofContext context, TPortableObject testObj)
            where TPortableObject : IPortableObject
        {
            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                serializer.Serialize(ms, testObj);
                ms.Position = 0;
                var deserialized = serializer.Deserialize <TPortableObject>(ms);
                NMockitoStatic.AssertEquals(testObj, deserialized);
            }
        }
        public void Run()
        {
            var context = new PofContext();

            context.RegisterPortableObjectType(1, typeof(Box));

            var serializer = new PofSerializer(context);
            var obj        = new object[] { null, 1, 2, 3 };
            var box        = new Box(obj);

            using (var ms = new MemoryStream())
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true))
                    using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                        serializer.Serialize(writer, box);
                        serializer.Serialize(writer, null);
                        ms.Position = 0;
                        var newBox  = serializer.Deserialize <Box>(reader);
                        var nullBox = serializer.Deserialize <Box>(reader);
                        AssertEquals(box, newBox);
                        AssertNull(nullBox);
                    }
        }
 private void Send <TPayload>(Opcode opcode, TPayload payload)
     where TPayload : IPortableObject
 {
     using (var ms = new MemoryStream()) {
         ms.Position = 4;
         using (var writer = new BinaryWriter(ms)) {
             writer.Write((uint)opcode);
             serializer.Serialize(writer, payload);
         }
         ms.Position = 0;
         using (var writer = new BinaryWriter(ms)) {
             writer.Write((uint)ms.Length);
         }
         session.FrameWriter.WriteRaw(ms);
     }
 }
Exemple #11
0
        public void NestedDictionariesTest()
        {
            var testObj = new Wrapper <SCG.Dictionary <int, SCG.Dictionary <int, bool[]>[]>[]>(Util.Generate(10, i => MakeSubDictionary(i)));

            var context    = new PofContext().With(x => x.RegisterPortableObjectType(0, typeof(Wrapper <>)));
            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, (object)testObj);
                }
                ms.Position = 0;
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    serializer.Deserialize(reader);
                }
            }
        }
Exemple #12
0
        public void ComplexTest()
        {
            var context = new PofContext();

            context.RegisterPortableObjectType(0, typeof(DummyClass <>));
            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                var writtenTypes = new Type[] { typeof(int), typeof(DummyClass <>), typeof(DummyClass <int>) };
                Debug.WriteLine("Written Types: " + writtenTypes.Join(", "));
                serializer.Serialize(ms, writtenTypes);
                ms.Position = 0;
                var readTypes = serializer.Deserialize <Type[]>(ms);
//            AssertEquals(readType, writtenType);
                Debug.WriteLine("   Read Types: " + readTypes.Join(", "));
            }
        }
Exemple #13
0
        public void SimpleTest()
        {
            var context = new PofContext();

            context.RegisterPortableObjectType(0, typeof(DummyClass <>));
            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                var writtenType = typeof(DummyClass <int>);
                Debug.WriteLine("Written Type: " + writtenType);
                serializer.Serialize(ms, writtenType);
                ms.Position = 0;
                var readType = serializer.Deserialize <Type>(ms);
                AssertEquals(readType, writtenType);
                Debug.WriteLine("   Read Type: " + readType);
            }
        }
Exemple #14
0
        public void NestedArraysTest()
        {
            var dict   = new SCG.Dictionary <int, float[][]>();
            var random = new Random(0);

            for (var i = 0; i < 10; i++)
            {
                dict.Add(i, Util.Generate(i, x => Util.Generate(x, y => (float)random.NextDouble())));
            }

            var context = new PofContext();

            context.RegisterPortableObjectType(1, typeof(Wrapper));
            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, new Wrapper(dict));
                }
                ms.Position = 0;
                Console.WriteLine(ms.ToArray().ToHex());
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    var wrapperCopy = (Wrapper)serializer.Deserialize(reader);
                    var dictClone   = (SCG.Dictionary <int, float[][]>)wrapperCopy.Value;
                    AssertTrue(new ICL.HashSet <int>(dict.Keys).SetEquals(dictClone.Keys));
                    random = new Random();
                    for (var i = 0; i < 10; i++)
                    {
                        var arr = dict[i];
                        for (var j = 0; j < arr.Length; j++)
                        {
                            AssertTrue(new ICL.HashSet <float>(arr[j]).SetEquals(dictClone[i][j]));
                        }
                    }
                }
            }
        }
Exemple #15
0
        public void RunTest()
        {
            const string name1 = "Henry has a first name!";
            const string name2 = "Larry doesn't have a last name!";
            var          key1  = new PersonKey(Guid.NewGuid(), name1);
            var          key2  = new PersonKey(Guid.NewGuid(), name2);

            var personEntry1 = new PersonEntry(key1, 10);
            var personEntry2 = new PersonEntry(key2, 5);

            personEntry1.Friends.Add(new PersonFriend(0xAEF8329dF, "Mark"));
            personEntry2.Friends.Add(new PersonFriend(0xF8372D33F, "Henry"));
            personEntry2.Friends.Add(new PersonFriend(0x47928C3ED, "Jane"));

            var thresholdsByKey = new Dictionary <PersonKey, int>();

            thresholdsByKey.Add(key1, 30);
            thresholdsByKey.Add(key2, 2);

            var levelRemovalProcessor   = new RemovalByLevelThresholdProcessor(thresholdsByKey);
            var friendClearingProcessor = new FriendClearingProcessor();

            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, levelRemovalProcessor);
                    serializer.Serialize(writer, friendClearingProcessor);
                }
                ms.Position = 0;
                Console.WriteLine(ms.ToArray().ToHex());
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    levelRemovalProcessor   = serializer.Deserialize <RemovalByLevelThresholdProcessor>(reader);
                    friendClearingProcessor = serializer.Deserialize <FriendClearingProcessor>(reader);
                }
            }

            var entry1         = new Entry <PersonKey, PersonEntry>(key1, personEntry1);
            var entry2         = new Entry <PersonKey, PersonEntry>(key2, personEntry2);
            var originalEntry1 = entry1;
            var originalEntry2 = entry2;

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, entry1);
                    serializer.Serialize(writer, entry2);
                }
                ms.Position = 0;
                Console.WriteLine(ms.ToArray().ToHex());
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    entry1 = serializer.Deserialize <Entry <PersonKey, PersonEntry> >(reader);

                    int frameLength = reader.ReadInt32();
                    var frameBody   = reader.ReadBytes(frameLength);
                    using (var innerMs = new MemoryStream(frameBody))
                        using (var innerReader = new BinaryReader(innerMs, Encoding.UTF8, true)) {
                            entry2 = (Entry <PersonKey, PersonEntry>)serializer.Deserialize(innerReader, SerializationFlags.Lengthless, null);
                        }
                }
            }

            friendClearingProcessor.Process(entry1);
            friendClearingProcessor.Process(entry2);

            levelRemovalProcessor.Process(entry1);
            levelRemovalProcessor.Process(entry2);

            AssertTrue(entry1.IsPresent());
            AssertFalse(entry2.IsPresent());

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, entry1);
                    serializer.Serialize(writer, entry2);
                }
                ms.Position = 0;
                Console.WriteLine(ms.ToArray().ToHex());
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    entry1 = serializer.Deserialize <Entry <PersonKey, PersonEntry> >(reader);
                    entry2 = serializer.Deserialize <Entry <PersonKey, PersonEntry> >(reader);
                }
            }

            AssertTrue(entry1.IsPresent());
            AssertFalse(entry2.IsPresent());
        }