update MeatKit (9a1a68ab68cd0650227af944ffa30d1166b9e056)

This commit is contained in:
msk
2023-07-26 16:45:05 -07:00
parent 920875f56b
commit d2316bac96
266 changed files with 2855 additions and 9187 deletions
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine.Networking;
namespace MeatKit
{
[InitializeOnLoad]
public static class AsyncDownloader
{
private static readonly Dictionary<UnityWebRequest, Action<UnityWebRequest>> ActiveRequests = new Dictionary<UnityWebRequest, Action<UnityWebRequest>>();
static AsyncDownloader()
{
EditorApplication.update += Update;
}
public static void WaitForCompletion(UnityWebRequest request, Action<UnityWebRequest> callback)
{
if (request == null) throw new ArgumentException("Request cannot be null", "request");
request.Send();
ActiveRequests.Add(request, callback);
}
private static void Update()
{
foreach (var kv in ActiveRequests.ToArray())
{
UnityWebRequest request = kv.Key;
Action<UnityWebRequest> callback = kv.Value;
if (!request.isDone) continue;
if (callback != null) callback(request);
ActiveRequests.Remove(request);
}
}
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bd989610b12e49edbdc6f03afc4b598f
timeCreated: 1684959395
@@ -0,0 +1,29 @@
using System.IO;
using UnityEditor;
using UnityEngine;
namespace MeatKit
{
public static class CreateUpdatePackage
{
private static readonly string[] ExportAssets =
{
"Assets/MeatKit",
"Assets/Managed/0Harmony.dll",
"Assets/Managed/BepInEx.dll",
"Assets/Managed/DotNetZip.dll",
"Assets/Managed/Mono.Cecil.dll",
"Assets/Managed/MonoMod.RuntimeDetour.dll",
"Assets/Managed/MonoMod.Utils.dll",
"Assets/Managed/Sodalite.dll",
"Assets/Managed/Valve.Newtonsoft.Json.dll",
};
[MenuItem("MeatKit/Developer/Create update package")]
public static void Create()
{
AssetDatabase.ExportPackage(ExportAssets, Updater.UpdatePackageName, ExportPackageOptions.Recurse);
Debug.Log("Exported an update package to " + Path.Combine(Path.GetDirectoryName(Application.dataPath) , Updater.UpdatePackageName));
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9075e2699d46423fbc049d02942b6f09
timeCreated: 1684962211
@@ -0,0 +1,138 @@
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace MeatKit
{
public class SimpleVersion : IComparable<SimpleVersion>
{
private const string RegexPattern = @"^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$";
public int Major { get; private set; }
public int Minor { get; private set; }
public int Patch { get; private set; }
public string Prerelease { get; private set; }
public string BuildMetadata { get; private set; }
public static SimpleVersion Parse(string version)
{
var match = Regex.Match(version, RegexPattern);
if (!match.Success) throw new ArgumentException("Provided version is not valid SemVer.", "version");
return new SimpleVersion
{
Major = int.Parse(match.Groups[1].Value),
Minor = int.Parse(match.Groups[2].Value),
Patch = int.Parse(match.Groups[3].Value),
Prerelease = match.Groups[4].Value,
BuildMetadata = match.Groups[5].Value,
};
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append(Major).Append(".").Append(Minor).Append(".").Append(Patch);
if (!string.IsNullOrEmpty(Prerelease)) sb.Append("-").Append(Prerelease);
if (!string.IsNullOrEmpty(BuildMetadata)) sb.Append("+").Append(BuildMetadata);
return sb.ToString();
}
public int CompareByPrecedence(SimpleVersion other)
{
if (other == null)
return 1;
var r = Major.CompareTo(other.Major);
if (r != 0) return r;
r = Minor.CompareTo(other.Minor);
if (r != 0) return r;
r = Patch.CompareTo(other.Patch);
if (r != 0) return r;
return CompareComponent(Prerelease, other.Prerelease, true);
}
private static int CompareComponent(string a, string b, bool nonemptyIsLower = false)
{
var aEmpty = string.IsNullOrEmpty(a);
var bEmpty = string.IsNullOrEmpty(b);
if (aEmpty && bEmpty)
return 0;
if (aEmpty)
return nonemptyIsLower ? 1 : -1;
if (bEmpty)
return nonemptyIsLower ? -1 : 1;
var aComps = a.Split('.');
var bComps = b.Split('.');
var minLen = Math.Min(aComps.Length, bComps.Length);
for (int i = 0; i < minLen; i++)
{
var ac = aComps[i];
var bc = bComps[i];
int aNum;
var aIsNum = int.TryParse(ac, out aNum);
int bNum;
var bIsNum = int.TryParse(bc, out bNum);
int r;
if (aIsNum && bIsNum)
{
r = aNum.CompareTo(bNum);
if (r != 0) return r;
}
else
{
if (aIsNum)
return -1;
if (bIsNum)
return 1;
r = string.CompareOrdinal(ac, bc);
if (r != 0)
return r;
}
}
return aComps.Length.CompareTo(bComps.Length);
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
if (ReferenceEquals(this, obj))
return true;
var other = (SimpleVersion)obj;
return Major == other.Major
&& Minor == other.Minor
&& Patch == other.Patch
&& string.Equals(Prerelease, other.Prerelease, StringComparison.Ordinal)
&& string.Equals(BuildMetadata, other.BuildMetadata, StringComparison.Ordinal);
}
public override int GetHashCode()
{
unchecked
{
int result = Major.GetHashCode();
result = result * 31 + Minor.GetHashCode();
result = result * 31 + Patch.GetHashCode();
result = result * 31 + Prerelease.GetHashCode();
result = result * 31 + BuildMetadata.GetHashCode();
return result;
}
}
public int CompareTo(SimpleVersion other)
{
return CompareByPrecedence(other);
}
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 06b3949a4ca64e09af4d019e93c4dd73
timeCreated: 1684948996
+123
View File
@@ -0,0 +1,123 @@
using System;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
using Valve.Newtonsoft.Json.Linq;
namespace MeatKit
{
public static class Updater
{
public const string UpdatePackageName = "MeatKitUpdate.unitypackage";
public const string UpdateUrl = "https://api.github.com/repos/H3VR-Modding/MeatKit/releases";
public static bool CheckingForUpdate { get; private set; }
private static bool AllowPreReleases { get; set; }
public static SimpleVersion _currentVersion;
public static SimpleVersion CurrentVersion
{
get
{
if (_currentVersion == null)
{
if (File.Exists("ProjectSettings/MeatKitVersion.txt"))
_currentVersion = SimpleVersion.Parse(File.ReadAllText("ProjectSettings/MeatKitVersion.txt"));
else
{
File.WriteAllText("ProjectSettings/MeatKitVersion.txt", "0.0.0");
_currentVersion = SimpleVersion.Parse("0.0.0");
}
}
return _currentVersion;
}
}
public static SimpleVersion OnlineVersion { get; private set; }
private static long OnlineReleaseId { get; set; }
public static void CheckForUpdate(bool allowPrerelease)
{
CheckingForUpdate = true;
AllowPreReleases = allowPrerelease;
var request = UnityWebRequest.Get(UpdateUrl);
AsyncDownloader.WaitForCompletion(request, UpdateCheckComplete);
}
private static void UpdateCheckComplete(UnityWebRequest request)
{
if (request.isError)
{
Debug.LogError("Error fetching releases for " + request.url + "\n" + request.error);
}
else
{
var response = JArray.Parse(request.downloadHandler.text);
var latestRelease = response.First(r => !r["prerelease"].Value<bool>() || AllowPreReleases);
OnlineVersion = SimpleVersion.Parse(latestRelease["tag_name"].Value<string>());
OnlineReleaseId = latestRelease["id"].Value<long>();
}
MeatKitCache.LastUpdateCheckTime = DateTime.Now;
CheckingForUpdate = false;
// Force a repaint on the update window if it's open
UpdaterEditorWindow window = EditorWindow.GetWindow<UpdaterEditorWindow>();
if (window) window.Repaint();
}
public static void StartUpdate()
{
if (OnlineVersion == null || OnlineReleaseId == 0) return;
// Try and fetch the assets on the release
AsyncDownloader.WaitForCompletion(UnityWebRequest.Get(UpdateUrl + "/" + OnlineReleaseId + "/assets"), CheckForUpdateAsset);
}
private static void CheckForUpdateAsset(UnityWebRequest request)
{
var response = JArray.Parse(request.downloadHandler.text);
// Check if any of the filenames are "MeatKitUpdate.unitypackage"
var updateAsset = response.FirstOrDefault(t => t["name"].Value<string>() == "MeatKitUpdate.unitypackage");
if (updateAsset == null)
{
EditorUtility.DisplayDialog("Failed to update", "Could not find the unity package associated with the target version. Nothing has been modified.", "Ok.");
return;
}
// Start a new request to download the package.
AsyncDownloader.WaitForCompletion(UnityWebRequest.Get(updateAsset["browser_download_url"].Value<string>()), ApplyUpdatePackage);
}
private static void ApplyUpdatePackage(UnityWebRequest request)
{
// Save the downloaded file to a temp location
string tempFile = Path.GetTempFileName();
File.WriteAllBytes(tempFile, request.downloadHandler.data);
// Wipe the MeatKit folder so it's fresh and ready for the new stuff
string dataDir = Path.Combine(Application.dataPath, "MeatKit");
Directory.Delete(dataDir, true);
File.Delete(dataDir + ".meta");
// Import all the files from it
AssetDatabase.ImportPackage(tempFile, false);
// Kick the asset database to refresh now that we're done
AssetDatabase.Refresh();
// Remove the temp file
File.Delete(tempFile);
// Update the saved version number
File.WriteAllText("ProjectSettings/MeatKitVersion.txt", OnlineVersion.ToString());
}
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 406166d3dc514f5bb884a2b40b183e26
timeCreated: 1684892343
@@ -0,0 +1,55 @@
using System;
using UnityEditor;
using UnityEngine;
namespace MeatKit
{
public class UpdaterEditorWindow : EditorWindow
{
private bool _allowPrerelease;
[MenuItem("MeatKit/Check for updates")]
public static void Open()
{
GetWindow<UpdaterEditorWindow>("MeatKit Updater").Show();
}
private void OnGUI()
{
EditorGUILayout.LabelField("MeatKit Updater", EditorStyles.boldLabel);
EditorGUILayout.LabelField("Installed version: " + Updater.CurrentVersion);
SimpleVersion onlineVersion = Updater.OnlineVersion;
if (onlineVersion == null) EditorGUILayout.LabelField("Online version: Unknown (check for updates)");
else EditorGUILayout.LabelField("Online version: " + onlineVersion);
if (MeatKitCache.LastUpdateCheckTime != default(DateTime))
GUILayout.Label("Last update check: " + MeatKitCache.LastUpdateCheckTime);
else GUILayout.Label("Last update check: Never");
if (!Updater.CheckingForUpdate)
{
if (GUILayout.Button("Check for updates"))
{
Updater.CheckForUpdate(_allowPrerelease);
}
_allowPrerelease = GUILayout.Toggle(_allowPrerelease, "Allow pre-release versions");
if (Updater.CurrentVersion.CompareTo(onlineVersion) < 0)
{
if (GUILayout.Button("Update to " + onlineVersion, GUILayout.Height(50)))
{
Updater.StartUpdate();
}
}
}
else
{
EditorGUI.BeginDisabledGroup(true);
GUILayout.Button("Checking...");
EditorGUI.EndDisabledGroup();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b54e300d191d47d4a2666733d6bd77e2
timeCreated: 1684891987