Allow #ENDSHORT tags

Detect where Preview and ShortEnd came from
Introduce a function to check whether a given GameMode is available for a song
This commit is contained in:
Flamefire committed 2013-10-12 21:42:03 +02:00
1 parent 87ba525811
commit b12e4156fa
14 files changed
+167 -97

No files matched your search

+54 -31
View File
@@ -44,12 +44,11 @@ namespace VocaluxeLib.Songs
Artist = 2,
MP3 = 4,
BPM = 8,
PreviewStart = 16,
MedleyStartBeat = 32,
MedleyEndBeat = 64
MedleyStartBeat = 16,
MedleyEndBeat = 32
}
public enum EMedleySource
public enum EDataSource
{
None = 0,
Calculated,
@@ -58,13 +57,25 @@ namespace VocaluxeLib.Songs
public struct SMedley
{
public EMedleySource Source;
public EDataSource Source;
public int StartBeat;
public int EndBeat;
public float FadeInTime;
public float FadeOutTime;
}
public struct SShortEnd
{
public EDataSource Source;
public int EndBeat;
}
public struct SPreview
{
public EDataSource Source;
public float StartTime;
}
public partial class CSong
{
private CTexture _CoverTextureSmall;
@@ -73,12 +84,12 @@ namespace VocaluxeLib.Songs
public SMedley Medley;
private bool _CalculateMedley = true;
public float PreviewStart;
public SPreview Preview;
public int ShortEnd;
public SShortEnd ShortEnd;
public Encoding Encoding = Encoding.Default;
public bool ManualEncoding = false;
public bool ManualEncoding;
public string Folder = String.Empty;
public string FolderName = String.Empty;
public string FileName = String.Empty;
@@ -167,19 +178,30 @@ namespace VocaluxeLib.Songs
// Notes
public readonly CNotes Notes = new CNotes();
public EGameMode[] AvailableGameModes
public IList<EGameMode> AvailableGameModes
{
get
{
var gms = new List<EGameMode> {IsDuet ? EGameMode.TR_GAMEMODE_DUET : EGameMode.TR_GAMEMODE_NORMAL};
if (Medley.Source != EMedleySource.None)
if (Medley.Source != EDataSource.None)
gms.Add(EGameMode.TR_GAMEMODE_MEDLEY);
gms.Add(EGameMode.TR_GAMEMODE_SHORTSONG);
if (ShortEnd.Source != EDataSource.None)
gms.Add(EGameMode.TR_GAMEMODE_SHORTSONG);
return gms.ToArray();
return gms;
}
}
/// <summary>
/// Returns true if the requested game mode is available
/// </summary>
/// <param name="gameMode"></param>
/// <returns>true if the requested game mode is available</returns>
public bool IsGameModeAvailable(EGameMode gameMode)
{
return AvailableGameModes.Any(gm => gm == gameMode);
}
//No point creating a song without a text file --> Use factory method LoadSong
private CSong() {}
@@ -191,7 +213,7 @@ namespace VocaluxeLib.Songs
Medley = song.Medley;
_CalculateMedley = song._CalculateMedley;
PreviewStart = song.PreviewStart;
Preview = song.Preview;
ShortEnd = song.ShortEnd;
@@ -380,13 +402,10 @@ namespace VocaluxeLib.Songs
{
if (IsDuet)
{
Medley.Source = EMedleySource.None;
Medley.Source = EDataSource.None;
return;
}
if (Medley.Source == EMedleySource.Tag)
return;
if (!_CalculateMedley)
return;
@@ -410,41 +429,44 @@ namespace VocaluxeLib.Songs
Medley.StartBeat = voice.Lines[series[longest].Start].FirstNoteBeat;
Medley.EndBeat = voice.Lines[series[longest].End].LastNoteBeat;
bool foundEnd = CBase.Game.GetTimeFromBeats(Medley.StartBeat, BPM) + CBase.Settings.GetMedleyMinDuration() >
CBase.Game.GetTimeFromBeats(Medley.EndBeat, BPM);
bool foundEnd = CBase.Game.GetTimeFromBeats(Medley.EndBeat, BPM) - CBase.Game.GetTimeFromBeats(Medley.StartBeat, BPM) < CBase.Settings.GetMedleyMinDuration();
// set end if duration > MedleyMinDuration
// set end if duration < MedleyMinDuration
if (!foundEnd)
{
for (int i = series[longest].Start + 1; i < voice.NumLines - 1; i++)
for (int i = series[longest].End + 1; i < voice.NumLines - 1; i++)
{
if (CBase.Game.GetTimeFromBeats(Medley.StartBeat, BPM) + CBase.Settings.GetMedleyMinDuration() >
CBase.Game.GetTimeFromBeats(voice.Lines[i].LastNoteBeat, BPM))
if (CBase.Game.GetTimeFromBeats(voice.Lines[i].LastNoteBeat, BPM) - CBase.Game.GetTimeFromBeats(Medley.StartBeat, BPM) <
CBase.Settings.GetMedleyMinDuration())
{
foundEnd = true;
Medley.EndBeat = voice.Lines[i].LastNoteBeat;
break;
}
}
}
if (foundEnd)
{
Medley.Source = EMedleySource.Calculated;
Medley.Source = EDataSource.Calculated;
Medley.FadeInTime = CBase.Settings.GetDefaultMedleyFadeInTime();
Medley.FadeOutTime = CBase.Settings.GetDefaultMedleyFadeOutTime();
}
}
if (Math.Abs(PreviewStart) < 0.001)
if (Preview.Source == EDataSource.None)
{
if (Medley.Source == EMedleySource.Calculated)
PreviewStart = CBase.Game.GetTimeFromBeats(Medley.StartBeat, BPM);
if (Medley.Source != EDataSource.None)
Preview.StartTime = CBase.Game.GetTimeFromBeats(Medley.StartBeat, BPM);
}
}
private void _FindShortEnd()
{
if (ShortEnd.Source != EDataSource.None)
return;
List<SSeries> series = _GetSeries();
if (series == null)
return;
@@ -461,10 +483,10 @@ namespace VocaluxeLib.Songs
{
if (stop < (voice.Lines[series[i].Start].FirstNoteBeat + ((voice.Lines[series[i].End].LastNoteBeat - voice.Lines[series[i].Start].FirstNoteBeat) / 2)))
{
ShortEnd = voice.Lines[series[i].Start - 1].LastNote.EndBeat;
ShortEnd.EndBeat = voice.Lines[series[i].Start - 1].LastNote.EndBeat;
return;
}
ShortEnd = voice.Lines[series[i].End].LastNote.EndBeat;
ShortEnd.EndBeat = voice.Lines[series[i].End].LastNote.EndBeat;
return;
}
}
@@ -474,12 +496,13 @@ namespace VocaluxeLib.Songs
{
if (line.FirstNoteBeat < stop && line.LastNoteBeat > stop)
{
ShortEnd = line.LastNoteBeat;
ShortEnd.EndBeat = line.LastNoteBeat;
return;
}
}
ShortEnd = stop;
ShortEnd.EndBeat = stop;
ShortEnd.Source = EDataSource.Calculated;
}
}
}
+2 -2
View File
@@ -82,12 +82,12 @@ namespace VocaluxeLib.Songs
//Check for overlapping notes
if (insPos >= 0) //Check note before
{
if (_Notes[insPos].EndBeat > note.StartBeat)
if (_Notes[insPos].EndBeat >= note.StartBeat)
return false;
}
if (insPos < _Notes.Count - 1) //Check note after
{
if (_Notes[insPos + 1].StartBeat < note.EndBeat)
if (_Notes[insPos + 1].StartBeat <= note.EndBeat)
return false;
}
_Notes.Insert(insPos + 1, note);
+33 -19
View File
@@ -77,7 +77,7 @@ namespace VocaluxeLib.Songs
_LogMsg(msg, false, withLineNr);
}
public bool ReadHeader(bool ignoreSetEncoding = false)
public bool ReadHeader(bool useSetEncoding = false)
{
string filePath = Path.Combine(_Song.Folder, _Song.FileName);
@@ -89,13 +89,17 @@ namespace VocaluxeLib.Songs
_Song.UnknownTags.Clear();
_Song._Comment = "";
_Song.ManualEncoding = false;
_Song.Medley.Source = EDataSource.None;
_Song._CalculateMedley = false;
_Song.Preview.Source = EDataSource.None;
_Song.ShortEnd.Source = EDataSource.None;
var headerFlags = new EHeaderFlags();
StreamReader sr = null;
_LineNr = 0;
try
{
sr = new StreamReader(filePath, _Song.Encoding, true);
sr = useSetEncoding ? new StreamReader(filePath, _Song.Encoding, true) : new StreamReader(filePath, true);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
@@ -134,7 +138,7 @@ namespace VocaluxeLib.Songs
_Song.ManualEncoding = true;
if (!newEncoding.Equals(sr.CurrentEncoding))
{
if (ignoreSetEncoding)
if (useSetEncoding)
{
_LogWarning("Duplicate encoding ignored");
continue;
@@ -268,17 +272,17 @@ namespace VocaluxeLib.Songs
_LogWarning("Invalid end");
break;
case "PREVIEWSTART":
if (CHelper.TryParse(value, out _Song.PreviewStart) && _Song.PreviewStart >= 0f)
headerFlags |= EHeaderFlags.PreviewStart;
if (CHelper.TryParse(value, out _Song.Preview.StartTime) && _Song.Preview.StartTime >= 0f)
_Song.Preview.Source = EDataSource.Tag;
else
_LogWarning("Invalid previewstart");
break;
case "PREVIEW":
if (CHelper.TryParse(value, out _Song.PreviewStart) && _Song.PreviewStart >= 0f)
if (CHelper.TryParse(value, out _Song.Preview.StartTime) && _Song.Preview.StartTime >= 0f)
{
//This is stored in ms not like PREVIEWSTART!
_Song.PreviewStart /= 1000f;
headerFlags |= EHeaderFlags.PreviewStart;
_Song.Preview.StartTime /= 1000f;
_Song.Preview.Source = EDataSource.Tag;
}
else
_LogWarning("Invalid previewstart");
@@ -295,6 +299,19 @@ namespace VocaluxeLib.Songs
else
_LogWarning("Invalid medleyendbeat");
break;
case "ENDSHORT":
if ((headerFlags & EHeaderFlags.BPM) != 0)
{
int endTime;
if (int.TryParse(value, out endTime) || endTime < 0)
{
_Song.ShortEnd.EndBeat = (int)CBase.Game.GetBeatFromTime(endTime / 1000f, _Song.BPM, _Song.Gap);
_Song.ShortEnd.Source = EDataSource.Tag;
}
else
_LogWarning("Invalid medleyendbeat");
}
break;
case "CALCMEDLEY":
if (value.ToUpper() == "OFF")
_Song._CalculateMedley = false;
@@ -373,15 +390,15 @@ namespace VocaluxeLib.Songs
}
}
if ((headerFlags & EHeaderFlags.PreviewStart) == 0 || _Song.PreviewStart < 0)
if (_Song.Preview.Source == EDataSource.None)
{
//PreviewStart is not set or <=0
_Song.PreviewStart = (headerFlags & EHeaderFlags.MedleyStartBeat) != 0 ? CBase.Game.GetTimeFromBeats(_Song.Medley.StartBeat, _Song.BPM) : 0f;
_Song.Preview.StartTime = (headerFlags & EHeaderFlags.MedleyStartBeat) != 0 ? CBase.Game.GetTimeFromBeats(_Song.Medley.StartBeat, _Song.BPM) : 0f;
}
if ((headerFlags & EHeaderFlags.MedleyStartBeat) != 0 && (headerFlags & EHeaderFlags.MedleyEndBeat) != 0)
{
_Song.Medley.Source = EMedleySource.Tag;
_Song.Medley.Source = EDataSource.Tag;
_Song.Medley.FadeInTime = CBase.Settings.GetDefaultMedleyFadeInTime();
_Song.Medley.FadeOutTime = CBase.Settings.GetDefaultMedleyFadeOutTime();
}
@@ -394,6 +411,7 @@ namespace VocaluxeLib.Songs
_LogError("Error reading txt header" + e.Message, false);
return false;
}
_Song.Encoding = sr.CurrentEncoding;
sr.Dispose();
_Song._CheckFiles();
@@ -485,7 +503,7 @@ namespace VocaluxeLib.Songs
StreamReader sr = null;
try
{
sr = new StreamReader(filePath, _Song.Encoding);
sr = new StreamReader(filePath, _Song.Encoding, true);
_Song.Notes.Reset();
@@ -544,8 +562,8 @@ namespace VocaluxeLib.Songs
sr.Dispose();
return false;
}
string text = noteData[3];
if (text.Trim() == "")
string text = noteData[3].TrimMultipleWs();
if (text == "")
{
_LogWarning("Ignored note without text");
changesMade.NoTextNoteCt++;
@@ -579,7 +597,6 @@ namespace VocaluxeLib.Songs
if (_Song.Relative)
beat += currentBeat;
bool ignored = false;
foreach (int curPlayer in player.GetSetBits())
{
@@ -697,10 +714,7 @@ namespace VocaluxeLib.Songs
string msg = "Automatic changes have been made to " + filePath + " Please check result!\r\n" + changesMade;
CBase.Log.LogError("Warning:" + msg);
if (CBase.Config.GetSaveModifiedSongs() == EOffOn.TR_CONFIG_ON)
{
string name = Path.GetFileNameWithoutExtension(_Song.FileName);
_Song.Save(Path.Combine(_Song.Folder, name + ".fix.txt"));
}
_Song.Save(filePath + ".fix");
}
return true;
}
+13 -3
View File
@@ -34,6 +34,12 @@ namespace VocaluxeLib.Songs
_WriteHeaderEntry(id, value.ToString());
}
private void _WriteHeaderEntry(string id, int value, int def = 0)
{
if (value != def)
_WriteHeaderEntry(id, value.ToString());
}
private void _WriteHeaderEntrys(string id, ICollection<string> value)
{
if (value == null || value.Count <= 0)
@@ -76,12 +82,15 @@ namespace VocaluxeLib.Songs
_WriteHeaderEntry("RELATIVE", _Song.Relative);
_WriteHeaderEntry("BPM", _Song.BPM / _BPMFactor);
_WriteHeaderEntry("GAP", _Song.Gap * 1000f);
_WriteHeaderEntry("PREVIEWSTART", _Song.PreviewStart);
if (_Song.Preview.Source == EDataSource.Tag)
_WriteHeaderEntry("PREVIEWSTART", _Song.Preview.StartTime);
_WriteHeaderEntry("START", _Song.Start);
_WriteHeaderEntry("END", _Song.Finish * 1000f);
if (!_Song._CalculateMedley)
if (_Song.ShortEnd.Source == EDataSource.Tag)
_WriteHeaderEntry("ENDSHORT", (int)(CBase.Game.GetTimeFromBeats(_Song.ShortEnd.EndBeat, _Song.BPM) + _Song.Gap) * 1000);
if (_Song.Medley.Source == EDataSource.None)
_WriteHeaderEntry("CALCMEDLEY", "OFF");
if (_Song.Medley.Source == EMedleySource.Tag)
if (_Song.Medley.Source == EDataSource.Tag)
{
_WriteHeaderEntry("MEDLEYSTARTBEAT", _Song.Medley.StartBeat);
_WriteHeaderEntry("MEDLEYENDBEAT", _Song.Medley.EndBeat);
@@ -139,6 +148,7 @@ namespace VocaluxeLib.Songs
}
}
}
tw.WriteLine("E");
}
public bool SaveFile(string filePath)