ScriptManager: Improvements by Raymond.

This commit is contained in:
Merlijn Wajer 2010-12-05 17:42:41 +01:00
parent 02da77c417
commit 19c37f70be
1 changed files with 94 additions and 55 deletions

View File

@ -1,6 +1,6 @@
{ {
This file is part of the Simba Project This file is part of the Simba Project
Copyright (c) 2009 by Raymond van Venetië and Merlijn Wajer Copyright (c) 2009 by Raymond van Venetië and Merlijn Wajer
Simba is free software: you can redistribute it and/or modify Simba is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -15,8 +15,8 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with MML. If not, see <http://www.gnu.org/licenses/>. along with MML. If not, see <http://www.gnu.org/licenses/>.
See the file COPYING, included in this distribution, See the file COPYING, included in this distribution,
for details about the copyright. for details about the copyright.
Script Manager for the Simba project. Script Manager for the Simba project.
} }
@ -45,10 +45,13 @@ type
TSimbaScript = class(TObject) TSimbaScript = class(TObject)
private private
function IsInstalled: boolean;
procedure LoadFromNode( Script : TDOMNode); procedure LoadFromNode( Script : TDOMNode);
public public
Name, Version, Author, Description: String; Name, Version, Author, Description: String;
Tags, Files: TStringList; Tags, Files: TStringList;
LocalScript : TSimbaScript;
property Installed : boolean read IsInstalled;
procedure Dbg; procedure Dbg;
constructor Create; constructor Create;
destructor Destroy; override; destructor Destroy; override;
@ -76,10 +79,13 @@ type
FLScripts: TList; //Array of the local scripts FLScripts: TList; //Array of the local scripts
FVersion : String; FVersion : String;
FUpdating : boolean; FUpdating : boolean;
function FindScriptByName(name : string) : Integer;
function FindLScriptByName(name : string) : Integer;
function GetLScriptCount: integer; function GetLScriptCount: integer;
function GetMainDir: string; function GetMainDir: string;
function GetScript(index : integer): TSimbaScript; function GetScript(index : integer): TSimbaScript;
function GetScriptCount: integer; function GetScriptCount: integer;
procedure MatchLocalOnline;
public public
property MainDir : string read GetMainDir write FMaindir; property MainDir : string read GetMainDir write FMaindir;
property SimbaScript[index : integer] : TSimbaScript read GetScript; property SimbaScript[index : integer] : TSimbaScript read GetScript;
@ -87,7 +93,7 @@ type
procedure LUpdate; //Loads the local scripts, uses MainDir procedure LUpdate; //Loads the local scripts, uses MainDir
function NewVersion(Script : integer) : boolean; //Checks for updates for Script function NewVersion(Script : integer) : boolean; //Checks for updates for Script
procedure InstallNewScript(Script : integer); //Installs Script (Online -> Local) procedure InstallNewScript(Script : integer); //Installs Script (Online -> Local)
procedure UpdateScript(Script : integer); //Updates all the info/files of local script procedure UpdateScript(Script : integer; ignoreupdating : boolean = false); //Updates all the info/files of local script
procedure LSave; //Saves the local scripts, uses MainDir procedure LSave; //Saves the local scripts, uses MainDir
property LScriptCount : integer read GetLScriptCount; //LScript = Local Script = Installed Script property LScriptCount : integer read GetLScriptCount; //LScript = Local Script = Installed Script
property ScriptCount : integer read GetScriptCount; //Online script property ScriptCount : integer read GetScriptCount; //Online script
@ -128,34 +134,6 @@ uses
{$R *.lfm} {$R *.lfm}
{ TForm1 } { TForm1 }
procedure fill(s: TMMLSettings);
var
i:integer;
ss: TSimbaScript;
LI: TListItem;
strarr: TStringArray;
b: TButton;
begin
if not s.ListKeys('Scripts/ScriptList', strarr) then
writeln('ListKeys returned false');
writeln('strarr length: ' + inttostr(length(strarr)));
for i := 0 to high(strarr) do
begin
writeln(s.GetKeyValue('Scripts/ScriptList/Script/Name'));
ss := TSimbaScript.Create();
ss.Name := s.GetKeyValue('Scripts/ScriptList/Script/Name');
ss.Author:= s.GetKeyValue('Scripts/ScriptList/Script/Author');
ss.Description:= s.GetKeyValue('Scripts/ScriptList/Script/Description');
LI := Form1.ListView1.Items.Add;
LI.Caption := ss.Name;
LI.Data := ss;
LI.ImageIndex:= 0;
s.DeleteKey('Scripts/ScriptList/Script');
end;
end;
procedure TForm1.FormCreate(Sender: TObject); procedure TForm1.FormCreate(Sender: TObject);
begin begin
Mng := TScriptManager.Create; Mng := TScriptManager.Create;
@ -171,10 +149,11 @@ begin
begin begin
Memo1.Clear; Memo1.Clear;
Script := TSimbaScript(Item.data); Script := TSimbaScript(Item.data);
Memo1.Lines.Add('Name: ' + Script.Name); Memo1.Lines.add('Name: ' + Script.Name);
Memo1.lines.add('Author: ' + Script.Author); Memo1.lines.add('Author: ' + Script.Author);
Memo1.Lines.add('Version: ' + Script.Version); Memo1.Lines.add('Version: ' + Script.Version);
Memo1.Lines.Add('Description: ' + Script.Description); Memo1.Lines.add('Installed: '+ BoolToStr(Script.Installed,true));
Memo1.Lines.add('Description: ' + Script.Description);
end; end;
end; end;
@ -221,6 +200,11 @@ end;
{ TSimbaScript } { TSimbaScript }
function TSimbaScript.IsInstalled: boolean;
begin
Result := (LocalScript <> nil);
end;
procedure TSimbaScript.LoadFromNode(Script: TDOMNode); procedure TSimbaScript.LoadFromNode(Script: TDOMNode);
function NodeContents(ItemStr : string; node : TDOMNode) : string; function NodeContents(ItemStr : string; node : TDOMNode) : string;
var var
@ -268,6 +252,7 @@ begin
Writeln(' Author: ' + Author); Writeln(' Author: ' + Author);
Writeln(' Version: ' + Version); Writeln(' Version: ' + Version);
Writeln(' Description: ' + Description); Writeln(' Description: ' + Description);
Writeln(' Installed: '+ BoolToStr(Installed,true));
Writeln(' Tags:'); Writeln(' Tags:');
for i := 0 to Tags.Count - 1 do for i := 0 to Tags.Count - 1 do
Writeln(' ' + Tags[i]); Writeln(' ' + Tags[i]);
@ -279,7 +264,6 @@ end;
constructor TSimbaScript.Create; constructor TSimbaScript.Create;
begin begin
inherited; inherited;
{stuff here} {stuff here}
end; end;
@ -296,6 +280,26 @@ end;
{ TScriptManager } { TScriptManager }
function TScriptManager.FindScriptByName(name: string): Integer;
var
I : integer;
begin
for i := FScripts.Count - 1 downto 0 do
if TSimbaScript(FScripts[i]).Name = Name then
exit(i);
result := -1;
end;
function TScriptManager.FindLScriptByName(name: string): Integer;
var
I : integer;
begin
for i := FScripts.Count - 1 downto 0 do
if TLSimbaScript(FLScripts[i]).Name = Name then
exit(i);
result := -1;
end;
function TScriptManager.GetLScriptCount: integer; function TScriptManager.GetLScriptCount: integer;
begin begin
result := FLScripts.Count; result := FLScripts.Count;
@ -316,6 +320,25 @@ begin
result := FScripts.Count; result := FScripts.Count;
end; end;
procedure TScriptManager.MatchLocalOnline;
var
Scrpt : TLSimbaScript;
I,II : integer;
begin
for ii := 0 to LScriptCount - 1 do
begin
Scrpt := TLSimbaScript(FLScripts[ii]);
if Scrpt.OnlineScript = nil then
for i := 0 to ScriptCount-1 do
if TSimbaScript(FScripts[i]).Name = Scrpt.Name then
begin
Scrpt.OnlineScript := TSimbaScript(FScripts[i]);
Break;
end;
Scrpt.OnlineScript.LocalScript := Scrpt;
end;
end;
procedure TScriptManager.Update; procedure TScriptManager.Update;
var var
XMLFile : string; XMLFile : string;
@ -325,6 +348,7 @@ var
Subs : TStringList; Subs : TStringList;
Down : TDownloadThread; Down : TDownloadThread;
SScript : TSimbaScript; SScript : TSimbaScript;
I : integer;
begin begin
if FUpdating then if FUpdating then
exit; exit;
@ -350,13 +374,21 @@ begin
begin begin
SScript := TSimbaScript.Create; SScript := TSimbaScript.Create;
SScript.LoadFromNode(Script); SScript.LoadFromNode(Script);
FScripts.Add(SScript);
SScript.Dbg; SScript.Dbg;
i := FindScriptByName(SScript.Name);
if (i = -1) then
FScripts.Add(SScript)
else
begin
SScript.free;
TSimbaScript(FScripts[i]).LoadFromNode(Script);
end;
Script := Script.NextSibling; Script := Script.NextSibling;
end; end;
end; end;
XMLDoc.Free; XMLDoc.Free;
FUpdating := false; FUpdating := false;
MatchLocalOnline;
end; end;
procedure TScriptManager.LUpdate; procedure TScriptManager.LUpdate;
@ -366,6 +398,7 @@ var
Subs : TStringList; Subs : TStringList;
Down : TDownloadThread; Down : TDownloadThread;
SScript : TLSimbaScript; SScript : TLSimbaScript;
I : integer;
begin begin
if DirectoryExists(MainDir) = false then if DirectoryExists(MainDir) = false then
exit; exit;
@ -380,29 +413,28 @@ begin
begin begin
SScript := TLSimbaScript.Create; SScript := TLSimbaScript.Create;
SScript.LoadFromName(Node.TextContent,maindir); SScript.LoadFromName(Node.TextContent,maindir);
FLScripts.Add(SScript); i := FindLScriptByName(SScript.Name);
SScript.Dbg; SScript.Dbg;
if (i = -1) then
FLScripts.Add(SScript)
else
begin
SScript.free;
TLSimbaScript(FLScripts[i]).LoadFromName(Node.TextContent,maindir);
end;
Script := Script.NextSibling; Script := Script.NextSibling;
end; end;
end; end;
XMLDoc.Free; XMLDoc.Free;
end; end;
MatchLocalOnline;
end; end;
function TScriptManager.NewVersion(Script: integer): boolean; function TScriptManager.NewVersion(Script: integer): boolean;
var
Scrpt : TLSimbaScript;
I : integer;
begin begin
Scrpt := TLSimbaScript(FLScripts[Script]); MatchLocalOnline;
if Scrpt.OnlineScript = nil then with TLSimbaScript(FLScripts[Script]) do
for i := 0 to ScriptCount-1 do result := OnlineScript.Version <> Version;
if TSimbaScript(FScripts[i]).Name = Scrpt.Name then
begin
Scrpt.OnlineScript := TSimbaScript(FScripts[i]);
Break;
end;
result := Scrpt.OnlineScript.Version <> Scrpt.Version;
end; end;
procedure TScriptManager.InstallNewScript(Script: integer); procedure TScriptManager.InstallNewScript(Script: integer);
@ -411,6 +443,9 @@ var
LScrpt: TLSimbaScript; LScrpt: TLSimbaScript;
Dir : string; Dir : string;
begin begin
if FUpdating then
exit;
FUpdating := true;
Scrpt := TSimbaScript(FScripts[Script]); Scrpt := TSimbaScript(FScripts[Script]);
LScrpt := TLSimbaScript.create; LScrpt := TLSimbaScript.create;
FLScripts.Add(LScrpt); FLScripts.Add(LScrpt);
@ -421,17 +456,21 @@ begin
Writeln('Directory already exists, yet continue?'); Writeln('Directory already exists, yet continue?');
if not CreateDir(Dir) then if not CreateDir(Dir) then
Writeln('Failed to create dir..'); Writeln('Failed to create dir..');
UpdateScript(FLScripts.Count - 1); UpdateScript(FLScripts.Count - 1,true);
end; end;
procedure TScriptManager.UpdateScript(Script: integer); procedure TScriptManager.UpdateScript(Script: integer; ignoreupdating : boolean = false);
var var
LScrpt : TLSimbaScript; LScrpt : TLSimbaScript;
Scrpt : TSimbaScript; Scrpt : TSimbaScript;
DownloadThread : TDownloadThread;
begin begin
LScrpt := TLSimbaScript(FLScripts[Script]);
if not NewVersion(Script) then if not NewVersion(Script) then
Exit; Exit;
if FUpdating and not ignoreupdating then
exit;
FUpdating := true;
LScrpt := TLSimbaScript(FLScripts[Script]);
Scrpt := LScrpt.OnlineScript; Scrpt := LScrpt.OnlineScript;
with LScrpt do with LScrpt do
begin begin
@ -444,6 +483,8 @@ begin
end; end;
LScrpt.Save(MainDir); //Saves the setting file, now we only need to update the files LScrpt.Save(MainDir); //Saves the setting file, now we only need to update the files
//Download files & write to folder //Download files & write to folder
FUPdating := false;
end; end;
procedure TScriptManager.LSave; procedure TScriptManager.LSave;
@ -497,7 +538,6 @@ begin
end; end;
{ TLSimbaScript } { TLSimbaScript }
procedure TLSimbaScript.LoadFromFile(const filename: string); procedure TLSimbaScript.LoadFromFile(const filename: string);
var var
XMLDoc : TXMLDocument; XMLDoc : TXMLDocument;
@ -571,4 +611,3 @@ begin
end; end;
end. end.