Ejemplo n.º 1
0
        public static CR2WFile FromCResource(this CR2WFile file, CResource res, bool cooked = false)
        {
            // checks to see if the variable from which the chunk is built is properly constructed
            if (res == null || res.REDName != res.REDType || res.ParentVar != null)
            {
                throw new NotImplementedException();
            }

            file.CreateChunk(res);

            return(file);
        }
Ejemplo n.º 2
0
        void Write(FileInfo f)
        {
            CR2WFile cr2w = new CR2WFile();

            {
                var blob = new Multilayer_Mask(cr2w, null, "Multilayer_Mask")
                {
                    IsSerialized = true
                };
                blob.CookingPlatform = new CEnum <Enums.ECookingPlatform>(cr2w, blob, "cookingPlatform")
                {
                    IsSerialized = true, Value = Enums.ECookingPlatform.PLATFORM_PC
                };
                blob.CookingPlatform.EnumValueList.Add("PLATFORM_PC");
                blob.RenderResourceBlob.RenderResourceBlobPC = new CHandle <IRenderResourceBlob>(cr2w, blob.RenderResourceBlob, "renderResourceBlobPC")
                {
                    IsSerialized = true
                };
                cr2w.CreateChunk(blob, 0);
            }
            {
                var blob = new rendRenderMultilayerMaskBlobPC(cr2w, null, "rendRenderMultilayerMaskBlobPC")
                {
                    IsSerialized = true
                };
                blob.Header = new rendRenderMultilayerMaskBlobHeader(cr2w, blob, "header")
                {
                    IsSerialized = true
                };
                blob.Header.Version = new CUInt32(cr2w, blob.Header, "version")
                {
                    IsSerialized = true, Value = 3
                };
                blob.Header.AtlasWidth = new CUInt32(cr2w, blob.Header, "atlasWidth")
                {
                    IsSerialized = true, Value = mlmask._atlasWidth
                };
                blob.Header.AtlasHeight = new CUInt32(cr2w, blob.Header, "atlasHeight")
                {
                    IsSerialized = true, Value = mlmask._atlasHeight
                };
                blob.Header.NumLayers = new CUInt32(cr2w, blob.Header, "numLayers")
                {
                    IsSerialized = true, Value = (uint)mlmask.layers.Length
                };
                blob.Header.MaskWidth = new CUInt32(cr2w, blob.Header, "maskWidth")
                {
                    IsSerialized = true, Value = mlmask._widthHigh
                };
                blob.Header.MaskHeight = new CUInt32(cr2w, blob.Header, "maskHeight")
                {
                    IsSerialized = true, Value = mlmask._heightHigh
                };
                blob.Header.MaskWidthLow = new CUInt32(cr2w, blob.Header, "maskWidthLow")
                {
                    IsSerialized = true, Value = mlmask._widthLow
                };
                blob.Header.MaskHeightLow = new CUInt32(cr2w, blob.Header, "maskHeightLow")
                {
                    IsSerialized = true, Value = mlmask._heightLow
                };
                blob.Header.MaskTileSize = new CUInt32(cr2w, blob.Header, "maskTileSize")
                {
                    IsSerialized = true, Value = mlmask._tileSize
                };
                blob.Header.Flags = new CUInt32(cr2w, blob.Header, "flags")
                {
                    IsSerialized = true, Value = 2
                };
                blob.AtlasData = new serializationDeferredDataBuffer(cr2w, blob, "atlasData")
                {
                    IsSerialized = true
                };
                blob.AtlasData.Buffer = new CUInt16(cr2w, blob.AtlasData, "Buffer")
                {
                    IsSerialized = true, Value = 1
                };
                blob.TilesData = new serializationDeferredDataBuffer(cr2w, blob, "tilesData")
                {
                    IsSerialized = true
                };
                blob.TilesData.Buffer = new CUInt16(cr2w, blob.TilesData, "Buffer")
                {
                    IsSerialized = true, Value = 2
                };

                cr2w.CreateChunk(blob, 1);
            }
            (cr2w.Chunks[0].Data as Multilayer_Mask).RenderResourceBlob.RenderResourceBlobPC.SetReference(cr2w.Chunks[1]);

            //test write
            var ms = new MemoryStream();
            var bw = new BinaryWriter(ms);

            using (var compressed = new MemoryStream())
            {
                using var buff  = new BinaryWriter(compressed);
                var(zsize, crc) = buff.CompressAndWrite(mlmask._AtlasBuffer);

                cr2w.Buffers.Add(new CR2WBufferWrapper(new CR2WBuffer()
                {
                    flags    = 0,
                    index    = 0,
                    offset   = 0,
                    diskSize = zsize,
                    memSize  = (UInt32)mlmask._AtlasBuffer.Length,
                    crc32    = crc
                }));

                cr2w.Buffers[0].ReadData(new BinaryReader(compressed));
                // to get offset
                cr2w.Write(bw);
                cr2w.Buffers[0].Offset = (uint)ms.Length;
            }
            using (var compressed = new MemoryStream())
            {
                using var buff  = new BinaryWriter(compressed);
                var(zsize, crc) = buff.CompressAndWrite(mlmask._tilesBuffer);

                cr2w.Buffers.Add(new CR2WBufferWrapper(new CR2WBuffer()
                {
                    flags    = 0,
                    index    = 1,
                    offset   = 0,
                    diskSize = zsize,
                    memSize  = (UInt32)mlmask._tilesBuffer.Length,
                    crc32    = crc
                }));

                cr2w.Buffers[1].ReadData(new BinaryReader(compressed));
                cr2w.Buffers[1].Offset = cr2w.Buffers[0].Offset + cr2w.Buffers[0].DiskSize;
            }

            cr2w.Write(bw);
            if (!Directory.Exists(f.Directory.FullName))
            {
                Directory.CreateDirectory(f.Directory.FullName);
            }
            File.WriteAllBytes(f.FullName, ms.ToArray());
        }
Ejemplo n.º 3
0
        public bool WriteMatToMesh(ref CR2WFile cr2w, string _matData)
        {
            if (cr2w == null || !cr2w.Chunks.Select(_ => _.Data).OfType <CMesh>().Any() || !cr2w.Chunks.Select(_ => _.Data).OfType <rendRenderMeshBlob>().Any() || cr2w.Buffers.Count < 1)
            {
                return(false);
            }

            var obj = JsonConvert.DeserializeObject <MatData>(_matData);

            var           materialbuffer = new MemoryStream();
            List <UInt32> offsets        = new List <UInt32>();
            List <UInt32> sizes          = new List <UInt32>();
            List <string> names          = new List <string>();

            if (obj.Materials.Count < 1)
            {
                return(false);
            }

            for (int i = 0; i < obj.Materials.Count; i++)
            {
                var mat = obj.Materials[i];
                names.Add(mat.Name);
                CR2WFile mi = new CR2WFile();
                {
                    var chunk = new CMaterialInstance(mi, null, "CMaterialInstance")
                    {
                        IsSerialized = true
                    };
                    chunk.CookingPlatform = new CEnum <Enums.ECookingPlatform>(mi, chunk, "cookingPlatform")
                    {
                        IsSerialized = true, Value = Enums.ECookingPlatform.PLATFORM_PC
                    };
                    chunk.CookingPlatform.EnumValueList.Add("PLATFORM_PC");
                    chunk.ResourceVersion = new CUInt8(mi, chunk, "resourceVersion")
                    {
                        IsSerialized = true, Value = 4
                    };
                    chunk.BaseMaterial = new rRef <IMaterial>(mi, chunk, "baseMaterial")
                    {
                        IsSerialized = true, DepotPath = mat.BaseMaterial
                    };
                    chunk.CMaterialInstanceData = new CArray <CVariantSizeNameType>(mi, chunk, "CMaterialInstanceData")
                    {
                        IsSerialized = true
                    };
                    mi.CreateChunk(chunk, 0);
                }
                MATERIAL.WriteMatToMeshEnum(ref mi, ref mat);

                offsets.Add((UInt32)materialbuffer.Position);
                var m = new MemoryStream();
                var b = new BinaryWriter(m);
                mi.Write(b);
                materialbuffer.Write(m.ToArray(), 0, (int)m.Length);
                sizes.Add((UInt32)m.Length);
            }

            var blob = cr2w.Chunks.Select(_ => _.Data).OfType <CMesh>().First();

            // remove existing data
            while (blob.MaterialEntries.Count != 0)
            {
                blob.MaterialEntries.Remove(blob.MaterialEntries[blob.MaterialEntries.Count - 1]);
            }
            while (blob.LocalMaterialBuffer.RawDataHeaders.Count != 0)
            {
                blob.LocalMaterialBuffer.RawDataHeaders.Remove(blob.LocalMaterialBuffer.RawDataHeaders[blob.LocalMaterialBuffer.RawDataHeaders.Count - 1]);
            }
            while (blob.PreloadLocalMaterialInstances.Count != 0)
            {
                blob.PreloadLocalMaterialInstances.Remove(blob.PreloadLocalMaterialInstances[blob.PreloadLocalMaterialInstances.Count - 1]);
            }
            while (blob.PreloadExternalMaterials.Count != 0)
            {
                blob.PreloadExternalMaterials.Remove(blob.PreloadExternalMaterials[blob.PreloadExternalMaterials.Count - 1]);
            }
            while (blob.ExternalMaterials.Count != 0)
            {
                blob.ExternalMaterials.Remove(blob.ExternalMaterials[blob.ExternalMaterials.Count - 1]);
            }
            while (blob.LocalMaterialInstances.Count != 0)
            {
                blob.LocalMaterialInstances.Remove(blob.LocalMaterialInstances[blob.LocalMaterialInstances.Count - 1]);
            }
            //
            for (int i = 0; i < names.Count; i++)
            {
                var c = new CMeshMaterialEntry(cr2w, blob.MaterialEntries, Convert.ToString(i))
                {
                    IsSerialized = true,
                };
                c.IsLocalInstance = new CBool(cr2w, c, "isLocalInstance")
                {
                    Value = true, IsSerialized = true
                };
                c.Name = new CName(cr2w, c, "name")
                {
                    Value = names[i], IsSerialized = true
                };
                c.Index = new CUInt16(cr2w, c, "index")
                {
                    Value = (UInt16)i, IsSerialized = true
                };
                blob.MaterialEntries.Add(c);

                var m = new meshLocalMaterialHeader(cr2w, blob.LocalMaterialBuffer.RawDataHeaders, Convert.ToString(i))
                {
                    IsSerialized = true
                };
                m.Offset = new CUInt32(cr2w, m, "offset")
                {
                    Value = offsets[i], IsSerialized = true
                };
                m.Size = new CUInt32(cr2w, m, "size")
                {
                    Value = sizes[i], IsSerialized = true
                };
                blob.LocalMaterialBuffer.RawDataHeaders.Add(m);
            }

            var compressed = new MemoryStream();

            using var buff  = new BinaryWriter(compressed);
            var(zsize, crc) = buff.CompressAndWrite(materialbuffer.ToArray());

            bool check = false;

            check = blob.LocalMaterialBuffer.RawData.IsSerialized;
            if (!check)
            {
                blob.LocalMaterialBuffer.RawData = new DataBuffer(cr2w, blob.LocalMaterialBuffer, "rawData")
                {
                    IsSerialized = true
                };
                blob.LocalMaterialBuffer.RawData.Buffer = new CUInt16(cr2w, blob.LocalMaterialBuffer.RawData, "Buffer")
                {
                    Value = (UInt16)(cr2w.Buffers.Count + 1), IsSerialized = true
                };

                uint idx = (uint)cr2w.Buffers.Count;
                cr2w.Buffers.Add(new CR2WBufferWrapper(new CR2WBuffer()
                {
                    flags    = 0,
                    index    = idx,
                    offset   = 0,
                    diskSize = zsize,
                    memSize  = (UInt32)materialbuffer.Length,
                    crc32    = crc
                }));

                cr2w.Buffers[(int)idx].ReadData(new BinaryReader(compressed));
                cr2w.Buffers[(int)idx].Offset = cr2w.Buffers[(int)idx - 1].Offset + cr2w.Buffers[(int)idx - 1].DiskSize;
            }
            else
            {
                UInt16 p = (blob.LocalMaterialBuffer.RawData.Buffer.Value);
                cr2w.Buffers[p - 1].DiskSize = zsize;
                cr2w.Buffers[p - 1].Crc32    = crc;
                cr2w.Buffers[p - 1].MemSize  = (UInt32)materialbuffer.Length;
                var off = cr2w.Buffers[p - 1].Offset;
                cr2w.Buffers[p - 1].Offset = 0;
                cr2w.Buffers[p - 1].ReadData(new BinaryReader(compressed));
                cr2w.Buffers[p - 1].Offset = off;
            }

            return(true);
        }
Ejemplo n.º 4
0
        public bool WriteMatToMesh(ref CR2WFile cr2w, string _matData, List <Archive> archives)
        {
            if (cr2w == null || !cr2w.Chunks.Select(_ => _.Data).OfType <CMesh>().Any() || !cr2w.Chunks.Select(_ => _.Data).OfType <rendRenderMeshBlob>().Any() || cr2w.Buffers.Count < 1)
            {
                return(false);
            }
            var matData = JsonConvert.DeserializeObject <MatData>(_matData);

            var           materialbuffer = new MemoryStream();
            List <UInt32> offsets        = new List <UInt32>();
            List <UInt32> sizes          = new List <UInt32>();
            List <string> names          = new List <string>();

            if (matData.Materials.Count < 1)
            {
                return(false);
            }

            Dictionary <string, CMaterialTemplate> mts = new Dictionary <string, CMaterialTemplate>();

            for (int i = 0; i < matData.Materials.Count; i++)
            {
                var mat = matData.Materials[i];
                names.Add(mat.Name);
                CR2WFile mi = new CR2WFile();
                {
                    var chunk = new CMaterialInstance(mi, null, "CMaterialInstance")
                    {
                        IsSerialized = true
                    };
                    chunk.CookingPlatform = new CEnum <Enums.ECookingPlatform>(mi, chunk, "cookingPlatform")
                    {
                        IsSerialized = true, Value = Enums.ECookingPlatform.PLATFORM_PC
                    };
                    chunk.CookingPlatform.EnumValueList.Add("PLATFORM_PC");
                    chunk.EnableMask = new CBool(mi, chunk, "enableMask")
                    {
                        IsSerialized = true, Value = true
                    };
                    chunk.ResourceVersion = new CUInt8(mi, chunk, "resourceVersion")
                    {
                        IsSerialized = true, Value = 4
                    };
                    chunk.BaseMaterial = new rRef <IMaterial>(mi, chunk, "baseMaterial")
                    {
                        IsSerialized = true, DepotPath = mat.BaseMaterial
                    };
                    chunk.CMaterialInstanceData = new CArray <CVariantSizeNameType>(mi, chunk, "CMaterialInstanceData")
                    {
                        IsSerialized = true
                    };

                    CMaterialTemplate mt = null;
                    if (mts.ContainsKey(mat.MaterialTemplate))
                    {
                        mt = mts[mat.MaterialTemplate];
                    }
                    else
                    {
                        ulong hash = FNV1A64HashAlgorithm.HashString(mat.MaterialTemplate);
                        foreach (Archive ar in archives)
                        {
                            if (ar.Files.ContainsKey(hash))
                            {
                                var ms = new MemoryStream();
                                ModTools.ExtractSingleToStream(ar, hash, ms);
                                mt = _wolvenkitFileService.TryReadRED4File(ms).Chunks.Select(_ => _.Data).OfType <CMaterialTemplate>().First();
                                mts.Add(mat.MaterialTemplate, mt);
                                break;
                            }
                        }
                    }
                    var keys = matData.Materials[i].Data.Keys.ToList();
                    if (mt != null)
                    {
                        for (int j = 0; j < keys.Count; j++)
                        {
                            string typename = null;
                            for (int k = 0; k < mt.Parameters[2].Count; k++)
                            {
                                var refer = mt.Parameters[2][k].GetReference().Data;
                                if ((refer.ChildrEditableVariables[0] as CName).Value == keys[j])
                                {
                                    if (refer.ChildrEditableVariables.Count > 2)
                                    {
                                        typename = refer.ChildrEditableVariables[2].REDType;
                                    }
                                }
                            }
                            if (typename != null)
                            {
                                // remove when setfromobj deserialization is fixed
                                if (typename == "Color")
                                {
                                    CColor_ value0 = new CColor_(new CR2WFile(), null, keys[j]);
                                    value0.IsSerialized = true;
                                    value0.SetFromJObject(matData.Materials[i].Data[keys[j]]);

                                    var    variant = new CVariantSizeNameType(mi, chunk.CMaterialInstanceData, keys[j]);
                                    CColor value   = new CColor(mi, variant, keys[j]);
                                    value.IsSerialized = true;
                                    value.Red          = new CUInt8(mi, value, "Red")
                                    {
                                        IsSerialized = true, Value = value0.Red.Value
                                    };
                                    value.Green = new CUInt8(mi, value, "Green")
                                    {
                                        IsSerialized = true, Value = value0.Green.Value
                                    };
                                    value.Blue = new CUInt8(mi, value, "Blue")
                                    {
                                        IsSerialized = true, Value = value0.Blue.Value
                                    };
                                    value.Alpha = new CUInt8(mi, value, "Alpha")
                                    {
                                        IsSerialized = true, Value = value0.Alpha.Value
                                    };
                                    variant.SetVariant(value);
                                    chunk.CMaterialInstanceData.Add(variant);
                                }
                                else
                                {
                                    var variant = new CVariantSizeNameType(mi, chunk.CMaterialInstanceData, keys[j]);
                                    var value   = CR2WTypeManager.Create(typename, keys[j], mi, variant);
                                    value.IsSerialized = true;
                                    value.SetFromJObject(matData.Materials[i].Data[keys[j]]);
                                    variant.SetVariant(value);
                                    chunk.CMaterialInstanceData.Add(variant);
                                }
                            }
                        }
                    }


                    mi.CreateChunk(chunk, 0);
                }

                offsets.Add((UInt32)materialbuffer.Position);
                var m = new MemoryStream();
                var b = new BinaryWriter(m);
                mi.Write(b);
                materialbuffer.Write(m.ToArray(), 0, (int)m.Length);
                sizes.Add((UInt32)m.Length);
            }

            var blob = cr2w.Chunks.Select(_ => _.Data).OfType <CMesh>().First();

            // remove existing data
            while (blob.MaterialEntries.Count != 0)
            {
                blob.MaterialEntries.Remove(blob.MaterialEntries[blob.MaterialEntries.Count - 1]);
            }
            while (blob.LocalMaterialBuffer.RawDataHeaders.Count != 0)
            {
                blob.LocalMaterialBuffer.RawDataHeaders.Remove(blob.LocalMaterialBuffer.RawDataHeaders[blob.LocalMaterialBuffer.RawDataHeaders.Count - 1]);
            }
            while (blob.PreloadLocalMaterialInstances.Count != 0)
            {
                blob.PreloadLocalMaterialInstances.Remove(blob.PreloadLocalMaterialInstances[blob.PreloadLocalMaterialInstances.Count - 1]);
            }
            while (blob.PreloadExternalMaterials.Count != 0)
            {
                blob.PreloadExternalMaterials.Remove(blob.PreloadExternalMaterials[blob.PreloadExternalMaterials.Count - 1]);
            }
            while (blob.ExternalMaterials.Count != 0)
            {
                blob.ExternalMaterials.Remove(blob.ExternalMaterials[blob.ExternalMaterials.Count - 1]);
            }
            while (blob.LocalMaterialInstances.Count != 0)
            {
                blob.LocalMaterialInstances.Remove(blob.LocalMaterialInstances[blob.LocalMaterialInstances.Count - 1]);
            }
            //
            for (int i = 0; i < names.Count; i++)
            {
                var c = new CMeshMaterialEntry(cr2w, blob.MaterialEntries, Convert.ToString(i))
                {
                    IsSerialized = true,
                };
                c.IsLocalInstance = new CBool(cr2w, c, "isLocalInstance")
                {
                    Value = true, IsSerialized = true
                };
                c.Name = new CName(cr2w, c, "name")
                {
                    Value = names[i], IsSerialized = true
                };
                c.Index = new CUInt16(cr2w, c, "index")
                {
                    Value = (UInt16)i, IsSerialized = true
                };
                blob.MaterialEntries.Add(c);

                var m = new meshLocalMaterialHeader(cr2w, blob.LocalMaterialBuffer.RawDataHeaders, Convert.ToString(i))
                {
                    IsSerialized = true
                };
                m.Offset = new CUInt32(cr2w, m, "offset")
                {
                    Value = offsets[i], IsSerialized = true
                };
                m.Size = new CUInt32(cr2w, m, "size")
                {
                    Value = sizes[i], IsSerialized = true
                };
                blob.LocalMaterialBuffer.RawDataHeaders.Add(m);
            }

            var compressed = new MemoryStream();

            using var buff  = new BinaryWriter(compressed);
            var(zsize, crc) = buff.CompressAndWrite(materialbuffer.ToArray());

            bool check = false;

            check = blob.LocalMaterialBuffer.RawData.IsSerialized;
            if (!check)
            {
                blob.LocalMaterialBuffer.RawData = new DataBuffer(cr2w, blob.LocalMaterialBuffer, "rawData")
                {
                    IsSerialized = true
                };
                blob.LocalMaterialBuffer.RawData.Buffer = new CUInt16(cr2w, blob.LocalMaterialBuffer.RawData, "Buffer")
                {
                    Value = (UInt16)(cr2w.Buffers.Count + 1), IsSerialized = true
                };

                uint idx = (uint)cr2w.Buffers.Count;
                cr2w.Buffers.Add(new CR2WBufferWrapper(new CR2WBuffer()
                {
                    flags    = 0,
                    index    = idx,
                    offset   = 0,
                    diskSize = zsize,
                    memSize  = (UInt32)materialbuffer.Length,
                    crc32    = crc
                }));

                cr2w.Buffers[(int)idx].ReadData(new BinaryReader(compressed));
                cr2w.Buffers[(int)idx].Offset = cr2w.Buffers[(int)idx - 1].Offset + cr2w.Buffers[(int)idx - 1].DiskSize;
            }
            else
            {
                UInt16 p = (blob.LocalMaterialBuffer.RawData.Buffer.Value);
                cr2w.Buffers[p - 1].DiskSize = zsize;
                cr2w.Buffers[p - 1].Crc32    = crc;
                cr2w.Buffers[p - 1].MemSize  = (UInt32)materialbuffer.Length;
                var off = cr2w.Buffers[p - 1].Offset;
                cr2w.Buffers[p - 1].Offset = 0;
                cr2w.Buffers[p - 1].ReadData(new BinaryReader(compressed));
                cr2w.Buffers[p - 1].Offset = off;
            }

            return(true);
        }