From 624dc698345c4fa04f8d8a7dadb9f7fb05aec0ec Mon Sep 17 00:00:00 2001 From: Wu-Star <200423380@163.com> Date: Fri, 17 Feb 2017 15:35:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=BA=94=E7=94=A8=E5=90=8D=E7=A7=B0=E5=9B=BD?= =?UTF-8?q?=E9=99=85=E5=8C=96=20Based=20configuration=20to=20realize=20the?= =?UTF-8?q?=20AppName=20I18N.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit { "i18n_name": [ "en:English","zh-Hans:简体中文","zh-Hant:繁体中文”],//“Language:AppName” } --- Mods/I18NAppName.projmods | 12 ++++++ PBX Editor/PBXObject.cs | 25 ++++++++---- PBX Editor/PBXParser.cs | 32 +++++++++------ PBX Editor/PBXVariantGroup.cs | 0 Plist/Plist.cs | 0 XCMod.cs | 6 +++ XCPlist.cs | 11 ++++-- XCProject.cs | 73 +++++++++++++++++++++++++++++++++-- XCodePostProcess.cs | 16 +++++--- 9 files changed, 142 insertions(+), 33 deletions(-) create mode 100644 Mods/I18NAppName.projmods mode change 100644 => 100755 PBX Editor/PBXVariantGroup.cs mode change 100644 => 100755 Plist/Plist.cs mode change 100644 => 100755 XCPlist.cs diff --git a/Mods/I18NAppName.projmods b/Mods/I18NAppName.projmods new file mode 100644 index 0000000..cd219a9 --- /dev/null +++ b/Mods/I18NAppName.projmods @@ -0,0 +1,12 @@ +{ + "group": "", + "i18n_name": [], + "libs": [], + "frameworks": [], + "headerpaths": [], + "files": [], + "folders": [], + "excludes": ["^.*.meta$", "^.*.mdown$", "^.*.pdf$"], + "compiler_flags": [], + "linker_flags": [] +} \ No newline at end of file diff --git a/PBX Editor/PBXObject.cs b/PBX Editor/PBXObject.cs index 0d85ca2..4f6fe13 100755 --- a/PBX Editor/PBXObject.cs +++ b/PBX Editor/PBXObject.cs @@ -136,12 +136,21 @@ public PBXContainerItemProxy( string guid, PBXDictionary dictionary ) : base( gu } } - public class PBXReferenceProxy : PBXObject - { - public PBXReferenceProxy() : base() { - } - - public PBXReferenceProxy( string guid, PBXDictionary dictionary ) : base( guid, dictionary ) { - } - } + public class PBXReferenceProxy : PBXObject + { + public PBXReferenceProxy() : base() { + } + + public PBXReferenceProxy( string guid, PBXDictionary dictionary ) : base( guid, dictionary ) { + } + } + + public class PBXTargetDependency : PBXObject + { + public PBXTargetDependency() : base() { + } + + public PBXTargetDependency( string guid, PBXDictionary dictionary ) : base( guid, dictionary ) { + } + } } diff --git a/PBX Editor/PBXParser.cs b/PBX Editor/PBXParser.cs index 9e1b185..20c3424 100755 --- a/PBX Editor/PBXParser.cs +++ b/PBX Editor/PBXParser.cs @@ -104,18 +104,26 @@ public string ResolveName( string guid ) return null; } - else if( entity is PBXNativeTarget ) - { - PBXNativeTarget obj = (PBXNativeTarget)entity; - //Debug.LogWarning ("PBXNativeTarget " + guid + " " + obj.ToString()); - - if( obj.data.ContainsKey( "name" ) ) { - //Debug.Log ("PBXNativeTarget " + (string)obj.data[ "name" ] + " " + guid); - return (string)obj.data[ "name" ]; - } - - return null; - } + else if( entity is PBXNativeTarget ) + { + PBXNativeTarget obj = (PBXNativeTarget)entity; + //Debug.LogWarning ("PBXNativeTarget " + guid + " " + obj.ToString()); + + if( obj.data.ContainsKey( "name" ) ) { + //Debug.Log ("PBXNativeTarget " + (string)obj.data[ "name" ] + " " + guid); + return (string)obj.data[ "name" ]; + } + + return null; + } + else if( entity is PBXContainerItemProxy ) + { + return "PBXContainerItemProxy"; + } + else if( entity is PBXTargetDependency ) + { + return "PBXTargetDependency"; + } else if( entity is XCBuildConfiguration ) { XCBuildConfiguration obj = (XCBuildConfiguration)entity; diff --git a/PBX Editor/PBXVariantGroup.cs b/PBX Editor/PBXVariantGroup.cs old mode 100644 new mode 100755 diff --git a/Plist/Plist.cs b/Plist/Plist.cs old mode 100644 new mode 100755 diff --git a/XCMod.cs b/XCMod.cs index 8652421..ad4ccb8 100755 --- a/XCMod.cs +++ b/XCMod.cs @@ -38,6 +38,12 @@ public ArrayList libs { return _libs; } } + + public ArrayList i18n_name { + get { + return (ArrayList)_datastore["i18n_name"]; + } + } public ArrayList frameworks { get { diff --git a/XCPlist.cs b/XCPlist.cs old mode 100644 new mode 100755 index d3ff503..5420766 --- a/XCPlist.cs +++ b/XCPlist.cs @@ -31,10 +31,13 @@ public XCPlist(string plistPath) public void Process(Hashtable plist) { Dictionary dict = (Dictionary)PlistCS.Plist.readPlist(plistPath); - foreach( DictionaryEntry entry in plist) - { - this.AddPlistItems((string)entry.Key, entry.Value, dict); - } + if(plist != null) + { + foreach( DictionaryEntry entry in plist) + { + this.AddPlistItems((string)entry.Key, entry.Value, dict); + } + } if (plistModified) { PlistCS.Plist.writeXml(dict, plistPath); diff --git a/XCProject.cs b/XCProject.cs index 256de11..6db39e4 100755 --- a/XCProject.cs +++ b/XCProject.cs @@ -30,7 +30,9 @@ public partial class XCProject : System.IDisposable private PBXSortedDictionary _buildFiles; private PBXSortedDictionary _groups; private PBXSortedDictionary _fileReferences; - private PBXDictionary _nativeTargets; + private PBXDictionary _nativeTargets; + private PBXDictionary _containerItemProxy; + private PBXDictionary _targetDependency; private PBXDictionary _frameworkBuildPhases; private PBXDictionary _resourcesBuildPhases; @@ -236,6 +238,24 @@ public PBXDictionary copyBuildPhases { } } + public PBXDictionary containerItemProxy { + get { + if( _containerItemProxy == null ) { + _containerItemProxy = new PBXDictionary( _objects ); + } + return _containerItemProxy; + } + } + + public PBXDictionary targetDependency { + get { + if( _targetDependency == null ) { + _targetDependency = new PBXDictionary( _objects ); + } + return _targetDependency; + } + } + #endregion @@ -334,7 +354,7 @@ public object GetObject( string guid ) return _objects[guid]; } - public PBXDictionary AddFile( string filePath, PBXGroup parent = null, string tree = "SOURCE_ROOT", bool createBuildFiles = true, bool weak = false ) + public PBXDictionary AddFile( string filePath, PBXGroup parent = null, string tree = "SOURCE_ROOT", bool createBuildFiles = true, bool weak = false, bool IsI18NName = false ) { //Debug.Log("AddFile " + filePath + ", " + parent + ", " + tree + ", " + (createBuildFiles? "TRUE":"FALSE") + ", " + (weak? "TRUE":"FALSE") ); @@ -372,7 +392,10 @@ public PBXDictionary AddFile( string filePath, PBXGroup parent = null, string tr if( parent == null ) { parent = _rootGroup; } - + if(IsI18NName) + { + filePath = absPath.Substring( absPath.LastIndexOf("/", absPath.IndexOf(".lproj")) + 1 ); + } //Check if there is already a file PBXFileReference fileReference = GetFile( System.IO.Path.GetFileName( filePath ) ); if( fileReference != null ) { @@ -381,6 +404,7 @@ public PBXDictionary AddFile( string filePath, PBXGroup parent = null, string tr } fileReference = new PBXFileReference( filePath, (TreeEnum)System.Enum.Parse( typeof(TreeEnum), tree ) ); + parent.AddChild( fileReference ); fileReferences.Add( fileReference ); results.Add( fileReference.guid, fileReference ); @@ -779,6 +803,45 @@ public void ApplyMod( XCMod mod ) XCPlist plist = new XCPlist (plistPath); plist.Process(mod.plist); + Debug.Log ("Adding I18N name..."); + if(mod.i18n_name != null && mod.i18n_name.Count > 0){ + Dictionary dict = (Dictionary)PlistCS.Plist.readPlist(plistPath); + if(dict.ContainsKey("CFBundleDisplayName")) + { + dict.Remove("CFBundleDisplayName"); + } + dict["LSHasLocalizedDisplayName"] = true; + PlistCS.Plist.writeXml(dict, plistPath); + + var variant = new PBXVariantGroup("InfoPlist.strings", null, "GROUP"); + // mark variants + variantGroups.Add(variant); + // add variant to project + _rootGroup.AddChild(variant); + + foreach (string entry in mod.i18n_name) + { + string[] filename = entry.Split( ':' ); + string folder = this.projectRootPath + "/" + filename[0] + ".lproj"; + if (!Directory.Exists(folder)) + { + Directory.CreateDirectory(folder); + } + string filePath = folder + "/InfoPlist.strings"; + string content = "\"CFBundleDisplayName\" = \"" + filename[1] + "\";\n"; + content += "\"CFBundleName\" = \"" + filename[1] + "\";\n"; + File.WriteAllText(filePath, content); + + var result = AddFile( filePath, variant, "GROUP", true, false, true ); + foreach (var item in result.Keys) + { + PBXFileReference fileReference = (PBXFileReference)result[item]; + fileReference.Remove("name"); + fileReference.Add("name",filename[1]); + } + } + } + this.Consolidate(); } @@ -793,7 +856,9 @@ public void Consolidate() consolidated.Append( this.fileReferences );//sort! consolidated.Append( this.frameworkBuildPhases ); consolidated.Append( this.groups );//sort! - consolidated.Append( this.nativeTargets ); + consolidated.Append( this.nativeTargets ); + consolidated.Append( this.containerItemProxy ); + consolidated.Append( this.targetDependency ); consolidated.Add( project.guid, project.data );//TODO this should be named PBXProject? consolidated.Append( this.resourcesBuildPhases ); consolidated.Append( this.shellScriptBuildPhases ); diff --git a/XCodePostProcess.cs b/XCodePostProcess.cs index f962014..51b7f0f 100755 --- a/XCodePostProcess.cs +++ b/XCodePostProcess.cs @@ -13,11 +13,17 @@ public static class XCodePostProcess [PostProcessBuild(999)] public static void OnPostProcessBuild( BuildTarget target, string pathToBuiltProject ) { - if (target != BuildTarget.iPhone) { - Debug.LogWarning("Target is not iPhone. XCodePostProcess will not run"); - return; - } - + #if UNITY_5 + if (target != BuildTarget.iOS) { + Debug.LogWarning("Target is not iPhone. XCodePostProcess will not run"); + return; + } + #else + if (target != BuildTarget.iPhone) { + Debug.LogWarning("Target is not iPhone. XCodePostProcess will not run"); + return; + } + #endif // Create a new project object from build target XCProject project = new XCProject( pathToBuiltProject ); From 1148365dc04308d23313d2ba04195a5eabfaa503 Mon Sep 17 00:00:00 2001 From: Wu-Star <200423380@163.com> Date: Fri, 17 Feb 2017 16:16:37 +0800 Subject: [PATCH 2/2] Update Readme.mdown --- Readme.mdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Readme.mdown b/Readme.mdown index 0bc0e41..5170d11 100755 --- a/Readme.mdown +++ b/Readme.mdown @@ -17,6 +17,7 @@ After the Unity's building phase, OnPostProcessBuild in XCodePostProcess.cs will In these setting files, specify the fields using a json pattern. * group:The group name in Xcode to which files and folders will added by this projmods file +* i18n_name:The application name internationalization, format for “language : application name” * libs:The name of libs should be added in Xcode Build Phases, libz.dylib for example. If you want to import a .a lib to Xcode, add it as a file(in "files") * frameworks:The name of framework should be added in Xcode Build Phases, Security.framework for example. If you want to add a third party framework, add it using a relative path to `files` section instead of here. * headerpaths:Header Search Paths in Build Setting of Xcode @@ -36,6 +37,7 @@ One example, the following file will add all files in /iOS/KKKeychain to the Xco { "group": "KKKeychain", "libs": [], + "i18n_name": [ "en:English","zh-Hans:简体中文","zh-Hant:繁体中文”], "frameworks": ["Security.framework"], "headerpaths": [], "files": [],