AELicenseSystem.cpp 14 KB

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