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

Fix post process to manage binaries on iOS builds #90

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning]
(https://semver.org/spec/v2.0.0.html).

## [4.6.2] - 2023-03-08
vitorbaraujo marked this conversation as resolved.
Show resolved Hide resolved
### Fixed
- (unity) Fix post processor to manage pitaya libraries on iOS builds.

## [4.6.1] - 2023-02-07
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion include/pitaya_version.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define PC_VERSION_MAJOR 4
#define PC_VERSION_MINOR 6
#define PC_VERSION_REVISION 0
#define PC_VERSION_REVISION 2
#define PC_VERSION_SUFFIX ""
49 changes: 0 additions & 49 deletions unity/PitayaExample/Assets/Pitaya/Editor/BuildProcessor.cs

This file was deleted.

11 changes: 0 additions & 11 deletions unity/PitayaExample/Assets/Pitaya/Editor/BuildProcessor.cs.meta

This file was deleted.

130 changes: 81 additions & 49 deletions unity/PitayaExample/Assets/Pitaya/Editor/PitayaXcodeProjectUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,90 @@
#if UNITY_IOS || UNITY_EDITOR
#if UNITY_IOS || UNITY_EDITOR_OSX
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System;
using System.IO;
using System.Linq;

public class PitayaBuildPostprocessor
{
private const string SIMULATOR_IDENTIFIER = "simulator";
private const string DEVICE_IDENTIFIER = "device";
private const string FRAMEWORK_ORIGIN_PATH = "Assets/Pitaya/Native/iOS";
private const string FRAMEWORK_UPM_ORIGIN_PATH = "Libraries/com.wildlifestudios.nuget.libpitaya/Native/iOS";

[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.iOS)
{
var projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";

PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));

var targetGuid = proj.GetUnityFrameworkTargetGuid();

// Pitaya should be linked with zlib when on iOS.
proj.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-lz");

// To avoid referencing wrong libraries, the folder containing those
// libraries should be hidden depending on which target the build
// is running (device or simulator).
foreach (var file in Directory.GetFiles(GetPathToHide(path))) {
var fileGuid = proj.FindFileGuidByProjectPath(file.Substring(path.Length + 1));
// Files will be kept in the build folder, but won't be referenced in the built project
proj.RemoveFile(fileGuid);
}

File.WriteAllText(projPath, proj.WriteToString());
}
}

private static string GetPathToHide(string projectPath)
{
var originPath = Path.Combine(projectPath, FRAMEWORK_ORIGIN_PATH);
if (!Directory.Exists(originPath))
{
originPath = Path.Combine(projectPath, FRAMEWORK_UPM_ORIGIN_PATH);
}

if(PlayerSettings.iOS.sdkVersion == iOSSdkVersion.DeviceSDK)
{
return Path.Combine(originPath, SIMULATOR_IDENTIFIER);
}

return Path.Combine(originPath, DEVICE_IDENTIFIER);
}
private const string SIMULATOR_IDENTIFIER = "simulator";
private const string DEVICE_IDENTIFIER = "device";
private const string PITAYA_IOS_PATH = "PitayaNativeLibraries/iOS";

[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.iOS)
{
var projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
vitorbaraujo marked this conversation as resolved.
Show resolved Hide resolved
var currPath = Directory.GetCurrentDirectory();
var lookupPaths = new string[]
{
Path.Combine(currPath, "Assets"), // relative to project path
Path.Combine(currPath, "Library", "PackageCache"), // relative to project path
Path.Combine(path, "Libraries") // relative to build path
};
vitorbaraujo marked this conversation as resolved.
Show resolved Hide resolved

PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));

var targetGuid = proj.GetUnityFrameworkTargetGuid();

// Pitaya should be linked with zlib when on iOS.
proj.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-lz");

var filesToRemove = new List<string>();
foreach (var lookupPath in lookupPaths)
{
if (Directory.Exists(lookupPath)) {
vitorbaraujo marked this conversation as resolved.
Show resolved Hide resolved
filesToRemove
.AddRange(Directory.GetFiles(lookupPath, "*", SearchOption.AllDirectories)
.Where(f => f.Contains(GetPathToHide())));
vitorbaraujo marked this conversation as resolved.
Show resolved Hide resolved
}
}

// To avoid referencing wrong libraries, the folder containing those
// libraries should be hidden depending on which target the build
// is running (device or simulator).
foreach (var file in filesToRemove)
{
var relative = GetRelativePath(path + Path.DirectorySeparatorChar, file);
vitorbaraujo marked this conversation as resolved.
Show resolved Hide resolved
var fileGuid = proj.FindFileGuidByRealPath(relative);
if (fileGuid == null)
{
fileGuid = proj.FindFileGuidByProjectPath(relative);
}
// Files will be kept in the build folder, but won't be referenced in the built project
proj.RemoveFile(fileGuid);
}

File.WriteAllText(projPath, proj.WriteToString());
vitorbaraujo marked this conversation as resolved.
Show resolved Hide resolved
}
}

private static string GetPathToHide()
{
if(PlayerSettings.iOS.sdkVersion == iOSSdkVersion.DeviceSDK)
{
return Path.Combine(PITAYA_IOS_PATH, SIMULATOR_IDENTIFIER);
}

return Path.Combine(PITAYA_IOS_PATH, DEVICE_IDENTIFIER);
}

private static string GetRelativePath(string relativeTo, string path)
{
var uri = new Uri(relativeTo);
var rel = Uri.UnescapeDataString(uri.MakeRelativeUri(new Uri(path))
.ToString())
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (rel.Contains(Path.DirectorySeparatorChar.ToString()) == false)
{
rel = $".{ Path.DirectorySeparatorChar }{ rel }";
}
return rel;
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define PC_VERSION_MAJOR 4
#define PC_VERSION_MINOR 6
#define PC_VERSION_REVISION 0
#define PC_VERSION_REVISION 2
#define PC_VERSION_SUFFIX ""