-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add backwards compatibility support (graph conversion)
- Loading branch information
1 parent
43542e1
commit 441438b
Showing
10 changed files
with
169 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
Editor/Util/Versioning/Conversion/ConvertMethodAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
|
||
namespace Dlog { | ||
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false), ] | ||
public class ConvertMethodAttribute : Attribute { | ||
public readonly SemVer TargetVersion; | ||
|
||
/// <summary> | ||
/// Specifies that tha attached method is a converting method (from one version of Dialogue Graph to another) | ||
/// </summary> | ||
/// <param name="targetVersion">The target version the method converts to.</param> | ||
public ConvertMethodAttribute(string targetVersion) { | ||
TargetVersion = (SemVer)targetVersion; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Editor/Util/Versioning/Conversion/ConvertMethodAttribute.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using UnityEngine; | ||
|
||
namespace Dlog { | ||
public static class VersionConverter { | ||
private static readonly SemVer v111 = (SemVer) "1.1.1"; | ||
private static readonly SemVer v112 = (SemVer) "1.1.2"; | ||
|
||
private static SemVer[] sortedVersions = {v111, v112}; | ||
|
||
private static bool builtMethodCache; | ||
private static Dictionary<SemVer, Action<DlogGraphObject>> upgradeMethodCache; | ||
|
||
private static SemVer GetNextVersion(SemVer from) { | ||
for (var i = 1; i < sortedVersions.Length; i++) { | ||
var comparePrev = from.CompareTo(sortedVersions[i - 1]); | ||
var compareNext = from.CompareTo(sortedVersions[i]); | ||
if (comparePrev >= 0 && compareNext < 0) return sortedVersions[i]; | ||
} | ||
|
||
return SemVer.Invalid; | ||
} | ||
|
||
public static void ConvertVersion(SemVer from, SemVer to, DlogGraphObject dlogObject) { | ||
if (from == to) return; | ||
var next = GetNextVersion(from); | ||
if (next == SemVer.Invalid) { | ||
Debug.Log($"Could not find upgrading method [{from} -> {to}]"); | ||
return; | ||
} | ||
|
||
UpgradeTo(next, dlogObject); | ||
ConvertVersion(next, to, dlogObject); | ||
} | ||
|
||
|
||
[ConvertMethod("1.1.2")] | ||
private static void U_112(DlogGraphObject dlogObject) { | ||
dlogObject.DlogGraph.DialogueGraphVersion = v112; | ||
} | ||
|
||
private static void UpgradeTo(SemVer version, DlogGraphObject dlogGraphObject) { | ||
if (!builtMethodCache) { | ||
BuildMethodCache(); | ||
} | ||
if(upgradeMethodCache.ContainsKey(version)) | ||
upgradeMethodCache[version](dlogGraphObject); | ||
else Debug.LogWarning($"Upgrade conversion with [target={version}] is not supported."); | ||
} | ||
|
||
|
||
private static void BuildMethodCache() { | ||
Debug.Log("Building cache"); | ||
builtMethodCache = true; | ||
upgradeMethodCache = new Dictionary<SemVer, Action<DlogGraphObject>>(); | ||
|
||
var methods = typeof(VersionConverter).GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); | ||
foreach (var method in methods) { | ||
var attributes = method.GetCustomAttributes<ConvertMethodAttribute>(false).ToList(); | ||
if (attributes.Count <= 0) continue; | ||
var attribute = attributes[0]; | ||
|
||
var methodCall = method.CreateDelegate(typeof(Action<DlogGraphObject>)) as Action<DlogGraphObject>; | ||
upgradeMethodCache.Add(attribute.TargetVersion, methodCall); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters