Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Report the correct editing status to Unity. Fixes #934 #959

Merged
merged 4 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions GitHub.Unity.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnityTests", "src\UnityExte
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionLoader", "src\UnityExtension\Assets\Editor\GitHub.Unity\ExtensionLoader\ExtensionLoader.csproj", "{6B0EAB30-511A-44C1-87FE-D9AB7E34D115}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnityShim", "src\UnityShim\UnityShim.csproj", "{F94F8AE1-C171-4A83-89E8-6557CA91A188}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -161,6 +163,14 @@ Global
{6B0EAB30-511A-44C1-87FE-D9AB7E34D115}.dev|Any CPU.Build.0 = dev|Any CPU
{6B0EAB30-511A-44C1-87FE-D9AB7E34D115}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B0EAB30-511A-44C1-87FE-D9AB7E34D115}.Release|Any CPU.Build.0 = Release|Any CPU
{F94F8AE1-C171-4A83-89E8-6557CA91A188}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F94F8AE1-C171-4A83-89E8-6557CA91A188}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F94F8AE1-C171-4A83-89E8-6557CA91A188}.DebugNoUnity|Any CPU.ActiveCfg = Debug|Any CPU
{F94F8AE1-C171-4A83-89E8-6557CA91A188}.DebugNoUnity|Any CPU.Build.0 = Debug|Any CPU
{F94F8AE1-C171-4A83-89E8-6557CA91A188}.dev|Any CPU.ActiveCfg = dev|Any CPU
{F94F8AE1-C171-4A83-89E8-6557CA91A188}.dev|Any CPU.Build.0 = dev|Any CPU
{F94F8AE1-C171-4A83-89E8-6557CA91A188}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F94F8AE1-C171-4A83-89E8-6557CA91A188}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ExtensionLoader",
"references": [],
"references": ["../../build/GitHub.UnityShim.dll"],
"includePlatforms": [
"Editor"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ public bool Initialized
private static string[] assemblies20 = { "System.Threading.dll", "AsyncBridge.Net35.dll", "ReadOnlyCollectionsInterfaces.dll", "GitHub.Api.dll", "GitHub.Unity.dll" };
private static string[] assemblies45 = { "GitHub.Api.45.dll", "GitHub.Unity.45.dll" };

private const string GITHUB_UNITY_DISABLE = "GITHUB_UNITY_DISABLE";
private static bool IsDisabled { get { return Environment.GetEnvironmentVariable(GITHUB_UNITY_DISABLE) == "1"; } }

static ExtensionLoader()
{
if (Environment.GetEnvironmentVariable("GITHUB_UNITY_DISABLE") == "1")
if (IsDisabled)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<ProjectReference Include="..\..\..\..\..\UnityShim\UnityShim.csproj">
<Project>{F94F8AE1-C171-4A83-89E8-6557CA91A188}</Project>
<Name>UnityShim</Name>
</ProjectReference>
<Reference Include="UnityEditor">
<HintPath>$(UnityDir)Managed\UnityEditor.dll</HintPath>
<Private>False</Private>
Expand All @@ -65,6 +69,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ExtensionLoader.cs" />
<Compile Include="UnityAPIWrapper.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using UnityEditor;
using UnityEngine;
using System.IO;
using System;

namespace GitHub.Unity
{
[InitializeOnLoad]
public class UnityAPIWrapper : ScriptableSingleton<UnityAPIWrapper>
{
static UnityAPIWrapper()
{
#if UNITY_2018_2_OR_NEWER
Editor.finishedDefaultHeaderGUI += editor => {
UnityShim.Raise_Editor_finishedDefaultHeaderGUI(editor);
};
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
<Project>{bb6a8eda-15d8-471b-a6ed-ee551e0b3ba0}</Project>
<Name>GitHub.Logging</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\UnityShim\UnityShim.csproj">
<Project>{F94F8AE1-C171-4A83-89E8-6557CA91A188}</Project>
<Name>UnityShim</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="ApplicationCache.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "GitHub.Unity",
"references": [],
"references": [
],
"optionalUnityReferences": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": []
"excludePlatforms": [],
"allowUnsafeCode": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
<Project>{bb6a8eda-15d8-471b-a6ed-ee551e0b3ba0}</Project>
<Name>GitHub.Logging</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\UnityShim\UnityShim.csproj">
<Project>{F94F8AE1-C171-4A83-89E8-6557CA91A188}</Project>
<Name>UnityShim</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="IconsAndLogos\added%402x.png" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using GitHub.Logging;
using UnityEditor;
using UnityEngine;

namespace GitHub.Unity
{
Expand All @@ -21,8 +22,12 @@ public static void Initialize(IEnvironment env, IPlatform plat)
environment = env;
platform = plat;
platform.Keychain.ConnectionsChanged += UserMayHaveChanged;
// we need to do this to get the initial user information up front
UserMayHaveChanged();

repository = environment.Repository;
UnityShim.Editor_finishedDefaultHeaderGUI += InspectorHeaderFinished;

if (repository != null)
{
repository.LocksChanged += RepositoryOnLocksChanged;
Expand All @@ -37,19 +42,27 @@ public static string[] OnWillSaveAssets(string[] paths)

public static AssetMoveResult OnWillMoveAsset(string oldPath, string newPath)
{
return IsLocked(oldPath) || IsLocked(newPath) ? AssetMoveResult.FailedMove : AssetMoveResult.DidNotMove;
return IsLockedBySomeoneElse(oldPath) || IsLockedBySomeoneElse(newPath) ? AssetMoveResult.FailedMove : AssetMoveResult.DidNotMove;
}

public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions option)
{
return IsLocked(assetPath) ? AssetDeleteResult.FailedDelete : AssetDeleteResult.DidNotDelete;
return IsLockedBySomeoneElse(assetPath) ? AssetDeleteResult.FailedDelete : AssetDeleteResult.DidNotDelete;
}

// Returns true if this file can be edited by this user
public static bool IsOpenForEdit(string assetPath, out string message)
{
var lck = GetLock(assetPath);
message = lck.HasValue ? "File is locked for editing by " + lck.Value.Owner : null;
return !lck.HasValue;
bool canEdit = true;
if (assetPath.EndsWith(".meta"))
{
canEdit &= !IsLockedBySomeoneElse(lck);
assetPath = assetPath.TrimEnd(".meta");
}
canEdit &= !IsLockedBySomeoneElse(lck);
message = !canEdit ? string.Format("File is locked for editing by {0}", lck.Value.Owner.Name) : null;
return canEdit;
}

private static void RepositoryOnLocksChanged(CacheUpdateEvent cacheUpdateEvent)
Expand All @@ -66,9 +79,14 @@ private static void UserMayHaveChanged()
loggedInUser = platform.Keychain.Connections.Select(x => x.Username).FirstOrDefault();
}

private static bool IsLocked(string assetPath)
private static bool IsLockedBySomeoneElse(GitLock? lck)
{
return GetLock(assetPath).HasValue;
return lck.HasValue && !lck.Value.Owner.Name.Equals(loggedInUser);
}

private static bool IsLockedBySomeoneElse(string assetPath)
{
return IsLockedBySomeoneElse(GetLock(assetPath));
}

private static GitLock? GetLock(string assetPath)
Expand All @@ -78,9 +96,42 @@ private static bool IsLocked(string assetPath)

GitLock lck;
var repositoryPath = environment.GetRepositoryPath(assetPath.ToNPath());
if (!locks.TryGetValue(repositoryPath, out lck) || lck.Owner.Name.Equals(loggedInUser))
return null;
return lck;
if (locks.TryGetValue(repositoryPath, out lck))
return lck;
return null;
}

private static void InspectorHeaderFinished(Editor editor)
{
string message = "";
if (!IsOpenForEdit(AssetDatabase.GetAssetPath(editor.target), out message))
{
var enabled = GUI.enabled;
GUI.enabled = true;
GUILayout.BeginVertical();
{
GUILayout.Space(9);
GUILayout.BeginHorizontal();
{
GUILayout.BeginVertical(GUILayout.Width(32));
{
GUILayout.Label(Utility.GetIcon("big-logo.png", "[email protected]", Utility.IsDarkTheme), GUILayout.Width(32), GUILayout.Height(32));
}
GUILayout.EndVertical();

GUILayout.BeginVertical();
{
GUILayout.Space(9);
GUILayout.Label(message, Styles.HeaderBranchLabelStyle);
}
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
GUI.enabled = enabled;
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private static bool ContextMenu_CanLock()
{
if (!EnsureInitialized())
return false;
if(!Repository.CurrentRemote.HasValue)
if (!Repository.CurrentRemote.HasValue)
return false;
if (isBusy)
return false;
Expand Down Expand Up @@ -222,7 +222,15 @@ private static void OnLocksUpdate()
guidsLocks.Add(g);
}

EditorApplication.RepaintProjectWindow();
// https://github.com/github-for-unity/Unity/pull/959#discussion_r236694800
// We need to repaint not only the project window, but also the inspector.
// so that we can show the "this thing is locked by X" and that the IsOpenForEdit call happens
// and the inspector is disabled. There's no way to refresh the editor directly
// (well, there is, but it's an internal api), so this just causes Unity to repaint everything.
// Nail, meet bazooka, unfortunately, but that's the only way to do it with public APIs ¯_(ツ)_/¯

//EditorApplication.RepaintProjectWindow();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
}

private static void OnStatusUpdate()
Expand Down
14 changes: 14 additions & 0 deletions src/UnityShim/UnityShim.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using UnityEditor;
namespace GitHub.Unity
{
public static class UnityShim
{
public static event Action<Editor> Editor_finishedDefaultHeaderGUI;
public static void Raise_Editor_finishedDefaultHeaderGUI(Editor editor)
{
if (Editor_finishedDefaultHeaderGUI != null)
Editor_finishedDefaultHeaderGUI(editor);
}
}
}
74 changes: 74 additions & 0 deletions src/UnityShim/UnityShim.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F94F8AE1-C171-4A83-89E8-6557CA91A188}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GitHub.Unity</RootNamespace>
<AssemblyName>GitHub.UnityShim</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<LangVersion>6</LangVersion>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<OutputPath>..\UnityExtension\Assets\Editor\build\</OutputPath>
</PropertyGroup>
<Import Project="..\..\common\properties.props" />
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<DefineConstants>DEBUG;TRACE;$(BuildDefs)</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<RunCodeAnalysis>false</RunCodeAnalysis>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<DefineConstants>TRACE;$(BuildDefs)</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<BuildConfig>Release</BuildConfig>
<RunCodeAnalysis>false</RunCodeAnalysis>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'dev|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<DefineConstants>TRACE;DEBUG;DEVELOPER_BUILD;$(BuildDefs)</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<RunCodeAnalysis>false</RunCodeAnalysis>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
<CodeAnalysisRuleSet>..\..\common\codeanalysis-debug.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup>
<BuildConfig Condition=" '$(BuildConfig)' == '' ">Debug</BuildConfig>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="UnityEditor">
<HintPath>$(UnityDir)Managed\UnityEditor.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine">
<HintPath>$(UnityDir)Managed\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="UnityShim.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\common\build.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<Link>ExtensionLoader.cs</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(SolutionDir)\src\UnityExtension\Assets\Editor\GitHub.Unity\ExtensionLoader\UnityAPIWrapper.cs">
<Link>UnityAPIWrapper.cs</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(SolutionDir)\packages\ReadOnlyCollectionInterfaces.1.0.0\lib\NET20\ReadOnlyCollectionsInterfaces.dll">
<Link>ReadOnlyCollectionsInterfaces.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down