public byte [] CompressCustomAttribute(CustomAttrib ca, MethodReference ctor)
        {
            MemoryBinaryWriter writer = new MemoryBinaryWriter();

            CompressCustomAttribute(ca, ctor, writer);
            return(writer.ToArray());
        }
        void Write(CustomAttrib ca, MethodReference ctor, MemoryBinaryWriter writer)
        {
            if (ca == null)
            {
                return;
            }

            if (ca.Prolog != CustomAttrib.StdProlog)
            {
                return;
            }

            writer.Write(ca.Prolog);

            for (int i = 0; i < ctor.Parameters.Count; i++)
            {
                Write(ca.FixedArgs [i], writer);
            }

            writer.Write(ca.NumNamed);

            for (int i = 0; i < ca.NumNamed; i++)
            {
                Write(ca.NamedArgs [i], writer);
            }
        }
 public MetadataRowWriter(MetadataTableWriter mtwv)
 {
     m_binaryWriter = mtwv.GetWriter();
     m_root         = mtwv.GetMetadataRoot();
     m_ciCache      = new int [13];
     m_counter      = new Utilities.TableRowCounter(GetNumberOfRows);
 }
Exemple #4
0
 public MetadataTableWriter(MetadataWriter mrv, MemoryBinaryWriter writer)
 {
     m_root         = mrv.GetMetadataRoot();
     m_heap         = m_root.Streams.TablesHeap;
     m_binaryWriter = writer;
     m_mrrw         = new MetadataRowWriter(this);
 }
Exemple #5
0
        public static unsafe bool WriteToDisk(FixedString128 gameSaveFilePath, MemoryBinaryWriter writer)
        {
            Baselib_ErrorState errorState = new Baselib_ErrorState();

            Baselib_FileIO_SyncFile gameSaveFileHandle = Baselib_FileIO_SyncOpen(gameSaveFilePath.GetUnsafePtr(), Baselib_FileIO_OpenFlags.CreateAlways | Baselib_FileIO_OpenFlags.Write, &errorState);

            if (errorState.code != Baselib_ErrorCode.Success)
            {
                return(false);
            }

            Baselib_FileIO_SyncWrite(gameSaveFileHandle, 0, (IntPtr)writer.Data, (uint)writer.Length, &errorState);
            if (errorState.code != Baselib_ErrorCode.Success)
            {
                return(false);
            }

            Baselib_FileIO_SyncFlush(gameSaveFileHandle, &errorState);
            if (errorState.code != Baselib_ErrorCode.Success)
            {
                return(false);
            }

            // If we get this far, we'll consider the write a success because it should have been completed.
            Baselib_FileIO_SyncClose(gameSaveFileHandle, &errorState);
            return(true);
        }
        void Write(CustomAttrib.NamedArg na, MemoryBinaryWriter writer)
        {
            if (na.Field)
            {
                writer.Write((byte)0x53);
            }
            else if (na.Property)
            {
                writer.Write((byte)0x54);
            }
            else
            {
                throw new MetadataFormatException("Unknown kind of namedarg");
            }

            if (na.FixedArg.SzArray)
            {
                writer.Write((byte)ElementType.SzArray);
            }

            writer.Write((byte)na.FieldOrPropType);

            if (na.FieldOrPropType == ElementType.Enum)
            {
                Write(na.FixedArg.Elems [0].ElemType.FullName);
            }

            Write(na.FieldOrPropName);

            Write(na.FixedArg, writer);
        }
        public static void SaveAssembly(AssemblyDefinition asm, out byte[] assembly)
        {
            MemoryBinaryWriter bw = new MemoryBinaryWriter();

            SaveAssembly(asm, bw.BaseStream);
            assembly = bw.ToArray();
        }
Exemple #8
0
        public MetadataWriter(AssemblyDefinition asm, MetadataRoot root,
                              AssemblyKind kind, TargetRuntime rt, BinaryWriter writer)
        {
            m_assembly     = asm;
            m_root         = root;
            m_runtime      = rt;
            m_imgWriter    = new ImageWriter(this, kind, writer);
            m_binaryWriter = m_imgWriter.GetTextWriter();

            m_stringCache  = new Hashtable();
            m_stringWriter = new MemoryBinaryWriter(Encoding.UTF8);
            m_stringWriter.Write((byte)0);

            m_guidCache  = new Hashtable();
            m_guidWriter = new MemoryBinaryWriter();

            m_usCache  = new Hashtable();
            m_usWriter = new MemoryBinaryWriter(Encoding.Unicode);
            m_usWriter.Write((byte)0);

            m_blobCache  = new Hashtable(ByteArrayEqualityComparer.Instance, ByteArrayEqualityComparer.Instance);
            m_blobWriter = new MemoryBinaryWriter();
            m_blobWriter.Write((byte)0);

            m_tWriter     = new MemoryBinaryWriter();
            m_tableWriter = new MetadataTableWriter(this, m_tWriter);

            m_cilWriter = new MemoryBinaryWriter();

            m_fieldDataWriter = new MemoryBinaryWriter();
            m_resWriter       = new MemoryBinaryWriter();
        }
Exemple #9
0
 public static SR.Assembly CreateReflectionAssembly(AssemblyDefinition asm, AppDomain domain)
 {
     using (MemoryBinaryWriter writer = new MemoryBinaryWriter()) {
         WriteAssembly(asm, writer);
         return(domain.Load(writer.ToArray()));
     }
 }
Exemple #10
0
        public CodeWriter(ReflectionWriter reflectWriter, MemoryBinaryWriter writer)
        {
            m_reflectWriter = reflectWriter;
            m_binaryWriter  = writer;
            m_codeWriter    = new MemoryBinaryWriter();

            m_localSigCache = new Hashtable();
        }
Exemple #11
0
        public static byte[] SerializeWorld(EntityManager entityManager, out object[] referencedObjects)
        {
            using (var binaryWriter = new MemoryBinaryWriter())
            {
                SerializeUtility.SerializeWorld(entityManager, binaryWriter, out referencedObjects);

                return(binaryWriter.GetDataArray());
            }
        }
        static void PatchHeap(MemoryBinaryWriter heap_writer, MetadataHeap heap)
        {
            if (heap == null)
            {
                return;
            }

            heap_writer.BaseStream.Position = 0;
            heap_writer.Write(heap.Data);
        }
Exemple #13
0
        public static byte[] GetDataArray(this MemoryBinaryWriter binaryWriter)
        {
            byte[] arr = new byte[binaryWriter.Length];

            unsafe
            {
                Marshal.Copy((IntPtr)binaryWriter.Data, arr, 0, binaryWriter.Length);
            }

            return(arr);
        }
        void Write(CustomAttrib.FixedArg fa, MemoryBinaryWriter writer)
        {
            if (fa.SzArray)
            {
                writer.Write(fa.NumElem);
            }

            foreach (CustomAttrib.Elem elem in fa.Elems)
            {
                Write(elem, writer);
            }
        }
Exemple #15
0
 void SetHeapSize(MetadataHeap heap, MemoryBinaryWriter data, byte flag)
 {
     if (data.BaseStream.Length > 65536)
     {
         m_root.Streams.TablesHeap.HeapSizes |= flag;
         heap.IndexSize = 4;
     }
     else
     {
         heap.IndexSize = 2;
     }
 }
 public void WorldHash_Performance_BinaryWorldMemorySerialization()
 {
     Measure.Method(() =>
     {
         using (var writer = new MemoryBinaryWriter())
         {
             SerializeUtility.SerializeWorld(World.EntityManager, writer);
             HashUtility.GetDenseHash(InternallyExposedPerformanceTestExtensions.GetContentAsNativeArray(writer), SeedForTesting);
         }
     })
     .Definition("WorldHashPerformance_WorldSerializationBinaryMemory_72MB")
     .MeasurementCount(3)
     .Run();
 }
        public void Collect(DynamicComponentTypeHandle typeHandle, ArchetypeChunk chunk, Dictionary <uint, byte[]> outResult)
        {
            NativeArray <PhysicsColliderBlob> components  = chunk.GetDynamicComponentDataArrayReinterpret <PhysicsColliderBlob>(typeHandle, PhysicsColliderBlob.TYPE_SIZE);
            NativeArray <SimAssetId>          simAssetIds = chunk.Has(_simAssetIdsHandle) ? chunk.GetNativeArray(_simAssetIdsHandle) : default;

            for (int i = 0; i < components.Length; i++)
            {
                if (components[i].Collider.IsCreated)
                {
                    byte[] data;
                    IntPtr ptr;

                    unsafe
                    {
                        ptr = new IntPtr(components[i].Collider.GetUnsafePtr());
                    }

                    if (!_cachedColliders.TryGetValue(ptr, out data))
                    {
                        using (var writer = new MemoryBinaryWriter())
                        {
                            writer.Write(components[i].Collider);
                            data = writer.GetDataArray();
                        }

                        _cachedColliders[ptr] = data;
                    }

                    if (simAssetIds.IsCreated)
                    {
                        outResult[simAssetIds[i].Value] = data;
                    }
                    else
                    {
#if SAFETY && UNITY_EDITOR
                        if (!chunk.Has(_tileColliderTagHandle))
                        {
                            Log.Error($"Error in collider collection: An entity ({string.Join(", ", chunk.Archetype.GetComponentTypes().Select(c => c.ToString()))}) " +
                                      $"has a collider but no SimAssetId");
                            continue;
                        }
#endif
                        outResult[ushort.MaxValue + 1] = data;
                    }
                }
            }
        }
Exemple #18
0
        public string Serialize(World world)
        {
            unsafe
            {
                var writer = new MemoryBinaryWriter();
                SerializeUtility.SerializeWorld(world.EntityManager, writer);

                var writtenBytes = new NativeArray <byte>(writer.Length, Allocator.Temp);

                UnsafeUtility.MemCpy((byte *)writtenBytes.GetUnsafePtr(), writer.Data, writer.Length);
                var text = Convert.ToBase64String(writtenBytes.ToArray());

                writtenBytes.Dispose();

                return(text);
            }
        }
        /// <summary>
        /// Create a clone of a BlobAssetReference
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static unsafe BlobAssetReference <T> Clone <T>(this BlobAssetReference <T> source) where T : struct
        {
            if (source == BlobAssetReference <T> .Null)
            {
                return(BlobAssetReference <T> .Null);
            }

            var writer = new MemoryBinaryWriter();

            writer.Write(source);
            var reader = new MemoryBinaryReader(writer.Data);
            var clone  = reader.Read <T>();

            writer.Dispose();
            reader.Dispose();

            return(clone);
        }
Exemple #20
0
        private static int ConfigureBlocks(UIModel.Node node, MemoryBinaryWriter writer, TypeDecomposer decomposer, ref UIPropertyWriterContext context, List <Type> types, int headerSize, out byte flags)
        {
            var configBlocks = new List <object>();

            UIConfigUtility.GetTypes(node.mask, types);
            UIConfigUtility.CreateConfiguration(node.mask, configBlocks);
            var configSize = UIConfigUtility.GetLength(node.mask);

            using var extraBytesStream = new MemoryBinaryWriter();
            IntPtr configData        = (IntPtr)UnsafeUtility.Malloc(configSize, 0, Allocator.Temp);
            int    configBlockOffset = 0;
            var    configFields      = new Dictionary <string, TypeDecomposer.FieldData>();

            flags = 0;
            foreach (var configBlock in configBlocks)
            {
                decomposer.Decompose(configBlock.GetType(), configFields, configBlock.GetType().GetCustomAttribute <UIConfigBlockAttribute>()?.Name, configBlockOffset, '-');
                Marshal.StructureToPtr(configBlock, configData + configBlockOffset, true);
                flags             |= StandardConfigurationHandlers.PreInit(configBlock.GetType(), configData + configBlockOffset, node.mask, ref context);
                configBlockOffset += UnsafeUtility.SizeOf(configBlock.GetType());
            }
            foreach (var property in node.properties)
            {
                if (configFields.TryGetValue(property.path, out TypeDecomposer.FieldData fieldData))
                {
                    StandardPropertyWriters.writers.Write(property.Value, configData, fieldData, extraBytesStream, configSize + headerSize, context);
                }
            }
            configBlockOffset = 0;
            foreach (var configBlockType in types)
            {
                flags             |= StandardConfigurationHandlers.PostInit(configBlockType, configData + configBlockOffset, configData, node.mask, extraBytesStream, configSize + headerSize, context);
                configBlockOffset += UnsafeUtility.SizeOf(configBlockType);
            }
            int length = configSize + extraBytesStream.Length;

            writer.WriteBytes(configData.ToPointer(), configSize);
            if (extraBytesStream.Length > 0)
            {
                writer.WriteBytes(extraBytesStream.Data, extraBytesStream.Length);
            }
            UnsafeUtility.Free(configData.ToPointer(), Allocator.Temp);
            return(length);
        }
Exemple #21
0
        /// <summary>
        /// Serializes a UIModel in the following format:
        /// Total Length: ulong (excluding this field)
        /// Node Count: int
        /// For each node present:
        /// Node Size: int (excluding this field)
        /// Header Data: HeaderConfig
        /// Children: int[], total length of children array can be found inside the HeaderConfig.
        /// ConfigBlocks: Which config blocks are present can be found by querying the configuration mask in the headerconfig
        /// </summary>
        public static void Write(this UIModel model, Stream stream, UISchema schema)
        {
            Contract.Assert(model.group != null);
            using var modelWriter = new MemoryBinaryWriter();
            modelWriter.Write(0UL);
            modelWriter.Write(model.nodes.Count);
            var context = new UIPropertyWriterContext
            {
                group = model.group
            };
            var  decomposer = new TypeDecomposer();
            var  types      = new List <Type>();
            long totalSize  = UnsafeUtility.SizeOf <int>();

            Configure(model, -1, 0, schema, modelWriter, decomposer, context, types, ref totalSize);

            /* for (int i = 0; i < model.nodes.Count; i++) {
             *  totalSize += Configure(model.nodes[i], modelWriter, decomposer, context, types) + UnsafeUtility.SizeOf<int>();
             * } */
            UnsafeUtility.MemCpy(modelWriter.Data, UnsafeUtility.AddressOf(ref totalSize), UnsafeUtility.SizeOf <ulong>());
            using var modelStream = new UnmanagedMemoryStream(modelWriter.Data, modelWriter.Length);
            modelStream.CopyTo(stream);
        }
Exemple #22
0
        public static bool WriteWorldToFile(World world, FileInfo outputFile)
        {
            // TODO need to bring this back
#if false
            // Check for missing assembly references
            var unresolvedComponentTypes = GetAllUsedComponentTypes(world).Where(t => !DomainCache.IsIncludedInProject(project, t.GetManagedType())).ToArray();
            if (unresolvedComponentTypes.Length > 0)
            {
                foreach (var unresolvedComponentType in unresolvedComponentTypes)
                {
                    var type = unresolvedComponentType.GetManagedType();
                    Debug.LogError($"Could not resolve component type '{type.FullName}' while exporting {scenePath.ToHyperLink()}. Are you missing an assembly reference to '{type.Assembly.GetName().Name}' ?");
                }
                return(false);
            }
#endif

            var directoryName = Path.GetDirectoryName(outputFile.FullName);
            if (string.IsNullOrEmpty(directoryName))
            {
                throw new ArgumentException($"Invalid output file directory: {directoryName}", nameof(outputFile));
            }

            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            // Merges the entities and shared component streams, (optionally) compresses them, and finally serializes to disk with a small header in front
            using (var fileStream = new StreamBinaryWriter(outputFile.FullName))
                using (var entitiesWriter = new MemoryBinaryWriter())
                {
                    var entityRemapInfos = new NativeArray <EntityRemapUtility.EntityRemapInfo>(world.EntityManager.EntityCapacity, Allocator.Temp);
                    SerializeUtility.SerializeWorldInternal(world.EntityManager, entitiesWriter, out var referencedObjects, entityRemapInfos, isDOTSRuntime: true);
                    entityRemapInfos.Dispose();

                    if (referencedObjects != null)
                    {
                        throw new ArgumentException("We are serializing a world that contains UnityEngine.Object references which are not supported in Dots Runtime.");
                    }

#if TINY_SCENE_DEP
                    unsafe
                    {
                        var worldHeader = new SceneHeader();
                        worldHeader.DecompressedSize = entitiesWriter.Length;
                        worldHeader.Codec            = Codec.LZ4;
                        worldHeader.SerializeHeader(fileStream);

                        if (worldHeader.Codec != Codec.None)
                        {
                            int compressedSize = CodecService.Compress(worldHeader.Codec, entitiesWriter.Data, entitiesWriter.Length, out var compressedData);
                            fileStream.WriteBytes(compressedData, compressedSize);
                        }
                        else
                        {
                            fileStream.WriteBytes(entitiesWriter.Data, entitiesWriter.Length);
                        }
                    }
#endif
                }

            return(true);
        }
 public MetadataRowWriter(MetadataTableWriter mtwv)
 {
     m_binaryWriter = mtwv.GetWriter();
     m_root         = mtwv.GetMetadataRoot();
     m_ciCache      = new Hashtable();
 }
        public override unsafe byte PostInit(TextConfig *value, IntPtr config, ulong mask, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context)
        {
            TMP_FontAsset fontAsset     = null;
            var           fontConfigPtr = UIConfigUtility.GetConfig(mask, UIConfigLayoutTable.FontConfig, config);


            if (fontConfigPtr == IntPtr.Zero)
            {
                return(0);
            }
            FontConfig *fontConfig = ((FontConfig *)fontConfigPtr);
            var         guid       = fontConfig->asset.ToHex();

#if UNITY_EDITOR
            fontAsset = UnityEditor.AssetDatabase.LoadAssetAtPath <TMP_FontAsset>(UnityEditor.AssetDatabase.GUIDToAssetPath(guid));
#else
//var task = Addressables.LoadAssetAsync<TMP_FontAsset>(guid);
//fontAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(AssetDatabase.GUIDToAssetPath(guid));
#endif
            var index = context.group.GetAtlasIndex(guid);
            if (fontAsset != null && index >= 0)
            {
                value->fontInfo       = new FontInfo(fontAsset);
                value->charInfoOffset = extraBytesStream.Length;
                for (int charIndex = 0; charIndex < value->text.length; charIndex++)
                {
                    UnsafeUtility.CopyPtrToStructure(new IntPtr(((IntPtr)extraBytesStream.Data).ToInt64() + (value->text.offset - extraByteStreamOffset) + (charIndex * 2)).ToPointer(), out char character);
                    var charInfo      = fontAsset.characterLookupTable[character];
                    var charInfoValue = new CharInfo
                    {
                        uvs     = new float4(charInfo.glyph.glyphRect.x / (float)fontAsset.atlasWidth, charInfo.glyph.glyphRect.y / (float)fontAsset.atlasHeight, charInfo.glyph.glyphRect.width / (float)fontAsset.atlasWidth, charInfo.glyph.glyphRect.height / (float)fontAsset.atlasHeight),
                        metrics = charInfo.glyph.metrics,
                        index   = (byte)index,
                        unicode = charInfo.unicode
                    };
                    extraBytesStream.WriteBytes(UnsafeUtility.AddressOf(ref charInfoValue), UnsafeUtility.SizeOf <CharInfo>());
                }
                value->charInfoLength = value->text.length;
                //value->charInfoLength = (int)((extraBytesStream.Length - value->charInfoOffset) / UnsafeUtility.SizeOf<CharInfo>());
                value->charInfoOffset += extraByteStreamOffset;
            }
            return(0);
        }
 public override unsafe byte PostInit(NameConfig *value, IntPtr config, ulong mask, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context)
 {
     return((byte)(value->name.IsCreated ? 1 : 0));
 }
Exemple #26
0
 public static NativeArray <byte> GetContentAsNativeArray(MemoryBinaryWriter writer) => writer.GetContentAsNativeArray();
 public static byte PostInit(Type type, IntPtr value, IntPtr config, ulong mask, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context)
 {
     if (configurators.TryGetValue(type, out UIConfigurationHandler configurator))
     {
         return(configurator.PostInit(value, config, mask, extraBytesStream, extraByteStreamOffset, context));
     }
     else
     {
         return(0);
     }
 }
Exemple #28
0
        public static unsafe bool WriteToDisk(FixedString128 gameSaveFilePath, MemoryBinaryWriter writer)
        {
            string gameSaveFilePathString = gameSaveFilePath.ToString();

            return(js_gamesave_writeToDisk(gameSaveFilePathString, (byte *)writer.Data, (int)writer.Length));
        }
 public override unsafe byte PostInit(SizeConfig *value, IntPtr config, ulong mask, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context)
 {
     return(0);
 }
 public virtual byte PostInit(IntPtr value, IntPtr config, ulong mask, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context)
 {
     return(0);
 }