/// <summary> /// Loads the session file assigned to the given game. /// </summary> public static bool LoadSessionData(TextAsset sessionFile, ref SessionData sData) { XmlDocument doc = new XmlDocument(); XmlNode settingsElem; XmlNode trialsElem; try { doc.LoadXml(sessionFile.text); settingsElem = doc.SelectSingleNode("/" + ELEM_SESSION + "/" + ELEM_SETTINGS); trialsElem = doc.SelectSingleNode("/" + ELEM_SESSION + "/" + ELEM_TRIALS); sData.fileName = sessionFile.name; ParseSessionSettings(settingsElem, ref sData); ParseSessionTrials(trialsElem, ref sData); if (sData.shuffleTrials) { SessionUtil.Shuffle(sData.trials); } return(true); } catch (Exception e) { GUILog.Error("Failed to load Session file: {0}\n{1}", sessionFile.name, e.Message); return(false); } }
/// <summary> /// Parses all the session file settings. /// </summary> private static void ParseSessionSettings(XmlNode settingsElement, ref SessionData sData) { if (settingsElement == null) { GUILog.Error("Session {0}, settings element not found.", sData.fileName); return; } foreach (XmlNode n in settingsElement.ChildNodes) { switch (n.Name) { case ELEM_GENERAL: sData.ParseElement(n as XmlElement); break; case ELEM_REACT: sData.gameData = new ReactData(n as XmlElement); break; case ELEM_REACTTOCOLOR: sData.gameData = new ReactToColorData(n as XmlElement); break; default: break; } } }
/// <summary> /// Creates a log file of the given session data. /// </summary> public static void WriteSessionLog(ref SessionData sData) { XDocument doc = new XDocument(); XElement sessionElem = new XElement(ELEM_SESSION); // Session Elem children. XElement resultsElem = new XElement(ELEM_RESULTS); XElement settingsElem = new XElement(ELEM_SETTINGS); XElement trialsElem = new XElement(ELEM_TRIALS); // Populate the elements with data. WriteResultsElement(sData, ref resultsElem); WriteSettingsElement(sData, ref settingsElem); WriteTrialsElement(sData, ref trialsElem); // Add all the Elements to the document in the proper order. doc.Add(sessionElem); sessionElem.Add(resultsElem, settingsElem, trialsElem); // Save the document. string fileName = GetOutputFilepath(sData); GUILog.Log("Using output file {0} for module {1}", fileName, sData.GameName); try { doc.Save(fileName); sData.outputWritten = true; } catch (Exception e) { GUILog.Error("XMLUtil::WriteSessionLog:: Failed to Save output document {0} \n{1}", fileName, e.Message.ToString()); } }
/// <summary> /// Parses a bool attribute. /// And assigns it to the referenced variable. /// Does nothing if the attribute fails to parse. /// </summary> public static bool ParseAttribute(XmlNode n, string att, ref bool val, bool optional = false) { if (n.Attributes[att] != null && bool.TryParse(n.Attributes[att].Value, out val)) { return(true); } if (!optional) { GUILog.Error("Could not parse attribute {1} under node {0}", n.Name, att); } return(false); }
/// <summary> /// Checks that all the folders we define here are accessible, and if not /// creates them. If something goes wrong returns False. /// </summary> public static bool InitializeFolders() { foreach (string path in FoldersList) { if (!CheckAndCreateDir(path)) { GUILog.Error("Unable to create folder {0}", path); return(false); } } GUILog.Log("Folders initialized for {0}", DashboardPath); return(true); }
/// <summary> /// Called when the game session has started. /// </summary> public virtual GameBase StartSession(TextAsset sessionFile) { // Create and load a SessionData object to give to the active game. sessionData = new SessionData(); if (XMLUtil.LoadSessionData(sessionFile, ref sessionData)) { GUILog.Log("Game {0} starting Session {1}", this.gameObject.name, sessionFile.name); OnStart(); } else { GUILog.Error("Game {0} failed to load session file {1}", this.gameObject.name, sessionFile.name); } return(this); }
/// <summary> /// Parses a GameType enum attribute. /// And assigns it to the referenced variable. /// Does nothing if the attribute fails to parse. /// </summary> public static bool ParseAttribute(XmlNode n, string att, ref GameType val, bool optional = false) { try { val = (GameType)Enum.Parse(typeof(GameType), n.Attributes[att].Value, true); return(true); } catch { if (!optional) { GUILog.Error("Could not parse attribute {1} under node {0}", n.Name, att); } return(false); } }
/// <summary> /// Checks to see if the given path directory exists, and if not tries to create. If all goes /// well returns true. If something goes wrong, false is returned. /// </summary> public static bool CheckAndCreateDir(string dirPath) { DirectoryInfo dir = new DirectoryInfo(dirPath); if (!dir.Exists) { try { Directory.CreateDirectory(dir.FullName); } catch (Exception e) { GUILog.Error("Couldn't create directory {0}, Exception: {1}", dir.FullName, e.Message.ToString()); return(false); } } return(true); }
/// <summary> /// Parses all the Trials attributes and Trial elements. /// </summary> private static void ParseSessionTrials(XmlNode trialsNode, ref SessionData sData) { if (trialsNode == null) { GUILog.Error("Session {0}, Trials element not found.", sData.fileName); return; } sData.trials = new List <Trial>(); foreach (XmlNode n in trialsNode.ChildNodes) { switch (n.Name) { case ELEM_TRIAL: ParseTrial(n as XmlElement, ref sData); break; } } GUILog.Log("Found {0} Trials defined in {1}", sData.trials.Count, sData.fileName); }