- unit Unit1;
-
- interface
-
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IniFiles, Vcl.StdCtrls, IdBaseComponent,
- IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, StrUtils, IdHashCRC,
- Vcl.ComCtrls;
-
- type
- TForm1 = class(TForm)
- IdHTTP1: TIdHTTP;
- Memo1: TMemo;
- ProgressBar1: TProgressBar;
- procedure FormCreate(Sender: TObject);
- private
- function GetFile_CRC(const iFileName: String): String;
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.dfm}
-
- type
- inifmt = record
- path: String;
- url: String;
- filename: String;
- size: String;
- crc: String;
- method: String;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- Var
- RealCRC, BufferCRC: String;
- myinifile:Tinifile;
- Section: TStrings; // 存放ini中的節點資料 [exe] [file_1] [file_2] 這類節點標記
- Path: String;
- Temp, ScrCRC: inifmt;
- i: Integer;
- Buffer: TFileStream;
- HttpClient: TIdHttp;
- UpdateFileName: String;
- ScriptCRC, NewScript: TMemoryStream;
- begin
- Memo1.Clear;
- ScriptCRC:= TMemoryStream.Create;
- HttpClient:= TIdHttp.Create(nil);
- HttpClient.Get('http://patch.joypark.com.tw/cg_test/script_CRC.ini', ScriptCRC);
- ScriptCRC.Seek(0, 0);
- ScriptCRC.SaveToFile('Script_CRC.ini');
- ScriptCRC.Free;
- HttpClient.Free;
- Path:= Extractfilepath(Application.ExeName) + 'Script_CRC.ini';
- myinifile:= Tinifile.create(Path);
- BufferCRC:= myinifile.ReadString('CRC', 'crc','');
- myinifile.Free;
- RealCRC:= GetFile_CRC(Extractfilepath(Application.ExeName) + 'script.ini');
-
- if RealCRC = BufferCRC then
- begin
- Application.Terminate; // CRC值相同退出流程
- Exit;
- end;
- NewScript:= TMemoryStream.Create;
- HttpClient:= TIdHttp.Create(nil);
- HttpClient.Get('http://patch.joypark.com.tw/cg_test/script.ini', NewScript);
- NewScript.SaveToFile('script.ini');
- NewScript.Free;
- Path:= Extractfilepath(Application.ExeName) + 'script.ini';
- Section := TStringList.Create;
- myinifile:= Tinifile.create(Path);
- Section.Clear;
- Section.LoadFromFile(Path);
- myinifile.ReadSections(Section);
- // Memo1.Lines.Text:= Section.Text;
- progressbar1.Max:= Section.Count;
- for i := 1 to Section.Count-1 do
- begin
- Temp.path:= myinifile.ReadString(Section[i], 'path', '');
- Temp.filename:= myinifile.ReadString(Section[i], 'filename', '');
- Temp.url:= myinifile.ReadString(Section[i], 'url', '');
- Temp.size:= myinifile.ReadString(Section[i], 'size', '');
- Temp.crc:= myinifile.ReadString(Section[i], 'crc', '');
- Temp.method:= myinifile.ReadString(Section[i], 'method', '');
-
-
- if (Temp.method = '') or (Temp.method = 'replace') then
- begin
- if not (FileExists(ExtractFilePath(Application.ExeName) + Temp.path + Temp.filename)) then
- continue; // 如果文件不存在跳過該文件
-
- Buffer := TFileStream.Create(
- ExtractFilePath(Application.ExeName) + '\' + Temp.path + Temp.filename,
- fmOpenWrite or fmShareDenyNone);
-
-
- try
- HttpClient := TIdHttp.Create(nil);
- HttpClient.Get(Temp.url, Buffer); // wait until it is done
- finally
- HttpClient.Free;
- Buffer.Free;
- end;
- end
- else if Temp.method = 'append' then
- begin
- if not (FileExists(ExtractFilePath(Application.ExeName) + Temp.path + Temp.filename)) then
- continue; // 如果文件不存在跳過該文件
- Buffer := TFileStream.Create(
- ExtractFilePath(Application.ExeName) + Temp.path + Temp.filename, +
- fmOpenWrite or fmShareDenyNone);
-
- Buffer.Seek(0, soFromEnd); // append模式
- try
- HttpClient := TIdHttp.Create(nil);
- HttpClient.Get(Temp.url, Buffer);
- finally
- HttpClient.Free;
- Buffer.Free;
- UpdateFileName:= RightStr(Temp.url, Length(Temp.filename));
- RenameFile(ExtractFilePath(Application.ExeName) + Temp.path + Temp.filename,
- PCHAR(ExtractFilePath(Application.ExeName) + Temp.path + UpdateFileName));
- end;
- end;
- Memo1.Lines.Add('更新文件: ' + ExtractFilePath(Application.ExeName) + Temp.path + UpdateFileName);
- ProgressBar1.Position:= i + 1;
- end;
-
-
- myinifile.Free;
- Section.Free;
-
- end;
-
- function TForm1.GetFile_CRC(const iFileName: String): String;
- var
- crc: TIdHashCRC32;
- fileStream: TMemoryStream;
- begin
- crc:= TIdHashCRC32.Create;
- fileStream:= TMemoryStream.Create;
- try
- fileStream.LoadFromFile(iFileName);
- Result:= crc.HashStreamAsHex(fileStream);
- finally
- fileStream.Free;
- crc.Free;
- end;
- end;
-
- end.
-
复制代码
|