Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add switching firebase settings task #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/FirebaseConfig.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/FirebaseConfig/development.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BUNDLE_ID</key>
<string>com.example.app.dev</string>
</dict>
</plist>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Assets/FirebaseConfig/development/google-services_development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"client": [
{
"client_info": {
"android_client_info": {
"package_name": "com.example.app.dev"
}
}
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/FirebaseConfig/release.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BUNDLE_ID</key>
<string>com.example.app</string>
</dict>
</plist>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Assets/FirebaseConfig/release/google-services_release.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"client": [
{
"client_info": {
"android_client_info": {
"package_name": "com.example.app"
}
}
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// --------------------------------------------------------------
// Copyright 2024 CyberAgent, Inc.
// --------------------------------------------------------------

using System;
using System.IO;
using System.Xml;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace BuildMagicEditor.BuiltIn
{
[GenerateBuildTaskAccessories("Copy Firebase Config",
PropertyName = "BuildMagicEditor.BuiltIn.CopyFirebaseConfigTask")]
public class CopyFirebaseConfigTask : BuildTaskBase<IPreBuildContext>
{
private readonly string _destinationDirectory;
private readonly Object _googleServiceInfoPlist;
private readonly Object _googleServicesJson;

public CopyFirebaseConfigTask(string destinationDirectory, Object googleServiceInfoPlist,
Object googleServicesJson)
{
_googleServicesJson = googleServicesJson;
_googleServiceInfoPlist = googleServiceInfoPlist;
_destinationDirectory = destinationDirectory;
}

public override void Run(IPreBuildContext context)
{
var dest = Path.Combine("Assets", _destinationDirectory);
Directory.CreateDirectory(dest);

if (_googleServiceInfoPlist)
{
var source = AssetDatabase.GetAssetPath(_googleServiceInfoPlist);
{
var bundleId = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS);
// It may contain multiple bundle ids
if (!ContainsBundleIdGoogleServiceInfoPlist(source, bundleId))
throw new InvalidOperationException(
$"{source} does not contain the bundle id \"{bundleId}\" which is currently set to this project.");
}
File.Copy(AssetDatabase.GetAssetPath(_googleServiceInfoPlist),
Path.Combine(dest, "GoogleService-Info.plist"), true);
}

if (_googleServicesJson)
{
var source = AssetDatabase.GetAssetPath(_googleServicesJson);
{
var bundleId = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.Android);
// It may contain multiple bundle ids
if (!ContainsBundleIdGoogleServicesJson(source, bundleId))
throw new InvalidOperationException(
$"{source} does not contain the bundle id \"{bundleId}\" which is currently set to this project.");
}
File.Copy(source, Path.Combine(dest, "google-services.json"), true);
}

AssetDatabase.Refresh();
}

private bool ContainsBundleIdGoogleServiceInfoPlist(string path, string bundleId)
{
var doc = new XmlDocument();
using var reader = new StreamReader(path);
doc.Load(reader);
if (doc.SelectSingleNode("plist/dict") is not { } dictNode) return false;

for (var i = 0; i < dictNode.ChildNodes.Count; i++)
{
var node = dictNode.ChildNodes[i];
if (node.Name == "key" && node.InnerText == "BUNDLE_ID")
{
var valueNode = dictNode.ChildNodes[i + 1];
if (valueNode.Name != "string")
continue;

if (valueNode.InnerText == bundleId) return true;
}
}

return false;
}

private bool ContainsBundleIdGoogleServicesJson(string path, string bundleId)
{
var json = JsonUtility.FromJson<GoogleServicesJson>(File.ReadAllText(path));
foreach (var client in json.client)
{
var packageName = client.client_info.android_client_info.package_name;
if (packageName == bundleId) return true;
}

return false;
}

[Serializable]
private class GoogleServicesJson
{
public Client[] client;

[Serializable]
public class Client
{
public ClientInfo client_info;
}

[Serializable]
public class ClientInfo
{
public AndroidClientInfo android_client_info;
}

[Serializable]
public class AndroidClientInfo
{
public string package_name;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.