update CCK to 3.10, fixing unity 2021 crash :)

This commit is contained in:
Crispy 2024-08-03 22:24:42 +02:00
parent 48a978fa2a
commit d11e0fb3a9
492 changed files with 2165204 additions and 437687 deletions

View file

@ -0,0 +1,202 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditorInternal;
namespace ABI.CCK.Scripts.Editor.Tools
{
// todo: revamp and allow OnCopy/OnPaste/OnCut/OnDelete/OnReset events to be listened for and invoked on the target
public static class ClipboardUtils
{
private static readonly List<object> s_clipboard = new();
#region Copy Methods
public static void CopySelected<T>(List<T> source, int selectedIndex, SerializedProperty property = null)
{
if (selectedIndex < 0 || selectedIndex >= source.Count)
return;
s_clipboard.Clear();
s_clipboard.Add(source[selectedIndex]);
}
public static void CopyAll<T>(List<T> source, SerializedProperty property = null)
{
s_clipboard.Clear();
foreach (T item in source) s_clipboard.Add(item);
}
#endregion
#region Cut Methods
public static void CutSelected<T>(List<T> source, int selectedIndex)
{
if (selectedIndex < 0 || selectedIndex >= source.Count)
return;
s_clipboard.Clear();
s_clipboard.Add(source[selectedIndex]);
source.RemoveAt(selectedIndex);
}
public static void CutAll<T>(List<T> source)
{
s_clipboard.Clear();
foreach (T item in source)
s_clipboard.Add(item);
source.Clear();
}
#endregion
#region Paste Methods
public static void Paste<T>(List<T> target, SerializedProperty property = null) where T : new()
{
if (property == null)
{
target.AddRange(from item in s_clipboard where item is T select CreateNewInstanceWithValues<T>(item));
return;
}
for (int i = 0; i < s_clipboard.Count; i++) property.CreateFromInstance((T)s_clipboard[i]);
property.serializedObject.ApplyModifiedProperties(); // record undo/redo
}
private static T CreateNewInstanceWithValues<T>(object sourceItem) where T : new()
{
T newInstance = new();
if (sourceItem == null)
return newInstance;
// this is not a deep copy, but for most things in the cck it shouldn't matter
foreach (PropertyInfo property in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (property.CanRead && property.CanWrite)
property.SetValue(newInstance, property.GetValue(sourceItem, null), null);
}
foreach (FieldInfo field in typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance))
field.SetValue(newInstance, field.GetValue(sourceItem));
return newInstance;
}
public static bool IsValidPaste<T>(List<T> target, SerializedProperty property = null)
{
return s_clipboard.Count != 0 && s_clipboard.All(item => item is T);
}
#endregion
#region Reset Methods
public static void ResetSelected<T>(List<T> source, int selectedIndex) where T : new()
{
if (selectedIndex < 0 || selectedIndex >= source.Count)
return;
if (typeof(T).IsValueType || typeof(T).GetConstructor(Type.EmptyTypes) != null)
source[selectedIndex] = new T();
else
source[selectedIndex] = default(T);
}
public static void ResetAll<T>(List<T> source) where T : new()
{
for (int i = 0; i < source.Count; i++)
{
if (typeof(T).IsValueType || typeof(T).GetConstructor(Type.EmptyTypes) != null)
source[i] = new T();
else
source[i] = default(T);
}
}
#endregion
#region Delete Methods
public static void DeleteSelected<T>(List<T> source, int selectedIndex, SerializedProperty property = null)
{
if (selectedIndex < 0 || selectedIndex >= source.Count)
return;
if (property == null)
{
source.RemoveAt(selectedIndex);
return;
}
property.DeleteArrayElementAtIndex(selectedIndex);
property.serializedObject.ApplyModifiedProperties(); // record undo/redo
}
public static void DeleteAll<T>(List<T> source, SerializedProperty property = null)
{
if (property == null)
{
source.Clear();
return;
}
property.ClearArray();
property.serializedObject.ApplyModifiedProperties(); // record undo/redo
}
#endregion
#region Menu Builder
public static void AppendToMenu(GenericMenuBuilder genericMenuBuilder, ReorderableList list, SerializedProperty property = null)
=> AppendToMenuInternal(genericMenuBuilder, list.list, list.index, property);
private static void AppendToMenuInternal(GenericMenuBuilder genericMenuBuilder, IList list, int index = -1, SerializedProperty property = null)
{
Type itemType = GetItemTypeOfList(list);
if (itemType == null) return;
bool hasSelectedItem = index != -1;
bool hasItems = list.Count > 0;
genericMenuBuilder.AddMenuItem("Copy/Selected", hasSelectedItem, () => InvokeClipboardMethod("CopySelected", itemType, list, index, property));
genericMenuBuilder.AddMenuItem("Copy/All", hasItems, () => InvokeClipboardMethod("CopyAll", itemType, list, property: property));
//menuBuilder.AddMenuItem("Cut/Selected", hasSelectedItem, () => InvokeClipboardMethod("CutSelected", itemType, list, index));
//menuBuilder.AddMenuItem("Cut/All", hasTasks, () => InvokeClipboardMethod("CutAll", itemType, list));
genericMenuBuilder.AddMenuItem("Paste", (bool)InvokeClipboardMethod("IsValidPaste", itemType, list, property: property), () => InvokeClipboardMethod("Paste", itemType, list, property: property));
//menuBuilder.AddMenuItem("Reset/Selected", hasSelectedItem, () => InvokeClipboardMethod("ResetSelected", itemType, list, index));
//menuBuilder.AddMenuItem("Reset/All", hasTasks, () => InvokeClipboardMethod("ResetAll", itemType, list));
genericMenuBuilder.AddMenuItem("Delete/Selected", hasSelectedItem, () => InvokeClipboardMethod("DeleteSelected", itemType, list, index, property: property));
genericMenuBuilder.AddMenuItem("Delete/All", hasItems, () => InvokeClipboardMethod("DeleteAll", itemType, list, property: property));
}
private static Type GetItemTypeOfList(IList list)
{
if (list == null) return null;
Type listType = list.GetType();
if (listType.IsGenericType && listType.GetGenericTypeDefinition() == typeof(List<>))
return listType.GetGenericArguments()[0];
return null;
}
private static object InvokeClipboardMethod(string methodName, Type itemType, IList list, int? selectedIndex = null, SerializedProperty property = null)
{
MethodInfo method = typeof(ClipboardUtils).GetMethod(methodName)?.MakeGenericMethod(itemType);
return method?.Invoke(null, selectedIndex.HasValue ? new object[] { list, selectedIndex.Value, property } : new object[] { list, property });
}
#endregion
}
}

View file

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

View file

@ -0,0 +1,18 @@

using UnityEditor;
namespace ABI.CCK.Scripts.Editor.Tools
{
public static class LuaScriptCreator
{
private const string kLuaScriptTemplatePath = "Assets/ABI.CCK/Templates/LuaScriptTemplate.lua";
private const string kLuaScriptDefaultName = "NewLuaScript.lua";
[MenuItem("Assets/Create/CVR Lua Script", priority = 0)]
public static void CreateLuaScript()
{
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(
kLuaScriptTemplatePath, kLuaScriptDefaultName);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b6bd77550b6b4e5491831cad26d052b0
timeCreated: 1713054878

View file

@ -0,0 +1,78 @@
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace ABI.CCK.Scripts.Editor.Tools
{
public static class SerializedPropertyExtensions
{
public static SerializedProperty AddWithDefaults<T>(this SerializedProperty prop) where T : new()
{
prop.arraySize++;
SerializedProperty newArrayElement = prop.GetArrayElementAtIndex(prop.arraySize - 1);
T defaultInstance = new();
Type type = typeof(T);
foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
{
SerializedProperty property = newArrayElement.FindPropertyRelative(field.Name);
if (property == null) continue;
object defaultValue = field.GetValue(defaultInstance);
if (defaultValue == null) continue;
if (field.FieldType == typeof(float))
{
property.floatValue = (float)defaultValue;
}
else if (field.FieldType == typeof(int)
|| field.FieldType.IsEnum) // dont use enumValueIndex here
{
property.intValue = (int)defaultValue;
}
// todo: add more types here if needed, mainly enum seemed to not respect default
}
return newArrayElement;
}
public static SerializedProperty CreateFromInstance<T>(this SerializedProperty prop, T instance)
{
prop.arraySize++;
SerializedProperty newArrayElement = prop.GetArrayElementAtIndex(prop.arraySize - 1);
Type type = typeof(T);
foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
{
SerializedProperty property = newArrayElement.FindPropertyRelative(field.Name);
if (property == null) continue;
object value = field.GetValue(instance);
if (value == null) continue;
if (field.FieldType == typeof(float))
{
property.floatValue = (float)value;
}
else if (field.FieldType == typeof(int)
|| field.FieldType.IsEnum)
{
property.intValue = (int)value;
}
else if (field.FieldType == typeof(bool))
{
property.boolValue = (bool)value;
}
else if (field.FieldType == typeof(string))
{
property.stringValue = (string)value;
}
}
return newArrayElement;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 141d91ed9bff41b0827f63e59c10a1dc
timeCreated: 1707322951

View file

@ -0,0 +1,82 @@
using System.IO;
using System.Linq;
using Abi.Newtonsoft.Json;
using Abi.Newtonsoft.Json.Linq;
using UnityEditor;
namespace ABI.CCK.Scripts.Editor.Tools
{
public static class SetupVSCode
{
public const string CVR_STDLIB_LUA_STUBS_PATH = "Assets/ABI.CCK/LuaStubs/Common";
public const string VSCODE_PATH = ".vscode/settings.json";
[MenuItem("Alpha Blend Interactive/Scripting/Set Up VSCode", priority = 0)]
public static void CreateLuaScript()
{
bool changed = false;
JToken cfg = null;
if (File.Exists(VSCODE_PATH))
{
using (StreamReader f = File.OpenText(VSCODE_PATH))
using (JsonTextReader reader = new JsonTextReader(f))
{
cfg = JToken.ReadFrom(reader);
}
}
else
{
changed = true;
}
if (cfg == null)
{
cfg = new JObject();
changed = true;
}
if (!cfg.Contains("Lua"))
{
cfg["Lua"] = new JObject();
changed = true;
}
if (!cfg["Lua"].Contains("workspace"))
{
cfg["Lua"]["workspace"] = new JObject();
changed = true;
}
if (!cfg["Lua"]["workspace"].Contains("library") ||
cfg["Lua"]["workspace"]["library"].Type != JTokenType.Array)
{
cfg["Lua"]["workspace"]["library"] = new JArray();
changed = true;
}
var lib = (JArray)cfg["Lua"]["workspace"]["library"];
var path = new JValue(CVR_STDLIB_LUA_STUBS_PATH);
if (!lib.Contains(path))
{
lib.Add(path);
changed = true;
}
if (changed)
{
var pdir = Path.GetDirectoryName(VSCODE_PATH);
if (pdir != null) Directory.CreateDirectory(pdir);
using (StreamWriter sw = File.CreateText(VSCODE_PATH))
using (JsonTextWriter writer = new JsonTextWriter(sw))
{
cfg.WriteTo(writer);
}
}
EditorUtility.DisplayDialog(
"VSCode Setup Complete",
"VSCode has been configured to find the LuaLS autocomplete stubs.\n\nYou will need to install the \"sumneko.lua\" package to make use of this feature.",
"OK");
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 464ec7bb39164d3f8cb203666f017525
timeCreated: 1713056617