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

Commit 9d78208

Browse files
author
Kayla Firestack
committed
Updates,
1 parent 1dafa61 commit 9d78208

File tree

21 files changed

+734
-500
lines changed

21 files changed

+734
-500
lines changed

UMFTests/UMFS.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using UnityMultiFramework;
45

56
namespace UMFTests
67
{
@@ -10,7 +11,7 @@ public class UMFS
1011
[TestMethod]
1112
public void TestFileLocations()
1213
{
13-
var a = UnityMultiFramework.Unity.Projects.ProjectLocations.ToList();
14+
var a = Unity.Projects.ToList();
1415

1516
}
1617
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace UnityMultiFramework
7+
{
8+
public class ExecutableAccessor : IEnumerable<Executable>
9+
{
10+
internal ExecutableAccessor() { }
11+
12+
public List<Executable> Locations = new List<Executable>();
13+
14+
public void AddIn(Uri baseLocation)
15+
=> Locations.AddRange(FindIn(baseLocation));
16+
17+
private IEnumerable<Executable> FindIn(Uri baseLocation)
18+
=> System.IO.Directory
19+
.EnumerateFiles(baseLocation.LocalPath, "Unity.exe", System.IO.SearchOption.AllDirectories)
20+
.Select(loc => new Executable(new Uri(loc)));
21+
22+
public IEnumerable<Executable> ClosestExecutables(IVersionable ver)
23+
=> Locations.Where(loc => loc.FuzzyEquals(ver)).OrderBy(exe => Math.Abs(exe.FuzzyCompareTo(ver)));
24+
25+
public IEnumerator<Executable> GetEnumerator()
26+
{
27+
return ((IEnumerable<Executable>)Locations).GetEnumerator();
28+
}
29+
30+
IEnumerator IEnumerable.GetEnumerator()
31+
{
32+
return ((IEnumerable<Executable>)Locations).GetEnumerator();
33+
}
34+
}
35+
36+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Microsoft.Win32;
2+
using System;
3+
using System.IO;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Collections;
8+
9+
namespace UnityMultiFramework
10+
{
11+
12+
public class ProjectAccessor : IEnumerable<Project>
13+
{
14+
private const string UNITYREGKEY = @"SOFTWARE\Unity Technologies\Unity Editor 5.x\";
15+
16+
internal ProjectAccessor() { }
17+
18+
private List<Uri> ExtraLocations = new List<Uri>();
19+
20+
public IEnumerable<Project> ProjectLocations
21+
=> ExistingLocations
22+
.Select(uri => new Project(uri));
23+
24+
public IEnumerable<Uri> ExistingLocations
25+
=> ExtraLocations
26+
.Concat((RegKeyToUri() ?? Enumerable.Empty<Uri>()))
27+
.Distinct()
28+
.Where(uri =>
29+
Directory.Exists(uri.LocalPath) &&
30+
Directory.Exists(Path.Combine(uri.LocalPath, "Assets"))
31+
);
32+
33+
public void AddLocations(params Uri[] uris)
34+
=> ExtraLocations.AddRange(uris);
35+
36+
public IEnumerable<Uri> RegKeyToUri()
37+
{
38+
RegistryKey UnityRegKey = Registry
39+
.CurrentUser
40+
.OpenSubKey(UNITYREGKEY, false);
41+
42+
return UnityRegKey?
43+
.GetValueNames()
44+
.Where(key => key.StartsWith("RecentlyUsedProjectPaths"))
45+
.Select(key =>
46+
new Uri(Encoding.UTF8.GetString(UnityRegKey.GetValue(key) as byte[]).TrimEnd((char)0))
47+
);
48+
}
49+
50+
#region IEnumerable<Project>
51+
52+
public IEnumerator<Project> GetEnumerator() => ProjectLocations.GetEnumerator();
53+
54+
IEnumerator IEnumerable.GetEnumerator() => ProjectLocations.GetEnumerator();
55+
56+
#endregion
57+
}
58+
}

UnityMultiFramework/Executable.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
3+
namespace UnityMultiFramework
4+
{
5+
public class Executable : IVersionable, IEquatable<Executable>, ILaunchable
6+
{
7+
private const string WILDCARD = "*";
8+
9+
private Executable() { }
10+
11+
public Executable(Uri Location)
12+
{
13+
this.Location = Location;
14+
VersionType = WILDCARD;
15+
16+
var versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(Location.LocalPath);
17+
Version = new Version(versionInfo.ProductMajorPart, versionInfo.ProductMinorPart, versionInfo.ProductBuildPart, 0);
18+
}
19+
20+
public Uri Location { get; private set; }
21+
22+
public Version Version { get; private set; }
23+
24+
public string VersionType { get; set; }
25+
26+
public int MinorVersion { set { Version = new Version(Version.Major, Version.Minor, Version.Build, value); } }
27+
28+
public void Launch(params string[] args)
29+
=> System.Diagnostics.Process.Start(Location.LocalPath, string.Join(" ", args));
30+
31+
public int CompareTo(IVersionable other)
32+
=> Version.CompareTo(other);
33+
34+
public bool Equals(IVersionable other)
35+
=> FuzzyEquals(other) && (VersionType == WILDCARD || other.VersionType == WILDCARD) ? true : (VersionType == other.VersionType);
36+
37+
public bool FuzzyEquals(IVersionable other)
38+
{
39+
return (Version.Major == other.Version.Major) && (Version.Minor == other.Version.Minor) && (Version.Build == other.Version.Build);
40+
}
41+
42+
public int FuzzyCompareTo(IVersionable other)
43+
{
44+
if ( !FuzzyEquals(other) ) { return CompareTo(other); }
45+
if( VersionType != other.VersionType ) { return VersionType.CompareTo(other.VersionType); }
46+
return Version.Revision - other.Version.Revision;
47+
}
48+
49+
public bool Equals(Executable other)
50+
=> Location == other.Location;
51+
}
52+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace UnityMultiFramework
8+
{
9+
public interface ILaunchable
10+
{
11+
void Launch(params string[] args);
12+
}
13+
}

UnityMultiFramework/Properties/IVersionable.cs renamed to UnityMultiFramework/Interfaces/IVersionable.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ public interface IVersionable : IComparable<IVersionable>, IEquatable<IVersionab
66
{
77
Version Version { get; }
88
string VersionType { get; }
9+
10+
//bool operator ==(IVersionable lhs, IVersionable rhs);
11+
//bool operator !=(IVersionable lhs, IVersionable rhs);
912
}
1013
}

UnityMultiFramework/Project.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text.RegularExpressions;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace UnityMultiFramework
9+
{
10+
11+
[System.Diagnostics.DebuggerDisplay("Name = {Name}, Version = {ProjectVersionString}")]
12+
public class Project : IVersionable, IEquatable<Project>, ILaunchable
13+
{
14+
//m_EditorVersion: 2017.1.0f3
15+
private static Regex versionExp = new Regex(@"^m_EditorVersion: (\d+\.\d+\.\d+([A-z]{1,4})\d+)", RegexOptions.Compiled);
16+
17+
public Project(Uri location)
18+
{
19+
Location = location;
20+
LoadVersionInfo();
21+
Name = Uri.UnescapeDataString(Location.Segments.Last());
22+
}
23+
24+
public string Name { get; private set; }
25+
26+
public Uri Location { get; private set; }
27+
28+
public string ProjectVersionString { get; private set; }
29+
30+
public Version Version { get; private set; }
31+
32+
public string VersionType { get; private set; }
33+
34+
private void LoadVersionInfo()
35+
{
36+
var filename = Path.Combine(Location.LocalPath, @"ProjectSettings\ProjectVersion.txt");
37+
if (File.Exists(filename))
38+
{
39+
var filedata = File.ReadAllText(filename, Encoding.UTF8);
40+
var matches = versionExp.Match(filedata);
41+
try
42+
{
43+
ProjectVersionString = matches.Groups[1].Value;
44+
VersionType = matches.Groups[2].Value;
45+
Version = Version.Parse(ProjectVersionString.Replace(VersionType, "."));
46+
}
47+
catch (Exception E)
48+
{
49+
E.Data["ProjectText"] = filedata.ToString();
50+
E.Data["Matches"] = matches.ToString();
51+
throw;
52+
}
53+
}
54+
}
55+
56+
public void Launch(params string[] args)
57+
=> Launch(Unity.Executables.ClosestExecutables(this).First(), args);
58+
59+
public void Launch(Executable exe = null, params string[] args)
60+
{
61+
var argsList = args.ToList();
62+
argsList.Insert(0, $@"-projectpath ""{Location.LocalPath}""");
63+
64+
exe.Launch(argsList.ToArray());
65+
}
66+
67+
#region Interfaces
68+
69+
public int CompareTo(IVersionable other)
70+
{
71+
return this.Version.CompareTo(other.Version);
72+
}
73+
74+
public bool Equals(IVersionable other)
75+
{
76+
return Version == other.Version && VersionType == other.VersionType;
77+
}
78+
79+
public bool Equals(Project other)
80+
{
81+
return Location == other.Location;
82+
}
83+
84+
#endregion
85+
}
86+
}

UnityMultiFramework/Properties/Unity.Executables.cs

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)