/// <summary> /// Compiles the lists of commands, scripts, and procedures in the given code. /// </summary> /// <param name="projectId">the id of the code's project</param> /// <param name="date">the date the code was updated</param> /// <param name="rawCode">the complete code of a project</param> /// <returns>the list of commands, scripts, and procedures in the given code</returns> private static ParsedCode ParseCode(int projectId, DateTime date, string rawCode) { ParsedCode parsedCode = new ParsedCode(); // Procedure definitions in root JObject code = JObject.Parse(rawCode); { JArray scripts = code.GetValue("scripts") as JArray; if (scripts != null) { foreach (JArray scriptArray in scripts.OfType <JArray>()) { Script script = new Script(projectId, date, scriptArray[2] as JArray, ScopeType.Stage, "stage"); parsedCode.Join(ParseScript(script)); } } } // Procedure definitions in sprites JArray sprites = (JArray)code.GetValue("children"); foreach (JObject sprite in sprites.OfType <JObject>()) { if (sprite["objName"] == null) { continue; } string spriteName = sprite["objName"].ToString(); JArray scripts = sprite.GetValue("scripts") as JArray; if (scripts != null) { foreach (JArray scriptArray in scripts.OfType <JArray>()) { Script script = new Script(projectId, date, scriptArray[2] as JArray, ScopeType.Sprite, spriteName); parsedCode.Join(ParseScript(script)); } } } return(parsedCode); }
/// <summary> /// Compiles the given script. /// </summary> /// <param name="script">a script</param> /// <returns>the list of commands and procedures in the given script, and the script itself</returns> private static ParsedCode ParseScript(Script script) { ParsedCode scriptCode = new ParsedCode(); ScopeType scopeType = script.ScopeType; string scopeName = script.ScopeName; int indent = 0; scriptCode.Join(ParseScripts(script, script.Code, ref scopeType, ref scopeName, ref indent)); scriptCode.Scripts.Add(new List <object> { script.ScriptId, script.ProjectId, script.Date.ToString("yyyy-MM-dd"), scopeType, scopeName, scriptCode.Commands.Count }); return(scriptCode); }
/// <summary> /// Recursively compiles the lists of commands and procedures in the given script. /// </summary> /// <param name="script">a script</param> /// <param name="scripts">the script's code</param> /// <param name="scopeType">the type of the current scope</param> /// <param name="scopeName">the name of the current scope</param> /// <param name="depth">the current recursive depth</param> /// <returns>the lists of commands and procedures in the given script</returns> private static ParsedCode ParseScripts(Script script, JArray scripts, ref ScopeType scopeType, ref string scopeName, ref int depth) { ParsedCode parsedCode = new ParsedCode(); List <object> command = new List <object> { 0, script.ScriptId, script.ProjectId, script.Date.ToString("yyyy-MM-dd"), depth, scopeType, scopeName }; bool added = false; int i = 0; foreach (JToken innerScript in scripts) { if (innerScript is JValue) { i++; added = true; command.Add(innerScript); } else if (innerScript is JArray) { JArray array = (JArray)innerScript; if (AllOneField(array)) { if (!array.Any()) { command.Add("[]"); } else { int newDepth = depth + 1; parsedCode.Join(ParseScripts(script, array, ref scopeType, ref scopeName, ref newDepth)); } } else { if (array.Any() && array[0].ToString() == "procDef") { i++; added = true; command.Add("procdef"); parsedCode.Procedures.Add(new List <object> { 0, script.ProjectId, script.Date.ToString("yyyy-MM-dd"), scopeType, scopeName, array[1].ToString(), array[2].Count() }); scopeType = ScopeType.ProcDef; scopeName = array[1].ToString(); } else { int newDepth = depth + 1; parsedCode.Join( ParseScripts(script, array, ref scopeType, ref scopeName, ref newDepth)); } } } } for (; i < ParamCount + 1; i++) { command.Add(""); } if (added) { parsedCode.Commands.Add(command); } return(parsedCode); }