Exemple #1
0
        private void FixupLootTables()
        {
            // Fixup loot tables
            if (Alias == "mining:base_loot_table")
            {
                JsonFileData jsonFileData = FileData as JsonFileData;

                if (JsonHelper.FixupLootTable(jsonFileData.Json, "mineable_blocks.*"))
                {
                    jsonFileData.TrySetFlatFileData(jsonFileData.GetJsonFileString());
                    jsonFileData.TrySaveFile();
                }
            }
            else
            {
                JsonFileData jsonFileData = FileData as JsonFileData;
                if (jsonFileData != null && jsonFileData.Json != null)
                {
                    JToken harvestLootTable = jsonFileData.Json.SelectToken("entity_data.stonehearth:harvest_beast_loot_table");
                    if (harvestLootTable != null)
                    {
                        if (harvestLootTable["entries"] == null)
                        {
                            if (JsonHelper.FixupLootTable(jsonFileData.Json, "entity_data.stonehearth:harvest_beast_loot_table"))
                            {
                                jsonFileData.TrySetFlatFileData(jsonFileData.GetJsonFileString());
                                jsonFileData.TrySaveFile();
                            }
                        }
                    }

                    JToken destroyedLootTable = jsonFileData.Json.SelectToken("entity_data.stonehearth:destroyed_loot_table");
                    if (destroyedLootTable != null)
                    {
                        if (destroyedLootTable["entries"] == null)
                        {
                            if (JsonHelper.FixupLootTable(jsonFileData.Json, "entity_data.stonehearth:destroyed_loot_table"))
                            {
                                jsonFileData.TrySetFlatFileData(jsonFileData.GetJsonFileString());
                                jsonFileData.TrySaveFile();
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        public override bool Clone(string newPath, CloneObjectParameters parameters, HashSet <string> alreadyCloned, bool execute)
        {
            if (JsonType == JSONTYPE.RECIPE)
            {
                string newNameToUse = parameters.TransformParameter(GetNameForCloning()).Replace("_recipe", "");
                if (execute)
                {
                    JsonFileData recipesList = RelatedFiles[RelatedFiles.Count - 1] as JsonFileData;
                    // Only add the recipe to the recipe index if we're inside the same module
                    if (recipesList.Path.Contains(parameters.TargetModule))
                    {
                        JObject json        = recipesList.mJson;
                        JToken  foundParent = null;
                        foreach (JToken token in json["craftable_recipes"].Children())
                        {
                            if (foundParent != null)
                            {
                                break;
                            }

                            foreach (JToken recipe in token.First["recipes"].Children())
                            {
                                if (recipe.Last.ToString().Contains(FileName))
                                {
                                    foundParent = token.First["recipes"];
                                    break;
                                }
                            }
                        }

                        if (foundParent != null)
                        {
                            string recipeFileName = System.IO.Path.GetFileName(newPath);
                            (foundParent as JObject).Add(newNameToUse, JObject.Parse("{\"recipe\": \"file(" + recipeFileName + ")\"}"));
                            recipesList.TrySetFlatFileData(recipesList.GetJsonFileString());
                            recipesList.TrySaveFile();
                        }
                    }
                }
            }

            return(base.Clone(newPath, parameters, alreadyCloned, execute));
        }
Exemple #3
0
        public bool TryModifyJson(string newJsonString)
        {
            try
            {
                JObject newJson = JObject.Parse(newJsonString);
                if (newJson != null)
                {
                    if (newJson.ToString().Equals(Json.ToString()))
                    {
                        return(false); // not modified because jsons are equivalent
                    }

                    mJsonFileData.TrySetFlatFileData(newJsonString);
                    IsModified = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Unable to modify json. Error: " + e.Message);
                return(false);
            }

            return(true);
        }
        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();
        }
        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();
        }