Improve partymode loading (return bool instead of incomplete PM)

This commit is contained in:
Flamefire committed 2014-10-15 15:32:16 +02:00
1 parent c1866c0ad5
commit e119e46c5a
4 files changed
+26 -40

No files matched your search

+25 -31
View File
@@ -43,7 +43,6 @@ namespace Vocaluxe.Base
public int PartyModeVersionMajor; public int PartyModeVersionMajor;
public int PartyModeVersionMinor; public int PartyModeVersionMinor;
public string TargetAudience; public string TargetAudience;
public bool NoErrors;
public IPartyMode PartyMode; public IPartyMode PartyMode;
} }
#endregion Structs #endregion Structs
@@ -53,7 +52,7 @@ namespace Vocaluxe.Base
private const int _PartyModeSystemVersion = 1; private const int _PartyModeSystemVersion = 1;
private static Dictionary<int, SPartyMode> _PartyModes; private static Dictionary<int, SPartyMode> _PartyModes;
private static Queue<int> _IDs; private static int _NextID;
private static int _NormalGameModeID; private static int _NormalGameModeID;
private static SPartyMode _CurrentPartyMode; private static SPartyMode _CurrentPartyMode;
@@ -62,15 +61,12 @@ namespace Vocaluxe.Base
public static void Init() public static void Init()
{ {
_PartyModes = new Dictionary<int, SPartyMode>(); _PartyModes = new Dictionary<int, SPartyMode>();
_IDs = new Queue<int>(1000); _NextID = 0;
for (int i = 0; i < 1000; i++)
_IDs.Enqueue(i);
//add dummy normal game mode and set it as default //add dummy normal game mode and set it as default
var pm = new SPartyMode {PartyMode = new CPartyModeNone(), ScreenFiles = new List<string>()}; var pm = new SPartyMode {PartyMode = new CPartyModeNone(), ScreenFiles = new List<string>()};
pm.PartyMode.Initialize(); pm.PartyMode.Initialize();
pm.PartyModeID = _IDs.Dequeue(); pm.PartyModeID = _NextID++;
_NormalGameModeID = pm.PartyModeID; _NormalGameModeID = pm.PartyModeID;
_PartyModes.Add(pm.PartyModeID, pm); _PartyModes.Add(pm.PartyModeID, pm);
_CurrentPartyMode = pm; _CurrentPartyMode = pm;
@@ -123,7 +119,6 @@ namespace Vocaluxe.Base
Description = mode.Description, Description = mode.Description,
Name = mode.Name, Name = mode.Name,
PartyModeID = mode.PartyModeID, PartyModeID = mode.PartyModeID,
Playable = mode.NoErrors,
VersionMajor = mode.PartyModeVersionMajor, VersionMajor = mode.PartyModeVersionMajor,
VersionMinor = mode.PartyModeVersionMinor, VersionMinor = mode.PartyModeVersionMinor,
TargetAudience = mode.TargetAudience, TargetAudience = mode.TargetAudience,
@@ -230,51 +225,51 @@ namespace Vocaluxe.Base
foreach (string file in files) foreach (string file in files)
{ {
SPartyMode pm = _LoadPartyMode(file); SPartyMode pm;
if (pm.NoErrors) if (_LoadPartyMode(file, out pm))
_PartyModes.Add(pm.PartyModeID, pm); _PartyModes.Add(pm.PartyModeID, pm);
} }
} }
private static SPartyMode _LoadPartyMode(string file) private static bool _LoadPartyMode(string file, out SPartyMode pm)
{ {
var pm = new SPartyMode {PartyModeID = _IDs.Dequeue(), ScreenFiles = new List<string>(), NoErrors = false}; pm = new SPartyMode {PartyModeID = _NextID++, ScreenFiles = new List<string>()};
CXMLReader xmlReader = CXMLReader.OpenFile(file); CXMLReader xmlReader = CXMLReader.OpenFile(file);
//Error... //Error...
if (xmlReader == null) if (xmlReader == null)
return pm; return false;
bool loaded = true; bool loaded = true;
loaded &= xmlReader.TryGetIntValue("//root/PartyModeSystemVersion", ref pm.PartyModeSystemVersion); loaded &= xmlReader.TryGetIntValue("//root/PartyModeSystemVersion", ref pm.PartyModeSystemVersion);
loaded &= xmlReader.GetValue("//root/Info/Name", out pm.Name, "ERROR Name"); loaded &= xmlReader.GetValue("//root/Info/Name", out pm.Name);
loaded &= xmlReader.GetValue("//root/Info/Description", out pm.Description, "ERROR Description"); loaded &= xmlReader.GetValue("//root/Info/Description", out pm.Description);
loaded &= xmlReader.GetValue("//root/Info/Author", out pm.Author, "ERROR Author"); loaded &= xmlReader.GetValue("//root/Info/Author", out pm.Author);
loaded &= xmlReader.GetValue("//root/Info/Folder", out pm.Folder, "ERROR Folder"); loaded &= xmlReader.GetValue("//root/Info/Folder", out pm.Folder);
loaded &= xmlReader.GetValue("//root/Info/PartyModeFile", out pm.PartyModeFile, "ERROR PartyModeFile"); loaded &= xmlReader.GetValue("//root/Info/PartyModeFile", out pm.PartyModeFile);
loaded &= xmlReader.TryGetIntValue("//root/Info/PartyModeVersionMajor", ref pm.PartyModeVersionMajor); loaded &= xmlReader.TryGetIntValue("//root/Info/PartyModeVersionMajor", ref pm.PartyModeVersionMajor);
loaded &= xmlReader.TryGetIntValue("//root/Info/PartyModeVersionMinor", ref pm.PartyModeVersionMinor); loaded &= xmlReader.TryGetIntValue("//root/Info/PartyModeVersionMinor", ref pm.PartyModeVersionMinor);
loaded &= xmlReader.GetValue("//root/Info/TargetAudience", out pm.TargetAudience, "ERROR TargetAudience"); loaded &= xmlReader.GetValue("//root/Info/TargetAudience", out pm.TargetAudience);
loaded &= xmlReader.GetValues("//root/PartyScreens/*", ref pm.ScreenFiles); loaded &= xmlReader.GetValues("//root/PartyScreens/*", ref pm.ScreenFiles);
if (!loaded) if (!loaded)
{ {
CLog.LogError("Error loading PartyMode file: " + file); CLog.LogError("Error loading PartyMode file: " + file);
return pm; return false;
} }
if (pm.PartyModeSystemVersion != _PartyModeSystemVersion) if (pm.PartyModeSystemVersion != _PartyModeSystemVersion)
{ {
CLog.LogError("Error loading PartyMode file (wrong PartyModeSystemVersion): " + file); CLog.LogError("Error loading PartyMode file (wrong PartyModeSystemVersion): " + file);
return pm; return false;
} }
if (pm.ScreenFiles.Count == 0) if (pm.ScreenFiles.Count == 0)
{ {
CLog.LogError("Error loading PartyMode file (no ScreenFiles found): " + file); CLog.LogError("Error loading PartyMode file (no ScreenFiles found): " + file);
return pm; return false;
} }
string pathToPm = Path.Combine(CSettings.ProgramFolder, CSettings.FolderNamePartyModes, pm.Folder); string pathToPm = Path.Combine(CSettings.ProgramFolder, CSettings.FolderNamePartyModes, pm.Folder);
@@ -285,19 +280,19 @@ namespace Vocaluxe.Base
Assembly output = _CompileFiles(filesToCompile.ToArray()); Assembly output = _CompileFiles(filesToCompile.ToArray());
if (output == null) if (output == null)
return pm; return false;
if (!CLanguage.LoadPartyLanguageFiles(pm.PartyModeID, Path.Combine(pathToPm, CSettings.FolderNamePartyModeLanguages))) if (!CLanguage.LoadPartyLanguageFiles(pm.PartyModeID, Path.Combine(pathToPm, CSettings.FolderNamePartyModeLanguages)))
{ {
CLog.LogError("Error loading language files for PartyMode: " + file); CLog.LogError("Error loading language files for PartyMode: " + file);
return pm; return false;
} }
object instance = output.CreateInstance(typeof(IPartyMode).Namespace + "." + pm.Folder + "." + pm.PartyModeFile); object instance = output.CreateInstance(typeof(IPartyMode).Namespace + "." + pm.Folder + "." + pm.PartyModeFile);
if (instance == null) if (instance == null)
{ {
CLog.LogError("Error creating Instance of PartyMode file: " + file); CLog.LogError("Error creating Instance of PartyMode file: " + file);
return pm; return false;
} }
try try
@@ -307,16 +302,16 @@ namespace Vocaluxe.Base
catch (Exception e) catch (Exception e)
{ {
CLog.LogError("Error casting PartyMode file: " + file + "; " + e.Message); CLog.LogError("Error casting PartyMode file: " + file + "; " + e.Message);
return pm; return false;
} }
pm.PartyMode.Initialize(); pm.PartyMode.Initialize();
pm.PartyMode.SetFolder(pathToPm); pm.PartyMode.SetFolder(pathToPm);
if (!CThemes.ReadThemesFromFolder(Path.Combine(pathToPm, CSettings.FolderNameThemes), pm.PartyModeID)) if (!CThemes.ReadThemesFromFolder(Path.Combine(pathToPm, CSettings.FolderNameThemes), pm.PartyModeID))
return pm; return false;
if (!CThemes.LoadPartymodeTheme(pm.PartyModeID)) if (!CThemes.LoadPartymodeTheme(pm.PartyModeID))
return pm; return false;
foreach (string screenfile in pm.ScreenFiles) foreach (string screenfile in pm.ScreenFiles)
{ {
@@ -332,11 +327,10 @@ namespace Vocaluxe.Base
pm.PartyMode.AddScreen(screen, screenfile); pm.PartyMode.AddScreen(screen, screenfile);
} }
else else
return pm; return false;
} }
pm.NoErrors = true; return true;
return pm;
} }
private static Assembly _CompileFiles(string[] files) private static Assembly _CompileFiles(string[] files)
+1 -7
View File
@@ -173,13 +173,7 @@ namespace Vocaluxe.Screens
_Texts[_TextVersion].Text = _PartyModeInfos[index].VersionMajor + "." + _PartyModeInfos[index].VersionMinor; _Texts[_TextVersion].Text = _PartyModeInfos[index].VersionMajor + "." + _PartyModeInfos[index].VersionMinor;
_Texts[_TextVersion].TranslationID = _PartyModeInfos[index].PartyModeID; _Texts[_TextVersion].TranslationID = _PartyModeInfos[index].PartyModeID;
if (!_PartyModeInfos[index].Playable) if (CConfig.GetMaxNumMics() == 0)
{
_Buttons[_ButtonStart].Visible = false;
_Texts[_TextError].Text = "TR_SCREENPARTY_ERROR";
_Texts[_TextError].Visible = true;
}
else if (CConfig.GetMaxNumMics() == 0)
{ {
_Buttons[_ButtonStart].Visible = false; _Buttons[_ButtonStart].Visible = false;
_Texts[_TextError].Text = "TR_SCREENPARTY_ERROR_MICS"; _Texts[_TextError].Text = "TR_SCREENPARTY_ERROR_MICS";
-1
View File
@@ -15,7 +15,6 @@
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>. // along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
#endregion #endregion
using System.Xml;
using VocaluxeLib.Xml; using VocaluxeLib.Xml;
namespace VocaluxeLib.Menu namespace VocaluxeLib.Menu
-1
View File
@@ -371,7 +371,6 @@ namespace VocaluxeLib
public int MinTeams; public int MinTeams;
public string Author; public string Author;
public bool Playable;
public int VersionMajor; public int VersionMajor;
public int VersionMinor; public int VersionMinor;
} }