示例#1
0
        public Part(IDictionary <string, object> jsonDict, Dictionary <string, RotationSet> rotationSets, Dictionary <string, Material> physicsMaterials)
        {
            if (jsonDict.ContainsKey("uuid"))
            {
                this.ItemUuid = Guid.Parse(jsonDict["uuid"].ToString());

                if (jsonDict.ContainsKey("legacy_id"))
                {
                    if (!int.TryParse(jsonDict["legacy_id"].ToString(), out this.LegacyId))
                    {
                        this.LegacyId = -1;
                    }
                }

                if (jsonDict.ContainsKey("mat"))
                {
                    if (jsonDict["mat"] is string)
                    {
                        this.RenderMaterial.Add(jsonDict["mat"].ToString());
                    }
                    else
                    {
                        foreach (object q in (List <object>)jsonDict["mat"])
                        {
                            this.RenderMaterial.Add(q.ToString());
                        }
                    }
                }
                if (jsonDict.ContainsKey("mesh"))
                {
                    this.MeshFileName = jsonDict["mesh"].ToString();
                }

                // Texture
                if (jsonDict.ContainsKey("asg"))
                {
                    this.TextureAsg = jsonDict["asg"].ToString();
                }
                if (jsonDict.ContainsKey("dif"))
                {
                    this.TextureDif = jsonDict["dif"].ToString();
                }
                if (jsonDict.ContainsKey("nor"))
                {
                    this.TextureNor = jsonDict["nor"].ToString();
                }

                if (jsonDict.ContainsKey("color"))
                {
                    this.Color = RgbColor.FromHtmlNotation(jsonDict["color"].ToString());
                }

                // Collision
                if (jsonDict.ContainsKey("cylinder"))
                {
                    this.Colision = new CylinderCollision(
                        (IDictionary <string, object>)jsonDict["cylinder"]);
                }
                if (jsonDict.ContainsKey("box"))
                {
                    this.Colision = new BoxCollision(
                        (IDictionary <string, object>)jsonDict["box"]);
                }
                if (jsonDict.ContainsKey("hull"))
                {
                    this.Colision = new HullCollision(
                        (IDictionary <string, object>)jsonDict["hull"]);
                }

                // Rotationset
                if (jsonDict.ContainsKey("rotationSet"))
                {
                    string rotSetName = jsonDict["rotationSet"].ToString();
                    if (rotationSets.ContainsKey(rotSetName))
                    {
                        this.RotationSet = rotationSets[rotSetName];
                    }
                    else
                    {
                        throw new Exception("Part has invalid rotation set: " + rotSetName);
                    }
                }

                if (jsonDict.ContainsKey("density"))
                {
                    if (!float.TryParse(jsonDict["density"].ToString(), out this.Density))
                    {
                        this.Density = -1;
                    }
                }

                if (jsonDict.ContainsKey("type"))
                {
                    this.CustomType = jsonDict["type"].ToString();
                }

                // Feature
                if (jsonDict.ContainsKey("bearing"))
                {
                    this.Feature = new Features.Bearing();
                }
                if (jsonDict.ContainsKey("sensor"))
                {
                    this.Feature = new Features.Sensor();
                }
                if (jsonDict.ContainsKey("radio"))
                {
                    this.Feature = new Features.Radio();
                }
                if (jsonDict.ContainsKey("horn"))
                {
                    this.Feature = new Features.Horn();
                }
                if (jsonDict.ContainsKey("timedjoint"))
                {
                    this.Feature = new Features.Controller();
                }
                if (jsonDict.ContainsKey("logic"))
                {
                    this.Feature = new Features.LogicGate();
                }
                if (jsonDict.ContainsKey("timer"))
                {
                    this.Feature = new Features.Timer();
                }
                if (jsonDict.ContainsKey("tone"))
                {
                    this.Feature = new Features.Tone((IDictionary <string, object>)jsonDict["tone"]);
                }
                if (jsonDict.ContainsKey("lever"))
                {
                    this.Feature = new Features.Lever();
                }
                if (jsonDict.ContainsKey("button"))
                {
                    this.Feature = new Features.Button();
                }
                if (jsonDict.ContainsKey("spring"))
                {
                    this.Feature = new Features.Spring((IDictionary <string, object>)jsonDict["spring"]);
                }
                if (jsonDict.ContainsKey("steering"))
                {
                    this.Feature = new Features.Steering((IDictionary <string, object>)jsonDict["steering"]);
                }
                if (jsonDict.ContainsKey("seat"))
                {
                    this.Feature = new Features.Seat((IDictionary <string, object>)jsonDict["seat"]);
                }
                if (jsonDict.ContainsKey("engine"))
                {
                    this.Feature = new Features.Engine((IDictionary <string, object>)jsonDict["engine"]);
                }
                if (jsonDict.ContainsKey("thruster"))
                {
                    this.Feature = new Features.Thruster((IDictionary <string, object>)jsonDict["thruster"]);
                }

                if (jsonDict.ContainsKey("script"))
                {
                    foreach (object q in (List <object>)jsonDict["script"])
                    {
                        this.Script.Add(q.ToString());
                    }
                }

                if (jsonDict.ContainsKey("sticky"))
                {
                    string sticky = jsonDict["sticky"].ToString();
                    for (int i = 0; i < sticky.Length; i += 2)
                    {
                        // -
                        if (sticky[i] == '-')
                        {
                            if (sticky[i + 1] == 'X')
                            {
                                this.NegativeStickyAxes.Add(Axis.X);
                            }
                            else if (sticky[i + 1] == 'Y')
                            {
                                this.NegativeStickyAxes.Add(Axis.Y);
                            }
                            else
                            {
                                this.NegativeStickyAxes.Add(Axis.Z);
                            }
                        }
                        // +
                        else
                        {
                            if (sticky[i + 1] == 'X')
                            {
                                this.PositiveStickyAxes.Add(Axis.X);
                            }
                            else if (sticky[i + 1] == 'Y')
                            {
                                this.PositiveStickyAxes.Add(Axis.Y);
                            }
                            else
                            {
                                this.PositiveStickyAxes.Add(Axis.Z);
                            }
                        }
                    }
                }

                if (jsonDict.ContainsKey("physicsMaterial"))
                {
                    string phyMat = jsonDict["physicsMaterial"].ToString();
                    if (physicsMaterials.ContainsKey(phyMat))
                    {
                        this.PhysicsMaterial = physicsMaterials[phyMat];
                    }
                }
            }
            else
            {
                throw new Exception("Part doesn't have UUID");
            }
        }
示例#2
0
        public static Dictionary <Guid, Block> ReadFromJson(string jsonText, Dictionary <string, Material> materials)
        {
            Dictionary <Guid, Block>     blocks = new Dictionary <Guid, Block>();
            IDictionary <string, object> doc    = JsonConvert.DeserializeObject <ExpandoObject>(jsonText);

            if (doc.ContainsKey("basicMaterialList"))
            {
                foreach (object blockObj in (List <object>)doc["basicMaterialList"])
                {
                    IDictionary <string, object> block = (IDictionary <string, object>)blockObj;
                    int      legacyId = -1;
                    Guid     itemUuid;
                    string   renderMaterial = "";
                    bool     transparent    = false;
                    string   textureDif     = "";
                    string   textureAsg     = "";
                    string   textureNor     = "nonor_nor.tga";
                    RgbColor textureColor   = new RgbColor(255, 255, 255);
                    float    density        = -1;

                    if (block.ContainsKey("legacy_id"))
                    {
                        if (!int.TryParse(block["legacy_id"].ToString(), out legacyId))
                        {
                            legacyId = -1;
                        }
                    }

                    if (block.ContainsKey("uuid"))
                    {
                        itemUuid = new Guid(block["uuid"].ToString());

                        if (!blocks.ContainsKey(itemUuid))
                        {
                            if (block.ContainsKey("dif"))
                            {
                                textureDif = block["dif"].ToString();
                            }

                            if (block.ContainsKey("asg"))
                            {
                                textureAsg = block["asg"].ToString();
                            }

                            if (block.ContainsKey("nor"))
                            {
                                textureNor = block["nor"].ToString();
                            }

                            if (block.ContainsKey("color"))
                            {
                                RgbColor c = RgbColor.FromHtmlNotation(block["color"].ToString());
                                if (c != null)
                                {
                                    textureColor = c;
                                }
                            }

                            if (block.ContainsKey("density"))
                            {
                                if (!float.TryParse(block["density"].ToString(), out density))
                                {
                                    density = -1;
                                }
                            }

                            if (block.ContainsKey("transparent"))
                            {
                                bool.TryParse(block["transparent"].ToString(), out transparent);
                            }

                            if (block.ContainsKey("mat"))
                            {
                                renderMaterial = block["mat"].ToString();
                            }

                            if (block.ContainsKey("physics_material"))
                            {
                                string pMat = block["physics_material"].ToString();
                                if (materials.ContainsKey(pMat))
                                {
                                    blocks.Add(itemUuid, new Block(itemUuid, textureDif, textureAsg, textureNor, textureColor, materials[pMat], density, renderMaterial, legacyId, transparent));
                                }
                                else
                                {
                                    throw new Exception("basicmaterial physics_material invalid");
                                }
                            }
                            else
                            {
                                throw new Exception("basicmaterial doesn't have physics_material.");
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("basicmaterial doesn't have uuid.");
                    }
                }
            }
            return(blocks);
        }