Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MapMagic v1.5 Setting up Auto-Save of Nodes data. Modify MapMagic at your own risk. There is no guarantee that the node file generated by this is useful. Be sure to test it for your own assurance.
Edit MapMagicWindow.cs
Make the following method static, it doesn't reference internal class data, so it is ok.
private static Generator[] SmartCopyGenerators(Generator gen)
Then refactor the SaveGenerator method, extracting the file saving to a public static ExportToFile method
void SaveGenerator(Generator gen, Vector2 pos)
{
if (locked) return;
string path = UnityEditor.EditorUtility.SaveFilePanel(
"Export Nodes",
"",
"MapMagicExport.nodes",
"nodes");
ExportToFile(path, gen);
}
/// <summary>
/// Export the whole Map Magic Nodes data to file if gen is null.
/// Path should have the extension .nodes for the file name.
/// </summary>
/// <param name="path"></param>
/// <param name="gen"></param>
/// <param name="saveIfNotDirty">Set to true if you only want to save if the MapMagic is considered dirty (untested)</param>
/// <returns>True if file was saved.</returns>
public static bool ExportToFile(string path, Generator gen = null, bool saveIfNotDirty = true)
{
if (string.IsNullOrEmpty(path)) return false;
// is SetDirty true if nodes need saving?
// if (!MapMagic.instance.setDirty && !saveIfNotDirty) return false;
// < Not working. Need another way of checking for nodes being dirty
Generator[] saveGens = SmartCopyGenerators(gen);
if (gen != null) for (int i = 0; i < saveGens.Length; i++) saveGens[i].guiRect.position -= gen.guiRect.position;
//preparing serialization arrays
List<string> classes = new List<string>();
List<UnityEngine.Object> objects = new List<UnityEngine.Object>();
List<object> references = new List<object>();
List<float> floats = new List<float>();
//saving
CustomSerialization.WriteClass(saveGens, classes, objects, floats, references);
// should put StreamWriter in this instance into using(){} statement. Closes when using exits.
// see - http://stackoverflow.com/questions/1018963/is-it-necessary-to-wrap-streamwriter-in-a-using-block
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(path))
writer.Write(CustomSerialization.ExportXML(classes, objects, floats));
//writer.Close();
//AssetDatabase.CreateAsset(saveGens, path);
//AssetDatabase.SaveAssets();
return true;
}
Then create this class, and put it in the ~Assets/MapMagic/Editor directory
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
using System.IO;
using System.Text;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
[InitializeOnLoad]
public class AutoSave_MapMagicNodes
{
static string nodeSaveLocation;
static AutoSave_MapMagicNodes()
{
EditorApplication.playmodeStateChanged += AutoSaveNodes;
}
static void AutoSaveNodes()
{
//var names = SceneManager.GetActiveScene().GetRootGameObjects().Select(go => go.name).ToList();
if (!SceneManager.GetActiveScene().GetRootGameObjects().Any(go => go.name == "MapMagic"))
return; // No MapMagic object loaded with scene, so don't attempt to save nodes.
if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
{
if (string.IsNullOrEmpty(nodeSaveLocation))
{
nodeSaveLocation = Combine(GameFolder, "BackupData", "MapMagic", "NodeBackups");
if (!Directory.Exists(nodeSaveLocation))
Directory.CreateDirectory(nodeSaveLocation);
}
StringBuilder name = new StringBuilder();
name.Append("MapMagicNodeData_");
// Add Project Name to the data file name
if (!string.IsNullOrEmpty(Application.productName))
name.Append(Application.productName + "_");
// Add Scene Name to the data file name
Scene scene = EditorSceneManager.GetActiveScene();
if (!string.IsNullOrEmpty(scene.name))
name.Append(scene.name + "_");
// Add DateTime.Now filename friendly time stamp, and the .nodes extension to the data file name
name.Append(Now() + ".nodes");
string path = Path.Combine(nodeSaveLocation, name.ToString());
if (MapMagic.MapMagicWindow.ExportToFile(path))// dirty check not working in ExportToFile , saveIfNotDirty: false))
Debug.Log("Auto-Saving Map Magic Nodes to file: " + path); // only say this if we actually saved
}
}
public static string GameFolder
{
get
{
return _GameFolder ?? (_GameFolder = Application.dataPath.Substring(0, Application.dataPath.Length - ("/Assets".Length)));
}
}
static string _GameFolder;
public static string Now(string format = "yyyy_MM_dd_HH_mm_ss_ff")
{
return DateTime.Now.ToString(format);
}
/// <summary>
/// Apply Path.Combine(on path and list)
/// </summary>
/// <param name="path"></param>
/// <param name="list"></param>
/// <returns></returns>
public static string Combine(string path, params string[] list)
{
List<string> value = list.ToList();
value.Insert(0, path);
return Combine(value.ToArray());
}
/// <summary>
/// Apply Path.Combine(on path and list)
/// </summary>
/// <param name="path"></param>
/// <param name="list"></param>
/// <returns></returns>
public static string Combine(string path, string path2, params string[] list)
{
List<string> value = list.ToList();
value.Insert(0, path2);
value.Insert(0, path);
return Combine(value.ToArray());
}
/// <summary>
/// Apply Path.Combine on the list.
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static string Combine(params string[] list)
{
if (null == list)
throw new ArgumentNullException("Argument to method cannot be null");
string path = string.Empty;
for (int i = 0; i < list.Length; i++)
if (!string.IsNullOrEmpty(list[i]))
path = Path.Combine(path, list[i]);
return path;
}
}
@ChuckSavage

This comment has been minimized.

Copy link
Owner Author

@ChuckSavage ChuckSavage commented Oct 6, 2016

MapMagic.instance.SetDirty not working as I'd hoped. Checking for dirty shouldn't be hard. Whenever a change is made in the nodes, the scene is marked as dirty. In know EditorUtility.SetDirty is being called, but not sure how to check for it. To prevent extraneous saves of nodes when nothing has changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.