aboutsummaryrefslogtreecommitdiffstats
path: root/cmake/src/base/UFiles.pas
blob: 5a258e3e0182a064fc778ca15484e1c707ec2b36 (plain) (blame)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
{* UltraStar Deluxe - Karaoke Game
 *
 * UltraStar Deluxe is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; see the file COPYING. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *}

unit UFiles;

interface

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}
{$I switches.inc}

uses
  SysUtils,
  Classes,
  ULog,
  UMusic,
  USongs,
  USong,
  UPath;

procedure ResetSingTemp;

type
  TSaveSongResult = (ssrOK, ssrFileError, ssrEncodingError);

{**
 * Throws a TEncodingException if the song's fields cannot be encoded in the
 * requested encoding.
 *}
function SaveSong(const Song: TSong; const Lines: TLines; const Name: IPath; Relative: boolean): TSaveSongResult;

implementation

uses
  TextGL,
  UIni,
  UNote,
  UPlatform,
  UUnicodeUtils,
  UTextEncoding;

//--------------------
// Resets the temporary Sentence Arrays for each Player and some other Variables
//--------------------
procedure ResetSingTemp;
var
  Count:  integer;
begin
  SetLength(Lines, Length(Player));
  for Count := 0 to High(Player) do begin
    SetLength(Lines[Count].Line, 1);
    SetLength(Lines[Count].Line[0].Note, 0);
    Lines[Count].Line[0].Lyric := '';
    Player[Count].Score := 0;
    Player[Count].LengthNote := 0;
    Player[Count].HighNote := -1;
  end;
end;

//--------------------
// Saves a Song
//--------------------
function SaveSong(const Song: TSong; const Lines: TLines; const Name: IPath; Relative: boolean): TSaveSongResult;
var
  C:      integer;
  N:      integer;
  S:      AnsiString;
  B:      integer;
  RelativeSubTime: integer;
  NoteState: AnsiString;
  SongFile: TTextFileStream;

  function EncodeToken(const Str: UTF8String): RawByteString;
  var
    Success: boolean;
  begin
    Success := EncodeStringUTF8(Str, Result, Song.Encoding);
    if (not Success) then
      SaveSong := ssrEncodingError;
  end;

  procedure WriteCustomTags;
    var
      I: integer;
      Line: RawByteString;
  begin
    for I := 0 to High(Song.CustomTags) do
    begin
      Line := EncodeToken(Song.CustomTags[I].Content);
      if (Length(Song.CustomTags[I].Tag) > 0) then
        Line := EncodeToken(Song.CustomTags[I].Tag) + ':' + Line;

      SongFile.WriteLine('#' + Line);
    end;

  end;

begin
  //  Relative := true; // override (idea - use shift+S to save with relative)
  Result := ssrOK;

  try
    SongFile := TMemTextFileStream.Create(Name, fmCreate);
    try
      // to-do: should we really write the BOM?
      //        it causes problems w/ older versions
      //        e.g. usdx 1.0.1a or ultrastar < 0.7.0
      if (Song.Encoding = encUTF8) then
        SongFile.WriteString(UTF8_BOM);

      SongFile.WriteLine('#ENCODING:' + EncodingName(Song.Encoding));
      SongFile.WriteLine('#TITLE:'    + EncodeToken(Song.Title));
      SongFile.WriteLine('#ARTIST:'   + EncodeToken(Song.Artist));

      if Song.Creator     <> ''        then SongFile.WriteLine('#CREATOR:'   + EncodeToken(Song.Creator));
      if Song.Edition     <> 'Unknown' then SongFile.WriteLine('#EDITION:'   + EncodeToken(Song.Edition));
      if Song.Genre       <> 'Unknown' then SongFile.WriteLine('#GENRE:'     + EncodeToken(Song.Genre));
      if Song.Language    <> 'Unknown' then SongFile.WriteLine('#LANGUAGE:'  + EncodeToken(Song.Language));
      if Song.Year        <> 0         then SongFile.WriteLine('#YEAR:'      + IntToStr(Song.Year));

      SongFile.WriteLine('#MP3:' + EncodeToken(Song.Mp3.ToUTF8));
      if Song.Cover.IsSet      then    SongFile.WriteLine('#COVER:'       + EncodeToken(Song.Cover.ToUTF8));
      if Song.Background.IsSet then    SongFile.WriteLine('#BACKGROUND:'  + EncodeToken(Song.Background.ToUTF8));
      if Song.Video.IsSet      then    SongFile.WriteLine('#VIDEO:'       + EncodeToken(Song.Video.ToUTF8));

      if Song.VideoGAP    <> 0  then    SongFile.WriteLine('#VIDEOGAP:'    + FloatToStr(Song.VideoGAP));
      if Song.Resolution  <> 4  then    SongFile.WriteLine('#RESOLUTION:'  + IntToStr(Song.Resolution));
      if Song.NotesGAP    <> 0  then    SongFile.WriteLine('#NOTESGAP:'    + IntToStr(Song.NotesGAP));
      if Song.Start       <> 0  then    SongFile.WriteLine('#START:'       + FloatToStr(Song.Start));
      if Song.Finish      <> 0  then    SongFile.WriteLine('#END:'         + IntToStr(Song.Finish));
      if Relative               then    SongFile.WriteLine('#RELATIVE:yes');

      SongFile.WriteLine('#BPM:' + FloatToStr(Song.BPM[0].BPM / 4));
      SongFile.WriteLine('#GAP:' + FloatToStr(Song.GAP));

      // write custom header tags
      WriteCustomTags;

      RelativeSubTime := 0;
      for B := 1 to High(Song.BPM) do
        SongFile.WriteLine('B ' + FloatToStr(Song.BPM[B].StartBeat) + ' '
                                + FloatToStr(Song.BPM[B].BPM/4));

      for C := 0 to Lines.High do
      begin
        for N := 0 to Lines.Line[C].HighNote do
        begin
          with Lines.Line[C].Note[N] do
          begin
            //Golden + Freestyle Note Patch
            case Lines.Line[C].Note[N].NoteType of
              ntFreestyle: NoteState := 'F ';
              ntNormal: NoteState := ': ';
              ntGolden: NoteState := '* ';
            end; // case
            S := NoteState + IntToStr(Start-RelativeSubTime) + ' '
                           + IntToStr(Length) + ' '
                           + IntToStr(Tone) + ' '
                           + EncodeToken(Text);

            SongFile.WriteLine(S);
          end; // with
        end; // N

        if C < Lines.High then // don't write end of last sentence
        begin
          if not Relative then
            S := '- ' + IntToStr(Lines.Line[C+1].Start)
          else
          begin
            S := '- ' + IntToStr(Lines.Line[C+1].Start - RelativeSubTime) +
              ' ' + IntToStr(Lines.Line[C+1].Start - RelativeSubTime);
            RelativeSubTime := Lines.Line[C+1].Start;
          end;
          SongFile.WriteLine(S);
        end;
      end; // C

      SongFile.WriteLine('E');
    finally
      SongFile.Free;
    end;
  except
    Result := ssrFileError;
  end;
end;

end.