You've already forked Commercialization.tapadn
release: 1.0.4
This commit is contained in:
@@ -15,7 +15,7 @@ public static class TapadnSmartLoadOrchestrator
|
||||
private static TapadnSmartLoadConfig _runtimeConfig;
|
||||
private static Dictionary<string, TapadnSmartLoadSceneState> _states = new Dictionary<string, TapadnSmartLoadSceneState>(StringComparer.Ordinal);
|
||||
private static Dictionary<string, TapadnSmartLoadPolicyItem> _policies = new Dictionary<string, TapadnSmartLoadPolicyItem>(StringComparer.Ordinal);
|
||||
private static Dictionary<int, TapadnSmartLoadCacheState> _cacheStates = new Dictionary<int, TapadnSmartLoadCacheState>();
|
||||
private static Dictionary<string, TapadnSmartLoadCacheState> _cacheStates = new Dictionary<string, TapadnSmartLoadCacheState>(StringComparer.Ordinal);
|
||||
private static bool _initialized;
|
||||
private static bool _enabled;
|
||||
|
||||
@@ -116,7 +116,7 @@ public static class TapadnSmartLoadOrchestrator
|
||||
_enabled = options?.SmartPreloadEnabled ?? false;
|
||||
_initialized = true;
|
||||
_states = new Dictionary<string, TapadnSmartLoadSceneState>(StringComparer.Ordinal);
|
||||
_cacheStates = new Dictionary<int, TapadnSmartLoadCacheState>();
|
||||
_cacheStates = new Dictionary<string, TapadnSmartLoadCacheState>(StringComparer.Ordinal);
|
||||
LoadStates();
|
||||
EnsureDefaultPolicies(_runtimeConfig);
|
||||
}
|
||||
@@ -135,10 +135,10 @@ public static class TapadnSmartLoadOrchestrator
|
||||
record.ShowRequestCount = Math.Max(0, record.ShowRequestCount);
|
||||
SaveStates();
|
||||
|
||||
TryPreload(adType, normalizedScenario);
|
||||
TryPreload(adType, normalizedScenario, _slotId);
|
||||
}
|
||||
|
||||
public static void OnPlayRequestStarted(AD_Type adType, string scenario, bool cacheReadyAtRequest)
|
||||
public static void OnPlayRequestStarted(AD_Type adType, string scenario, bool cacheReadyAtRequest, string slotId = null)
|
||||
{
|
||||
if (!_initialized || !_enabled)
|
||||
{
|
||||
@@ -152,49 +152,49 @@ public static class TapadnSmartLoadOrchestrator
|
||||
record.LastUpdatedUnix = GetNowUnixSeconds();
|
||||
if (cacheReadyAtRequest)
|
||||
{
|
||||
MarkImmediateHit(adType, normalizedScenario);
|
||||
MarkImmediateHit(adType, normalizedScenario, slotId);
|
||||
}
|
||||
else
|
||||
{
|
||||
MarkCacheExpiredIfStale(adType);
|
||||
MarkCacheExpiredIfStale(adType, slotId);
|
||||
}
|
||||
|
||||
SaveStates();
|
||||
}
|
||||
|
||||
public static void OnLoadRequested(AD_Type adType, string scenario)
|
||||
public static void OnLoadRequested(AD_Type adType, string scenario, string slotId = null)
|
||||
{
|
||||
if (!_initialized || !_enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PrepareSmartLoadRequest(adType, scenario);
|
||||
PrepareSmartLoadRequest(adType, scenario, slotId);
|
||||
}
|
||||
|
||||
public static void OnLoadStarted(AD_Type adType, string scenario)
|
||||
public static void OnLoadStarted(AD_Type adType, string scenario, string slotId = null)
|
||||
{
|
||||
if (!_initialized || !_enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
BeginLoadRequest(adType, scenario);
|
||||
BeginLoadRequest(adType, scenario, slotId);
|
||||
SaveStates();
|
||||
}
|
||||
|
||||
public static void OnLoadResult(AD_Type adType, string scenario, bool success)
|
||||
public static void OnLoadResult(AD_Type adType, string scenario, bool success, string slotId = null)
|
||||
{
|
||||
if (!_initialized || !_enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CompleteLoadRequest(adType, scenario, success);
|
||||
CompleteLoadRequest(adType, scenario, success, slotId);
|
||||
SaveStates();
|
||||
}
|
||||
|
||||
public static void OnShowStart(AD_Type adType, string scenario)
|
||||
public static void OnShowStart(AD_Type adType, string scenario, string slotId = null)
|
||||
{
|
||||
if (!_initialized || !_enabled)
|
||||
{
|
||||
@@ -204,11 +204,11 @@ public static class TapadnSmartLoadOrchestrator
|
||||
var record = GetOrCreateState(adType, NormalizeScenario(scenario));
|
||||
record.ShowStartCount = Math.Max(0, record.ShowStartCount) + 1;
|
||||
record.LastUpdatedUnix = GetNowUnixSeconds();
|
||||
MarkCacheConsumed(adType, scenario);
|
||||
MarkCacheConsumed(adType, scenario, slotId);
|
||||
SaveStates();
|
||||
}
|
||||
|
||||
public static void OnShowError(AD_Type adType, string scenario)
|
||||
public static void OnShowError(AD_Type adType, string scenario, string slotId = null)
|
||||
{
|
||||
if (!_initialized || !_enabled)
|
||||
{
|
||||
@@ -218,11 +218,11 @@ public static class TapadnSmartLoadOrchestrator
|
||||
var record = GetOrCreateState(adType, NormalizeScenario(scenario));
|
||||
record.ShowFailureCount = Math.Max(0, record.ShowFailureCount) + 1;
|
||||
record.LastUpdatedUnix = GetNowUnixSeconds();
|
||||
MarkCacheShowFailed(adType, scenario);
|
||||
MarkCacheShowFailed(adType, scenario, slotId);
|
||||
SaveStates();
|
||||
}
|
||||
|
||||
private static void TryPreload(AD_Type adType, string scenario)
|
||||
private static void TryPreload(AD_Type adType, string scenario, string slotId)
|
||||
{
|
||||
if (!_initialized || !_enabled)
|
||||
{
|
||||
@@ -257,11 +257,11 @@ public static class TapadnSmartLoadOrchestrator
|
||||
return;
|
||||
}
|
||||
|
||||
MarkCacheExpiredIfStale(adType);
|
||||
OnLoadRequested(adType, scenario);
|
||||
MarkCacheExpiredIfStale(adType, slotId);
|
||||
OnLoadRequested(adType, scenario, slotId);
|
||||
ADManager.Instance.LoadAD(adType);
|
||||
var smartLoadStarted = HasPendingSmartLoadForScene(adType, scenario);
|
||||
ClearPreparedLoadIfNotStarted(adType);
|
||||
var smartLoadStarted = HasPendingSmartLoadForScene(adType, scenario, slotId);
|
||||
ClearPreparedLoadIfNotStarted(adType, slotId);
|
||||
if (!smartLoadStarted)
|
||||
{
|
||||
return;
|
||||
@@ -302,9 +302,9 @@ public static class TapadnSmartLoadOrchestrator
|
||||
return Mathf.Lerp(policy.BaseProbability, observedRate, trust);
|
||||
}
|
||||
|
||||
private static void PrepareSmartLoadRequest(AD_Type adType, string scenario)
|
||||
private static void PrepareSmartLoadRequest(AD_Type adType, string scenario, string slotId)
|
||||
{
|
||||
var state = GetOrCreateCacheState(adType);
|
||||
var state = GetOrCreateCacheState(adType, slotId);
|
||||
if (state.PendingSmartLoad)
|
||||
{
|
||||
return;
|
||||
@@ -316,9 +316,9 @@ public static class TapadnSmartLoadOrchestrator
|
||||
state.PreparedRequestUnix = GetNowUnixSeconds();
|
||||
}
|
||||
|
||||
private static void BeginLoadRequest(AD_Type adType, string scenario)
|
||||
private static void BeginLoadRequest(AD_Type adType, string scenario, string slotId)
|
||||
{
|
||||
var state = GetOrCreateCacheState(adType);
|
||||
var state = GetOrCreateCacheState(adType, slotId);
|
||||
var now = GetNowUnixSeconds();
|
||||
if (state.PreparedSmartLoad)
|
||||
{
|
||||
@@ -346,9 +346,9 @@ public static class TapadnSmartLoadOrchestrator
|
||||
state.PreparedRequestUnix = 0;
|
||||
}
|
||||
|
||||
private static void ClearPreparedLoadIfNotStarted(AD_Type adType)
|
||||
private static void ClearPreparedLoadIfNotStarted(AD_Type adType, string slotId)
|
||||
{
|
||||
var state = GetOrCreateCacheState(adType);
|
||||
var state = GetOrCreateCacheState(adType, slotId);
|
||||
if (state.PendingSmartLoad)
|
||||
{
|
||||
return;
|
||||
@@ -360,16 +360,16 @@ public static class TapadnSmartLoadOrchestrator
|
||||
state.PreparedRequestUnix = 0;
|
||||
}
|
||||
|
||||
private static bool HasPendingSmartLoadForScene(AD_Type adType, string scenario)
|
||||
private static bool HasPendingSmartLoadForScene(AD_Type adType, string scenario, string slotId)
|
||||
{
|
||||
var state = GetOrCreateCacheState(adType);
|
||||
var state = GetOrCreateCacheState(adType, slotId);
|
||||
return state.PendingSmartLoad &&
|
||||
string.Equals(NormalizeScenario(state.PendingOriginScene), NormalizeScenario(scenario), StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
private static void CompleteLoadRequest(AD_Type adType, string scenario, bool success)
|
||||
private static void CompleteLoadRequest(AD_Type adType, string scenario, bool success, string slotId)
|
||||
{
|
||||
var cacheState = GetOrCreateCacheState(adType);
|
||||
var cacheState = GetOrCreateCacheState(adType, slotId);
|
||||
var normalizedScenario = NormalizeScenario(scenario);
|
||||
var originScene = cacheState.PendingSmartLoad ? NormalizeScenario(cacheState.PendingOriginScene) : normalizedScenario;
|
||||
var originRecord = GetOrCreateState(adType, originScene);
|
||||
@@ -408,23 +408,23 @@ public static class TapadnSmartLoadOrchestrator
|
||||
cacheState.PendingRequestUnix = 0;
|
||||
}
|
||||
|
||||
private static void MarkImmediateHit(AD_Type adType, string scenario)
|
||||
private static void MarkImmediateHit(AD_Type adType, string scenario, string slotId)
|
||||
{
|
||||
var consumedScene = NormalizeScenario(scenario);
|
||||
var currentSceneRecord = GetOrCreateState(adType, consumedScene);
|
||||
currentSceneRecord.ImmediateHitCount = Math.Max(0, currentSceneRecord.ImmediateHitCount) + 1;
|
||||
currentSceneRecord.LastUpdatedUnix = GetNowUnixSeconds();
|
||||
|
||||
var cacheState = GetOrCreateCacheState(adType);
|
||||
var cacheState = GetOrCreateCacheState(adType, slotId);
|
||||
if (!cacheState.HasReadyCache)
|
||||
{
|
||||
currentSceneRecord.UnattributedCacheHitCount = Math.Max(0, currentSceneRecord.UnattributedCacheHitCount) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
private static void MarkCacheConsumed(AD_Type adType, string scenario)
|
||||
private static void MarkCacheConsumed(AD_Type adType, string scenario, string slotId)
|
||||
{
|
||||
var cacheState = GetOrCreateCacheState(adType);
|
||||
var cacheState = GetOrCreateCacheState(adType, slotId);
|
||||
if (!cacheState.HasReadyCache || cacheState.Consumed)
|
||||
{
|
||||
return;
|
||||
@@ -458,9 +458,9 @@ public static class TapadnSmartLoadOrchestrator
|
||||
ClearReadyCache(cacheState);
|
||||
}
|
||||
|
||||
private static void MarkCacheShowFailed(AD_Type adType, string scenario)
|
||||
private static void MarkCacheShowFailed(AD_Type adType, string scenario, string slotId)
|
||||
{
|
||||
var cacheState = GetOrCreateCacheState(adType);
|
||||
var cacheState = GetOrCreateCacheState(adType, slotId);
|
||||
if (!cacheState.HasReadyCache)
|
||||
{
|
||||
return;
|
||||
@@ -479,9 +479,9 @@ public static class TapadnSmartLoadOrchestrator
|
||||
ClearReadyCache(cacheState);
|
||||
}
|
||||
|
||||
private static void MarkCacheExpiredIfStale(AD_Type adType)
|
||||
private static void MarkCacheExpiredIfStale(AD_Type adType, string slotId)
|
||||
{
|
||||
var cacheState = GetOrCreateCacheState(adType);
|
||||
var cacheState = GetOrCreateCacheState(adType, slotId);
|
||||
if (!cacheState.HasReadyCache)
|
||||
{
|
||||
return;
|
||||
@@ -502,9 +502,9 @@ public static class TapadnSmartLoadOrchestrator
|
||||
ClearReadyCache(cacheState);
|
||||
}
|
||||
|
||||
private static TapadnSmartLoadCacheState GetOrCreateCacheState(AD_Type adType)
|
||||
private static TapadnSmartLoadCacheState GetOrCreateCacheState(AD_Type adType, string slotId)
|
||||
{
|
||||
var key = (int)adType;
|
||||
var key = ComposeCacheKey(adType, slotId);
|
||||
if (_cacheStates.TryGetValue(key, out var state) && state != null)
|
||||
{
|
||||
return state;
|
||||
@@ -879,6 +879,16 @@ public static class TapadnSmartLoadOrchestrator
|
||||
return ((int)adType) + "|" + NormalizeScenario(scenario);
|
||||
}
|
||||
|
||||
private static string ComposeCacheKey(AD_Type adType, string slotId)
|
||||
{
|
||||
return ((int)adType) + "|" + NormalizeSlotId(slotId);
|
||||
}
|
||||
|
||||
private static string NormalizeSlotId(string slotId)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(slotId) ? "__default_slot__" : slotId.Trim();
|
||||
}
|
||||
|
||||
private static string NormalizeScenario(string scenario)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(scenario))
|
||||
|
||||
Reference in New Issue
Block a user