Browse Source

Improved Flowchart UpdateVersion system

master
chrisgregan 9 years ago
parent
commit
0087d8c482
  1. 7
      Assets/Fungus/Flowchart/Editor/FlowchartMenuItems.cs
  2. 65
      Assets/Fungus/Flowchart/Scripts/Flowchart.cs

7
Assets/Fungus/Flowchart/Editor/FlowchartMenuItems.cs

@ -14,6 +14,13 @@ namespace Fungus
GameObject go = SpawnPrefab("Flowchart"); GameObject go = SpawnPrefab("Flowchart");
go.transform.position = Vector3.zero; go.transform.position = Vector3.zero;
// This is the latest version of Flowchart, so no need to update.
Flowchart flowchart = go.GetComponent<Flowchart>();
if (flowchart != null)
{
flowchart.version = Flowchart.CURRENT_VERSION;
}
// Only the first created Flowchart in the scene should have a default GameStarted block // Only the first created Flowchart in the scene should have a default GameStarted block
if (GameObject.FindObjectsOfType<Flowchart>().Length > 1) if (GameObject.FindObjectsOfType<Flowchart>().Length > 1)
{ {

65
Assets/Fungus/Flowchart/Scripts/Flowchart.cs

@ -9,6 +9,16 @@ using System.Text.RegularExpressions;
namespace Fungus namespace Fungus
{ {
/**
* Interface for Flowchart components which can be updated when the
* scene loads in the editor. This is used to maintain backwards
* compatibility with earlier versions of Fungus.
*/
interface IUpdateable
{
void UpdateToVersion(int oldVersion, int newVersion);
}
/** /**
* Visual scripting controller for the Flowchart programming language. * Visual scripting controller for the Flowchart programming language.
* Flowchart objects may be edited visually using the Flowchart editor window. * Flowchart objects may be edited visually using the Flowchart editor window.
@ -16,10 +26,10 @@ namespace Fungus
[ExecuteInEditMode] [ExecuteInEditMode]
public class Flowchart : MonoBehaviour public class Flowchart : MonoBehaviour
{ {
/** /**
* Current version used to compare with the previous version so older versions can be custom-updated from previous versions. * The current version of the Flowchart. Used for updating components.
*/ */
public const string CURRENT_VERSION = "1.0"; public const int CURRENT_VERSION = 1;
/** /**
* The name of the initial block in a new flowchart. * The name of the initial block in a new flowchart.
@ -27,10 +37,10 @@ namespace Fungus
public const string DEFAULT_BLOCK_NAME = "New Block"; public const string DEFAULT_BLOCK_NAME = "New Block";
/** /**
* Variable to track flowchart's version and if initial set up has completed. * Variable to track flowchart's version so components can update to new versions.
*/ */
[HideInInspector] [HideInInspector]
public string version; public int version = 0; // Default to 0 to always trigger an update for older versions of Fungus.
/** /**
* Scroll position of Flowchart editor window. * Scroll position of Flowchart editor window.
@ -209,7 +219,28 @@ namespace Fungus
CheckItemIds(); CheckItemIds();
CleanupComponents(); CleanupComponents();
UpdateVersion(); UpdateVersion();
}
protected virtual void UpdateVersion()
{
if (version == CURRENT_VERSION)
{
// No need to update
return;
}
// Tell all components that implement IUpdateable to update to the new version
foreach (Component component in GetComponents<Component>())
{
IUpdateable u = component as IUpdateable;
if (u != null)
{
u.UpdateToVersion(version, CURRENT_VERSION);
}
}
version = CURRENT_VERSION;
} }
public virtual void OnDisable() public virtual void OnDisable()
@ -302,26 +333,6 @@ namespace Fungus
} }
} }
private void UpdateVersion()
{
// If versions match, then we are already using the latest.
if (version == CURRENT_VERSION) return;
switch (version)
{
// Version never set, so we are initializing on first creation or this flowchart is pre-versioning.
case null:
case "":
Initialize();
break;
}
version = CURRENT_VERSION;
}
protected virtual void Initialize()
{}
protected virtual Block CreateBlockComponent(GameObject parent) protected virtual Block CreateBlockComponent(GameObject parent)
{ {
Block block = parent.AddComponent<Block>(); Block block = parent.AddComponent<Block>();

Loading…
Cancel
Save