Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright Seven and stuff
- Open source - Change / edit / whatever don't care...
- Blah Blah Blah computer breaks not my fault. Enjoy
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Xml.Linq;
- namespace p3dChangeTexture
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Count() != 0)
- {
- Console.WriteLine("Seven's p3d Batch Texture Changer");
- Console.WriteLine("---------------------------------\r\n");
- //Get Initial Values
- XDocument list = XDocument.Load(args[0]);
- var models = list.Descendants("model");
- //Loop through all the models in the XML
- foreach ( var model in models )
- {
- //Get all of the vars
- var sourceModel = model.Descendants("source-model");
- var destDirectory = model.Descendants("dest-directory");
- var textureDirectory = model.Descendants("texture-directory");
- var originalTexture = model.Descendants("original-texture");
- var originalRVMat = model.Descendants("original-rvmat");
- var newRVMat = model.Descendants("new-rvmat");
- var texturePrefix = model.Descendants("texture-prefix");
- var modelPostfix = model.Descendants("model-postfix");
- //Get all the Values from the vars
- string sourceModelVal = sourceModel.First().Value;
- string destDirectoryVal = destDirectory.First().Value;
- string textureDirectoryVal = textureDirectory.First().Value;
- byte[] originalTextureVal = Encoding.UTF8.GetBytes(originalTexture.First().Value);
- byte[] originalRVMatVal = Encoding.UTF8.GetBytes(originalRVMat.First().Value);
- byte[] newRVMatVal = Encoding.UTF8.GetBytes(newRVMat.First().Value);
- byte[] texturePrefixVal = Encoding.UTF8.GetBytes(texturePrefix.First().Value);
- string modelPostfixVal = modelPostfix.First().Value;
- //Some Debug output
- Console.WriteLine("Source Model: " + sourceModelVal);
- Console.WriteLine("Working Directory: " + destDirectoryVal);
- Console.WriteLine("Texture Directory: " + textureDirectoryVal);
- Console.WriteLine("Source Texture: " + originalTexture.First().Value);
- Console.WriteLine("Texture Prefix: " + texturePrefix.First().Value + "\r\n");
- Console.WriteLine("Source Texture Byte Size: " + originalTextureVal.Count());
- Console.WriteLine("Texture Prefix Byte Size: " + texturePrefixVal.Count() + "\r\n");
- //Open Source model and store in memory
- FileStream fStream = new FileStream(sourceModelVal, FileMode.Open, FileAccess.Read);
- byte[] btFileDump = new byte[fStream.Length];
- fStream.Read(btFileDump, 0, (int)fStream.Length);
- fStream.Close();
- fStream.Dispose();
- //Find all of the images so we can build a list
- string[] filesTmp = Directory.GetFiles(textureDirectoryVal);
- string[] files = new string[filesTmp.Count()];
- string[] textures = new string[filesTmp.Count()];
- //Cleanup some of the strings
- for (int count = 0; count < filesTmp.Count(); count++)
- {
- string file = Path.GetFileName(filesTmp[count]);
- files[count] = file.Substring(0, file.Length-7);
- textures[count] = file;
- count++;
- }
- //Loop through each model and create the new version
- for(int j = 0; j < files.Count(); j++) {
- string modelName = files[j];
- string modelTexture = textures[j];
- Console.WriteLine("Generating Model: " + modelName + " with Texture: " + modelTexture);
- byte[] byteTexture = Encoding.UTF8.GetBytes(modelTexture);
- byte[] byteRVMat = Encoding.UTF8.GetBytes(modelTexture);
- List<byte> tmpBytes = new List<byte>();
- //Run over and change the Texture
- tmpBytes = WriteNewBytes(tmpBytes, btFileDump, originalTextureVal, texturePrefixVal, byteTexture);
- //Run over and change the RVMat
- tmpBytes = WriteNewBytes(tmpBytes, btFileDump, originalRVMatVal, texturePrefixVal, newRVMatVal);
- //Dump the new byte array to the file and reset.
- ByteArrayToFile(destDirectoryVal + modelName + modelPostfixVal + ".p3d", tmpBytes.ToArray());
- tmpBytes.RemoveRange(0,tmpBytes.Count()-1);
- }
- }
- Console.WriteLine("Press any key to continue...");
- Console.ReadKey(true);
- }
- else //No List XML defined
- {
- Console.WriteLine("No Model List given");
- Console.WriteLine("Press any key to continue...");
- Console.ReadKey(true);
- }
- }
- //Functionn for excluding the old texture bytes and inserting new bytes
- public static List<byte> WriteNewBytes(List<byte> tmpBytes, byte[] btFileDump, byte[] originalTextureVal, byte[] texturePrefixVal, byte[] byteTexture )
- {
- int offset = 0;
- int oldoffset = 0;
- while (offset != -1 )
- {
- if ( ByteSearch(btFileDump, originalTextureVal, offset) > 0)
- {
- offset = ByteSearch(btFileDump, originalTextureVal, offset);
- for (int i = oldoffset; i < offset; i++)
- {
- tmpBytes.Add(btFileDump[i]);
- }
- for (int i = 0; i < texturePrefixVal.Count(); i++)
- {
- tmpBytes.Add(texturePrefixVal[i]);
- }
- for (int i = 0; i < byteTexture.Count(); i++)
- {
- tmpBytes.Add(byteTexture[i]);
- }
- oldoffset = offset + originalTextureVal.Count();
- offset = offset + originalTextureVal.Count();
- }
- else
- {
- for (int i = oldoffset; i < btFileDump.Count(); i++)
- {
- tmpBytes.Add(btFileDump[i]);
- }
- offset = -1;
- }
- }
- return tmpBytes;
- }
- //Code below is not mine - Abstracts ripped from the internet for speed.
- public static bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
- {
- try
- {
- // Open file for reading
- System.IO.FileStream _FileStream =
- new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
- System.IO.FileAccess.Write);
- // Writes a block of bytes to this stream using data from
- // a byte array.
- _FileStream.Write(_ByteArray, 0, _ByteArray.Length);
- // close file stream
- _FileStream.Close();
- return true;
- }
- catch (Exception _Exception)
- {
- // Error
- Console.WriteLine("Exception caught in process: {0}",
- _Exception.ToString());
- }
- // error occured, return false
- return false;
- }
- private static int ByteSearch(byte[] searchIn, byte[] searchBytes, int start = 0)
- {
- int found = -1;
- bool matched = false;
- if (start < 0) { start = 0; }
- //only look at this if we have a populated search array and search bytes with a sensible start
- if (searchIn.Length > 0 && searchBytes.Length > 0 && start <= (searchIn.Length - searchBytes.Length) && searchIn.Length >= searchBytes.Length)
- {
- //iterate through the array to be searched
- for (int i = start; i <= searchIn.Length - searchBytes.Length; i++)
- {
- //if the start bytes match we will start comparing all other bytes
- if (searchIn[i] == searchBytes[0])
- {
- if (searchIn.Length > 1)
- {
- //multiple bytes to be searched we have to compare byte by byte
- matched = true;
- for (int y = 1; y <= searchBytes.Length - 1; y++)
- {
- if (searchIn[i + y] != searchBytes[y])
- {
- matched = false;
- break;
- }
- }
- //everything matched up
- if (matched)
- {
- found = i;
- break;
- }
- }
- else
- {
- //search byte is only one bit nothing else to do
- found = i;
- break; //stop the loop
- }
- }
- }
- }
- return found;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment