#pragma once
#include "dblreader.h"
#include "chunkdt.h"
#include "wtexture_viewer.h"
#include "wsomed.h"

/*char** filelist;
tTextureList_s* pTexList;
dbl_chunk_header_s* pChunkHeader;*/

namespace DBLFucker {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for main
	/// </summary>
	public ref class main : public System::Windows::Forms::Form
	{
	public: 
		Delegate^ BuildAChunk_Del;
		Thread^ pThread;
		char** filelist;
		tTextureList_s* pTexList;
		dbl_chunk_header_s* pChunkHeader;
		main(void)
		{
			InitializeComponent();
			this->Icon = Drawing::Icon::FromHandle( (IntPtr)LoadIconA(0, (LPCSTR)IDI_APPLICATION) );
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~main()
		{
			if (components)
			{
				delete components;
			}
		}
	public: delegate void BuildAChunkDel( void );
	private: System::Windows::Forms::Button^  button16;
	private: System::Windows::Forms::RichTextBox^  richTextBox1;
	private: System::Windows::Forms::Button^  button1;



	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::Button^  button4;
	private: System::Windows::Forms::FolderBrowserDialog^  folderBrowserDialog1;
	private: System::Windows::Forms::TabControl^  tabControl1;
	private: System::Windows::Forms::TabPage^  tabPage1;
	private: System::Windows::Forms::TabPage^  tabPage2;

	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Button^  button6;
	private: System::Windows::Forms::TextBox^  textBox3;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::Button^  button7;
	private: System::Windows::Forms::Button^  button8;
	private: System::Windows::Forms::TextBox^  textBox4;
	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;
	private: System::Windows::Forms::ComboBox^  comboBox1;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::Label^  label6;
	private: System::Windows::Forms::TextBox^  textBox5;
	private: System::Windows::Forms::TabPage^  tabPage3;
	private: System::Windows::Forms::Label^  label7;
	private: System::Windows::Forms::Label^  label8;
	private: System::Windows::Forms::Button^  button5;
	private: System::Windows::Forms::TextBox^  textBox6;
	private: System::Windows::Forms::RichTextBox^  richTextBox2;
	private: System::Windows::Forms::Label^  label9;
	private: System::Windows::Forms::Button^  button9;
	private: System::Windows::Forms::Button^  button10;
	private: System::Windows::Forms::TabPage^  tabPage4;
	private: System::Windows::Forms::Label^  label10;
	private: System::Windows::Forms::ComboBox^  comboBox2;
	private: System::Windows::Forms::Label^  label11;
	private: System::Windows::Forms::ComboBox^  comboBox3;
	private: System::Windows::Forms::Label^  label12;
	private: System::Windows::Forms::ComboBox^  comboBox4;
	private: System::Windows::Forms::TextBox^  textBox7;
	private: System::Windows::Forms::Label^  label13;
	private: System::Windows::Forms::TextBox^  textBox8;
	private: System::Windows::Forms::Label^  label14;
	private: System::Windows::Forms::Button^  button11;
	private: System::Windows::Forms::Button^  button12;
	private: System::Windows::Forms::ListBox^  listBox1;
	private: System::Windows::Forms::Label^  label15;


	private: System::Windows::Forms::Button^  button13;
	private: System::Windows::Forms::Label^  label17;
	private: System::Windows::Forms::ComboBox^  comboBox5;
	private: System::Windows::Forms::Label^  label18;
	private: System::Windows::Forms::ComboBox^  comboBox6;
	private: System::Windows::Forms::Label^  label19;
	private: System::Windows::Forms::ComboBox^  comboBox7;
	private: System::Windows::Forms::Button^  button15;
	private: System::Windows::Forms::Button^  button14;
	private: System::Windows::Forms::TextBox^  textBox9;
	private: System::Windows::Forms::Label^  label20;
	private: System::Windows::Forms::ProgressBar^  progressBar1;
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->button4 = (gcnew System::Windows::Forms::Button());
			this->folderBrowserDialog1 = (gcnew System::Windows::Forms::FolderBrowserDialog());
			this->tabControl1 = (gcnew System::Windows::Forms::TabControl());
			this->tabPage1 = (gcnew System::Windows::Forms::TabPage());
			this->tabPage2 = (gcnew System::Windows::Forms::TabPage());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->textBox5 = (gcnew System::Windows::Forms::TextBox());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->button6 = (gcnew System::Windows::Forms::Button());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->button7 = (gcnew System::Windows::Forms::Button());
			this->button8 = (gcnew System::Windows::Forms::Button());
			this->textBox4 = (gcnew System::Windows::Forms::TextBox());
			this->tabPage3 = (gcnew System::Windows::Forms::TabPage());
			this->button15 = (gcnew System::Windows::Forms::Button());
			this->button14 = (gcnew System::Windows::Forms::Button());
			this->textBox9 = (gcnew System::Windows::Forms::TextBox());
			this->label20 = (gcnew System::Windows::Forms::Label());
			this->label17 = (gcnew System::Windows::Forms::Label());
			this->comboBox5 = (gcnew System::Windows::Forms::ComboBox());
			this->label18 = (gcnew System::Windows::Forms::Label());
			this->comboBox6 = (gcnew System::Windows::Forms::ComboBox());
			this->label19 = (gcnew System::Windows::Forms::Label());
			this->comboBox7 = (gcnew System::Windows::Forms::ComboBox());
			this->button10 = (gcnew System::Windows::Forms::Button());
			this->richTextBox2 = (gcnew System::Windows::Forms::RichTextBox());
			this->label9 = (gcnew System::Windows::Forms::Label());
			this->button9 = (gcnew System::Windows::Forms::Button());
			this->label8 = (gcnew System::Windows::Forms::Label());
			this->button5 = (gcnew System::Windows::Forms::Button());
			this->textBox6 = (gcnew System::Windows::Forms::TextBox());
			this->tabPage4 = (gcnew System::Windows::Forms::TabPage());
			this->button16 = (gcnew System::Windows::Forms::Button());
			this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
			this->button13 = (gcnew System::Windows::Forms::Button());
			this->button12 = (gcnew System::Windows::Forms::Button());
			this->listBox1 = (gcnew System::Windows::Forms::ListBox());
			this->label15 = (gcnew System::Windows::Forms::Label());
			this->button11 = (gcnew System::Windows::Forms::Button());
			this->textBox8 = (gcnew System::Windows::Forms::TextBox());
			this->label14 = (gcnew System::Windows::Forms::Label());
			this->textBox7 = (gcnew System::Windows::Forms::TextBox());
			this->label13 = (gcnew System::Windows::Forms::Label());
			this->label12 = (gcnew System::Windows::Forms::Label());
			this->comboBox4 = (gcnew System::Windows::Forms::ComboBox());
			this->label11 = (gcnew System::Windows::Forms::Label());
			this->comboBox3 = (gcnew System::Windows::Forms::ComboBox());
			this->label10 = (gcnew System::Windows::Forms::Label());
			this->comboBox2 = (gcnew System::Windows::Forms::ComboBox());
			this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
			this->label7 = (gcnew System::Windows::Forms::Label());
			this->tabControl1->SuspendLayout();
			this->tabPage1->SuspendLayout();
			this->tabPage2->SuspendLayout();
			this->tabPage3->SuspendLayout();
			this->tabPage4->SuspendLayout();
			this->SuspendLayout();
			// 
			// richTextBox1
			// 
			this->richTextBox1->BackColor = System::Drawing::SystemColors::Control;
			this->richTextBox1->Font = (gcnew System::Drawing::Font(L"Times New Roman", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->richTextBox1->Location = System::Drawing::Point(0, 299);
			this->richTextBox1->Name = L"richTextBox1";
			this->richTextBox1->ReadOnly = true;
			this->richTextBox1->ScrollBars = System::Windows::Forms::RichTextBoxScrollBars::ForcedVertical;
			this->richTextBox1->Size = System::Drawing::Size(624, 146);
			this->richTextBox1->TabIndex = 0;
			this->richTextBox1->Text = L"";
			// 
			// button1
			// 
			this->button1->Enabled = false;
			this->button1->Location = System::Drawing::Point(533, 73);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 2;
			this->button1->Text = L"Extract...";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &main::button1_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(531, 10);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(75, 23);
			this->button2->TabIndex = 3;
			this->button2->Text = L"Browse...";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &main::button2_Click);
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(111, 12);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(414, 20);
			this->textBox1->TabIndex = 4;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(3, 15);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(102, 13);
			this->label1->TabIndex = 5;
			this->label1->Text = L"Path to the DBL file:";
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(452, 73);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(75, 23);
			this->button3->TabIndex = 6;
			this->button3->Text = L"Load DBL...";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &main::button3_Click);
			// 
			// openFileDialog1
			// 
			this->openFileDialog1->Filter = L"DBL game archives|*.dbl";
			this->openFileDialog1->Title = L"Pick a DBL file to load";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(3, 41);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(71, 13);
			this->label2->TabIndex = 9;
			this->label2->Text = L"Output folder:";
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(111, 38);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(414, 20);
			this->textBox2->TabIndex = 8;
			this->textBox2->Text = L"data";
			// 
			// button4
			// 
			this->button4->Location = System::Drawing::Point(531, 36);
			this->button4->Name = L"button4";
			this->button4->Size = System::Drawing::Size(75, 23);
			this->button4->TabIndex = 7;
			this->button4->Text = L"Browse...";
			this->button4->UseVisualStyleBackColor = true;
			this->button4->Click += gcnew System::EventHandler(this, &main::button4_Click);
			// 
			// folderBrowserDialog1
			// 
			this->folderBrowserDialog1->Description = L"Select the output folder to put the extracted chunks into";
			this->folderBrowserDialog1->RootFolder = System::Environment::SpecialFolder::MyComputer;
			this->folderBrowserDialog1->SelectedPath = L".\\";
			// 
			// tabControl1
			// 
			this->tabControl1->Controls->Add(this->tabPage1);
			this->tabControl1->Controls->Add(this->tabPage2);
			this->tabControl1->Controls->Add(this->tabPage3);
			this->tabControl1->Controls->Add(this->tabPage4);
			this->tabControl1->Location = System::Drawing::Point(0, 0);
			this->tabControl1->Name = L"tabControl1";
			this->tabControl1->SelectedIndex = 0;
			this->tabControl1->Size = System::Drawing::Size(627, 278);
			this->tabControl1->TabIndex = 10;
			// 
			// tabPage1
			// 
			this->tabPage1->Controls->Add(this->button3);
			this->tabPage1->Controls->Add(this->label2);
			this->tabPage1->Controls->Add(this->button1);
			this->tabPage1->Controls->Add(this->textBox2);
			this->tabPage1->Controls->Add(this->label1);
			this->tabPage1->Controls->Add(this->button4);
			this->tabPage1->Controls->Add(this->button2);
			this->tabPage1->Controls->Add(this->textBox1);
			this->tabPage1->Location = System::Drawing::Point(4, 22);
			this->tabPage1->Name = L"tabPage1";
			this->tabPage1->Padding = System::Windows::Forms::Padding(3);
			this->tabPage1->Size = System::Drawing::Size(619, 252);
			this->tabPage1->TabIndex = 0;
			this->tabPage1->Text = L"Extract";
			this->tabPage1->UseVisualStyleBackColor = true;
			// 
			// tabPage2
			// 
			this->tabPage2->Controls->Add(this->label6);
			this->tabPage2->Controls->Add(this->textBox5);
			this->tabPage2->Controls->Add(this->label5);
			this->tabPage2->Controls->Add(this->comboBox1);
			this->tabPage2->Controls->Add(this->label3);
			this->tabPage2->Controls->Add(this->button6);
			this->tabPage2->Controls->Add(this->textBox3);
			this->tabPage2->Controls->Add(this->label4);
			this->tabPage2->Controls->Add(this->button7);
			this->tabPage2->Controls->Add(this->button8);
			this->tabPage2->Controls->Add(this->textBox4);
			this->tabPage2->Location = System::Drawing::Point(4, 22);
			this->tabPage2->Name = L"tabPage2";
			this->tabPage2->Padding = System::Windows::Forms::Padding(3);
			this->tabPage2->Size = System::Drawing::Size(619, 252);
			this->tabPage2->TabIndex = 1;
			this->tabPage2->Text = L"Create";
			this->tabPage2->UseVisualStyleBackColor = true;
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Location = System::Drawing::Point(270, 72);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(121, 13);
			this->label6->TabIndex = 20;
			this->label6->Text = L"Max. number of chunks:";
			// 
			// textBox5
			// 
			this->textBox5->Location = System::Drawing::Point(397, 70);
			this->textBox5->Name = L"textBox5";
			this->textBox5->Size = System::Drawing::Size(103, 20);
			this->textBox5->TabIndex = 19;
			this->textBox5->Text = L"0";
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(5, 72);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(45, 13);
			this->label5->TabIndex = 18;
			this->label5->Text = L"DBL ID:";
			// 
			// comboBox1
			// 
			this->comboBox1->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->comboBox1->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->Items->AddRange(gcnew cli::array< System::Object^  >(5) {L"DB", L"DX", L"WIN32", L"XBOX", L"PS2"});
			this->comboBox1->Location = System::Drawing::Point(135, 69);
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(121, 21);
			this->comboBox1->TabIndex = 18;
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(5, 46);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(58, 13);
			this->label3->TabIndex = 17;
			this->label3->Text = L"Output file:";
			// 
			// button6
			// 
			this->button6->Location = System::Drawing::Point(509, 68);
			this->button6->Name = L"button6";
			this->button6->Size = System::Drawing::Size(99, 23);
			this->button6->TabIndex = 10;
			this->button6->Text = L"Create the DBL...";
			this->button6->UseVisualStyleBackColor = true;
			this->button6->Click += gcnew System::EventHandler(this, &main::button6_Click);
			// 
			// textBox3
			// 
			this->textBox3->Location = System::Drawing::Point(135, 43);
			this->textBox3->Name = L"textBox3";
			this->textBox3->Size = System::Drawing::Size(392, 20);
			this->textBox3->TabIndex = 16;
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(5, 20);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(124, 13);
			this->label4->TabIndex = 13;
			this->label4->Text = L"Path to the data chunks:";
			// 
			// button7
			// 
			this->button7->Location = System::Drawing::Point(533, 41);
			this->button7->Name = L"button7";
			this->button7->Size = System::Drawing::Size(75, 23);
			this->button7->TabIndex = 15;
			this->button7->Text = L"Browse...";
			this->button7->UseVisualStyleBackColor = true;
			this->button7->Click += gcnew System::EventHandler(this, &main::button7_Click);
			// 
			// button8
			// 
			this->button8->Location = System::Drawing::Point(533, 15);
			this->button8->Name = L"button8";
			this->button8->Size = System::Drawing::Size(75, 23);
			this->button8->TabIndex = 11;
			this->button8->Text = L"Browse...";
			this->button8->UseVisualStyleBackColor = true;
			this->button8->Click += gcnew System::EventHandler(this, &main::button8_Click);
			// 
			// textBox4
			// 
			this->textBox4->Location = System::Drawing::Point(135, 17);
			this->textBox4->Name = L"textBox4";
			this->textBox4->Size = System::Drawing::Size(392, 20);
			this->textBox4->TabIndex = 12;
			this->textBox4->Text = L"data";
			// 
			// tabPage3
			// 
			this->tabPage3->Controls->Add(this->button15);
			this->tabPage3->Controls->Add(this->button14);
			this->tabPage3->Controls->Add(this->textBox9);
			this->tabPage3->Controls->Add(this->label20);
			this->tabPage3->Controls->Add(this->label17);
			this->tabPage3->Controls->Add(this->comboBox5);
			this->tabPage3->Controls->Add(this->label18);
			this->tabPage3->Controls->Add(this->comboBox6);
			this->tabPage3->Controls->Add(this->label19);
			this->tabPage3->Controls->Add(this->comboBox7);
			this->tabPage3->Controls->Add(this->button10);
			this->tabPage3->Controls->Add(this->richTextBox2);
			this->tabPage3->Controls->Add(this->label9);
			this->tabPage3->Controls->Add(this->button9);
			this->tabPage3->Controls->Add(this->label8);
			this->tabPage3->Controls->Add(this->button5);
			this->tabPage3->Controls->Add(this->textBox6);
			this->tabPage3->Location = System::Drawing::Point(4, 22);
			this->tabPage3->Name = L"tabPage3";
			this->tabPage3->Size = System::Drawing::Size(619, 252);
			this->tabPage3->TabIndex = 2;
			this->tabPage3->Text = L"Chunk Editor";
			this->tabPage3->UseVisualStyleBackColor = true;
			// 
			// button15
			// 
			this->button15->Enabled = false;
			this->button15->Location = System::Drawing::Point(160, 218);
			this->button15->Name = L"button15";
			this->button15->Size = System::Drawing::Size(109, 23);
			this->button15->TabIndex = 34;
			this->button15->Text = L"Update chunk size";
			this->button15->UseVisualStyleBackColor = true;
			this->button15->Click += gcnew System::EventHandler(this, &main::button15_Click);
			// 
			// button14
			// 
			this->button14->Enabled = false;
			this->button14->Location = System::Drawing::Point(11, 218);
			this->button14->Name = L"button14";
			this->button14->Size = System::Drawing::Size(143, 23);
			this->button14->TabIndex = 33;
			this->button14->Text = L"Overwrite chunk header";
			this->button14->UseVisualStyleBackColor = true;
			this->button14->Click += gcnew System::EventHandler(this, &main::button14_Click);
			// 
			// textBox9
			// 
			this->textBox9->Location = System::Drawing::Point(237, 192);
			this->textBox9->Name = L"textBox9";
			this->textBox9->Size = System::Drawing::Size(87, 20);
			this->textBox9->TabIndex = 32;
			this->textBox9->Text = L"0";
			// 
			// label20
			// 
			this->label20->AutoSize = true;
			this->label20->Location = System::Drawing::Point(169, 194);
			this->label20->Name = L"label20";
			this->label20->Size = System::Drawing::Size(62, 13);
			this->label20->TabIndex = 31;
			this->label20->Text = L"Chunk size:";
			// 
			// label17
			// 
			this->label17->AutoSize = true;
			this->label17->Location = System::Drawing::Point(36, 194);
			this->label17->Name = L"label17";
			this->label17->Size = System::Drawing::Size(61, 13);
			this->label17->TabIndex = 29;
			this->label17->Text = L"Chunk flag:";
			// 
			// comboBox5
			// 
			this->comboBox5->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->comboBox5->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->comboBox5->FormattingEnabled = true;
			this->comboBox5->Items->AddRange(gcnew cli::array< System::Object^  >(3) {L"0x0E", L"0x06", L"0x00"});
			this->comboBox5->Location = System::Drawing::Point(103, 191);
			this->comboBox5->Name = L"comboBox5";
			this->comboBox5->Size = System::Drawing::Size(60, 21);
			this->comboBox5->TabIndex = 30;
			// 
			// label18
			// 
			this->label18->AutoSize = true;
			this->label18->Location = System::Drawing::Point(279, 167);
			this->label18->Name = L"label18";
			this->label18->Size = System::Drawing::Size(88, 13);
			this->label18->TabIndex = 27;
			this->label18->Text = L"Chunk version #:";
			// 
			// comboBox6
			// 
			this->comboBox6->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->comboBox6->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->comboBox6->FormattingEnabled = true;
			this->comboBox6->Items->AddRange(gcnew cli::array< System::Object^  >(4) {L"1", L"2", L"3", L"4"});
			this->comboBox6->Location = System::Drawing::Point(373, 164);
			this->comboBox6->Name = L"comboBox6";
			this->comboBox6->Size = System::Drawing::Size(60, 21);
			this->comboBox6->TabIndex = 28;
			// 
			// label19
			// 
			this->label19->AutoSize = true;
			this->label19->Location = System::Drawing::Point(9, 167);
			this->label19->Name = L"label19";
			this->label19->Size = System::Drawing::Size(88, 13);
			this->label19->TabIndex = 25;
			this->label19->Text = L"Chunk data-type:";
			// 
			// comboBox7
			// 
			this->comboBox7->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->comboBox7->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->comboBox7->FormattingEnabled = true;
			this->comboBox7->Items->AddRange(gcnew cli::array< System::Object^  >(25) {L"DB_TEXTURE_SET", L"DB_TEXTURE_CONTEXT", L"DB_AISCRIPT", 
				L"DB_ANIMSCRIPT", L"DB_ANIMATED_TEXTURE_SET", L"DB_MERGED_TEXTURE_SET", L"DB_PARTICLE_EMITTER", L"DB_PARTICLE_SOURCE", L"DB_PARTICLE_EFFECT", 
				L"DB_EXPANSION_TABLE", L"DB_GROUP", L"DB_INSTANCE", L"DB_LOD", L"DB_SOMARRAYBONE", L"DB_SOMARRAY", L"DB_SINGLSOMBONE", L"DB_SINGLSOM", 
				L"DB_COLLISIONSOMARRAY", L"DB_WORLD_EMITTERS", L"DB_WORLD_DISTORTIONS", L"DB_PARTICLE_SURFACE_LIST", L"DB_PARTICLE_LIST", L"DB_EMITTER_LIST", 
				L"DB_VIS_REGIONS", L"DB_SCENE_TREE"});
			this->comboBox7->Location = System::Drawing::Point(103, 164);
			this->comboBox7->Name = L"comboBox7";
			this->comboBox7->Size = System::Drawing::Size(166, 21);
			this->comboBox7->TabIndex = 26;
			this->comboBox7->SelectedIndexChanged += gcnew System::EventHandler(this, &main::comboBox7_SelectedIndexChanged);
			// 
			// button10
			// 
			this->button10->Enabled = false;
			this->button10->Location = System::Drawing::Point(488, 218);
			this->button10->Name = L"button10";
			this->button10->Size = System::Drawing::Size(120, 23);
			this->button10->TabIndex = 20;
			this->button10->Text = L"Open texture viewer";
			this->button10->UseVisualStyleBackColor = true;
			this->button10->Click += gcnew System::EventHandler(this, &main::button10_Click);
			// 
			// richTextBox2
			// 
			this->richTextBox2->Location = System::Drawing::Point(11, 68);
			this->richTextBox2->Name = L"richTextBox2";
			this->richTextBox2->ReadOnly = true;
			this->richTextBox2->ScrollBars = System::Windows::Forms::RichTextBoxScrollBars::ForcedVertical;
			this->richTextBox2->Size = System::Drawing::Size(600, 87);
			this->richTextBox2->TabIndex = 19;
			this->richTextBox2->Text = L"";
			// 
			// label9
			// 
			this->label9->AutoSize = true;
			this->label9->Location = System::Drawing::Point(8, 44);
			this->label9->Name = L"label9";
			this->label9->Size = System::Drawing::Size(86, 13);
			this->label9->TabIndex = 18;
			this->label9->Text = L"Data chunk info:";
			// 
			// button9
			// 
			this->button9->Location = System::Drawing::Point(488, 39);
			this->button9->Name = L"button9";
			this->button9->Size = System::Drawing::Size(123, 23);
			this->button9->TabIndex = 17;
			this->button9->Text = L"Load data-chunk file...";
			this->button9->UseVisualStyleBackColor = true;
			this->button9->Click += gcnew System::EventHandler(this, &main::button9_Click);
			// 
			// label8
			// 
			this->label8->AutoSize = true;
			this->label8->Location = System::Drawing::Point(8, 15);
			this->label8->Name = L"label8";
			this->label8->Size = System::Drawing::Size(119, 13);
			this->label8->TabIndex = 16;
			this->label8->Text = L"Path to the data chunk:";
			// 
			// button5
			// 
			this->button5->Location = System::Drawing::Point(536, 10);
			this->button5->Name = L"button5";
			this->button5->Size = System::Drawing::Size(75, 23);
			this->button5->TabIndex = 14;
			this->button5->Text = L"Browse...";
			this->button5->UseVisualStyleBackColor = true;
			this->button5->Click += gcnew System::EventHandler(this, &main::button5_Click);
			// 
			// textBox6
			// 
			this->textBox6->Location = System::Drawing::Point(138, 12);
			this->textBox6->Name = L"textBox6";
			this->textBox6->Size = System::Drawing::Size(392, 20);
			this->textBox6->TabIndex = 15;
			// 
			// tabPage4
			// 
			this->tabPage4->Controls->Add(this->button16);
			this->tabPage4->Controls->Add(this->progressBar1);
			this->tabPage4->Controls->Add(this->button13);
			this->tabPage4->Controls->Add(this->button12);
			this->tabPage4->Controls->Add(this->listBox1);
			this->tabPage4->Controls->Add(this->label15);
			this->tabPage4->Controls->Add(this->button11);
			this->tabPage4->Controls->Add(this->textBox8);
			this->tabPage4->Controls->Add(this->label14);
			this->tabPage4->Controls->Add(this->textBox7);
			this->tabPage4->Controls->Add(this->label13);
			this->tabPage4->Controls->Add(this->label12);
			this->tabPage4->Controls->Add(this->comboBox4);
			this->tabPage4->Controls->Add(this->label11);
			this->tabPage4->Controls->Add(this->comboBox3);
			this->tabPage4->Controls->Add(this->label10);
			this->tabPage4->Controls->Add(this->comboBox2);
			this->tabPage4->Location = System::Drawing::Point(4, 22);
			this->tabPage4->Name = L"tabPage4";
			this->tabPage4->Size = System::Drawing::Size(619, 252);
			this->tabPage4->TabIndex = 3;
			this->tabPage4->Text = L"Chunk Assembler";
			this->tabPage4->UseVisualStyleBackColor = true;
			// 
			// button16
			// 
			this->button16->Location = System::Drawing::Point(285, 129);
			this->button16->Name = L"button16";
			this->button16->Size = System::Drawing::Size(64, 23);
			this->button16->TabIndex = 36;
			this->button16->Text = L"Clear";
			this->button16->UseVisualStyleBackColor = true;
			this->button16->Click += gcnew System::EventHandler(this, &main::button16_Click);
			// 
			// progressBar1
			// 
			this->progressBar1->Location = System::Drawing::Point(8, 239);
			this->progressBar1->Name = L"progressBar1";
			this->progressBar1->Size = System::Drawing::Size(602, 10);
			this->progressBar1->Step = 1;
			this->progressBar1->Style = System::Windows::Forms::ProgressBarStyle::Continuous;
			this->progressBar1->TabIndex = 35;
			// 
			// button13
			// 
			this->button13->Location = System::Drawing::Point(22, 211);
			this->button13->Name = L"button13";
			this->button13->Size = System::Drawing::Size(64, 23);
			this->button13->TabIndex = 34;
			this->button13->Text = L"Assemble";
			this->button13->UseVisualStyleBackColor = true;
			this->button13->Click += gcnew System::EventHandler(this, &main::button13_Click);
			// 
			// button12
			// 
			this->button12->Location = System::Drawing::Point(285, 100);
			this->button12->Name = L"button12";
			this->button12->Size = System::Drawing::Size(64, 23);
			this->button12->TabIndex = 30;
			this->button12->Text = L"Add...";
			this->button12->UseVisualStyleBackColor = true;
			this->button12->Click += gcnew System::EventHandler(this, &main::button12_Click);
			// 
			// listBox1
			// 
			this->listBox1->FormattingEnabled = true;
			this->listBox1->Location = System::Drawing::Point(113, 100);
			this->listBox1->Name = L"listBox1";
			this->listBox1->Size = System::Drawing::Size(166, 134);
			this->listBox1->TabIndex = 31;
			// 
			// label15
			// 
			this->label15->AutoSize = true;
			this->label15->Location = System::Drawing::Point(52, 103);
			this->label15->Name = L"label15";
			this->label15->Size = System::Drawing::Size(55, 13);
			this->label15->TabIndex = 30;
			this->label15->Text = L"Input files:";
			// 
			// button11
			// 
			this->button11->Location = System::Drawing::Point(285, 72);
			this->button11->Name = L"button11";
			this->button11->Size = System::Drawing::Size(64, 23);
			this->button11->TabIndex = 29;
			this->button11->Text = L"Browse...";
			this->button11->UseVisualStyleBackColor = true;
			this->button11->Click += gcnew System::EventHandler(this, &main::button11_Click);
			// 
			// textBox8
			// 
			this->textBox8->Location = System::Drawing::Point(113, 74);
			this->textBox8->Name = L"textBox8";
			this->textBox8->Size = System::Drawing::Size(166, 20);
			this->textBox8->TabIndex = 28;
			this->textBox8->Text = L"data_output";
			this->textBox8->WordWrap = false;
			// 
			// label14
			// 
			this->label14->AutoSize = true;
			this->label14->Location = System::Drawing::Point(36, 77);
			this->label14->Name = L"label14";
			this->label14->Size = System::Drawing::Size(71, 13);
			this->label14->TabIndex = 27;
			this->label14->Text = L"Output folder:";
			// 
			// textBox7
			// 
			this->textBox7->Location = System::Drawing::Point(528, 46);
			this->textBox7->MaxLength = 2;
			this->textBox7->Name = L"textBox7";
			this->textBox7->Size = System::Drawing::Size(60, 20);
			this->textBox7->TabIndex = 26;
			this->textBox7->Text = L"1";
			this->textBox7->WordWrap = false;
			// 
			// label13
			// 
			this->label13->AutoSize = true;
			this->label13->Location = System::Drawing::Point(455, 49);
			this->label13->Name = L"label13";
			this->label13->Size = System::Drawing::Size(67, 13);
			this->label13->TabIndex = 25;
			this->label13->Text = L"Chunk file #:";
			// 
			// label12
			// 
			this->label12->AutoSize = true;
			this->label12->Location = System::Drawing::Point(46, 49);
			this->label12->Name = L"label12";
			this->label12->Size = System::Drawing::Size(61, 13);
			this->label12->TabIndex = 23;
			this->label12->Text = L"Chunk flag:";
			// 
			// comboBox4
			// 
			this->comboBox4->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->comboBox4->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->comboBox4->FormattingEnabled = true;
			this->comboBox4->Items->AddRange(gcnew cli::array< System::Object^  >(3) {L"0x0E", L"0x06", L"0x00"});
			this->comboBox4->Location = System::Drawing::Point(113, 46);
			this->comboBox4->Name = L"comboBox4";
			this->comboBox4->Size = System::Drawing::Size(60, 21);
			this->comboBox4->TabIndex = 24;
			// 
			// label11
			// 
			this->label11->AutoSize = true;
			this->label11->Location = System::Drawing::Point(434, 22);
			this->label11->Name = L"label11";
			this->label11->Size = System::Drawing::Size(88, 13);
			this->label11->TabIndex = 21;
			this->label11->Text = L"Chunk version #:";
			// 
			// comboBox3
			// 
			this->comboBox3->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->comboBox3->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->comboBox3->FormattingEnabled = true;
			this->comboBox3->Items->AddRange(gcnew cli::array< System::Object^  >(4) {L"1", L"2", L"3", L"4"});
			this->comboBox3->Location = System::Drawing::Point(528, 19);
			this->comboBox3->Name = L"comboBox3";
			this->comboBox3->Size = System::Drawing::Size(60, 21);
			this->comboBox3->TabIndex = 22;
			// 
			// label10
			// 
			this->label10->AutoSize = true;
			this->label10->Location = System::Drawing::Point(19, 22);
			this->label10->Name = L"label10";
			this->label10->Size = System::Drawing::Size(88, 13);
			this->label10->TabIndex = 19;
			this->label10->Text = L"Chunk data-type:";
			// 
			// comboBox2
			// 
			this->comboBox2->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->comboBox2->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->comboBox2->FormattingEnabled = true;
			this->comboBox2->Items->AddRange(gcnew cli::array< System::Object^  >(25) {L"DB_TEXTURE_SET", L"DB_TEXTURE_CONTEXT", L"DB_AISCRIPT", 
				L"DB_ANIMSCRIPT", L"DB_ANIMATED_TEXTURE_SET", L"DB_MERGED_TEXTURE_SET", L"DB_PARTICLE_EMITTER", L"DB_PARTICLE_SOURCE", L"DB_PARTICLE_EFFECT", 
				L"DB_EXPANSION_TABLE", L"DB_GROUP", L"DB_INSTANCE", L"DB_LOD", L"DB_SOMARRAYBONE", L"DB_SOMARRAY", L"DB_SINGLSOMBONE", L"DB_SINGLSOM", 
				L"DB_COLLISIONSOMARRAY", L"DB_WORLD_EMITTERS", L"DB_WORLD_DISTORTIONS", L"DB_PARTICLE_SURFACE_LIST", L"DB_PARTICLE_LIST", L"DB_EMITTER_LIST", 
				L"DB_VIS_REGIONS", L"DB_SCENE_TREE"});
			this->comboBox2->Location = System::Drawing::Point(113, 19);
			this->comboBox2->Name = L"comboBox2";
			this->comboBox2->Size = System::Drawing::Size(166, 21);
			this->comboBox2->TabIndex = 20;
			this->comboBox2->SelectedIndexChanged += gcnew System::EventHandler(this, &main::comboBox2_SelectedIndexChanged);
			// 
			// saveFileDialog1
			// 
			this->saveFileDialog1->Filter = L"DBL game archives|*.dbl";
			this->saveFileDialog1->Title = L"DBL file to save data chunks in";
			// 
			// label7
			// 
			this->label7->AutoSize = true;
			this->label7->Location = System::Drawing::Point(7, 281);
			this->label7->Name = L"label7";
			this->label7->Size = System::Drawing::Size(28, 13);
			this->label7->TabIndex = 11;
			this->label7->Text = L"Log:";
			// 
			// main
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(624, 442);
			this->Controls->Add(this->label7);
			this->Controls->Add(this->tabControl1);
			this->Controls->Add(this->richTextBox1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
			this->MaximizeBox = false;
			this->Name = L"main";
			this->Text = L"DBLFucker";
			this->Load += gcnew System::EventHandler(this, &main::main_Load);
			this->tabControl1->ResumeLayout(false);
			this->tabPage1->ResumeLayout(false);
			this->tabPage1->PerformLayout();
			this->tabPage2->ResumeLayout(false);
			this->tabPage2->PerformLayout();
			this->tabPage3->ResumeLayout(false);
			this->tabPage3->PerformLayout();
			this->tabPage4->ResumeLayout(false);
			this->tabPage4->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
		// UNDONE
	void AssembleASomChunk( void ) {
		tImagedata_s* tIMGDATA;
		INT iLastSlash = 0;
		String^ sfilename;

		if( listBox1->Items->Count <= 0 ) {
			MessageBoxA( NULL, "You haven't selected a single file to pack into the data-chunk", "ERROR", MB_ICONERROR );
			return;
		}

		pTexList = (tTextureList_s*)malloc( sizeof(tTextureList_s) );
		pTexList->texturenum = openFileDialog1->FileNames->Length;
		tIMGDATA = (tImagedata_s*)malloc( pTexList->texturenum*sizeof(tImagedata_s) );
		pTexList->textures = tIMGDATA;
			 
		pChunkHeader = (dbl_chunk_header_s*)malloc( sizeof(dbl_chunk_header_s) );
		pChunkHeader->iDataChunkType = ChunkDataTypeFromString( comboBox2->SelectedItem->ToString() );
		pChunkHeader->iDataChunkVer = comboBox3->SelectedIndex+1;
		switch( comboBox4->SelectedIndex ) {
			case 0:
				pChunkHeader->iFlag = 0x0E;
			break;
			case 1:
				pChunkHeader->iFlag = 0x06;
			break;
			case 2:
				pChunkHeader->iFlag = 0x00;
			break;
		}

		for( INT i = 0; i < pTexList->texturenum; i++ ) { // Separate filename from path
			sfilename = (String^)openFileDialog1->FileNames[i];
			iLastSlash = sfilename->LastIndexOf( "\\" );
			sfilename = sfilename->Substring( iLastSlash+1, sfilename->Length-(iLastSlash+1) );
			strcpy( tIMGDATA[i].name, (char*)Marshal::StringToHGlobalAnsi(sfilename).ToPointer() );
			sfilename = (String^)openFileDialog1->FileNames[i];
			sfilename = sfilename->Substring( 0, iLastSlash );
			tIMGDATA[i].basepath = (char*)malloc( sfilename->Length );
			strcpy( tIMGDATA[i].basepath, (char*)Marshal::StringToHGlobalAnsi(sfilename).ToPointer() );
			iLastSlash = 0;
		}
	}

	void AssembleATexChunk( void ) {
		tImagedata_s* tIMGDATA;
		INT iLastSlash = 0;
		String^ sfilename;

		if( listBox1->Items->Count <= 0 ) {
			MessageBoxA( NULL, "You haven't selected a single file to pack into the data-chunk", "ERROR", MB_ICONERROR );
			return;
		}

		pTexList = (tTextureList_s*)malloc( sizeof(tTextureList_s) );
		pTexList->texturenum = openFileDialog1->FileNames->Length;
		tIMGDATA = (tImagedata_s*)malloc( pTexList->texturenum*sizeof(tImagedata_s) );
		pTexList->textures = tIMGDATA;
			 
		pChunkHeader = (dbl_chunk_header_s*)malloc( sizeof(dbl_chunk_header_s) );
		pChunkHeader->iDataChunkType = ChunkDataTypeFromString( comboBox2->SelectedItem->ToString() );
		pChunkHeader->iDataChunkVer = comboBox3->SelectedIndex+1;
		switch( comboBox4->SelectedIndex ) {
			case 0:
				pChunkHeader->iFlag = 0x0E;
			break;
			case 1:
				pChunkHeader->iFlag = 0x06;
			break;
			case 2:
				pChunkHeader->iFlag = 0x00;
			break;
		}

		for( INT i = 0; i < pTexList->texturenum; i++ ) { // Separate filename from path
			sfilename = (String^)openFileDialog1->FileNames[i];
			iLastSlash = sfilename->LastIndexOf( "\\" );
			sfilename = sfilename->Substring( iLastSlash+1, sfilename->Length-(iLastSlash+1) );
			strcpy( tIMGDATA[i].name, (char*)Marshal::StringToHGlobalAnsi(sfilename).ToPointer() );
			sfilename = (String^)openFileDialog1->FileNames[i];
			sfilename = sfilename->Substring( 0, iLastSlash );
			tIMGDATA[i].basepath = (char*)malloc( sfilename->Length );
			strcpy( tIMGDATA[i].basepath, (char*)Marshal::StringToHGlobalAnsi(sfilename).ToPointer() );
			iLastSlash = 0;
		}
	}

	// ==========

	void BuildAChunk( void ) {
		CreateTexChunk( pTexList, pChunkHeader, (char*)Marshal::StringToHGlobalAnsi(textBox8->Text).ToPointer(), Convert::ToInt16(textBox7->Text),
			richTextBox1, progressBar1 );
		for( INT i = 0; i < pTexList->texturenum; i++ ) {
			if( pTexList->textures[i].pixelcount > 0 ) free( pTexList->textures[i].pixels );
			if( pTexList->textures[i].paloffs > 0 ) free( pTexList->textures[i].pal );
		}
		GC::Collect();
		free( pTexList->textures );
		free( pTexList );
		free( pChunkHeader );

		progressBar1->Value = 0;
		//listBox1->Items->Clear();
	}

	void THREAD_BuildAChunk( void ) {
		this->Invoke( BuildAChunk_Del );
	}

	private: System::Void main_Load(System::Object^  sender, System::EventArgs^  e) {
				 BuildAChunk_Del = gcnew BuildAChunkDel( this, &main::BuildAChunk );
				 filelist = NULL;
			 }
	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
				 openFileDialog1->Filter = "DBL game archives|*.dbl";
				 openFileDialog1->Title = "Pick a DBL file to load";
				 openFileDialog1->Multiselect = FALSE;
				 openFileDialog1->ShowDialog();
				 if( IO::File::Exists(openFileDialog1->FileName) ) textBox1->Text = openFileDialog1->FileName;
				 else MessageBoxA( NULL, "The file doesn't exist anymore!", "ERROR", MB_ICONERROR );
			 }
private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {
			 folderBrowserDialog1->Description = "Select the output folder to put the extracted chunks into";
			 folderBrowserDialog1->ShowDialog();
			 if( IO::Directory::Exists(folderBrowserDialog1->SelectedPath) ) textBox2->Text = folderBrowserDialog1->SelectedPath;
			 else MessageBoxA( NULL, "The selected folder doesn't exist anymore!", "ERROR", MB_ICONERROR );
		 }

		 // Load a DBL file
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
			 if( !ReadDBLFile((char*)Marshal::StringToHGlobalAnsi(textBox1->Text).ToPointer(), richTextBox1) ) {
				 MessageBoxA( NULL, "Couldn't read the DBL file!", "ERROR", MB_ICONERROR );
				 return;
			 }
			 button1->Enabled = TRUE;
		 }

		 // Extract the loaded DBL file
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 if( !ExtractChunks((char*)Marshal::StringToHGlobalAnsi(textBox2->Text).ToPointer(), richTextBox1) ) {
				 MessageBoxA( NULL, "Couldn't extract data chunks from the DBL file!", "ERROR", MB_ICONERROR );
				 return;
			 }
			 MessageBoxA( NULL, "The DBL file has been successfully unpacked", "SUCKCESS", MB_ICONINFORMATION );
		 }
private: System::Void button8_Click(System::Object^  sender, System::EventArgs^  e) {
			 folderBrowserDialog1->Description = "Select the folder containing data chunks you want to pack into a DBL file";
			 folderBrowserDialog1->ShowDialog();
			 if( IO::Directory::Exists(folderBrowserDialog1->SelectedPath) ) textBox4->Text = folderBrowserDialog1->SelectedPath;
			 else MessageBoxA( NULL, "The selected folder doesn't exist anymore!", "ERROR", MB_ICONERROR );
		 }
private: System::Void button7_Click(System::Object^  sender, System::EventArgs^  e) {
			 saveFileDialog1->ShowDialog();
			 if( !String::IsNullOrEmpty(saveFileDialog1->FileName) ) textBox3->Text = saveFileDialog1->FileName;
			 else MessageBoxA( NULL, "You didn't pick the file fool!", "ERROR", MB_ICONERROR );
		 }

		 // Assemble a DBL file from the chunks in a specified folder
private: System::Void button6_Click(System::Object^  sender, System::EventArgs^  e) {
				 dbl_header_s* pHeader = (dbl_header_s*)malloc( sizeof(dbl_header_s) );
				 INT16 iMaxChunks = 0;
				 pHeader->dblId = (char*)malloc( 64 );
				 if( !Int16::TryParse(textBox5->Text, iMaxChunks) ) {
					 MessageBoxA( NULL, "MaxChunks is NAN", "ERROR", MB_ICONERROR );
					 return;
				 }
				 if( iMaxChunks < 0 ) {
					 MessageBoxA( NULL, "MaxChunks is less than zero", "ERROR", MB_ICONERROR );
					 return;
				 }
				 pHeader->iChunksNum = iMaxChunks;
				 strcpy( pHeader->dblId, (char*)Marshal::StringToHGlobalAnsi(comboBox1->Text).ToPointer() );

				 if( CreateDBL((char*)Marshal::StringToHGlobalAnsi(textBox4->Text).ToPointer(),
					 (char*)Marshal::StringToHGlobalAnsi(textBox3->Text).ToPointer(), richTextBox1, pHeader) ) {
						 MessageBoxA( NULL, "The DBL file has been successfully created", "SUCKCESS", MB_ICONINFORMATION );
				 }
				 else MessageBoxA( NULL, "Couldn't create the DBL file", "ERROR", MB_ICONERROR );
				 free( pHeader );
		 }
private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) {
			 openFileDialog1->Filter = "Data-chunk files|*.chk";
			 openFileDialog1->Title = "Pick a data-chunk file to load";
			 openFileDialog1->Multiselect = FALSE;
			 openFileDialog1->ShowDialog();
			 if( !IO::File::Exists(openFileDialog1->FileName) ) {
				 MessageBoxA( NULL, "The file doesn't exist anymore!", "ERROR", MB_ICONERROR );
				 return;
			 }
			 textBox6->Text = openFileDialog1->FileName;
		 }

		 // Load a datachunk file
private: System::Void button9_Click(System::Object^  sender, System::EventArgs^  e) {
			 dbl_chunk_header_s* pDatachunkHeader = NULL;
			 String^ chunk_dt_name;

			 button10->Enabled = FALSE;

			 if( !(pDatachunkHeader = LoadChunkFromFile((char*)Marshal::StringToHGlobalAnsi(textBox6->Text).ToPointer())) ) {
				 MessageBoxA( NULL, "Couldn't load the DBL data-chunk file", "ERROR", MB_ICONINFORMATION );
				 return;
			 }
			 chunk_dt_name = MakeString( (char*)ChunkDataTypeToString(pDatachunkHeader) );
			 richTextBox2->Text = "Data-chunk file "+textBox6->Text+":\n";
			 richTextBox2->Text += "\n===============================";
			 richTextBox2->Text += "\nChunk data type: 0x"+String::Format("{0:X}", pDatachunkHeader->iDataChunkType)
				 +" ("+chunk_dt_name+")";
			 richTextBox2->Text += "\nChunk version: "+pDatachunkHeader->iDataChunkVer.ToString();
			 if( chunk_dt_name != "Unknown" ) {
				 if( !CheckChunkDataTypeForVersion(pDatachunkHeader->iDataChunkType, pDatachunkHeader->iDataChunkVer) ) {
					 richTextBox2->Text += "\n\t* VERSION DOES NOT MATCH! Should be "
					 +Chunk_VersionConsistency[pDatachunkHeader->iDataChunkVer]+" *";
				 }
				 else richTextBox2->Text += "\n\t* VERSION MATCH *";
			 }
			 else
				 richTextBox2->Text += "\n\t* CANNOT CHECK FOR VERSION CONSISTENCY, DATA TYPE IS UNKNOWN *";

			 switch( pDatachunkHeader->iDataChunkType ) {
				 case DB_TEXTURE_SET:
					 button10->Tag = DB_TEXTURE_SET;
					 button10->Text = "Open texture viewer";
					 button10->Enabled = TRUE;
				 break;
				 case DB_TEXTURE_CONTEXT:// texture set will pop up the window asking if you want to view the context chunk too
					 button10->Text = "Inaccessible";
					 button10->Enabled = FALSE;
					 MessageBoxA( NULL, "Select a DB_TEXTURE_SET chunk to edit the texture context", "ERROR", MB_ICONEXCLAMATION );
				 break;
				 case DB_TEXTURE_CONTEXT2: // texture set will pop up the window asking if you want to view the context chunk too
					 button10->Text = "Inaccessible";
					 button10->Enabled = FALSE;
					 MessageBoxA( NULL, "Select a DB_TEXTURE_SET chunk to edit the texture context", "ERROR", MB_ICONEXCLAMATION );
				 break;
				 case DB_SOMARRAY:
					 button10->Tag = DB_SOMARRAY;
					 button10->Text = "Open SOM array editor";
					 button10->Enabled = TRUE;
				 break;
				 case DB_SOMARRAYBONE:
					 button10->Tag = DB_SOMARRAYBONE;
					 button10->Text = "Open SOM array editor";
					 button10->Enabled = TRUE;
				 break;
			 }

			 comboBox7->SelectedItem = (Object^)MakeString((char*)ChunkDataTypeToString(pDatachunkHeader));
			 switch( pDatachunkHeader->iFlag ) {
				case 0x0E:
					comboBox5->SelectedIndex = 0;
				break;
				case 0x06:
					comboBox5->SelectedIndex = 1;
				break;
				case 0x00:
					comboBox5->SelectedIndex = 2;
				break;
			 }
			 if( pDatachunkHeader->iDataChunkVer > 3 ) {
				 //comboBox6->SelectedIndex = Chunk_VersionConsistency[pDatachunkHeader->iDataChunkVer]-1;
				 comboBox6->Text = pDatachunkHeader->iDataChunkVer.ToString();
			 }
			 else comboBox6->SelectedIndex = pDatachunkHeader->iDataChunkVer-1;
			 textBox9->Text = pDatachunkHeader->iDataChunkSize.ToString();

			 delete[] chunk_dt_name;
			 free( pDatachunkHeader );

			 button14->Enabled = TRUE; button15->Enabled = TRUE;
		 }

		 // Individual button to edit a chunk, whether texture, context or something else
private: System::Void button10_Click(System::Object^  sender, System::EventArgs^  e) {
			 if( !IO::File::Exists(textBox6->Text) ) {
				 MessageBoxA( NULL, "The file doesn't exist anymore!", "ERROR", MB_ICONERROR );
				 return;
			 }
			 switch( (INT)button10->Tag ) {
				 case DB_TEXTURE_SET: {
					 wtexture_viewer^ pTexViewerWindow = gcnew wtexture_viewer();
					 pTexViewerWindow->szChunkfilename = (char*)Marshal::StringToHGlobalAnsi(textBox6->Text).ToPointer();
					 pTexViewerWindow->Show();
				 break; }
				 case DB_SOMARRAY: {
					 wsomed^ pSomEdWindow = gcnew wsomed();
					 pSomEdWindow->szChunkfilename = (char*)Marshal::StringToHGlobalAnsi(textBox6->Text).ToPointer();
					 pSomEdWindow->Show();
				 break; }
				 case DB_SOMARRAYBONE: {
					 wsomed^ pSomEdWindow = gcnew wsomed();
					 pSomEdWindow->szChunkfilename = (char*)Marshal::StringToHGlobalAnsi(textBox6->Text).ToPointer();
					 pSomEdWindow->Show();
				 break; }
			 }
		 }
private: System::Void comboBox2_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			 INT16 chunk_dt;

			 chunk_dt = ChunkDataTypeFromString( comboBox2->SelectedItem->ToString() );
			 for( INT i = 0; i < 25; i++ ) {
				 if( Chunk_DataTypes[i] == chunk_dt ) {
					 comboBox3->SelectedIndex = Chunk_VersionConsistency[i]-1;
					 break;
				 }
			 }

			 switch( chunk_dt ) {
				 case DB_TEXTURE_SET: {
					 label15->Text = "Textures:";
				 } break;
				 case DB_SOMARRAY: {
					 label15->Text = "OBJ meshes:";
				 } break;
			 }
		 }
private: System::Void button11_Click(System::Object^  sender, System::EventArgs^  e) {
			 folderBrowserDialog1->Description = "Select the folder you want the assembled chunk to be put into";
			 folderBrowserDialog1->ShowDialog();
			 if( IO::Directory::Exists(folderBrowserDialog1->SelectedPath) ) textBox8->Text = folderBrowserDialog1->SelectedPath;
			 else MessageBoxA( NULL, "The selected folder doesn't exist anymore!", "ERROR", MB_ICONERROR );
		 }
private: System::Void button12_Click(System::Object^  sender, System::EventArgs^  e) {
			 String^ fname;

			 openFileDialog1->Filter = "All files|*.*";
			 openFileDialog1->Title = "Pick files to add to the data-chunk";
			 openFileDialog1->Multiselect = TRUE;
			 openFileDialog1->ShowDialog();

			 for( INT i = 0; i < openFileDialog1->FileNames->Length; i++ ) {
				 if( !IO::File::Exists((String^)openFileDialog1->FileNames[i]) ) {
					 richTextBox1->Text += "\nERROR! File "+(String^)openFileDialog1->FileNames[i]+" doesn't exist!";
					 continue;
				 }
				 fname = (String^)openFileDialog1->FileNames[i];
				 listBox1->Items->Add( fname->Substring(fname->LastIndexOf("\\")+1, fname->Length-(fname->LastIndexOf("\\")+1)) );
				 delete[] fname;
			 }
		 }
		 // ASSEMBLE CHUNK BUTTON
private: System::Void button13_Click(System::Object^  sender, System::EventArgs^  e) {
			 INT chunk_dt;

			 chunk_dt = ChunkDataTypeFromString( comboBox2->SelectedItem->ToString() );
			 switch( chunk_dt ) {
				case DB_TEXTURE_SET:
					AssembleATexChunk();
				break;
			 }

			 richTextBox1->Text = "Trying to assemble the chunk "+textBox8->Text;

			 //for( INT i = 0; i < pTexList->texturenum; i++ ) free( tIMGDATA[i].basepath );
			 //free( tIMGDATA );

			 pThread = gcnew Thread( gcnew Threading::ThreadStart( this, &main::THREAD_BuildAChunk ) );
			 pThread->Start();
		 }

		 // Overwrite chunk header
private: System::Void button14_Click(System::Object^  sender, System::EventArgs^  e) {
			 dbl_chunk_header_s* pChunkHeader;
			 
			 if( !IO::File::Exists(textBox6->Text) ) {
				 MessageBoxA( NULL, "The file doesn't exist anymore!", "ERROR", MB_ICONERROR );
				 return;
			 }

			 pChunkHeader = (dbl_chunk_header_s*)malloc( sizeof(dbl_chunk_header_s) );
			 pChunkHeader->iDataChunkType = ChunkDataTypeFromString( comboBox7->SelectedItem->ToString() );
			 switch( comboBox5->SelectedIndex ) {
				case 0:
					pChunkHeader->iFlag = 0x0E;
				break;
				case 1:
					pChunkHeader->iFlag = 0x06;
				break;
				case 2:
					pChunkHeader->iFlag = 0x00;
				break;
			 }
			 pChunkHeader->iDataChunkVer = Convert::ToInt16( comboBox6->SelectedItem->ToString() );
			 pChunkHeader->iDataChunkSize = Convert::ToInt32( textBox9->Text );

			 ChangeChunkHeader( (char*)Marshal::StringToHGlobalAnsi(textBox6->Text).ToPointer(),
				 pChunkHeader, richTextBox1 );

			 if( !Convert::ToInt32(textBox9->Text) ) {
				 UpdateChunkSize( (char*)Marshal::StringToHGlobalAnsi(textBox6->Text).ToPointer(), richTextBox1 );
			 }

			 free( pChunkHeader );
		 }
private: System::Void comboBox7_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			 for( INT i = 0; i < 25; i++ ) {
				 if( Chunk_DataTypes[i] == ChunkDataTypeFromString(comboBox7->SelectedItem->ToString()) ) {
					 comboBox6->SelectedIndex = Chunk_VersionConsistency[i]-1;
					 break;
				 }
			 }
		 }

		 // Update chunk size
private: System::Void button15_Click(System::Object^  sender, System::EventArgs^  e) {
			 UpdateChunkSize( (char*)Marshal::StringToHGlobalAnsi(textBox6->Text).ToPointer(), richTextBox1 );
		 }
private: System::Void button16_Click(System::Object^  sender, System::EventArgs^  e) {
			 listBox1->Items->Clear();
		 }
};
}