Serialize playlists
This commit is contained in:
1 parent
206a6cda35
commit
1698c1b027
10 files changed
+76
-96
No files matched your search
@@ -295,12 +295,12 @@ namespace ServerLib
|
||||
#endregion
|
||||
|
||||
#region playlist
|
||||
public SPlaylistInfo[] GetPlaylists()
|
||||
public SPlaylistData[] GetPlaylists()
|
||||
{
|
||||
return CServer.GetPlaylists();
|
||||
}
|
||||
|
||||
public SPlaylistInfo GetPlaylist(int playlistId)
|
||||
public SPlaylistData GetPlaylist(int playlistId)
|
||||
{
|
||||
return CServer.GetPlaylist(playlistId);
|
||||
}
|
||||
|
||||
@@ -128,9 +128,9 @@ namespace ServerLib
|
||||
#endregion
|
||||
|
||||
#region playlists
|
||||
public delegate SPlaylistInfo[] GetPlaylistsDelegate();
|
||||
public delegate SPlaylistData[] GetPlaylistsDelegate();
|
||||
|
||||
public delegate SPlaylistInfo GetPlaylistDelegate(int playlistId);
|
||||
public delegate SPlaylistData GetPlaylistDelegate(int playlistId);
|
||||
|
||||
public delegate void AddSongToPlaylistDelegate(int songId, int playlistId, bool allowDuplicates);
|
||||
|
||||
@@ -156,7 +156,7 @@ namespace ServerLib
|
||||
}
|
||||
|
||||
[DataContract]
|
||||
public struct SPlaylistInfo
|
||||
public struct SPlaylistData
|
||||
{
|
||||
[DataMember] public int PlaylistId;
|
||||
[DataMember] public string PlaylistName;
|
||||
|
||||
@@ -128,12 +128,12 @@ namespace ServerLib
|
||||
[OperationContract, WebInvoke(Method = "GET",
|
||||
ResponseFormat = WebMessageFormat.Json,
|
||||
UriTemplate = "/getPlaylists")]
|
||||
SPlaylistInfo[] GetPlaylists();
|
||||
SPlaylistData[] GetPlaylists();
|
||||
|
||||
[OperationContract, WebInvoke(Method = "GET",
|
||||
ResponseFormat = WebMessageFormat.Json,
|
||||
UriTemplate = "/getPlaylist?id={playlistId}")]
|
||||
SPlaylistInfo GetPlaylist(int playlistId);
|
||||
SPlaylistData GetPlaylist(int playlistId);
|
||||
|
||||
[OperationContract, WebInvoke(Method = "GET",
|
||||
ResponseFormat = WebMessageFormat.Json,
|
||||
|
||||
@@ -31,7 +31,6 @@ namespace Vocaluxe.Base
|
||||
static class CPlaylists
|
||||
{
|
||||
private static List<CPlaylistFile> _Playlists;
|
||||
private static int _NextID;
|
||||
|
||||
public static IEnumerable<CPlaylistFile> Playlists
|
||||
{
|
||||
@@ -78,7 +77,8 @@ namespace Vocaluxe.Base
|
||||
|
||||
foreach (string file in files)
|
||||
{
|
||||
CPlaylistFile playlist = new CPlaylistFile(files.IndexOf(file), file);
|
||||
CPlaylistFile playlist = new CPlaylistFile();
|
||||
if(playlist._Load(file))
|
||||
_Playlists.Add(playlist);
|
||||
}
|
||||
}
|
||||
@@ -130,7 +130,7 @@ namespace Vocaluxe.Base
|
||||
|
||||
public static int NewPlaylist(string name = "New Playlist")
|
||||
{
|
||||
CPlaylistFile pl = new CPlaylistFile(_NextID++) {Name = name};
|
||||
CPlaylistFile pl = new CPlaylistFile {Name = name};
|
||||
_Playlists.Add(pl);
|
||||
return pl.Id;
|
||||
}
|
||||
@@ -212,7 +212,7 @@ namespace Vocaluxe.Base
|
||||
|
||||
private static CPlaylistFile _ConvertUSDXPlaylist(string file)
|
||||
{
|
||||
var pl = new CPlaylistFile(_NextID++);
|
||||
var pl = new CPlaylistFile();
|
||||
ReadOnlyCollection<CSong> allSongs = CSongs.AllSongs;
|
||||
|
||||
if (!File.Exists(file))
|
||||
|
||||
@@ -453,13 +453,13 @@ namespace Vocaluxe.Base.Server
|
||||
#endregion
|
||||
|
||||
#region playlist
|
||||
private static SPlaylistInfo[] _GetPlaylists()
|
||||
private static SPlaylistData[] _GetPlaylists()
|
||||
{
|
||||
return (from p in CPlaylists.Playlists
|
||||
select _GetPlaylistInfo(p)).ToArray();
|
||||
}
|
||||
|
||||
private static SPlaylistInfo _GetPlaylist(int playlistId)
|
||||
private static SPlaylistData _GetPlaylist(int playlistId)
|
||||
{
|
||||
if (CPlaylists.Get(playlistId) == null)
|
||||
throw new ArgumentException("invalid playlistId");
|
||||
@@ -548,9 +548,9 @@ namespace Vocaluxe.Base.Server
|
||||
return result;
|
||||
}
|
||||
|
||||
private static SPlaylistInfo _GetPlaylistInfo(CPlaylistFile playlist)
|
||||
private static SPlaylistData _GetPlaylistInfo(CPlaylistFile playlist)
|
||||
{
|
||||
return new SPlaylistInfo
|
||||
return new SPlaylistData
|
||||
{
|
||||
PlaylistId = playlist.Id,
|
||||
PlaylistName = playlist.Name,
|
||||
|
||||
@@ -97,4 +97,15 @@ namespace Vocaluxe.Base
|
||||
public Dictionary<string, string> Skins;
|
||||
public Dictionary<string, string> Videos;
|
||||
}
|
||||
|
||||
struct SPlaylistInfo
|
||||
{
|
||||
[XmlElement("PlaylistName")] public string Name;
|
||||
}
|
||||
|
||||
struct SPlaylist
|
||||
{
|
||||
public SPlaylistInfo Info;
|
||||
[XmlArray] public SPlaylistSong[] Songs;
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
using Vocaluxe.Base;
|
||||
using VocaluxeLib;
|
||||
using VocaluxeLib.Menu;
|
||||
@@ -32,15 +31,14 @@ namespace Vocaluxe.Lib.Playlist
|
||||
{
|
||||
public string Name = "";
|
||||
public string File;
|
||||
public readonly int Id = -1;
|
||||
public readonly int Id;
|
||||
public List<CPlaylistSong> Songs = new List<CPlaylistSong>();
|
||||
|
||||
public CPlaylistFile(int id, string file = "")
|
||||
private static int _NextID;
|
||||
|
||||
public CPlaylistFile()
|
||||
{
|
||||
Id = id;
|
||||
File = file;
|
||||
if (File != "")
|
||||
_Load();
|
||||
Id = _NextID++;
|
||||
}
|
||||
|
||||
public void Save()
|
||||
@@ -62,90 +60,40 @@ namespace Vocaluxe.Lib.Playlist
|
||||
File = CHelper.GetUniqueFileName(Path.Combine(CSettings.DataFolder, CConfig.FolderPlaylists), filename + ".xml");
|
||||
}
|
||||
|
||||
XmlWriter writer;
|
||||
SPlaylist data=new SPlaylist {Info = {Name = Name}, Songs = Songs.Select(plSong => plSong.ToStruct()).ToArray()};
|
||||
|
||||
var xml = new CXmlSerializer();
|
||||
xml.Serialize(File,data);
|
||||
}
|
||||
|
||||
public bool _Load(string file)
|
||||
{
|
||||
File = file;
|
||||
SPlaylist data;
|
||||
try
|
||||
{
|
||||
writer = XmlWriter.Create(File, CConfig.XMLSettings);
|
||||
var xml = new CXmlSerializer();
|
||||
data = xml.Deserialize<SPlaylist>(File);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
CLog.LogError("Error creating/opening Playlist File " + File + ": " + e.Message);
|
||||
return;
|
||||
CLog.LogError("Cannot load playlist from "+file+": "+e.Message);
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
Name = data.Info.Name;
|
||||
Songs = new List<CPlaylistSong>();
|
||||
foreach (var songEntry in data.Songs)
|
||||
{
|
||||
writer.WriteStartDocument();
|
||||
writer.WriteStartElement("root");
|
||||
|
||||
writer.WriteStartElement("Info");
|
||||
writer.WriteElementString("PlaylistName", Name);
|
||||
writer.WriteEndElement();
|
||||
|
||||
writer.WriteStartElement("Songs");
|
||||
for (int i = 0; i < Songs.Count; i++)
|
||||
CSong plSong = CSongs.AllSongs.FirstOrDefault(song => song.Artist == songEntry.Artist && song.Title == songEntry.Title);
|
||||
if (plSong == null)
|
||||
CLog.LogError("Can't find song '" + songEntry.Title + "' from '" + songEntry.Artist + "' in playlist file: " + File);
|
||||
else
|
||||
{
|
||||
CSong song = CSongs.GetSong(Songs[i].SongID);
|
||||
if (song != null)
|
||||
{
|
||||
writer.WriteStartElement("Song" + (i + 1));
|
||||
writer.WriteElementString("Artist", song.Artist);
|
||||
writer.WriteElementString("Title", song.Title);
|
||||
writer.WriteElementString("GameMode", Enum.GetName(typeof(EGameMode), Songs[i].GameMode));
|
||||
writer.WriteEndElement();
|
||||
}
|
||||
else
|
||||
CLog.LogError("Playlist.SavePlaylist(): Can't find Song. This should never happen!");
|
||||
}
|
||||
writer.WriteEndElement();
|
||||
|
||||
writer.WriteEndElement(); //end of root
|
||||
writer.WriteEndDocument();
|
||||
|
||||
writer.Flush();
|
||||
}
|
||||
finally
|
||||
{
|
||||
writer.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private void _Load()
|
||||
{
|
||||
CXMLReader xmlReader = CXMLReader.OpenFile(File);
|
||||
if (xmlReader == null)
|
||||
return;
|
||||
|
||||
string value;
|
||||
if (xmlReader.GetValue("//root/Info/PlaylistName", out value, ""))
|
||||
{
|
||||
Name = value;
|
||||
|
||||
Songs = new List<CPlaylistSong>();
|
||||
|
||||
List<string> songs = xmlReader.GetNames("//root/Songs/*");
|
||||
|
||||
foreach (string songEntry in songs)
|
||||
{
|
||||
string artist;
|
||||
string title;
|
||||
xmlReader.GetValue("//root/Songs/" + songEntry + "/Artist", out artist, String.Empty);
|
||||
xmlReader.GetValue("//root/Songs/" + songEntry + "/Title", out title, String.Empty);
|
||||
EGameMode gm = EGameMode.TR_GAMEMODE_NORMAL;
|
||||
xmlReader.TryGetEnumValue("//root/Songs/" + songEntry + "/GameMode", ref gm);
|
||||
|
||||
CSong plSong = CSongs.AllSongs.FirstOrDefault(song => song.Artist == artist && song.Title == title);
|
||||
if (plSong != null)
|
||||
{
|
||||
var playlistSong = new CPlaylistSong(plSong.ID, gm);
|
||||
Songs.Add(playlistSong);
|
||||
}
|
||||
else
|
||||
CLog.LogError("Can't find song '" + title + "' from '" + artist + "' in playlist file: " + File);
|
||||
var playlistSong = new CPlaylistSong(plSong.ID, songEntry.GameMode);
|
||||
Songs.Add(playlistSong);
|
||||
}
|
||||
}
|
||||
else
|
||||
CLog.LogError("Can't find PlaylistName in Playlist File: " + File);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void AddSong(int songID)
|
||||
|
||||
@@ -46,6 +46,14 @@ namespace VocaluxeLib.Menu
|
||||
{
|
||||
GameMode = EGameMode.TR_GAMEMODE_NORMAL;
|
||||
}
|
||||
|
||||
public SPlaylistSong ToStruct()
|
||||
{
|
||||
CSong song = CBase.Songs.GetSongByID(SongID);
|
||||
if (song == null)
|
||||
throw new Exception("Can't find Song. This should never happen!");
|
||||
return new SPlaylistSong{Artist = song.Artist, Title = song.Title, GameMode = GameMode};
|
||||
}
|
||||
}
|
||||
|
||||
[XmlType("Playlist")]
|
||||
|
||||
@@ -108,6 +108,7 @@
|
||||
<Compile Include="Draw\CTextureRef.cs" />
|
||||
<Compile Include="Menu\IThemeable.cs" />
|
||||
<Compile Include="Menu\SingNotes\CNoteBars.cs" />
|
||||
<Compile Include="XmlStructs.cs" />
|
||||
<Compile Include="Xml\CTypeExtensions.cs" />
|
||||
<Compile Include="Xml\CXmlDeserializer.cs" />
|
||||
<Compile Include="Xml\CXMLReader.cs" />
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.ComponentModel;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace VocaluxeLib
|
||||
{
|
||||
[XmlType("Song")]
|
||||
public struct SPlaylistSong
|
||||
{
|
||||
public string Artist, Title;
|
||||
[DefaultValue(EGameMode.TR_GAMEMODE_NORMAL)] public EGameMode GameMode;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user