StonehearthEditor.GameMasterNode.SaveIfNecessary C# (CSharp) Method

SaveIfNecessary() public method

public SaveIfNecessary ( ) : void
return void
        public void SaveIfNecessary()
        {
            if (IsModified)
            {
                bool success = true;
                try
                {
                    using (StreamWriter wr = new StreamWriter(mPath, false, new UTF8Encoding(false)))
                    {
                        string jsonAsString = GetJsonFileString();
                        wr.Write(jsonAsString);
                    }
                }
                catch (Exception e)
                {
                    success = false;
                    Console.WriteLine("Could not write to file " + mPath + " because of exception: " + e.Message);
                }

                if (success)
                {
                    // If successfully written, clear is modified flag since there are no more changes to save
                    IsModified = false;
                }
            }
        }

Usage Example

        public bool AddNewGenericScriptNode(IGraphOwner owner, string scriptNodeName, string filePath)
        {
            if (mCurrentGraphRoot == null)
            {
                return(false);
            }

            EncounterScriptFile scriptFile = mGenericScriptNodes[scriptNodeName];

            scriptFile.WriteDefaultToFile(filePath);
            GameMasterNode newNode = new GameMasterNode(mCurrentGraphRoot.Module, filePath);

            mGameMasterNodes.Add(newNode.Path, newNode);
            newNode.Load(mGameMasterNodes);

            string           nodeName         = Path.GetFileNameWithoutExtension(filePath);
            CampaignNodeData campaignNodeData = mCurrentGraphRoot.NodeData as CampaignNodeData;
            string           arcsDir          = Path.GetFullPath(Path.Combine(campaignNodeData.NodeFile.Path, "..", "arcs")).ToUpperInvariant();
            string           filePathUpper    = Path.GetFullPath(filePath).ToUpperInvariant();

            bool foundMatchingArc = false;

            foreach (var arcNode in campaignNodeData.GetAllArcs())
            {
                string arcDir = Path.GetFullPath(Path.Combine(arcNode.Path, "..")).ToUpperInvariant();
                if (filePathUpper.StartsWith(arcDir))
                {
                    // Add new node to arc's index file (ex. game_events_arc) using nodeName as the key
                    (arcNode.NodeData as ArcNodeData).AddEncounter(newNode.NodeData as EncounterNodeData);
                    newNode.Owner    = arcNode;
                    foundMatchingArc = true;
                    break;
                }
            }

            if (!foundMatchingArc)
            {
                (mCurrentGraphRoot.NodeData as CampaignNodeData).OrphanedNodes.Add(newNode);
                newNode.Owner = mCurrentGraphRoot;
            }

            newNode.IsModified = true;
            newNode.SaveIfNecessary();
            RefreshGraph(owner);
            return(true);
        }
All Usage Examples Of StonehearthEditor.GameMasterNode::SaveIfNecessary