Guest User

form1.cs

a guest
Jan 19th, 2014
67
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace Hex2obj_Extension1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         string path;
  22.  
  23.         List<int> faceSectionOffsets = new List<int>();
  24.         List<int> faceIndexCounts = new List<int>();
  25.  
  26.         string H2oFileName;
  27.         string H2oPath;
  28.        
  29.  
  30.         private void button1_Click(object sender, EventArgs e)
  31.         {
  32.             OpenFileDialog ofd = new OpenFileDialog();
  33.             if (ofd.ShowDialog() == DialogResult.OK)
  34.             {
  35.                 button2.Enabled = true;
  36.                 path = ofd.FileName;
  37.  
  38.                 textBox2.Text = ("40");
  39.                 textBox3.Text = ("32");
  40.  
  41.                 //grab the entire chunk payload
  42.                 BinaryReader br = new BinaryReader(File.OpenRead(path));
  43.                 byte[] chunkPayload = br.ReadBytes((int)br.BaseStream.Length);
  44.  
  45.                 //find the offsets of face sections
  46.                 byte[] pattern = new byte[] { 00, 00, 01, 00, 02, 00 };
  47.                 faceSectionOffsets = SearchBytePattern(pattern, chunkPayload);
  48.  
  49.                 //calculate sizes from offset differences
  50.                 for (int i = 0; i < faceSectionOffsets.Count - 1; i++)
  51.                     faceIndexCounts.Add((faceSectionOffsets[i + 1] - faceSectionOffsets[i]) / 2);
  52.  
  53.                 //assume EOF (which is the current position of the base stream) as the end of the last face section
  54.                 faceIndexCounts.Add(((int)br.BaseStream.Position - faceSectionOffsets.Last())/2);
  55.  
  56.                 br.Dispose();
  57.             }
  58.         }
  59.  
  60.         private void button2_Click(object sender, EventArgs e)
  61.         {  
  62.             string H2o;
  63.             string VB;
  64.  
  65.             textBox1.Clear();
  66.  
  67.             for (int faceSectionIndex = 0; faceSectionIndex < faceSectionOffsets.Count; faceSectionIndex++)
  68.             {
  69.                 var faceSectionOffset = faceSectionOffsets[faceSectionIndex];
  70.                 var faceIndexCount = faceIndexCounts[faceSectionIndex];
  71.  
  72.  
  73.                 if (checkBox1.Checked) ; //semicolon makes the next line always execute
  74.                 {
  75.                     VB = ("VB1");
  76.                 }
  77.  
  78.                 textBox1.Text += faceSectionIndex + " mesh offset " + "0x" + faceSectionOffset.ToString("X") + "  Face indices  " + faceIndexCount +
  79.                     Environment.NewLine + "Vertex Block Size " + textBox2.Text + " UV Position " + textBox3.Text + Environment.NewLine;
  80.  
  81.                 H2oFileName = "Object" + faceSectionIndex + ".h2o";
  82.                 H2oPath = (Path.GetFullPath(path));
  83.                 H2o = (path+H2oFileName);
  84.                
  85.                 StreamWriter sw = new StreamWriter(File.Create(H2o));
  86.                 sw.Write(("0x" + faceSectionOffset.ToString("X") + " " + faceIndexCount) + Environment.NewLine + VB + Environment.NewLine + textBox2.Text + " " + textBox3.Text);
  87.                 sw.Dispose();
  88.             }
  89.         }
  90.  
  91.  
  92.  
  93.  
  94.         static private List<int> SearchBytePattern(byte[] pattern, byte[] bytes)
  95.         {
  96.             List<int> positions = new List<int>();
  97.             int patternLength = pattern.Length;
  98.             int totalLength = bytes.Length;
  99.             byte firstMatchByte = pattern[0];
  100.             for (int i = 0; i < totalLength; i++)
  101.             {
  102.                 if (firstMatchByte == bytes[i] && totalLength - i >= patternLength)
  103.                 {
  104.                     byte[] match = new byte[patternLength];
  105.                     Array.Copy(bytes, i, match, 0, patternLength);
  106.                     if (match.SequenceEqual<byte>(pattern))
  107.                     {
  108.                         positions.Add(i);
  109.                         i += patternLength - 1;
  110.                     }
  111.                 }
  112.             }
  113.             return positions;
  114.         }
  115.  
  116.         private void Form1_Load(object sender, EventArgs e)
  117.         {
  118.  
  119.         }
  120.  
  121.         private void label1_Click(object sender, EventArgs e)
  122.         {
  123.  
  124.         }
  125.  
  126.         private void checkBox1_CheckedChanged(object sender, EventArgs e)
  127.         {
  128.  
  129.         }
  130.     }
  131. }
RAW Paste Data