Spaces:
Sleeping
Sleeping
File size: 11,821 Bytes
ad67bb9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
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
{
/// <summary>
/// KSTools Revit 外掛主命令
/// 整合授權檢查和主要功能執行
/// </summary>
[Transaction(TransactionMode.ReadOnly)]
[Regeneration(RegenerationOption.Manual)]
public class KSToolsCommand : IExternalCommand
{
// 這裡設定你的 API URL,可以是 Hugging Face Spaces 或其他部署位置
private const string API_BASE_URL = "https://your-username-kstools-license.hf.space/api";
private static LicenseManager _licenseManager;
/// <summary>
/// 命令執行入口點
/// </summary>
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;
}
}
/// <summary>
/// 異步檢查授權狀態
/// </summary>
/// <returns>檢查結果</returns>
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;
}
}
/// <summary>
/// 處理授權啟用流程
/// </summary>
/// <returns>處理結果</returns>
private async Task<Result> HandleLicenseActivation()
{
// 顯示硬體ID和授權啟用對話框
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);
// 這裡應該顯示進度,但 TaskDialog 不支援異步,所以直接等待
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;
}
}
/// <summary>
/// 顯示授權錯誤並提供選項
/// </summary>
/// <param name="errorMessage">錯誤訊息</param>
/// <returns>用戶選擇結果</returns>
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;
}
}
/// <summary>
/// 顯示技術支援資訊
/// </summary>
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);
}
/// <summary>
/// 執行主要功能 (這裡實作你的 KSTools 功能)
/// </summary>
/// <param name="commandData">Revit 命令資料</param>
/// <returns>執行結果</returns>
private Result ExecuteMainFeatures(ExternalCommandData commandData)
{
try
{
// 這裡實作你的 KSTools 主要功能
// 例如:
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;
}
}
/// <summary>
/// 執行功能 A
/// </summary>
private Result ExecuteFeatureA(ExternalCommandData commandData)
{
TaskDialog.Show("功能 A", "KSTools 功能 A 執行成功!\n\n這裡實作你的具體功能...");
return Result.Succeeded;
}
/// <summary>
/// 執行功能 B
/// </summary>
private Result ExecuteFeatureB(ExternalCommandData commandData)
{
TaskDialog.Show("功能 B", "KSTools 功能 B 執行成功!\n\n這裡實作你的具體功能...");
return Result.Succeeded;
}
/// <summary>
/// 顯示授權資訊
/// </summary>
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("授權資訊", "無法取得授權資訊");
}
}
}
} |