Initial Commit

This commit is contained in:
Sebastian Cabrera 2021-08-02 05:44:37 -04:00
parent 53eb92e9af
commit 270ab7d11f
15341 changed files with 700234 additions and 0 deletions

View file

@ -0,0 +1,10 @@
using System;
using UnityEngine;
namespace Unity.Cloud.Collaborate.Common {
[Serializable]
internal class ArrayContainer<T>
{
[SerializeField] public T[] Values = new T[0];
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b4c383403b36ad948aaa161b45b6728b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,38 @@
using System;
using UnityEditorInternal;
using UnityEngine;
namespace Unity.Cloud.Collaborate.Common {
[AttributeUsage(AttributeTargets.Class)]
internal sealed class LocationAttribute : Attribute
{
public enum Location { PreferencesFolder, LibraryFolder }
string m_RelativePath;
readonly Location m_Location;
string m_FilePath;
public string FilePath {
get {
if (m_FilePath != null) return m_FilePath;
if (m_RelativePath[0] == '/')
m_RelativePath = m_RelativePath.Substring(1);
if (m_Location == Location.PreferencesFolder)
m_FilePath = $"{InternalEditorUtility.unityPreferencesFolder}/{m_RelativePath}";
else if (m_Location == Location.LibraryFolder)
m_FilePath = $"Library/Collab/{m_RelativePath}";
return m_FilePath;
}
}
public LocationAttribute(string relativePath, Location location)
{
//Guard.ArgumentNotNullOrWhiteSpace(relativePath, "relativePath");
m_RelativePath = relativePath;
m_Location = location;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f70f59d7b56fd39459b32bfbdc72eb23
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,15 @@
# Unity Collaborate Common Code
This directory contains common classes and logic for the package.
## Overview
This is the structure of the directory:
```none
<root>
├── LocationAttribute.cs
└── ScriptableObjectSingleton.cs
```
`LocationAttribute.cs` attribute used to specify where to cache the ScriptableObjectSingleton.
`ScriptableObjectSingleton.cs` public version of the ScriptableSingleton class in Unity and modified from the
GitHubForUnity implementation of it.

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 4f5692fbca7d9f6459e5506fc3037de0
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,83 @@
using System;
using System.IO;
using System.Linq;
using JetBrains.Annotations;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Assertions;
using Object = UnityEngine.Object;
namespace Unity.Cloud.Collaborate.Common
{
internal class ScriptableObjectSingleton<T> : ScriptableObject where T : ScriptableObject
{
static T s_Instance;
public static T Instance
{
get
{
if (s_Instance == null)
CreateAndLoad();
return s_Instance;
}
}
protected ScriptableObjectSingleton()
{
if (s_Instance != null)
{
Debug.LogError("Singleton already exists!");
}
else
{
s_Instance = this as T;
Assert.IsFalse(s_Instance == null);
}
}
static void CreateAndLoad()
{
Assert.IsTrue(s_Instance == null);
var filePath = GetFilePath();
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
InternalEditorUtility.LoadSerializedFileAndForget(filePath);
}
if (s_Instance == null)
{
var inst = CreateInstance<T>() as ScriptableObjectSingleton<T>;
Assert.IsFalse(inst == null);
inst.hideFlags = HideFlags.HideAndDontSave;
inst.Save();
}
Assert.IsFalse(s_Instance == null);
}
protected void Save()
{
if (s_Instance == null)
{
Debug.LogError("Cannot save singleton, no instance!");
return;
}
var locationFilePath = GetFilePath();
var directoryName = Path.GetDirectoryName(locationFilePath);
if (directoryName == null) return;
Directory.CreateDirectory(directoryName);
InternalEditorUtility.SaveToSerializedFileAndForget(new Object[]{ s_Instance }, locationFilePath, true);
}
[CanBeNull]
static string GetFilePath()
{
var attr = typeof(T).GetCustomAttributes(true)
.OfType<LocationAttribute>()
.FirstOrDefault();
return attr?.FilePath;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9e9013c2188594044bd7c651ad8c9bf3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using UnityEngine;
namespace Unity.Cloud.Collaborate.Common
{
//http://answers.unity3d.com/answers/809221/view.html
[Serializable]
internal class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
{
[SerializeField]
List<TKey> m_Keys = new List<TKey>();
[SerializeField]
List<TValue> m_Values = new List<TValue>();
public SerializableDictionary()
{
}
public SerializableDictionary(IDictionary<TKey, TValue> input) : base(input)
{
}
// Save the dictionary to lists
public void OnBeforeSerialize()
{
m_Keys.Clear();
m_Values.Clear();
foreach (var pair in this)
{
m_Keys.Add(pair.Key);
m_Values.Add(pair.Value);
}
}
// load dictionary from lists
public void OnAfterDeserialize()
{
Clear();
if (m_Keys.Count != m_Values.Count)
{
throw new SerializationException($"there are {m_Keys.Count} keys and {m_Values.Count} " +
"values after deserialization. Make sure that both key and value types are serializable.");
}
for (var i = 0; i < m_Keys.Count; i++)
{
Add(m_Keys[i], m_Values[i]);
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 73b5e3c234aedab45891126c667b675c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,7 @@
using System;
namespace Unity.Cloud.Collaborate.Common {
[Serializable]
internal class StringArrayContainer : ArrayContainer<string>
{}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 15c52dcd6affd004b9932309045504bf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: