From ce484ce148d1db51ddb3cda575786f0871843cb3 Mon Sep 17 00:00:00 2001 From: eddie-0815 Date: Tue, 20 Nov 2007 21:02:37 +0000 Subject: Changed Platform from Interface to Class. Added TerminateIfAlreadyRunning and GetGamePath to UPlatform.pas. Fixed a bug in THookManager.Create ("SpacetoAllocate-1"). git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@617 b956fd51-792f-4845-bead-9b4dfca2ff2c --- Game/Code/Classes/UCommon.pas | 92 ++-------------------------------- Game/Code/Classes/UHooks.pas | 2 +- Game/Code/Classes/UMain.pas | 55 ++------------------ Game/Code/Classes/UPlatform.pas | 46 ++++++++++++++--- Game/Code/Classes/UPlatformLinux.pas | 6 +-- Game/Code/Classes/UPlatformMacOSX.pas | 26 ++++++++-- Game/Code/Classes/UPlatformWindows.pas | 35 +++++++++++-- 7 files changed, 105 insertions(+), 157 deletions(-) diff --git a/Game/Code/Classes/UCommon.pas b/Game/Code/Classes/UCommon.pas index 65d98e30..fb74af0b 100644 --- a/Game/Code/Classes/UCommon.pas +++ b/Game/Code/Classes/UCommon.pas @@ -64,24 +64,7 @@ function AdaptFilePaths( const aPath : widestring ): widestring; procedure ZeroMemory( Destination: Pointer; Length: DWORD ); {$ENDIF} -{$IFNDEF FPC} -type - TSearchRecW = record - Time: Integer; - Size: Integer; - Attr: Integer; - Name: WideString; - ExcludeAttr: Integer; - FindHandle: THandle; - FindData: TWin32FindDataW; - end; - - function FindFirstW(const Path: WideString; Attr: Integer; var F: TSearchRecW): Integer; - function FindNextW(var F: TSearchRecW): Integer; - procedure FindCloseW(var F: TSearchRecW); - function FindMatchingFileW(var F: TSearchRecW): Integer; - function DirectoryExistsW(const Directory: widestring): Boolean; -{$endif} +// eddie: FindFirstW etc are now in UPlatformWindows.pas implementation @@ -225,77 +208,8 @@ end; procedure DeallocateHWnd(Wnd: HWND); begin end; -{$ENDIF} - - - - -{$ENDIF} - -{$ifNdef FPC} -function FindFirstW(const Path: widestring; Attr: Integer; var F: TSearchRecW): Integer; -const - faSpecial = faHidden or faSysFile or faVolumeID or faDirectory; -begin - F.ExcludeAttr := not Attr and faSpecial; - F.FindHandle := FindFirstFileW(PWideChar(Path), F.FindData); - if F.FindHandle <> INVALID_HANDLE_VALUE then - begin - Result := FindMatchingFileW(F); - if Result <> 0 then FindCloseW(F); - end else - Result := GetLastError; -end; - -function FindNextW(var F: TSearchRecW): Integer; -begin - if FindNextFileW(F.FindHandle, F.FindData) then - Result := FindMatchingFileW(F) - else - Result := GetLastError; -end; - -procedure FindCloseW(var F: TSearchRecW); -begin - if F.FindHandle <> INVALID_HANDLE_VALUE then - begin - Windows.FindClose(F.FindHandle); - F.FindHandle := INVALID_HANDLE_VALUE; - end; -end; - -function FindMatchingFileW(var F: TSearchRecW): Integer; -var - LocalFileTime: TFileTime; -begin - with F do - begin - while FindData.dwFileAttributes and ExcludeAttr <> 0 do - if not FindNextFileW(FindHandle, FindData) then - begin - Result := GetLastError; - Exit; - end; - FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime); - FileTimeToDosDateTime(LocalFileTime, LongRec(Time).Hi, LongRec(Time).Lo); - Size := FindData.nFileSizeLow; - Attr := FindData.dwFileAttributes; - Name := FindData.cFileName; - end; - Result := 0; -end; - -function DirectoryExistsW(const Directory: widestring): Boolean; -var - Code: Integer; -begin - Code := GetFileAttributesW(PWideChar(Directory)); - Result := (Code <> -1) and (FILE_ATTRIBUTE_DIRECTORY and Code <> 0); -end; -{$endif} - - - +{$ENDIF} // IFDEF DARWIN +{$ENDIF} // IFDEF FPC end. diff --git a/Game/Code/Classes/UHooks.pas b/Game/Code/Classes/UHooks.pas index 3e22bc75..8b33959d 100644 --- a/Game/Code/Classes/UHooks.pas +++ b/Game/Code/Classes/UHooks.pas @@ -76,7 +76,7 @@ var I: Integer; begin //Get the Space and "Zero" it SetLength (Events, SpacetoAllocate); - For I := 0 to SpacetoAllocate do + For I := 0 to SpacetoAllocate-1 do Events[I].Name[1] := chr(0); SpaceinEvents := SpacetoAllocate; diff --git a/Game/Code/Classes/UMain.pas b/Game/Code/Classes/UMain.pas index c11b68d9..019e638a 100644 --- a/Game/Code/Classes/UMain.pas +++ b/Game/Code/Classes/UMain.pas @@ -125,7 +125,7 @@ implementation uses USongs, UJoystick, math, UCommandLine, ULanguage, SDL_ttf, USkins, UCovers, UCatCovers, UDataBase, UPlaylist, UDLLManager, - UParty, UCore, UGraphicClasses, UPluginDefs; + UParty, UCore, UGraphicClasses, UPluginDefs, UPlatform; const Version = 'UltraStar Deluxe V 1.10 Alpha Build'; @@ -133,36 +133,14 @@ const Procedure Main; var WndTitle: string; - hWnd: THandle; - I: Integer; begin try WndTitle := Version; - {$IFDEF MSWINDOWS} - //------------------------------ - //Start more than One Time Prevention - //------------------------------ - hWnd:= FindWindow(nil, PChar(WndTitle)); - //Programm already started - if (hWnd <> 0) then - begin - I := Messagebox(0, PChar('Another Instance of Ultrastar is already running. Continue ?'), PChar(WndTitle), MB_ICONWARNING or MB_YESNO); - if (I = IDYes) then - begin - I := 1; - repeat - Inc(I); - hWnd := FindWindow(nil, PChar(WndTitle + ' Instance ' + InttoStr(I))); - until (hWnd = 0); - WndTitle := WndTitle + ' Instance ' + InttoStr(I); - end - else - Exit; - end; - {$ENDIF} - + if Platform.TerminateIfAlreadyRunning( {var} WndTitle) then + Exit; + //------------------------------ //StartUp - Create Classes and Load Files //------------------------------ @@ -1008,25 +986,6 @@ begin Player[PlayerNum].ScoreTotalI := 0; end; -{$IFDEF DARWIN} -// Mac applications are packaged in directories. -// We have to cut the last two directories -// to get the application directory. -Function GetGamePath : String; -var - x, - i : integer; -begin - Result := ExtractFilePath(ParamStr(0)); - for x := 0 to 2 do begin - i := Length(Result); - repeat - Delete( Result, i, 1); - i := Length(Result); - until (i = 0) or (Result[i] = '/'); - end; -end; -{$ENDIF} //-------------------- // Function sets all Absolute Paths e.g. Song Path and makes sure the Directorys exist @@ -1062,11 +1021,7 @@ procedure InitializePaths; begin -{$IFDEF DARWIN} - GamePath := GetGamePath; -{$ELSE} - GamePath := ExtractFilePath(ParamStr(0)); -{$ENDIF} + GamePath := Platform.GetGamePath; initialize_path( LogPath , GamePath ); initialize_path( SoundPath , GamePath + 'Sounds' + PathDelim ); diff --git a/Game/Code/Classes/UPlatform.pas b/Game/Code/Classes/UPlatform.pas index cc971bed..878c1ec2 100644 --- a/Game/Code/Classes/UPlatform.pas +++ b/Game/Code/Classes/UPlatform.pas @@ -25,20 +25,25 @@ type TDirectoryEntryArray = Array of TDirectoryEntry; - IPlatform = interface - - // DirectoryFindFiles returns all files matching the filter. Do not use '*' in the filter. - // If you set ReturnAllSubDirs = true all directories will be returned, if yout set it to false - // directories are completely ignored. - Function DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; + TPlatform = class + + // DirectoryFindFiles returns all files matching the filter. Do not use '*' in the filter. + // If you set ReturnAllSubDirs = true all directories will be returned, if yout set it to false + // directories are completely ignored. + Function DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; virtual; abstract; + + function TerminateIfAlreadyRunning(var WndTitle : String) : Boolean; virtual; + + function GetGamePath : WideString; virtual; end; var - Platform : IPlatform; + Platform : TPlatform; implementation uses + SysUtils, {$IFDEF MSWINDOWS} UPlatformWindows; {$ENDIF} @@ -49,8 +54,33 @@ uses UPlatformMacOSX; {$ENDIF} +{ TPlatform } + +function TPlatform.GetGamePath: WideString; +begin + // Windows and Linux use this: + Result := ExtractFilePath(ParamStr(0)); +end; + +function TPlatform.TerminateIfAlreadyRunning(var WndTitle : String) : Boolean; +begin + // Linux and Mac don't check for running apps at the moment + Result := false; +end; + initialization - Platform := TPlatform.Create; + {$IFDEF MSWINDOWS} + Platform := TPlatformWindows.Create; + {$ENDIF} + {$IFDEF LINUX} + Platform := TPlatformLinux.Create; + {$ENDIF} + {$IFDEF DARWIN} + Platform := TPlatformMacOSX.Create; + {$ENDIF} + +finalization + Platform.Free; end. diff --git a/Game/Code/Classes/UPlatformLinux.pas b/Game/Code/Classes/UPlatformLinux.pas index fd06097d..e6d90e20 100644 --- a/Game/Code/Classes/UPlatformLinux.pas +++ b/Game/Code/Classes/UPlatformLinux.pas @@ -12,16 +12,16 @@ uses Classes, UPlatform; type - TPlatform = class(TInterfacedObject, IPlatform) + TPlatform = class(TPlatform) public - Function DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; + Function DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; override; end; implementation uses SysUtils, oldlinux; -Function TPlatform.DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; +Function TPlatformLinux.DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; var i : Integer; TheDir : oldlinux.pdir; diff --git a/Game/Code/Classes/UPlatformMacOSX.pas b/Game/Code/Classes/UPlatformMacOSX.pas index 56469299..4e0c9061 100644 --- a/Game/Code/Classes/UPlatformMacOSX.pas +++ b/Game/Code/Classes/UPlatformMacOSX.pas @@ -12,16 +12,36 @@ uses Classes, UPlatform; type - TPlatform = class(TInterfacedObject, IPlatform) + TPlatformMacOSX = class(TPlatform) + private public - Function DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; + Function DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; override; + function GetGamePath: WideString; override; end; implementation uses SysUtils, baseunix; -Function TPlatform.DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; +// Mac applications are packaged in directories. +// We have to cut the last two directories +// to get the application directory. +Function TPlatformMacOSX.GetGamePath : WideString; +var + x, + i : integer; +begin + Result := ExtractFilePath(ParamStr(0)); + for x := 0 to 2 do begin + i := Length(Result); + repeat + Delete( Result, i, 1); + i := Length(Result); + until (i = 0) or (Result[i] = '/'); + end; +end; + +Function TPlatformMacOSX.DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; var i : Integer; TheDir : pdir; diff --git a/Game/Code/Classes/UPlatformWindows.pas b/Game/Code/Classes/UPlatformWindows.pas index 7e65d700..afdcebcf 100644 --- a/Game/Code/Classes/UPlatformWindows.pas +++ b/Game/Code/Classes/UPlatformWindows.pas @@ -12,9 +12,10 @@ uses Classes, UPlatform; type - TPlatform = class(TInterfacedObject, IPlatform) + TPlatformWindows = class(TPlatform) public - Function DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; + Function DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; override; + function TerminateIfAlreadyRunning(var WndTitle : String) : Boolean; override; end; implementation @@ -99,7 +100,35 @@ begin Result := (Code <> -1) and (FILE_ATTRIBUTE_DIRECTORY and Code <> 0); end; -Function TPlatform.DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; +//------------------------------ +//Start more than One Time Prevention +//------------------------------ +function TPlatformWindows.TerminateIfAlreadyRunning(var WndTitle : String) : Boolean; +var + hWnd: THandle; + I: Integer; +begin + Result := false; + hWnd:= FindWindow(nil, PChar(WndTitle)); + //Programm already started + if (hWnd <> 0) then + begin + I := Messagebox(0, PChar('Another Instance of Ultrastar is already running. Continue ?'), PChar(WndTitle), MB_ICONWARNING or MB_YESNO); + if (I = IDYes) then + begin + I := 1; + repeat + Inc(I); + hWnd := FindWindow(nil, PChar(WndTitle + ' Instance ' + InttoStr(I))); + until (hWnd = 0); + WndTitle := WndTitle + ' Instance ' + InttoStr(I); + end + else + Result := true; + end; +end; + +Function TPlatformWindows.DirectoryFindFiles(Dir, Filter : WideString; ReturnAllSubDirs : Boolean) : TDirectoryEntryArray; var i : Integer; SR : TSearchRecW; -- cgit v1.2.3