private DecomposedSb3Target DecomposeTarget(JObject target, int projectId) { DecomposedSb3Target toReturn = new DecomposedSb3Target(); Dictionary <string, JObject> allBlocksInTarget = GetAllBlockObjectsById(target); Dictionary <JObject, Block> ObjectToBlock = ConvertToDatabaseBlocks(allBlocksInTarget); Sb3BlockUnwinder unwinder = new Sb3BlockUnwinder(allBlocksInTarget, ObjectToBlock); int spriteTypeId = target.Value <bool>("isStage") ? 2 : 1; //For each hat block... foreach (KeyValuePair <string, JObject> block in allBlocksInTarget.Where(o => o.Value.Value <bool>("topLevel")).ToDictionary(o => o.Key, o => o.Value)) { Script encounteredScript = new Script() { Coordinates = $"{block.Value.Value<int>("x")}-{block.Value.Value<int>("y")}", SpriteName = target.Value <string>("name"), ProjectId = projectId, SpriteTypeId = (block.Value.Value <string>("opcode") == "procedures_definition") ? 3 : spriteTypeId, TotalBlocks = 0, }; if (encounteredScript.SpriteTypeId == 3) //If it's a procedure definition, add the procedure too. { Procedure encounteredProcedure = unwinder.GetProcedureFromDefinition(block.Value, encounteredScript); toReturn.AllProcedures.Add(encounteredProcedure); } int blockRankCount = 0; List <Block> blocksOfScript = unwinder.UnwindBlock2(block.Value, 0, ref blockRankCount, encounteredScript); toReturn.AllScriptsAndBlocks.Add(encounteredScript, blocksOfScript); } return(toReturn); }
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(); } } }