Skip to content

Commit 86b24e5

Browse files
committed
Add missing essential files: Exceptions.cs, Utils.cs, WebhookHandler.cs, Logger.cs, Configuration.cs, AdvancedLicenseChainExample.cs, and LicenseChainEditor.cs
1 parent 0724551 commit 86b24e5

7 files changed

Lines changed: 1516 additions & 0 deletions

File tree

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using UnityEngine.UI;
6+
7+
namespace LicenseChain.Unity
8+
{
9+
/// <summary>
10+
/// Advanced example demonstrating comprehensive LicenseChain SDK usage
11+
/// </summary>
12+
public class AdvancedLicenseChainExample : MonoBehaviour
13+
{
14+
[Header("UI References")]
15+
public Text statusText;
16+
public Text logText;
17+
public Button createLicenseButton;
18+
public Button validateLicenseButton;
19+
public Button createUserButton;
20+
public Button webhookTestButton;
21+
public InputField licenseKeyInput;
22+
public InputField userIdInput;
23+
public InputField productIdInput;
24+
25+
private LicenseChainManager _licenseManager;
26+
private WebhookHandler _webhookHandler;
27+
private List<string> _logMessages = new List<string>();
28+
29+
private void Start()
30+
{
31+
InitializeSDK();
32+
SetupUI();
33+
LogMessage("Advanced LicenseChain Example Started");
34+
}
35+
36+
private void InitializeSDK()
37+
{
38+
// Configure the SDK
39+
var config = new LicenseChainConfig
40+
{
41+
ApiKey = "your-api-key-here",
42+
BaseUrl = "https://api.licensechain.com",
43+
Timeout = 30000,
44+
Retries = 3,
45+
EnableLogging = true
46+
};
47+
48+
_licenseManager = new LicenseChainManager(config);
49+
_webhookHandler = new WebhookHandler("your-webhook-secret");
50+
51+
// Setup logging
52+
Logger.SetLogLevel(Logger.LogLevel.Debug);
53+
Logger.SetFileLogging(true);
54+
55+
LogMessage("SDK Initialized Successfully");
56+
}
57+
58+
private void SetupUI()
59+
{
60+
createLicenseButton.onClick.AddListener(CreateLicense);
61+
validateLicenseButton.onClick.AddListener(ValidateLicense);
62+
createUserButton.onClick.AddListener(CreateUser);
63+
webhookTestButton.onClick.AddListener(TestWebhook);
64+
65+
// Set default values
66+
userIdInput.text = "unity_user_" + UnityEngine.Random.Range(1000, 9999);
67+
productIdInput.text = "unity_product_" + UnityEngine.Random.Range(100, 999);
68+
}
69+
70+
private async void CreateLicense()
71+
{
72+
try
73+
{
74+
LogMessage("Creating license...");
75+
UpdateStatus("Creating License...");
76+
77+
var userId = userIdInput.text;
78+
var productId = productIdInput.text;
79+
80+
var metadata = new Dictionary<string, object>
81+
{
82+
{"platform", "Unity"},
83+
{"version", Application.version},
84+
{"device", Utils.GetDeviceId()},
85+
{"features", new string[] {"premium", "api_access", "support"}},
86+
{"maxUsers", 50}
87+
};
88+
89+
var license = await _licenseManager.CreateLicenseAsync(userId, productId, metadata);
90+
91+
licenseKeyInput.text = license.LicenseKey;
92+
LogMessage($"License created successfully: {license.LicenseKey}");
93+
UpdateStatus("License Created Successfully");
94+
}
95+
catch (Exception ex)
96+
{
97+
LogMessage($"Error creating license: {ex.Message}");
98+
UpdateStatus("Error Creating License");
99+
}
100+
}
101+
102+
private async void ValidateLicense()
103+
{
104+
try
105+
{
106+
var licenseKey = licenseKeyInput.text;
107+
if (string.IsNullOrEmpty(licenseKey))
108+
{
109+
LogMessage("Please enter a license key");
110+
return;
111+
}
112+
113+
LogMessage("Validating license...");
114+
UpdateStatus("Validating License...");
115+
116+
var isValid = await _licenseManager.ValidateLicenseAsync(licenseKey);
117+
118+
if (isValid)
119+
{
120+
LogMessage("License is valid!");
121+
UpdateStatus("License Valid");
122+
}
123+
else
124+
{
125+
LogMessage("License is invalid or expired");
126+
UpdateStatus("License Invalid");
127+
}
128+
}
129+
catch (Exception ex)
130+
{
131+
LogMessage($"Error validating license: {ex.Message}");
132+
UpdateStatus("Error Validating License");
133+
}
134+
}
135+
136+
private async void CreateUser()
137+
{
138+
try
139+
{
140+
LogMessage("Creating user...");
141+
UpdateStatus("Creating User...");
142+
143+
var userRegistration = new UserRegistration
144+
{
145+
Username = "unity_user_" + UnityEngine.Random.Range(1000, 9999),
146+
Email = "unity@example.com",
147+
Password = "secure_password_123",
148+
Metadata = new Dictionary<string, object>
149+
{
150+
{"platform", "Unity"},
151+
{"version", Application.version},
152+
{"device", Utils.GetDeviceId()},
153+
{"company", "Unity Game Studio"}
154+
}
155+
};
156+
157+
var user = await _licenseManager.CreateUserAsync(userRegistration);
158+
159+
LogMessage($"User created successfully: {user.Username}");
160+
UpdateStatus("User Created Successfully");
161+
}
162+
catch (Exception ex)
163+
{
164+
LogMessage($"Error creating user: {ex.Message}");
165+
UpdateStatus("Error Creating User");
166+
}
167+
}
168+
169+
private void TestWebhook()
170+
{
171+
try
172+
{
173+
LogMessage("Testing webhook handling...");
174+
UpdateStatus("Testing Webhook...");
175+
176+
// Simulate webhook events
177+
var webhookEvents = new List<WebhookEvent>
178+
{
179+
new WebhookEvent
180+
{
181+
type = "license.created",
182+
data = new { licenseKey = "TEST123", userId = "test_user" },
183+
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
184+
},
185+
new WebhookEvent
186+
{
187+
type = "user.registered",
188+
data = new { username = "test_user", email = "test@example.com" },
189+
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
190+
}
191+
};
192+
193+
foreach (var webhookEvent in webhookEvents)
194+
{
195+
var payload = Utils.ToJson(webhookEvent.data);
196+
var signature = "simulated_signature"; // In real usage, this would come from the webhook
197+
198+
var isValidSignature = _webhookHandler.VerifySignature(payload, signature);
199+
LogMessage($"Webhook {webhookEvent.type}: {(isValidSignature ? "Valid" : "Invalid")} signature");
200+
201+
_webhookHandler.ProcessEvent(webhookEvent.type, webhookEvent.data);
202+
}
203+
204+
LogMessage("Webhook testing completed");
205+
UpdateStatus("Webhook Test Completed");
206+
}
207+
catch (Exception ex)
208+
{
209+
LogMessage($"Error testing webhook: {ex.Message}");
210+
UpdateStatus("Error Testing Webhook");
211+
}
212+
}
213+
214+
private void LogMessage(string message)
215+
{
216+
var timestamp = DateTime.Now.ToString("HH:mm:ss");
217+
var logMessage = $"[{timestamp}] {message}";
218+
_logMessages.Add(logMessage);
219+
220+
// Keep only last 50 messages
221+
if (_logMessages.Count > 50)
222+
{
223+
_logMessages.RemoveAt(0);
224+
}
225+
226+
// Update UI
227+
if (logText != null)
228+
{
229+
logText.text = string.Join("\n", _logMessages);
230+
}
231+
232+
// Also log to console
233+
Logger.Info(message, this);
234+
}
235+
236+
private void UpdateStatus(string status)
237+
{
238+
if (statusText != null)
239+
{
240+
statusText.text = status;
241+
}
242+
}
243+
244+
private void OnDestroy()
245+
{
246+
// Cleanup
247+
_licenseManager?.Dispose();
248+
}
249+
250+
// Additional utility methods for demonstration
251+
private void Update()
252+
{
253+
// Rotate log file if it gets too large
254+
if (Time.frameCount % 1000 == 0) // Check every 1000 frames
255+
{
256+
Logger.RotateLogFile();
257+
}
258+
}
259+
260+
// Public methods for external access
261+
public void ClearLogs()
262+
{
263+
_logMessages.Clear();
264+
if (logText != null)
265+
{
266+
logText.text = "";
267+
}
268+
Logger.ClearLogFile();
269+
LogMessage("Logs cleared");
270+
}
271+
272+
public void ExportLogs()
273+
{
274+
var logFilePath = Logger.GetLogFilePath();
275+
LogMessage($"Logs exported to: {logFilePath}");
276+
}
277+
278+
public void SetLogLevel(int level)
279+
{
280+
Logger.SetLogLevel((Logger.LogLevel)level);
281+
LogMessage($"Log level set to: {(Logger.LogLevel)level}");
282+
}
283+
}
284+
}

0 commit comments

Comments
 (0)