EHDSeven

p3dChangeTexture Source

Aug 6th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.18 KB | None | 0 0
  1. /*
  2.     Copyright Seven and stuff
  3.     Open source - Change / edit / whatever don't care...
  4.     Blah Blah Blah computer breaks not my fault. Enjoy
  5. */
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.IO;
  11. using System.Xml.Linq;
  12.  
  13. namespace p3dChangeTexture
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             if (args.Count() != 0)
  20.             {
  21.                 Console.WriteLine("Seven's p3d Batch Texture Changer");
  22.                 Console.WriteLine("---------------------------------\r\n");
  23.                
  24.                 //Get Initial Values
  25.                 XDocument list = XDocument.Load(args[0]);
  26.  
  27.                 var models = list.Descendants("model");
  28.  
  29.                 //Loop through all the models in the XML
  30.                 foreach ( var model in models )
  31.                 {
  32.                     //Get all of the vars
  33.                     var sourceModel = model.Descendants("source-model");
  34.                     var destDirectory = model.Descendants("dest-directory");
  35.                     var textureDirectory = model.Descendants("texture-directory");
  36.                     var originalTexture = model.Descendants("original-texture");
  37.                     var originalRVMat = model.Descendants("original-rvmat");
  38.                     var newRVMat = model.Descendants("new-rvmat");
  39.                     var texturePrefix = model.Descendants("texture-prefix");
  40.                     var modelPostfix = model.Descendants("model-postfix");
  41.  
  42.                     //Get all the Values from the vars
  43.                     string sourceModelVal = sourceModel.First().Value;
  44.                     string destDirectoryVal = destDirectory.First().Value;
  45.                     string textureDirectoryVal = textureDirectory.First().Value;
  46.                     byte[] originalTextureVal = Encoding.UTF8.GetBytes(originalTexture.First().Value);
  47.                     byte[] originalRVMatVal = Encoding.UTF8.GetBytes(originalRVMat.First().Value);
  48.                     byte[] newRVMatVal = Encoding.UTF8.GetBytes(newRVMat.First().Value);
  49.                     byte[] texturePrefixVal = Encoding.UTF8.GetBytes(texturePrefix.First().Value);
  50.                     string modelPostfixVal = modelPostfix.First().Value;
  51.  
  52.                     //Some Debug output
  53.                     Console.WriteLine("Source Model: " + sourceModelVal);
  54.                     Console.WriteLine("Working Directory: " + destDirectoryVal);
  55.                     Console.WriteLine("Texture Directory: " + textureDirectoryVal);
  56.                     Console.WriteLine("Source Texture: " + originalTexture.First().Value);
  57.                     Console.WriteLine("Texture Prefix: " + texturePrefix.First().Value + "\r\n");
  58.  
  59.                     Console.WriteLine("Source Texture Byte Size: " + originalTextureVal.Count());
  60.                     Console.WriteLine("Texture Prefix Byte Size: " + texturePrefixVal.Count() + "\r\n");
  61.                    
  62.                     //Open Source model and store in memory
  63.                     FileStream fStream = new FileStream(sourceModelVal, FileMode.Open, FileAccess.Read);
  64.                     byte[] btFileDump = new byte[fStream.Length];
  65.                     fStream.Read(btFileDump, 0, (int)fStream.Length);
  66.                     fStream.Close();
  67.                     fStream.Dispose();
  68.                
  69.                     //Find all of the images so we can build a list
  70.                     string[] filesTmp = Directory.GetFiles(textureDirectoryVal);
  71.                     string[] files = new string[filesTmp.Count()];
  72.                     string[] textures = new string[filesTmp.Count()];
  73.  
  74.                     //Cleanup some of the strings
  75.                     for (int count = 0; count < filesTmp.Count(); count++)
  76.                     {
  77.                         string file = Path.GetFileName(filesTmp[count]);
  78.                         files[count] = file.Substring(0, file.Length-7);
  79.                         textures[count] = file;
  80.                         count++;
  81.                     }
  82.  
  83.                     //Loop through each model and create the new version
  84.                     for(int j = 0; j < files.Count(); j++) {
  85.                         string modelName = files[j];
  86.                         string modelTexture = textures[j];
  87.                         Console.WriteLine("Generating Model: " + modelName + " with Texture: " + modelTexture);
  88.                         byte[] byteTexture = Encoding.UTF8.GetBytes(modelTexture);
  89.                         byte[] byteRVMat = Encoding.UTF8.GetBytes(modelTexture);
  90.                         List<byte> tmpBytes = new List<byte>();
  91.                         //Run over and change the Texture
  92.                         tmpBytes = WriteNewBytes(tmpBytes, btFileDump, originalTextureVal, texturePrefixVal, byteTexture);
  93.                         //Run over and change the RVMat
  94.                         tmpBytes = WriteNewBytes(tmpBytes, btFileDump, originalRVMatVal, texturePrefixVal, newRVMatVal);
  95.                        
  96.                         //Dump the new byte array to the file and reset.
  97.                         ByteArrayToFile(destDirectoryVal + modelName + modelPostfixVal + ".p3d", tmpBytes.ToArray());
  98.                         tmpBytes.RemoveRange(0,tmpBytes.Count()-1);
  99.                     }
  100.                 }
  101.  
  102.                 Console.WriteLine("Press any key to continue...");
  103.                 Console.ReadKey(true);
  104.             }
  105.             else //No List XML defined
  106.             {
  107.                 Console.WriteLine("No Model List given");
  108.                 Console.WriteLine("Press any key to continue...");
  109.                 Console.ReadKey(true);
  110.             }
  111.         }
  112.  
  113.         //Functionn for excluding the old texture bytes and inserting new bytes
  114.         public static List<byte> WriteNewBytes(List<byte> tmpBytes, byte[] btFileDump, byte[] originalTextureVal, byte[] texturePrefixVal, byte[] byteTexture )
  115.         {
  116.             int offset = 0;
  117.             int oldoffset = 0;
  118.             while (offset != -1 )
  119.             {
  120.                 if ( ByteSearch(btFileDump, originalTextureVal, offset) > 0)
  121.                 {
  122.                     offset = ByteSearch(btFileDump, originalTextureVal, offset);
  123.                     for (int i = oldoffset; i < offset; i++)
  124.                     {
  125.                         tmpBytes.Add(btFileDump[i]);
  126.                     }
  127.                     for (int i = 0; i < texturePrefixVal.Count(); i++)
  128.                     {
  129.                         tmpBytes.Add(texturePrefixVal[i]);
  130.                     }
  131.                     for (int i = 0; i < byteTexture.Count(); i++)
  132.                     {
  133.                         tmpBytes.Add(byteTexture[i]);
  134.                     }
  135.                     oldoffset = offset + originalTextureVal.Count();
  136.                     offset = offset + originalTextureVal.Count();
  137.                 }
  138.                 else
  139.                 {
  140.                     for (int i = oldoffset; i < btFileDump.Count(); i++)
  141.                     {
  142.                         tmpBytes.Add(btFileDump[i]);
  143.                     }
  144.                     offset = -1;
  145.                 }
  146.             }
  147.  
  148.             return tmpBytes;
  149.         }
  150.  
  151.         //Code below is not mine - Abstracts ripped from the internet for speed.
  152.         public static bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
  153.         {
  154.             try
  155.             {
  156.                 // Open file for reading
  157.                 System.IO.FileStream _FileStream =
  158.                    new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
  159.                                             System.IO.FileAccess.Write);
  160.                 // Writes a block of bytes to this stream using data from
  161.                 // a byte array.
  162.                 _FileStream.Write(_ByteArray, 0, _ByteArray.Length);
  163.  
  164.                 // close file stream
  165.                 _FileStream.Close();
  166.  
  167.                 return true;
  168.             }
  169.             catch (Exception _Exception)
  170.             {
  171.                 // Error
  172.                 Console.WriteLine("Exception caught in process: {0}",
  173.                                   _Exception.ToString());
  174.             }
  175.  
  176.             // error occured, return false
  177.             return false;
  178.         }
  179.  
  180.         private static int ByteSearch(byte[] searchIn, byte[] searchBytes, int start = 0)
  181.         {
  182.             int found = -1;
  183.             bool matched = false;
  184.             if (start < 0) { start = 0; }
  185.             //only look at this if we have a populated search array and search bytes with a sensible start
  186.             if (searchIn.Length > 0 && searchBytes.Length > 0 && start <= (searchIn.Length - searchBytes.Length) && searchIn.Length >= searchBytes.Length)
  187.             {
  188.                 //iterate through the array to be searched
  189.                 for (int i = start; i <= searchIn.Length - searchBytes.Length; i++)
  190.                 {
  191.                     //if the start bytes match we will start comparing all other bytes
  192.                     if (searchIn[i] == searchBytes[0])
  193.                     {
  194.                         if (searchIn.Length > 1)
  195.                         {
  196.                             //multiple bytes to be searched we have to compare byte by byte
  197.                             matched = true;
  198.                             for (int y = 1; y <= searchBytes.Length - 1; y++)
  199.                             {
  200.                                 if (searchIn[i + y] != searchBytes[y])
  201.                                 {
  202.                                     matched = false;
  203.                                     break;
  204.                                 }
  205.                             }
  206.                             //everything matched up
  207.                             if (matched)
  208.                             {
  209.                                 found = i;
  210.                                 break;
  211.                             }
  212.  
  213.                         }
  214.                         else
  215.                         {
  216.                             //search byte is only one bit nothing else to do
  217.                             found = i;
  218.                             break; //stop the loop
  219.                         }
  220.  
  221.                     }
  222.                 }
  223.  
  224.             }
  225.             return found;
  226.         }
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment