Example #1
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();
        }
Example #2
0
        private void selectJsonFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            string filePath = selectJsonFileDialog.FileName;

            if (filePath == null)
            {
                return;
            }

            filePath = JsonHelper.NormalizeSystemPath(filePath);
            Module selectedMod = selectJsonFileDialog.Tag as Module;

            if (!filePath.Contains(selectedMod.Path))
            {
                MessageBox.Show("The file must be under the directory " + selectedMod.Path);
                return;
            }

            mLastModuleLocations[selectedMod.Name] = filePath;
            string shortPath = filePath.Replace(selectedMod.Path + "/", "");

            string[] pathSplit  = shortPath.Split('/');
            string   samplePath = string.Empty;

            for (int i = 1; i < (pathSplit.Length - 1); ++i)
            {
                if (string.IsNullOrEmpty(samplePath))
                {
                    samplePath = pathSplit[i];
                }
                else
                {
                    samplePath = samplePath + ':' + pathSplit[i];
                }
            }

            if (pathSplit.Length > 2)
            {
                // Make the file path not contain the .json part if it doesn't have to
                string fileName  = pathSplit[pathSplit.Length - 1];
                string extension = System.IO.Path.GetExtension(fileName);
                if (extension == ".json")
                {
                    string folder = pathSplit[pathSplit.Length - 2];
                    if (folder.Equals(System.IO.Path.GetFileNameWithoutExtension(fileName)))
                    {
                        shortPath = shortPath.Replace("/" + fileName, "");
                    }
                }
                else if (extension == ".lua")
                {
                    string nameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fileName);
                    if (nameWithoutExtension.EndsWith("_action"))
                    {
                        nameWithoutExtension = nameWithoutExtension.Substring(0, nameWithoutExtension.Length - 7);
                        samplePath           = samplePath + ':' + nameWithoutExtension;
                    }
                }
            }

            NewAliasCallback callback = new NewAliasCallback(this, selectedMod, shortPath);
            InputDialog      dialog   = new InputDialog("Add New Alias", "Type the name of the alias for " + filePath, samplePath, "Add!");

            dialog.SetCallback(callback);
            dialog.ShowDialog();
        }
Example #3
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();
        }