AELicenseSystem.cpp 13 KB

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