using System.IO;
using UnityEngine;
namespace Ubisoft.Hotel.Package.Editor
{
// Class responsible for setting up Orion SocialCore.PlayGames for Android.
//
// It requires consumers to pass in games-ids.json to this property.
// SocialCoreBuildProperty copies this file into Assets/Hotel/Plugins/Android/com.ubisoft.hotel.packagemanager.androidlib/res/values and
// retrieves app id from this file and sets it up in Assets/Plugins/Android/com.ubisoft.orion.socialcore.playgames.androidlib/AndroidManifest.xml.
public class SocialCoreBuildProperty : PackageProperty
{
private const string PLUGINS_ORION_SOCIALCORE_PATH = "Assets/Plugins/Android/com.ubisoft.orion.socialcore.playgames.androidlib/AndroidManifest.xml";
private const string PLUGINS_HOTEL_PACKAGEMANAGER_PATH = "Assets/Hotel/Plugins/Android/com.ubisoft.hotel.packagemanager.androidlib";
private const string ANDROID_MANIFEST_APP_ID_NAME = "com.google.android.gms.games.APP_ID";
private const string ANDROID_MANIFEST_APP_ID_VALUE_PREFIX = "\\u003";
public static SocialCoreBuildProperty CreateInstanceWithParams(string name, string srcGamesIdsUnityPath = "")
{
SocialCoreBuildProperty returnValue = CreateInstance();
returnValue.name = name;
returnValue.SrcGamesIdsUnityPath = srcGamesIdsUnityPath;
return returnValue;
}
[SerializeField]
private string m_srcGamesIdsUnityPath;
private string SrcGamesIdsUnityPath
{
get { return m_srcGamesIdsUnityPath; }
set { m_srcGamesIdsUnityPath = value; }
}
public void Clear()
{
SrcGamesIdsUnityPath = "";
}
private string CalculateAppId()
{
string returnValue = null;
if (!string.IsNullOrEmpty(SrcGamesIdsUnityPath))
{
// App id is calculated by opening game_ids.xml
string platformPath = HtAssetDatabase.UnityPathToPlatformPath($"Hotel/Assets/{SrcGamesIdsUnityPath}", true);
if (File.Exists(platformPath))
{
string[] lines = File.ReadAllLines(platformPath);
int count = lines.Length;
for (int i = 0; i < count && returnValue == null; ++i)
{
if (lines[i].Contains("name=\"app_id\""))
{
int startIndex = lines[i].IndexOf(">");
int endIndex = lines[i].IndexOf("<", startIndex);
if (endIndex > startIndex)
{
returnValue = lines[i].Substring(startIndex + 1, endIndex - startIndex - 1);
}
}
}
}
}
return returnValue;
}
public override void Apply(BuildSuite buildSuite)
{
if (buildSuite != null)
{
buildSuite.OrderProperty(this);
}
}
public void Join(SocialCoreBuildProperty p)
{
if (p != null)
{
// game_ids path
if (!string.IsNullOrEmpty(p.SrcGamesIdsUnityPath))
{
if (string.IsNullOrEmpty(SrcGamesIdsUnityPath))
{
SrcGamesIdsUnityPath = p.SrcGamesIdsUnityPath;
}
else if (!SrcGamesIdsUnityPath.Equals(p.SrcGamesIdsUnityPath))
{
string valuesStr = $"Values: {Debug.FormatTextInUserContext(p.SrcGamesIdsUnityPath)} and {Debug.FormatTextInUserContext(SrcGamesIdsUnityPath)}.";
Debug.EditorLogError($"Two {Debug.FormatTextInCodeContext("SorcialCoreBuildProperty")} properties have been defined but Hotel only supports one. {valuesStr}");
}
}
}
}
public void PreBuild()
{
string appId = CalculateAppId();
if (!string.IsNullOrEmpty(appId))
{
// Set app id in orion SocialCore.PlayGames Plugins/AndroidManifest.xml
string platformPath = HtAssetDatabase.UnityPathToPlatformPath(PLUGINS_ORION_SOCIALCORE_PATH, true);
if (File.Exists(platformPath))
{
bool applyingAppId = false;
string[] lines = File.ReadAllLines(platformPath);
int count = lines.Length;
for (int i = 0; i < count; ++i)
{
if (!applyingAppId)
{
applyingAppId = lines[i].Contains(ANDROID_MANIFEST_APP_ID_NAME);
}
if (applyingAppId && lines[i].Contains(ANDROID_MANIFEST_APP_ID_VALUE_PREFIX))
{
int startIndex = lines[i].IndexOf(ANDROID_MANIFEST_APP_ID_VALUE_PREFIX);
int endIndex = lines[i].IndexOf("\"", startIndex);
if (endIndex > startIndex)
{
string token = lines[i].Substring(startIndex, endIndex - startIndex);
lines[i] = lines[i].Replace(token, $"{ANDROID_MANIFEST_APP_ID_VALUE_PREFIX}{appId}");
break;
}
applyingAppId = false;
}
}
File.WriteAllLines(platformPath, lines);
}
// Copy games-ids.xml to Hotel PackageManager res/value folder
platformPath = HtAssetDatabase.UnityPathToPlatformPath(PLUGINS_HOTEL_PACKAGEMANAGER_PATH, true);
if (Directory.Exists(platformPath))
{
platformPath = HtAssetDatabase.PlatformPathCombine(platformPath, "res");
if (!Directory.Exists(platformPath))
{
_ = Directory.CreateDirectory(platformPath);
}
platformPath = HtAssetDatabase.PlatformPathCombine(platformPath, "values");
if (!Directory.Exists(platformPath))
{
_ = Directory.CreateDirectory(platformPath);
}
platformPath = HtAssetDatabase.PlatformPathCombine(platformPath, "games-ids.xml");
if (File.Exists(platformPath))
{
File.Delete(platformPath);
}
string srcPlatformPath = HtAssetDatabase.UnityPathToPlatformPath($"Hotel/Assets/{SrcGamesIdsUnityPath}", true);
File.Copy(srcPlatformPath, platformPath);
UnityEditor.AssetDatabase.Refresh();
}
}
else
{
// Delete games-ids.xml from Hotel PackageManager res/value folder
string platformPath = HtAssetDatabase.UnityPathToPlatformPath($"{PLUGINS_HOTEL_PACKAGEMANAGER_PATH}/res/values/games-ids.xml", true);
if (File.Exists(platformPath))
{
File.Delete(platformPath);
platformPath = HtAssetDatabase.UnityPathToPlatformPath($"{PLUGINS_HOTEL_PACKAGEMANAGER_PATH}/res/value", true);
Directory.Delete(platformPath);
}
}
}
public void PostBuild()
{
}
}
}