AELicenseSystem.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. // BEGIN LICENSE MANAGEMENT
  5. #ifdef ATOMIC_PLATFORM_WINDOWS
  6. #ifndef _MSC_VER
  7. #define _WIN32_IE 0x501
  8. #endif
  9. #include <windows.h>
  10. #include <shellapi.h>
  11. #include <direct.h>
  12. #include <shlobj.h>
  13. #include <sys/types.h>
  14. #include <sys/utime.h>
  15. #endif
  16. #include "AtomicEditor.h"
  17. #include <Atomic/Core/CoreEvents.h>
  18. #include <Atomic/IO/FileSystem.h>
  19. #include <Atomic/IO/File.h>
  20. #include <Atomic/IO/Log.h>
  21. #include "AtomicEditor.h"
  22. #include "AEEditor.h"
  23. #include "AEEvents.h"
  24. #include "AELicenseSystem.h"
  25. #include "UI/Modal/UIModalOps.h"
  26. #include "UI/Modal/UIMessageModal.h"
  27. #include "UI/UIMainFrame.h"
  28. #include <Poco/MD5Engine.h>
  29. #include <Poco/File.h>
  30. namespace AtomicEditor
  31. {
  32. LicenseSystem::LicenseSystem(Context* context) :
  33. Object(context)
  34. , eulaAgreementConfirmed_(false)
  35. {
  36. ResetLicense();
  37. }
  38. LicenseSystem::~LicenseSystem()
  39. {
  40. }
  41. void LicenseSystem::Initialize()
  42. {
  43. FileSystem* filesystem = GetSubsystem<FileSystem>();
  44. String eulaConfirmedFilePath = filesystem->GetAppPreferencesDir("AtomicEditor", "License");
  45. eulaConfirmedFilePath = AddTrailingSlash(eulaConfirmedFilePath);
  46. eulaConfirmedFilePath += "EulaConfirmed";
  47. eulaAgreementConfirmed_ = filesystem->FileExists(eulaConfirmedFilePath);
  48. if (!LoadLicense() || !key_.Length() || !eulaAgreementConfirmed_)
  49. {
  50. ResetLicense();
  51. UIModalOps* ops = GetSubsystem<UIModalOps>();
  52. if (eulaAgreementConfirmed_)
  53. ops->ShowActivation();
  54. else
  55. ops->ShowEulaAgreement();
  56. }
  57. else
  58. {
  59. RequestServerVerification(key_);
  60. }
  61. }
  62. void LicenseSystem::LicenseAgreementConfirmed()
  63. {
  64. eulaAgreementConfirmed_ = true;
  65. FileSystem* filesystem = GetSubsystem<FileSystem>();
  66. String eulaConfirmedFilePath = filesystem->GetAppPreferencesDir("AtomicEditor", "License");
  67. eulaConfirmedFilePath = AddTrailingSlash(eulaConfirmedFilePath);
  68. eulaConfirmedFilePath += "EulaConfirmed";
  69. SharedPtr<File> file(new File(context_, eulaConfirmedFilePath, FILE_WRITE));
  70. file->WriteInt(1);
  71. file->Close();
  72. UIModalOps* ops = GetSubsystem<UIModalOps>();
  73. ops->ShowActivation();
  74. }
  75. String LicenseSystem::GenerateMachineID()
  76. {
  77. #ifdef ATOMIC_PLATFORM_OSX
  78. String path = getenv("HOME");
  79. #else
  80. wchar_t pathName[MAX_PATH];
  81. pathName[0] = 0;
  82. SHGetSpecialFolderPathW(0, pathName, CSIDL_PERSONAL, 0);
  83. String path(pathName);
  84. #endif
  85. Poco::MD5Engine md5;
  86. md5.update(path.CString(), path.Length());
  87. String id = Poco::MD5Engine::digestToHex(md5.digest()).c_str();
  88. return id;
  89. }
  90. void LicenseSystem::ResetLicense()
  91. {
  92. key_ = "";
  93. licenseWindows_ = false;
  94. licenseMac_ = false;
  95. licenseAndroid_ = false;
  96. licenseIOS_ = false;
  97. licenseHTML5_ = false;
  98. licenseModule3D_ = false;
  99. }
  100. bool LicenseSystem::LoadLicense()
  101. {
  102. ResetLicense();
  103. FileSystem* filesystem = GetSubsystem<FileSystem>();
  104. String licenseFilePath = filesystem->GetAppPreferencesDir("AtomicEditor", "License");
  105. licenseFilePath = AddTrailingSlash(licenseFilePath);
  106. licenseFilePath += "AtomicLicense";
  107. if (!filesystem->FileExists(licenseFilePath))
  108. return false;
  109. SharedPtr<File> file(new File(context_, licenseFilePath, FILE_READ));
  110. file->ReadInt(); // version
  111. String key = file->ReadString();
  112. if (!ValidateKey(key))
  113. return false;
  114. key_ = key;
  115. licenseWindows_ = file->ReadBool();
  116. licenseMac_ = file->ReadBool();
  117. licenseAndroid_ = file->ReadBool();
  118. licenseIOS_ = file->ReadBool();
  119. licenseHTML5_ = file->ReadBool();
  120. licenseModule3D_ = file->ReadBool();
  121. return true;
  122. }
  123. bool LicenseSystem::ValidateKey(const String& key)
  124. {
  125. if (!key.StartsWith("ATOMIC-"))
  126. return false;
  127. Vector<String> elements = key.Split('-');
  128. if (elements.Size() != 5)
  129. return false;
  130. for (unsigned i = 1; i < elements.Size(); i++)
  131. {
  132. String element = elements[i];
  133. if (element.Length() != 4)
  134. return false;
  135. for (unsigned j = 0; j < 4; j++)
  136. {
  137. char c = element[j];
  138. if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
  139. continue;
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. void LicenseSystem::SaveLicense()
  146. {
  147. FileSystem* filesystem = GetSubsystem<FileSystem>();
  148. String licenseFilePath = filesystem->GetAppPreferencesDir("AtomicEditor", "License");
  149. licenseFilePath = AddTrailingSlash(licenseFilePath);
  150. if (!filesystem->DirExists(licenseFilePath))
  151. {
  152. Poco::File dirs(licenseFilePath.CString());
  153. dirs.createDirectories();
  154. }
  155. licenseFilePath += "AtomicLicense";
  156. SharedPtr<File> file(new File(context_, licenseFilePath, FILE_WRITE));
  157. file->WriteInt(1); // version
  158. file->WriteString(key_);
  159. file->WriteBool(licenseWindows_);
  160. file->WriteBool(licenseMac_);
  161. file->WriteBool(licenseAndroid_);
  162. file->WriteBool(licenseIOS_);
  163. file->WriteBool(licenseHTML5_);
  164. file->WriteBool(licenseModule3D_);
  165. file->Close();
  166. }
  167. void LicenseSystem::RemoveLicense()
  168. {
  169. FileSystem* filesystem = GetSubsystem<FileSystem>();
  170. String licenseFilePath = filesystem->GetAppPreferencesDir("AtomicEditor", "License");
  171. licenseFilePath = AddTrailingSlash(licenseFilePath);
  172. licenseFilePath += "AtomicLicense";
  173. if (filesystem->FileExists(licenseFilePath))
  174. {
  175. filesystem->Delete(licenseFilePath);
  176. }
  177. }
  178. bool LicenseSystem::HasPlatformLicense()
  179. {
  180. return licenseWindows_ || licenseMac_ || licenseAndroid_ || licenseIOS_ || licenseHTML5_;
  181. }
  182. void LicenseSystem::RequestServerVerification(const String& key)
  183. {
  184. if (serverVerification_.NotNull())
  185. {
  186. LOGERROR("LicenseSystem::RequestServerLicense - request already exists");
  187. return;
  188. }
  189. key_ = key;
  190. CurlManager* cm = GetSubsystem<CurlManager>();
  191. String post;
  192. String id = GenerateMachineID();
  193. post.AppendWithFormat("key=%s&id=%s", key.CString(), id.CString());
  194. serverVerification_ = cm->MakeRequest("https://store.atomicgameengine.com/licenses/license_verify.php", post);
  195. SubscribeToEvent(serverVerification_, E_CURLCOMPLETE, HANDLER(LicenseSystem, HandleVerification));
  196. }
  197. int LicenseSystem::ParseResponse(const String& response, LicenseParse& parse)
  198. {
  199. if (response.StartsWith("AC_ACTIVATIONSEXCEEDED"))
  200. {
  201. return 1;
  202. }
  203. if (response.StartsWith("AC_IDNOTACTIVATED"))
  204. {
  205. return 4;
  206. }
  207. if (response.StartsWith("AC_FAILED"))
  208. {
  209. return 2;
  210. }
  211. if (!response.StartsWith("WINDOWS"))
  212. {
  213. LOGERRORF("Error Parsing Server Response %s", response.CString());
  214. return 3;
  215. }
  216. String codes = response;
  217. codes.Replace("\n", "");
  218. codes.Replace("\r", "");
  219. Vector<String> cvector = codes.Split(' ');
  220. for (unsigned i = 0; i < cvector.Size(); i++)
  221. {
  222. Vector<String> feature = cvector[i].Split('=');
  223. if (feature.Size() != 2)
  224. continue;
  225. if (feature[0] == "WINDOWS")
  226. parse.licenseWindows_ = !feature[1].StartsWith("0");
  227. else if (feature[0] == "MAC")
  228. parse.licenseMac_ = !feature[1].StartsWith("0");
  229. else if (feature[0] == "ANDROID")
  230. parse.licenseAndroid_ = !feature[1].StartsWith("0");
  231. else if (feature[0] == "IOS")
  232. parse.licenseIOS_ = !feature[1].StartsWith("0");
  233. else if (feature[0] == "HTML5")
  234. parse.licenseHTML5_ = !feature[1].StartsWith("0");
  235. else if (feature[0] == "THREED")
  236. parse.licenseModule3D_ = !feature[1].StartsWith("0");
  237. }
  238. return 0;
  239. }
  240. bool LicenseSystem::RequestPlatformChange(AEEditorPlatform platform)
  241. {
  242. if (platform == AE_PLATFORM_HTML5 && LicenseHTML5())
  243. return true;
  244. if (platform == AE_PLATFORM_IOS && LicenseIOS())
  245. return true;
  246. if (platform == AE_PLATFORM_WINDOWS && LicenseWindows())
  247. return true;
  248. if (platform == AE_PLATFORM_MAC && LicenseMac())
  249. return true;
  250. if (platform == AE_PLATFORM_ANDROID && LicenseAndroid())
  251. return true;
  252. #ifdef ATOMIC_PLATFORM_OSX
  253. if (platform == AE_PLATFORM_MAC)
  254. return true;
  255. #else
  256. if (platform == AE_PLATFORM_WINDOWS)
  257. return true;
  258. #endif
  259. return false;
  260. }
  261. void LicenseSystem::Activate(const String& key, const LicenseParse& parse)
  262. {
  263. key_ = key;
  264. licenseWindows_ = parse.licenseWindows_;
  265. licenseMac_ = parse.licenseMac_;
  266. licenseAndroid_ = parse.licenseAndroid_;
  267. licenseIOS_= parse.licenseIOS_;
  268. licenseHTML5_= parse.licenseHTML5_;
  269. licenseModule3D_= parse.licenseModule3D_;
  270. SaveLicense();
  271. }
  272. SharedPtr<CurlRequest>& LicenseSystem::Deactivate()
  273. {
  274. if (deactivate_.NotNull())
  275. {
  276. LOGERROR("LicenseSystem::Deactivate - request already exists");
  277. return deactivate_;
  278. }
  279. if (!key_.Length())
  280. {
  281. LOGERROR("LicenseSystem::Deactivate - zero length key");
  282. return deactivate_;
  283. }
  284. CurlManager* cm = GetSubsystem<CurlManager>();
  285. String post;
  286. String id = GenerateMachineID();
  287. post.AppendWithFormat("key=%s&id=%s", key_.CString(), id.CString());
  288. deactivate_ = cm->MakeRequest("https://store.atomicgameengine.com/licenses/license_deactivate.php", post);
  289. SubscribeToEvent(deactivate_, E_CURLCOMPLETE, HANDLER(LicenseSystem, HandleDeactivate));
  290. return deactivate_;
  291. }
  292. void LicenseSystem::HandleVerification(StringHash eventType, VariantMap& eventData)
  293. {
  294. CurlRequest* request = (CurlRequest*) (eventData[CurlComplete::P_CURLREQUEST].GetPtr());
  295. if (serverVerification_.NotNull())
  296. {
  297. assert(request == serverVerification_);
  298. if (serverVerification_->GetError().Length())
  299. {
  300. LOGERRORF("Unable to verify with server: %s", serverVerification_->GetError().CString());
  301. }
  302. else
  303. {
  304. LicenseParse parse;
  305. int code = ParseResponse(serverVerification_->GetResponse(), parse);
  306. // Not activated
  307. if (code == 4)
  308. {
  309. }
  310. else if (code == 2)
  311. {
  312. // something is wrong with the key
  313. key_ = "";
  314. }
  315. else if (code == 3)
  316. {
  317. // something is wrong on the activation server
  318. key_ = "";
  319. }
  320. else if (code == 1)
  321. {
  322. // exceeded code, should not happen here as we aren't activating
  323. key_ = "";
  324. }
  325. else if (code == 0)
  326. {
  327. // we should raise an error if there is a mismatch between local and server keys
  328. // when the local says there are more enabled than server?
  329. // otherwise, they could be being added
  330. bool mismatch = false;
  331. if (parse.licenseWindows_ != licenseWindows_)
  332. mismatch = true;
  333. if (parse.licenseMac_ != licenseMac_)
  334. mismatch = true;
  335. if (parse.licenseWindows_ != licenseWindows_)
  336. mismatch = true;
  337. if (parse.licenseAndroid_ != licenseAndroid_)
  338. mismatch = true;
  339. if (parse.licenseIOS_ != licenseIOS_)
  340. mismatch = true;
  341. if (parse.licenseHTML5_ != licenseHTML5_)
  342. mismatch = true;
  343. if (parse.licenseModule3D_ != licenseModule3D_)
  344. mismatch = true;
  345. if (mismatch)
  346. {
  347. LOGERROR("License Mismatch, reseting");
  348. licenseWindows_ = parse.licenseWindows_;
  349. licenseMac_ = parse.licenseMac_;
  350. licenseAndroid_ = parse.licenseAndroid_;
  351. licenseIOS_= parse.licenseIOS_;
  352. licenseHTML5_= parse.licenseHTML5_;
  353. licenseModule3D_= parse.licenseModule3D_;
  354. SaveLicense();
  355. }
  356. if (!HasPlatformLicense())
  357. {
  358. UIModalOps* ops = GetSubsystem<UIModalOps>();
  359. if (!ops->ModalActive())
  360. ops->ShowPlatformsInfo();
  361. }
  362. }
  363. }
  364. UnsubscribeFromEvents(serverVerification_);
  365. serverVerification_ = 0;
  366. }
  367. }
  368. void LicenseSystem::HandleDeactivate(StringHash eventType, VariantMap& eventData)
  369. {
  370. Editor* editor = GetSubsystem<Editor>();
  371. CurlRequest* request = (CurlRequest*) (eventData[CurlComplete::P_CURLREQUEST].GetPtr());
  372. if (deactivate_.NotNull())
  373. {
  374. assert(request == deactivate_);
  375. if (deactivate_->GetError().Length())
  376. {
  377. String msg;
  378. msg.AppendWithFormat("Unable to deactivate with server: %s", deactivate_->GetError().CString());
  379. editor->PostModalError("Deactivation Error", msg);
  380. LOGERROR(msg);
  381. }
  382. else
  383. {
  384. String response = request->GetResponse();
  385. if (response.StartsWith("AC_FAILED"))
  386. {
  387. String msg;
  388. msg.AppendWithFormat("Unable to deactivate with server: %s", response.CString());
  389. editor->PostModalError("Deactivation Error", msg);
  390. LOGERROR(msg);
  391. }
  392. else if (response.StartsWith("AC_NOTACTIVATED") || response.StartsWith("AC_SUCCESS"))
  393. {
  394. editor->PostModalInfo("Deactivation Success", "Deactivation was successful");
  395. ResetLicense();
  396. RemoveLicense();
  397. }
  398. }
  399. UnsubscribeFromEvents(deactivate_);
  400. deactivate_ = 0;
  401. }
  402. }
  403. }
  404. // END LICENSE MANAGEMENT