StonehearthEditor.ManifestView.addGhostToolStripMenuItem_Click C# (CSharp) Method

addGhostToolStripMenuItem_Click() private method

private addGhostToolStripMenuItem_Click ( object sender, EventArgs e ) : void
sender object
e EventArgs
return void
        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();
        }