Ejemplo n.º 1
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();
        }