//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "registry.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IdExplicitTLSClientServerBase"
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE Thread;
int CurrentVersion, counter = 0, ServerVersionOnWeb;
int downloadrate = 0, currentdownloadtotal = 0, previousdownloadtotal = 0, downloadtime = 0, totalrate = 0;
int totaldownloadtotal = 0;
String filename;
bool required = false;
bool start_install = false;
//---------------------------------------------------------------------------
DWORD WINAPI ThreadFunction(LPVOID Param)
{
//if Memo2 has anything in it, start downloading
do
{
try
{
filename = Form1->Memo2->Lines->Strings[0];
//get size and update progressbar
Form1->ProgressBar2->Max = Form1->FTP1->Size(filename);
Form1->ProgressBar2->Position = 0;
Form1->FTP1->Get(filename, filename, true, false);
}
catch(...)
{
Form1->FTP1->Abort();
Form1->FTP1->Disconnect();
Application->Terminate();
};
}
while(Form1->Memo2->Lines->Strings[0] != "");
if(Form1->Memo2->Lines->Strings[0] == "")
{
required = false;
Form1->Label1->Caption = "Overall Progress: Download Complete!";
Form1->ProgressBar1->Position += 1;
Form1->Timer1->Enabled = false;
Form1->Label2->Caption = "File Progress: ";
//start next process
start_install = true;
SuspendThread(Thread);
}
}
//---------------------------------------------------------------------------
void ExitApp(void)
{
try
{
//Free all open resources, then exit the app
Form1->FTP1->Abort();
Form1->FTP1->Disconnect();
Application->Terminate();
}
catch(...){};
}
//---------------------------------------------------------------------------
void FilterMemo12(void)
{
//filter Memo1
int x = 0;
do
{
if(Form1->Memo1->Lines->Strings[x] == "")
{
Form1->Memo1->Lines->Delete(x);
}
else
{
x++;
}
}
while(x < Form1->Memo1->Lines->Count);
//repeat the process for Memo2
x = 0;
do
{
if(Form1->Memo2->Lines->Strings[x] == "")
{
Form1->Memo2->Lines->Delete(x);
}
else
{
x++;
}
}
while(x < Form1->Memo2->Lines->Count);
//now allow Threading Process to begin
Form1->Label1->Caption = "Overall Progress: Downloading Updates...";
Form1->ProgressBar1->Position += 1;
Form1->Timer1->Enabled = true;
Form1->ProgressBar1->Max += Form1->Memo2->Lines->Count;
required = true;
//Set Cursor to beginning of Memo1, so user can see top of Memo
ResumeThread(Thread);
}
//---------------------------------------------------------------------------
void CompareVersions(void)
{
//Compare CurrentVersion and ServerVersionOnWeb
if(CurrentVersion >= ServerVersionOnWeb)
{
//close program and launch vsu
ExitApp();
ShellExecute(GetDesktopWindow(), "open", "VSU.exe", NULL, NULL, SW_SHOWNORMAL);
}
else if(CurrentVersion < ServerVersionOnWeb)
{
//VSU needs to be updated, download nufls.vor, swn.vor
//after these files are downloaded, then begin to download the files
//contained in "nufls.vor"
try
{
Form1->Label1->Caption = "Overall Progress: Preparing Information...";
Form1->ProgressBar1->Position += 1;
Form1->ProgressBar2->Max = Form1->FTP1->Size("nufls.vor");
required = false;
Form1->FTP1->Get("nufls.vor", "nufls.vor", true, false);
Form1->ProgressBar2->Max = Form1->FTP1->Size("swn.vor");
Form1->FTP1->Get("swn.vor", "swn.vor", true, false);
//done downloading important updates, now begin showing them and
//preparing data for final update process
Form1->Memo2->Clear();
Form1->Memo2->Lines->LoadFromFile("nufls.vor");
Form1->Memo1->Clear();
Form1->Memo1->Lines->LoadFromFile("swn.vor");
//done displaying content, now filter spaces in memo1, memo2
FilterMemo12();
}
catch(...)
{
ExitApp();
}
}
}
//---------------------------------------------------------------------------
void FTPConnect(void)
{
//Connect to FTP
try
{
Form1->FTP1->Connect();
}
catch(...)
{
ExitApp();
}
}
//---------------------------------------------------------------------------
void CheckRegistry(void)
{
//Check the registry for the current server version installed on the
//system, so we can come back and check to see if the current version is
//more recent than the latest update or not.
Form1->Label1->Caption = "Overall Progress: Checking Registry...";
Form1->ProgressBar1->Position += 1;
TRegistry *Reg = new TRegistry();
Reg->RootKey = HKEY_LOCAL_MACHINE;
AnsiString temp;
char gSerial[40];
if(Reg->KeyExists("SOFTWARE\\VSU"))
{
//try to open the key and read from it
try
{
//
if(Reg->OpenKey("SOFTWARE\\VSU", FALSE))
{
//key has been opened now begin reading
temp = Reg->ReadString("ServerVersion");
strcpy(gSerial, temp.c_str());
Reg->CloseKey();
CurrentVersion = StrToInt(gSerial);
//RETRIEVED THE KEY, NOW CONTINUE WITH PROCESS
Form1->Show();
Form1->FTPConnectTimer->Enabled = true;
}
else
{
//could not open the key for 2 reasons
//1) Vista needs to run the program with administrative rights
//2) no idea lol
/* close or no?? */
ExitApp();
}
}
catch(...)
{
//could not open the key and read from it, close the program
ExitApp();
};
}
else
{
//could not open the key, because it probably does not exist
CurrentVersion = 0;
Form1->Show();
Form1->FTPConnectTimer->Enabled = true;
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
DWORD Id;
Thread = CreateThread(0, 0, ThreadFunction, Form1->Handle, CREATE_SUSPENDED, &Id);
if(!Thread)
{
//ADD CODING FOR WHAT TO DO WHEN THREAD CANNOT BE CREATED
Application->Terminate();
}
else
{
//BEGIN PROCESSING
CheckRegistry();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FTP1AfterClientLogin(TObject *Sender)
{
Form1->Label1->Caption = "Overall Progress: Connected to the VSU Network!";
ProgressBar1->Position += 1;
//Client has connected, begin the process of file downloads
try
{
//try to get the csv.vor file
ProgressBar2->Max = FTP1->Size("csv.vor");
//tell the FTP component that this is not a required file
required = false;
Form1->Show();
FTP1->Get("csv.vor", "csv.vor", true, false);
Memo3->Clear();
Memo3->Lines->LoadFromFile("csv.vor");
ServerVersionOnWeb = StrToInt(Memo3->Lines->Strings[0]);
CompareVersions();
}
catch(...)
{
ExitApp();
};
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FTP1WorkEnd(TObject *ASender, TWorkMode AWorkMode)
{
//check to see if the file that was downloaded was a required file for the updater
if(required == true)
{
Memo2->Lines->Delete(0);
totaldownloadtotal += ProgressBar2->Max;
ProgressBar1->Position += 1;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FTP1Work(TObject *ASender, TWorkMode AWorkMode,
int AWorkCount)
{
ProgressBar2->Position = AWorkCount;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FTPConnectTimerTimer(TObject *Sender)
{
FTPConnectTimer->Enabled = false;
FTPConnect();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
try
{
downloadtime += 1;
currentdownloadtotal = ProgressBar2->Position;
totalrate += (totaldownloadtotal + currentdownloadtotal) - previousdownloadtotal;
previousdownloadtotal = totaldownloadtotal + currentdownloadtotal;
downloadrate = totalrate / downloadtime;
Form1->Label2->Caption = "File Progress: "+filename+"... "+IntToStr(downloadrate/1024)+" KB/Second...";
}
catch(...){};
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
if(start_install == true)
{
//
Timer2->Enabled = false;
Label1->Caption = "Overall Progress: Installing Files...";
//begin the installation process by calling 'sinsall.bat'
ShellExecute(GetDesktopWindow(), "open", "sinstall.bat", NULL, NULL, SW_HIDE);
//begin timer
Timer3->Enabled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer3Timer(TObject *Sender)
{
Timer3->Enabled = false;
//installation done, now close and restart 'VSU.exe'
Label1->Caption = "Overall Progress: Installation Complete! Launching VSU Server...";
ExitApp();
}
//---------------------------------------------------------------------------