197 lines
4.0 KiB
C#
197 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Steamworks;
|
|
using UnityEngine;
|
|
|
|
public class DLCManager : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public class DLCContent
|
|
{
|
|
[Tooltip("DLC ID obtained from Steam")]
|
|
public AppId_t appId;
|
|
|
|
[Header("Levels included in this DLC")]
|
|
public int[] mapIds;
|
|
|
|
[NonSerialized]
|
|
public bool licenseOwned;
|
|
|
|
[NonSerialized]
|
|
public int iDLC;
|
|
|
|
[NonSerialized]
|
|
public bool available;
|
|
|
|
[NonSerialized]
|
|
public string name;
|
|
}
|
|
|
|
private static DLCManager instance;
|
|
|
|
public List<DLCContent> dlcContents;
|
|
|
|
[Header("Debugging")]
|
|
public bool forceTrue_LicenseOwned;
|
|
|
|
public bool forceTrue_Available;
|
|
|
|
public static DLCManager Instance => instance;
|
|
|
|
public bool Initialized { get; private set; }
|
|
|
|
public static event Action OnUpdateDLCContents;
|
|
|
|
public DLCContent GetDLCContent(string nameDLC)
|
|
{
|
|
return dlcContents.FirstOrDefault((DLCContent element) => element.name == nameDLC);
|
|
}
|
|
|
|
public DLCContent GetDLCContent(AppId_t appId)
|
|
{
|
|
return dlcContents.FirstOrDefault((DLCContent element) => element.appId == appId);
|
|
}
|
|
|
|
public void Refresh()
|
|
{
|
|
UpdateDLCContents();
|
|
}
|
|
|
|
public DLCContent GetNewestLockedDLC()
|
|
{
|
|
DLCContent result = null;
|
|
for (int num = dlcContents.Count - 1; num >= 0; num--)
|
|
{
|
|
DLCContent dLCContent = dlcContents[num];
|
|
if (!DebugLicense(dLCContent) && DebugAvailable(dLCContent))
|
|
{
|
|
result = dLCContent;
|
|
break;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public bool IsDLCUnlocked(AppId_t id)
|
|
{
|
|
bool result = false;
|
|
if (Initialized)
|
|
{
|
|
DLCContent dLCContent = dlcContents.Find((DLCContent element) => element.appId == id);
|
|
if (dLCContent != null && DebugLicense(dLCContent))
|
|
{
|
|
result = true;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public bool IsDLCUnlocked(string name)
|
|
{
|
|
bool result = false;
|
|
DLCContent dLCContent = dlcContents.Find((DLCContent element) => element.name == name);
|
|
if (dLCContent != null)
|
|
{
|
|
result = IsDLCUnlocked(dLCContent.appId);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public bool IsLevelUnlocked(int mapID, out string nameDLC)
|
|
{
|
|
bool result = false;
|
|
nameDLC = null;
|
|
if (!IsLevelInAnyDLC(mapID))
|
|
{
|
|
result = true;
|
|
}
|
|
else if (Initialized)
|
|
{
|
|
DLCContent dLCContent = dlcContents.Find((DLCContent element) => element.mapIds.Contains(mapID));
|
|
if (DebugLicense(dLCContent))
|
|
{
|
|
result = true;
|
|
}
|
|
nameDLC = dLCContent.name;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public bool IsLevelInAnyDLC(int mapID)
|
|
{
|
|
return dlcContents.Exists((DLCContent element) => element.mapIds.Contains(mapID));
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
UnityEngine.Object.DontDestroyOnLoad(this);
|
|
if (instance == null)
|
|
{
|
|
instance = this;
|
|
}
|
|
else if (instance != this)
|
|
{
|
|
UnityEngine.Object.Destroy(base.gameObject);
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (!Initialized && SteamManager.Initialized)
|
|
{
|
|
Initialized = true;
|
|
UpdateDLCContents();
|
|
}
|
|
}
|
|
|
|
private void UpdateDLCContents()
|
|
{
|
|
List<DLCContent> list = new List<DLCContent>();
|
|
int dLCCount = SteamApps.GetDLCCount();
|
|
for (int i = 0; i < dLCCount; i++)
|
|
{
|
|
if (SteamApps.BGetDLCDataByIndex(i, out var pAppID, out var pbAvailable, out var pchName, 128))
|
|
{
|
|
list.Add(new DLCContent
|
|
{
|
|
appId = pAppID,
|
|
iDLC = i,
|
|
available = pbAvailable,
|
|
name = pchName
|
|
});
|
|
}
|
|
}
|
|
for (int j = 0; j < dlcContents.Count; j++)
|
|
{
|
|
DLCContent dlcContent = dlcContents[j];
|
|
DLCContent dLCContent = list.Find((DLCContent element) => element.appId == dlcContent.appId);
|
|
if (dLCContent != null)
|
|
{
|
|
dlcContent.licenseOwned = SteamApps.BIsDlcInstalled(dlcContent.appId);
|
|
dlcContent.iDLC = dLCContent.iDLC;
|
|
dlcContent.available = dLCContent.available;
|
|
dlcContent.name = dLCContent.name;
|
|
}
|
|
else
|
|
{
|
|
dlcContent.licenseOwned = false;
|
|
dlcContent.iDLC = -1;
|
|
dlcContent.available = false;
|
|
dlcContent.name = "[DLC Name]";
|
|
}
|
|
}
|
|
DLCManager.OnUpdateDLCContents?.Invoke();
|
|
}
|
|
|
|
private bool DebugLicense(DLCContent dlcContent)
|
|
{
|
|
return SteamApps.BIsDlcInstalled(dlcContent.appId);
|
|
}
|
|
|
|
private bool DebugAvailable(DLCContent dlcContent)
|
|
{
|
|
return dlcContent.available;
|
|
}
|
|
}
|