public byte[] ToArray(uint pccExportDataOffset, ME3PCCObject pcc) { using (MemoryStream tempStream = new MemoryStream()) { tempStream.WriteBytes(headerData); // Whilst testing get rid of this // Heff: Seems like the shadowmap was the best solution in most cases, // adding an exception for known problematic animated textures for now. // (See popup in tpftools) if (properties.ContainsKey("LODGroup")) properties["LODGroup"].Value.String2 = "TEXTUREGROUP_Shadowmap"; else { tempStream.WriteValueS64(pcc.addName2("LODGroup")); tempStream.WriteValueS64(pcc.addName2("ByteProperty")); tempStream.WriteValueS64(8); tempStream.WriteValueS64(pcc.addName2("TextureGroup")); tempStream.WriteValueS64(pcc.addName2("TEXTUREGROUP_Shadowmap")); } foreach (KeyValuePair<string, SaltPropertyReader.Property> kvp in properties) { SaltPropertyReader.Property prop = kvp.Value; if (prop.Name == "UnpackMin") { for (int i = 0; i < UnpackNum; i++) { tempStream.WriteValueS64(pcc.addName2(prop.Name)); tempStream.WriteValueS64(pcc.addName2(prop.TypeVal.ToString())); tempStream.WriteValueS32(prop.Size); tempStream.WriteValueS32(i); tempStream.WriteValueF32(prop.Value.FloatValue); } continue; } tempStream.WriteValueS64(pcc.addName2(prop.Name)); if (prop.Name == "None") continue; tempStream.WriteValueS64(pcc.addName2(prop.TypeVal.ToString())); tempStream.WriteValueS64(prop.Size); switch (prop.TypeVal) { case SaltPropertyReader.Type.FloatProperty: tempStream.WriteValueF32(prop.Value.FloatValue); break; case SaltPropertyReader.Type.IntProperty: tempStream.WriteValueS32(prop.Value.IntValue); break; case SaltPropertyReader.Type.NameProperty: tempStream.WriteValueS64(pcc.addName2(prop.Value.StringValue)); // Heff: Modified to handle name references. //var nameIndex = pcc.addName2(prop.Value.StringValue); //tempStream.WriteValueS32(nameIndex); //tempStream.WriteValueS32(prop.Value.NameValue.count); break; case SaltPropertyReader.Type.ByteProperty: tempStream.WriteValueS64(pcc.addName2(prop.Value.StringValue)); tempStream.WriteValueS64(pcc.addName2(prop.Value.String2)); // Heff: Modified to handle name references. //var valueIndex = pcc.addName2(prop.Value.String2); //tempStream.WriteValueS32(valueIndex); //tempStream.WriteValueS32(prop.Value.NameValue.count); //tempStream.WriteValueS32(pcc.addName2(prop.Value.String2)); //byte[] footer = new byte[4]; //Buffer.BlockCopy(prop.raw, prop.raw.Length - 4, footer, 0, 4); //tempStream.WriteBytes(footer); break; case SaltPropertyReader.Type.BoolProperty: tempStream.WriteValueBoolean(prop.Value.Boolereno); break; case SaltPropertyReader.Type.StructProperty: tempStream.WriteValueS64(pcc.addName2(prop.Value.StringValue)); for (int i = 0; i < prop.Size; i++) tempStream.WriteByte((byte)prop.Value.Array[i].IntValue); break; default: throw new NotImplementedException("Property type: " + prop.TypeVal + ", not yet implemented. TELL ME ABOUT THIS!"); } } //Remove empty textures List<ImageInfo> tempList = new List<ImageInfo>(); foreach (ImageInfo imgInfo in privateimgList) { if (imgInfo.storageType != storage.empty) tempList.Add(imgInfo); } privateimgList = tempList; numMipMaps = (uint)privateimgList.Count; tempStream.WriteValueU32(numMipMaps); foreach (ImageInfo imgInfo in privateimgList) { tempStream.WriteValueS32((int)imgInfo.storageType); tempStream.WriteValueS32(imgInfo.uncSize); tempStream.WriteValueS32(imgInfo.cprSize); if (imgInfo.storageType == storage.pccSto) { tempStream.WriteValueS32((int)(imgInfo.offset + pccExportDataOffset + dataOffset)); tempStream.Write(imageData, imgInfo.offset, imgInfo.uncSize); } else tempStream.WriteValueS32(imgInfo.offset); tempStream.WriteValueU32(imgInfo.imgSize.width); tempStream.WriteValueU32(imgInfo.imgSize.height); } //// Texture2D footer, 24 bytes size - changed to 20 //tempStream.Write(imageData, imageData.Length - 20, 20); tempStream.WriteBytes(footerData); return tempStream.ToArray(); } #region Unused Code /* bool lodExists = false; foreach (KeyValuePair<string, PropertyReader.Property> kvp in properties) { PropertyReader.Property property = kvp.Value; if (kvp.Key == "LODGroup") { lodExists = true; break; } } MemoryStream buffer = new MemoryStream(); buffer.Write(headerData, 0, headerData.Length); if (lodExists) { // extracting values from LODGroup Property PropertyReader.Property LODGroup = properties["LODGroup"]; string textureGroupName = pcc.Names[LODGroup.Value.IntValue]; bool nameExists = false; string newTextureGroupName = "TEXTUREGROUP_Shadowmap"; if (String.Compare(newTextureGroupName, textureGroupName) != 0) { textureGroupName = newTextureGroupName; if (!pcc.Names.Exists(name => name == newTextureGroupName)) pcc.Names.Add(newTextureGroupName); using (MemoryStream rawStream = new MemoryStream(LODGroup.raw)) { rawStream.Seek(32, SeekOrigin.Begin); rawStream.WriteValueS32(pcc.Names.FindIndex(name => name == newTextureGroupName)); rawStream.WriteValueS32(0); properties["LODGroup"].raw = rawStream.ToArray(); } } else nameExists = true; //MemoryStream buffer = new MemoryStream(); //buffer.Write(headerData, 0, headerData.Length); foreach (KeyValuePair<string, PropertyReader.Property> kvp in properties) { PropertyReader.Property property = kvp.Value; if (kvp.Key == "LODBias") continue; if (kvp.Key == "InternalFormatLODBias") continue; if (kvp.Key == "LODGroup" && nameExists == false) { int name; if (!nameExists) name = pcc.Names.Count - 1; //Warranty Voiders Name redirect hack^^ else name = LODGroup.Value.IntValue; ME3_HR_Patch.Helper.BitConverter.IsLittleEndian = true; byte[] buff = ME3_HR_Patch.Helper.BitConverter.GetBytes(name); for (int i = 0; i < 4; i++) property.raw[i + 24] = buff[i]; } buffer.Write(property.raw, 0, property.raw.Length); if (kvp.Key == "UnpackMin") { buffer.Write(property.raw, 0, property.raw.Length); buffer.Write(property.raw, 0, property.raw.Length); } } } else { //MemoryStream buffer = new MemoryStream(); //buffer.Write(headerData, 0, headerData.Length); int lodID = pcc.findName("LODGroup"); if (lodID == -1) { pcc.addName("LODGroup"); lodID = pcc.Names.Count - 1; } buffer.WriteBytes(ME3_HR_Patch.Helper.BitConverter.GetBytes(lodID)); buffer.WriteBytes(ME3_HR_Patch.Helper.BitConverter.GetBytes((int)0)); lodID = pcc.findName("ByteProperty"); buffer.WriteBytes(ME3_HR_Patch.Helper.BitConverter.GetBytes(lodID)); buffer.WriteBytes(ME3_HR_Patch.Helper.BitConverter.GetBytes((int)0)); //Write an int buffer.WriteBytes(ME3_HR_Patch.Helper.BitConverter.GetBytes((int)8)); buffer.WriteBytes(ME3_HR_Patch.Helper.BitConverter.GetBytes((int)0)); lodID = pcc.findName("TextureGroup"); if (lodID == -1) { pcc.addName("TextureGroup"); lodID = pcc.Names.Count - 1; } buffer.WriteBytes(ME3_HR_Patch.Helper.BitConverter.GetBytes(lodID)); buffer.WriteBytes(ME3_HR_Patch.Helper.BitConverter.GetBytes((int)0)); lodID = pcc.findName("TEXTUREGROUP_Shadowmap"); if (lodID == -1) { pcc.addName("TEXTUREGROUP_Shadowmap"); lodID = pcc.Names.Count - 1; } buffer.WriteBytes(ME3_HR_Patch.Helper.BitConverter.GetBytes(lodID)); buffer.WriteBytes(ME3_HR_Patch.Helper.BitConverter.GetBytes((int)0)); foreach (KeyValuePair<string, PropertyReader.Property> kvp in properties) { PropertyReader.Property property = kvp.Value; if (kvp.Key == "LODBias") continue; if (kvp.Key == "InternalFormatLODBias") continue; if (kvp.Key == "LODGroup") { int name = pcc.Names.Count - 1; //Warranty Voiders Name redirect hack^^ ME3_HR_Patch.Helper.BitConverter.IsLittleEndian = true; byte[] buff = ME3_HR_Patch.Helper.BitConverter.GetBytes(name); for (int i = 0; i < 4; i++) property.raw[i + 24] = buff[i]; } buffer.Write(property.raw, 0, property.raw.Length); if (kvp.Key == "UnpackMin") { buffer.Write(property.raw, 0, property.raw.Length); buffer.Write(property.raw, 0, property.raw.Length); } } } buffer.WriteValueU32(numMipMaps); foreach (ImageInfo imgInfo in imgList) { buffer.WriteValueS32((int)imgInfo.storageType); buffer.WriteValueS32(imgInfo.uncSize); buffer.WriteValueS32(imgInfo.cprSize); if (imgInfo.storageType == storage.pccSto) { buffer.WriteValueS32((int)(imgInfo.offset + pccExportDataOffset + dataOffset)); buffer.Write(imageData, imgInfo.offset, imgInfo.uncSize); } else buffer.WriteValueS32(imgInfo.offset); buffer.WriteValueU32(imgInfo.imgSize.width); buffer.WriteValueU32(imgInfo.imgSize.height); } // Texture2D footer, 24 bytes size buffer.Write(imageData, imageData.Length - 24, 24); byte[] rawData = buffer.ToArray(); return rawData; */ #endregion }
public void CopyImgList(ME3SaltTexture2D inTex, ME3PCCObject pcc) { imageData = inTex.imageData; privateimgList = inTex.privateimgList; numMipMaps = inTex.numMipMaps; //Copy Properties byte[] buff; using (MemoryStream tempMem = new MemoryStream()) { tempMem.WriteBytes(headerData); for (int i = 0; i < inTex.properties.Count; i++) { SaltPropertyReader.Property prop = inTex.properties.ElementAt(i).Value; if (prop.Name == "UnpackMin") { for (int j = 0; j < inTex.UnpackNum; j++) { tempMem.WriteValueS64(pcc.addName2(prop.Name)); tempMem.WriteValueS64(pcc.addName2(prop.TypeVal.ToString())); tempMem.WriteValueS32(prop.Size); tempMem.WriteValueS32(j); tempMem.WriteValueF32(prop.Value.FloatValue); } continue; } tempMem.WriteValueS64(pcc.addName2(prop.Name)); if (prop.Name == "None") continue; tempMem.WriteValueS64(pcc.addName2(prop.TypeVal.ToString())); tempMem.WriteValueS64(prop.Size); switch (prop.TypeVal) { case SaltPropertyReader.Type.FloatProperty: tempMem.WriteValueF32(prop.Value.FloatValue); break; case SaltPropertyReader.Type.IntProperty: tempMem.WriteValueS32(prop.Value.IntValue); break; case SaltPropertyReader.Type.NameProperty: tempMem.WriteValueS64(pcc.addName2(prop.Value.StringValue)); // Heff: Modified to handle name references. //var index = pcc.addName2(prop.Value.StringValue); //tempMem.WriteValueS32(index); //tempMem.WriteValueS32(prop.Value.NameValue.count); break; case SaltPropertyReader.Type.ByteProperty: tempMem.WriteValueS64(pcc.addName2(prop.Value.StringValue)); tempMem.WriteValueS32(pcc.addName2(prop.Value.String2)); byte[] footer = new byte[4]; Buffer.BlockCopy(prop.raw, prop.raw.Length - 4, footer, 0, 4); tempMem.WriteBytes(footer); break; case SaltPropertyReader.Type.BoolProperty: tempMem.WriteValueBoolean(prop.Value.Boolereno); break; case SaltPropertyReader.Type.StructProperty: tempMem.WriteValueS64(pcc.addName2(prop.Value.StringValue)); for (int k = 0; k < prop.Size; k++) tempMem.WriteByte((byte)prop.Value.Array[k].IntValue); break; default: throw new NotImplementedException("Property type: " + prop.TypeVal + ", not yet implemented. TELL ME ABOUT THIS!"); } } buff = tempMem.ToArray(); } properties = new Dictionary<string, SaltPropertyReader.Property>(); List<SaltPropertyReader.Property> tempProperties = SaltPropertyReader.ReadProp(pcc, buff, headerData.Length); for (int i = 0; i < tempProperties.Count; i++) { SaltPropertyReader.Property property = tempProperties[i]; if (property.Name == "UnpackMin") UnpackNum++; if (!properties.ContainsKey(property.Name)) properties.Add(property.Name, property); switch (property.Name) { case "Format": texFormat = Textures.Methods.ParseFormat(pcc.Names[property.Value.IntValue].Substring(3)); break; case "TextureFileCacheName": arcName = property.Value.NameValue.Name; break; case "LODGroup": LODGroup = property.Value.NameValue.Name; break; case "None": dataOffset = (uint)(property.offsetval + property.Size); break; } } // if "None" property isn't found throws an exception if (dataOffset == 0) throw new Exception("\"None\" property not found"); }