aboutsummaryrefslogblamecommitdiffstats
path: root/Game/Code/MacOSX/Wrapper/UMacResources.pp
blob: 65c817fc867f498515c078ea4e9b00d00235fd37 (plain) (tree)
1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
unit UMacResources;

interface

procedure init;

implementation

uses
  SysUtils,
  Classes;

procedure init;

  const
    PathName : string = '/Library/Application Support/UltraStarDeluxe/Resources';
    
  var
    RelativePath, BaseDir, OldBaseDir : string;
    SearchInfo : TSearchRec;
    DirectoryList, FileList : TStringList;
    DirectoryIsFinished : longint;
    counter : longint;

    UserPathName : string;
    SourceFile, TargetFile : TFileStream;
    FileCopyBuffer : array [1..4096] of byte;
    NumberOfBytes : integer;
    
  begin
    getdir (0, OldBaseDir);
    BaseDir := OldBaseDir + '/UltraStarDeluxe.app/Contents/Resources';
    chdir (BaseDir);
    UserPathName := GetEnvironmentVariable('HOME') + PathName;

    DirectoryIsFinished := 0;
    DirectoryList := TStringList.Create();
    FileList := TStringList.Create();
    DirectoryList.Add('.');
    repeat
      RelativePath := DirectoryList[DirectoryIsFinished];
      chdir (BaseDir + '/' + RelativePath);
      if (FindFirst('*', faAnyFile, SearchInfo) = 0) then
	repeat
	  if DirectoryExists(SearchInfo.Name) then
	  begin
	    if (SearchInfo.Name <> '.') and (SearchInfo.Name <> '..')  then
	      DirectoryList.Add(RelativePath + '/' + SearchInfo.Name);
	  end
	  else
	    Filelist.Add(RelativePath + '/' + SearchInfo.Name);
	until (FindNext(SearchInfo) <> 0);
      FindClose(SearchInfo);
      DirectoryIsFinished := succ(DirectoryIsFinished);
    until (DirectoryIsFinished = DirectoryList.Count);

    if not DirectoryExists(UserPathName) then
	mkdir (UserPathName);

    for counter := 0 to DirectoryList.Count-1 do
      if not DirectoryExists(UserPathName + '/' + DirectoryList[counter]) then
	mkdir (UserPathName + '/' + DirectoryList[counter]);
    DirectoryList.Free();

    for counter := 0 to Filelist.Count-1 do
      if not FileExists(UserPathName + '/' + Filelist[counter]) then
      begin
	SourceFile := TFileStream.Create(BaseDir      + '/' + Filelist[counter], fmOpenRead);
	TargetFile := TFileStream.Create(UserPathName + '/' + Filelist[counter], fmCreate);
	repeat
	  NumberOfBytes := SourceFile.Read(FileCopyBuffer, SizeOf(FileCopyBuffer));
	  TargetFile.Write(FileCopyBuffer, NumberOfBytes);
	until (NumberOfBytes < SizeOf(FileCopyBuffer));
	SourceFile.Free;
	TargetFile.Free;
      end;
    FileList.Free();

    chdir (OldBaseDir);

  end;

end.