lib_unity_purchase/Editor/WinRTPatcher.cs

85 lines
2.4 KiB
C#

using System;
using System.IO;
using UnityEngine;
namespace UnityEditor.Purchasing
{
internal class WinRTPatcher
{
private const string k_OutputPath = "Assets/Scripts/UnityPurchasing/generated";
private const string k_WorkaroundFileName = "WindowsRuntimeWorkaround.cs";
private const string k_WorkaroundTemplateFileName = "WindowsRuntimeWorkaround.cs.template";
internal static void PatchWinRTBuild()
{
#if UNITY_2020
if (!DoesWorkaroundClassExist())
{
try
{
BuildWorkaroundClass();
}
catch (Exception patchException)
{
Debug.LogWarning(patchException.StackTrace);
}
}
AssetDatabase.Refresh();
#endif
}
private static bool DoesWorkaroundClassExist()
{
return File.Exists(GetFullPathForWorkaroundClass());
}
private static string GetFullPathForWorkaroundClass()
{
return Path.Combine(k_OutputPath, k_WorkaroundFileName);
}
private static void BuildWorkaroundClass()
{
var templateText = LoadTemplateText();
if (templateText != null)
{
GeneratePatchFile(templateText);
}
}
private static string LoadTemplateText()
{
var templateGUID = FindTemplateGUID(k_WorkaroundFileName);
string templateText = null;
if (templateGUID != null)
{
var templateAbsolutePath = Path.Combine(Path.GetDirectoryName(Application.dataPath), AssetDatabase.GUIDToAssetPath(templateGUID));
templateText = File.ReadAllText(templateAbsolutePath);
}
else
{
Debug.LogError($"Could not find template \"{k_WorkaroundTemplateFileName}\".");
}
return templateText;
}
private static string FindTemplateGUID(string templateFilename)
{
var assetGUIDs = AssetDatabase.FindAssets(k_WorkaroundFileName);
return (assetGUIDs.Length > 0) ? assetGUIDs[0] : null;
}
private static void GeneratePatchFile(string templateText)
{
Directory.CreateDirectory(k_OutputPath);
File.WriteAllText(GetFullPathForWorkaroundClass(), templateText);
}
}
}