|
using UnityEngine; |
|
using UnityEditor; |
|
using System.IO; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Text.RegularExpressions; |
|
|
|
public static class ConvertSerializationVersion |
|
{ |
|
private static Regex objectIDSearch = new Regex("--- !u!(?:[0-9]{1,5}) &([0-9]*)"); |
|
|
|
[MenuItem("Assets/Convert Serialization Version 5.0", false)] |
|
public static void ConvertSerializationVersion50 () { ConvertSelectedAssetSerializationVersion (1); } |
|
|
|
[MenuItem("Assets/Convert Serialization Version 5.3", false)] |
|
public static void ConvertSerializationVersion53 () { ConvertSelectedAssetSerializationVersion (2); } |
|
|
|
[MenuItem("Assets/Convert Serialization Version 5.5", false)] |
|
public static void ConvertSerializationVersion55 () { ConvertSelectedAssetSerializationVersion (3); } |
|
|
|
[MenuItem("Assets/Convert Serialization Version 5.0", true)] |
|
[MenuItem("Assets/Convert Serialization Version 5.3", true)] |
|
[MenuItem("Assets/Convert Serialization Version 5.5", true)] |
|
public static bool ValidateAsset () |
|
{ |
|
if (Selection.activeObject == null || Selection.activeObject.GetType () == typeof (UnityEditor.DefaultAsset)) |
|
return false; |
|
return !string.IsNullOrEmpty (AssetDatabase.GetAssetPath (Selection.activeInstanceID)); |
|
} |
|
|
|
public static void ConvertSelectedAssetSerializationVersion (int version) |
|
{ |
|
foreach (var instanceID in Selection.instanceIDs) |
|
{ |
|
|
|
string path = AssetDatabase.GetAssetPath (instanceID); |
|
Debug.Log(string.Format("Handling instanceId:{0}, path:{1}", instanceID, path)); |
|
if (string.IsNullOrEmpty (path)) |
|
throw new System.InvalidOperationException ("Selected object is not an asset!"); |
|
|
|
// Fetch serialized data |
|
StringBuilder data = new StringBuilder (File.ReadAllText (path)); |
|
if (data.Length == 0) |
|
throw new System.FormatException ("Could not read text from asset '" + path + "'! Make sure you have text serialization force on!"); |
|
|
|
// Convert data to new version |
|
SetSerializationVersion (ref data, version); |
|
|
|
// Save converted file |
|
path = Path.GetDirectoryName (path) + Path.AltDirectorySeparatorChar + Path.GetFileNameWithoutExtension (path) + "_Converted" + Path.GetExtension (path); |
|
if (path.StartsWith ("Assets")) |
|
path = Application.dataPath + path.Substring(6); |
|
File.WriteAllText (path, data.ToString ()); |
|
AssetDatabase.Refresh (); |
|
} |
|
|
|
} |
|
|
|
public static bool SetSerializationVersion (ref StringBuilder data, int version) |
|
{ |
|
if (version < 2) |
|
{ // Pre 5.3 the file IDs were under 10 chars long (usually 8 or 9 max), Longer IDs, which are used in 5.3+, will corrupt the file |
|
MatchCollection matches = objectIDSearch.Matches (data.ToString ()); |
|
foreach (Match match in matches) |
|
{ |
|
if (match.Groups.Count != 2) |
|
{ |
|
Debug.LogWarning ("Discarding match '" + match.Value + "'!"); |
|
continue; |
|
} |
|
string key = match.Groups[0].Value; |
|
string id = match.Groups[1].Value; |
|
if (id.Length > 10) |
|
{ // Cut id string length (TODO: make sure no duplicate IDs are existing when cutting chars, although unlikely) |
|
string cutID = id.Substring (0, 8); |
|
data.Replace (id, cutID); |
|
} |
|
} |
|
} |
|
if (version < 3) // Just lower serialized version for GameObjects only so Unity doesn't mess with the data (fixes empty name field pre-5.5) |
|
data.Replace ("serializedVersion: 5", "serializedVersion: 4"); |
|
return true; |
|
} |
|
} |