public void TestRealFiles() { Type[] types = new Type[] { typeof(SThemeCover), typeof(CConfig.SConfig), typeof(SThemeScreen), typeof(SDefaultFonts), typeof(SSkin), typeof(STheme), typeof(Dictionary <string, string>) }; string filePath = Path.Combine(new string[] { AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "TestXmlFiles" }); foreach (Type type in types) { string xmlPath = Path.Combine(filePath, type.Name + ".xml"); var deser = new CXmlDeserializer(); object foo = null; try { foo = deser.Deserialize(xmlPath, Activator.CreateInstance(type)); } catch (Exception e) { Assert.Fail("Exception with " + type.Name + ": {0}", new object[] { e.Message }); } Assert.IsInstanceOfType(foo, type, "Wrong type with " + type.Name); var ser = new CXmlSerializer(type == typeof(CConfig.SConfig)); string newXml = ser.Serialize(foo, type == typeof(Dictionary <string, string>) ? "resources" : null); // Typename will be uppercase but input is lowercase newXml = newXml.Replace("<String", "<string").Replace("</String", "</string"); string oldXml = File.ReadAllText(xmlPath); Assert.AreEqual(oldXml, newXml, "Error with " + type.Name); } }
public void TestBasic() { const string s = @"<root> <I>1</I> <S>2</S> <F>3</F> <D>4</D> </root>"; const string sUnordered = @"<root> <D>4</D> <I>1</I> <F>3</F> <S>2</S> </root>"; var xml = new CXmlDeserializer(); SBasic foo = xml.DeserializeString <SBasic>(s); Assert.AreEqual(1, foo.I); Assert.AreEqual("2", foo.S); Assert.AreEqual(3, foo.F, 0.0001); Assert.AreEqual(4, foo.D, 0.0001); foo = xml.DeserializeString <SBasic>(sUnordered); Assert.AreEqual(1, foo.I); Assert.AreEqual("2", foo.S); Assert.AreEqual(3, foo.F, 0.0001); Assert.AreEqual(4, foo.D, 0.0001); }
public void TestDefaultValues() { const string s = _Head + @"<root> <I>1</I> <S>2</S> <F>3</F> <D>4</D> <Sub> <I>22</I> </Sub> </root>"; _AssertSerDeserMatch <SDefault>(s); var xml = new CXmlDeserializer(new CXmlErrorHandler(exception => { })); SDefault foo = xml.DeserializeString <SDefault>(@"<root />"); Assert.AreEqual(foo.I, 1337); Assert.AreEqual(foo.F, null); Assert.AreEqual(foo.S, "Foo"); Assert.AreEqual(foo.D, 666); Assert.AreEqual(foo.Sub.I, 111); string newXml = new CXmlSerializer().Serialize(foo); Assert.AreEqual(_Head + @"<root> <Sub /> </root>", newXml); }
public bool _Load(string file) { File = file; SPlaylist data; try { var xml = new CXmlDeserializer(); data = xml.Deserialize <SPlaylist>(File); } catch (Exception e) { CLog.Error("Cannot load playlist from " + file + ": " + e.Message); return(false); } Name = data.Info.Name; Songs = new List <CPlaylistSong>(); foreach (SPlaylistSong songEntry in data.Songs) { CSong plSong = CSongs.AllSongs.FirstOrDefault(song => song.Artist == songEntry.Artist && song.Title == songEntry.Title); if (plSong == null) { CLog.Error("Can't find song '" + songEntry.Title + "' from '" + songEntry.Artist + "' in playlist file: " + File); } else { var playlistSong = new CPlaylistSong(plSong.ID, songEntry.GameMode); Songs.Add(playlistSong); } } return(true); }
/// <summary> /// Loads all cover-themes to list. /// </summary> private static void _LoadCoverThemes() { _CoverThemes.Clear(); string folderPath = Path.Combine(CSettings.ProgramFolder, CSettings.FolderNameCover); IEnumerable <string> files = CHelper.ListFiles(folderPath, "*.xml"); var xml = new CXmlDeserializer(); foreach (string file in files) { SThemeCover theme; try { theme = xml.Deserialize <SThemeCover>(Path.Combine(folderPath, file)); } catch (CXmlException e) { CLog.Error("Error loading cover theme " + file + ": " + e); continue; } if (!String.IsNullOrEmpty(theme.Info.Folder) && !String.IsNullOrEmpty(theme.Info.Name)) { theme.FolderPath = Path.Combine(folderPath, theme.Info.Folder); _CoverThemes.Add(theme); } } }
public bool LoadProfile() { var xml = new CXmlDeserializer(); try { xml.Deserialize(FilePath, this); //If ID couldn't be loaded, generate a new one and save it if (ID == Guid.Empty) { ID = Guid.NewGuid(); SaveProfile(); } } catch (Exception e) { if (_ConvertProfile(ref e)) { return(true); } CLog.Error("Error loading profile file " + Path.GetFileName(FilePath) + ": " + e.Message); return(false); } return(true); }
private bool _ConvertProfile(ref Exception e) { var xml = new CXmlDeserializer(); var ser = new CXmlSerializer(); try { var old = xml.Deserialize <SOldXmlProfile>(FilePath); string newXml = ser.Serialize(old.Info); xml.DeserializeString(newXml, this); if (ID == null) { ID = Guid.NewGuid(); } ser.Serialize(FilePath, this); } catch (Exception e2) { if (!(e2 is CXmlException)) { e = e2; } return(false); } return(true); }
public void TestArrayEmbedded() { var xml = new CXmlDeserializer(); var ser = new CXmlSerializer(); SArrayEmb foo = xml.DeserializeString <SArrayEmb>(_XmlListEmb); Assert.AreEqual(foo.Ints.Length, 1, "Deserialization failed"); Assert.AreEqual(foo.Ints[0].I, 1, "Deserialization failed"); string res = ser.Serialize(foo); Assert.AreEqual(_XmlListEmb, res, "Serialization failed"); foo = xml.DeserializeString <SArrayEmb>(_XmlListEmb2); Assert.AreEqual(foo.Ints.Length, 2, "Deserialization failed"); Assert.AreEqual(foo.Ints[1].I, 2, "Deserialization failed"); res = ser.Serialize(foo); Assert.AreEqual(_XmlListEmb2, res, "Serialization failed"); foo = xml.DeserializeString <SArrayEmb>(_XmlListEmb3); Assert.AreEqual(foo.Ints.Length, 0, "Deserialization failed"); res = ser.Serialize(foo); Assert.AreEqual(_XmlListEmb3, res, "Serialization failed"); ser = new CXmlSerializer(true); foo.Ints = new SEntry[0]; res = ser.Serialize(foo); Assert.AreEqual(_XmlListEmb4, res, "Serialization failed"); _AssertFail <SArrayEmb, CXmlException>(_Empty); }
#pragma warning restore 169 #pragma warning restore 649 private static void _AssertFail <T, T2>(String xmlString) where T2 : Exception where T : new() { var deserializer = new CXmlDeserializer(); Exception exception = Assert.Catch(() => deserializer.DeserializeString <T>(xmlString)); Assert.IsInstanceOf(typeof(T2), exception); }
private static T _AssertSerDeserMatch <T>(string xmlString) where T : new() { var deserializer = new CXmlDeserializer(); var serializer = new CXmlSerializer(); T foo = deserializer.DeserializeString <T>(xmlString); string xmlNew = serializer.Serialize(foo); Assert.AreEqual(xmlString, xmlNew); return(foo); }
/// <summary> /// Load default fonts /// </summary> /// <returns></returns> private static bool _LoadDefaultFonts() { SDefaultFonts defaultFonts; try { var xml = new CXmlDeserializer(); defaultFonts = xml.Deserialize <SDefaultFonts>(Path.Combine(CSettings.ProgramFolder, CSettings.FolderNameFonts, CSettings.FileNameFonts)); } catch (Exception e) { CLog.Error(e, "Error loading default Fonts"); return(false); } return(LoadThemeFonts(defaultFonts.Fonts, Path.Combine(CSettings.ProgramFolder, CSettings.FolderNameFonts), "", -1)); }
public bool LoadProfile() { var xml = new CXmlDeserializer(); try { xml.Deserialize(FilePath, this); } catch (Exception e) { if (_ConvertProfile(ref e)) { return(true); } CBase.Log.LogError("Error loading profile file " + Path.GetFileName(FilePath) + ": " + e.Message); return(false); } return(true); }
public void TestRealFiles([Values(typeof(SThemeCover), typeof(CConfig.SConfig), /*typeof(SThemeScreen),*/ typeof(SDefaultFonts), typeof(SSkin), typeof(STheme), typeof(Dictionary <string, string>))] Type type) { string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "VocaluxeLib", "XML", "TestFiles"); string xmlPath = Path.Combine(filePath, type.Name + ".xml"); var deser = new CXmlDeserializer(); object foo = deser.Deserialize(xmlPath, Activator.CreateInstance(type)); Assert.IsInstanceOf(type, foo, "Wrong type with " + type.Name); var ser = new CXmlSerializer(type == typeof(CConfig.SConfig)); string newXml = ser.Serialize(foo, type == typeof(Dictionary <string, string>) ? "resources" : null); // Typename will be uppercase but input is lowercase newXml = newXml.Replace("<String", "<string").Replace("</String", "</string"); string oldXml = File.ReadAllText(xmlPath); Assert.AreEqual(oldXml, newXml, "Recontructed XML has differences."); }
public bool Init() { try { var xml = new CXmlDeserializer(); _Data = xml.Deserialize <SSkin>(Path.Combine(_Folder, _FileName)); if (_Data.SkinSystemVersion != _SkinSystemVersion) { string errorMsg = _Data.SkinSystemVersion < _SkinSystemVersion ? "the file ist outdated!" : "the file is for newer program versions!"; errorMsg += " Current Version is " + _SkinSystemVersion; throw new Exception(errorMsg); } } catch (Exception e) { CLog.Error(e, "Can't load skin file \"" + Path.Combine(_Folder, _FileName) + "\". Invalid file!"); return(false); } return(true); }
private static bool _LoadLanguageEntries(string filePath, out Dictionary <string, string> texts) { var deser = new CXmlDeserializer(); try { texts = deser.Deserialize <Dictionary <string, string> >(filePath); string language; if (!texts.TryGetValue("language", out language)) { throw new Exception("'language' entry is missing"); } } catch (Exception e) { CLog.LogError("Error reading language file " + filePath + ": " + e.Message); texts = null; return(false); } return(true); }
public void TestExisting() { var foo = new SIgnore { I = 1 }; var xml = new CXmlDeserializer(); var ser = new CXmlSerializer(); SIgnore bar = xml.DeserializeString(_XmlIgnore, foo); Assert.AreEqual(1, bar.I); Assert.AreEqual(_XmlIgnore, ser.Serialize(bar)); var foo2 = new CIgnore { I = 1 }; CIgnore bar2 = xml.DeserializeString(_XmlIgnore, foo2); Assert.AreEqual(1, bar2.I); Assert.AreEqual(foo2.J, bar2.J, "Original classes should be modified by the deserialization"); Assert.AreEqual(_XmlIgnore, ser.Serialize(bar2)); }
public void TestArray() { var xml = new CXmlDeserializer(); var ser = new CXmlSerializer(); SArray foo = xml.DeserializeString <SArray>(_XmlList[0]); Assert.AreEqual(foo.Ints.Length, 2, "Deserialization failed"); Assert.AreEqual(foo.Ints[0].I, 1, "Deserialization failed"); Assert.AreEqual(foo.Ints[1].I, 1, "Deserialization failed"); string res = ser.Serialize(foo); Assert.AreEqual(_XmlList[0], res, "Serialization failed"); foo = xml.DeserializeString <SArray>(_XmlList[1]); Assert.AreEqual(foo.Ints.Length, 0, "Deserialization2 failed"); res = ser.Serialize(foo); Assert.AreEqual(_XmlList[2], res, "Serialization2 failed"); foo = xml.DeserializeString <SArray>(_XmlList[2]); Assert.AreEqual(foo.Ints.Length, 0, "Deserialization2 failed"); res = ser.Serialize(foo); Assert.AreEqual(_XmlList[2], res, "Serialization2 failed"); _AssertFail <SArray, CXmlException>(_Empty); }
public void TestList() { var xml = new CXmlDeserializer(); var ser = new CXmlSerializer(); SList foo = xml.DeserializeString <SList>(_XmlList[0]); Assert.AreEqual(foo.Ints.Count, 2, "Deserialization failed"); Assert.AreEqual(foo.Ints[0].I, 1, "Deserialization failed"); Assert.AreEqual(foo.Ints[1].I, 1, "Deserialization failed"); string res = ser.Serialize(foo); Assert.AreEqual(_XmlList[0], res, "Serialization failed"); foo = xml.DeserializeString <SList>(_XmlList[1]); Assert.AreEqual(foo.Ints.Count, 0, "Deserialization2 failed"); res = ser.Serialize(foo); Assert.AreEqual(_XmlList[2], res, "Serialization2 failed"); foo = xml.DeserializeString <SList>(_XmlList[2]); Assert.AreEqual(foo.Ints.Count, 0, "Deserialization2 failed"); _AssertSerDeserMatch <SList>(_XmlList[2]); res = ser.Serialize(foo); Assert.AreEqual(_XmlList[2], res, "Serialization2 failed"); _AssertFail <SList, CXmlException>(_Empty); }
public static bool InitRequiredElements() { string path = Path.Combine(CSettings.ProgramFolder, CSettings.FileNameRequiredSkinElements); var xml = new CXmlDeserializer(); try { _Required = xml.Deserialize <SRequiredElements>(path); } catch (CXmlException e) { CLog.Error("Error reading required elements: " + e); return(false); } for (int i = 1; i <= CSettings.MaxNumPlayer; i++) { string name = "Player" + i; if (!_Required.Colors.Contains(name)) { _Required.Colors.Add(name); } } return(true); }
public bool Init() { try { var xml = new CXmlDeserializer(); _Data = xml.Deserialize <STheme>(Path.Combine(_Folder, _FileName)); if (_Data.ThemeSystemVersion != _ThemeSystemVersion) { string errorMsg = _Data.ThemeSystemVersion < _ThemeSystemVersion ? "the file ist outdated!" : "the file is for newer program versions!"; errorMsg += " Current Version is " + _ThemeSystemVersion; throw new Exception(errorMsg); } } catch (Exception e) { CLog.LogError("Can't load theme \"" + _FileName + "\". Invalid file!", false, false, e); return(false); } string path = Path.Combine(_Folder, Name); List <string> files = CHelper.ListFiles(path, "*.xml"); // Load skins, succeed if at least 1 skin was loaded bool ok = false; foreach (string file in files) { CSkin skin = _GetNewSkin(path, file); if (skin.Init()) { _Skins.Add(skin.Name, skin); ok = true; } } return(ok); }
private static bool _LoadPartyMode(string filePath, out SPartyMode pm) { CXmlDeserializer deser = new CXmlDeserializer(); try { pm = deser.Deserialize <SPartyMode>(filePath); if (pm.PartyModeSystemVersion != _PartyModeSystemVersion) { throw new Exception("Wrong PartyModeSystemVersion " + pm.PartyModeSystemVersion + " expected: " + _PartyModeSystemVersion); } if (pm.ScreenFiles.Count == 0) { throw new Exception("No ScreenFiles found"); } } catch (Exception e) { pm = new SPartyMode(); CLog.LogError("Error loading PartyMode file " + filePath + ": " + e.Message); return(false); } string pathToPm = Path.Combine(CSettings.ProgramFolder, CSettings.FolderNamePartyModes, pm.Info.Folder); string pathToCode = Path.Combine(pathToPm, CSettings.FolderNamePartyModeCode); var filesToCompile = new List <string>(); filesToCompile.AddRange(CHelper.ListFiles(pathToCode, "*.cs", false, true)); Assembly output = _CompileFiles(filesToCompile.ToArray()); if (output == null) { return(false); } object instance = output.CreateInstance(typeof(IPartyMode).Namespace + "." + pm.Info.Folder + "." + pm.Info.PartyModeFile, false, BindingFlags.Public | BindingFlags.Instance, null, new object[] { _NextID++ }, null, null); if (instance == null) { CLog.LogError("Error creating Instance of PartyMode file: " + filePath); return(false); } try { pm.PartyMode = (IPartyMode)instance; } catch (Exception e) { CLog.LogError("Error casting PartyMode file: " + filePath + "; " + e.Message); return(false); } if (!CLanguage.LoadPartyLanguageFiles(pm.PartyMode.ID, Path.Combine(pathToPm, CSettings.FolderNamePartyModeLanguages))) { CLog.LogError("Error loading language files for PartyMode: " + filePath); return(false); } if (!CThemes.ReadThemesFromFolder(Path.Combine(pathToPm, CSettings.FolderNameThemes), pm.PartyMode.ID)) { return(false); } if (!CThemes.LoadPartymodeTheme(pm.PartyMode.ID)) { return(false); } foreach (string screenfile in pm.ScreenFiles) { CMenuParty screen = _GetPartyScreenInstance(output, screenfile, pm.Info.Folder); if (screen != null) { screen.AssignPartyMode(pm.PartyMode); pm.PartyMode.AddScreen(screen, screenfile); } else { return(false); } } pm.PartyMode.LoadTheme(); pm.Info.ExtInfo = pm.PartyMode; return(true); }
public virtual void LoadTheme(string xmlPath) { ThemePath = xmlPath; string file = Path.Combine(xmlPath, ThemeName + ".xml"); try { CXmlDeserializer deserializer = new CXmlDeserializer(new CLoadThemeErrorHandler()); Theme = deserializer.Deserialize <SThemeScreen>(file); foreach (SThemeBackground bg in Theme.Backgrounds) { _AddBackground(new CBackground(bg, PartyModeID), bg.Name); } foreach (SThemeButton bt in Theme.Buttons) { _AddButton(new CButton(bt, PartyModeID), bt.Name); } foreach (SThemeEqualizer eq in Theme.Equalizers) { _AddEqualizer(new CEqualizer(eq, PartyModeID), eq.Name); } foreach (SThemeLyrics ly in Theme.Lyrics) { _AddLyric(new CLyric(ly, PartyModeID), ly.Name); } foreach (SThemeNameSelection ns in Theme.NameSelections) { _AddNameSelection(new CNameSelection(ns, PartyModeID), ns.Name); } foreach (SThemeParticleEffect pe in Theme.ParticleEffects) { _AddParticleEffect(new CParticleEffect(pe, PartyModeID), pe.Name); } foreach (SThemePlaylist pl in Theme.Playlists) { _AddPlaylist(new CPlaylist(pl, PartyModeID), pl.Name); } foreach (SThemeProgressBar pb in Theme.ProgressBars) { _AddProgressBar(new CProgressBar(pb, PartyModeID), pb.Name); } foreach (SThemeScreenSetting ss in Theme.ScreenSettings) { _AddScreenSetting(new CScreenSetting(ss, PartyModeID), ss.Name); } foreach (SThemeSelectSlide sl in Theme.SelectSlides) { _AddSelectSlide(new CSelectSlide(sl, PartyModeID), sl.Name); } foreach (SThemeSingBar sb in Theme.SingNotes) { _AddSingNote(new CSingNotes(sb, PartyModeID), sb.Name); } foreach (SThemeSongMenu sm in Theme.SongMenus) { _AddSongMenu(CSongMenuFactory.CreateSongMenu(sm, PartyModeID), sm.Name); } foreach (SThemeStatic st in Theme.Statics) { _AddStatic(new CStatic(st, PartyModeID), st.Name); } foreach (SThemeText te in Theme.Texts) { _AddText(new CText(te, PartyModeID), te.Name); } if (_ScreenVersion != Theme.Informations.ScreenVersion) { string msg = "Can't load screen file of screen \"" + ThemeName + "\", "; if (Theme.Informations.ScreenVersion < _ScreenVersion) { msg += "the file ist outdated! "; } else { msg += "the file is for newer program versions! "; } msg += "Current screen version is " + _ScreenVersion; CLog.Error(msg); } foreach (IThemeable el in _Elements.Select(_GetElement).OfType <IThemeable>()) { el.LoadSkin(); } } catch (Exception e) { CLog.Fatal(e, "Error while reading {ThemeName}.xml", CLog.Params(ThemeName), true); } }
#pragma warning restore 169 #pragma warning restore 649 private static void _AssertFail <T, T2>(String xmlString) where T2 : Exception where T : new() { var deserializer = new CXmlDeserializer(); CTestHelpers.AssertFail <T2>(() => deserializer.DeserializeString <T>(xmlString)); }