WriteWhitespace() public method

Writes the given white space.
public WriteWhitespace ( string ws ) : void
ws string The string of white space characters.
return void
    public void WriteRawInStart()
    {
      StringBuilder sb = new StringBuilder();
      StringWriter sw = new StringWriter(sb);

      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
      {
        jsonWriter.Formatting = Formatting.Indented;
        jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol;

        jsonWriter.WriteRaw("[1,2,3,4,5]");
        jsonWriter.WriteWhitespace("  ");
        jsonWriter.WriteStartArray();
        jsonWriter.WriteValue(double.NaN);
        jsonWriter.WriteEndArray();
      }

      string expected = @"[1,2,3,4,5]  [
  NaN
]";
      string result = sb.ToString();

      Assert.AreEqual(expected, result);
    }
Exemplo n.º 2
0
        public void SaveAnim(string name)
        {
            if (!Directory.Exists("blueprints\\anim"))
            { Directory.CreateDirectory("blueprints\\anim"); }

            FileStream fs = File.Open("blueprints\\anim\\" + name + ".json", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            JsonTextWriter jw = new JsonTextWriter(sw);
            jw.Formatting = Formatting.Indented;

            jw.WriteStartObject();
            //anim
            jw.WritePropertyName("animations");
            jw.WriteStartArray();
            foreach (Animation a in anims)
            {
                jw.WriteStartObject();

                jw.WritePropertyName("name");
                jw.WriteValue(a.name);
                jw.WritePropertyName("speed");
                jw.WriteValue(a.speed);
                jw.WritePropertyName("loop");
                jw.WriteValue(a.loopback);
                jw.WritePropertyName("frames");
                jw.WriteValue(a.frames.Count);

                //collision
                jw.WritePropertyName("collisionVolumes");
                jw.WriteStartArray();
                for (int i = 0; i < a.frames.Count; i++)
                {
                    if (a.collision[i].Count > 0)
                    {
                        jw.WriteStartObject();
                        jw.WritePropertyName("volumes");
                        jw.WriteStartArray();
                        foreach (CollisionList cl in a.collision[i])
                        {
                            jw.WriteStartObject();
                            jw.WritePropertyName("physical");
                            jw.WriteValue(cl.Physical);
                            jw.WritePropertyName("centerX");
                            jw.WriteValue(a.frames[i].Width / 2);
                            jw.WritePropertyName("centerY");
                            jw.WriteValue(a.frames[i].Height / 2);
                            jw.WritePropertyName("xpoints");
                            jw.WriteStartArray();
                            foreach (CollisionPoint p in cl.Nodes)
                            {
                                jw.WriteValue(p.X - (a.frames[i].Width / 2));
                            }
                            jw.WriteEnd();
                            jw.WritePropertyName("ypoints");
                            jw.WriteStartArray();
                            foreach (CollisionPoint p in cl.Nodes)
                            {
                                jw.WriteValue(p.Y - (a.frames[i].Height / 2));
                            }
                            jw.WriteEnd();
                            jw.WriteEnd();
                        }
                        jw.WriteEnd();
                        jw.WriteEnd();
                    }
                    else
                    {
                        jw.WriteWhitespace("");
                    }
                }
                jw.WriteEnd();
                // end anim
                jw.WriteEnd();
            }
            jw.WriteEnd();

            jw.WriteEnd();
            jw.Close();

            if (!Directory.Exists("images\\anim"))
            { Directory.CreateDirectory("images\\anim"); }

            if (!Directory.Exists("images\\anim\\" + name))
            { Directory.CreateDirectory("images\\anim\\" + name); }
            foreach (Animation a in anims)
            {
                if (!Directory.Exists("images\\anim\\" + name + "\\" + a.name))
                { Directory.CreateDirectory("images\\anim\\" + name + "\\" + a.name); }
                int frameNum = 0;
                foreach (Texture2D t in a.frames)
                {
                    Stream s = File.OpenWrite("images\\anim\\" + name + "\\" + a.name + "\\" + frameNum + ".png");
                    t.SaveAsPng(s, t.Width, t.Height);
                    s.Close();
                    frameNum++;
                }
            }
        }
Exemplo n.º 3
0
        void TrStmtList(List<Statement/*!*/>/*!*/ stmts)
        {
            Contract.Requires(cce.NonNullElements(stmts));
              List<LocalVariable> AllDecls = new List<LocalVariable>();

              using (WriteArray()) {
            j.WriteValue(KremlinAst.ESequence);
            using (WriteArray()) {
              WriteEUnit(); // in case the statement list is empty
              foreach (Statement ss in stmts) {
            // JsonTextWriter is forward-only, but after calling TrStmt() we may need
            // to go back and inject new ELet statements to introduce temp variables.
            // So call TrStmt() once to generate code to a throw-away MemoryStream,
            // but remember what temps need to be introduced.  Then introduce them
            // and call TrStmt() once more, to generate the actual Json.
            JsonTextWriter oldj = j;
            VariableTracker oldtracker = VarTracker.Clone();

            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            JsonTextWriter newj = new JsonTextWriter(sw);
            newj.Formatting = oldj.Formatting;
            newj.Indentation = oldj.Indentation;
            j = newj;

            var oldVarDeclsList = varDeclsList;
            varDeclsList = new List<LocalVariable>();

            TrStmt(ss);

            j = oldj;
            VarTracker = oldtracker;
            var decls = varDeclsList; // Grab the set of just-declared variables generated by the first TrStmt() pass
            varDeclsList = null;      // Switch modes for next TrStmt() pass
            AllDecls.AddRange(decls); // Accumulate the set of all variables this stmt list has generated
            foreach (var l in decls) {
              // ELet v in { Stmt
              j.WriteStartArray();
              j.WriteValue(KremlinAst.ELet);
              j.WriteStartArray();
              WriteBinder(l, l.Name, true); // lident
              WriteDefaultValue(l.Type);    // = default
              VarTracker.Push(l);
              // "in" is the contents that follow
              j.WriteStartArray();
              j.WriteValue(KremlinAst.ESequence);
              j.WriteStartArray();
              WriteEUnit();
            }

            newj.Close();
            string RawJson = sb.ToString();
            if (RawJson != "") {
              j.WriteRaw(",");
              j.WriteWhitespace("\n");
              j.WriteRaw(RawJson); // Now paste the JSON
            }

            if (ss.Labels != null) {
              // bugbug: these appear to be used to support "Break" statements.
              j.WriteComment("Labels are unsupported: " + ss.Labels.Data.AssignUniqueId("after_", idGenerator));
            }

            varDeclsList = oldVarDeclsList;
              }

              // Now that all statements in the list have been generated, close out the nested ELets generated above
              AllDecls.Reverse();
              foreach (var l in AllDecls) {
            VarTracker.Pop(l);
            j.WriteEndArray(); // Closing out the expr list in the ESequence
            j.WriteEndArray(); // Closing out the array aboce ESequence
            j.WriteEndArray(); // Closing out the list of binder * expr * expr
            j.WriteEndArray(); // Closing out the array above ELet
              }
            }
              }
        }
Exemplo n.º 4
0
        public bool SaveToFile(string filename)
        {
            try
            {
                using (StreamWriter sr = new StreamWriter(filename))
                {
                    using (JsonTextWriter jr = new JsonTextWriter(sr))
                    {
                        jr.WriteRaw("[");
                        jr.WriteStartObject();
                        jr.WritePropertyName("FlightType"); jr.WriteValue("V1");
                        jr.WritePropertyName("Date"); jr.WriteValue(DateTime.Now.ToString());
                        jr.WriteEndObject();
                        jr.WriteRaw(",");
                        jr.WriteWhitespace(Environment.NewLine);

                        for (int i = 0; i < entries.Count; i++)
                        {
                            jr.WriteStartObject();
                            jr.WritePropertyName("T"); jr.WriteValue(entries[i].offsettime);

                            if (entries[i].IsPosSet)
                            {
                                jr.WritePropertyName("Pos"); jr.WriteStartArray(); jr.WriteValue(entries[i].pos.X);
                                jr.WriteValue(entries[i].pos.Y); jr.WriteValue(entries[i].pos.Z); jr.WriteEndArray();

                                if (entries[i].timetofly != 0)
                                {
                                    jr.WritePropertyName("FlyTime"); jr.WriteValue(entries[i].timetofly);
                                }
                            }

                            if (entries[i].IsDirSet)
                            {
                                jr.WritePropertyName("Dir"); jr.WriteStartArray(); jr.WriteValue(entries[i].dir.X);
                                jr.WriteValue(entries[i].dir.Y); jr.WriteValue(entries[i].dir.Z); jr.WriteEndArray();

                                if (entries[i].timetopan != 0)
                                {
                                    jr.WritePropertyName("PanTime"); jr.WriteValue(entries[i].timetopan);
                                }
                            }

                            if (entries[i].IsZoomSet)
                            {
                                jr.WritePropertyName("Z"); jr.WriteValue(entries[i].zoom);

                                if (entries[i].timetozoom != 0)
                                {
                                    jr.WritePropertyName("ZTime"); jr.WriteValue(entries[i].timetozoom);
                                }
                            }

                            if ( entries[i].IsMessageSet )
                            {
                                jr.WritePropertyName("Msg"); jr.WriteValue(entries[i].message);

                                if (entries[i].messagetime != 0)
                                {
                                    jr.WritePropertyName("MsgTime"); jr.WriteValue(entries[i].messagetime);
                                }
                            }

                            jr.WriteEndObject();

                            if ( i < entries.Count-1 )
                                jr.WriteRaw(",");
                            jr.WriteWhitespace(Environment.NewLine);
                        }

                        jr.WriteRaw("]");
                    }
                }

                return true;
            }
            catch { }

            return false;
        }