public static UnitySolution Parse(string unitypath) { //check unity project; if (string.IsNullOrEmpty(unitypath)) { return(null); } DirectoryInfo dirinfo = new DirectoryInfo(unitypath); if (!dirinfo.Exists) { return(null); } var assetpath = Path.Combine(dirinfo.FullName, "Assets"); if (!Directory.Exists(assetpath)) { Console.WriteLine("Invalid Unity project path"); return(null); } var csprojPath = Path.Combine(dirinfo.FullName, "Assembly-CSharp.csproj"); var csprojPathEditor = Path.Combine(dirinfo.FullName, "Assembly-CSharp-Editor.csproj"); if (!File.Exists(csprojPath)) { Console.WriteLine("Missing Assembly-CSharp.csproj"); return(null); } if (!File.Exists(csprojPathEditor)) { Console.WriteLine("Missing Assembly-CSharp-Editor.csproj"); } var solution = new UnitySolution(); solution.AssemblyCSharp = UnityCSProject.Parse(csprojPath); solution.AssemblyCSharpEditor = UnityCSProject.Parse(csprojPathEditor); return(solution); }
public static UnityCSProject Parse(string path) { if (string.IsNullOrEmpty(path)) { return(null); } if (!path.ToLower().EndsWith(".csproj")) { return(null); } if (!File.Exists(path)) { return(null); } XElement xproject = XElement.Load(path); string xns = xproject.Name.NamespaceName; UnityCSProject csproj = new UnityCSProject(); try { //Compile files List <CompileItem> compileFiles = new List <CompileItem>(); List <ReferenceItem> referenceItems = new List <ReferenceItem>(); var itemgroups = xproject.Elements(XName.Get("ItemGroup", xns)); foreach (var itemgroup in itemgroups) { var compiles = itemgroup.Elements(XName.Get("Compile", xns)); foreach (var compile in compiles) { var compilePath = compile.Attribute(XName.Get("Include")); if (compilePath == null) { continue; } compileFiles.Add(new CompileItem(compilePath.Value)); } var references = itemgroup.Elements(XName.Get("Reference", xns)); foreach (var reference in references) { var attrName = reference.Attribute(XName.Get("Include"))?.Value; var hintPath = reference.Element(XName.Get("HintPath", xns))?.Value; referenceItems.Add(new ReferenceItem(attrName, hintPath)); } } csproj.CompileItems = compileFiles; csproj.ReferenceItems = referenceItems; //PropettyGroup var propertyGroups = xproject.Elements(XName.Get("PropertyGroup", xns)); foreach (var propertyGroup in propertyGroups) { var condition = propertyGroup.Attribute(XName.Get("Condition")); if (condition == null) { //parse main config csproj.AssemblyName = propertyGroup.Element(XName.Get(nameof(AssemblyName), xns))?.Value; csproj.BaseDirectory = propertyGroup.Element(XName.Get(nameof(BaseDirectory), xns))?.Value; csproj.FileAlignment = propertyGroup.Element(XName.Get(nameof(FileAlignment), xns))?.Value; csproj.OutputType = propertyGroup.Element(XName.Get(nameof(OutputType), xns))?.Value; csproj.ProductVersion = propertyGroup.Element(XName.Get(nameof(ProductVersion), xns))?.Value; csproj.ProjectGuid = propertyGroup.Element(XName.Get(nameof(ProjectGuid), xns))?.Value; csproj.SchemaVersion = propertyGroup.Element(XName.Get(nameof(SchemaVersion), xns))?.Value; csproj.TargetFrameworkVersion = propertyGroup.Element(XName.Get(nameof(TargetFrameworkVersion), xns))?.Value; } else { var conditionDesc = condition.Value; if (conditionDesc.Contains("Debug")) { csproj.PropertyDebug = new PropertyGroup(propertyGroup, xns); } else if (conditionDesc.Contains("Release")) { csproj.PropertyRelease = new PropertyGroup(propertyGroup, xns); } } } } catch (Exception e) { Console.WriteLine(e.Message + "\n" + e.StackTrace); } return(csproj); }