Skip to content

Instantly share code, notes, and snippets.

@cdhanna
Last active February 10, 2021 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdhanna/a76c9dd7e63ba10f4ccf09a175f20bb7 to your computer and use it in GitHub Desktop.
Save cdhanna/a76c9dd7e63ba10f4ccf09a175f20bb7 to your computer and use it in GitHub Desktop.
A tool to aid in Beamable package migration
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.VersionControl;
using UnityEngine;
public class BeamableMigrationHelper : EditorWindow
{
[MenuItem(
"Window/Beamable/Migration Tool",
priority = 0)]
public static void RunTool(){
Debug.Log($"Starting migration tool.... {BEAMABLE_FOLDER}");
RenameDisruptorEngineToBeamable();
FixContentScriptGuids();
AssetDatabase.Refresh();
}
private static readonly string DISRUPTOR_ENGINE_FOLDER = Path.Combine(Application.dataPath, "DisruptorEngine");
private static readonly string BEAMABLE_FOLDER = Path.Combine(Application.dataPath, "Beamable");
private static readonly ContentGuidFixData[] GuidFixes = new[]
{
new ContentGuidFixData
{
Name = "Item",
NewGuid = "4e65df06d8f8e499788b3ed5b359d54a",
OldGuid = "6e27788e2a314236a140dc4a409b9249"
},
new ContentGuidFixData
{
Name = "Currency",
NewGuid = "7071e886d4789435d9ad361e916b2207",
OldGuid = "5531181e6a08e4d6882d1e0bf64aeb5e"
},
new ContentGuidFixData
{
Name = "Announcements",
NewGuid = "8b8612187b1a246e5b691c90486deb34",
OldGuid = "ec1a162a275224c81b4dc66174e0abfb"
},
new ContentGuidFixData
{
Name = "Calendars",
NewGuid = "0c222bf6b195348e5be37e5cf0169f08",
OldGuid = "485745a1357640d0999ad440992934fb"
},
new ContentGuidFixData
{
Name = "Listings",
NewGuid = "fd68497a79b464de0b4e09ab2d5a5bbf",
OldGuid = "840eefedb06f44a35ba9ffd558ba8840"
},
new ContentGuidFixData
{
Name = "SKU",
NewGuid = "b5bfeeca7cf3246b3a89418e98719de7",
OldGuid = "8813c741626bd459797986cbf75812db"
},
new ContentGuidFixData
{
Name = "Store",
NewGuid = "a658558ea86c749979b310e828e3892e",
OldGuid = "4e4344ba97e6d4cbeb1a7d24ad6f778b"
},
new ContentGuidFixData
{
Name = "Leaderboard",
NewGuid = "ebf776d7c945048daab1e231d27dcdeb",
OldGuid = "7bd5890ede5a415ba93437f0d0ce7914"
},
new ContentGuidFixData
{
Name = "Tournaments",
NewGuid = "6da5f82a5df104fd4b67a842fafa5c5a",
OldGuid = "1d36333db684547d09c3cbbfff05d3e1"
},
new ContentGuidFixData
{
Name = "Events",
NewGuid = "11f962489e5a44d7c877b4a5b71f4d0e",
OldGuid = "e62f0767b71f47740b2ab2b9a03aa03f"
},
new ContentGuidFixData
{
Name = "GameType",
NewGuid = "404f8a85a397642f5b56ce1f2d8cebea",
OldGuid = "3e764f5e52bc4814ad80326864ba4aff"
},
new ContentGuidFixData
{
Name = "Email",
NewGuid = "b5d38e7e955cc47f9933ec9a3a1e2c5a",
OldGuid = "3b9f329a7c08143f19cad1d65673e105"
}
};
static void RenameDisruptorEngineToBeamable()
{
if (!Directory.Exists(DISRUPTOR_ENGINE_FOLDER))
{
Debug.Log("folder already renamed");
return;
}
Debug.Log("renaming folder...");
try
{
DirectoryCopy(DISRUPTOR_ENGINE_FOLDER, BEAMABLE_FOLDER, true);
}
catch (Exception ex)
{
Debug.LogError($"migration failed at rename folder step. error=[{ex.Message}] stack=[{ex.StackTrace}]");
Debug.LogError(ex);
throw;
}
}
static void FixContentScriptGuids()
{
Directory.CreateDirectory(BEAMABLE_FOLDER);
Debug.Log("checking content files");
var filePaths = Directory.GetFiles(BEAMABLE_FOLDER, "*.asset", SearchOption.AllDirectories);
Debug.Log($"found {filePaths.Length} content files");
var modifiedFiles = 0;
foreach (var filePath in filePaths)
{
var text = File.ReadAllText(filePath);
var modifications = new HashSet<string>();
foreach (var fix in GuidFixes)
{
if (text.Contains(fix.OldGuid))
{
text = text.Replace(fix.OldGuid, fix.NewGuid);
modifications.Add(fix.Name);
}
}
if (modifications.Count > 0)
{
modifiedFiles++;
File.WriteAllText(filePath, text);
Debug.Log($"Fixed content file fixes=[{string.Join(",",modifications)}] file=[{filePath}]");
}
}
Debug.Log($"modified {modifiedFiles} files");
}
struct ContentGuidFixData
{
public string Name,OldGuid, NewGuid;
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
Directory.CreateDirectory(destDirName);
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string tempPath = Path.Combine(destDirName, file.Name);
var isUsingVersionControl = Provider.enabled && Provider.isActive;
if (isUsingVersionControl)
{
var checkoutTargetFileTask = Provider.Checkout(tempPath, CheckoutMode.Both);
checkoutTargetFileTask.Wait();
if (checkoutTargetFileTask.success)
{
Debug.LogWarning($"Unable to checkout file {tempPath}");
}
var checkoutSourceFileTask = Provider.Checkout(file.Name, CheckoutMode.Both);
checkoutSourceFileTask.Wait();
if (checkoutSourceFileTask.success)
{
Debug.LogWarning($"Unable to checkout file {file.Name}");
}
}
file.CopyTo(tempPath, true);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string tempPath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, tempPath, copySubDirs);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment