1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-30 12:52:16 -05:00

Worked on the GUI, fixed loading of script lists, left is installing/updating them

This commit is contained in:
Thomas Dunne 2011-07-08 13:38:34 +01:00
parent f2873b1f1c
commit ecd96be99c

View File

@ -75,29 +75,31 @@ type
TScriptManager = class (TObject) TScriptManager = class (TObject)
private private
FMaindir: string; FMaindir: string;
FScripts : TList; //Array of the online scripts FRScripts : TList; //Array of the online scripts
FLScripts: TList; //Array of the local scripts FLScripts: TList; //Array of the local scripts
FVersion : String; FRVersion : String;
FUpdating : boolean; FUpdating : boolean;
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 GetRScriptCount: integer;
procedure MatchLocalOnline; procedure MatchLocalOnline;
function GetLScriptByName(name: string): TLSimbaScript;
function isNewerVersion(ver1, ver2: string): boolean;
public public
function FindScriptByName(name : string) : Integer; function FindRScriptByName(name : string) : Integer;
function FindLScriptByName(name : string) : Integer; function FindLScriptByName(name : string) : Integer;
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;
procedure Update; //Gets the online scripts procedure RUpdate; //Gets the online scripts
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 InstallNewRScript(Script : integer); //Installs Script (Online -> Local)
function UpdateScript(Script : integer; ignoreupdating : boolean = false) : boolean; //Updates all the info/files of local script function UpdateLScript(Script : integer; ignoreupdating : boolean = false) : boolean; //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 RScriptCount : integer read GetRScriptCount; //Online script
property Version : string read FVersion; property Version : string read FRVersion;
constructor Create; constructor Create;
destructor Destroy; override; destructor Destroy; override;
end; end;
@ -188,16 +190,18 @@ end;
procedure TForm1.Button1Click(Sender: TObject); procedure TForm1.Button1Click(Sender: TObject);
var var
i : integer; i, l: integer;
Item : TListItem; Item : TListItem;
begin begin
Mng.Update; Mng.LUpdate;
Mng.RUpdate;
ListView1.Items.Clear; ListView1.Items.Clear;
for i := 0 to Mng.ScriptCount - 1 do for i := 0 to Mng.RScriptCount - 1 do
begin begin
Item := ListView1.Items.Add; Item := ListView1.Items.Add;
Item.Data:= Mng.SimbaScript[i]; Item.Data := Mng.SimbaScript[i];
Item.Caption:= Mng.SimbaScript[i].Name; Item.Caption := Mng.SimbaScript[i].Name;
Item.SubItems.Add(Mng.SimbaScript[i].Author);
end; end;
end; end;
@ -208,7 +212,10 @@ begin
if (ListView1.Selected <> nil) and (ListView1.Selected.Data <> nil) then if (ListView1.Selected <> nil) and (ListView1.Selected.Data <> nil) then
begin begin
Script := TSimbaScript(ListView1.Selected.Data); Script := TSimbaScript(ListView1.Selected.Data);
Mng.InstallNewScript(mng.FindScriptByName(Script.Name)); // if Script.IsInstalled then
// Mng.UpdateLScript(mng.FindRScriptByName(Script.Name))
// else
Mng.InstallNewRScript(mng.FindRScriptByName(Script.Name));
end; end;
end; end;
@ -237,7 +244,10 @@ procedure TSimbaScript.LoadFromNode(Script: TDOMNode);
begin begin
Result := TStringList.Create; Result := TStringList.Create;
if node = nil then if node = nil then
begin
Result.Free;
exit; exit;
end;
tmpNode := node.FindNode(itemstr); tmpNode := node.FindNode(itemstr);
if tmpNode <> nil then if tmpNode <> nil then
begin begin
@ -295,12 +305,25 @@ end;
{ TScriptManager } { TScriptManager }
function TScriptManager.FindScriptByName(name: string): Integer; function TScriptManager.GetLScriptByName(name: string): TLSimbaScript;
var
I : integer;
Scr : TLSimbaScript;
begin
result := nil;
for i := FLScripts.Count - 1 downto 0 do
scr := TLSimbaScript(FLScripts[i]);
if scr.Name = Name then
result := scr;
exit();
end;
function TScriptManager.FindRScriptByName(name: string): Integer;
var var
I : integer; I : integer;
begin begin
for i := FScripts.Count - 1 downto 0 do for i := FRScripts.Count - 1 downto 0 do
if TSimbaScript(FScripts[i]).Name = Name then if TSimbaScript(FRScripts[i]).Name = Name then
exit(i); exit(i);
result := -1; result := -1;
end; end;
@ -309,7 +332,7 @@ function TScriptManager.FindLScriptByName(name: string): Integer;
var var
I : integer; I : integer;
begin begin
for i := FScripts.Count - 1 downto 0 do for i := FLScripts.Count - 1 downto 0 do
if TLSimbaScript(FLScripts[i]).Name = Name then if TLSimbaScript(FLScripts[i]).Name = Name then
exit(i); exit(i);
result := -1; result := -1;
@ -327,12 +350,12 @@ end;
function TScriptManager.GetScript(index : integer): TSimbaScript; function TScriptManager.GetScript(index : integer): TSimbaScript;
begin begin
result := TSimbaScript(FScripts[index]); result := TSimbaScript(FRScripts[index]);
end; end;
function TScriptManager.GetScriptCount: integer; function TScriptManager.GetRScriptCount: integer;
begin begin
result := FScripts.Count; result := FRScripts.Count;
end; end;
procedure TScriptManager.MatchLocalOnline; procedure TScriptManager.MatchLocalOnline;
@ -344,17 +367,17 @@ begin
begin begin
Scrpt := TLSimbaScript(FLScripts[ii]); Scrpt := TLSimbaScript(FLScripts[ii]);
if Scrpt.OnlineScript = nil then if Scrpt.OnlineScript = nil then
for i := 0 to ScriptCount-1 do for i := 0 to RScriptCount-1 do
if TSimbaScript(FScripts[i]).Name = Scrpt.Name then if TSimbaScript(FRScripts[i]).Name = Scrpt.Name then
begin begin
Scrpt.OnlineScript := TSimbaScript(FScripts[i]); Scrpt.OnlineScript := TSimbaScript(FRScripts[i]);
Break; Break;
end; end;
Scrpt.OnlineScript.LocalScript := Scrpt; Scrpt.OnlineScript.LocalScript := Scrpt;
end; end;
end; end;
procedure TScriptManager.Update; procedure TScriptManager.RUpdate;
var var
XMLFile : string; XMLFile : string;
Stream : TStringStream; Stream : TStringStream;
@ -363,12 +386,14 @@ var
Subs : TStringList; Subs : TStringList;
Down : TDownloadThread; Down : TDownloadThread;
SScript : TSimbaScript; SScript : TSimbaScript;
LScr : TLSimbaScript;
I : integer; I : integer;
begin begin
if FUpdating then if FUpdating then
exit; exit;
FUpdating := True; FUpdating := True;
Down := TDownloadThread.Create('http://old.villavu.com/sm',@XMLFile); // Down := TDownloadThread.Create('http://old.villavu.com/sm',@XMLFile);
Down := TDownloadThread.Create('http://tootoot222.hopto.org:8080/~mcteo/scriptman/scripts.xml',@XMLFile);
down.Execute; down.Execute;
while down.Done = false do while down.Done = false do
begin begin
@ -380,7 +405,7 @@ begin
Stream.Free; Stream.Free;
Node := XMLDoc.FirstChild.FindNode('Version'); Node := XMLDoc.FirstChild.FindNode('Version');
if node <> nil then if node <> nil then
FVersion:= Node.TextContent; FRVersion:= Node.TextContent;
Node := XMLDoc.FirstChild.FindNode('ScriptList'); Node := XMLDoc.FirstChild.FindNode('ScriptList');
if node <> nil then if node <> nil then
begin begin
@ -390,26 +415,41 @@ begin
SScript := TSimbaScript.Create; SScript := TSimbaScript.Create;
SScript.LoadFromNode(Script); SScript.LoadFromNode(Script);
SScript.Dbg; SScript.Dbg;
i := FindScriptByName(SScript.Name); i := FindRScriptByName(SScript.Name);
if (i = -1) then if (i = -1) then // if no remote scripts with same name
FScripts.Add(SScript) begin
else i := FindLScriptByName(SScript.Name);
if (i = -1) then // if no local scripts with same name
begin
FRScripts.Add(SScript);
end else // there is a local script with same name
begin
LScr := GetLScriptByName(SScript.Name);
if (LScr <> nil) then
begin
if isNewerVersion(SScript.Version, LScr.Version) then // if local script is older
begin
FRScripts.Add(SScript); // add version of script
end;
end;
end;
end else
begin begin
SScript.free; SScript.free;
TSimbaScript(FScripts[i]).LoadFromNode(Script); // TSimbaScript(FScripts[i]).LoadFromNode(Script);
end; end;
Script := Script.NextSibling; Script := Script.NextSibling;
end; end;
end; end;
XMLDoc.Free; XMLDoc.Free;
FUpdating := false; FUpdating := false;
MatchLocalOnline; // MatchLocalOnline;
end; end;
procedure TScriptManager.LUpdate; procedure TScriptManager.LUpdate;
var var
XMLDoc : TXMLDocument; XMLDoc : TXMLDocument;
Node,Script : TDOMNode; Node,Script,tmpNode : TDOMNode;
Subs : TStringList; Subs : TStringList;
Down : TDownloadThread; Down : TDownloadThread;
SScript : TLSimbaScript; SScript : TLSimbaScript;
@ -420,29 +460,45 @@ begin
if FileExists(maindir + 'General' + DirectorySeparator+ 'scripts.xml') then if FileExists(maindir + 'General' + DirectorySeparator+ 'scripts.xml') then
begin begin
ReadXMLFile(XMLDoc,maindir + 'General' + DirectorySeparator+ 'scripts.xml'); ReadXMLFile(XMLDoc,maindir + 'General' + DirectorySeparator+ 'scripts.xml');
Node := XMLDoc.FirstChild.FindNode('Scripts'); Node := XMLDoc.FirstChild.FindNode('ScriptList');
// Node := XMLDoc.FindNode('ScriptList');
if node <> nil then if node <> nil then
begin begin
script := Node.FirstChild; script := Node.FirstChild;
while Script <> nil do while script <> nil do
begin begin
SScript := TLSimbaScript.Create; SScript := TLSimbaScript.Create;
SScript.LoadFromName(Node.TextContent,maindir); tmpNode := script.FindNode('Name');
if tmpNode <> nil then
begin
SScript.LoadFromName(Trim(tmpNode.TextContent), maindir);
i := FindLScriptByName(SScript.Name); i := FindLScriptByName(SScript.Name);
SScript.Dbg; // SScript.Dbg;
if (i = -1) then if (i = -1) then
FLScripts.Add(SScript) begin
FLScripts.Add(SScript);
end
else else
begin begin
SScript.free; SScript.free;
TLSimbaScript(FLScripts[i]).LoadFromName(Node.TextContent,maindir); TLSimbaScript(FLScripts[i]).LoadFromName(Node.TextContent,maindir);
end; end;
Script := Script.NextSibling; end;
script := script.NextSibling;
end; end;
end; end;
XMLDoc.Free; XMLDoc.Free;
end; end;
MatchLocalOnline; // MatchLocalOnline;
end;
function TScriptManager.isNewerVersion(ver1, ver2: string): boolean;
var
v1, v2: Extended;
begin
v1 := StrToFloat(ver1);
v2 := StrToFloat(ver2);
result := (v1 > v2);
end; end;
function TScriptManager.NewVersion(Script: integer): boolean; function TScriptManager.NewVersion(Script: integer): boolean;
@ -452,7 +508,7 @@ begin
result := OnlineScript.Version <> Version; result := OnlineScript.Version <> Version;
end; end;
procedure TScriptManager.InstallNewScript(Script: integer); procedure TScriptManager.InstallNewRScript(Script: integer);
var var
Scrpt : TSimbaScript; Scrpt : TSimbaScript;
LScrpt: TLSimbaScript; LScrpt: TLSimbaScript;
@ -461,7 +517,7 @@ begin
if FUpdating then if FUpdating then
exit; exit;
FUpdating := true; FUpdating := true;
Scrpt := TSimbaScript(FScripts[Script]); Scrpt := TSimbaScript(FRScripts[Script]);
LScrpt := TLSimbaScript.create; LScrpt := TLSimbaScript.create;
FLScripts.Add(LScrpt); FLScripts.Add(LScrpt);
LScrpt.Name:= Scrpt.Name; LScrpt.Name:= Scrpt.Name;
@ -471,10 +527,10 @@ 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,true); UpdateLScript(FLScripts.Count - 1,true);
end; end;
function TScriptManager.UpdateScript(Script: integer; ignoreupdating : boolean = false) : boolean; function TScriptManager.UpdateLScript(Script: integer; ignoreupdating : boolean = false) : boolean;
var var
LScrpt : TLSimbaScript; LScrpt : TLSimbaScript;
Scrpt : TSimbaScript; Scrpt : TSimbaScript;
@ -493,10 +549,11 @@ begin
Version:= Scrpt.Version; Version:= Scrpt.Version;
Name:= Scrpt.Name; Name:= Scrpt.Name;
Author := Scrpt.Author; Author := Scrpt.Author;
Description:= Scrpt.Version; Description:= Scrpt.Description;
Tags.Assign(Scrpt.Tags); Tags.Assign(Scrpt.Tags);
Files.Assign(Scrpt.Files); Files.Assign(Scrpt.Files);
URL := 'http://old.villavu.com/sm/scripts/'+name+ '.tar.bz2'; // URL := 'http://old.villavu.com/sm/scripts/'+name+ '.tar.bz2';
URL := 'http://tootoot222.hopto.org:8080/~mcteo/scriptman/'+name+'.tar.bz2';
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
DownloadThread := TDownloadDecompressThread.Create(LScrpt.URL,MainDir + LScrpt.Name + DS,true); DownloadThread := TDownloadDecompressThread.Create(LScrpt.URL,MainDir + LScrpt.Name + DS,true);
@ -524,16 +581,18 @@ begin
node.TextContent:= Text; node.TextContent:= Text;
end; end;
var var
Node : TDOMNode; Node, ChildNode : TDOMNode;
i : integer; i : integer;
begin begin
if DirectoryExists(MainDir) = false then if DirectoryExists(MainDir) = false then
exit; exit;
XMLDoc := TXMLDocument.Create; XMLDoc := TXMLDocument.Create;
Node := XMLDoc.CreateElement('Scripts'); Node := XMLDoc.CreateElement('ScriptList');
XMLDoc.AppendChild(node); XMLDoc.AppendChild(node);
for i := 0 to FLScripts.Count - 1 do for i := 0 to FLScripts.Count - 1 do
AddTextElement(node,'Script', TLSimbaScript(FLScripts[i]).Name); ChildNode := XMLDoc.CreateElement('Script');
AddTextElement(childnode,'Name', TLSimbaScript(FLScripts[i]).Name);
Node.AppendChild(ChildNode);
WriteXMLFile(XMLDoc,maindir + 'General' + DirectorySeparator+ 'scripts.xml'); WriteXMLFile(XMLDoc,maindir + 'General' + DirectorySeparator+ 'scripts.xml');
XMLDoc.Free; XMLDoc.Free;
end; end;
@ -542,8 +601,8 @@ constructor TScriptManager.Create;
begin begin
inherited; inherited;
FLScripts := TList.Create; FLScripts := TList.Create;
FScripts := TList.Create; FRScripts := TList.Create;
FVersion := ''; FRVersion := '';
FUpdating:= False; FUpdating:= False;
FMainDir:= ExtractFileDir(Application.ExeName); FMainDir:= ExtractFileDir(Application.ExeName);
CreateDir(MainDir + 'General'); CreateDir(MainDir + 'General');
@ -551,10 +610,10 @@ end;
destructor TScriptManager.Destroy; destructor TScriptManager.Destroy;
begin begin
while FScripts.Count > 0 do while FRScripts.Count > 0 do
begin begin
TSimbaScript(FScripts[0]).Free; TSimbaScript(FRScripts[0]).Free;
FScripts.Delete(0); FRScripts.Delete(0);
end; end;
while FLScripts.Count > 0 do while FLScripts.Count > 0 do
begin begin
@ -574,15 +633,15 @@ begin
XMLDoc.Free; XMLDoc.Free;
end; end;
function TLSimbaScript.LoadFromName(const ScriptName,MainDir : string) : boolean; function TLSimbaScript.LoadFromName(const ScriptName, MainDir : string) : boolean;
begin begin
Result := false; Result := false;
if FileExists(MainDir + 'General' + DirectorySeparator + ScriptName + if FileExists(MainDir + 'General' + DirectorySeparator + ScriptName +
'info.xml') then DirectorySeparator + 'info.xml') then
begin begin
Result := true; Result := true;
LoadFromFile(MainDir + 'General' + DirectorySeparator + ScriptName + LoadFromFile(MainDir + 'General' + DirectorySeparator + ScriptName +
'info.xml'); DirectorySeparator + 'info.xml');
end; end;
end; end;
@ -609,7 +668,7 @@ begin
AddTextElement(node,'Name',Name); AddTextElement(node,'Name',Name);
AddTextElement(node,'Author',Author); AddTextElement(node,'Author',Author);
AddTextElement(node,'Version',Version); AddTextElement(node,'Version',Version);
AddTextElement(node,'Description',description); AddTextElement(node,'Description',Description);
AddTextElement(node,'AutoCheckUpdates', BoolToStr(AutoCheckUpdates,true)); AddTextElement(node,'AutoCheckUpdates', BoolToStr(AutoCheckUpdates,true));
SubNode := XMLDoc.CreateElement('Tags'); SubNode := XMLDoc.CreateElement('Tags');
Node.AppendChild(SubNode); Node.AppendChild(SubNode);