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
+126
View File
@@ -0,0 +1,126 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace MeatKit
{
public static partial class MeatKit
{
private const string EditorAssemblyPath = "Library/ScriptAssemblies/";
public const string BundleOutputPath = "AssetBundles/";
private static void ExportEditorAssembly(string folder)
{
if (!File.Exists(EditorAssemblyPath + AssemblyName + ".dll")) return;
// Delete the old file
var settings = BuildWindow.SelectedProfile;
var exportPath = folder + settings.PackageName + ".dll";
if (File.Exists(exportPath)) File.Delete(exportPath);
var rParams = new ReaderParameters
{
AssemblyResolver =
new RedirectedAssemblyResolver(Path.GetDirectoryName(typeof(UnityEngine.Object).Assembly.Location))
};
// Get the MeatKitPlugin class and rename it
string tempFile = Path.GetTempFileName();
File.Copy(EditorAssemblyPath + AssemblyName + ".dll", tempFile, true);
using (var asm = AssemblyDefinition.ReadAssembly(tempFile, rParams))
{
var plugin = asm.MainModule.GetType("MeatKitPlugin");
plugin.Name = settings.PackageName + "Plugin";
// This is some quantum bullshit.
// If you don't enumerate the constructor arguments for attributes their values aren't updated correctly.
foreach (var x in GetAllCustomAttributes(asm).SelectMany(a => a.ConstructorArguments))
{
}
// Get the BepInPlugin attribute and replace the values in it with our own
var str = asm.MainModule.TypeSystem.String;
var guid = settings.Author + "." + settings.PackageName;
var pluginAttribute = plugin.CustomAttributes.First(a => a.AttributeType.Name == "BepInPlugin");
pluginAttribute.ConstructorArguments[0] = new CustomAttributeArgument(str, guid);
pluginAttribute.ConstructorArguments[1] = new CustomAttributeArgument(str, settings.PackageName);
pluginAttribute.ConstructorArguments[2] = new CustomAttributeArgument(str, settings.Version);
// Get the LoadAssets method and make a new body for it
var loadAssetsMethod = plugin.Methods.First(m => m.Name == "LoadAssets");
loadAssetsMethod.Body = new MethodBody(loadAssetsMethod);
var il = loadAssetsMethod.Body.GetILProcessor();
// Let any build items insert their own code in here
foreach (var item in settings.BuildItems)
item.GenerateLoadAssets(plugin, il);
// Insert a ret at the end so it's valid
il.Emit(OpCodes.Ret);
// Module name needs to be changed away from Assembly-CSharp.dll because it is a reserved name.
asm.Name = new AssemblyNameDefinition(settings.PackageName, asm.Name.Version);
asm.MainModule.Name = settings.PackageName + ".dll";
// References to renamed unity code must be swapped out.
foreach (var ii in asm.MainModule.AssemblyReferences)
switch (ii.Name)
{
case AssemblyRename:
ii.Name = AssemblyName;
break;
case AssemblyFirstpassRename:
ii.Name = AssemblyFirstpassName;
break;
}
if (BuildWindow.SelectedProfile.StripNamespaces)
{
// Remove types not in an allowed namespace or the global namespace
string[] allowedNamespaces = BuildWindow.SelectedProfile.GetAllAllowedNamespaces();
List<TypeDefinition> typesToRemove = new List<TypeDefinition>();
foreach (var type in asm.MainModule.Types)
{
if (type.Namespace == "" || allowedNamespaces.Any(x => type.Namespace.Contains(x)))
continue;
typesToRemove.Add(type);
}
foreach (var type in typesToRemove) asm.MainModule.Types.Remove(type);
}
// Remove the same types we didn't want to import. This cannot be skipped.
foreach (var type in StripAssemblyTypes
.Select(x => asm.MainModule.GetType(x))
.Where(x => x != null))
asm.MainModule.Types.Remove(type);
try
{
// Save it
asm.Write(exportPath);
}
catch (ArgumentException e)
{
throw new MeatKitBuildException("Unable to write exported scripts file. This is likely due to namespace stripping being enabled and a required namespace is not whitelisted.", e);
}
}
// Delete temp file now that we're done.
File.Delete(tempFile);
}
private static IEnumerable<CustomAttribute> GetAllCustomAttributes(AssemblyDefinition asm)
{
foreach (var type in asm.MainModule.Types)
{
foreach (var attrib in type.CustomAttributes) yield return attrib;
foreach (CustomAttribute attrib in type.Methods.SelectMany(method => method.CustomAttributes))
yield return attrib;
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0c6f57f9c2dd40b8a7eabb4f85e2e283
timeCreated: 1617344435
+260
View File
@@ -0,0 +1,260 @@
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using Mono.Cecil;
using NStrip;
using UnityEditor;
using UnityEngine;
namespace MeatKit
{
/// <summary>
/// Assembly importer class to get the managed assemblies from the game into the Unity editor without
/// the editor wanting to crash itself. Original implementation by Nolenz.
/// https://github.com/WurstModders/WurstMod-Reloaded/blob/2e33e83284b3a9f39c8df210ad907925d1d7d9d8/WMRWorkbench/Assets/Editor/Manglers/AssemblyMangler.cs
/// </summary>
public static partial class MeatKit
{
private const string AssemblyName = "Assembly-CSharp";
private const string AssemblyRename = "H3VRCode-CSharp";
private const string AssemblyFirstpassName = "Assembly-CSharp-firstpass";
private const string AssemblyFirstpassRename = "H3VRCode-CSharp-passfirst";
// Types we want to strip from the main Unity assembly
private static readonly string[] StripAssemblyTypes =
{
// Alloy classes
"MaterialMapChannelPackerDefinition",
"Alloy.PackedMapDefinition",
"Alloy.BaseTextureChannelMapping",
"Alloy.MapChannel",
"Alloy.TextureValueChannelMode",
"Alloy.NormalMapChannelTextureChannelMapping",
"Alloy.TextureImportConfig",
"Alloy.MapTextureChannelMapping",
"AlloyUtils",
"Alloy.EnumExtension",
"MinValueAttribute",
"MaxValueAttribute",
"AlloyEffectsManager",
"Alloy.EnumFlagsAttribute",
// Bakery MonoBehaviours
"BakeryAlwaysRender",
"BakeryDirectLight",
"BakeryLightmapGroup",
"BakeryLightmapGroupSelector",
"BakeryLightmappedPrefab",
"BakeryLightMesh",
"BakeryPointLight",
"BakerySkyLight",
"BakeryVolume",
"BakeryVolumeReceiver",
"BakeryVolumeTrigger",
"BakeryProjectSettings",
"VolumeTestScene2",
"BakeryPackAsSingleSquare",
"BakerySector",
"BakerySectorCapture",
"ftGlobalStorage",
"ftLightmaps",
"ftLightmapsStorage",
"ftLocalStorage",
// Bakery supporting types
"ftUniqueIDRegistry",
"BakeryLightmapGroupPlain",
//Editor Tool Scripts
"IconCamera"
};
// Array of the extra assemblies that need to come with the main Unity assemblies
private static readonly string[] ExtraAssemblies =
{
"DinoFracture.dll",
"ES2.dll"
};
private static void ImportAssemblies(string assembliesDirectory, string destinationDirectory)
{
// Remove whatever was there before and make the folder again
if (!Directory.Exists(destinationDirectory)) Directory.CreateDirectory(destinationDirectory);
// Load all of our modifiers
var editors = Extensions.GetAllInstances<AssemblyModifier>();
foreach (var editor in editors) editor.Applied = false;
// We need a custom assembly resolver that sometimes points to different directories.
var rParams = new ReaderParameters
{
AssemblyResolver = new RedirectedAssemblyResolver(assembliesDirectory, destinationDirectory)
};
// Rename the game's firstpass assembly
{
var firstpassAssembly =
AssemblyDefinition.ReadAssembly(Path.Combine(assembliesDirectory, AssemblyFirstpassName + ".dll"));
firstpassAssembly.Name =
new AssemblyNameDefinition(AssemblyFirstpassRename, firstpassAssembly.Name.Version);
firstpassAssembly.MainModule.Name = AssemblyFirstpassRename + ".dll";
// Apply modifications
foreach (var editor in editors) editor.ApplyModification(firstpassAssembly);
// Publicize Assembly
AssemblyStripper.MakePublic(firstpassAssembly, new string[0], false, false);
firstpassAssembly.Write(Path.Combine(destinationDirectory, AssemblyFirstpassRename + ".dll"));
firstpassAssembly.Dispose();
}
// Main assembly
{
// Rename the main assembly
var mainAssembly =
AssemblyDefinition.ReadAssembly(Path.Combine(assembliesDirectory, AssemblyName + ".dll"), rParams);
mainAssembly.Name = new AssemblyNameDefinition(AssemblyRename, mainAssembly.Name.Version);
mainAssembly.MainModule.Name = AssemblyRename + ".dll";
// Change the firstpass reference in this assembly
mainAssembly.MainModule.AssemblyReferences
.First(x => x.Name == AssemblyFirstpassName)
.Name = AssemblyFirstpassRename;
// Strip some types from the assembly to prevent doubles in the editor
foreach (var typename in StripAssemblyTypes)
{
var type = mainAssembly.MainModule.GetType(typename);
if (type != null) mainAssembly.MainModule.Types.Remove(type);
else Debug.LogWarning("Type " + typename + " was not found in assembly.");
}
// Apply modifications
foreach (var editor in editors) editor.ApplyModification(mainAssembly);
// Publicize assembly
AssemblyStripper.MakePublic(mainAssembly, new string[0], false, false);
// Write the main assembly out into the destination folder and dispose it
mainAssembly.Write(Path.Combine(destinationDirectory, AssemblyRename + ".dll"));
}
// Then lastly copy the other assemblies to the destination folder
foreach (var file in ExtraAssemblies)
{
var path = Path.Combine(assembliesDirectory, file);
if (File.Exists(path))
ImportSingleAssembly(path, destinationDirectory);
}
// Check if anything didn't apply
foreach (var editor in editors)
if (!editor.Applied)
Debug.LogWarning(editor.name + " was not applied while importing.", editor);
// When we're done importing assemblies, let Unity refresh the asset database
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "H3VR_IMPORTED");
NormalizeMetaFileGUIDs();
}
private static void ImportSingleAssembly(string assemblyPath, string destinationDirectory)
{
var rParams = new ReaderParameters
{
AssemblyResolver =
new RedirectedAssemblyResolver(Path.GetDirectoryName(assemblyPath), destinationDirectory)
};
// We just want to rename the references to the game's assemblies here
var asm = AssemblyDefinition.ReadAssembly(assemblyPath, rParams);
foreach (var reference in asm.MainModule.AssemblyReferences)
switch (reference.Name)
{
case AssemblyName:
reference.Name = AssemblyRename;
break;
case AssemblyFirstpassName:
reference.Name = AssemblyFirstpassRename;
break;
}
asm.Write(Path.Combine(destinationDirectory, Path.GetFileName(assemblyPath)));
NormalizeMetaFileGUIDs();
}
private static void NormalizeMetaFileGUIDs()
{
// This is a really important step. We need to make sure that the meta files for the assemblies are generated
// WITH THE SAME GUIDs each time. Otherwise, if you lose one and didn't have a backup, all your scripts will be missing
// and that is of course no bueno. Unity expects 32 hexadecimal digits for the guid so we'll use md5.
// We need every meta file to exist already.
AssetDatabase.Refresh();
var hashFunction = MD5.Create();
var replaceWith = new Regex(@"^guid: [0-9a-f]{32}$", RegexOptions.Multiline);
foreach (var metaFile in Directory.GetFiles(ManagedDirectory, "*.meta"))
{
// First we get the hash
var assemblyName = Path.GetFileName(metaFile.Substring(0, metaFile.Length - 5));
var hash = hashFunction.ComputeHash(Encoding.UTF8.GetBytes(assemblyName));
var hexHash = Extensions.ByteArrayToString(hash).ToLower();
// Then we need to replace the hash in the meta file with it.
var metaText = File.ReadAllText(metaFile);
metaText = replaceWith.Replace(metaText, "guid: " + hexHash);
File.WriteAllText(metaFile, metaText);
}
// If anything was changed we need Unity to apply it immediately.
AssetDatabase.Refresh();
}
/// <summary>
/// Assembly resolver that redirects references to another path if not found.
/// </summary>
private class RedirectedAssemblyResolver : BaseAssemblyResolver
{
private readonly DefaultAssemblyResolver _defaultResolver = new DefaultAssemblyResolver();
private readonly string[] _redirectPaths;
public RedirectedAssemblyResolver(params string[] redirectPath)
{
_redirectPaths = redirectPath;
}
public override AssemblyDefinition Resolve(AssemblyNameReference name)
{
AssemblyDefinition asm = null;
try
{
asm = _defaultResolver.Resolve(name);
}
catch (AssemblyResolutionException)
{
foreach (var path in _redirectPaths)
try
{
var asmPath = Path.Combine(path, name.Name + ".dll");
Debug.Log("Assembly path: " + asmPath);
if (File.Exists(asmPath))
asm = AssemblyDefinition.ReadAssembly(asmPath,
new ReaderParameters {AssemblyResolver = this});
}
catch (AssemblyResolutionException)
{
// Ignored
}
}
if (asm != null) return asm;
throw new AssemblyResolutionException(name);
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4d2f661647864f0689e2c38c7c687c99
timeCreated: 1617325184
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 49df45b608024e5eb33594ddd1200d89
timeCreated: 1628213725
@@ -0,0 +1,14 @@
using System;
using Mono.Cecil;
using UnityEngine;
namespace MeatKit
{
public abstract class AssemblyModifier : ScriptableObject
{
[NonSerialized]
public bool Applied = false;
public abstract void ApplyModification(AssemblyDefinition assembly);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c7e21420a1d84ca5947b2fd9eb9db6d8
timeCreated: 1628213934
@@ -0,0 +1,50 @@
using System;
using Mono.Cecil;
using UnityEngine;
namespace MeatKit
{
[CreateAssetMenu(menuName = "MeatKit/Assembly Editors/Enum", fileName = "New Enum Editor")]
public class EnumModifier : AssemblyModifier
{
private const FieldAttributes Attributes =
FieldAttributes.Static | FieldAttributes.Literal | FieldAttributes.Public | FieldAttributes.HasDefault;
[Tooltip("Specify the FULL NAME of the enum you want to change. e.g. Sub.Namespace.Type")]
public string EnumName = "FistVR.FireArmRoundType";
[Tooltip("The new values you want to add to this enum")]
public EnumValue[] AddedValues = new EnumValue[0];
public override void ApplyModification(AssemblyDefinition assembly)
{
// Try to get this type from the assembly. If it doesn't exist, we can just skip.
TypeReference type = assembly.MainModule.GetType(EnumName);
if (type == null) return;
var definition = type.Resolve();
if (!definition.IsEnum)
{
Debug.LogError(EnumName + " is not an enum type!", this);
Applied = true;
return;
}
// Add the new enum value to the type
foreach (var value in AddedValues)
definition.Fields.Add(new FieldDefinition(value.Name, Attributes, definition) {Constant = value.Value});
Applied = true;
}
[Serializable]
public struct EnumValue
{
[Tooltip("The name of the new enum value")]
public string Name;
[Tooltip("The new enum value")]
public int Value;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 486e27304e8449efb6c6136482952c4d
timeCreated: 1628213734
@@ -0,0 +1,62 @@
using System;
using System.Linq;
using UnityEditor;
namespace MeatKit
{
[CustomEditor(typeof(EnumModifier))]
public class EnumModifierEditor : Editor
{
private static readonly string[] CommonTypes =
{
"FistVR.FireArmRoundType",
"FistVR.FireArmRoundClass",
"FistVR.FireArmMagazineType",
"FistVR.ItemSpawnerObjectDefinition.ItemSpawnerCategory",
"FistVR.SosigEnemyID"
};
private SerializedProperty _addedValues;
private EnumModifier _enumModifier;
private bool _isCustomType;
private int _selectedType;
// Called when an object of this type is selected
private void OnEnable()
{
// Get our properties and check if this is a common type
_addedValues = serializedObject.FindProperty("AddedValues");
_enumModifier = (EnumModifier) target;
_selectedType = Array.IndexOf(CommonTypes, _enumModifier.EnumName);
_isCustomType = _selectedType == -1 || string.IsNullOrEmpty(_enumModifier.EnumName);
}
// Called to draw the inspector GUI
public override void OnInspectorGUI()
{
// I'll be real I have no idea what this does but the Unity docs had it so I'm not gonna mess with it
serializedObject.Update();
// Use a toggle (checkbox) to determine if we're using a custom type or a commonly used one from the array
_isCustomType = EditorGUILayout.Toggle("Custom type", _isCustomType);
if (_isCustomType)
_enumModifier.EnumName = EditorGUILayout.TextField("Enum name", _enumModifier.EnumName);
else
{
if (_selectedType < 0 || _selectedType >= CommonTypes.Length) _selectedType = 0;
_selectedType = EditorGUILayout.Popup("Type", _selectedType, CommonTypes);
_enumModifier.EnumName = CommonTypes[_selectedType];
}
// Draw the values field and then save the object
EditorGUILayout.PropertyField(_addedValues, true);
serializedObject.ApplyModifiedProperties();
// Suggest to the user that all added enums should be negative
if (_enumModifier.AddedValues.Any(x => x.Value >= 0))
EditorGUILayout.HelpBox(
"Your added enum values should be negative to avoid conflicts with vanilla items.",
MessageType.Warning);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4980597d341c4eed913ae30680f3f9f3
timeCreated: 1628218472
+138
View File
@@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Ionic.Zip;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace MeatKit
{
public partial class MeatKit
{
public static void DoBuild()
{
try
{
DoBuildInternal();
}
catch (MeatKitBuildException e)
{
string message = e.Message;
if (e.InnerException != null) message += "\n\n" + e.InnerException.Message;
EditorUtility.DisplayDialog("Build failed", message, "Ok.");
}
catch (Exception e)
{
EditorUtility.DisplayDialog("Build failed with unknown error",
"Error message: " + e.Message + "\n\nCheck console for full exception text.", "Ok.");
Debug.LogException(e);
}
}
private static void DoBuildInternal()
{
// Make sure the scripts are imported.
if (ShowErrorIfH3VRNotImported()) return;
// Get our profile and make sure it isn't null
BuildProfile profile = BuildWindow.SelectedProfile;
if (!profile) return;
// Start a stopwatch to time the build
Stopwatch sw = Stopwatch.StartNew();
// If there's anything invalid in the settings don't continue
if (!profile.EnsureValidForEditor()) return;
// Clean the output folder
CleanBuild();
// And export the assembly to the folder
ExportEditorAssembly(BundleOutputPath);
// Then get their asset bundle configurations
var bundles = profile.BuildItems.SelectMany(x => x.ConfigureBuild()).ToArray();
BuildPipeline.BuildAssetBundles(BundleOutputPath, bundles, BuildAssetBundleOptions.None,
BuildTarget.StandaloneWindows64);
// Cleanup the unused files created with building the bundles
foreach (var file in Directory.GetFiles(BundleOutputPath, "*.manifest"))
File.Delete(file);
File.Delete(Path.Combine(BundleOutputPath, "AssetBundles"));
// With the bundles done building we can process them
var replaceMap = new Dictionary<string, string>
{
{"Assembly-CSharp.dll", profile.PackageName + ".dll"},
{"Assembly-CSharp-firstpass.dll", profile.PackageName + "-firstpass.dll"},
{"H3VRCode-CSharp.dll", "Assembly-CSharp.dll"},
{"H3VRCode-CSharp-firstpass.dll", "Assembly-CSharp-firstpass.dll"}
};
foreach (var bundle in bundles)
{
var path = Path.Combine(BundleOutputPath, bundle.assetBundleName);
ProcessBundle(path, path, replaceMap, profile.BundleCompressionType);
}
// Now we can write the Thunderstore stuff to the folder
profile.WriteThunderstoreManifest(BundleOutputPath + "manifest.json");
// Check if the icon is already 256x256
Texture2D icon = profile.Icon;
// Make sure our icon is marked as readable
var importSettings = (TextureImporter) AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(profile.Icon));
if (!importSettings.isReadable ||
importSettings.textureCompression != TextureImporterCompression.Uncompressed)
{
importSettings.isReadable = true;
importSettings.textureCompression = TextureImporterCompression.Uncompressed;
importSettings.SaveAndReimport();
}
if (profile.Icon.width != 256 || profile.Icon.height != 256)
{
// Resize it for the build
icon = icon.ScaleTexture(256, 256);
}
// Write the texture to file
File.WriteAllBytes(BundleOutputPath + "icon.png", icon.EncodeToPNG());
// Copy the readme
File.Copy(AssetDatabase.GetAssetPath(profile.ReadMe), BundleOutputPath + "README.md");
string packageName = profile.Author + "-" + profile.PackageName;
if (profile.BuildAction == BuildAction.CopyToProfile)
{
string pluginFolder = Path.Combine(profile.OutputProfile, "BepInEx/plugins/" + packageName);
if (Directory.Exists(pluginFolder)) Directory.Delete(pluginFolder, true);
Directory.CreateDirectory(pluginFolder);
Extensions.CopyFilesRecursively(BundleOutputPath, pluginFolder);
}
else if (profile.BuildAction == BuildAction.CreateThunderstorePackage)
{
using (var zip = new ZipFile())
{
zip.AddDirectory(BundleOutputPath, "");
zip.Save(Path.Combine(BundleOutputPath, packageName + ".zip"));
}
}
// End the stopwatch and save the time
MeatKitCache.LastBuildDuration = sw.Elapsed;
MeatKitCache.LastBuildTime = DateTime.Now;
}
public static void CleanBuild()
{
if (Directory.Exists(BundleOutputPath)) Directory.Delete(BundleOutputPath, true);
Directory.CreateDirectory(BundleOutputPath);
}
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 918df2720ddc4655b198835c566d115a
timeCreated: 1639093595
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 70638cf8eda1487ba7a232bb4377a23c
folderAsset: yes
timeCreated: 1635003658
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
@@ -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
+85
View File
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.IO;
using AssetsTools.NET;
using AssetsTools.NET.Extra;
using UnityEditor;
namespace MeatKit
{
public static partial class MeatKit
{
private static void ProcessBundle(string source, string destination, IDictionary<string, string> replaceMap,
AssetBundleCompressionType recompressAs)
{
try
{
// Step 1: Load the asset bundle.
EditorUtility.DisplayProgressBar("Processing bundle", "Loading asset bundle...", 0f);
var am = new AssetsManager();
var bundle = am.LoadBundleFile(source);
var assets = am.LoadAssetsFileFromBundle(bundle, 0);
// Step 2: For each MonoScript asset, alter it's assembly name
EditorUtility.DisplayProgressBar("Processing bundle", "Modifying assets...", 0.33f);
var modifications = new List<AssetsReplacer>();
foreach (AssetFileInfoEx assetInfo in assets.table.assetFileInfo)
{
// We only want MonoScripts (type 115)
if (assetInfo.curFileType != 115) continue;
// Get the field for this asset
var field = am.GetTypeInstance(assets, assetInfo).GetBaseField();
var assemblyNameValue = field["m_AssemblyName"].GetValue();
// Check if we want to replace this name
var asmName = assemblyNameValue.AsString();
if (replaceMap.ContainsKey(asmName))
{
// Modify it's assembly name
assemblyNameValue.Set(replaceMap[asmName]);
// Write the modifications to the list
var newBytes = field.WriteToByteArray();
modifications.Add(new AssetsReplacerFromMemory(0, assetInfo.index, (int) assetInfo.curFileType,
0xFFFF, newBytes));
}
}
// Step 3: Write the modified assets back into an uncompressed bundle
EditorUtility.DisplayProgressBar("Processing bundle", "Saving changes...", 0.66f);
using (var fileStream = new FileStream(destination + ".uncompressed", FileMode.Create))
{
var bunRepl = new BundleReplacerFromAssets(assets.name, assets.name, assets.file, modifications);
var bunWriter = new AssetsFileWriter(fileStream);
bundle.file.Write(bunWriter, new List<BundleReplacer> {bunRepl});
}
// Unload the existing bundle
am.UnloadAll();
// Step 4: Re-compress the bundle if requested
EditorUtility.DisplayProgressBar("Processing bundle", "Recompressing bundle...", 1f);
if (recompressAs != AssetBundleCompressionType.NONE)
{
var compressedBundle = am.LoadBundleFile(destination + ".uncompressed");
using (var fs = File.OpenWrite(destination))
using (var writer = new AssetsFileWriter(fs))
{
compressedBundle.file.Pack(compressedBundle.file.reader, writer, recompressAs);
}
am.UnloadAll();
File.Delete(destination + ".uncompressed");
}
else File.Move(destination + ".uncompressed", destination);
}
finally
{
EditorUtility.ClearProgressBar();
GC.Collect();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 69efae90f6bb4c3eadcd9e7c4af6ec67
timeCreated: 1617337071
+192
View File
@@ -0,0 +1,192 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace MeatKit
{
public static class Extensions
{
public static Type[] GetTypesSafe(this Assembly assembly)
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
return e.Types.Where(t => t != null).ToArray();
}
}
public static string ByteArrayToString(byte[] ba)
{
var hex = new StringBuilder(ba.Length * 2);
foreach (var b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
// Modified version of http://answers.unity.com/answers/1425776/view.html
public static T[] GetAllInstances<T>() where T : ScriptableObject
{
return AssetDatabase.FindAssets("t:" + typeof(T).FullName)
.Select(AssetDatabase.GUIDToAssetPath)
.Select(AssetDatabase.LoadAssetAtPath<T>)
.ToArray();
}
public static Object[] GetAllInstances(Type t)
{
return AssetDatabase.FindAssets("t:" + t.FullName)
.Select(AssetDatabase.GUIDToAssetPath)
.Select(p => AssetDatabase.LoadAssetAtPath(p, t))
.ToArray();
}
// https://stackoverflow.com/a/25223884
public static string MakeValidFileName(string text, char? replacement = '_')
{
if (string.IsNullOrEmpty(text)) return "";
var invalids = Path.GetInvalidFileNameChars();
var sb = new StringBuilder(text.Length);
var changed = false;
for (var i = 0; i < text.Length; i++)
{
var c = text[i];
if (invalids.Contains(c))
{
changed = true;
var repl = replacement ?? '\0';
if (repl != '\0')
sb.Append(repl);
}
else
sb.Append(c);
}
if (sb.Length == 0)
return "_";
return changed ? sb.ToString() : text;
}
// https://answers.unity.com/questions/150942/texture-scale.html
public static Texture2D ScaleTexture(this Texture2D source, int targetWidth, int targetHeight)
{
Texture2D result = new Texture2D(targetWidth, targetHeight, TextureFormat.ARGB32, false);
for (int i = 0; i < result.height; ++i)
{
for (int j = 0; j < result.width; ++j)
{
Color newColor = source.GetPixelBilinear(j / (float) result.width, i / (float) result.height);
result.SetPixel(j, i, newColor);
}
}
result.Apply();
return result;
}
public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target) {
foreach (DirectoryInfo dir in source.GetDirectories())
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
foreach (FileInfo file in source.GetFiles())
file.CopyTo(Path.Combine(target.FullName, file.Name));
}
public static void CopyFilesRecursively(string source, string target)
{
CopyFilesRecursively(new DirectoryInfo(source), new DirectoryInfo(target));
}
#region TimeSpan formatting https://stackoverflow.com/a/21649465/8809017
public static string GetReadableTimespan(this TimeSpan ts)
{
// formats and its cutoffs based on totalseconds
var cutoff = new SortedList<long, string>
{
{59, "{3:S}"},
{60, "{2:M}"},
{60 * 60 - 1, "{2:M}, {3:S}"},
{60 * 60, "{1:H}"},
{24 * 60 * 60 - 1, "{1:H}, {2:M}"},
{24 * 60 * 60, "{0:D}"},
{long.MaxValue, "{0:D}, {1:H}"}
};
// find nearest best match
var find = cutoff.Keys.ToList()
.BinarySearch((long) ts.TotalSeconds);
// negative values indicate a nearest match
var near = find < 0 ? Math.Abs(find) - 1 : find;
// use custom formatter to get the string
return String.Format(
new HMSFormatter(),
cutoff[cutoff.Keys[near]],
ts.Days,
ts.Hours,
ts.Minutes,
ts.Seconds);
}
// formatter for forms of
// seconds/hours/day
private class HMSFormatter : ICustomFormatter, IFormatProvider
{
// list of Formats, with a P customformat for pluralization
static Dictionary<string, string> timeformats = new Dictionary<string, string>
{
{"S", "{0:P:s:s}"},
{"M", "{0:P:m:m}"},
{"H", "{0:P:h:h}"},
{"D", "{0:P:d:d}"}
};
public string Format(string format, object arg, IFormatProvider formatProvider)
{
return String.Format(new PluralFormatter(), timeformats[format], arg);
}
public object GetFormat(Type formatType)
{
return formatType == typeof(ICustomFormatter) ? this : null;
}
}
// formats a numeric value based on a format P:Plural:Singular
private class PluralFormatter : ICustomFormatter, IFormatProvider
{
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if (arg != null)
{
var parts = format.Split(':'); // ["P", "Plural", "Singular"]
if (parts[0] == "P") // correct format?
{
// which index postion to use
int partIndex = (arg.ToString() == "1") ? 2 : 1;
// pick string (safe guard for array bounds) and format
return String.Format("{0}{1}", arg, (parts.Length > partIndex ? parts[partIndex] : ""));
}
}
return String.Format(format, arg);
}
public object GetFormat(Type formatType)
{
return formatType == typeof(ICustomFormatter) ? this : null;
}
}
#endregion
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d928d818e3c24bce8c1c938e971da916
timeCreated: 1628577239
+113
View File
@@ -0,0 +1,113 @@
using System.Collections.Generic;
using System.IO;
using AssetsTools.NET;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace MeatKit
{
public static partial class MeatKit
{
private static readonly string ManagedDirectory = Path.Combine(Application.dataPath, "MeatKit/Managed/");
private static bool ShowErrorIfH3VRNotImported()
{
#if (H3VR_IMPORTED == false)
EditorUtility.DisplayDialog("Cannot continue.", "You don't have the H3 scripts imported. Please do that before trying to export anything.", "Ok");
return true;
#endif
return false;
}
[MenuItem("MeatKit/Scripts/Import Game", priority = 0)]
public static void ImportAssemblies()
{
// If the path has never been set, or no longer exists, prompt the user to find it again
var gameManagedLocation = MeatKitCache.GameManagedLocation;
if (string.IsNullOrEmpty(gameManagedLocation) || !Directory.Exists(gameManagedLocation))
{
gameManagedLocation = EditorUtility.OpenFolderPanel("Select H3VR Managed directory", string.Empty, "Managed");
MeatKitCache.GameManagedLocation = gameManagedLocation;
}
// If it's _still_ empty, the user must have cancelled.
if (string.IsNullOrEmpty(gameManagedLocation)) return;
ImportAssemblies(gameManagedLocation, ManagedDirectory);
}
[MenuItem("MeatKit/Scripts/Import Single", priority = 0)]
public static void ImportSingleAssembly()
{
var assemblyLocation =
EditorUtility.OpenFilePanel("Select assembly", null, "dll");
if (string.IsNullOrEmpty(assemblyLocation)) return;
MeatKitCache.LastImportedAssembly = assemblyLocation;
ImportSingleAssembly(assemblyLocation, ManagedDirectory);
Debug.Log("Finished importing " + assemblyLocation);
}
[MenuItem("MeatKit/Scripts/Re-Import Last", priority = 0)]
public static void ReimportLast()
{
if (string.IsNullOrEmpty(MeatKitCache.LastImportedAssembly))
{
Debug.Log("Nothing to re-import.");
return;
}
ImportSingleAssembly(MeatKitCache.LastImportedAssembly, ManagedDirectory);
Debug.Log("Re-imported " + MeatKitCache.LastImportedAssembly);
}
[MenuItem("MeatKit/Scripts/Export", priority = 0)]
public static void ExportEditorScripts()
{
// Make sure the scripts are imported and there are no errors before exporting
if (ShowErrorIfH3VRNotImported()) return;
if (!BuildWindow.SelectedProfile.EnsureValidForEditor()) return;
ExportEditorAssembly(BundleOutputPath);
}
[MenuItem("MeatKit/Asset Bundle/Export", priority = 1)]
public static void ExportBundle()
{
var assetBundlePath = EditorUtility.OpenFilePanel("Select asset bundle", Application.dataPath, "");
var settings = BuildWindow.SelectedProfile;
var replaceMap = new Dictionary<string, string>
{
{"Assembly-CSharp.dll", settings.PackageName + ".dll"},
{"Assembly-CSharp-firstpass.dll", settings.PackageName + "-firstpass.dll"},
{"H3VRCode-CSharp.dll", "Assembly-CSharp.dll"},
{"H3VRCode-CSharp-firstpass.dll", "Assembly-CSharp-firstpass.dll"}
};
ProcessBundle(assetBundlePath, assetBundlePath, replaceMap, AssetBundleCompressionType.LZ4);
}
[MenuItem("MeatKit/Asset Bundle/Import", priority = 1)]
public static void ImportBundle()
{
var assetBundlePath = EditorUtility.OpenFilePanel("Select asset bundle", Application.dataPath, "");
var replaceMap = new Dictionary<string, string>
{
{"Assembly-CSharp.dll", "H3VRCode-CSharp.dll"},
{"Assembly-CSharp-firstpass.dll", "H3VRCode-CSharp-firstpass.dll"}
};
ProcessBundle(assetBundlePath, assetBundlePath + "-imported", replaceMap, AssetBundleCompressionType.NONE);
}
public static void ClearCache()
{
AssetDatabase.SaveAssets();
if (Directory.Exists(ManagedDirectory))
Directory.Delete(ManagedDirectory, true);
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "");
AssetDatabase.Refresh();
}
}
}
+12
View File
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: dc821cd525214a745a8b5ee5624f98c6
timeCreated: 1617325095
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+112
View File
@@ -0,0 +1,112 @@
using System;
using System.Globalization;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace MeatKit
{
[Serializable]
public class MeatKitCache
{
private const string CacheFileName = "meatkit.json";
[SerializeField]
private string _lastBuildTime = default(DateTime).ToString(CultureInfo.InvariantCulture);
[SerializeField]
private string _lastBuildDuration = default(TimeSpan).ToString();
[SerializeField]
private string _gameManagedLocation;
[SerializeField]
private string _lastImportedAssembly;
[SerializeField]
private string _lastSelectedProfileGuid;
private static string CacheFilePath
{
get { return Path.Combine(Path.GetDirectoryName(Application.dataPath), CacheFileName); }
}
private static MeatKitCache _instance;
private static MeatKitCache Instance
{
get
{
if (_instance != null) return _instance;
if (!File.Exists(CacheFilePath))
{
_instance = new MeatKitCache();
WriteCache();
}
else _instance = JsonUtility.FromJson<MeatKitCache>(File.ReadAllText(CacheFileName));
return _instance;
}
}
private static void WriteCache()
{
File.WriteAllText(CacheFilePath, JsonUtility.ToJson(_instance));
}
public static DateTime LastBuildTime
{
get { return DateTime.Parse(Instance._lastBuildTime); }
set
{
Instance._lastBuildTime = value.ToString(CultureInfo.InvariantCulture);
WriteCache();
}
}
public static TimeSpan LastBuildDuration
{
get { return TimeSpan.Parse(Instance._lastBuildDuration); }
set
{
Instance._lastBuildDuration = value.ToString();
WriteCache();
}
}
public static string GameManagedLocation
{
get { return Instance._gameManagedLocation; }
set
{
Instance._gameManagedLocation = value;
WriteCache();
}
}
public static string LastImportedAssembly
{
get { return Instance._lastImportedAssembly; }
set
{
Instance._lastImportedAssembly = value;
WriteCache();
}
}
public static BuildProfile LastSelectedProfile
{
get
{
if (string.IsNullOrEmpty(Instance._lastSelectedProfileGuid)) return null;
var path = AssetDatabase.GUIDToAssetPath(Instance._lastSelectedProfileGuid);
return string.IsNullOrEmpty(path) ? null : AssetDatabase.LoadAssetAtPath<BuildProfile>(path);
}
set
{
if (value == null) Instance._lastSelectedProfileGuid = "";
Instance._lastSelectedProfileGuid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(value));
WriteCache();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0dd6818cfc454c2a881d31442b37269e
timeCreated: 1638838073