ILSpy中baml转化为xaml的改进(三)

算是和ILSpy干上了。

目前做了两步:

1) 能保存xaml了;

2)能批量保存了;

但是,工程文件没有改变,所以,装载项目后,那些xaml和cs文件,各在各的地方。难用极了。


每天都提醒自己是中国人,象现在学习的这个项目,人家国外是几十人,三年多来做到这个样子。这里呢,现在就一个人,一个月。

所以,只能学习。而且,要付出巨大的努力。


还是要工具。工具不好使,还得改。


今天状态奇差,所以不得不加班才改完,上午来,调试了一会,竟然睡着了。满腹愁肠瞌睡多啊。

不过,也说明c#相比较c,调试时压力的确小一些。


说正事。


第一件事,就是找在哪里下手。

由于ILSpy也是本周刚接触,TMD找好一个多小时,而最可气的,那代码就在我看的那个函数的上面停了一个多小时。

不过,找到了:

就在:ILSpy\Languages\CSharpLanguage.cs

中的WriteProjectFile中


这个函数挺老长。

就不废话了。只把改过的部分传上来:


在这段后面:


				w.WriteStartElement("ItemGroup"); // References
				foreach (AssemblyNameReference r in module.AssemblyReferences) {
					if (r.Name != "mscorlib") {
						w.WriteStartElement("Reference");
						w.WriteAttributeString("Include", r.Name);
						w.WriteEndElement();
					}
				}
				w.WriteEndElement(); // </ItemGroup> (References)

改成这个样子:


				foreach (IGrouping<string, string> gr in (from f in files group f.Item2 by f.Item1 into g orderby g.Key select g)) {
					w.WriteStartElement("ItemGroup");

                    bool isCsfile = false;
                    if (gr.Key.Equals("Compile"))
                    {
                        isCsfile = true;
                    }

					foreach (string file in gr.OrderBy(f => f, StringComparer.OrdinalIgnoreCase)) {

						w.WriteStartElement(gr.Key);


                        string strXamlFileName = "";
                        string xamlPath= "";
                        if (isCsfile && findXamlAddBy_haoyujie(files, file,ref strXamlFileName,ref  xamlPath) )
                        {

          
                            // 列表中的原始文件全路径名
                            string oldStr = Path.Combine(options.SaveAsProjectDirectory, file);

                            // 新文件名,无扩展名 
                            string newCsfilePath = xamlPath + Path.GetFileNameWithoutExtension(file);
                            //加上新的扩展名
                            string purcsFilename = newCsfilePath + ".xaml.cs";

                            //改名方法
                            {
                                string newStr = Path.Combine(options.SaveAsProjectDirectory, purcsFilename);
                                FileInfo fi = new FileInfo(oldStr);
                                if (File.Exists(newStr))
                                {
                                    File.Delete(newStr);
                                }
                                fi.MoveTo(newStr);
                            }

                            
                            {
                                //把xaml文件名字,大小写改得与cs文件一样
                                
                                //string newStrXaml = Path.Combine(options.SaveAsProjectDirectory, xamlPath + strXamlFileName) + ".xaml";
                                 改名方法
                                //FileInfo fiXaml = new FileInfo(newStrXaml);
                                //string tmpFile = newStrXaml + "555";
                                //if (File.Exists(tmpFile))
                                //{
                                //    File.Delete(tmpFile);
                                //}
                                //fiXaml.MoveTo(Path.Combine(tmpFile));

                                 改名方法
                                //FileInfo fiXaml2 = new FileInfo(tmpFile);
                                //fiXaml2.MoveTo(Path.Combine(newStrXaml));
                                //File.Delete(tmpFile);

                            }

                            w.WriteAttributeString("Include", purcsFilename);

                            w.WriteStartElement("DependentUpon");
                            w.WriteString(strXamlFileName + ".xaml");
                            w.WriteEndElement();
                            w.WriteStartElement("SubType");
                            w.WriteString("Code");
                            w.WriteEndElement();

                            strXamlFileName = "";
                            xamlPath = "";
                          
                        }else
                        {
						    w.WriteAttributeString("Include", file);
                        }
						w.WriteEndElement();
					}
					w.WriteEndElement();
				}

注意,上面的函数,参数是被改过的:

加了一个options, 原因是我们需要知道用户选择的保存目录。

void WriteProjectFile(TextWriter writer, IEnumerable<Tuple<string, string>> files, ModuleDefinition module, DecompilationOptions options)


加了两个自定义函数

        //得到一个路径+文件名中的路径部分,因为系统自带的函数中,没有找到
        static public string GetFolderWithX(string strCur)
        {
            string xamlPath = "";
            if (strCur.IndexOf(@"\") > 0) //如果xaml有目录
            {
                xamlPath = strCur.Substring(0, strCur.LastIndexOf(@"\") + 1);


            }
            else if (strCur.IndexOf(@"/") > 0) //如果xaml有目录
            {
                xamlPath = strCur.Substring(0, strCur.LastIndexOf(@"/") + 1);

            }
            else
            {
                xamlPath = "";
            }
            return xamlPath;
        }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="files"></param>
    /// <param name="strcsFileName">当前需进行判断的cs文件</param>
    /// <param name="strXamlFileName">新的xaml的名称,因为要改大小写</param>
    /// <param name="xamlPath">新的xaml的路径</param>
    /// <returns></returns>
        bool findXamlAddBy_haoyujie( IEnumerable<Tuple<string, string>> files,string strcsFileName,ref string strXamlFileName,ref string xamlPath)
        {
            foreach (IGrouping<string, string> gr in (from f in files group f.Item2 by f.Item1 into g orderby g.Key select g))
            {
                string strGroupName = gr.Key;   //得到分组
                
                if (strGroupName.Equals("Page"))    //得到xaml那个组
                {
                    foreach (string fileXaml in gr.OrderBy(f => f, StringComparer.OrdinalIgnoreCase))
                    {                        

                        string purXamlFilename = Path.GetFileNameWithoutExtension(fileXaml);
                        string tmpCSFilepath = Path.GetFileNameWithoutExtension(strcsFileName);

                        如果在c#文件路径中,有strCur,则返回真                       
                        if (tmpCSFilepath.Equals(purXamlFilename, StringComparison.OrdinalIgnoreCase))
                        {
                            //这里是唯一的改变,也就是说,xaml的名字,从cs 路径中得到strcsFileName
                            strXamlFileName = Path.GetFileNameWithoutExtension(strcsFileName);
                            xamlPath = GetFolderWithX(fileXaml);
                            
                            return true;
                        }
                    }
                }
            }
            return false;
        }


今天脑子极其混乱,周四了,连上5天班了,也能理解。而且,这个破地方,在反编译完所有的文件之后,大大提高了调试的难度。直到建了一个小工程,在那个小工程里调完,才算是今天能做完。


代码和可执行文件,以后再传上来。

要回家了。有一段代码被我注掉了,不太对路。因为xaml文件的名字,原本想改成与cs文件一样的骆驼式。现在应当是对了。但以后再调吧。


今天这断代码改的,没什么水准。应当用xaml的组进行遍历,好象搞反了,当然,也能用,唯一的问题,在.csproj中,没的把xaml的文件改成骆驼式,不太美观,也不耽误用。

但这个ILSpy的确不是当前本人工作的重点,明天还得接着看解出来的信息,向老外学习吧。


可执行文件

代码


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值