Subversion Repositories spk

Rev

Rev 36 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#pragma once

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


namespace Creator {

        /// <summary>
        /// Summary for LoadText
        ///
        /// WARNING: If you change the name of this class, you will need to change the
        ///          'Resource File Name' property for the managed resource compiler tool
        ///          associated with all .resx files this class depends on.  Otherwise,
        ///          the designers will not be able to interact properly with localized
        ///          resources associated with this form.
        /// </summary>
        public ref class LoadText : public System::Windows::Forms::Form
        {
        public:
                LoadText(CLinkList<SGameDir> *dirs, CPackages *p, Collections::Hashtable ^text)
                {
                        InitializeComponent();

                        m_pTextList = text;
                        m_pCurrentDir = NULL;
                        m_pPackages = p;
                        m_lDirs = dirs;
                }

                void Start()
                {
                        if ( m_pCurrentDir ) this->label2->Text = SystemStringFromCyString(m_pCurrentDir->sDir);
                        this->backgroundWorker1->RunWorkerAsync();
                }

                void DoWork()
                {
                        if ( !m_pCurrentDir ) return;

                        int game = m_pCurrentDir->sGame.GetToken(" ", 1, 1).ToInt();
                        int flags = m_pPackages->GetGameExe()->GetGameFlags(game);

                        CyString textFile = SPK::FormatTextName(1, m_pPackages->GetLanguage(), (flags & EXEFLAG_TCTEXT)) + ".pck";

                        // find the latest text file
                        if ( m_pPackages->ExtractGameFile(CyString("t/") + textFile, m_pPackages->GetTempDirectory() + "/text.xml", m_pCurrentDir->sDir) )
                                this->ReadTextFromFile(SystemStringFromCyString(m_pPackages->GetTempDirectory() + "/text.xml"), game);

                        if ( !m_pCurrentDir->bLoad ) return;

                        // extract any text files in the t directory
                        cli::array<String ^> ^files = IO::Directory::GetFiles(SystemStringFromCyString(m_pCurrentDir->sDir.FindReplace("/", "\\") + "\\t"), "*.xml");
                        for ( int i = 0; i < files->Length; i++ )
                                this->ReadTextFromFile(files[i], game);

                        cli::array<String ^> ^filespck = IO::Directory::GetFiles(SystemStringFromCyString(m_pCurrentDir->sDir.FindReplace("/", "\\") + "\\t"), "*.pck");
                        for ( int i = 0; i < filespck->Length; i++ )
                        {
                                // extract the t file
                                C_File F(CyStringFromSystemString(filespck[i]));
                                if ( F.UnPCKFile() )
                                {
                                        if ( F.WriteToFile(m_pPackages->GetTempDirectory() + "/text.xml") )
                                                this->ReadTextFromFile(SystemStringFromCyString(m_pPackages->GetTempDirectory() + "/text.xml"), game);
                                }
                        }
                }

                void Next()
                {
                        m_pCurrentDir = m_lDirs->Next();
                        if ( m_pCurrentDir )
                                this->Start();
                        else
                                this->Close();
                }

                void ReadTextFromFile(String ^file, int game)
                {

                        XmlTextReader ^reader = gcnew XmlTextReader(file);
                        int inPage = -1;
                        int inID = -1;
                        while ( reader->Read() )
                        {
                                switch(reader->NodeType)
                                {
                                        case XmlNodeType::Element:
                                                if ( String::Compare(reader->Name, "page") == 0 )
                                                {
                                                        while (reader->MoveToNextAttribute())
                                                        {
                                                                if ( String::Compare(reader->Name, "id") == 0 )
                                                                {
                                                                        inPage = Convert::ToInt32(reader->Value);
                                                                        if ( inPage >= 10000 )
                                                                                inPage %= 10000;
                                                                }
                                                        }
                                                }
                                                else if ( inPage > 0 && String::Compare(reader->Name, "t") == 0 )
                                                {
                                                        while (reader->MoveToNextAttribute())
                                                        {
                                                                if ( String::Compare(reader->Name, "id") == 0 )
                                                                        inID = Convert::ToInt32(reader->Value);
                                                        }
                                                }
                                                break;
                                        case XmlNodeType::Text:
                                                if ( inID > 0 )
                                                {
                                                        String ^key = Convert::ToString(game) + "," + Convert::ToString(inPage) + "," + Convert::ToString(inID);
                                                        String ^t = reader->Value;
                                                        while ( t->IndexOf('(') != -1 && t->IndexOf(')') != -1 )
                                                        {
                                                                int s = t->IndexOf('(');
                                                                t = t->Remove(s, t->IndexOf(')') - s + 1);
                                                        }

                                                        if ( m_pTextList->ContainsKey(key) )
                                                                m_pTextList[key] = t;
                                                        else
                                                                m_pTextList->Add(key, t);
                                                }
                                                break;
                                        case XmlNodeType::EndElement:
                                                if ( String::Compare(reader->Name, "page") == 0 )
                                                        inPage = -1;
                                                else if ( String::Compare(reader->Name, "t") == 0 )
                                                        inID = -1;
                                                break;

                                }
                        }
                        return;
                }


        protected:
                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                ~LoadText()
                {
                        if (components)
                        {
                                delete components;
                        }
                }
        private: System::Windows::Forms::Label^  label1;
        protected: 
        private: System::Windows::Forms::Label^  label2;
        private: System::ComponentModel::BackgroundWorker^  backgroundWorker1;

        private:
                Collections::Hashtable  ^m_pTextList;
                CPackages       *m_pPackages;
                SGameDir        *m_pCurrentDir;
                CLinkList<SGameDir>     *m_lDirs;
                /// <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->label1 = (gcnew System::Windows::Forms::Label());
                        this->label2 = (gcnew System::Windows::Forms::Label());
                        this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
                        this->SuspendLayout();
                        // 
                        // label1
                        // 
                        this->label1->Dock = System::Windows::Forms::DockStyle::Top;
                        this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
                                static_cast<System::Byte>(0)));
                        this->label1->Location = System::Drawing::Point(10, 10);
                        this->label1->Name = L"label1";
                        this->label1->Size = System::Drawing::Size(231, 32);
                        this->label1->TabIndex = 0;
                        this->label1->Text = L"Loading Text";
                        this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
                        this->label1->UseWaitCursor = true;
                        // 
                        // label2
                        // 
                        this->label2->Dock = System::Windows::Forms::DockStyle::Fill;
                        this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Italic, System::Drawing::GraphicsUnit::Point, 
                                static_cast<System::Byte>(0)));
                        this->label2->Location = System::Drawing::Point(10, 42);
                        this->label2->Name = L"label2";
                        this->label2->Size = System::Drawing::Size(231, 23);
                        this->label2->TabIndex = 1;
                        this->label2->TextAlign = System::Drawing::ContentAlignment::TopCenter;
                        this->label2->UseWaitCursor = true;
                        // 
                        // backgroundWorker1
                        // 
                        this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &LoadText::backgroundWorker1_DoWork);
                        this->backgroundWorker1->RunWorkerCompleted += gcnew System::ComponentModel::RunWorkerCompletedEventHandler(this, &LoadText::backgroundWorker1_RunWorkerCompleted);
                        // 
                        // LoadText
                        // 
                        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                        this->BackColor = System::Drawing::Color::Gray;
                        this->ClientSize = System::Drawing::Size(251, 75);
                        this->ControlBox = false;
                        this->Controls->Add(this->label2);
                        this->Controls->Add(this->label1);
                        this->Cursor = System::Windows::Forms::Cursors::WaitCursor;
                        this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
                        this->Name = L"LoadText";
                        this->Padding = System::Windows::Forms::Padding(10);
                        this->ShowInTaskbar = false;
                        this->SizeGripStyle = System::Windows::Forms::SizeGripStyle::Hide;
                        this->StartPosition = System::Windows::Forms::FormStartPosition::CenterParent;
                        this->Text = L"LoadText";
                        this->TopMost = true;
                        this->UseWaitCursor = true;
                        this->Load += gcnew System::EventHandler(this, &LoadText::LoadText_Load);
                        this->ResumeLayout(false);

                }
#pragma endregion
        private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
                                 this->DoWork();
                         }
        private: System::Void LoadText_Load(System::Object^  sender, System::EventArgs^  e) {
                                 m_pCurrentDir = m_lDirs->First();
                                 this->Start();
                         }
        private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^  sender, System::ComponentModel::RunWorkerCompletedEventArgs^  e) {
                                 this->Next();
                         }
};
}