1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-25 10:42:20 -05:00

Added settings to the extensions.. Should work allright, however there is some bug in Pascalscript that makes it fail, for now..

Renamed all the setting methods..
This commit is contained in:
Raymond 2010-04-01 00:22:46 +02:00
parent bd27789eb2
commit 001a593789
11 changed files with 318 additions and 167 deletions

View File

@ -12,6 +12,7 @@ end;
procedure Attach; procedure Attach;
begin; begin;
Settings.SetKeyValue('TestSetting','1337');
Writeln('Your extension has been enabled, do stuff here'); Writeln('Your extension has been enabled, do stuff here');
end; end;

View File

@ -28,7 +28,7 @@ type
StartDisabled : boolean; StartDisabled : boolean;
property OnChange : TNotifyEvent read FOnChange write SetOnchange; property OnChange : TNotifyEvent read FOnChange write SetOnchange;
function GetExtensionIndex(Filename : string) : integer; function GetExtensionIndex(Filename : string) : integer;
function LoadPSExtension(Filename : string) : boolean; function LoadPSExtension(Filename : string; enabled : boolean=false) : boolean;
function LoadPSExtensionsDir(Directory,ext : string) : boolean; function LoadPSExtensionsDir(Directory,ext : string) : boolean;
function HandleHook(HookName: String; Args: Array of Variant): Variant; function HandleHook(HookName: String; Args: Array of Variant): Variant;
end; end;
@ -38,7 +38,7 @@ var
implementation implementation
uses uses
TestUnit; TestUnit, settingssandbox,simbasettings;
procedure TExtensionManager.SetOnchange(const AValue: TNotifyEvent); procedure TExtensionManager.SetOnchange(const AValue: TNotifyEvent);
var var
@ -76,7 +76,7 @@ begin
result := -1; result := -1;
end; end;
function TExtensionManager.LoadPSExtension(Filename: string): boolean; function TExtensionManager.LoadPSExtension(Filename: string; enabled: boolean): boolean;
var var
Ext : TExtension; Ext : TExtension;
begin begin
@ -84,12 +84,17 @@ begin
exit(true); exit(true);
Result := False; Result := False;
try try
Ext := TSimbaPSExtension.Create(filename,startdisabled); Ext := TSimbaPSExtension.Create(filename,True);
result := TSimbaPSExtension(ext).Working; // result := TSimbaPSExtension(ext).Working;
Extensions.Add(ext); Extensions.Add(ext);
ext.Settings := TMMLSettingsSandbox.Create(SettingsForm.Settings);
ext.Settings.Prefix := format('Extensions/Extension%d/Settings/',[Extensions.Count - 1]);
if enabled then
ext.Enabled := true;
ext.OnChange:= FOnChange; ext.OnChange:= FOnChange;
if assigned(FOnChange) then if assigned(FOnChange) then
FOnChange(Self); FOnChange(Self);
Result := True;
except except
on e : exception do on e : exception do
formWritelnex(format('Error in LoadPSExtension(%s): %s',[FileName, e.message])); formWritelnex(format('Error in LoadPSExtension(%s): %s',[FileName, e.message]));
@ -98,22 +103,22 @@ end;
function GetFiles(Path, Ext: string): TstringArray; function GetFiles(Path, Ext: string): TstringArray;
var var
SearchRec : TSearchRec; SearchRec : TSearchRec;
c : integer; c : integer;
begin begin
c := 0; c := 0;
if FindFirst(Path + '*.' + ext, faAnyFile, SearchRec) = 0 then if FindFirst(Path + '*.' + ext, faAnyFile, SearchRec) = 0 then
begin begin
repeat repeat
inc(c); inc(c);
SetLength(Result,c); SetLength(Result,c);
Result[c-1] := SearchRec.Name; Result[c-1] := SearchRec.Name;
until FindNext(SearchRec) <> 0; until FindNext(SearchRec) <> 0;
SysUtils.FindClose(SearchRec); SysUtils.FindClose(SearchRec);
end; end;
end; end;
function TExtensionManager.LoadPSExtensionsDir(Directory,ext: string): boolean; function TExtensionManager.LoadPSExtensionsDir(Directory, ext: string): boolean;
var var
Files : TstringArray; Files : TstringArray;
i : integer; i : integer;
@ -127,7 +132,7 @@ begin
Directory := IncludeTrailingPathDelimiter(directory); Directory := IncludeTrailingPathDelimiter(directory);
Files := GetFiles(Directory,ext); Files := GetFiles(Directory,ext);
for i := 0 to high(Files) do for i := 0 to high(Files) do
result := result or LoadPSExtension(Directory + files[i]); result := LoadPSExtension(Directory + files[i],not StartDisabled) or result;
FOnChange := Tempevent; FOnChange := Tempevent;
if Assigned(FOnChange) then if Assigned(FOnChange) then
FOnChange(self); FOnChange(self);

View File

@ -27,6 +27,8 @@ type
function FreeScript: boolean; function FreeScript: boolean;
function InitScript: Boolean; function InitScript: Boolean;
procedure OutputMessages; procedure OutputMessages;
procedure SIRegister_Settings(Cl: TPSPascalCompiler);
procedure RIRegister_Settings(Cl: TPSRuntimeClassImporter);
public public
function HookExists(HookName: String): Boolean;override; function HookExists(HookName: String): Boolean;override;
@ -47,7 +49,7 @@ uses
uPSC_extctrls,uPSC_menus, //Compile libs uPSC_extctrls,uPSC_menus, //Compile libs
uPSR_std, uPSR_controls,uPSR_classes,uPSR_graphics,uPSR_stdctrls,uPSR_forms, uPSR_std, uPSR_controls,uPSR_classes,uPSR_graphics,uPSR_stdctrls,uPSR_forms,
uPSR_extctrls,uPSR_menus, //Runtime-libs uPSR_extctrls,uPSR_menus, //Runtime-libs
testunit//Writeln testunit,updateform,settingssandbox//Writeln
; ;
function TSimbaPSExtension.HookExists(HookName: String): Boolean; function TSimbaPSExtension.HookExists(HookName: String): Boolean;
@ -110,10 +112,13 @@ begin
result := ExecuteHook('Free',[],bla) = SExt_ok; result := ExecuteHook('Free',[],bla) = SExt_ok;
end; end;
{$I ../../Units/MMLAddon/PSInc/Wrappers/extensions.inc}
procedure TSimbaPSExtension.RegisterMyMethods(Sender: TPSScript); procedure TSimbaPSExtension.RegisterMyMethods(Sender: TPSScript);
begin begin
Sender.AddFunction(@formWritelnEx,'procedure Writeln(s : string)'); Sender.AddFunction(@formWritelnEx,'procedure Writeln(s : string)');
Sender.AddFunction(@ext_GetPage,'function GetPage(url : string) : string');
Sender.AddRegisteredPTRVariable('Settings','TMMLSettingsSandbox');
Sender.AddRegisteredVariable('Simba','TForm'); Sender.AddRegisteredVariable('Simba','TForm');
Sender.AddRegisteredVariable('Simba_MainMenu','TMainMenu'); Sender.AddRegisteredVariable('Simba_MainMenu','TMainMenu');
end; end;
@ -122,6 +127,7 @@ procedure TSimbaPSExtension.OnPSExecute(Sender: TPSScript);
begin begin
Sender.SetVarToInstance('simba',Form1); Sender.SetVarToInstance('simba',Form1);
Sender.SetVarToInstance('Simba_MainMenu',Form1.MainMenu); Sender.SetVarToInstance('Simba_MainMenu',Form1.MainMenu);
Sender.SetPointerToData('Settings',@Self.Settings,Sender.FindNamedType('TMMLSettingsSandbox'));
end; end;
procedure TSimbaPSExtension.SetEnabled(bool: boolean); procedure TSimbaPSExtension.SetEnabled(bool: boolean);
@ -148,6 +154,41 @@ begin
inherited SetEnabled(bool); inherited SetEnabled(bool);
end; end;
procedure TSimbaPSExtension.SIRegister_Settings(Cl: TPSPascalCompiler);
begin
with cl.AddClassN(nil,'TMMLSettingsSandbox') do
begin;
RegisterMethod('function IsKey(KeyName: String): Boolean;');
RegisterMethod('function IsDirectory(KeyName: String): Boolean;');
RegisterMethod('function SetKeyValue(Keyname : string; Value : string) : boolean;');
RegisterMethod('function GetKeyValue(KeyName: String): String;');
RegisterMethod('function GetKeyValueDef(KeyName, defVal: String): String;');
RegisterMethod('function ListKeys(KeyName: String): TStringArray;');
RegisterMethod('function DeleteKey(KeyName: String): Boolean;');
RegisterMethod('function DeleteSubKeys(KeyName: String): Boolean;');
RegisterProperty('Prefix','String',iptR);
end;
end;
procedure SettingsPrefix(self : TMMLSettingsSandbox; var Prefix : String);
begin; Prefix := self.Prefix; end;
procedure TSimbaPSExtension.RIRegister_Settings(Cl: TPSRuntimeClassImporter);
begin
with cl.Add(TMMLSettingsSandbox) do
begin
RegisterMethod(@TMMLSettingsSandbox.IsKey,'ISKEY');
RegisterMethod(@TMMLSettingsSandbox.IsDirectory,'ISDIRECTORY');
RegisterMethod(@TMMLSettingsSandbox.SetKeyValue,'SETKEYVALUE');
RegisterMethod(@TMMLSettingsSandbox.GetKeyValue,'GETKEYVALUE');
RegisterMethod(@TMMLSettingsSandbox.GetKeyValueDef,'GETKEYVALUEDEF');
RegisterMethod(@TMMLSettingsSandbox.ListKeys,'LISTKEYS');
RegisterMethod(@TMMLSettingsSandbox.DeleteKey,'DELETEKEY');
RegisterMethod(@TMMLSettingsSandbox.DeleteSubKeys,'DELETESUBKEYS');
RegisterPropertyHelper(@SettingsPrefix,nil,'Prefix');
end;
end;
procedure TSimbaPSExtension.RegisterPSCComponents(Sender: TObject; x: TPSPascalCompiler); procedure TSimbaPSExtension.RegisterPSCComponents(Sender: TObject; x: TPSPascalCompiler);
begin begin
SIRegister_Std(x); SIRegister_Std(x);
@ -158,6 +199,7 @@ begin
SIRegister_Forms(x); SIRegister_Forms(x);
SIRegister_ExtCtrls(x); SIRegister_ExtCtrls(x);
SIRegister_Menus(x); SIRegister_Menus(x);
SIRegister_Settings(x);
end; end;
procedure TSimbaPSExtension.RegisterPSRComponents(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter); procedure TSimbaPSExtension.RegisterPSRComponents(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
@ -170,6 +212,7 @@ begin
RIRegister_Forms(x); RIRegister_Forms(x);
RIRegister_ExtCtrls(x); RIRegister_ExtCtrls(x);
RIRegister_Menus(x); RIRegister_Menus(x);
RIRegister_Settings(x);
end; end;
destructor TSimbaPSExtension.Destroy; destructor TSimbaPSExtension.Destroy;
@ -224,6 +267,5 @@ begin
end; end;
end. end.

View File

@ -337,7 +337,8 @@ type
procedure SetScriptState(const State: TScriptState); procedure SetScriptState(const State: TScriptState);
function LoadSettingDef(Key : string; Def : string) : string; function LoadSettingDef(Key : string; Def : string) : string;
function CreateSetting(Key : string; Value : string) : string; function CreateSetting(Key : string; Value : string) : string;
procedure SetSetting(key : string; Value : string); procedure SetSetting(key : string; Value : string; save : boolean = false);
function SettingExtists(key : string) : boolean;
procedure FontUpdate; procedure FontUpdate;
public public
DebugStream: String; DebugStream: String;
@ -1004,6 +1005,7 @@ begin
{Creates the paths and returns the path} {Creates the paths and returns the path}
PluginsPath := CreateSetting('Settings/Plugins/Path', ExpandFileName(MainDir+ DS+ 'Plugins' + DS)); PluginsPath := CreateSetting('Settings/Plugins/Path', ExpandFileName(MainDir+ DS+ 'Plugins' + DS));
extensionsPath := CreateSetting('Settings/Extensions/Path',ExpandFileName(MainDir +DS + 'Extensions' + DS)); extensionsPath := CreateSetting('Settings/Extensions/Path',ExpandFileName(MainDir +DS + 'Extensions' + DS));
CreateSetting('Extensions/ExtensionCount','0');
CreateSetting('LastConfig/MainForm/Position',''); CreateSetting('LastConfig/MainForm/Position','');
CreateSetting('LastConfig/MainForm/State','Normal'); CreateSetting('LastConfig/MainForm/State','Normal');
{$ifdef MSWindows} {$ifdef MSWindows}
@ -1025,6 +1027,41 @@ begin
end; end;
procedure TForm1.LoadFormSettings; procedure TForm1.LoadFormSettings;
var
extCount : integer;
function LoadExtension(Number : integer) : boolean;
var
Path : string;
ExtPath : string;
ExtEnabled : boolean;
begin;
result := false;
if (number < 0) or (number >= extCount) then
exit;
path := 'Extensions/Extension' + inttostr(number);
if SettingExtists(Path) = false then
exit;
ExtPath := LoadSettingDef(Path + '/Path','');
if ExtPath = '' then
exit;
ExtEnabled := StrToBoolDef(LoadSettingDef(Path + '/Enabled','false'),false);
if ExtManager.LoadPSExtension(ExtPath,ExtEnabled) = false then
exit;
Result := true;
end;
procedure DeleteExtension(number : integer);
var
i : integer;
path : string;
begin;
path := 'Extensions/Extension';
SettingsForm.Settings.DeleteKey(path + inttostr(number));
for i := number + 1 to extCount - 1 do
SettingsForm.Settings.RenameKey(path + inttostr(i),'Extension' + inttostr(i-1));
SetSetting('Extensions/ExtensionCount',inttostr(extCount - 1),true);
dec(extCount);
end;
var var
str,str2 : string; str,str2 : string;
Data : TStringArray; Data : TStringArray;
@ -1068,65 +1105,58 @@ begin
else else
ShowConsole(false); ShowConsole(false);
{$endif} {$endif}
extCount := StrToIntDef(LoadSettingDef('Extensions/ExtensionCount/','0'),0);
for i := 0 to extCount - 1 do
while (i < extCount) and not LoadExtension(i) do
DeleteExtension(i);
SetSetting('Extensions/ExtensionCount',inttostr(extCount));
str := LoadSettingDef('Settings/Extensions/Path',ExpandFileName(MainDir +DS + 'Extensions' + DS)); str := LoadSettingDef('Settings/Extensions/Path',ExpandFileName(MainDir +DS + 'Extensions' + DS));
str2 := LoadSettingDef('Settings/Extensions/FileExtension','sex'); str2 := LoadSettingDef('Settings/Extensions/FileExtension','sex');
ExtManager.LoadPSExtensionsDir(str,str2); ExtManager.LoadPSExtensionsDir(str,str2);
str := LoadSettingDef('LastConfig/Extensions/EnabledExts','');
if str <> '' then
begin
data := Explode(';',str);
for i := 0 to high(data) do
for ii := 0 to ExtManager.Extensions.Count - 1 do
if data[i] = TVirtualSimbaExtension(ExtManager.Extensions[ii]).Filename then
TVirtualSimbaExtension(ExtManager.Extensions[ii]).Enabled := true;
end;
end; end;
procedure TForm1.SaveFormSettings; procedure TForm1.SaveFormSettings;
var var
Data : TStringArray; Data : TStringArray;
path : string;
i : integer; i : integer;
begin begin
with SettingsForm.Settings do with SettingsForm.Settings do
begin begin
if Self.WindowState = wsMaximized then if Self.WindowState = wsMaximized then
SetKeyValue('LastConfig/MainForm/State','maximized') SetSetting('LastConfig/MainForm/State','maximized')
else else
begin; //Only save the form position if its non maximized. begin; //Only save the form position if its non maximized.
SetKeyValue('LastConfig/MainForm/State','normal'); SetSetting('LastConfig/MainForm/State','normal');
Data := ConvArr([inttostr(Self.left),inttostr(self.top),inttostr(self.width),inttostr(self.height)]); Data := ConvArr([inttostr(Self.left),inttostr(self.top),inttostr(self.width),inttostr(self.height)]);
SetKeyValue('LastConfig/MainForm/Position', Implode(':',Data )); SetSetting('LastConfig/MainForm/Position', Implode(':',Data ));
end; end;
if RecentFiles.Count > 0 then if RecentFiles.Count > 0 then
begin begin
SetLength(data,RecentFiles.Count); SetLength(data,RecentFiles.Count);
for i := 0 to high(data) do //First entry should be the last-opened for i := 0 to high(data) do //First entry should be the last-opened
data[high(data) - i] := RecentFiles[i]; data[high(data) - i] := RecentFiles[i];
SetKeyValue('LastConfig/MainForm/RecentFiles',implode(';',data)); SetSetting('LastConfig/MainForm/RecentFiles',implode(';',data));
end else end else
SetKeyValue('LastConfig/MainForm/RecentFiles',''); SetSetting('LastConfig/MainForm/RecentFiles','');
if MenuItemFunctionList.Checked then if MenuItemFunctionList.Checked then
SetKeyValue('LastConfig/MainForm/FunctionListShown','True') SetSetting('LastConfig/MainForm/FunctionListShown','True')
else else
SetKeyValue('LastConfig/MainForm/FunctionListShown','False'); SetSetting('LastConfig/MainForm/FunctionListShown','False');
{$ifdef MSWindows} {$ifdef MSWindows}
if ConsoleVisible then if ConsoleVisible then
SetKeyValue('LastConfig/Console/Visible','True') SetSetting('LastConfig/Console/Visible','True')
else else
SetKeyValue('LastConfig/Console/Visible','false'); SetSetting('LastConfig/Console/Visible','false');
{$endif} {$endif}
if ExtManager.Extensions.Count > 0 then SetSetting('Extensions/ExtensionCount',inttostr(ExtManager.Extensions.Count));
begin for i := 0 to ExtManager.Extensions.Count-1 do
SetLength(data,0); begin;
for i := 0 to ExtManager.Extensions.Count-1 do
if TVirtualSimbaExtension(ExtManager.Extensions[i]).Enabled then path :='Extensions/Extension' + inttostr(I);
begin SetSetting(Path + '/Path',TVirtualSimbaExtension(ExtManager.Extensions[i]).Filename);
setlength(data,length(data)+1); SetSetting(Path + '/Enabled',BoolToStr(TVirtualSimbaExtension(ExtManager.Extensions[i]).Enabled,True));
data[high(data)] := TVirtualSimbaExtension(ExtManager.Extensions[i]).FileName; end;
end;
SetKeyValue('LastConfig/Extensions/EnabledExts',Implode(';',data));
end else
SetKeyValue('LastConfig/Extensions/EnabledExts','');
SaveToXML(SimbaSettingsFile); SaveToXML(SimbaSettingsFile);
end; end;
end; end;
@ -1222,7 +1252,7 @@ begin
Thread.Client.MOCR.SetFonts(OCR_Fonts.GetFonts); Thread.Client.MOCR.SetFonts(OCR_Fonts.GetFonts);
Se := TMMLSettingsSandbox.Create(SettingsForm.Settings); Se := TMMLSettingsSandbox.Create(SettingsForm.Settings);
Se.SetPrefix('Scripts/'); Se.Prefix := 'Scripts/';
Thread.SetSettings(Se); Thread.SetSettings(Se);
end; end;
@ -2305,7 +2335,7 @@ end;
procedure TForm1.SetFontPath(const AValue: String); procedure TForm1.SetFontPath(const AValue: String);
begin begin
SetSetting('Settings/Fonts/Path',AValue); SetSetting('Settings/Fonts/Path',AValue,true);
end; end;
function TForm1.GetFontPath: String; function TForm1.GetFontPath: String;
@ -2320,7 +2350,7 @@ end;
procedure TForm1.SetIncludePath(const AValue: String); procedure TForm1.SetIncludePath(const AValue: String);
begin begin
SetSetting('Settings/Includes/Path',AValue); SetSetting('Settings/Includes/Path',AValue,true);
end; end;
procedure TForm1.SetScriptState(const State: TScriptState); procedure TForm1.SetScriptState(const State: TScriptState);
@ -2353,22 +2383,25 @@ end;
function TForm1.LoadSettingDef(Key: string; Def: string): string; function TForm1.LoadSettingDef(Key: string; Def: string): string;
begin begin
result := SettingsForm.Settings.GetSetLoadSaveDefaultKeyValueIfNotExists(Key,def,SimbaSettingsFile); result := SettingsForm.Settings.GetKeyValueDefLoad(Key,def,SimbaSettingsFile);
end; end;
function TForm1.CreateSetting(Key: string; Value: string): string; function TForm1.CreateSetting(Key: string; Value: string): string;
begin begin
result := SettingsForm.Settings.GetSetDefaultKeyValue(Key,value); result := SettingsForm.Settings.GetKeyValueDef(Key,value);
end; end;
procedure TForm1.SetSetting(key: string; Value: string); procedure TForm1.SetSetting(key: string; Value: string; save : boolean);
begin begin
//Creates the setting if needed //Creates the setting if needed
if CreateSetting(key,value) <> value then //The setting already occurs, and has a different value.. Lets change it SettingsForm.Settings.SetKeyValue(key,value);
begin; if save then
SettingsForm.Settings.SetKeyValue(key,value);
SettingsForm.Settings.SaveToXML(SimbaSettingsFile); SettingsForm.Settings.SaveToXML(SimbaSettingsFile);
end; end;
function TForm1.SettingExtists(key: string): boolean;
begin
result :=SettingsForm.Settings.KeyExists(key);
end; end;
procedure TForm1.FontUpdate; procedure TForm1.FontUpdate;
@ -2401,7 +2434,7 @@ begin
if UnTar(decompressed, FontPath,true) then if UnTar(decompressed, FontPath,true) then
begin; begin;
FormWriteln('Succesfully installed the new fonts!'); FormWriteln('Succesfully installed the new fonts!');
SetSetting('Settings/Fonts/Version',IntToStr(LatestVersion)); SetSetting('Settings/Fonts/Version',IntToStr(LatestVersion),true);
if Assigned(self.OCR_Fonts) then if Assigned(self.OCR_Fonts) then
self.OCR_Fonts.Free; self.OCR_Fonts.Free;
Self.OCR_Fonts := TMOCR.Create(nil); Self.OCR_Fonts := TMOCR.Create(nil);

View File

@ -94,7 +94,7 @@ begin
if FontVersionThread = nil then//Create thread (only if no-other one is already running) if FontVersionThread = nil then//Create thread (only if no-other one is already running)
begin begin
FontVersionThread := TDownloadThread.Create(true); FontVersionThread := TDownloadThread.Create(true);
FontVersionThread.InputURL := SettingsForm.Settings.GetSetLoadSaveDefaultKeyValueIfNotExists( FontVersionThread.InputURL := SettingsForm.Settings.GetKeyValueDefLoad(
'Settings/Fonts/VersionLink',FontURL + 'Version',SimbaSettingsFile); 'Settings/Fonts/VersionLink',FontURL + 'Version',SimbaSettingsFile);
FontVersionThread.Resume; FontVersionThread.Resume;
while FontVersionThread.Done = false do//Wait till thread is done while FontVersionThread.Done = false do//Wait till thread is done
@ -123,7 +123,7 @@ begin
begin begin
SimbaVersionThread := TDownloadThread.Create(true); SimbaVersionThread := TDownloadThread.Create(true);
SimbaVersionThread.InputURL := SettingsForm.Settings.GetSetLoadSaveDefaultKeyValueIfNotExists( SimbaVersionThread.InputURL := SettingsForm.Settings.GetKeyValueDefLoad(
'Settings/Updater/RemoteVersionLink',SimbaURL + 'Version',SimbaSettingsFile); 'Settings/Updater/RemoteVersionLink',SimbaURL + 'Version',SimbaSettingsFile);
SimbaVersionThread.Resume; SimbaVersionThread.Resume;
while SimbaVersionThread.Done = false do//Wait till thread is done while SimbaVersionThread.Done = false do//Wait till thread is done
@ -214,7 +214,7 @@ begin
FCancelling := False; FCancelling := False;
FCancelled := False; FCancelled := False;
Updater.FileURL := SettingsForm.Settings.GetSetLoadSaveDefaultKeyValueIfNotExists( Updater.FileURL := SettingsForm.Settings.GetKeyValueDefLoad(
'Settings/Updater/RemoteLink', 'Settings/Updater/RemoteLink',
SimbaURL + 'Simba'{$IFDEF WINDOWS} +'.exe'{$ENDIF}, SimbaURL + 'Simba'{$IFDEF WINDOWS} +'.exe'{$ENDIF},
SimbaSettingsFile SimbaSettingsFile

View File

@ -5,7 +5,7 @@ unit virtualextension;
interface interface
uses uses
Classes, SysUtils; Classes, SysUtils,settingssandbox;
type type
{ TVirtualSimbaExtension } { TVirtualSimbaExtension }
@ -16,6 +16,7 @@ type
FVersion : string; FVersion : string;
FFilename : string; FFilename : string;
FEnabled : boolean; FEnabled : boolean;
FSettings : TMMLSettingsSandbox;
procedure SetEnabled(bool : boolean); virtual; procedure SetEnabled(bool : boolean); virtual;
public public
OnChange : TNotifyEvent; OnChange : TNotifyEvent;
@ -27,6 +28,7 @@ type
function GetName : string; function GetName : string;
function GetVersion : String; function GetVersion : String;
property Settings : TMMLSettingsSandbox read FSettings write FSettings;
property Filename : string read FFilename write FFilename; property Filename : string read FFilename write FFilename;
property Enabled : boolean read FEnabled write SetEnabled; property Enabled : boolean read FEnabled write SetEnabled;
end; end;

View File

@ -0,0 +1,38 @@
{
This file is part of the Mufasa Macro Library (MML)
Copyright (c) 2009 by Raymond van Venetië and Merlijn Wajer
MML is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MML is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MML. If not, see <http://www.gnu.org/licenses/>.
See the file COPYING, included in this distribution,
for details about the copyright.
extensions.inc for the Mufasa Macro Library
}
function ext_GetPage(url : string) : string;
var
t: TDownloadThread;
begin
t := TDownloadThread.Create(true);
t.InputURL:='url';
t.Resume;
while not t.done do
begin
Application.ProcessMessages;
Sleep(25);
end;
Exit(t.ResultStr);
end;

View File

@ -21,7 +21,7 @@
Settings.inc for the Mufasa Macro Library Settings.inc for the Mufasa Macro Library
} }
function SettingsIsKey(KeyName: String): Boolean; function KeyIsSetting(KeyName: String): Boolean;
begin begin
if CurrThread.Sett <> nil then if CurrThread.Sett <> nil then
result:= CurrThread.Sett.IsKey(KeyName) result:= CurrThread.Sett.IsKey(KeyName)
@ -29,7 +29,7 @@ begin
result:=false; result:=false;
end; end;
function SettingsIsDirectory(KeyName: String): Boolean; function KeyIsDirectory(KeyName: String): Boolean;
begin begin
if CurrThread.Sett <> nil then if CurrThread.Sett <> nil then
result:= CurrThread.Sett.IsDirectory(KeyName) result:= CurrThread.Sett.IsDirectory(KeyName)
@ -37,7 +37,7 @@ begin
result := false; result := false;
end; end;
function SettingsGetKeyValue(KeyName: String): String; function GetSettingValue(KeyName: String): String;
begin begin
if CurrThread.Sett <> nil then if CurrThread.Sett <> nil then
result:= CurrThread.Sett.GetKeyValue(KeyName) result:= CurrThread.Sett.GetKeyValue(KeyName)
@ -45,23 +45,23 @@ begin
result:=''; result:='';
end; end;
function SettingsGetSetDefaultKeyValue(KeyName, defVal: String): String; function GetSettingValueDef(KeyName, defVal: String): String;
begin begin
if CurrThread.Sett <> nil then if CurrThread.Sett <> nil then
result:= CurrThread.Sett.GetSetDefaultKeyValue(KeyName, defVal) result:= CurrThread.Sett.GetKeyValueDef(KeyName, defVal)
else else
result:=''; result:='';
end; end;
procedure SettingsListKeys(KeyName: String; var KeyReturn: TStringArray); function ListSettings(KeyName: String; var KeyReturn: TStringArray) : boolean;
begin begin
if CurrThread.Sett <> nil then if CurrThread.Sett <> nil then
KeyReturn := CurrThread.Sett.ListKeys(KeyName) Result := CurrThread.Sett.ListKeys(KeyName,keyReturn)
else else
setlength(KeyReturn, 0); result := false;
end; end;
function SettingsDeleteKey(KeyName: String): Boolean; function DeleteSetting(KeyName: String): Boolean;
begin begin
if CurrThread.Sett <> nil then if CurrThread.Sett <> nil then
Result := CurrThread.Sett.DeleteKey(KeyName) Result := CurrThread.Sett.DeleteKey(KeyName)
@ -69,7 +69,7 @@ begin
result := False; result := False;
end; end;
function SettingsDeleteSubKeys(KeyName: String): Boolean; function DeleteSubSettings(KeyName: String): Boolean;
begin begin
if CurrThread.Sett <> nil then if CurrThread.Sett <> nil then
Result := CurrThread.Sett.DeleteSubKeys(KeyName) Result := CurrThread.Sett.DeleteSubKeys(KeyName)

View File

@ -367,11 +367,10 @@ AddFunction(@SameTPA,'function SameTPA(aTPA, bTPA: TPointArray): Boolean;');
AddFunction(@TPAInATPA,'function TPAInATPA(TPA: TPointArray; InATPA: T2DPointArray; var Index: LongInt): Boolean;'); AddFunction(@TPAInATPA,'function TPAInATPA(TPA: TPointArray; InATPA: T2DPointArray; var Index: LongInt): Boolean;');
SetCurrSection('Settings'); SetCurrSection('Settings');
AddFunction(@SettingsIsKey, 'function SettingsIsKey(KeyName: String): Boolean;'); AddFunction(@KeyIsSetting, 'function KeyIsSetting(KeyName: String): Boolean;');
AddFunction(@SettingsIsDirectory, 'function SettingsIsDirectory(KeyName: String): Boolean;'); AddFunction(@KeyIsDirectory, 'function KeyIsDirectory(KeyName: String): Boolean;');
AddFunction(@SettingsGetKeyValue, 'function SettingsGetKeyValue(KeyName: String): String;'); AddFunction(@GetSettingValue, 'function GetSettingValue(KeyName: String): String;');
AddFunction(@SettingsGetSetDefaultKeyValue, 'function SettingsGetSetDefaultKeyValue(KeyName, defVal: String): String;'); AddFunction(@GetSettingValueDef, 'function GetSettingValueDef(KeyName, defVal: String): String;');
AddFunction(@SettingsListKeys, 'procedure SettingsListKeys(KeyName: String; var KeyReturn: TStringArray);'); AddFunction(@ListSettings, 'function ListSettings(KeyName: String; var KeyReturn: TStringArray) : boolean;');
AddFunction(@DeleteSetting, 'function DeleteSetting(KeyName: String): Boolean;');
AddFunction(@SettingsDeleteKey, 'function SettingsDeleteKey(KeyName: String): Boolean;'); AddFunction(@DeleteSubSettings, 'function DeleteSubSettings(KeyName: String): Boolean;');
AddFunction(@SettingsDeleteSubKeys, 'function SettingsDeleteSubKeys(KeyName: String): Boolean;');

View File

@ -57,51 +57,40 @@ type
} }
TMMLSettings = class(TObject) TMMLSettings = class(TObject)
private
Nodes: TTreeNodes;
function KeyNameToKeys(KeyName: String): TStringArray;
function WalkToNode(KeyName: String): TTreeNode;
public procedure InternalLoadFromXML(XMLDoc: TXMLDocument);
constructor Create(aNodes: TTreeNodes); procedure WriteXMLData(n: TTreeNode;
destructor Destroy; override; XMLNode: TDOMNode; XMLDoc: TXMLDocument;
var XMLChild: TDOMNode; var C: Integer);
procedure WalkTree(Node: TTreeNode; XMLNode: TDOMNode; XMLDoc: TXMLDocument;
var C: Integer);
function GetKeyValueDefLoadFirst(KeyName, defVal, fileName: String): String;
public
constructor Create(aNodes: TTreeNodes);
destructor Destroy; override;
function GetNodePath(Node: TTreeNode): String;
function ListKeys(KeyName: String; out Keys :TStringArray) : boolean;
private function KeyExists(KeyName: String): Boolean;
Nodes: TTreeNodes; function IsKey(KeyName: String): Boolean;
function KeyNameToKeys(KeyName: String): TStringArray; function IsDirectory(KeyName: String): Boolean;
function WalkToNode(KeyName: String): TTreeNode;
procedure InternalLoadFromXML(XMLDoc: TXMLDocument); function DeleteKey(KeyName: String): Boolean;
procedure WriteXMLData(n: TTreeNode; function DeleteSubKeys(KeyName: String): Boolean;
XMLNode: TDOMNode; XMLDoc: TXMLDocument;
var XMLChild: TDOMNode; var C: Integer);
procedure WalkTree(Node: TTreeNode; XMLNode: TDOMNode; XMLDoc: TXMLDocument;
var C: Integer);
public function CreateKey(KeyName: String; CreatePath: Boolean = True): Boolean;
function GetNodePath(Node: TTreeNode): String; function SetKeyValue(KeyName: String; KeyValue: String; CreatePath : boolean = true) : boolean;
function ListKeys(KeyName: String): TStringArray; function GetKeyValue(KeyName: String): String;
function GetKeyValueDef(KeyName, defVal: String): String;
function KeyExists(KeyName: String): Boolean; function RenameKey(oldKey,newKey : string) : boolean;
function IsKey(KeyName: String): Boolean; function GetKeyValueDefLoad(KeyName, defVal, fileName: String): String;
function IsDirectory(KeyName: String): Boolean;
function DeleteKey(KeyName: String): Boolean;
function DeleteSubKeys(KeyName: String): Boolean;
procedure SetKeyValue(KeyName: String; KeyValue: String);
function CreateKey(KeyName: String; CreatePath: Boolean = False): Boolean;
function GetKeyValue(KeyName: String): String;
// Horrible name
function GetSetDefaultKeyValue(KeyName, defVal: String): String;
private
// /facepalm
function GetSetLoadSaveDefaultKeyValue(KeyName, defVal, fileName: String): String;
public
// AAAAAAAAAAAHG??
function GetSetLoadSaveDefaultKeyValueIfNotExists(KeyName, defVal, fileName: String): String;
public
procedure LoadFromXML(fileName: String);
procedure SaveToXML(fileName: String);
procedure LoadFromXML(fileName: String);
procedure SaveToXML(fileName: String);
end; end;
implementation implementation
@ -255,19 +244,19 @@ end;
Equivalent to 'ls' or 'dir'. It lists the keys in a certain key (directory) Equivalent to 'ls' or 'dir'. It lists the keys in a certain key (directory)
} }
function TMMLSettings.ListKeys(KeyName: String): TStringArray; function TMMLSettings.ListKeys(KeyName: String; out Keys: TStringArray): boolean;
var var
N: TTreeNode; N: TTreeNode;
begin begin
SetLength(Result, 0); SetLength(Keys, 0);
N := WalkToNode(KeyName); N := WalkToNode(KeyName);
if N <> nil then if N <> nil then
N := N.GetFirstChild; N := N.GetFirstChild;
result := n <> nil;
while N <> nil do while N <> nil do
begin begin
setlength(result,length(result)+1); setlength(Keys,length(Keys)+1);
result[high(result)] := N.Text; Keys[high(Keys)] := N.Text;
N := N.GetNextSibling; N := N.GetNextSibling;
end; end;
end; end;
@ -345,13 +334,41 @@ begin
Exit(''); Exit('');
end; end;
function TMMLSettings.RenameKey(oldKey, newKey: string): boolean;
var
Node : TTreeNode;
Path : TstringArray;
newstr : string;
i : integer;
begin
result := false;
Path := KeyNameToKeys(OldKey);
if length(path) = 1 then
exit;
if pos('/',newKey) > 0 then
exit;
if not KeyExists(oldkey) then
exit;
for i := length(oldKey) downto 1 do
if oldkey[i] = '/' then
begin
newstr:= Copy(oldkey,1,i) + NewKey;
break;
end;
if KeyExists(NewKey) then
exit;
Node := WalkToNode(oldKey);
Node.Text:= NewKey;
result := true;
end;
{ {
If the key exists - return the value. If the key exists - return the value.
If it does not exist, create the key - with a possible path, set it to If it does not exist, create the key - with a possible path, set it to
defVal and return defVal. defVal and return defVal.
} }
function TMMLSettings.GetSetDefaultKeyValue(KeyName, defVal: String): String; function TMMLSettings.GetKeyValueDef(KeyName, defVal: String): String;
var var
Res: String; Res: String;
begin begin
@ -421,26 +438,26 @@ begin
end; end;
{ {
Clear the entire tree. Load from fileName. call GetSetDefaultKeyValue. Clear the entire tree. Load from fileName. call GetKeyValueDef.
} }
function TMMLSettings.GetSetLoadSaveDefaultKeyValue(KeyName, defVal, fileName: String): String; function TMMLSettings.GetKeyValueDefLoadFirst(KeyName, defVal, fileName: String): String;
begin begin
Nodes.Clear; Nodes.Clear;
LoadFromXML(fileName); LoadFromXML(fileName);
Result := GetSetDefaultKeyValue(KeyName, defVal); Result := GetKeyValueDef(KeyName, defVal);
SaveToXML(fileName); SaveToXML(fileName);
end; end;
{ {
If Key exists, call getSetDefaultKeyValue, else call GetSetLoadSaveDefaultKeyValue If Key exists, call GetKeyValueDef, else call GetSetLoadSaveDefaultKeyValue
} }
function TMMLSettings.GetSetLoadSaveDefaultKeyValueIfNotExists(KeyName, defVal, fileName: String): String; function TMMLSettings.GetKeyValueDefLoad(KeyName, defVal, fileName: String): String;
begin begin
if KeyExists(KeyName) then if KeyExists(KeyName) then
Exit(GetSetDefaultKeyValue(KeyName, defVal)) Exit(GetKeyValueDef(KeyName, defVal))
else else
Exit(GetSetLoadSaveDefaultKeyValue(KeyName, defVal, fileName)); Exit(GetKeyValueDefLoadFirst(KeyName, defVal, fileName));
end; end;
{ {
@ -449,7 +466,7 @@ end;
and CreatePath = True, then b,c,d and e are all created. and CreatePath = True, then b,c,d and e are all created.
} }
function TMMLSettings.CreateKey(KeyName: String; CreatePath: Boolean = False): Boolean; function TMMLSettings.CreateKey(KeyName: String; CreatePath: Boolean = True): Boolean;
var var
N, newN, nParent: TTreeNode; N, newN, nParent: TTreeNode;
Path: TStringArray; Path: TStringArray;
@ -524,15 +541,18 @@ end;
Set the value of a key. Set the value of a key.
} }
procedure TMMLSettings.SetKeyValue(KeyName: String; KeyValue: String); function TMMLSettings.SetKeyValue(KeyName: String; KeyValue: String; CreatePath: boolean) : boolean;
var var
N, NN: TTreeNode; N, NN: TTreeNode;
begin begin
if not KeyExists(KeyName) then result := false;
if not KeyExists(KeyName) and not CreatePath then
begin begin
mDebugLn('SetKeyValue - Key does not exist'); mDebugLn('SetKeyValue - Key does not exist');
Exit; Exit;
end; end else
if not KeyExists(keyname) then
CreateKey(keyname);
if not IsKey(KeyName) then if not IsKey(KeyName) then
begin begin
mDebugLn('SetKeyValue - IsKey returned false'); mDebugLn('SetKeyValue - IsKey returned false');
@ -558,6 +578,7 @@ begin
mDebugLn('Setting ' + KeyName + ' to ' + KeyValue); mDebugLn('Setting ' + KeyName + ' to ' + KeyValue);
N := N.GetNextSibling; N := N.GetNextSibling;
end; end;
result := true;
end; end;

View File

@ -30,26 +30,28 @@ uses
Classes, SysUtils, settings, strutils, mufasatypes; Classes, SysUtils, settings, strutils, mufasatypes;
type type
{ TMMLSettingsSandbox }
TMMLSettingsSandbox = class(TObject) TMMLSettingsSandbox = class(TObject)
public private
constructor Create(sett: TMMLSettings); ST: TMMLSettings;
destructor Destroy; override; FPrefix: String;
function GetPrefix: String;
function IsKey(KeyName: String): Boolean; procedure SetPrefix(s: String);
function IsDirectory(KeyName: String): Boolean; public
function GetKeyValue(KeyName: String): String; constructor Create(sett: TMMLSettings);
function GetSetDefaultKeyValue(KeyName, defVal: String): String; destructor Destroy; override;
function ListKeys(KeyName: String): TStringArray;
function DeleteKey(KeyName: String): Boolean;
function DeleteSubKeys(KeyName: String): Boolean;
public
function GetPrefix: String;
procedure SetPrefix(s: String);
private
ST: TMMLSettings;
Prefix: String;
function IsKey(KeyName: String): Boolean;
function IsDirectory(KeyName: String): Boolean;
function SetKeyValue(Keyname : string; Value : string) : boolean;
function GetKeyValue(KeyName: String): String;
function GetKeyValueDef(KeyName, defVal: String): String;
function ListKeys(KeyName: String; out Keys : TStringArray): boolean;
function DeleteKey(KeyName: String): Boolean;
function DeleteSubKeys(KeyName: String): Boolean;
property prefix : string read GetPrefix write SetPrefix;
end; end;
implementation implementation
@ -70,17 +72,17 @@ end;
function TMMLSettingsSandbox.GetPrefix: String; function TMMLSettingsSandbox.GetPrefix: String;
begin begin
result := Prefix; result := FPrefix;
end; end;
procedure TMMLSettingsSandbox.SetPrefix(s: String); procedure TMMLSettingsSandbox.SetPrefix(s: String);
begin begin
Prefix := s; FPrefix := s;
end; end;
function TMMLSettingsSandbox.ListKeys(KeyName: String): TStringArray; function TMMLSettingsSandbox.ListKeys(KeyName: String; out Keys :TStringArray): boolean;
begin begin
exit(ST.ListKeys(Prefix + KeyName)) exit(ST.ListKeys(Prefix + KeyName,keys))
end; end;
function TMMLSettingsSandbox.GetKeyValue(KeyName: String): String; function TMMLSettingsSandbox.GetKeyValue(KeyName: String): String;
@ -88,9 +90,9 @@ begin
exit(ST.GetKeyValue(Prefix + KeyName)) exit(ST.GetKeyValue(Prefix + KeyName))
end; end;
function TMMLSettingsSandbox.GetSetDefaultKeyValue(KeyName, defVal: String): String; function TMMLSettingsSandbox.GetKeyValueDef(KeyName, defVal: String): String;
begin begin
exit(ST.GetSetDefaultKeyValue(Prefix + KeyName, defVal)) exit(ST.GetKeyValueDef(Prefix + KeyName, defVal))
end; end;
function TMMLSettingsSandbox.IsKey(KeyName: String): Boolean; function TMMLSettingsSandbox.IsKey(KeyName: String): Boolean;
@ -103,6 +105,14 @@ begin
exit(ST.IsDirectory(Prefix + KeyName)) exit(ST.IsDirectory(Prefix + KeyName))
end; end;
function TMMLSettingsSandbox.SetKeyValue(Keyname: string; Value: string
): boolean;
begin
Writeln(KeyName);
Writeln(Value);
exit(ST.SetKeyValue(prefix + keyname,value,true));
end;
function TMMLSettingsSandbox.DeleteKey(KeyName: String): Boolean; function TMMLSettingsSandbox.DeleteKey(KeyName: String): Boolean;
begin begin
exit(ST.DeleteKey(Prefix + KeyName)); exit(ST.DeleteKey(Prefix + KeyName));