[Add] Hot Reload

This commit is contained in:
2026-02-27 03:16:18 +07:00
parent 5067cb51a1
commit b37579153b
431 changed files with 43054 additions and 1 deletions
@@ -0,0 +1,61 @@
using System;
using System.Globalization;
using SingularityGroup.HotReload.DTO;
using UnityEditor;
using UnityEditor.VSAttribution.HotReload;
using UnityEngine;
using UnityEngine.Analytics;
namespace SingularityGroup.HotReload.Editor {
internal static class Attribution {
internal const string LastLoginKey = "HotReload.Attribution.LastAttributionEventAt";
//Resend attribution event every 12 hours to be safe
static readonly TimeSpan resendPeriod = TimeSpan.FromHours(12);
//The last time the attribution event was sent.
//Returns unix epoch in case it has never been sent before.
static DateTime LastAttributionEventAt {
get {
if(EditorPrefs.HasKey(LastLoginKey)) {
return DateTime.ParseExact(EditorPrefs.GetString(LastLoginKey), "o", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
}
return DateTimeOffset.FromUnixTimeSeconds(0).UtcDateTime;
}
set {
EditorPrefs.SetString(LastLoginKey, value.ToUniversalTime().ToString("o"));
}
}
const string actionName = "Login";
const string partnerName = "The Naughty Cult Ltd.";
public static void RegisterLogin(LoginStatusResponse response) {
//Licensing might not be initialized yet.
//The hwId should be set eventually.
if(response?.hardwareId == null) {
return;
}
//Only forward attribution if this is an asset store build.
//We will still distribute this package outside of the asset store (i.e via our website).
if (!PackageConst.IsAssetStoreBuild) {
return;
}
var now = DateTime.UtcNow;
//If we sent an attribution event in the last 12 hours we should already be good.
if (now - LastAttributionEventAt < resendPeriod) {
return;
}
var result = VSAttribution.SendAttributionEvent(actionName, partnerName, response.hardwareId);
//Retry on transient errors
if (result == AnalyticsResult.NotInitialized) {
return;
}
LastAttributionEventAt = now;
}
}
}
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 67658aafb8404f0eb9496812ba4bb8a4
timeCreated: 1678721795
AssetOrigin:
serializedVersion: 1
productId: 254358
packageName: Hot Reload | Edit Code Without Compiling
packageVersion: 1.13.17
assetPath: Packages/com.singularitygroup.hotreload/Editor/Attribution/Attribution.cs
uploadId: 870414
@@ -0,0 +1,149 @@
using System;
using UnityEngine.Analytics;
#if UNITY_6000_0_OR_NEWER
namespace UnityEditor.VSAttribution.HotReload
{
internal static class VSAttribution
{
const int k_VersionId = 4;
const int k_MaxEventsPerHour = 10;
const int k_MaxNumberOfElements = 1000;
const string k_VendorKey = "unity.vsp-attribution";
const string k_EventName = "vspAttribution";
[Serializable]
private class VSAttributionData : IAnalytic.IData {
public string actionName;
public string partnerName;
public string customerUid;
public string extra;
}
[AnalyticInfo(k_EventName, k_VendorKey, k_VersionId, k_MaxEventsPerHour, k_MaxNumberOfElements)]
class VSAttributionAnalytics : IAnalytic {
private VSAttributionData m_Data;
private VSAttributionAnalytics(
string actionName,
string partnerName,
string customerUid,
string extra
) {
this.m_Data = new VSAttributionData {
actionName = actionName,
partnerName = partnerName,
customerUid = customerUid,
extra = extra
};
}
public bool TryGatherData(out IAnalytic.IData data, out Exception error) {
data = this.m_Data;
error = null;
return this.m_Data != null;
}
public static AnalyticsResult SendEvent(
string actionName,
string partnerName,
string customerUid,
string extra
) {
return EditorAnalytics.SendAnalytic(new VSAttributionAnalytics(actionName,
partnerName,
customerUid,
extra
));
}
}
/// <summary>
/// Registers and attempts to send a Verified Solutions Attribution event.
/// </summary>
/// <param name="actionName">Name of the action, identifying a place this event was called from.</param>
/// <param name="partnerName">Identifiable Verified Solutions Partner's name.</param>
/// <param name="customerUid">Unique identifier of the customer using Partner's Verified Solution.</param>
public static AnalyticsResult SendAttributionEvent(string actionName, string partnerName, string customerUid)
{
try
{
return VSAttributionAnalytics.SendEvent(actionName, partnerName, customerUid, "{}");
}
catch
{
// Fail silently
return AnalyticsResult.AnalyticsDisabled;
}
}
}
}
#else
namespace UnityEditor.VSAttribution.HotReload
{
internal static class VSAttribution
{
const int k_VersionId = 4;
const int k_MaxEventsPerHour = 10;
const int k_MaxNumberOfElements = 1000;
const string k_VendorKey = "unity.vsp-attribution";
const string k_EventName = "vspAttribution";
static bool RegisterEvent()
{
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour,
k_MaxNumberOfElements, k_VendorKey, k_VersionId);
var isResultOk = result == AnalyticsResult.Ok;
return isResultOk;
}
[Serializable]
struct VSAttributionData
{
public string actionName;
public string partnerName;
public string customerUid;
public string extra;
}
/// <summary>
/// Registers and attempts to send a Verified Solutions Attribution event.
/// </summary>
/// <param name="actionName">Name of the action, identifying a place this event was called from.</param>
/// <param name="partnerName">Identifiable Verified Solutions Partner's name.</param>
/// <param name="customerUid">Unique identifier of the customer using Partner's Verified Solution.</param>
public static AnalyticsResult SendAttributionEvent(string actionName, string partnerName, string customerUid)
{
try
{
// Are Editor Analytics enabled ? (Preferences)
if (!EditorAnalytics.enabled)
return AnalyticsResult.AnalyticsDisabled;
if (!RegisterEvent())
return AnalyticsResult.InvalidData;
// Create an expected data object
var eventData = new VSAttributionData
{
actionName = actionName,
partnerName = partnerName,
customerUid = customerUid,
extra = "{}"
};
return EditorAnalytics.SendEventWithLimit(k_EventName, eventData, k_VersionId);
}
catch
{
// Fail silently
return AnalyticsResult.AnalyticsDisabled;
}
}
}
}
#endif
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: d7493a30e78d4ec783ead20baea2c4d2
timeCreated: 1678721534
AssetOrigin:
serializedVersion: 1
productId: 254358
packageName: Hot Reload | Edit Code Without Compiling
packageVersion: 1.13.17
assetPath: Packages/com.singularitygroup.hotreload/Editor/Attribution/VSAttribution.cs
uploadId: 870414