Initial commit

This commit is contained in:
msk
2022-01-22 20:13:49 -08:00
parent f9d23e5bcf
commit 687473573d
878 changed files with 70957 additions and 0 deletions
@@ -0,0 +1,51 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BepInEx;
using Mono.Cecil;
using Mono.Cecil.Cil;
using UnityEditor;
using UnityEngine;
namespace MeatKit
{
public abstract class BuildItem : ScriptableObject, IValidatable
{
public abstract IEnumerable<string> RequiredDependencies { get; }
public virtual Dictionary<string, BuildMessage> Validate()
{
return new Dictionary<string, BuildMessage>();
}
public virtual List<AssetBundleBuild> ConfigureBuild()
{
return null;
}
public virtual void GenerateLoadAssets(TypeDefinition plugin, ILProcessor il)
{
}
protected void EnsurePluginDependsOn(TypeDefinition plugin, string pluginGuid, string pluginVersion)
{
// Check if the plugin already has this dependency
var alreadyDependsOn = plugin.CustomAttributes
.Where(a => a.AttributeType.Name == "BepInDependency")
.Any(attribute => (string) attribute.ConstructorArguments[0].Value == pluginGuid);
// If it doesn't we need to add it.
if (!alreadyDependsOn)
{
MethodBase constructor =
typeof(BepInDependency).GetConstructor(new[] {typeof(string), typeof(string)});
var attribute = new CustomAttribute(plugin.Module.ImportReference(constructor));
plugin.CustomAttributes.Add(attribute);
var str = plugin.Module.TypeSystem.String;
attribute.ConstructorArguments.Add(new CustomAttributeArgument(str, pluginGuid));
attribute.ConstructorArguments.Add(new CustomAttributeArgument(str, pluginVersion));
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 32dfb407962b465197e145b9cf760fcf
timeCreated: 1635010462
@@ -0,0 +1,70 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace MeatKit
{
[CustomEditor(typeof(BuildItem), true)]
public class BuildItemEditor : Editor
{
protected Dictionary<string, BuildMessage> ValidationMessages;
public override void OnInspectorGUI()
{
// Apply any changes and validate them
ValidationMessages = ((IValidatable) target).Validate();
// Draw the property fields and their message boxes, if any.
var property = serializedObject.GetIterator();
if (!property.NextVisible(true)) return;
do DrawProperty(property);
while (property.NextVisible(false));
serializedObject.ApplyModifiedProperties();
}
protected virtual void DrawProperty(SerializedProperty property)
{
// Don't draw the script name window.
if (property.name == "m_Script") return;
EditorGUILayout.PropertyField(property, true);
DrawMessageIfExists(property.name);
}
protected void DrawMessageIfExists(string propertyName)
{
BuildMessage message;
if (ValidationMessages.TryGetValue(propertyName, out message))
EditorGUILayout.HelpBox(message.Message, message.Type);
}
protected void DrawListWithRequiredElements(string[] required, SerializedProperty additional)
{
EditorGUI.indentLevel++;
// Draw the size field
var size = EditorGUILayout.DelayedIntField("Size", required.Length + additional.arraySize);
size = Mathf.Max(required.Length, size);
// Resize the array if necessary
var newSize = size - required.Length;
if (newSize != additional.arraySize) additional.arraySize = newSize;
// Draw the required dependencies. These are disabled.
EditorGUI.BeginDisabledGroup(true);
foreach (var dep in required)
EditorGUILayout.TextField(dep);
EditorGUI.EndDisabledGroup();
// Draw the additional dependencies
for (var i = 0; i < additional.arraySize; i++)
{
var value = additional.GetArrayElementAtIndex(i);
value.stringValue = EditorGUILayout.TextField(value.stringValue);
}
EditorGUI.indentLevel--;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ebaaff1430114c54b502477e745a02c2
timeCreated: 1635011555
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 131929050e124316a330bb2af8cf8f94
timeCreated: 1637507648
@@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using System.Linq;
using System.Reflection;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Sodalite;
using Sodalite.Api;
namespace MeatKit
{
[CreateAssetMenu(menuName = "MeatKit/Build Items/Preload Assets", fileName = "New build item")]
public class PreloadAssetsBuildItem : BuildItem
{
public Object[] Items;
public override IEnumerable<string> RequiredDependencies
{
get { return new[] {"nrgill28-Sodalite-1.2.0"}; }
}
public override List<AssetBundleBuild> ConfigureBuild()
{
List<AssetBundleBuild> bundles = new List<AssetBundleBuild>();
bundles.Add(new AssetBundleBuild
{
assetBundleName = BuildWindow.SelectedProfile.PackageName.ToLower() + "_preload",
assetNames = Items.Select(AssetDatabase.GetAssetPath).ToArray()
});
return bundles;
}
public override void GenerateLoadAssets(TypeDefinition plugin, ILProcessor il)
{
#if H3VR_IMPORTED
EnsurePluginDependsOn(plugin, SodaliteConstants.Guid, SodaliteConstants.Version);
// Get some references
const BindingFlags publicStatic = BindingFlags.Public | BindingFlags.Static;
FieldReference basePath = plugin.Fields.First(f => f.Name == "BasePath");
MethodInfo pathCombine = typeof(Path).GetMethod("Combine", publicStatic);
MethodInfo sodalitePreloadAllAssets = typeof(GameAPI).GetMethod("PreloadAllAssets", publicStatic);
// Emit our opcodes
il.Emit(OpCodes.Ldsfld, basePath);
il.Emit(OpCodes.Ldstr, BuildWindow.SelectedProfile.PackageName.ToLower() + "_preload");
il.Emit(OpCodes.Call, plugin.Module.ImportReference(pathCombine));
il.Emit(OpCodes.Call, plugin.Module.ImportReference(sodalitePreloadAllAssets));
#endif
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d18106ea7c52411db45cabd362f21f16
timeCreated: 1640061580
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 151c1f5398ee70041a49c417b23f1846, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace MeatKit
{
[CreateAssetMenu(menuName = "MeatKit/Build Items/Store Files", fileName = "New build item")]
public class StoreFilesBuildItem : BuildItem
{
public string BundleName;
public Object[] Items;
public override IEnumerable<string> RequiredDependencies
{
get { return new string[0]; }
}
public override Dictionary<string, BuildMessage> Validate()
{
var messages = base.Validate();
if (BundleName != Extensions.MakeValidFileName(BundleName))
messages["BundleName"] = BuildMessage.Error("Bundle name contains invalid characters.");
return messages;
}
public override List<AssetBundleBuild> ConfigureBuild()
{
List<AssetBundleBuild> bundles = new List<AssetBundleBuild>();
bundles.Add(new AssetBundleBuild
{
assetBundleName = BundleName,
assetNames = Items.Select(AssetDatabase.GetAssetPath).ToArray()
});
return bundles;
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f6d6db4144b34551885c304840edfb62
timeCreated: 1640061353
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 6fae6dfd178cd184180013acef55632c, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,31 @@
using UnityEditor;
namespace MeatKit
{
public class BuildMessage
{
private BuildMessage(MessageType type, string message)
{
Type = type;
Message = message;
}
public MessageType Type { get; private set; }
public string Message { get; private set; }
public static BuildMessage Info(string message)
{
return new BuildMessage(MessageType.Info, message);
}
public static BuildMessage Warning(string message)
{
return new BuildMessage(MessageType.Warning, message);
}
public static BuildMessage Error(string message)
{
return new BuildMessage(MessageType.Error, message);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4bad4598a9f145a3b5163239bb6c7536
timeCreated: 1635009317
@@ -0,0 +1,201 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using AssetsTools.NET;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
#if H3VR_IMPORTED
using Valve.Newtonsoft.Json;
using Valve.Newtonsoft.Json.Linq;
#endif
namespace MeatKit
{
[CreateAssetMenu(menuName = "MeatKit/Build Profile")]
public class BuildProfile : ScriptableObject, IValidatable
{
[Header("Thunderstore Metadata")]
public string PackageName = "";
public string Author = "";
public string Version = "";
public Texture2D Icon;
public Object ReadMe;
public string WebsiteURL = "";
public string Description = "";
public string[] AdditionalDependencies = new string[0];
[Header("Script Options")]
public bool StripNamespaces = true;
public string[] AdditionalNamespaces = new string[0];
[Header("Export Options")]
public BuildItem[] BuildItems = new BuildItem[0];
public AssetBundleCompressionType BundleCompressionType = AssetBundleCompressionType.LZ4;
public BuildAction BuildAction = BuildAction.JustBuildFiles;
[HideInInspector]
public string OutputProfile = "";
public Dictionary<string, BuildMessage> Validate()
{
var messages = new Dictionary<string, BuildMessage>();
// Package name needs to match regex
if (!Regex.IsMatch(PackageName, @"^[a-zA-Z_0-9]+$"))
messages["PackageName"] =
BuildMessage.Error("Package name can only contain letters, numbers, and underscores.");
// Make sure the version number is a valid x.x.x
if (!Regex.IsMatch(Version, @"^\d+\.\d+\.\d+$"))
messages["Version"] = BuildMessage.Error("Version number must be in format 'x.x.x'.");
// Description must be no longer than 250 chars
if (Description.Length > 250)
messages["Description"] = BuildMessage.Error("Description cannot be longer than 250 characters.");
// Icon must exist and be 256 x 256
if (!Icon)
messages["Icon"] = BuildMessage.Error("Missing icon.");
else if (Icon.width != 256 || Icon.height != 256)
messages["Icon"] = BuildMessage.Info("Icon will be resized to 256x256.");
if (!ReadMe)
messages["ReadMe"] = BuildMessage.Error("Missing readme.");
switch (BundleCompressionType)
{
case AssetBundleCompressionType.NONE:
messages["BundleCompressionType"] = BuildMessage.Warning(
"Uncompressed bundles are not recommended for publication. They can and will be very large.");
break;
case AssetBundleCompressionType.LZMA:
messages["BundleCompressionType"] = BuildMessage.Info(
"LZMA can take longer to compress than LZ4, however it will result in smaller file sizes usually.");
break;
}
switch (BuildAction)
{
case BuildAction.JustBuildFiles:
messages["BuildAction"] =
BuildMessage.Info(
"This will just create the files for a Thunderstore package in your AssetBundles folder.");
break;
case BuildAction.CopyToProfile:
messages["BuildAction"] =
BuildMessage.Info(
"This will copy the built files into the plugins folder of the selected profile.");
if (string.IsNullOrEmpty(OutputProfile))
messages["OutputProfile"] = BuildMessage.Error("Please set the output profile.");
else if (!Directory.Exists(OutputProfile))
messages["OutputProfile"] = BuildMessage.Error("Selected profile no longer exists.");
else if (!File.Exists(Path.Combine(OutputProfile, "mods.yml")))
messages["OutputProfile"] = BuildMessage.Error(
"Selected folder is not a r2mm profile. Please select the folder which contains the plugins folder.");
break;
case BuildAction.CreateThunderstorePackage:
messages["BuildAction"] =
BuildMessage.Info(
"This will zip up the built files as a final build for importing into r2mm / uploading to Thunderstore.");
break;
}
return messages;
}
public bool EnsureValidForEditor()
{
// Go over each build item
bool hasErrors = false, hasWarnings = false;
foreach (var item in BuildItems)
// Check if it has any validation messages
foreach (var message in item.Validate().Values)
// Log them
switch (message.Type)
{
case MessageType.Error:
Debug.LogError(AssetDatabase.GetAssetPath(item) + ": " + message.Message);
hasErrors = true;
break;
case MessageType.Warning:
Debug.LogWarning(AssetDatabase.GetAssetPath(item) + ": " + message.Message);
hasWarnings = true;
break;
case MessageType.Info:
Debug.Log(AssetDatabase.GetAssetPath(item) + ": " + message.Message);
break;
default:
throw new ArgumentOutOfRangeException();
}
// If there's errors don't let anything continue
if (hasErrors)
{
EditorUtility.DisplayDialog("Build errors",
"There were errors validating your build items. Please check the console for more info.", "Ok.");
return false;
}
// If there's only warnings, let the user decide if they want to continue
if (hasWarnings)
return EditorUtility.DisplayDialog("Build warnings",
"Some build items validated with warnings. Continue with build anyway?", "Yes", "No");
// Otherwise continue
return true;
}
public string[] GetRequiredDependencies()
{
return BuildItems
.Where(x => x != null)
.SelectMany(x => x.RequiredDependencies).ToArray();
}
public string MainNamespace
{
get
{
return Author + "." + PackageName;
}
}
public string[] GetRequiredNamespaces()
{
return new[] {MainNamespace};
}
public string[] GetAllAllowedNamespaces()
{
return GetRequiredNamespaces().Concat(AdditionalNamespaces).ToArray();
}
public void WriteThunderstoreManifest(string location)
{
#if H3VR_IMPORTED
var obj = new JObject();
obj["name"] = PackageName;
obj["author"] = Author;
obj["version_number"] = Version;
obj["description"] = Description;
obj["website_url"] = string.IsNullOrEmpty(WebsiteURL) ? "" : WebsiteURL;
// ReSharper disable once CoVariantArrayConversion
obj["dependencies"] = new JArray(GetRequiredDependencies().Concat(AdditionalDependencies).ToArray());
File.WriteAllText(location, JsonConvert.SerializeObject(obj));
#endif
}
}
public enum BuildAction
{
JustBuildFiles,
CopyToProfile,
CreateThunderstorePackage
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: afed54a239594b8abd631a8047a54987
timeCreated: 1635004125
@@ -0,0 +1,63 @@
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace MeatKit
{
[CustomEditor(typeof(BuildProfile))]
public class BuildProfileEditor : BuildItemEditor
{
private bool _folded1, _folded2;
private BuildProfile _profile;
private void OnEnable()
{
_profile = (BuildProfile) target;
}
protected override void DrawProperty(SerializedProperty property)
{
if (property.name == "AdditionalDependencies")
{
_folded1 = EditorGUILayout.Foldout(_folded1, "Dependencies");
if (_folded1) DrawListWithRequiredElements(_profile.GetRequiredDependencies(), property);
}
else if (property.name == "AdditionalNamespaces")
{
if (!_profile.StripNamespaces) return;
_folded2 = EditorGUILayout.Foldout(_folded2, "Allowed Namespaces");
if (_folded2)
{
DrawListWithRequiredElements(_profile.GetRequiredNamespaces(), property);
EditorGUI.indentLevel++;
EditorGUILayout.HelpBox("Namespaces relate to custom scripts in your project. Any code in a namespace not included here will not be included in your final build. Note this only applies to .cs files directly in your project. Scripts coming from .dll files are not affected by this setting.", MessageType.Info);
EditorGUI.indentLevel--;
}
}
else if (property.name == "BuildAction")
{
base.DrawProperty(property);
// Draw the build action stuff
if (_profile.BuildAction == BuildAction.CopyToProfile)
{
// Tell the user which profile it will be output to
string profileName = Path.GetFileName(_profile.OutputProfile);
GUILayout.Label("Selected profile: " + (string.IsNullOrEmpty(profileName) ? "None" : profileName));
// Give a button to change the output folder
if (GUILayout.Button("Select profile folder"))
_profile.OutputProfile = EditorUtility.OpenFolderPanel("Select your r2mm profile folder", @"%APPDATA%\Roaming\r2modmanPlus-local\H3VR\profiles", "");
// Draw any errors that come from the BuildAction property, as those won't get displayed otherwise.
DrawMessageIfExists("OutputProfile");
}
}
else base.DrawProperty(property);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b94a892488a449798f8fd227d82d86fc
timeCreated: 1635006063
@@ -0,0 +1,76 @@
using System;
using UnityEditor;
using UnityEngine;
namespace MeatKit
{
public class BuildWindow : EditorWindow
{
private static BuildProfile _selectedProfileInternal;
private static BuildProfileEditor _editor;
public static BuildProfile SelectedProfile
{
get
{
if (!_selectedProfileInternal)
_selectedProfileInternal = MeatKitCache.LastSelectedProfile;
return _selectedProfileInternal;
}
private set
{
if (_selectedProfileInternal != value)
_editor = null;
_selectedProfileInternal = value;
}
}
private static BuildProfileEditor EditorInstance
{
get
{
if (!_editor && SelectedProfile)
_editor = (BuildProfileEditor) Editor.CreateEditor(SelectedProfile, typeof(BuildProfileEditor));
return _editor;
}
}
[MenuItem("MeatKit/Build Window")]
public static void Open()
{
GetWindow<BuildWindow>("MeatKit Build").Show();
}
private void OnGUI()
{
EditorGUILayout.LabelField("Selected Build Profile", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
SelectedProfile = EditorGUILayout.ObjectField(SelectedProfile, typeof(BuildProfile), false) as BuildProfile;
if (EditorGUI.EndChangeCheck())
{
MeatKitCache.LastSelectedProfile = SelectedProfile;
}
if (!SelectedProfile)
{
GUILayout.Label("Please select a profile");
return;
}
EditorGUILayout.Space();
EditorInstance.OnInspectorGUI();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Build!", GUILayout.Height(50)))
MeatKit.DoBuild();
if (MeatKitCache.LastBuildTime != default(DateTime))
GUILayout.Label("Last build: " + MeatKitCache.LastBuildTime + " (" +
MeatKitCache.LastBuildDuration.GetReadableTimespan() + ")");
else GUILayout.Label("Last build: Never");
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0a4b91e470df48f8be84b62e7c91d4a7
timeCreated: 1639092690
@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace MeatKit
{
public interface IValidatable
{
Dictionary<string, BuildMessage> Validate();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f1c893eea8264e16a0b37abe9c2d6861
timeCreated: 1636270330
@@ -0,0 +1,12 @@
using System;
namespace MeatKit
{
public class MeatKitBuildException : Exception
{
public MeatKitBuildException(string message, Exception innerException) : base(message, innerException)
{
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9dd595cdd27a46049e3d8005658214de
timeCreated: 1641928037