|
|
using System; |
|
|
using System.Threading.Tasks; |
|
|
using System.Windows.Forms; |
|
|
using Autodesk.Revit.Attributes; |
|
|
using Autodesk.Revit.DB; |
|
|
using Autodesk.Revit.UI; |
|
|
|
|
|
namespace KSTools.Licensing |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Transaction(TransactionMode.ReadOnly)] |
|
|
[Regeneration(RegenerationOption.Manual)] |
|
|
public class KSToolsCommand : IExternalCommand |
|
|
{ |
|
|
|
|
|
private const string API_BASE_URL = "https://your-username-kstools-license.hf.space/api"; |
|
|
|
|
|
private static LicenseManager _licenseManager; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
|
|
|
if (_licenseManager == null) |
|
|
{ |
|
|
_licenseManager = new LicenseManager(API_BASE_URL); |
|
|
} |
|
|
|
|
|
|
|
|
var licenseCheckResult = CheckLicenseAsync().Result; |
|
|
|
|
|
if (licenseCheckResult == Result.Succeeded) |
|
|
{ |
|
|
|
|
|
return ExecuteMainFeatures(commandData); |
|
|
} |
|
|
else |
|
|
{ |
|
|
return licenseCheckResult; |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
TaskDialog.Show("KSTools 錯誤", $"執行過程發生錯誤:{ex.Message}"); |
|
|
return Result.Failed; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<Result> CheckLicenseAsync() |
|
|
{ |
|
|
try |
|
|
{ |
|
|
|
|
|
if (_licenseManager.HasValidLicense()) |
|
|
{ |
|
|
|
|
|
var validation = await _licenseManager.ValidateLicenseAsync(false); |
|
|
|
|
|
if (validation.Valid) |
|
|
{ |
|
|
|
|
|
var licenseInfo = _licenseManager.GetLicenseInfo(); |
|
|
if (licenseInfo?.IsExpiringSoon == true) |
|
|
{ |
|
|
TaskDialog.Show("授權提醒", |
|
|
$"您的授權將在 {licenseInfo.DaysRemaining} 天後到期,請及時聯絡管理員續期。"); |
|
|
} |
|
|
|
|
|
return Result.Succeeded; |
|
|
} |
|
|
else |
|
|
{ |
|
|
return ShowLicenseError(validation.Message); |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
return await HandleLicenseActivation(); |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
TaskDialog.Show("授權檢查錯誤", $"無法驗證授權狀態:{ex.Message}"); |
|
|
return Result.Failed; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<Result> HandleLicenseActivation() |
|
|
{ |
|
|
|
|
|
var hardwareId = _licenseManager.GetHardwareId(); |
|
|
|
|
|
var dialog = new LicenseActivationDialog(hardwareId); |
|
|
var dialogResult = dialog.ShowDialog(); |
|
|
|
|
|
if (dialogResult == DialogResult.OK && !string.IsNullOrEmpty(dialog.LicenseCode)) |
|
|
{ |
|
|
|
|
|
var progressDialog = new TaskDialog("KSTools") |
|
|
{ |
|
|
MainInstruction = "正在啟用授權...", |
|
|
MainContent = "請稍等,正在驗證授權碼有效性。", |
|
|
AllowCancellation = false |
|
|
}; |
|
|
|
|
|
|
|
|
var activationTask = _licenseManager.ActivateLicenseAsync(dialog.LicenseCode); |
|
|
|
|
|
|
|
|
var result = await activationTask; |
|
|
|
|
|
if (result.Success) |
|
|
{ |
|
|
TaskDialog.Show("啟用成功", |
|
|
$"授權啟用成功!\n\n" + |
|
|
$"用戶:{result.UserName}\n" + |
|
|
$"到期時間:{result.ExpiresAt:yyyy-MM-dd HH:mm}\n\n" + |
|
|
$"感謝使用 KSTools!"); |
|
|
|
|
|
return Result.Succeeded; |
|
|
} |
|
|
else |
|
|
{ |
|
|
return ShowLicenseError($"授權啟用失敗:{result.Message}"); |
|
|
} |
|
|
} |
|
|
else if (dialogResult == DialogResult.Retry) |
|
|
{ |
|
|
|
|
|
return await HandleLicenseActivation(); |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
return Result.Cancelled; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Result ShowLicenseError(string errorMessage) |
|
|
{ |
|
|
var dialog = new TaskDialog("KSTools 授權錯誤") |
|
|
{ |
|
|
MainInstruction = "授權驗證失敗", |
|
|
MainContent = errorMessage, |
|
|
CommonButtons = TaskDialogCommonButtons.None |
|
|
}; |
|
|
|
|
|
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "重新輸入授權碼"); |
|
|
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "聯絡技術支援"); |
|
|
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, "取消"); |
|
|
|
|
|
var result = dialog.Show(); |
|
|
|
|
|
switch (result) |
|
|
{ |
|
|
case TaskDialogResult.CommandLink1: |
|
|
|
|
|
_licenseManager.ClearLicense(); |
|
|
return HandleLicenseActivation().Result; |
|
|
|
|
|
case TaskDialogResult.CommandLink2: |
|
|
|
|
|
ShowSupportInfo(); |
|
|
return Result.Cancelled; |
|
|
|
|
|
default: |
|
|
return Result.Cancelled; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ShowSupportInfo() |
|
|
{ |
|
|
var supportInfo = $"KSTools 技術支援\n\n" + |
|
|
$"如需協助,請聯絡:\n" + |
|
|
$"• 電子郵件:support@kstools.com\n" + |
|
|
$"• 電話:(02) 1234-5678\n\n" + |
|
|
$"請提供以下資訊:\n" + |
|
|
$"• 硬體ID:{_licenseManager.GetHardwareId()}\n" + |
|
|
$"• 錯誤時間:{DateTime.Now:yyyy-MM-dd HH:mm:ss}\n" + |
|
|
$"• Revit 版本:{commandData.Application.Application.VersionName}"; |
|
|
|
|
|
TaskDialog.Show("技術支援", supportInfo); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Result ExecuteMainFeatures(ExternalCommandData commandData) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
var dialog = new TaskDialog("KSTools") |
|
|
{ |
|
|
MainInstruction = "KSTools 功能選單", |
|
|
MainContent = "請選擇要執行的功能:", |
|
|
CommonButtons = TaskDialogCommonButtons.Cancel |
|
|
}; |
|
|
|
|
|
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "功能 A", "執行 KSTools 功能 A"); |
|
|
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "功能 B", "執行 KSTools 功能 B"); |
|
|
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, "授權資訊", "查看當前授權狀態"); |
|
|
|
|
|
var result = dialog.Show(); |
|
|
|
|
|
switch (result) |
|
|
{ |
|
|
case TaskDialogResult.CommandLink1: |
|
|
return ExecuteFeatureA(commandData); |
|
|
|
|
|
case TaskDialogResult.CommandLink2: |
|
|
return ExecuteFeatureB(commandData); |
|
|
|
|
|
case TaskDialogResult.CommandLink3: |
|
|
ShowLicenseInfo(); |
|
|
return Result.Succeeded; |
|
|
|
|
|
default: |
|
|
return Result.Cancelled; |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
TaskDialog.Show("執行錯誤", $"功能執行失敗:{ex.Message}"); |
|
|
return Result.Failed; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Result ExecuteFeatureA(ExternalCommandData commandData) |
|
|
{ |
|
|
TaskDialog.Show("功能 A", "KSTools 功能 A 執行成功!\n\n這裡實作你的具體功能..."); |
|
|
return Result.Succeeded; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Result ExecuteFeatureB(ExternalCommandData commandData) |
|
|
{ |
|
|
TaskDialog.Show("功能 B", "KSTools 功能 B 執行成功!\n\n這裡實作你的具體功能..."); |
|
|
return Result.Succeeded; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ShowLicenseInfo() |
|
|
{ |
|
|
var licenseInfo = _licenseManager.GetLicenseInfo(); |
|
|
|
|
|
if (licenseInfo != null) |
|
|
{ |
|
|
var info = $"KSTools 授權資訊\n\n" + |
|
|
$"授權碼:{licenseInfo.LicenseCode}\n" + |
|
|
$"用戶:{licenseInfo.UserName}\n" + |
|
|
$"硬體ID:{licenseInfo.HardwareId}\n" + |
|
|
$"到期時間:{licenseInfo.ExpiresAt:yyyy-MM-dd HH:mm}\n" + |
|
|
$"剩餘天數:{licenseInfo.DaysRemaining} 天\n" + |
|
|
$"最後驗證:{licenseInfo.LastValidation:yyyy-MM-dd HH:mm}"; |
|
|
|
|
|
if (licenseInfo.IsExpiringSoon) |
|
|
{ |
|
|
info += "\n\n⚠️ 授權即將到期,請及時續期!"; |
|
|
} |
|
|
|
|
|
TaskDialog.Show("授權資訊", info); |
|
|
} |
|
|
else |
|
|
{ |
|
|
TaskDialog.Show("授權資訊", "無法取得授權資訊"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |