From f6489b19ebd31693d80a9f7a8c1b3835d280e8bb Mon Sep 17 00:00:00 2001 From: lihailuo Date: Fri, 3 Jul 2020 14:39:27 +0800 Subject: [PATCH 1/6] fix: plist exception && save project exception --- XCPlist.cs | 7 ++++++- XCProject.cs | 11 ++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/XCPlist.cs b/XCPlist.cs index d3ff503..fec5903 100644 --- a/XCPlist.cs +++ b/XCPlist.cs @@ -58,11 +58,16 @@ public void AddPlistItems(string key, object value, Dictionary d { processUrlTypes((ArrayList)value, dict); } - else + else if ( value is Hashtable) { dict[key] = HashtableToDictionary((Hashtable)value); plistModified = true; } + else + { + dict[key] = value; + plistModified = true; + } } private void processUrlTypes(ArrayList urltypes, Dictionary dict) diff --git a/XCProject.cs b/XCProject.cs index 256de11..416a80d 100755 --- a/XCProject.cs +++ b/XCProject.cs @@ -81,8 +81,13 @@ public XCProject( string filePath ) : this() this.filePath = projects[ 0 ]; } - projectFileInfo = new FileInfo( Path.Combine( this.filePath, "project.pbxproj" ) ); - string contents = projectFileInfo.OpenText().ReadToEnd(); + projectFileInfo = new FileInfo( Path.Combine( this.filePath, "project.pbxproj" ) ); + //string contents = projectFileInfo.OpenText().ReadToEnd; + string contents; + using( StreamReader r = projectFileInfo.OpenText() ) + { + contents = r.ReadToEnd(); + } PBXParser parser = new PBXParser(); _datastore = parser.Decode( contents ); @@ -850,7 +855,7 @@ public void Save() string projectPath = Path.Combine( this.filePath, "project.pbxproj" ); // Delete old project file, in case of an IOException 'Sharing violation on path Error' - DeleteExisting(projectPath); + DeleteExisting(projectPath); // Parse result object directly into file CreateNewProject(result,projectPath); From c4502e9a942c9dbde8fcb8eedeaf17f3055e9532 Mon Sep 17 00:00:00 2001 From: lihailuo Date: Fri, 3 Jul 2020 18:33:55 +0800 Subject: [PATCH 2/6] fix: set plist forlder parent path from mod.path if not exist in Application.dataPath --- XCProject.cs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/XCProject.cs b/XCProject.cs index 416a80d..3ef20ce 100755 --- a/XCProject.cs +++ b/XCProject.cs @@ -752,8 +752,12 @@ public void ApplyMod( XCMod mod ) } Debug.Log( "Adding folders..." ); - foreach( string folderPath in mod.folders ) { - string absoluteFolderPath = System.IO.Path.Combine( Application.dataPath, folderPath ); + foreach( string folderPath in mod.folders ) { + string absoluteFolderPath = System.IO.Path.Combine(Application.dataPath, folderPath); + if ( !Directory.Exists(folderPath) ) + { + absoluteFolderPath = System.IO.Path.Combine(mod.path, folderPath); + } Debug.Log ("Adding folder " + absoluteFolderPath); this.AddFolder( absoluteFolderPath, modGroup, (string[])mod.excludes.ToArray( typeof(string) ) ); } @@ -777,15 +781,19 @@ public void ApplyMod( XCMod mod ) Debug.Log( "Adding linker flags..." ); foreach( string flag in mod.linker_flags ) { this.AddOtherLinkerFlags( flag ); - } - - Debug.Log ("Adding plist items..."); - string plistPath = this.projectRootPath + "/Info.plist"; - XCPlist plist = new XCPlist (plistPath); - plist.Process(mod.plist); - + } + + this.ProcessPList(mod.plist); this.Consolidate(); } + + public void ProcessPList( Hashtable items ) + { + Debug.Log("Adding plist items..."); + string plistPath = this.projectRootPath + "/Info.plist"; + XCPlist plist = new XCPlist(plistPath); + plist.Process(items); + } #endregion From 63326a06c55c93ef1222468091741aca139fd974 Mon Sep 17 00:00:00 2001 From: lihailuo Date: Wed, 8 Jul 2020 11:09:40 +0800 Subject: [PATCH 3/6] feat: add text modify and settings && fix: buildSetting string type --- XCBuildConfiguration.cs | 61 +++++++++++++++------- XCMod.cs | 109 ++++++++++++++++++++++++++++++++++++---- XCProject.cs | 88 +++++++++++++++++++++++++++++++- XClass.cs | 104 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 330 insertions(+), 32 deletions(-) create mode 100644 XClass.cs diff --git a/XCBuildConfiguration.cs b/XCBuildConfiguration.cs index deca253..40bc09d 100755 --- a/XCBuildConfiguration.cs +++ b/XCBuildConfiguration.cs @@ -175,26 +175,49 @@ public bool overwriteBuildSetting(string settingName, string settingValue) { if( !ContainsKey( BUILDSETTINGS_KEY ) ) { Debug.Log ("creating key " + BUILDSETTINGS_KEY); this.Add( BUILDSETTINGS_KEY, new PBXSortedDictionary() ); - } - - if( !((PBXDictionary)_data[BUILDSETTINGS_KEY]).ContainsKey( settingName ) ) { - Debug.Log("adding key " + settingName); - ((PBXDictionary)_data[BUILDSETTINGS_KEY]).Add( settingName, new PBXList() ); - } - else if ( ((PBXDictionary)_data[BUILDSETTINGS_KEY])[ settingName ] is string ) { - //Debug.Log("key is string:" + settingName); - //string tempString = (string)((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName]; - ((PBXDictionary)_data[BUILDSETTINGS_KEY])[ settingName ] = new PBXList(); - //((PBXList)((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName]).Add( tempString ); - } - - if( !((PBXList)((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName]).Contains( settingValue ) ) { - Debug.Log("setting " + settingName + " to " + settingValue); - ((PBXList)((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName]).Add( settingValue ); - modified = true; - } + } + + + //fix string type show is array + /* + if( !((PBXDictionary)_data[BUILDSETTINGS_KEY]).ContainsKey( settingName ) ) { + Debug.Log("adding key " + settingName); + ((PBXDictionary)_data[BUILDSETTINGS_KEY]).Add( settingName, new PBXList() ); + } + + else if ( ((PBXDictionary)_data[BUILDSETTINGS_KEY])[ settingName ] is string ) { + //Debug.Log("key is string:" + settingName); + //string tempString = (string)((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName]; + ((PBXDictionary)_data[BUILDSETTINGS_KEY])[ settingName ] = new PBXList(); + //((PBXList)((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName]).Add( tempString ); + } - return modified; + if( !((PBXList)((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName]).Contains( settingValue ) ) { + Debug.Log("setting " + settingName + " to " + settingValue); + ((PBXList)((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName]).Add( settingValue ); + modified = true; + } + */ + if (!((PBXDictionary)_data[BUILDSETTINGS_KEY]).ContainsKey(settingName)) + { + Debug.Log("adding key " + settingName); + ((PBXDictionary)_data[BUILDSETTINGS_KEY]).Add(settingName, new PBXList()); + ((PBXList)((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName]).Add(settingValue); + modified = true; + } + else if (((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName] is string) + { + ((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName] = settingValue; + modified = true; + } + else if (!((PBXList)((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName]).Contains(settingValue)) + { + Debug.Log("setting " + settingName + " to " + settingValue); + ((PBXList)((PBXDictionary)_data[BUILDSETTINGS_KEY])[settingName]).Add(settingValue); + modified = true; + } + + return modified; } } } \ No newline at end of file diff --git a/XCMod.cs b/XCMod.cs index 8652421..7cd6fb1 100755 --- a/XCMod.cs +++ b/XCMod.cs @@ -21,8 +21,14 @@ public string group { } public ArrayList patches { - get { - return (ArrayList)_datastore["patches"]; + get { + //return (ArrayList)_datastore["patches"]; + ArrayList p_ = (ArrayList)_datastore["patches"]; + if ( null == p_ ) + { + return new ArrayList(); + } + return p_; } } @@ -41,57 +47,138 @@ public ArrayList libs { public ArrayList frameworks { get { - return (ArrayList)_datastore["frameworks"]; + //return (ArrayList)_datastore["frameworks"]; + ArrayList p_ = (ArrayList)_datastore["frameworks"]; + if (null == p_) + { + return new ArrayList(); + } + return p_; } } public ArrayList headerpaths { get { - return (ArrayList)_datastore["headerpaths"]; + //return (ArrayList)_datastore["headerpaths"]; + ArrayList p_ = (ArrayList)_datastore["headerpaths"]; + if (null == p_) + { + return new ArrayList(); + } + return p_; } } public ArrayList files { get { - return (ArrayList)_datastore["files"]; + //return (ArrayList)_datastore["files"]; + ArrayList p_ = (ArrayList)_datastore["files"]; + if (null == p_) + { + return new ArrayList(); + } + return p_; } } public ArrayList folders { get { - return (ArrayList)_datastore["folders"]; + //return (ArrayList)_datastore["folders"]; + ArrayList p_ = (ArrayList)_datastore["folders"]; + if (null == p_) + { + return new ArrayList(); + } + return p_; } } public ArrayList excludes { get { - return (ArrayList)_datastore["excludes"]; + //return (ArrayList)_datastore["excludes"]; + ArrayList p_ = (ArrayList)_datastore["excludes"]; + if (null == p_) + { + return new ArrayList(); + } + return p_; } } public ArrayList compiler_flags { get { - return (ArrayList)_datastore["compiler_flags"]; + //return (ArrayList)_datastore["compiler_flags"]; + ArrayList p_ = (ArrayList)_datastore["compiler_flags"]; + if (null == p_) + { + return new ArrayList(); + } + return p_; } } public ArrayList linker_flags { get { - return (ArrayList)_datastore["linker_flags"]; + //return (ArrayList)_datastore["linker_flags"]; + ArrayList p_ = (ArrayList)_datastore["linker_flags"]; + if (null == p_) + { + return new ArrayList(); + } + return p_; } } public ArrayList embed_binaries { get { - return (ArrayList)_datastore["embed_binaries"]; + //return (ArrayList)_datastore["embed_binaries"]; + ArrayList p_ = (ArrayList)_datastore["embed_binaries"]; + if (null == p_) + { + return new ArrayList(); + } + return p_; } } public Hashtable plist { get { - return (Hashtable)_datastore["plist"]; + //return (Hashtable)_datastore["plist"]; + Hashtable p_ = (Hashtable)_datastore["plist"]; + if (null == p_) + { + return new Hashtable(); + } + return p_; } } + + public Hashtable settings + { + get { + //return (Hashtable)_datastore["settings"]; + Hashtable p_ = (Hashtable)_datastore["settings"]; + if (null == p_) + { + return new Hashtable(); + } + return p_; + } + } + + public Hashtable textModify + { + get + { + //return (Hashtable)_datastore["textModify"]; + Hashtable p_ = (Hashtable)_datastore["textModify"]; + if (null == p_) + { + return new Hashtable(); + } + return p_; + } + } public XCMod( string filename ) { diff --git a/XCProject.cs b/XCProject.cs index 3ef20ce..8b98833 100755 --- a/XCProject.cs +++ b/XCProject.cs @@ -3,7 +3,8 @@ using System.Collections; using System.Collections.Generic; using System.IO; -using System.Text.RegularExpressions; +using System.Text.RegularExpressions; +using System; namespace UnityEditor.XCodeEditor { @@ -783,7 +784,23 @@ public void ApplyMod( XCMod mod ) this.AddOtherLinkerFlags( flag ); } - this.ProcessPList(mod.plist); + Debug.Log("Adding settings ..."); + foreach (DictionaryEntry kv in mod.settings) + { + string k = (string)kv.Key; + string v = (string)kv.Value; + if ( k == null || k.Trim().Length == 0 || null == v) + { + Debug.Log( string.Format( "setting item error(k:{0} v:{1} mod:{2})", + k == null ? "[null]" : k, v == null ? "[null]" :v, + mod.group ) ); + continue; + } + + this.overwriteBuildSetting(k.Trim(), v.Trim()); + } + this.ProcessPList(mod.plist); + this.ProcessTextModify(mod.textModify); this.Consolidate(); } @@ -794,6 +811,73 @@ public void ProcessPList( Hashtable items ) XCPlist plist = new XCPlist(plistPath); plist.Process(items); } + + public void ProcessTextModify( Hashtable items ) + { + foreach (DictionaryEntry kv in items) + { + string k = (string)kv.Key; + Hashtable v = (Hashtable)kv.Value; + if (k == null || k.Trim().Length == 0 || null == v) + { + Debug.Log(string.Format("textmodify item error(k:{0})", + k == null ? "[null]" : k, + v == null ? 0 : v.Count) ); + continue; + } + + string filePath = Path.Combine(this.projectRootPath, k); + if ( File.Exists( filePath ) ) + { + XClass xclass = new XClass(filePath); + + ArrayList beforeList = (ArrayList)v["before"]; + ProcessWriteText("before", beforeList, xclass.WriteAbove); + + ArrayList afterList = (ArrayList)v["after"]; + ProcessWriteText("after", afterList, xclass.WriteBelow); + + ArrayList replaceList = (ArrayList)v["replace"]; + ProcessWriteText("replace", replaceList, xclass.Replace); + + string addText = (string)v["add"]; + if ( null != addText ) + { + xclass.Add(addText); + } + } + else + { + Debug.LogError( string.Format("text modify file '{0}' not found!!!", filePath) ); + } + } + } + + private void ProcessWriteText( string sectionName, ArrayList modifyList, Action writeAction ) + { + Debug.Log(string.Format("Process text modify {0} section(size:{1} count:{2})", + sectionName, + null != modifyList ? modifyList.Count : 0, + null != modifyList ? modifyList.Count / 2 : 0)); + + if (null != modifyList) + { + int count = modifyList.Count; + for (int i = 0; i < count - 1; i += 2) + { + string textMark = (string)modifyList[i]; + string textModify = (string)modifyList[i + 1]; + if ( null != textMark && null != textModify ) + { + writeAction(textMark, textModify); + } + else + { + Debug.LogError(string.Format("Process text modify {0} section error, mark or text is null", sectionName)); + } + } + } + } #endregion diff --git a/XClass.cs b/XClass.cs new file mode 100644 index 0000000..87efea7 --- /dev/null +++ b/XClass.cs @@ -0,0 +1,104 @@ +using UnityEngine; +using System.Collections; +using System.IO; + +namespace UnityEditor.XCodeEditor +{ + public class XClass : System.IDisposable + { + private string filePath; + + public XClass(string fPath) + { + filePath = fPath; + if (!System.IO.File.Exists(filePath)) + { + Debug.LogError(filePath + " not found!!!"); + + return; + } + } + + + public void WriteAbove( string below, string text ) + { + StreamReader streamReader = new StreamReader(filePath); + string text_all = streamReader.ReadToEnd(); + streamReader.Close(); + + int beginIndex = text_all.IndexOf(below); + if (beginIndex == -1) + { + Debug.LogError(below + " not in " + filePath); + + return; + } + + text_all = text_all.Substring(0, beginIndex) + "\n" + text + "\n" + text_all.Substring(beginIndex); + + StreamWriter streamWriter = new StreamWriter(filePath); + streamWriter.Write(text_all); + streamWriter.Close(); + } + + public void WriteBelow(string below, string text) + { + StreamReader streamReader = new StreamReader(filePath); + string text_all = streamReader.ReadToEnd(); + streamReader.Close(); + + int beginIndex = text_all.IndexOf(below); + if (beginIndex == -1) + { + Debug.LogError(below + " not in " + filePath); + + return; + } + + int endIndex = text_all.LastIndexOf("\n", beginIndex + below.Length); + + text_all = text_all.Substring(0, endIndex) + "\n" + text + "\n" + text_all.Substring(endIndex); + + StreamWriter streamWriter = new StreamWriter(filePath); + streamWriter.Write(text_all); + streamWriter.Close(); + } + + public void Replace(string below, string newText) + { + StreamReader streamReader = new StreamReader(filePath); + string text_all = streamReader.ReadToEnd(); + streamReader.Close(); + + int beginIndex = text_all.IndexOf(below); + + if (beginIndex == -1) + { + Debug.LogError(below + " not in " + filePath); + return; + } + + text_all = text_all.Replace(below, newText); + StreamWriter streamWriter = new StreamWriter(filePath); + streamWriter.Write(text_all); + streamWriter.Close(); + } + + public void Add(string text) + { + StreamReader streamReader = new StreamReader(filePath); + string text_all = streamReader.ReadToEnd(); + streamReader.Close(); + + text_all = text_all + "\n" + text; + StreamWriter streamWriter = new StreamWriter(filePath); + streamWriter.Write(text_all); + streamWriter.Close(); + } + + public void Dispose() + { + + } + } +} From fb8f603b248c1cbdcdc5f045bd595931f0a17ccf Mon Sep 17 00:00:00 2001 From: lihailuo Date: Wed, 8 Jul 2020 16:45:03 +0800 Subject: [PATCH 4/6] fix: urltype crash --- XCPlist.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/XCPlist.cs b/XCPlist.cs index fec5903..2c7914f 100644 --- a/XCPlist.cs +++ b/XCPlist.cs @@ -124,12 +124,15 @@ private Dictionary findUrlTypeByName(List bundleUrlTypes return null; foreach(Dictionary dict in bundleUrlTypes) - { - string _n = (string)dict[BundleUrlName]; - if (string.Compare(_n, name) == 0) - { - return dict; - } + { + if (dict.ContainsKey(BundleUrlName)) + { + string _n = (string)dict[BundleUrlName]; + if (string.Compare(_n, name) == 0) + { + return dict; + } + } } return null; } From 88bce168afab9262cceaec891f4c606f4166cdba Mon Sep 17 00:00:00 2001 From: lihailuo Date: Wed, 8 Jul 2020 17:40:48 +0800 Subject: [PATCH 5/6] =?UTF-8?q?fix:=20=E4=B8=8D=E5=90=8C=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E4=B8=8B=E5=90=8C=E5=90=8D=E6=96=87=E4=BB=B6=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XCProject.cs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/XCProject.cs b/XCProject.cs index 8b98833..59eb643 100755 --- a/XCProject.cs +++ b/XCProject.cs @@ -380,8 +380,10 @@ public PBXDictionary AddFile( string filePath, PBXGroup parent = null, string tr } //Check if there is already a file - PBXFileReference fileReference = GetFile( System.IO.Path.GetFileName( filePath ) ); - if( fileReference != null ) { + //PBXFileReference fileReference = GetFile( System.IO.Path.GetFileName( filePath ) ); + PBXFileReference fileReference = GetFileByPath(filePath); + if ( fileReference != null ) + { Debug.Log("File already exists: " + filePath); //not a warning, because this is normal for most builds! return null; } @@ -504,8 +506,9 @@ public void AddEmbedFramework( string fileName) Debug.Log( "Add Embed Framework: " + fileName ); //Check if there is already a file - PBXFileReference fileReference = GetFile( System.IO.Path.GetFileName( fileName ) ); - if( fileReference == null ) { + //PBXFileReference fileReference = GetFile( System.IO.Path.GetFileName( fileName ) ); + PBXFileReference fileReference = GetFileByPath(fileName); + if( fileReference != null ) { Debug.Log("Embed Framework must added already: " + fileName); return; } @@ -675,6 +678,25 @@ public PBXFileReference GetFile( string name ) return null; } + + public PBXFileReference GetFileByPath( string path ) + { + if (string.IsNullOrEmpty(path)) + { + return null; + } + + foreach (KeyValuePair current in fileReferences) + { + if (!string.IsNullOrEmpty(current.Value.path) && current.Value.path.CompareTo(path) == 0) + { + return current.Value; + } + } + + return null; + } + public PBXGroup GetGroup( string name, string path = null, PBXGroup parent = null ) { if( string.IsNullOrEmpty( name ) ) From 1da7d7952612a26475317223b18cefa93bd3939e Mon Sep 17 00:00:00 2001 From: lihailuo Date: Thu, 9 Jul 2020 14:18:29 +0800 Subject: [PATCH 6/6] feat: enbed frameword conflict pod --- XCProject.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/XCProject.cs b/XCProject.cs index 59eb643..0588eee 100755 --- a/XCProject.cs +++ b/XCProject.cs @@ -508,7 +508,7 @@ public void AddEmbedFramework( string fileName) //Check if there is already a file //PBXFileReference fileReference = GetFile( System.IO.Path.GetFileName( fileName ) ); PBXFileReference fileReference = GetFileByPath(fileName); - if( fileReference != null ) { + if( fileReference == null ) { Debug.Log("Embed Framework must added already: " + fileName); return; } @@ -765,8 +765,9 @@ public void ApplyMod( XCMod mod ) if (mod.embed_binaries != null) { //1. Add LD_RUNPATH_SEARCH_PATHS for embed framework - this.overwriteBuildSetting("LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks", "Release"); - this.overwriteBuildSetting("LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks", "Debug"); + // CONFLIC TO POD METHOD + // this.overwriteBuildSetting("LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks", "Release"); + // this.overwriteBuildSetting("LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks", "Debug"); foreach( string binary in mod.embed_binaries ) { string absoluteFilePath = System.IO.Path.Combine( mod.path, binary );