diff --git a/Extensions/test.sex b/Extensions/test.sex index d4d1000..3c4ae0e 100644 --- a/Extensions/test.sex +++ b/Extensions/test.sex @@ -12,6 +12,7 @@ end; procedure Attach; begin; + Settings.SetKeyValue('TestSetting','1337'); Writeln('Your extension has been enabled, do stuff here'); end; diff --git a/Projects/SAMufasaGUI/extensionmanager.pas b/Projects/SAMufasaGUI/extensionmanager.pas index 2956e94..5f3eeba 100644 --- a/Projects/SAMufasaGUI/extensionmanager.pas +++ b/Projects/SAMufasaGUI/extensionmanager.pas @@ -28,7 +28,7 @@ type StartDisabled : boolean; property OnChange : TNotifyEvent read FOnChange write SetOnchange; 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 HandleHook(HookName: String; Args: Array of Variant): Variant; end; @@ -38,7 +38,7 @@ var implementation uses - TestUnit; + TestUnit, settingssandbox,simbasettings; procedure TExtensionManager.SetOnchange(const AValue: TNotifyEvent); var @@ -76,7 +76,7 @@ begin result := -1; end; -function TExtensionManager.LoadPSExtension(Filename: string): boolean; +function TExtensionManager.LoadPSExtension(Filename: string; enabled: boolean): boolean; var Ext : TExtension; begin @@ -84,12 +84,17 @@ begin exit(true); Result := False; try - Ext := TSimbaPSExtension.Create(filename,startdisabled); - result := TSimbaPSExtension(ext).Working; + Ext := TSimbaPSExtension.Create(filename,True); +// result := TSimbaPSExtension(ext).Working; 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; if assigned(FOnChange) then FOnChange(Self); + Result := True; except on e : exception do formWritelnex(format('Error in LoadPSExtension(%s): %s',[FileName, e.message])); @@ -98,22 +103,22 @@ end; function GetFiles(Path, Ext: string): TstringArray; var - SearchRec : TSearchRec; - c : integer; + SearchRec : TSearchRec; + c : integer; begin - c := 0; - if FindFirst(Path + '*.' + ext, faAnyFile, SearchRec) = 0 then - begin - repeat - inc(c); - SetLength(Result,c); - Result[c-1] := SearchRec.Name; - until FindNext(SearchRec) <> 0; - SysUtils.FindClose(SearchRec); - end; + c := 0; + if FindFirst(Path + '*.' + ext, faAnyFile, SearchRec) = 0 then + begin + repeat + inc(c); + SetLength(Result,c); + Result[c-1] := SearchRec.Name; + until FindNext(SearchRec) <> 0; + SysUtils.FindClose(SearchRec); + end; end; -function TExtensionManager.LoadPSExtensionsDir(Directory,ext: string): boolean; +function TExtensionManager.LoadPSExtensionsDir(Directory, ext: string): boolean; var Files : TstringArray; i : integer; @@ -127,7 +132,7 @@ begin Directory := IncludeTrailingPathDelimiter(directory); Files := GetFiles(Directory,ext); 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; if Assigned(FOnChange) then FOnChange(self); diff --git a/Projects/SAMufasaGUI/psextension.pas b/Projects/SAMufasaGUI/psextension.pas index 0624949..179bc7b 100644 --- a/Projects/SAMufasaGUI/psextension.pas +++ b/Projects/SAMufasaGUI/psextension.pas @@ -27,6 +27,8 @@ type function FreeScript: boolean; function InitScript: Boolean; procedure OutputMessages; + procedure SIRegister_Settings(Cl: TPSPascalCompiler); + procedure RIRegister_Settings(Cl: TPSRuntimeClassImporter); public function HookExists(HookName: String): Boolean;override; @@ -47,7 +49,7 @@ uses uPSC_extctrls,uPSC_menus, //Compile libs uPSR_std, uPSR_controls,uPSR_classes,uPSR_graphics,uPSR_stdctrls,uPSR_forms, uPSR_extctrls,uPSR_menus, //Runtime-libs - testunit//Writeln + testunit,updateform,settingssandbox//Writeln ; function TSimbaPSExtension.HookExists(HookName: String): Boolean; @@ -110,10 +112,13 @@ begin result := ExecuteHook('Free',[],bla) = SExt_ok; end; +{$I ../../Units/MMLAddon/PSInc/Wrappers/extensions.inc} procedure TSimbaPSExtension.RegisterMyMethods(Sender: TPSScript); begin 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_MainMenu','TMainMenu'); end; @@ -122,6 +127,7 @@ procedure TSimbaPSExtension.OnPSExecute(Sender: TPSScript); begin Sender.SetVarToInstance('simba',Form1); Sender.SetVarToInstance('Simba_MainMenu',Form1.MainMenu); + Sender.SetPointerToData('Settings',@Self.Settings,Sender.FindNamedType('TMMLSettingsSandbox')); end; procedure TSimbaPSExtension.SetEnabled(bool: boolean); @@ -148,6 +154,41 @@ begin inherited SetEnabled(bool); 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); begin SIRegister_Std(x); @@ -158,6 +199,7 @@ begin SIRegister_Forms(x); SIRegister_ExtCtrls(x); SIRegister_Menus(x); + SIRegister_Settings(x); end; procedure TSimbaPSExtension.RegisterPSRComponents(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter); @@ -170,6 +212,7 @@ begin RIRegister_Forms(x); RIRegister_ExtCtrls(x); RIRegister_Menus(x); + RIRegister_Settings(x); end; destructor TSimbaPSExtension.Destroy; @@ -224,6 +267,5 @@ begin end; - end. diff --git a/Projects/SAMufasaGUI/testunit.pas b/Projects/SAMufasaGUI/testunit.pas index d9db9a4..af167e7 100644 --- a/Projects/SAMufasaGUI/testunit.pas +++ b/Projects/SAMufasaGUI/testunit.pas @@ -337,7 +337,8 @@ type procedure SetScriptState(const State: TScriptState); function LoadSettingDef(Key : string; Def : 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; public DebugStream: String; @@ -1004,6 +1005,7 @@ begin {Creates the paths and returns the path} PluginsPath := CreateSetting('Settings/Plugins/Path', ExpandFileName(MainDir+ DS+ 'Plugins' + DS)); extensionsPath := CreateSetting('Settings/Extensions/Path',ExpandFileName(MainDir +DS + 'Extensions' + DS)); + CreateSetting('Extensions/ExtensionCount','0'); CreateSetting('LastConfig/MainForm/Position',''); CreateSetting('LastConfig/MainForm/State','Normal'); {$ifdef MSWindows} @@ -1025,6 +1027,41 @@ begin end; 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 str,str2 : string; Data : TStringArray; @@ -1068,65 +1105,58 @@ begin else ShowConsole(false); {$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)); str2 := LoadSettingDef('Settings/Extensions/FileExtension','sex'); 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; procedure TForm1.SaveFormSettings; var Data : TStringArray; + path : string; i : integer; begin with SettingsForm.Settings do begin if Self.WindowState = wsMaximized then - SetKeyValue('LastConfig/MainForm/State','maximized') + SetSetting('LastConfig/MainForm/State','maximized') else 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)]); - SetKeyValue('LastConfig/MainForm/Position', Implode(':',Data )); + SetSetting('LastConfig/MainForm/Position', Implode(':',Data )); end; if RecentFiles.Count > 0 then begin SetLength(data,RecentFiles.Count); for i := 0 to high(data) do //First entry should be the last-opened data[high(data) - i] := RecentFiles[i]; - SetKeyValue('LastConfig/MainForm/RecentFiles',implode(';',data)); + SetSetting('LastConfig/MainForm/RecentFiles',implode(';',data)); end else - SetKeyValue('LastConfig/MainForm/RecentFiles',''); + SetSetting('LastConfig/MainForm/RecentFiles',''); if MenuItemFunctionList.Checked then - SetKeyValue('LastConfig/MainForm/FunctionListShown','True') + SetSetting('LastConfig/MainForm/FunctionListShown','True') else - SetKeyValue('LastConfig/MainForm/FunctionListShown','False'); + SetSetting('LastConfig/MainForm/FunctionListShown','False'); {$ifdef MSWindows} if ConsoleVisible then - SetKeyValue('LastConfig/Console/Visible','True') + SetSetting('LastConfig/Console/Visible','True') else - SetKeyValue('LastConfig/Console/Visible','false'); + SetSetting('LastConfig/Console/Visible','false'); {$endif} - if ExtManager.Extensions.Count > 0 then - begin - SetLength(data,0); - for i := 0 to ExtManager.Extensions.Count-1 do - if TVirtualSimbaExtension(ExtManager.Extensions[i]).Enabled then - begin - setlength(data,length(data)+1); - data[high(data)] := TVirtualSimbaExtension(ExtManager.Extensions[i]).FileName; - end; - SetKeyValue('LastConfig/Extensions/EnabledExts',Implode(';',data)); - end else - SetKeyValue('LastConfig/Extensions/EnabledExts',''); + SetSetting('Extensions/ExtensionCount',inttostr(ExtManager.Extensions.Count)); + for i := 0 to ExtManager.Extensions.Count-1 do + begin; + + path :='Extensions/Extension' + inttostr(I); + SetSetting(Path + '/Path',TVirtualSimbaExtension(ExtManager.Extensions[i]).Filename); + SetSetting(Path + '/Enabled',BoolToStr(TVirtualSimbaExtension(ExtManager.Extensions[i]).Enabled,True)); + end; SaveToXML(SimbaSettingsFile); end; end; @@ -1222,7 +1252,7 @@ begin Thread.Client.MOCR.SetFonts(OCR_Fonts.GetFonts); Se := TMMLSettingsSandbox.Create(SettingsForm.Settings); - Se.SetPrefix('Scripts/'); + Se.Prefix := 'Scripts/'; Thread.SetSettings(Se); end; @@ -2305,7 +2335,7 @@ end; procedure TForm1.SetFontPath(const AValue: String); begin - SetSetting('Settings/Fonts/Path',AValue); + SetSetting('Settings/Fonts/Path',AValue,true); end; function TForm1.GetFontPath: String; @@ -2320,7 +2350,7 @@ end; procedure TForm1.SetIncludePath(const AValue: String); begin - SetSetting('Settings/Includes/Path',AValue); + SetSetting('Settings/Includes/Path',AValue,true); end; procedure TForm1.SetScriptState(const State: TScriptState); @@ -2353,22 +2383,25 @@ end; function TForm1.LoadSettingDef(Key: string; Def: string): string; begin - result := SettingsForm.Settings.GetSetLoadSaveDefaultKeyValueIfNotExists(Key,def,SimbaSettingsFile); + result := SettingsForm.Settings.GetKeyValueDefLoad(Key,def,SimbaSettingsFile); end; function TForm1.CreateSetting(Key: string; Value: string): string; begin - result := SettingsForm.Settings.GetSetDefaultKeyValue(Key,value); + result := SettingsForm.Settings.GetKeyValueDef(Key,value); end; -procedure TForm1.SetSetting(key: string; Value: string); +procedure TForm1.SetSetting(key: string; Value: string; save : boolean); begin - //Creates the setting if needed - if CreateSetting(key,value) <> value then //The setting already occurs, and has a different value.. Lets change it - begin; - SettingsForm.Settings.SetKeyValue(key,value); + //Creates the setting if needed + SettingsForm.Settings.SetKeyValue(key,value); + if save then SettingsForm.Settings.SaveToXML(SimbaSettingsFile); - end; +end; + +function TForm1.SettingExtists(key: string): boolean; +begin + result :=SettingsForm.Settings.KeyExists(key); end; procedure TForm1.FontUpdate; @@ -2401,7 +2434,7 @@ begin if UnTar(decompressed, FontPath,true) then begin; 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 self.OCR_Fonts.Free; Self.OCR_Fonts := TMOCR.Create(nil); diff --git a/Projects/SAMufasaGUI/updateform.pas b/Projects/SAMufasaGUI/updateform.pas index 12784a6..7effec1 100644 --- a/Projects/SAMufasaGUI/updateform.pas +++ b/Projects/SAMufasaGUI/updateform.pas @@ -94,7 +94,7 @@ begin if FontVersionThread = nil then//Create thread (only if no-other one is already running) begin FontVersionThread := TDownloadThread.Create(true); - FontVersionThread.InputURL := SettingsForm.Settings.GetSetLoadSaveDefaultKeyValueIfNotExists( + FontVersionThread.InputURL := SettingsForm.Settings.GetKeyValueDefLoad( 'Settings/Fonts/VersionLink',FontURL + 'Version',SimbaSettingsFile); FontVersionThread.Resume; while FontVersionThread.Done = false do//Wait till thread is done @@ -123,7 +123,7 @@ begin begin SimbaVersionThread := TDownloadThread.Create(true); - SimbaVersionThread.InputURL := SettingsForm.Settings.GetSetLoadSaveDefaultKeyValueIfNotExists( + SimbaVersionThread.InputURL := SettingsForm.Settings.GetKeyValueDefLoad( 'Settings/Updater/RemoteVersionLink',SimbaURL + 'Version',SimbaSettingsFile); SimbaVersionThread.Resume; while SimbaVersionThread.Done = false do//Wait till thread is done @@ -214,7 +214,7 @@ begin FCancelling := False; FCancelled := False; - Updater.FileURL := SettingsForm.Settings.GetSetLoadSaveDefaultKeyValueIfNotExists( + Updater.FileURL := SettingsForm.Settings.GetKeyValueDefLoad( 'Settings/Updater/RemoteLink', SimbaURL + 'Simba'{$IFDEF WINDOWS} +'.exe'{$ENDIF}, SimbaSettingsFile diff --git a/Projects/SAMufasaGUI/virtualextension.pas b/Projects/SAMufasaGUI/virtualextension.pas index c286a65..90a5633 100644 --- a/Projects/SAMufasaGUI/virtualextension.pas +++ b/Projects/SAMufasaGUI/virtualextension.pas @@ -5,7 +5,7 @@ unit virtualextension; interface uses - Classes, SysUtils; + Classes, SysUtils,settingssandbox; type { TVirtualSimbaExtension } @@ -16,6 +16,7 @@ type FVersion : string; FFilename : string; FEnabled : boolean; + FSettings : TMMLSettingsSandbox; procedure SetEnabled(bool : boolean); virtual; public OnChange : TNotifyEvent; @@ -27,6 +28,7 @@ type function GetName : string; function GetVersion : String; + property Settings : TMMLSettingsSandbox read FSettings write FSettings; property Filename : string read FFilename write FFilename; property Enabled : boolean read FEnabled write SetEnabled; end; diff --git a/Units/MMLAddon/PSInc/Wrappers/extensions.inc b/Units/MMLAddon/PSInc/Wrappers/extensions.inc new file mode 100644 index 0000000..cad4cfa --- /dev/null +++ b/Units/MMLAddon/PSInc/Wrappers/extensions.inc @@ -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 . + + 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; diff --git a/Units/MMLAddon/PSInc/Wrappers/settings.inc b/Units/MMLAddon/PSInc/Wrappers/settings.inc index 2a2552d..7618cb7 100644 --- a/Units/MMLAddon/PSInc/Wrappers/settings.inc +++ b/Units/MMLAddon/PSInc/Wrappers/settings.inc @@ -21,7 +21,7 @@ Settings.inc for the Mufasa Macro Library } -function SettingsIsKey(KeyName: String): Boolean; +function KeyIsSetting(KeyName: String): Boolean; begin if CurrThread.Sett <> nil then result:= CurrThread.Sett.IsKey(KeyName) @@ -29,7 +29,7 @@ begin result:=false; end; -function SettingsIsDirectory(KeyName: String): Boolean; +function KeyIsDirectory(KeyName: String): Boolean; begin if CurrThread.Sett <> nil then result:= CurrThread.Sett.IsDirectory(KeyName) @@ -37,7 +37,7 @@ begin result := false; end; -function SettingsGetKeyValue(KeyName: String): String; +function GetSettingValue(KeyName: String): String; begin if CurrThread.Sett <> nil then result:= CurrThread.Sett.GetKeyValue(KeyName) @@ -45,23 +45,23 @@ begin result:=''; end; -function SettingsGetSetDefaultKeyValue(KeyName, defVal: String): String; +function GetSettingValueDef(KeyName, defVal: String): String; begin if CurrThread.Sett <> nil then - result:= CurrThread.Sett.GetSetDefaultKeyValue(KeyName, defVal) + result:= CurrThread.Sett.GetKeyValueDef(KeyName, defVal) else result:=''; end; -procedure SettingsListKeys(KeyName: String; var KeyReturn: TStringArray); +function ListSettings(KeyName: String; var KeyReturn: TStringArray) : boolean; begin if CurrThread.Sett <> nil then - KeyReturn := CurrThread.Sett.ListKeys(KeyName) + Result := CurrThread.Sett.ListKeys(KeyName,keyReturn) else - setlength(KeyReturn, 0); + result := false; end; -function SettingsDeleteKey(KeyName: String): Boolean; +function DeleteSetting(KeyName: String): Boolean; begin if CurrThread.Sett <> nil then Result := CurrThread.Sett.DeleteKey(KeyName) @@ -69,7 +69,7 @@ begin result := False; end; -function SettingsDeleteSubKeys(KeyName: String): Boolean; +function DeleteSubSettings(KeyName: String): Boolean; begin if CurrThread.Sett <> nil then Result := CurrThread.Sett.DeleteSubKeys(KeyName) diff --git a/Units/MMLAddon/PSInc/psexportedmethods.inc b/Units/MMLAddon/PSInc/psexportedmethods.inc index ad851f6..6c2fa22 100644 --- a/Units/MMLAddon/PSInc/psexportedmethods.inc +++ b/Units/MMLAddon/PSInc/psexportedmethods.inc @@ -367,11 +367,10 @@ AddFunction(@SameTPA,'function SameTPA(aTPA, bTPA: TPointArray): Boolean;'); AddFunction(@TPAInATPA,'function TPAInATPA(TPA: TPointArray; InATPA: T2DPointArray; var Index: LongInt): Boolean;'); SetCurrSection('Settings'); -AddFunction(@SettingsIsKey, 'function SettingsIsKey(KeyName: String): Boolean;'); -AddFunction(@SettingsIsDirectory, 'function SettingsIsDirectory(KeyName: String): Boolean;'); -AddFunction(@SettingsGetKeyValue, 'function SettingsGetKeyValue(KeyName: String): String;'); -AddFunction(@SettingsGetSetDefaultKeyValue, 'function SettingsGetSetDefaultKeyValue(KeyName, defVal: String): String;'); -AddFunction(@SettingsListKeys, 'procedure SettingsListKeys(KeyName: String; var KeyReturn: TStringArray);'); - -AddFunction(@SettingsDeleteKey, 'function SettingsDeleteKey(KeyName: String): Boolean;'); -AddFunction(@SettingsDeleteSubKeys, 'function SettingsDeleteSubKeys(KeyName: String): Boolean;'); +AddFunction(@KeyIsSetting, 'function KeyIsSetting(KeyName: String): Boolean;'); +AddFunction(@KeyIsDirectory, 'function KeyIsDirectory(KeyName: String): Boolean;'); +AddFunction(@GetSettingValue, 'function GetSettingValue(KeyName: String): String;'); +AddFunction(@GetSettingValueDef, 'function GetSettingValueDef(KeyName, defVal: String): String;'); +AddFunction(@ListSettings, 'function ListSettings(KeyName: String; var KeyReturn: TStringArray) : boolean;'); +AddFunction(@DeleteSetting, 'function DeleteSetting(KeyName: String): Boolean;'); +AddFunction(@DeleteSubSettings, 'function DeleteSubSettings(KeyName: String): Boolean;'); diff --git a/Units/MMLAddon/settings.pas b/Units/MMLAddon/settings.pas index 50ff9cd..b142b79 100644 --- a/Units/MMLAddon/settings.pas +++ b/Units/MMLAddon/settings.pas @@ -57,51 +57,40 @@ type } TMMLSettings = class(TObject) + private + Nodes: TTreeNodes; + function KeyNameToKeys(KeyName: String): TStringArray; + function WalkToNode(KeyName: String): TTreeNode; - public - constructor Create(aNodes: TTreeNodes); - destructor Destroy; override; + procedure InternalLoadFromXML(XMLDoc: TXMLDocument); + procedure WriteXMLData(n: TTreeNode; + 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 - Nodes: TTreeNodes; - function KeyNameToKeys(KeyName: String): TStringArray; - function WalkToNode(KeyName: String): TTreeNode; + function KeyExists(KeyName: String): Boolean; + function IsKey(KeyName: String): Boolean; + function IsDirectory(KeyName: String): Boolean; - procedure InternalLoadFromXML(XMLDoc: TXMLDocument); - procedure WriteXMLData(n: TTreeNode; - XMLNode: TDOMNode; XMLDoc: TXMLDocument; - var XMLChild: TDOMNode; var C: Integer); - procedure WalkTree(Node: TTreeNode; XMLNode: TDOMNode; XMLDoc: TXMLDocument; - var C: Integer); + function DeleteKey(KeyName: String): Boolean; + function DeleteSubKeys(KeyName: String): Boolean; - public - function GetNodePath(Node: TTreeNode): String; - function ListKeys(KeyName: String): TStringArray; - - function KeyExists(KeyName: String): Boolean; - function IsKey(KeyName: String): Boolean; - 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); + function CreateKey(KeyName: String; CreatePath: Boolean = True): Boolean; + function SetKeyValue(KeyName: String; KeyValue: String; CreatePath : boolean = true) : boolean; + function GetKeyValue(KeyName: String): String; + function GetKeyValueDef(KeyName, defVal: String): String; + function RenameKey(oldKey,newKey : string) : boolean; + function GetKeyValueDefLoad(KeyName, defVal, fileName: String): String; + procedure LoadFromXML(fileName: String); + procedure SaveToXML(fileName: String); end; implementation @@ -255,19 +244,19 @@ end; 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 N: TTreeNode; begin - SetLength(Result, 0); + SetLength(Keys, 0); N := WalkToNode(KeyName); if N <> nil then N := N.GetFirstChild; - + result := n <> nil; while N <> nil do begin - setlength(result,length(result)+1); - result[high(result)] := N.Text; + setlength(Keys,length(Keys)+1); + Keys[high(Keys)] := N.Text; N := N.GetNextSibling; end; end; @@ -345,13 +334,41 @@ begin Exit(''); 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 it does not exist, create the key - with a possible path, set it to defVal and return defVal. } -function TMMLSettings.GetSetDefaultKeyValue(KeyName, defVal: String): String; +function TMMLSettings.GetKeyValueDef(KeyName, defVal: String): String; var Res: String; begin @@ -421,26 +438,26 @@ begin 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 Nodes.Clear; LoadFromXML(fileName); - Result := GetSetDefaultKeyValue(KeyName, defVal); + Result := GetKeyValueDef(KeyName, defVal); SaveToXML(fileName); 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 if KeyExists(KeyName) then - Exit(GetSetDefaultKeyValue(KeyName, defVal)) + Exit(GetKeyValueDef(KeyName, defVal)) else - Exit(GetSetLoadSaveDefaultKeyValue(KeyName, defVal, fileName)); + Exit(GetKeyValueDefLoadFirst(KeyName, defVal, fileName)); end; { @@ -449,7 +466,7 @@ end; 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 N, newN, nParent: TTreeNode; Path: TStringArray; @@ -524,15 +541,18 @@ end; Set the value of a key. } -procedure TMMLSettings.SetKeyValue(KeyName: String; KeyValue: String); +function TMMLSettings.SetKeyValue(KeyName: String; KeyValue: String; CreatePath: boolean) : boolean; var N, NN: TTreeNode; begin - if not KeyExists(KeyName) then + result := false; + if not KeyExists(KeyName) and not CreatePath then begin mDebugLn('SetKeyValue - Key does not exist'); Exit; - end; + end else + if not KeyExists(keyname) then + CreateKey(keyname); if not IsKey(KeyName) then begin mDebugLn('SetKeyValue - IsKey returned false'); @@ -558,6 +578,7 @@ begin mDebugLn('Setting ' + KeyName + ' to ' + KeyValue); N := N.GetNextSibling; end; + result := true; end; diff --git a/Units/MMLAddon/settingssandbox.pas b/Units/MMLAddon/settingssandbox.pas index a5b20e9..e2e0355 100644 --- a/Units/MMLAddon/settingssandbox.pas +++ b/Units/MMLAddon/settingssandbox.pas @@ -30,26 +30,28 @@ uses Classes, SysUtils, settings, strutils, mufasatypes; type + + { TMMLSettingsSandbox } + TMMLSettingsSandbox = class(TObject) - public - constructor Create(sett: TMMLSettings); - destructor Destroy; override; - - function IsKey(KeyName: String): Boolean; - function IsDirectory(KeyName: String): Boolean; - function GetKeyValue(KeyName: String): String; - function GetSetDefaultKeyValue(KeyName, defVal: String): String; - 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; + private + ST: TMMLSettings; + FPrefix: String; + function GetPrefix: String; + procedure SetPrefix(s: String); + public + constructor Create(sett: TMMLSettings); + destructor Destroy; override; + 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; implementation @@ -70,17 +72,17 @@ end; function TMMLSettingsSandbox.GetPrefix: String; begin - result := Prefix; + result := FPrefix; end; procedure TMMLSettingsSandbox.SetPrefix(s: String); begin - Prefix := s; + FPrefix := s; end; -function TMMLSettingsSandbox.ListKeys(KeyName: String): TStringArray; +function TMMLSettingsSandbox.ListKeys(KeyName: String; out Keys :TStringArray): boolean; begin - exit(ST.ListKeys(Prefix + KeyName)) + exit(ST.ListKeys(Prefix + KeyName,keys)) end; function TMMLSettingsSandbox.GetKeyValue(KeyName: String): String; @@ -88,9 +90,9 @@ begin exit(ST.GetKeyValue(Prefix + KeyName)) end; -function TMMLSettingsSandbox.GetSetDefaultKeyValue(KeyName, defVal: String): String; +function TMMLSettingsSandbox.GetKeyValueDef(KeyName, defVal: String): String; begin - exit(ST.GetSetDefaultKeyValue(Prefix + KeyName, defVal)) + exit(ST.GetKeyValueDef(Prefix + KeyName, defVal)) end; function TMMLSettingsSandbox.IsKey(KeyName: String): Boolean; @@ -103,6 +105,14 @@ begin exit(ST.IsDirectory(Prefix + KeyName)) 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; begin exit(ST.DeleteKey(Prefix + KeyName));