public void replaceImage(string strImgSize, ImageFile im, string archiveDir)
{
ImageSize imgSize = ImageSize.stringToSize(strImgSize);
if (!privateimgList.Exists(img => img.imgSize == imgSize))
throw new FileNotFoundException("Image with resolution " + imgSize + " isn't found");
int imageIdx = privateimgList.FindIndex(img => img.imgSize == imgSize);
ImageInfo imgInfo = privateimgList[imageIdx];
ImageFile imgFile = im;
if (imgFile.imgSize.height != imgInfo.imgSize.height || imgFile.imgSize.width != imgInfo.imgSize.width)
throw new FormatException("Incorrect input texture dimensions. Expected: " + imgInfo.imgSize.ToString());
if (!Methods.CheckTextureFormat(texFormat, imgFile.format))
throw new FormatException("Different image format, original is " + texFormat + ", new is " + imgFile.subtype());
byte[] imgBuffer;
// if the image is empty then recover the archive compression from the image list
if (imgInfo.storageType == storage.empty)
{
imgInfo.storageType = privateimgList.Find(img => img.storageType != storage.empty && img.storageType != storage.pccSto).storageType;
imgInfo.uncSize = imgFile.resize().Length;
imgInfo.cprSize = imgFile.resize().Length;
}
// overwrite previous choices for specific cases
if (properties.ContainsKey("NeverStream") && properties["NeverStream"].Value.IntValue == 1)
imgInfo.storageType = storage.pccSto;
switch (imgInfo.storageType)
{
case storage.arcCpr:
case storage.arcUnc:
string archivePath = FullArcPath;
if (String.IsNullOrEmpty(archivePath))
archivePath = GetTexArchive(archiveDir);
if (!File.Exists(archivePath))
throw new FileNotFoundException("Texture archive not found in " + archivePath);
if (getFileFormat() == ".tga")
imgBuffer = imgFile.resize(); // shrink image to essential data
else
imgBuffer = imgFile.imgData;
if (imgBuffer.Length != imgInfo.uncSize)
throw new FormatException("image sizes do not match, original is " + imgInfo.uncSize + ", new is " + imgBuffer.Length);
if (arcName.Length <= CustCache.Length || arcName.Substring(0, CustCache.Length) != CustCache) // Check whether existing texture is in a custom cache
{
ChooseNewCache(archiveDir, imgBuffer.Length);
archivePath = FullArcPath;
}
else
{
FileInfo arc = new FileInfo(archivePath);
if (arc.Length + imgBuffer.Length >= 0x80000000)
{
ChooseNewCache(archiveDir, imgBuffer.Length);
archivePath = FullArcPath;
}
}
using (FileStream archiveStream = new FileStream(archivePath, FileMode.Append, FileAccess.Write))
{
int newOffset = (int)archiveStream.Position;
//archiveStream.Position = imgInfo.offset;
if (imgInfo.storageType == storage.arcCpr)
{
byte[] tempBuff;
SaltLZOHelper lzohelper = new SaltLZOHelper();
tempBuff = lzohelper.CompressTex(imgBuffer);
imgBuffer = new byte[tempBuff.Length];
Buffer.BlockCopy(tempBuff, 0, imgBuffer, 0, tempBuff.Length);
imgInfo.cprSize = imgBuffer.Length;
}
//else
archiveStream.Write(imgBuffer, 0, imgBuffer.Length);
imgInfo.offset = newOffset;
}
break;
case storage.pccSto:
//imgBuffer = imgFile.imgData; // copy image data as-is
imgBuffer = imgFile.resize();
using (MemoryStream dataStream = new MemoryStream())
{
dataStream.WriteBytes(imageData);
if (imgBuffer.Length <= imgInfo.uncSize && imgInfo.offset > 0)
dataStream.Seek(imgInfo.offset, SeekOrigin.Begin);
else
imgInfo.offset = (int)dataStream.Position;
dataStream.WriteBytes(imgBuffer);
imgInfo.cprSize = imgBuffer.Length;
imgInfo.uncSize = imgBuffer.Length;
imageData = dataStream.ToArray();
}
break;
}
privateimgList[imageIdx] = imgInfo;
}