Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.08 KB | None | 0 0
  1. --[[
  2. ?2011 CloudSixteen.com do not share, re-distribute or modify
  3. without permission of its author (kurozael@gmail.com).
  4. --]]
  5.  
  6. openAura.config = {};
  7. openAura.config.indexes = {};
  8. openAura.config.stored = {};
  9. openAura.config.cache = {};
  10. openAura.config.class = {};
  11. openAura.config.map = {};
  12.  
  13. -- A function to create a new config object.
  14. function openAura.config.class:Create(key)
  15. local config = openAura:NewMetaTable(openAura.config.class);
  16.  
  17. config.data = openAura.config.stored[key];
  18. config.key = key;
  19.  
  20. return config;
  21. end;
  22.  
  23. -- A function to check if the config is valid.
  24. function openAura.config.class:IsValid()
  25. return self.data != nil;
  26. end;
  27.  
  28. -- A function to query the config.
  29. function openAura.config.class:Query(key, default)
  30. if (self.data and self.data[key] != nil) then
  31. return self.data[key];
  32. else
  33. return default;
  34. end;
  35. end;
  36.  
  37. -- A function to get the config's value as a boolean.
  38. function openAura.config.class:GetBoolean(default)
  39. if (self.data) then
  40. return (self.data.value == true or self.data.value == "true"
  41. or self.data.value == "yes" or self.data.value == "1" or self.data.value == 1);
  42. elseif (default != nil) then
  43. return default;
  44. else
  45. return false;
  46. end;
  47. end;
  48.  
  49. -- A function to get a config's value as a number.
  50. function openAura.config.class:GetNumber(default)
  51. if (self.data) then
  52. return tonumber(self.data.value) or default or 0;
  53. else
  54. return default or 0;
  55. end;
  56. end;
  57.  
  58. -- A function to get a config's value as a string.
  59. function openAura.config.class:GetString(default)
  60. if (self.data) then
  61. return tostring(self.data.value);
  62. else
  63. return default or "";
  64. end;
  65. end;
  66.  
  67. -- A function to get a config's default value.
  68. function openAura.config.class:GetDefault(default)
  69. if (self.data) then
  70. return self.data.default;
  71. else
  72. return default;
  73. end;
  74. end;
  75.  
  76. -- A function to get the config's next value.
  77. function openAura.config.class:GetNext(default)
  78. if (self.data and self.data.nextValue != nil) then
  79. return self.data.nextValue;
  80. else
  81. return default;
  82. end;
  83. end;
  84.  
  85. -- A function to get the config's value.
  86. function openAura.config.class:Get(default)
  87. if (self.data and self.data.value != nil) then
  88. return self.data.value;
  89. else
  90. return default;
  91. end;
  92. end;
  93.  
  94. -- A function to set whether the config has initialized.
  95. function openAura.config:SetInitialized(initalized)
  96. self.initialized = initalized;
  97. end;
  98.  
  99. -- A function to get whether the config has initialized.
  100. function openAura.config:HasInitialized()
  101. return self.initialized;
  102. end;
  103.  
  104. -- A function to get whether a config value is valid.
  105. function openAura.config:IsValidValue(value)
  106. return type(value) == "string" or type(value) == "number" or type(value) == "boolean";
  107. end;
  108.  
  109. -- A function to share a config key.
  110. function openAura.config:ShareKey(key)
  111. local shortCRC = openAura:GetShortCRC(key);
  112.  
  113. if (SERVER) then
  114. self.indexes[key] = shortCRC;
  115. else
  116. self.indexes[shortCRC] = key;
  117. end;
  118. end;
  119.  
  120. -- A function to get the stored config.
  121. function openAura.config:GetStored()
  122. return self.stored;
  123. end;
  124.  
  125. -- A function to import a config file.
  126. function openAura.config:Import(fileName)
  127. local data = _file.Read(fileName, true) or "";
  128.  
  129. for k, v in ipairs( string.Explode("\n", data) ) do
  130. if ( v != "" and !string.find(v, "^%s$") ) then
  131. if ( !string.find(v, "^%[.+%]$") and !string.find(v, "^//") ) then
  132. local class, key, value = string.match(v, "^(.-)%s(.-)%s=%s(.+);");
  133.  
  134. if (class and key and value) then
  135. if ( string.find(class, "boolean") ) then
  136. value = (value == "true" or value == "yes" or value == "1");
  137. elseif ( string.find(class, "number") ) then
  138. value = tonumber(value);
  139. end;
  140.  
  141. local forceSet = string.find(class, "force") != nil;
  142. local isGlobal = string.find(class, "global") != nil;
  143. local isShared = string.find(class, "shared") != nil;
  144. local isStatic = string.find(class, "static") != nil;
  145. local isPrivate = string.find(class, "private") != nil;
  146. local needsRestart = string.find(class, "restart") != nil;
  147.  
  148. if (value) then
  149. local config = self:Get(key);
  150.  
  151. if ( !config:IsValid() ) then
  152. self:Add(key, value, isShared, isGlobal, isStatic, isPrivate, needsRestart);
  153. else
  154. config:Set(value, nil, forceSet);
  155. end;
  156. end;
  157. end;
  158. end;
  159. end;
  160. end;
  161. end;
  162.  
  163. -- A function to parse config keys.
  164. function openAura.config:Parse(text)
  165. for key in string.gmatch(text, "%$(.-)%$") do
  166. local value = self:Get(key):Get();
  167.  
  168. if (value != nil) then
  169. text = openAura:Replace( text, "$"..key.."$", tostring(value) );
  170. end;
  171. end;
  172.  
  173. return text;
  174. end;
  175.  
  176. -- A function to get a config object.
  177. function openAura.config:Get(key)
  178. if ( !self.cache[key] ) then
  179. local configObject = self.class:Create(key);
  180.  
  181. if (configObject.data) then
  182. self.cache[key] = configObject;
  183. end;
  184.  
  185. return configObject;
  186. else
  187. return self.cache[key];
  188. end;
  189. end;
  190.  
  191. if (SERVER) then
  192. function openAura.config:Save(fileName, configTable)
  193. if (configTable) then
  194. local config = { global = {}, schema = {} };
  195.  
  196. for k, v in pairs(configTable) do
  197. if ( !v.map and !v.temporary and !string.find(k, "mysql_") ) then
  198. local value = v.value;
  199.  
  200. if (v.nextValue != nil) then
  201. value = v.nextValue;
  202. end;
  203.  
  204. if (value != v.default) then
  205. if (v.isGlobal) then
  206. config.global[k] = {
  207. value = value,
  208. default = v.default
  209. };
  210. else
  211. config.schema[k] = {
  212. value = value,
  213. default = v.default
  214. };
  215. end;
  216. end;
  217. end;
  218. end;
  219.  
  220. openAura:SaveOpenAuraData(fileName, config.global);
  221. openAura:SaveSchemaData(fileName, config.schema);
  222. else
  223. openAura:DeleteOpenAuraData(fileName);
  224. openAura:DeleteSchemaData(fileName);
  225. end;
  226. end;
  227.  
  228. -- A function to send the config to a player.
  229. function openAura.config:Send(player, key)
  230. if ( player and player:IsBot() ) then
  231. openAura.plugin:Call("PlayerConfigInitialized", player);
  232. player.configInitialized = true;
  233. return;
  234. end;
  235.  
  236. if (!player) then
  237. player = _player.GetAll();
  238. else
  239. player = {player};
  240. end;
  241.  
  242. if (key) then
  243. if ( self.stored[key] ) then
  244. local value = self.stored[key].value;
  245.  
  246. if (self.stored[key].isShared) then
  247. if ( self.indexes[key] ) then
  248. key = self.indexes[key];
  249. end;
  250.  
  251. openAura:StartDataStream( player, "Config", { [key] = value } );
  252. end;
  253. end;
  254. else
  255. local config = {};
  256.  
  257. for k, v in pairs(self.stored) do
  258. if (v.isShared) then
  259. local index = self.indexes[k];
  260.  
  261. if (index) then
  262. config[index] = v.value;
  263. else
  264. config[k] = v.value;
  265. end;
  266. end;
  267. end;
  268.  
  269. openAura:StartDataStream(player, "Config", config);
  270. end;
  271. end;
  272.  
  273. -- A function to load config from a _file.
  274. function openAura.config:Load(fileName, loadGlobal)
  275. if (!fileName) then
  276. local configClasses = {"default", "map"};
  277. local configTable;
  278. local map = string.lower( game.GetMap() );
  279.  
  280. if (loadGlobal) then
  281. self.global = {
  282. default = self:Load("config", true),
  283. map = self:Load("config/"..map, true)
  284. };
  285.  
  286. configTable = self.global;
  287. else
  288. self.schema = {
  289. default = self:Load("config"),
  290. map = self:Load("config/"..map)
  291. };
  292.  
  293. configTable = self.schema;
  294. end;
  295.  
  296. for k, v in pairs(configClasses) do
  297. for k2, v2 in pairs( configTable[v] ) do
  298. local configObject = self:Get(k2);
  299.  
  300. if ( configObject:IsValid() ) then
  301. if (configObject:Query("default") == v2.default) then
  302. if (v == "map") then
  303. configObject:Set(v2.value, map, true);
  304. else
  305. configObject:Set(v2.value, nil, true);
  306. end;
  307. end;
  308. end;
  309. end;
  310. end;
  311. elseif (loadGlobal) then
  312. return openAura:RestoreOpenAuraData(fileName);
  313. else
  314. return openAura:RestoreSchemaData(fileName);
  315. end;
  316. end;
  317.  
  318. -- A function to add a new config key.
  319. function openAura.config:Add(key, value, isShared, isGlobal, isStatic, isPrivate, needsRestart)
  320. if ( self:IsValidValue(value) ) then
  321. if ( !self.stored[key] ) then
  322. self.stored[key] = {
  323. needsRestart = needsRestart,
  324. isPrivate = isPrivate,
  325. isShared = isShared,
  326. isStatic = isStatic,
  327. isGlobal = isGlobal,
  328. default = value,
  329. value = value
  330. };
  331.  
  332. local configClasses = {"global", "schema"};
  333. local configObject = self.class:Create(key);
  334.  
  335. if (!isGlobal) then
  336. table.remove(configClasses, 1);
  337. end;
  338.  
  339. for k, v in pairs(configClasses) do
  340. local configTable = openAura.config[v];
  341. local map = string.lower( game.GetMap() );
  342.  
  343. if ( configTable and configTable.default and configTable.default[key] ) then
  344. if (configObject:Query("default") == configTable.default[key].default) then
  345. configObject:Set(configTable.default[key].value, nil, true);
  346. end;
  347. end;
  348.  
  349. if ( configTable and configTable.map and configTable.map[key] ) then
  350. if (configObject:Query("default") == configTable.map[key].default) then
  351. configObject:Set(configTable.map[key].value, map, true);
  352. end;
  353. end;
  354. end;
  355.  
  356. self:Send(nil, key);
  357.  
  358. return configObject;
  359. end;
  360. end;
  361. end;
  362.  
  363. -- A function to set the config's value.
  364. function openAura.config.class:Set(value, map, forceSet, temporary)
  365. if (map) then
  366. map = string.lower(map);
  367. end;
  368.  
  369. if (tostring(value) == "-1.#IND") then
  370. value = 0;
  371. end;
  372.  
  373. if ( self.data and openAura.config:IsValidValue(value) ) then
  374. if (self.data.value != value) then
  375. local previousValue = self.data.value;
  376. local default = (value == "!default");
  377.  
  378. if (!default) then
  379. if (type(self.data.value) == "number") then
  380. value = tonumber(value) or self.data.value;
  381. elseif (type(self.data.value) == "boolean") then
  382. value = (value == true or value == "true"
  383. or value == "yes" or value == "1" or value == 1);
  384. end;
  385. else
  386. value = self.data.default;
  387. end;
  388.  
  389. if (!self.data.isStatic or forceSet) then
  390. if (!map or string.lower( game.GetMap() ) == map) then
  391. if ( (!openAura.config:HasInitialized() and self.data.value == self.data.default)
  392. or !self.data.needsRestart or forceSet ) then
  393. self.data.value = value;
  394.  
  395. if (self.data.isShared) then
  396. openAura.config:Send(nil, self.key);
  397. end;
  398. end;
  399. end;
  400.  
  401. if ( openAura.config:HasInitialized() ) then
  402. self.data.temporary = temporary;
  403. self.data.forceSet = forceSet;
  404. self.data.map = map;
  405.  
  406. if (self.data.needsRestart) then
  407. if (self.data.forceSet) then
  408. self.data.nextValue = nil;
  409. else
  410. self.data.nextValue = value;
  411. end;
  412. end;
  413.  
  414. if (!self.data.map and !self.data.temporary) then
  415. openAura.config:Save("config", openAura.config.stored);
  416. end;
  417.  
  418. if (self.data.map) then
  419. if (default) then
  420. if ( openAura.config.map[self.data.map] ) then
  421. openAura.config.map[self.data.map][self.key] = nil;
  422. end;
  423. else
  424. if ( !openAura.config.map[self.data.map] ) then
  425. openAura.config.map[self.data.map] = {};
  426. end;
  427.  
  428. openAura.config.map[self.data.map][self.key] = {
  429. default = self.data.default,
  430. global = self.data.isGlobal,
  431. value = value
  432. };
  433. end;
  434.  
  435. if (!self.data.temporary) then
  436. openAura.config:Save( "config/"..self.data.map, openAura.config.map[self.data.map] );
  437. end;
  438. end;
  439. end;
  440. end;
  441.  
  442. if ( self.data.value != previousValue and openAura.config:HasInitialized() ) then
  443. openAura.plugin:Call("OpenAuraConfigChanged", self.key, self.data, previousValue, self.data.value);
  444. end;
  445. end;
  446.  
  447. return value;
  448. end;
  449. end;
  450.  
  451. openAura:HookDataStream("ConfigInitialized", function(player, data)
  452. if ( !player:HasConfigInitialized() ) then
  453. player:SetConfigInitialized(true);
  454.  
  455. openAura.plugin:Call("PlayerConfigInitialized", player);
  456. end;
  457. end);
  458. else
  459. openAura.config.aurawatch = {};
  460.  
  461. openAura:HookDataStream("Config", function(data)
  462. for k, v in pairs(data) do
  463. if ( openAura.config.indexes[k] ) then
  464. k = openAura.config.indexes[k];
  465. end;
  466.  
  467. if ( !openAura.config.stored[k] ) then
  468. openAura.config:Add(k, v);
  469. else
  470. openAura.config:Get(k):Set(v);
  471. end;
  472. end;
  473.  
  474. if ( !openAura.config:HasInitialized() ) then
  475. openAura.config:SetInitialized(true);
  476.  
  477. for k, v in pairs(openAura.config.stored) do
  478. openAura.plugin:Call("OpenAuraConfigInitialized", k, v.value);
  479. end;
  480.  
  481. if ( IsValid(openAura.Client) and !openAura.config:HasSentInitialized() ) then
  482. openAura:StartDataStream("ConfigInitialized", true);
  483.  
  484. openAura.config:SetSentInitialized(true);
  485. end;
  486. end;
  487. end);
  488.  
  489. -- A function to get whether the config has sent initialized.
  490. function openAura.config:SetSentInitialized(sentInitialized)
  491. self.sentInitialized = sentInitialized;
  492. end;
  493.  
  494. -- A function to get whether the config has sent initialized.
  495. function openAura.config:HasSentInitialized()
  496. return self.sentInitialized;
  497. end;
  498.  
  499. -- A function to set a config key's aurawatch.
  500. function openAura.config:AddAuraWatch(key, help, minimum, maximum, decimals)
  501. self.aurawatch[key] = {
  502. decimals = decimals or 0,
  503. maximum = maximum or 100,
  504. minimum = minimum or 0,
  505. help = help or "No help was provided for this config key!"
  506. };
  507. end;
  508.  
  509. -- A function to get a config key's aurawatch.
  510. function openAura.config:GetAuraWatch(key)
  511. return self.aurawatch[key];
  512. end;
  513.  
  514. -- A function to add a new config key.
  515. function openAura.config:Add(key, value)
  516. if ( self:IsValidValue(value) ) then
  517. if ( !self.stored[key] ) then
  518. self.stored[key] = {
  519. default = value,
  520. value = value
  521. };
  522.  
  523. return self.class:Create(key);
  524. end;
  525. end;
  526. end;
  527.  
  528. -- A function to set the config's value.
  529. function openAura.config.class:Set(value)
  530. if (tostring(value) == "-1.#IND") then
  531. value = 0;
  532. end;
  533.  
  534. if ( self.data and openAura.config:IsValidValue(value) ) then
  535. if (self.data.value != value) then
  536. local previousValue = self.data.value;
  537. local default = (value == "!default");
  538.  
  539. if (!default) then
  540. if (type(self.data.value) == "number") then
  541. value = tonumber(value) or self.data.value;
  542. elseif (type(self.data.value) == "boolean") then
  543. value = (value == true or value == "true"
  544. or value == "yes" or value == "1" or value == 1);
  545. elseif ( type(self.data.value) != type(value) ) then
  546. return;
  547. end;
  548.  
  549. self.data.value = value;
  550. else
  551. self.data.value = self.data.default;
  552. end;
  553.  
  554. if ( self.data.value != previousValue and openAura.config:HasInitialized() ) then
  555. openAura.plugin:Call("OpenAuraConfigChanged", self.key, self.data, previousValue, self.data.value);
  556. end;
  557. end;
  558.  
  559. return self.data.value;
  560. end;
  561. end;
  562. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement