Skip to content

Instantly share code, notes, and snippets.

@van800
Created May 30, 2018 20:33
Show Gist options
  • Save van800/8e0240a4dc67debbc8544d5755c4d406 to your computer and use it in GitHub Desktop.
Save van800/8e0240a4dc67debbc8544d5755c4d406 to your computer and use it in GitHub Desktop.
Update Unity generated csproj files to PS4Player BuildTarget
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using JetBrains.Rider.Unity.Editor;
using UnityEditor;
using UnityEngine;
namespace Editor
{
public class CsprojAssetPostprocessor : AssetPostprocessor
{
internal static string[] GetCsprojLinesInSln()
{
var projectDirectory = Directory.GetParent(Application.dataPath).FullName;
var projectName = Path.GetFileName(projectDirectory);
var slnFile = Path.GetFullPath($"{projectName}.sln");
if (!File.Exists(slnFile))
return new string[0];
var slnAllText = File.ReadAllText(slnFile);
var lines = slnAllText.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
.Where(a => a.StartsWith("Project(")).ToArray();
return lines;
}
public static void OnGeneratedCSProjectFiles()
{
if (!PluginEntryPoint.Enabled)
return;
try
{
// get only csproj files, which are mentioned in sln
var lines = GetCsprojLinesInSln();
var currentDirectory = Directory.GetCurrentDirectory();
var projectFiles = Directory.GetFiles(currentDirectory, "*.csproj")
.Where(csprojFile => lines.Any(line => line.Contains("\"" + Path.GetFileName(csprojFile) + "\""))).ToArray();
foreach (var file in projectFiles)
{
UpgradeProjectFile(file);
}
}
catch (Exception e)
{
// unhandled exception kills editor
Debug.LogError(e);
}
}
private static void UpgradeProjectFile(string projectFile)
{
XDocument doc;
try
{
doc = XDocument.Load(projectFile);
}
catch (Exception)
{
return;
}
var projectContentElement = doc.Root;
XNamespace xmlns = projectContentElement.Name.NamespaceName; // do not use var
if (UnityUtils.UnityVersion < new Version(2018, 0))
FixUnityEngineReference(projectContentElement, xmlns);
doc.Save(projectFile);
}
private static void FixUnityEngineReference(XElement projectContentElement, XNamespace xmlns)
{
//var group = EditorUserBuildSettings.activeBuildTargetGroup;
var target = EditorUserBuildSettings.activeBuildTarget;
string playbackEngineFolders = Path.Combine(Path.Combine(Directory.GetParent(EditorApplication.applicationPath).ToString(), "Data"), "PlaybackEngines");
var unityEngineDir = new DirectoryInfo(Path.Combine(Path.Combine(playbackEngineFolders, "PS4Player"), "Managed"));//windowsstandalonesupport
if (!unityEngineDir.Exists)
return;
var newDllPath = Path.Combine(unityEngineDir.FullName, "UnityEngine.dll");
if (!File.Exists(newDllPath))
return;
projectContentElement
.Elements(xmlns + "ItemGroup")
.Elements(xmlns + "Reference")
.Where(a => a.Attribute("Include") != null && a.Attribute("Include").Value.StartsWith("UnityEngine.")).Remove();
var files = unityEngineDir.GetFiles("*.dll");
foreach (var file in files)
{
var itemGroup = new XElement(xmlns + "ItemGroup");
var reference = new XElement(xmlns + "Reference");
reference.Add(new XAttribute("Include", Path.GetFileNameWithoutExtension(file.Name)));
reference.Add(new XElement(xmlns + "HintPath", file.FullName));
itemGroup.Add(reference);
projectContentElement.Add(itemGroup);
}
}
}
}
@van800
Copy link
Author

van800 commented May 31, 2018

I haven't found a good way to transform EditorUserBuildSettings.activeBuildTarget into folder name like "Switch", "PS4Player", etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment