LicenseSystem.cpp 13 KB

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