Skip to content

Commit 7f94742

Browse files
refactor: Use shared constants and add error handling for Analytics DLL copy
This commit addresses feedback from the previous review: - Created `FirebaseAnalyticsEditorConstants.cs` to store shared constants for: - `DllName` ("analytics_win.dll") - `DllSourcePath` ("./analytics_win.dll") - `AnalyticsTypeFullName` ("Firebase.Analytics.FirebaseAnalytics, Firebase.Analytics") - Updated `editor/app/src/DllLocationPatcher.cs`: - Replaced hardcoded strings with the new shared constants. - Wrapped file I/O operations (directory creation and DLL copy for Windows builds) in a try-catch block to handle potential exceptions (e.g., IOExceptions, UnauthorizedAccessException) and log specific errors. - Updated `editor/app/src/AnalyticsPlayModeSetup.cs`: - Replaced hardcoded strings and local constants with the new shared constants. - Wrapped file I/O operations (directory creation and DLL copy for Play Mode) in a try-catch block to handle potential exceptions and log specific errors. These changes improve maintainability by centralizing critical strings and enhance robustness by ensuring that file operation errors during build post-processing or Play Mode setup are caught and logged gracefully.
1 parent a08a6c1 commit 7f94742

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

editor/app/src/AnalyticsPlayModeSetup.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ namespace Firebase.Editor {
77

88
[InitializeOnLoad]
99
internal static class AnalyticsPlayModeSetup {
10-
private const string SourceDllPath = "./analytics_win.dll";
1110
private const string DestinationDir = "Assets/Plugins/";
12-
private const string DestinationDllName = "analytics_win.dll";
1311

1412
static AnalyticsPlayModeSetup() {
1513
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
@@ -18,21 +16,21 @@ static AnalyticsPlayModeSetup() {
1816
private static void OnPlayModeStateChanged(PlayModeStateChange state) {
1917
if (state == PlayModeStateChange.EnteredPlayMode) {
2018
if (Application.platform == RuntimePlatform.WindowsEditor) {
21-
Type firebaseAnalyticsType = Type.GetType("Firebase.Analytics.FirebaseAnalytics, Firebase.Analytics");
19+
Type firebaseAnalyticsType = Type.GetType(FirebaseAnalyticsEditorConstants.AnalyticsTypeFullName);
2220
if (firebaseAnalyticsType != null) {
23-
if (File.Exists(SourceDllPath)) {
21+
if (File.Exists(FirebaseAnalyticsEditorConstants.DllSourcePath)) {
2422
try {
2523
Directory.CreateDirectory(DestinationDir);
26-
string destinationDllPath = Path.Combine(DestinationDir, DestinationDllName);
27-
File.Copy(SourceDllPath, destinationDllPath, true);
28-
Debug.Log("Firebase Analytics: Copied " + DestinationDllName + " to " + DestinationDir + " for Play Mode.");
24+
string destinationDllPath = Path.Combine(DestinationDir, FirebaseAnalyticsEditorConstants.DllName);
25+
File.Copy(FirebaseAnalyticsEditorConstants.DllSourcePath, destinationDllPath, true);
26+
Debug.Log("Firebase Analytics: Copied " + FirebaseAnalyticsEditorConstants.DllName + " to " + DestinationDir + " for Play Mode.");
2927
AssetDatabase.Refresh();
3028
} catch (Exception e) {
31-
Debug.LogError("Firebase Analytics: Error copying " + DestinationDllName + " for Play Mode: " + e.Message);
29+
Debug.LogError("Firebase Analytics: Error copying " + FirebaseAnalyticsEditorConstants.DllName + " for Play Mode: " + e.Message);
3230
}
3331
} else {
3432
// Optional: Log if source DLL is not found, as it might be expected if Analytics is not used.
35-
// Debug.LogWarning("Firebase Analytics: Source DLL " + SourceDllPath + " not found. Skipping Play Mode setup.");
33+
// Debug.LogWarning("Firebase Analytics: Source DLL " + FirebaseAnalyticsEditorConstants.DllSourcePath + " not found. Skipping Play Mode setup.");
3634
}
3735
}
3836
}

editor/app/src/DllLocationPatcher.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,22 @@ internal static void OnPostProcessBuildWindowsAnalytics(
9696
BuildTarget buildTarget, string pathToBuiltProject) {
9797
if (buildTarget == BuildTarget.StandaloneWindows ||
9898
buildTarget == BuildTarget.StandaloneWindows64) {
99-
Type firebaseAnalyticsType = Type.GetType("Firebase.Analytics.FirebaseAnalytics, Firebase.Analytics");
99+
Type firebaseAnalyticsType = Type.GetType(FirebaseAnalyticsEditorConstants.AnalyticsTypeFullName);
100100
if (firebaseAnalyticsType != null) {
101-
string sourceDllPath = "./analytics_win.dll";
101+
string sourceDllPath = FirebaseAnalyticsEditorConstants.DllSourcePath;
102102
if (System.IO.File.Exists(sourceDllPath)) {
103-
string destinationDirectory = Path.Combine(
104-
Path.GetDirectoryName(pathToBuiltProject),
105-
Path.GetFileNameWithoutExtension(pathToBuiltProject) + "_Data",
106-
"Plugins");
107-
System.IO.Directory.CreateDirectory(destinationDirectory);
108-
string destinationDllPath = Path.Combine(destinationDirectory, "analytics_win.dll");
109-
System.IO.File.Copy(sourceDllPath, destinationDllPath, true);
110-
Debug.Log("Firebase Analytics: Copied analytics_win.dll to build plugins directory.");
103+
try {
104+
string destinationDirectory = Path.Combine(
105+
Path.GetDirectoryName(pathToBuiltProject),
106+
Path.GetFileNameWithoutExtension(pathToBuiltProject) + "_Data",
107+
"Plugins");
108+
System.IO.Directory.CreateDirectory(destinationDirectory);
109+
string destinationDllPath = Path.Combine(destinationDirectory, FirebaseAnalyticsEditorConstants.DllName);
110+
System.IO.File.Copy(sourceDllPath, destinationDllPath, true);
111+
Debug.Log("Firebase Analytics: Copied " + FirebaseAnalyticsEditorConstants.DllName + " to build plugins directory.");
112+
} catch (System.Exception e) {
113+
Debug.LogError("Firebase Analytics: Error copying " + FirebaseAnalyticsEditorConstants.DllName + " for build: " + e.Message);
114+
}
111115
}
112116
}
113117
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Firebase.Editor {
16+
17+
internal static class FirebaseAnalyticsEditorConstants {
18+
public const string DllName = "analytics_win.dll";
19+
public const string DllSourcePath = "./" + DllName;
20+
public const string AnalyticsTypeFullName = "Firebase.Analytics.FirebaseAnalytics, Firebase.Analytics";
21+
}
22+
23+
}

0 commit comments

Comments
 (0)