Skip to content

Commit

Permalink
Update how frameworks are added to the iOS project
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Miller committed Feb 6, 2018
1 parent 10832dc commit 732d214
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 22 deletions.
72 changes: 72 additions & 0 deletions Sources/UBKit/Files/Unity/UnityFrameworksScript.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// UnityProjectScript.swift
// UnityBuildKitPackageDescription
//
// Copyright (c) 2017 Handsome
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation

extension File {

class func unityFrameworksScriptFile(projectName: String, iOSProjectPath: String) -> Data? {
let file = """
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using UnityEditor.iOS.Xcode;
public class XcodeFrameworks: MonoBehaviour {
private const string iOSProjectRoot = \"\(iOSProjectPath)\";
private const string iOSProjectName = \"\(projectName)\";
private const string PbxFilePath = iOSProjectName + ".xcodeproj/project.pbxproj";
public static void Perform () {
var pbx = new PBXProject();
var pbxPath = Path.Combine(iOSProjectRoot, PbxFilePath);
pbx.ReadFromFile(pbxPath);
var targetGuid = pbx.TargetGuidByName(iOSProjectName);
pbx.AddFrameworkToProject(targetGuid, "GameKit.framework", true);
pbx.AddFrameworkToProject(targetGuid, "CoreGraphics.framework", false);
pbx.AddFrameworkToProject(targetGuid, "AVFoundation.framework", false);
pbx.AddFrameworkToProject(targetGuid, "CoreVideo.framework", false);
pbx.AddFrameworkToProject(targetGuid, "CoreMedia.framework", false);
pbx.AddFrameworkToProject(targetGuid, "SystemConfiguration.framework", false);
pbx.AddFrameworkToProject(targetGuid, "CoreLocation.framework", false);
pbx.AddFrameworkToProject(targetGuid, "MediaPlayer.framework", false);
pbx.AddFrameworkToProject(targetGuid, "CFNetwork.framework", false);
pbx.AddFrameworkToProject(targetGuid, "AudioToolbox.framework", false);
pbx.AddFrameworkToProject(targetGuid, "OpenAL.framework", false);
pbx.AddFrameworkToProject(targetGuid, "QuartzCore.framework", false);
pbx.AddFrameworkToProject(targetGuid, "Foundation.framework", false);
pbx.AddFrameworkToProject(targetGuid, "MediaToolbox.framework", false);
pbx.WriteToFile(pbxPath);
}
}
""".data(using: .utf8)
return file
}
}
2 changes: 1 addition & 1 deletion Sources/UBKit/Files/Xcode/SpecFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension File {
UNITY_SCRIPTING_BACKEND: il2cpp
UNITY_IOS_EXPORT_PATH: ${SRCROOT}/../Unity/\(projectName)/ios_build
GCC_PREFIX_HEADER: $(UNITY_IOS_EXPORT_PATH)/Classes/Prefix.pch
OTHER_LDFLAGS: -weak-lSystem -weak_framework CoreMotion -weak_framework GameKit -weak_framework iAd -framework CoreGraphics -framework AVFoundation -framework CoreVideo -framework CoreMedia -framework SystemConfiguration -framework CoreLocation -framework MediaPlayer -framework CFNetwork -framework AudioToolbox -framework OpenAL -framework QuartzCore -framework OpenGLES -framework UIKit -framework Foundation -framework MediaToolbox -liconv.2 -liPhone-lib
OTHER_LDFLAGS: -weak-lSystem -liconv.2 -liPhone-lib -weak_framework CoreMotion -weak_framework iAd -framework OpenGLES
HEADER_SEARCH_PATHS: $(UNITY_IOS_EXPORT_PATH)/Classes $(UNITY_IOS_EXPORT_PATH)/Classes/Native $(UNITY_IOS_EXPORT_PATH)/Libraries/libil2cpp/include
LIBRARY_SEARCH_PATHS: $(UNITY_IOS_EXPORT_PATH)/Libraries $(UNITY_IOS_EXPORT_PATH)/Libraries/libil2cpp/include
ENABLE_BITCODE: NO
Expand Down
1 change: 1 addition & 0 deletions Sources/UBKit/Models/UnityCommandLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Foundation
struct UnityCommandLine {
static let buildAction = "iOSBuilder.Perform"
static let refreshAction = "XcodeRefresher.Refresh"
static let frameworksAction = "XcodeFrameworks.Perform"

struct Arguments {
static let projectPath = "-projectPath"
Expand Down
58 changes: 38 additions & 20 deletions Sources/UBKit/Workers/FileCopier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class FileCopier {
return unityFilesResult
}

let addFrameworksResult = addXcodeFrameworks()
guard addFrameworksResult == .success else {
return addFrameworksResult
}

return .success
}
}
Expand Down Expand Up @@ -113,25 +118,6 @@ private extension FileCopier {
return .success
}

func x() -> Result {
guard let project = project else {
return .failure(UBKitError.invalidXcodeProject("Failed to find project file"))
}

guard let mainTarget = project.pbxproj.objects.nativeTargets.filter({ $0.value.name == config.iOS.projectName }).first else {
return .failure(UBKitError.invalidXcodeProject("Missing main target"))
}

for phase in mainTarget.value.buildPhases {
if phase.starts(with: "RBP_") {

break
}
}

return .success
}

func changeUnityFiles() -> Result {
let mainFilePath = workingPath.appending(config.unity.projectName).appending("/ios_build/Classes/main.mm")
guard fileManager.fileExists(atPath: mainFilePath) else {
Expand Down Expand Up @@ -165,12 +151,13 @@ private extension FileCopier {
func addUnityFiles() -> Result {
let semaphore = DispatchSemaphore(value: 0)
var statusCode: Int32 = 999
let projectPath = workingPath.appending(config.unity.projectName).appending("/ios_build")

shell.perform(
config.unity.applicationPath,
UnityCommandLine.Arguments.batchmode,
UnityCommandLine.Arguments.buildPath,
workingPath.appending(config.unity.projectName).appending("/ios_build"),
projectPath,
UnityCommandLine.Arguments.executeMethod,
UnityCommandLine.refreshAction,
UnityCommandLine.Arguments.quit,
Expand All @@ -192,6 +179,37 @@ private extension FileCopier {
}
}

func addXcodeFrameworks() -> Result {
let semaphore = DispatchSemaphore(value: 0)
var statusCode: Int32 = 999
let projectPath = workingPath.appending(config.unity.projectName)

shell.perform(
config.unity.applicationPath,
UnityCommandLine.Arguments.batchmode,
UnityCommandLine.Arguments.projectPath,
projectPath,
UnityCommandLine.Arguments.executeMethod,
UnityCommandLine.frameworksAction,
UnityCommandLine.Arguments.quit,
terminationHandler: { (process) in
statusCode = process.terminationStatus
semaphore.signal()
})

let timeout = semaphore.wait(timeout: DispatchTime.now()+60.0)
switch timeout {
case .success:
if statusCode == 0 {
return .success
} else {
return .failure(UBKitError.shellCommand("Initializing Unity Project"))
}
case .timedOut:
return .failure(UBKitError.waitTimedOut)
}
}

func saveProject() -> Result {
guard let project = project else {
return .failure(UBKitError.invalidXcodeProject("Failed to find project file"))
Expand Down
9 changes: 8 additions & 1 deletion Sources/UBKit/Workers/UnityProject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,19 @@ private extension UnityProject {
}

guard fileManager.createFile(
atPath: editorFilePath.appending("ProjectScript.cs"),
atPath: editorFilePath.appending("XcodeProjectRefresher.cs"),
contents: File.unityProjectScriptFile(projectName: projectName, iOSProjectPath: config.iOS.projectPath),
attributes: nil) else {
return .failure(UBKitError.unableToCreateFile("Unity Project Script"))
}

guard fileManager.createFile(
atPath: editorFilePath.appending("XcodeFrameworks.cs"),
contents: File.unityFrameworksScriptFile(projectName: projectName, iOSProjectPath: config.iOS.projectPath),
attributes: nil) else {
return .failure(UBKitError.unableToCreateFile("Xcode Frameworks Script"))
}

return .success
}

Expand Down
6 changes: 6 additions & 0 deletions UnityBuildKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
D4C15EC42023E6C3002B8938 /* UnityFrameworksScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C15EC32023E6C3002B8938 /* UnityFrameworksScript.swift */; };
D4C15EC52023E6C3002B8938 /* UnityFrameworksScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C15EC32023E6C3002B8938 /* UnityFrameworksScript.swift */; };
OBJ_204 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; };
OBJ_210 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_78 /* Package.swift */; };
OBJ_216 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_134 /* Package.swift */; };
Expand Down Expand Up @@ -428,6 +430,7 @@

/* Begin PBXFileReference section */
"AEXML::AEXML::Product" /* AEXML.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = AEXML.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D4C15EC32023E6C3002B8938 /* UnityFrameworksScript.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnityFrameworksScript.swift; sourceTree = "<group>"; };
"JSONUtilities::JSONUtilities::Product" /* JSONUtilities.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = JSONUtilities.framework; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_100 /* PBXHeadersBuildPhase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PBXHeadersBuildPhase.swift; sourceTree = "<group>"; };
OBJ_101 /* PBXLegacyTarget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PBXLegacyTarget.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -714,6 +717,7 @@
children = (
OBJ_14 /* UnityEditorBuildScript.swift */,
OBJ_15 /* UnityProjectScript.swift */,
D4C15EC32023E6C3002B8938 /* UnityFrameworksScript.swift */,
);
path = Unity;
sourceTree = "<group>";
Expand Down Expand Up @@ -1448,6 +1452,7 @@
buildActionMask = 0;
files = (
OBJ_204 /* Package.swift in Sources */,
D4C15EC42023E6C3002B8938 /* UnityFrameworksScript.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1526,6 +1531,7 @@
OBJ_291 /* SpecFile.swift in Sources */,
OBJ_292 /* ViewControllerFile.swift in Sources */,
OBJ_293 /* Config.swift in Sources */,
D4C15EC52023E6C3002B8938 /* UnityFrameworksScript.swift in Sources */,
OBJ_294 /* Result.swift in Sources */,
OBJ_295 /* Shell.swift in Sources */,
OBJ_296 /* UBKitError.swift in Sources */,
Expand Down

0 comments on commit 732d214

Please sign in to comment.