public void ParseProject(string projectJson, int projectId) { if (projectJson.StartsWith("PK")) { return; } JObject projectObject; try { projectObject = JObject.Parse(projectJson); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } projectObject.TryGetValue("targets", out JToken targetToken); if (targetToken == null) { return; } using (ApplicationDatabase ctxt = new ApplicationDatabase()) { foreach (var obj in (JArray)targetToken) { DecomposedSb3Target project = DecomposeTarget(obj as JObject, projectId); foreach (KeyValuePair <Script, List <Block> > script in project.AllScriptsAndBlocks) { script.Key.ProjectId = projectId; script.Key.TotalBlocks = script.Value.Count(); ctxt.Scripts.Add(script.Key); ctxt.SaveChanges(); foreach (Block b in script.Value) { JSONReader.SaveBlockWithParameters(b, b.parameters); } } ctxt.Procedures.AddRange(project.AllProcedures); ctxt.SaveChanges(); } } }
private List <Script> GetAllScripts(string projectJson, int projectId) { JObject projectObject = JObject.Parse(projectJson); List <Script> toReturn = new List <Script>(); projectObject.TryGetValue("scripts", out JToken stageScriptToken); using (ApplicationDatabase ctxt = new ApplicationDatabase()) { if (stageScriptToken != null) { foreach (var obj in stageScriptToken.Children()) { string scriptCoordinates = $"{obj[0].Value<Int32>()}-{obj[1].Value<Int32>()}"; //Get X and Y coordinates Script currentScript = new Script() { Coordinates = scriptCoordinates, ProjectId = projectId, SpriteTypeId = 2, SpriteName = "stage" }; currentScript = ctxt.Scripts.Add(currentScript); ctxt.SaveChanges(); List <Block> blocksInScript = GetAllBlockFromScript((JArray)obj, currentScript.ScriptId); currentScript.TotalBlocks = blocksInScript.Count(); foreach (Block b in blocksInScript) { b.ScriptId = currentScript.ScriptId; JSONReader.SaveBlockWithParameters(b, b.parameters); } ctxt.SaveChanges(); } } projectObject.TryGetValue("children", out JToken childrenToken); if (childrenToken != null) { foreach (JObject sprite in childrenToken.Children()) { string spriteName = sprite.Value <string>("objName"); sprite.TryGetValue("scripts", out JToken spriteScriptsToken); if (spriteScriptsToken != null) { foreach (var obj in spriteScriptsToken.Children()) { string scriptCoordinates = $"{obj[0].Value<Int32>()}-{obj[1].Value<Int32>()}"; //Get X and Y coordinates Script currentScript = new Script() { Coordinates = scriptCoordinates, ProjectId = projectId, SpriteTypeId = 1, SpriteName = spriteName }; currentScript = ctxt.Scripts.Add(currentScript); ctxt.SaveChanges(); List <Block> blocksInScript = GetAllBlockFromScript((JArray)obj, currentScript.ScriptId); currentScript.TotalBlocks = blocksInScript.Count(); foreach (Block b in blocksInScript) { b.ScriptId = currentScript.ScriptId; JSONReader.SaveBlockWithParameters(b, b.parameters); } if (blocksInScript.Any(o => o.IsPartOfProcDef)) //Just check if we found any block that was procDef , which can only occur if the entire script is a procDef { currentScript.SpriteTypeId = 3; } ctxt.SaveChanges(); } } } } } return(toReturn); }