Esempio n. 1
0
    private void FacadeTest <TUnderlyingType, TType, TConverter>(
        TUnderlyingType underlyingValue,
        TType value,
        TypeModelContainer container = null) where TConverter : struct, ITypeFacadeConverter <TUnderlyingType, TType>
    {
        if (container == null)
        {
            container = TypeModelContainer.CreateDefault();
            container.RegisterTypeFacade <TUnderlyingType, TType, TConverter>();
        }

        FlatBufferSerializer serializer = new FlatBufferSerializer(
            new FlatBufferSerializerOptions(FlatBufferDeserializationOption.Greedy),
            container);

        byte[] destination  = new byte[1024];
        byte[] destination2 = new byte[1024];

        var compiled = serializer.Compile <ExtensionTable <TType> >();

        var underlyingItem = new ExtensionTable <TUnderlyingType> {
            Item = underlyingValue
        };
        var facadeItem = new ExtensionTable <TType> {
            Item = value
        };

        serializer.Serialize(facadeItem, destination);
        serializer.Serialize(underlyingItem, destination2);

        Assert.True(destination.AsSpan().SequenceEqual(destination2));

        var parsed = serializer.Parse <ExtensionTable <TType> >(destination);

        Assert.Equal(parsed.Item, value);
        Assert.Equal(serializer.GetMaxSize(facadeItem), serializer.GetMaxSize(underlyingItem));
    }
    public void LazyMemoryCopySerialization()
    {
        TestTable <WriteThroughStruct> t = new()
        {
            Foo    = "foobar",
            Struct = new WriteThroughStruct
            {
                Bar = 12,
            }
        };

        FlatBufferSerializer serializer = new FlatBufferSerializer(FlatBufferDeserializationOption.Lazy);
        var compiled = serializer.Compile <TestTable <WriteThroughStruct> >().WithSettings(new SerializerSettings {
            EnableMemoryCopySerialization = true
        });

        byte[] data = new byte[1024];
        Assert.Equal(70, compiled.GetMaxSize(t));
        int actualBytes = compiled.Write(data, t);

        // First test: Parse the array but don't trim the buffer. This causes the underlying
        // buffer to be much larger than the actual data.
        var parsed = compiled.Parse(data);

        byte[] data2        = new byte[2048];
        int    bytesWritten = compiled.Write(data2, parsed);

        Assert.Equal(35, actualBytes);
        Assert.Equal(1024, bytesWritten); // We use mem copy serialization here, and we gave it 1024 bytes when we parsed. So we get 1024 bytes back out.
        Assert.Equal(1024, compiled.GetMaxSize(parsed));
        Assert.Throws <BufferTooSmallException>(() => compiled.Write(new byte[35], parsed));

        // Repeat, but now using the trimmed array.
        parsed       = compiled.Parse(data.AsMemory().Slice(0, actualBytes));
        bytesWritten = compiled.Write(data2, parsed);
        Assert.Equal(35, actualBytes);
        Assert.Equal(35, bytesWritten);
        Assert.Equal(35, compiled.GetMaxSize(parsed));

        // Default still returns 70.
        Assert.Equal(70, serializer.GetMaxSize(parsed));
    }
Esempio n. 3
0
        public static void SaveGame(String pathToFolder, NamelessGame game)
        {
            var  saveFile = new NamelessRogueSaveFile();
            Type iStorageInterfaceType = typeof(IStorage <>);

            foreach (var componentTypeCollection in EntityInfrastructureManager.Components)
            {
                if (componentTypeCollection.Key.GetCustomAttributes(true).Any(a => a.GetType() == typeof(NamelessRogue.Engine.Serialization.SkipClassGeneration)))
                {
                    continue;
                }

                foreach (var componentDictionary in componentTypeCollection.Value)
                {
                    //the type of the storage that can store current type
                    Type storageType = saveFile.ComponentTypeToStorge[componentTypeCollection.Key];

                    var constructor = storageType.GetConstructor(Type.EmptyTypes);
                    if (constructor == null)
                    {
                        throw new Exception($@"{storageType.Name} must contain a parameterless constructor in order for Save manager to serialize it");
                    }

                    dynamic storageObject = constructor.Invoke(null);

                    storageObject.FillFrom(ObjectExtensions.CastToReflected(componentDictionary.Value, componentDictionary.Value.GetType()));

                    saveFile.StoragesDictionary[storageObject.GetType()].Add(storageObject);
                }
            }
            foreach (var entity in EntityInfrastructureManager.Entities.Values)
            {
                var storage = new EntityStorage();
                storage.FillFrom(entity);
                saveFile.EntityStorageTable.Add(storage);
            }

            {
                int    maxBytesNeeded = FlatBufferSerializer.Default.GetMaxSize(saveFile);
                byte[] buffer         = new byte[maxBytesNeeded];
                int    bytesWritten   = FlatBufferSerializer.Default.Serialize(saveFile, buffer);

                var stream = File.OpenWrite("objectdata.nrs");
                stream.Write(buffer, 0, bytesWritten);
                stream.Close();
            }

            {
                TimelineStorage timelinesStorage = new TimelineStorage();
                var             timeline         = game.TimelineEntity.GetComponentOfType <TimeLine>();
                timelinesStorage.FillFrom(timeline);
                {
                    FlatBufferSerializer serializer = new FlatBufferSerializer(FlatBufferDeserializationOption.Greedy);
                    int maxBytesNeeded = serializer.GetMaxSize(timelinesStorage);

                    int byteNeeded = serializer.GetMaxSize(timelinesStorage.CurrentTimelineLayer.WorldTiles[50 * 10]);

                    byte[] buffer = new byte[maxBytesNeeded];

                    int bytesWritten = serializer.Serialize(timelinesStorage, buffer);

                    var stream = File.OpenWrite("chunksmemory.nrs");
                    stream.Write(buffer, 0, bytesWritten);
                    stream.Close();
                }
            }
        }