using System; using System.Collections.Generic; using LC.Newtonsoft.Json; using TapTap.TapDB.Standalone.Internal; using Logger = TapTap.TapDB.Standalone.Internal.Logger; using Constants = TapTap.TapDB.Standalone.Internal.Constants; namespace TapTap.TapDB.Standalone { public class TapDBStandalone : ITapDB { //Logger 可能会在初始化前被用到 internal static Logger Logger = new Logger(); internal static Prefs Prefs; internal static Identity Identity; internal static Tracker Tracker = new Tracker(); internal static Device Device; internal static User User; internal static Charge Charge; #region ITapDB public void Init(string clientId, string channel, string gameVersion, bool isCN) { TapDBRegion region = isCN ? TapDBRegion.CN : TapDBRegion.IO; Init(clientId, channel, gameVersion, region, null); } public void Init(string clientId, string channel, string gameVersion, TapDBRegion region, Dictionary properties) { // 尝试迁移旧数据 Migration migration = new Migration(); migration.TryMigrate(); // 实例化模块 if (Prefs == null) { Prefs = new Prefs(clientId); } Identity = new Identity(); Device = new Device(); User = new User(); Charge = new Charge(); // 初始化 Tracker.Init(clientId, channel, gameVersion, region, properties); } public void SetUser(string userId) { SetUser(userId, null); } public void SetUser(string userId, string loginType) { if (!IsInitialized) { return; } if (string.IsNullOrWhiteSpace(userId)) { Logger.Error("UserId is NULL."); return; } User.Login(userId, loginType); } public void SetUserWithProperties(string userId, Dictionary properties) { if (!IsInitialized) { return; } if (string.IsNullOrWhiteSpace(userId)) { Logger.Error("UserId is NULL."); return; } User.Login(userId, null, properties); } public void ClearUser() { if (!IsInitialized) { return; } User.Logout(); } public void SetName(string name) { if (!IsInitialized) { return; } User.SetName(name); } public void SetLevel(int level) { if (!IsInitialized) { return; } User.SetLevel(level); } public void SetServer(string server) { if (!IsInitialized) { return; } User.SetServer(server); } public void OnCharge(string orderId, string product, long amount, string currencyType, string payment) { Dictionary props = null; OnCharge(orderId, product, amount, currencyType, payment, props); } public void OnCharge(string orderId, string product, long amount, string currencyType, string payment, string properties) { Dictionary props = null; if (!string.IsNullOrWhiteSpace(properties)) { props = JsonConvert.DeserializeObject>(properties); } OnCharge(orderId, product, amount, currencyType, payment, props); } public void OnCharge(string orderId, string product, long amount, string currencyType, string payment, Dictionary properties) { if (!IsInitialized) { return; } Charge.Track(orderId, product, amount, currencyType, payment, properties); } public void OnEvent(string eventCode, string properties) { TrackEvent(eventCode, properties); } public void TrackEvent(string eventName, string properties) { try { Dictionary jsonData = JsonConvert.DeserializeObject>(properties); TrackEvent(eventName, jsonData); } catch (Exception e) { Logger.Error(e.Message); } } public void TrackEvent(string eventName, Dictionary properties) { if (string.IsNullOrWhiteSpace(eventName)) { Logger.Error("EventName is NULL."); return; } Tracker.TrackEvent(Constants.EVENT, eventName, properties); } public void RegisterStaticProperties(string properties) { try { Dictionary jsonData = JsonConvert.DeserializeObject>(properties); RegisterStaticProperties(jsonData); } catch (Exception e) { Logger.Error(e.Message); } } public void RegisterStaticProperties(Dictionary properties) { Tracker.RegisterStaticProps(properties); } public void UnregisterStaticProperty(string propertKey) { Tracker.UnregisterStaticProp(propertKey); } public void ClearStaticProperties() { Tracker.ClearStaticProps(); } public void RegisterDynamicProperties(IDynamicProperties properties) { Tracker.RegisterDynamicPropsDelegate(properties); } public void DeviceInitialize(string properties) { try { Dictionary jsonData = JsonConvert.DeserializeObject>(properties); DeviceInitialize(jsonData); } catch (Exception e) { Logger.Error(e.Message); } } public void DeviceInitialize(Dictionary properties) { if (!IsInitialized) { return; } Device.InitProps(properties); } public void DeviceUpdate(string properties) { try { Dictionary jsonData = JsonConvert.DeserializeObject>(properties); DeviceUpdate(jsonData); } catch (Exception e) { Logger.Error(e.Message); } } public void DeviceUpdate(Dictionary properties) { if (!IsInitialized) { return; } Device.UpdateProps(properties); } public void DeviceAdd(string properties) { try { Dictionary jsonData = JsonConvert.DeserializeObject>(properties); DeviceAdd(jsonData); } catch (Exception e) { Logger.Error(e.Message); } } public void DeviceAdd(Dictionary properties) { if (!IsInitialized) { return; } Device.AddProps(properties); } public void UserInitialize(string properties) { try { Dictionary jsonData = JsonConvert.DeserializeObject>(properties); UserInitialize(jsonData); } catch (Exception e) { Logger.Error(e.Message); } } public void UserInitialize(Dictionary properties) { if (!IsInitialized) { return; } User.InitProps(properties); } public void UserUpdate(string properties) { try { Dictionary jsonData = JsonConvert.DeserializeObject>(properties); UserUpdate(jsonData); } catch (Exception e) { Logger.Error(e.Message); } } public void UserUpdate(Dictionary properties) { if (!IsInitialized) { return; } User.UpdateProps(properties); } public void UserAdd(string properties) { try { Dictionary jsonData = JsonConvert.DeserializeObject>(properties); UserAdd(jsonData); } catch (Exception e) { Logger.Error(e.Message); } } public void UserAdd(Dictionary properties) { if (!IsInitialized) { return; } User.AddProps(properties); } public void EnableLog(bool enable) { Logger.Enable = enable; } public void AdvertiserIDCollectionEnabled(bool enable) { Logger.Error("Standalone has NOT be implemented."); } public void CloseFetchTapTapDeviceId() { Logger.Error("Standalone has NOT be implemented."); } public void GetTapTapDid(Action action) { Logger.Error("Standalone has NOT be implemented."); } public void ConfigAutoReportLogLevel(LogSeverity level) { Logger.Error("Standalone has NOT be implemented."); } public void ConfigAutoQuitApplication(bool enable) { Logger.Error("Standalone has NOT be implemented."); } public void RegisterLogCallback(TapDB.LogCallbackDelegate handler) { Logger.Error("Standalone has NOT be implemented."); } public void UnRegisterLogCallback(TapDB.LogCallbackDelegate handler) { Logger.Error("Standalone has NOT be implemented."); } public void ReportException(Exception e, string message) { Logger.Error("Standalone has NOT be implemented."); } public void SetOAIDCert(string cert) { Logger.Error("Standalone has NOT be implemented."); } public void EnableThemis(bool enable){ Logger.Error("Standalone has NOT be implemented."); } #endregion bool IsInitialized => Tracker != null && Tracker.IsInitialized; } }