Guest User

Untitled

a guest
Jan 23rd, 2014
161
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_Extension6
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21.  
  22. string path;
  23.  
  24. List<int> faceSectionOffsets = new List<int>();
  25. List<int> faceIndexCounts = new List<int>();
  26.  
  27. string H2oFileName;
  28. string H2oPath;
  29. string faceSectionIndex;
  30.  
  31.  
  32.  
  33. private void button1_Click(object sender, EventArgs e)
  34. {
  35. OpenFileDialog ofd = new OpenFileDialog();
  36. if (ofd.ShowDialog() == DialogResult.OK)
  37. {
  38. button2.Enabled = true;
  39. path = ofd.FileName;
  40.  
  41. textBox2.Text = ("40");
  42. textBox3.Text = ("32");
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. //grab the entire chunk payload
  50. BinaryReader br = new BinaryReader(File.OpenRead(path));
  51. byte[] chunkPayload = br.ReadBytes((int)br.BaseStream.Length);
  52.  
  53. //find the offsets of face sections
  54. byte[] pattern = new byte[] { 00, 00, 01, 00, 02, 00 };
  55. faceSectionOffsets = SearchBytePattern(pattern, chunkPayload);
  56.  
  57. //calculate sizes from offset differences
  58. for (int i = 0; i < faceSectionOffsets.Count - 1; i++)
  59. faceIndexCounts.Add((faceSectionOffsets[i + 1] - faceSectionOffsets[i]) / 2);
  60.  
  61. //assume EOF (which is the current position of the base stream) as the end of the last face section
  62. faceIndexCounts.Add(((int)br.BaseStream.Position - faceSectionOffsets.Last()) / 2);
  63.  
  64.  
  65. br.Dispose();
  66. }
  67. }
  68.  
  69. private void button2_Click(object sender, EventArgs e)
  70. {
  71. string H2o;
  72. string VB = "00";
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. for (int faceSectionIndex = 0; faceSectionIndex < faceSectionOffsets.Count; faceSectionIndex++)
  81. {
  82. var faceSectionOffset = faceSectionOffsets[faceSectionIndex];
  83. var faceIndexCount = faceIndexCounts[faceSectionIndex];
  84.  
  85.  
  86. if (checkBox1.Checked) //semicolon makes the next line always execute
  87. {
  88. VB = ("VB1");
  89. }
  90.  
  91. ListViewItem lvi = new ListViewItem(faceSectionIndex.ToString());
  92. lvi.SubItems.Add("bla");
  93. lvi.SubItems.Add(faceSectionOffset.ToString("X"));
  94. lvi.SubItems.Add(faceIndexCount.ToString());
  95. lvi.SubItems.Add(textBox2.Text); //VB Size
  96. lvi.SubItems.Add(textBox3.Text); //UV Position
  97. lvi.SubItems.Add("aaaa");
  98. lvi.SubItems.Add("bbbb");
  99.  
  100.  
  101. listView1.Items.Add(lvi);
  102.  
  103.  
  104. textBox1.Text += faceSectionIndex + " mesh offset " + "0x" + faceSectionOffset.ToString("X") + " Face indices " + faceIndexCount +
  105. Environment.NewLine + "Vertex Block Size " + textBox2.Text + " UV Position " + textBox3.Text + Environment.NewLine + Environment.NewLine;
  106.  
  107. H2oFileName = "Object" + faceSectionIndex + ".h2o";
  108. H2oPath = (Path.GetFullPath(path));
  109. H2o = (path + H2oFileName);
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. if (checkBox2.Checked)
  119. {
  120. StreamWriter sw = new StreamWriter(File.Create(H2o));
  121. sw.Write(("0x" + faceSectionOffset.ToString("X") + " " + faceIndexCount) + Environment.NewLine + VB + Environment.NewLine + textBox2.Text + " " + textBox3.Text);
  122. sw.Dispose();
  123. }
  124. }
  125. }
  126.  
  127.  
  128.  
  129.  
  130. static private List<int> SearchBytePattern(byte[] pattern, byte[] bytes)
  131. {
  132. List<int> positions = new List<int>();
  133. int patternLength = pattern.Length;
  134. int totalLength = bytes.Length;
  135. byte firstMatchByte = pattern[0];
  136. for (int i = 0; i < totalLength; i++)
  137. {
  138. if (firstMatchByte == bytes[i] && totalLength - i >= patternLength)
  139. {
  140. byte[] match = new byte[patternLength];
  141. Array.Copy(bytes, i, match, 0, patternLength);
  142. if (match.SequenceEqual<byte>(pattern))
  143. {
  144. positions.Add(i);
  145. i += patternLength - 1;
  146. }
  147. }
  148. }
  149. return positions;
  150. }
  151.  
  152. private void Form1_Load(object sender, EventArgs e)
  153. {
  154.  
  155. }
  156.  
  157. private void label1_Click(object sender, EventArgs e)
  158. {
  159.  
  160. }
  161.  
  162. private void checkBox1_CheckedChanged(object sender, EventArgs e)
  163. {
  164.  
  165. }
  166.  
  167.  
  168. }
  169. }
RAW Paste Data