Ejemplo n.º 1
0
        public string GetJsonFileString()
        {
            string jsonString = JsonHelper.GetFormattedJsonString(mJson);

            if (jsonString == null)
            {
                Console.WriteLine("Could not convert {0} to json string", Path);
            }

            return(jsonString != null ? jsonString : "INVALID JSON");
        }
Ejemplo n.º 2
0
        protected override bool TryChangeFlatFileData(string newData, out string newFlatFileData)
        {
            try
            {
                JObject json = JObject.Parse(newData);
                newFlatFileData = JsonHelper.GetFormattedJsonString(json);
            }
            catch (Exception e)
            {
                MessageBox.Show("Unable to change flat file data. Invalid Json: " + e.Message);
                newFlatFileData = null;
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        private void addGhostToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode     = treeView.SelectedNode;
            FileData selectedFileData = ModuleDataManager.GetInstance().GetSelectedFileData(treeView.SelectedNode);

            if (!CanAddEntityForm(selectedFileData, "ghost"))
            {
                return;
            }

            JsonFileData jsonFileData     = selectedFileData as JsonFileData;
            string       originalFileName = jsonFileData.FileName;
            string       ghostFilePath    = jsonFileData.Directory + "/" + originalFileName + "_ghost.json";

            try
            {
                JObject ghostJson = new JObject();
                ghostJson.Add("type", "entity");

                // Get a linked qb file
                string qbFilePath = null;
                foreach (FileData data in jsonFileData.LinkedFileData.Values)
                {
                    if (data is QubicleFileData)
                    {
                        qbFilePath = data.Path;
                    }
                }

                JObject ghostComponents = new JObject();
                ghostJson["components"] = ghostComponents;

                JObject json = jsonFileData.Json;
                JObject existingComponents = json["components"] as JObject;
                if (existingComponents != null)
                {
                    TryMoveJToken("unit_info", existingComponents, ghostComponents);
                    TryMoveJToken("render_info", existingComponents, ghostComponents);
                    TryMoveJToken("mob", existingComponents, ghostComponents);

                    // Only move the default model variant to the ghost:
                    JObject defaultModelVariant = existingComponents["model_variants"] as JObject;
                    if (defaultModelVariant != null && defaultModelVariant.Count == 1)
                    {
                        TryMoveJToken("model_variants", existingComponents, ghostComponents);
                    }
                    else
                    {
                        JObject modelVariants = new JObject();
                        ghostComponents["model_variants"] = modelVariants;
                        TryMoveJToken("default", defaultModelVariant, modelVariants);
                    }
                }

                string ghostJsonString = JsonHelper.GetFormattedJsonString(ghostJson);
                using (StreamWriter wr = new StreamWriter(ghostFilePath, false, new UTF8Encoding(false)))
                {
                    wr.Write(ghostJsonString);
                }

                JToken entityFormsComponent = json.SelectToken("components.stonehearth:entity_forms");
                if (entityFormsComponent == null)
                {
                    if (json["components"] == null)
                    {
                        json["components"] = new JObject();
                    }

                    JObject entityForms = new JObject();
                    json["components"]["stonehearth:entity_forms"] = entityForms;
                    entityFormsComponent = entityForms;
                }

                JToken mixins = json["mixins"];
                if (mixins == null)
                {
                    json.First.AddAfterSelf(new JProperty("mixins", "file(" + originalFileName + "_ghost.json" + ")"));
                }
                else
                {
                    JArray mixinsArray = mixins as JArray;
                    if (mixinsArray == null)
                    {
                        mixinsArray    = new JArray();
                        json["mixins"] = mixinsArray;
                        mixinsArray.Add(mixins.ToString());
                    }

                    mixinsArray.Add("file(" + originalFileName + "_ghost.json" + ")");
                }

                (entityFormsComponent as JObject).Add("ghost_form", "file(" + originalFileName + "_ghost.json" + ")");
                jsonFileData.TrySetFlatFileData(jsonFileData.GetJsonFileString());
                jsonFileData.TrySaveFile();
                MessageBox.Show("Adding file " + ghostFilePath);
            }
            catch (Exception ee)
            {
                MessageBox.Show("Unable to add iconic file because " + ee.Message);
                return;
            }

            Reload();
        }
Ejemplo n.º 4
0
        private void addIconicVersionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode     = treeView.SelectedNode;
            FileData selectedFileData = ModuleDataManager.GetInstance().GetSelectedFileData(treeView.SelectedNode);

            if (!CanAddEntityForm(selectedFileData, "iconic"))
            {
                return;
            }

            JsonFileData jsonFileData     = selectedFileData as JsonFileData;
            string       originalFileName = jsonFileData.FileName;
            string       iconicFilePath   = jsonFileData.Directory + "/" + originalFileName + "_iconic.json";

            try
            {
                string iconicJson = System.Text.Encoding.UTF8.GetString(StonehearthEditor.Properties.Resources.defaultIconic);
                if (iconicJson != null)
                {
                    // Get a linked qb file
                    string newQbFile                = null;
                    JToken defaultModelVariant      = jsonFileData.Json.SelectToken("components.model_variants.default");
                    string defaultModelVariantNames = defaultModelVariant != null?defaultModelVariant.ToString() : "";

                    foreach (FileData data in jsonFileData.LinkedFileData.Values)
                    {
                        if (data is QubicleFileData)
                        {
                            string fileName = data.FileName + ".qb";
                            if (defaultModelVariantNames.Contains(fileName))
                            {
                                CloneObjectParameters parameters = new CloneObjectParameters();
                                parameters.AddStringReplacement(data.FileName, data.FileName + "_iconic");
                                newQbFile = data.Path.Replace(".qb", "_iconic.qb");
                                data.Clone(newQbFile, parameters, new HashSet <string>(), true);
                            }
                        }
                    }

                    if (newQbFile != null)
                    {
                        string relativePath = JsonHelper.MakeRelativePath(iconicFilePath, newQbFile);
                        iconicJson = iconicJson.Replace("default_iconic.qb", relativePath);
                    }

                    try
                    {
                        JObject parsedIconicJson = JObject.Parse(iconicJson);
                        iconicJson = JsonHelper.GetFormattedJsonString(parsedIconicJson); // put it in the parser and back again to make sure we get valid json.

                        using (StreamWriter wr = new StreamWriter(iconicFilePath, false, new UTF8Encoding(false)))
                        {
                            wr.Write(iconicJson);
                        }
                    }
                    catch (Exception e2)
                    {
                        MessageBox.Show("Unable to write new iconic file because " + e2.Message);
                        return;
                    }

                    JObject json = jsonFileData.Json;
                    JToken  entityFormsComponent = json.SelectToken("components.stonehearth:entity_forms");
                    if (entityFormsComponent == null)
                    {
                        if (json["components"] == null)
                        {
                            json["components"] = new JObject();
                        }

                        JObject entityForms = new JObject();
                        json["components"]["stonehearth:entity_forms"] = entityForms;
                        entityFormsComponent = entityForms;
                    }

                    (entityFormsComponent as JObject).Add("iconic_form", "file(" + originalFileName + "_iconic.json" + ")");
                    jsonFileData.TrySetFlatFileData(jsonFileData.GetJsonFileString());
                    jsonFileData.TrySaveFile();
                    MessageBox.Show("Adding file " + iconicFilePath);
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show("Unable to add iconic file because " + ee.Message);
                return;
            }

            Reload();
        }