Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- namespace Hex2obj_Extension1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- string path;
- List<int> faceSectionOffsets = new List<int>();
- List<int> faceIndexCounts = new List<int>();
- string H2oFileName;
- string H2oPath;
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- button2.Enabled = true;
- path = ofd.FileName;
- textBox2.Text = ("40");
- textBox3.Text = ("32");
- //grab the entire chunk payload
- BinaryReader br = new BinaryReader(File.OpenRead(path));
- byte[] chunkPayload = br.ReadBytes((int)br.BaseStream.Length);
- //find the offsets of face sections
- byte[] pattern = new byte[] { 00, 00, 01, 00, 02, 00 };
- faceSectionOffsets = SearchBytePattern(pattern, chunkPayload);
- //calculate sizes from offset differences
- for (int i = 0; i < faceSectionOffsets.Count - 1; i++)
- faceIndexCounts.Add((faceSectionOffsets[i + 1] - faceSectionOffsets[i]) / 2);
- //assume EOF (which is the current position of the base stream) as the end of the last face section
- faceIndexCounts.Add(((int)br.BaseStream.Position - faceSectionOffsets.Last())/2);
- br.Dispose();
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- string H2o;
- string VB;
- textBox1.Clear();
- for (int faceSectionIndex = 0; faceSectionIndex < faceSectionOffsets.Count; faceSectionIndex++)
- {
- var faceSectionOffset = faceSectionOffsets[faceSectionIndex];
- var faceIndexCount = faceIndexCounts[faceSectionIndex];
- if (checkBox1.Checked) ; //semicolon makes the next line always execute
- {
- VB = ("VB1");
- }
- textBox1.Text += faceSectionIndex + " mesh offset " + "0x" + faceSectionOffset.ToString("X") + " Face indices " + faceIndexCount +
- Environment.NewLine + "Vertex Block Size " + textBox2.Text + " UV Position " + textBox3.Text + Environment.NewLine;
- H2oFileName = "Object" + faceSectionIndex + ".h2o";
- H2oPath = (Path.GetFullPath(path));
- H2o = (path+H2oFileName);
- StreamWriter sw = new StreamWriter(File.Create(H2o));
- sw.Write(("0x" + faceSectionOffset.ToString("X") + " " + faceIndexCount) + Environment.NewLine + VB + Environment.NewLine + textBox2.Text + " " + textBox3.Text);
- sw.Dispose();
- }
- }
- static private List<int> SearchBytePattern(byte[] pattern, byte[] bytes)
- {
- List<int> positions = new List<int>();
- int patternLength = pattern.Length;
- int totalLength = bytes.Length;
- byte firstMatchByte = pattern[0];
- for (int i = 0; i < totalLength; i++)
- {
- if (firstMatchByte == bytes[i] && totalLength - i >= patternLength)
- {
- byte[] match = new byte[patternLength];
- Array.Copy(bytes, i, match, 0, patternLength);
- if (match.SequenceEqual<byte>(pattern))
- {
- positions.Add(i);
- i += patternLength - 1;
- }
- }
- }
- return positions;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void checkBox1_CheckedChanged(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement