src/uge2source/uge2source.lpr2.0 KB · ObjectPascal
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
program uge2source;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils, CustApp,
  Song, Codegen;

type

  { TUGE2Source }

  TUGE2Source = class(TCustomApplication)
  protected
    procedure DoRun; override;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure WriteHelp; virtual;
  end;

{ TUGE2Source }

procedure TUGE2Source.DoRun;
var
  ErrorMsg: String;
  Song: TSong;
  NonOpts: TStringArray;
  S: TStream;
  Bank: Integer;
begin
  // quick check parameters
  ErrorMsg:=CheckOptions('hb:', ['help', 'bank:']);
  if ErrorMsg<>'' then begin
    ShowException(Exception.Create(ErrorMsg));
    Terminate;
    Exit;
  end;

  // parse parameters
  if HasOption('h', 'help') or (ParamCount = 0) then begin
    WriteHelp;
    Terminate;
    Exit;
  end;

  // Grab non-options
  NonOpts := GetNonOptions('hb:', ['help', 'bank:']);

  S := TFileStream.Create(NonOpts[0], fmOpenRead);

  try
    ReadSongFromStream(S, Song);
  finally
    S.Free;
  end;

  if HasOption('b', 'bank') then
    Bank := StrToInt(GetOptionValue('b', 'bank'))
  else
    Bank := -1;

  case LowerCase(ExtractFileExt(NonOpts[2])) of
    '.c': begin
      RenderSongToGBDKC(Song, NonOpts[1], NonOpts[2], Bank);
    end;
    '.asm': begin
      RenderSongToRGBDSAsm(Song, NonOpts[1], NonOpts[2]);
    end;
    else raise Exception.Create('Output file must be either .C or .ASM, but was ' + NonOpts[2]);
  end;

  Terminate;
end;

constructor TUGE2Source.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  StopOnException:=True;
end;

destructor TUGE2Source.Destroy;
begin
  inherited Destroy;
end;

procedure TUGE2Source.WriteHelp;
begin
  writeln('Usage: ', ExtractFileName(ExeName), ' infile.uge [-b<BANK>] descriptor outfile.(asm|c)');
end;

var
  Application: TUGE2Source;
begin
  Application:=TUGE2Source.Create(nil);
  Application.Title:='uge2source';
  Application.Run;
  Application.Free;
end.