PageRenderTime 68ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/NRaasStoryProgression/StoryProgressionSpace/GenericOptionBase.cs

https://gitlab.com/forpdfsending/NRaas
C# | 1634 lines | 1301 code | 316 blank | 17 comment | 189 complexity | 6735f8deab021e0f06af444bb2c18963 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. using NRaas.CommonSpace.Converters;
  2. using NRaas.CommonSpace.Helpers;
  3. using NRaas.CommonSpace.Options;
  4. using NRaas.CommonSpace.Selection;
  5. using NRaas.StoryProgressionSpace.Helpers;
  6. using NRaas.StoryProgressionSpace.Interfaces;
  7. using NRaas.StoryProgressionSpace.Managers;
  8. using NRaas.StoryProgressionSpace.Options;
  9. using NRaas.StoryProgressionSpace.Scenarios.Romances;
  10. using Sims3.Gameplay.Actors;
  11. using Sims3.Gameplay.ActorSystems;
  12. using Sims3.Gameplay.CAS;
  13. using Sims3.Gameplay.Core;
  14. using Sims3.Gameplay.UI;
  15. using Sims3.Gameplay.Utilities;
  16. using Sims3.SimIFace;
  17. using Sims3.UI;
  18. using System;
  19. using System.Collections.Generic;
  20. namespace NRaas.StoryProgressionSpace
  21. {
  22. [Persistable]
  23. public abstract class GenericOptionBase : PersistentOptionBase, IInstallationBase
  24. {
  25. public enum DefaultingLevel
  26. {
  27. None = 0x0,
  28. Castes = 0x1,
  29. HeadOfFamily = 0x2,
  30. All = Castes | HeadOfFamily,
  31. }
  32. static Dictionary<Type, IGenericLevelOption> sDefaultOptions = new Dictionary<Type, IGenericLevelOption>();
  33. [Persistable(false)]
  34. Dictionary<Type, IGenericLevelOption> mGenericOptions = new Dictionary<Type, IGenericLevelOption>();
  35. [Persistable(false)]
  36. Dictionary<Type, IGenericLevelOption> mCachedOptions = new Dictionary<Type, IGenericLevelOption>();
  37. public GenericOptionBase()
  38. { }
  39. public abstract string Name
  40. {
  41. get;
  42. }
  43. public bool AddOption(IGenericLevelOption option)
  44. {
  45. Type type = option.GetType();
  46. mGenericOptions.Remove(type);
  47. mCachedOptions.Remove(type);
  48. if (!IsValidOption(option))
  49. {
  50. return false;
  51. }
  52. mGenericOptions.Add(type, option);
  53. return true;
  54. }
  55. public void RemoveOption(IGenericLevelOption option)
  56. {
  57. Type type = option.GetType();
  58. mGenericOptions.Remove(type);
  59. mCachedOptions.Remove(type);
  60. Remove(option as OptionItem);
  61. }
  62. public abstract SimDescription SimDescription
  63. {
  64. get;
  65. }
  66. public abstract Household House
  67. {
  68. get;
  69. }
  70. public abstract Lot Lot
  71. {
  72. get;
  73. }
  74. private void Clear()
  75. {
  76. List<IGenericLevelOption> options = new List<IGenericLevelOption>(mGenericOptions.Values);
  77. foreach (IGenericLevelOption option in options)
  78. {
  79. option.Manager = this;
  80. if (!option.ShouldDisplay()) continue;
  81. RemoveOption(option);
  82. UpdateInheritors(option);
  83. }
  84. }
  85. protected abstract void GetParentOptions(List<ParentItem> options, DefaultingLevel level);
  86. protected T GetDefaultOption<T>()
  87. where T : OptionItem, IGenericLevelOption, new()
  88. {
  89. IGenericLevelOption option;
  90. if (!sDefaultOptions.TryGetValue(typeof(T), out option))
  91. {
  92. option = new T();
  93. option.InitDefaultValue();
  94. sDefaultOptions.Add(typeof(T), option);
  95. }
  96. return option as T;
  97. }
  98. protected T GetInternalOption<T>()
  99. where T : OptionItem, IGenericLevelOption
  100. {
  101. return GetInheritedOption(typeof(T)) as T;
  102. }
  103. protected R GetInternalValue<T, R>()
  104. where T : GenericOptionItem<R>, IGenericLevelOption, new()
  105. {
  106. T item = GetInternalOption<T>();
  107. if (item == null)
  108. {
  109. item = GetDefaultOption<T>();
  110. }
  111. GenericOptionBase trueManager = item.Manager;
  112. try
  113. {
  114. item.Manager = this;
  115. return item.Value;
  116. }
  117. finally
  118. {
  119. item.Manager = trueManager;
  120. }
  121. }
  122. protected bool HasInternalValue<T, TType>(TType value)
  123. where T : OptionItem, IGenericHasOption<TType>, IGenericLevelOption, new()
  124. {
  125. T option = GetInternalOption<T>();
  126. if (option == null)
  127. {
  128. option = GetDefaultOption<T>();
  129. }
  130. GenericOptionBase trueManager = option.Manager;
  131. try
  132. {
  133. option.Manager = this;
  134. return option.Contains(value);
  135. }
  136. finally
  137. {
  138. option.Manager = trueManager;
  139. }
  140. }
  141. protected bool HasAnyInternalValue<T, TType>()
  142. where T : OptionItem, IGenericHasOption<TType>, IGenericLevelOption, new()
  143. {
  144. T option = GetInternalOption<T>();
  145. if (option == null)
  146. {
  147. option = GetDefaultOption<T>();
  148. }
  149. GenericOptionBase trueManager = option.Manager;
  150. try
  151. {
  152. option.Manager = this;
  153. return (option.Count > 0);
  154. }
  155. finally
  156. {
  157. option.Manager = trueManager;
  158. }
  159. }
  160. /*
  161. public bool CopyOption(IGenericLevelOption option)
  162. {
  163. IGenericLevelOption newOption = GetLocalOption(option.GetType());
  164. if (newOption == null) return false;
  165. newOption.Set(option, true);
  166. return true;
  167. }
  168. */
  169. public void Import(Manager manager, string key, Persistence.Lookup settings)
  170. {
  171. Clear();
  172. //Common.StringBuilder msg = new Common.StringBuilder();
  173. foreach (DefaultableOption option in GetOptions(manager, key, false))
  174. {
  175. List<string> names = PersistenceEx.GetExportKey(option);
  176. if (names == null) continue;
  177. for (int i = 0; i < names.Count; i++)
  178. {
  179. string name = names[i];
  180. string value = settings.GetString(name);
  181. if (value == null) continue;
  182. try
  183. {
  184. option.Clear(this);
  185. //msg += Common.NewLine + "Name: " + option.Name;
  186. //msg += Common.NewLine + option.GetUIValue(false);
  187. //msg += Common.NewLine + value;
  188. option.PersistValue = value;
  189. //msg += Common.NewLine + option.GetUIValue(false);
  190. }
  191. catch (Exception e)
  192. {
  193. Common.Exception("Name: " + name + Common.NewLine + "Value: " + value, e);
  194. }
  195. }
  196. }
  197. //Common.WriteLog(msg);
  198. }
  199. public void Export(Manager manager, string key, Persistence.Lookup settings)
  200. {
  201. foreach (OptionItem option in mGenericOptions.Values)
  202. {
  203. if (option is INotExportableOption) continue;
  204. if (option == null) continue;
  205. try
  206. {
  207. List<string> names = PersistenceEx.GetExportKey(option);
  208. if ((names == null) || (names.Count == 0)) continue;
  209. settings.Add(key + names[0], option.GetExportValue());
  210. }
  211. catch (Exception e)
  212. {
  213. Common.Exception(option.Name, e);
  214. }
  215. }
  216. }
  217. protected T GetLocalOption<T>()
  218. where T : OptionItem, IGenericLevelOption, new()
  219. {
  220. return GetLocalOption(typeof(T)) as T;
  221. }
  222. protected IGenericLevelOption GetLocalOption(Type type)
  223. {
  224. IGenericLevelOption item = GetInternalOption(type) as IGenericLevelOption;
  225. if (item == null)
  226. {
  227. item = type.GetConstructor(new Type[0]).Invoke(new object[0]) as IGenericLevelOption;
  228. if (!Installer<GenericOptionBase>.Install(this, item, true)) return null;
  229. }
  230. item.Manager = this;
  231. return item;
  232. }
  233. protected bool SetInternalValue<T, R>(R value)
  234. where T : GenericOptionItem<R>, IGenericLevelOption, new()
  235. {
  236. T item = GetLocalOption<T>();
  237. if (item == null) return false;
  238. item.SetValue (value);
  239. UpdateInheritors(item);
  240. return true;
  241. }
  242. protected TType AddInternalValue<T, TType>(TType value)
  243. where T : OptionItem, IGenericAddOption<TType>, IGenericLevelOption, new()
  244. {
  245. T item = GetLocalOption<T>();
  246. if (item == null) return default(TType);
  247. TType result = item.AddValue(value);
  248. UpdateInheritors(item);
  249. return result;
  250. }
  251. protected void RemoveInternalValue<T, TType>(TType value)
  252. where T : OptionItem, IGenericRemoveOption<TType>, IGenericLevelOption, new()
  253. {
  254. T item = GetLocalOption<T>();
  255. if (item == null) return;
  256. item.RemoveValue(value);
  257. UpdateInheritors(item);
  258. }
  259. protected IGenericLevelOption GetCachedOption(Type optionType)
  260. {
  261. IGenericLevelOption item;
  262. if (mCachedOptions.TryGetValue(optionType, out item))
  263. {
  264. return item;
  265. }
  266. else
  267. {
  268. return null;
  269. }
  270. }
  271. protected IGenericLevelOption GetInternalOption(Type optionType)
  272. {
  273. IGenericLevelOption item;
  274. if (mGenericOptions.TryGetValue(optionType, out item))
  275. {
  276. item.Manager = this;
  277. return item;
  278. }
  279. else
  280. {
  281. return null;
  282. }
  283. }
  284. public virtual void InvalidateCache()
  285. {
  286. mCachedOptions.Clear();
  287. }
  288. public void Uncache(IGenericLevelOption option)
  289. {
  290. mCachedOptions.Remove(option.GetType());
  291. }
  292. public void CopyOptions(GenericOptionBase options)
  293. {
  294. foreach (IGenericLevelOption option in options.mGenericOptions.Values)
  295. {
  296. AddOption(option.Clone() as IGenericLevelOption);
  297. }
  298. }
  299. public List<ParentItem> GetCombinedParents(DefaultingLevel level)
  300. {
  301. List<ParentItem> parents = new List<ParentItem>();
  302. parents.Add(new ParentItem(this, level));
  303. int i = 0;
  304. while (i < parents.Count)
  305. {
  306. ParentItem parent = parents[i];
  307. List<ParentItem> newParents = new List<ParentItem>();
  308. parent.mParent.GetParentOptions(newParents, parent.mLevel);
  309. i++;
  310. bool first = true;
  311. foreach (ParentItem newParent in newParents)
  312. {
  313. if (first)
  314. {
  315. parents.Add(newParent);
  316. first = false;
  317. }
  318. else
  319. {
  320. parents.Insert(i, newParent);
  321. }
  322. }
  323. }
  324. return parents;
  325. }
  326. public struct ParentItem
  327. {
  328. public GenericOptionBase mParent;
  329. public DefaultingLevel mLevel;
  330. public ParentItem(GenericOptionBase parent, DefaultingLevel level)
  331. {
  332. mParent = parent;
  333. mLevel = level;
  334. }
  335. }
  336. protected IGenericLevelOption GetInheritedOption(Type optionType)
  337. {
  338. IGenericLevelOption item = GetCachedOption(optionType);
  339. if (item == null)
  340. {
  341. DefaultingLevel level = DefaultingLevel.None;
  342. if (!typeof(INotHeadOfFamilyCasteLevelOption).IsAssignableFrom(optionType))
  343. {
  344. level |= DefaultingLevel.HeadOfFamily;
  345. }
  346. if (!typeof(INotCasteLevelOption).IsAssignableFrom(optionType))
  347. {
  348. level |= DefaultingLevel.Castes;
  349. }
  350. List<ParentItem> parents = GetCombinedParents(level);
  351. for (int i = parents.Count-1; i >= 0; i--)
  352. {
  353. ParentItem parent = parents[i];
  354. IGenericLevelOption parentItem = parent.mParent.GetInternalOption(optionType);
  355. if (parentItem != null)
  356. {
  357. OverrideStyle style = OverrideStyle.None;
  358. if (i != 0)
  359. {
  360. style = OverrideStyle.ClearSet;
  361. }
  362. if (item == null)
  363. {
  364. item = parentItem.Clone() as IGenericLevelOption;
  365. item.ApplyOverride(parentItem, style);
  366. mCachedOptions[optionType] = item;
  367. }
  368. else
  369. {
  370. style |= OverrideStyle.CopyData;
  371. item.ApplyOverride(parentItem, style);
  372. item.Manager = parentItem.Manager;
  373. }
  374. }
  375. }
  376. }
  377. return item;
  378. }
  379. public static bool IsValidOption(GenericOptionBase manager, IGenericLevelOption option)
  380. {
  381. if (manager == null) return false;
  382. return manager.IsValidOption(option);
  383. }
  384. public abstract bool IsValidOption(IGenericLevelOption option);
  385. public virtual void UpdateInheritors(IGenericLevelOption option)
  386. {
  387. Uncache(option);
  388. }
  389. public override string ToString()
  390. {
  391. Common.StringBuilder text = new Common.StringBuilder();
  392. foreach (IGenericLevelOption option in mGenericOptions.Values)
  393. {
  394. option.Manager = this;
  395. text.AddXML(option.GetStoreKey(), option.PersistValue);
  396. }
  397. List<ParentItem> parents = GetCombinedParents(DefaultingLevel.All);
  398. if (parents.Count > 0)
  399. {
  400. bool found = false;
  401. foreach (ParentItem parent in parents)
  402. {
  403. if (parent.mParent == this) continue;
  404. if (!found)
  405. {
  406. text += Common.NewLine + "<Parent>";
  407. }
  408. found = true;
  409. text += Common.NewLine + parent.mParent.ToString();
  410. }
  411. if (found)
  412. {
  413. text += Common.NewLine + "</Parent>";
  414. }
  415. }
  416. else
  417. {
  418. text += Common.NewLine + "<NoParents/>";
  419. }
  420. return text.ToString();
  421. }
  422. public List<DefaultableOption> GetOptions(Manager manager, string prefix, bool checkDisplay)
  423. {
  424. List<IGenericLevelOption> simOptions = Common.DerivativeSearch.Find<IGenericLevelOption>(Common.DerivativeSearch.Caching.NoCache);
  425. if (manager.GetValue<Main.ShowHiddenOption, bool>())
  426. {
  427. checkDisplay = false;
  428. }
  429. bool debugging = manager.DebuggingEnabled;
  430. List<DefaultableOption> allOptions = new List<DefaultableOption>();
  431. foreach (IGenericLevelOption simOption in simOptions)
  432. {
  433. if (simOption is IDebuggingOption)
  434. {
  435. if (!debugging) continue;
  436. }
  437. DefaultableOption option = new DefaultableOption(this, prefix, simOption);
  438. if (option == null) continue;
  439. if (!option.ShouldDisplay(checkDisplay)) continue;
  440. allOptions.Add(option);
  441. }
  442. return allOptions;
  443. }
  444. public List<DefaultableOption> ListOptions(Manager manager, string localizedTitle, bool checkDisplay)
  445. {
  446. List<DefaultableOption> allOptions = GetOptions(manager, null, checkDisplay);
  447. bool okayed = false;
  448. List<DefaultableOption> selection = OptionItem.ListOptions(allOptions, localizedTitle, allOptions.Count, out okayed);
  449. if ((selection == null) || (selection.Count == 0)) return null;
  450. return selection;
  451. }
  452. public bool ShowOptions(Manager manager, string localizedTitle)
  453. {
  454. while (true)
  455. {
  456. List<DefaultableOption> selection = ListOptions(manager, localizedTitle, true);
  457. if (selection == null) return false;
  458. foreach (DefaultableOption option in selection)
  459. {
  460. option.Perform();
  461. }
  462. }
  463. }
  464. public virtual void ValidateCasteOptions(Dictionary<ulong, CasteOptions> castes)
  465. {
  466. foreach (IGenericLevelOption option in mGenericOptions.Values)
  467. {
  468. CasteListOption casteOption = option as CasteListOption;
  469. if (casteOption != null)
  470. {
  471. casteOption.Validate(castes);
  472. }
  473. }
  474. }
  475. public virtual void Restore()
  476. {
  477. OptionLogger.AddTrace("Restore: " + GetType());
  478. if (Lot != null)
  479. {
  480. OptionLogger.AddTrace("Lot: " + Lot.Address + " (" + Lot.LotId + ")");
  481. }
  482. if (House != null)
  483. {
  484. OptionLogger.AddTrace("House: " + House.Name);
  485. }
  486. if (SimDescription != null)
  487. {
  488. OptionLogger.AddTrace("Sim: " + SimDescription.FullName);
  489. }
  490. mGenericOptions.Clear();
  491. mCachedOptions.Clear();
  492. new Installer<GenericOptionBase> (this).Perform(false);
  493. List<IGenericLevelOption> options = new List<IGenericLevelOption>(mGenericOptions.Values);
  494. foreach (IGenericLevelOption option in options)
  495. {
  496. if (!Restore(option as OptionItem))
  497. {
  498. if (!RetainAllOptions)
  499. {
  500. Type type = option.GetType();
  501. mGenericOptions.Remove(type);
  502. mCachedOptions.Remove(type);
  503. }
  504. else if (Common.IsAwayFromHomeworld ())
  505. {
  506. IAdjustForVacationOption adjustForVacation = option as IAdjustForVacationOption;
  507. if (adjustForVacation != null)
  508. {
  509. adjustForVacation.AdjustForVacationTown();
  510. }
  511. }
  512. }
  513. }
  514. }
  515. protected virtual bool RetainAllOptions
  516. {
  517. get { return false; }
  518. }
  519. public class DefaultableOption : OptionItem, INotPersistableOption
  520. {
  521. GenericOptionBase mManager;
  522. IGenericLevelOption mOption;
  523. string mPrefix;
  524. bool mInherited = false;
  525. public DefaultableOption(GenericOptionBase manager, string prefix, IGenericLevelOption option)
  526. {
  527. mManager = manager;
  528. mOption = option.Clone() as IGenericLevelOption;
  529. mPrefix = prefix;
  530. mOption.Manager = mManager;
  531. IGenericLevelOption existingOption = mManager.GetInheritedOption(mOption.GetType ());
  532. if (existingOption != null)
  533. {
  534. mInherited = (existingOption.Manager != mManager);
  535. mOption.Set(existingOption, false);
  536. }
  537. else
  538. {
  539. mOption.InitDefaultValue();
  540. mInherited = true;
  541. }
  542. }
  543. public override string Name
  544. {
  545. get
  546. {
  547. return mOption.Name;
  548. }
  549. }
  550. public bool Resetable
  551. {
  552. get
  553. {
  554. if (mInherited) return false;
  555. if (mOption is INotPersistableOption) return false;
  556. return true;
  557. }
  558. }
  559. protected override string GetLocalizationValueKey()
  560. {
  561. return null;
  562. }
  563. public override string GetTitlePrefix()
  564. {
  565. return null;
  566. }
  567. public override string GetStoreKey()
  568. {
  569. if (mOption == null) return null;
  570. return mPrefix + mOption.GetStoreKey();
  571. }
  572. public void Clear(GenericOptionBase manager)
  573. {
  574. IGenericLevelOption option = manager.GetLocalOption(mOption.GetType());
  575. if (option == null) return;
  576. ICustomClearOption customClear = option as ICustomClearOption;
  577. if (customClear != null)
  578. {
  579. if (customClear.Clear(manager))
  580. {
  581. manager.RemoveOption(option);
  582. }
  583. else
  584. {
  585. option.Persist();
  586. }
  587. }
  588. else
  589. {
  590. manager.RemoveOption(option);
  591. }
  592. manager.UpdateInheritors(option);
  593. }
  594. public override string GetUIValue(bool pure)
  595. {
  596. string uiValue = mOption.GetUIValue(true);
  597. if ((!pure) && (!string.IsNullOrEmpty(uiValue)))
  598. {
  599. if (mInherited)
  600. {
  601. if ((!(mManager is CasteOptions)) || (mOption is ICasteFilterOption))
  602. {
  603. uiValue = "(" + uiValue + ")";
  604. }
  605. else
  606. {
  607. return null;
  608. }
  609. }
  610. }
  611. return uiValue;
  612. }
  613. public override object PersistValue
  614. {
  615. get
  616. {
  617. return mOption.PersistValue;
  618. }
  619. set
  620. {
  621. mOption.PersistValue = value;
  622. PostPerform(mManager, true);
  623. }
  624. }
  625. public override void NotifyAfterChange(string originalUIValue)
  626. { }
  627. // Used by MasterController Progression
  628. public void Persist(GenericOptionBase manager)
  629. {
  630. PostPerform(manager, false);
  631. }
  632. protected override bool PrivatePerform()
  633. {
  634. if (!mOption.Perform()) return false;
  635. PostPerform(mManager, false);
  636. return true;
  637. }
  638. protected void PostPerform(GenericOptionBase manager, bool clear)
  639. {
  640. if (manager == null) return;
  641. IGenericLevelOption option = manager.GetLocalOption(mOption.GetType());
  642. if (option != null)
  643. {
  644. if (clear)
  645. {
  646. option.Set(mOption, false);
  647. }
  648. else
  649. {
  650. option.ApplyOverride(mOption, OverrideStyle.CopyData | OverrideStyle.MergeSet);
  651. }
  652. option.Persist();
  653. manager.UpdateInheritors(option);
  654. }
  655. }
  656. public bool ShouldDisplay(bool checkDisplay)
  657. {
  658. if (checkDisplay)
  659. {
  660. return ShouldDisplay();
  661. }
  662. else
  663. {
  664. return mManager.IsValidOption(mOption);
  665. }
  666. }
  667. public override bool ShouldDisplay()
  668. {
  669. return mOption.ShouldDisplay();
  670. }
  671. }
  672. public abstract class BooleanOption : BooleanOptionItem, IGenericLevelOption
  673. {
  674. GenericOptionBase mManager = null;
  675. public BooleanOption(bool defValue)
  676. : base (defValue)
  677. { }
  678. public GenericOptionBase Manager
  679. {
  680. get { return mManager; }
  681. set { mManager = value; }
  682. }
  683. public virtual bool HasRequiredVersion()
  684. {
  685. return true;
  686. }
  687. public virtual bool Install(GenericOptionBase manager, bool initial)
  688. {
  689. if (!HasRequiredVersion()) return false;
  690. mManager = manager;
  691. return mManager.AddOption(this);
  692. }
  693. public virtual void Set(IGenericLevelOption option, bool persist)
  694. {
  695. BooleanOptionItem newOption = option as BooleanOptionItem;
  696. if (newOption == null) return;
  697. SetValue (newOption.PureValue, persist);
  698. }
  699. protected R GetValue<T, R>()
  700. where T : GenericOptionItem<R>, IGenericLevelOption, new()
  701. {
  702. return Manager.GetInternalValue<T, R>();
  703. }
  704. protected override PersistentOptionBase Store
  705. {
  706. get { return mManager; }
  707. }
  708. public void ApplyOverride(IGenericLevelOption paramOption, OverrideStyle style)
  709. {
  710. if ((style & OverrideStyle.CopyData) == OverrideStyle.CopyData)
  711. {
  712. BooleanOption option = paramOption as BooleanOption;
  713. if (option != null)
  714. {
  715. SetValue(option.Value, false);
  716. }
  717. }
  718. }
  719. public bool BaseShouldDisplay()
  720. {
  721. return Manager.IsValidOption(this);
  722. }
  723. public override bool ShouldDisplay()
  724. {
  725. return BaseShouldDisplay();
  726. }
  727. }
  728. public abstract class StringOption : StringOptionItem, IGenericLevelOption
  729. {
  730. GenericOptionBase mManager = null;
  731. public StringOption(string defValue)
  732. : base(defValue)
  733. { }
  734. public GenericOptionBase Manager
  735. {
  736. get { return mManager; }
  737. set { mManager = value; }
  738. }
  739. public virtual bool HasRequiredVersion()
  740. {
  741. return true;
  742. }
  743. public virtual bool Install(GenericOptionBase manager, bool initial)
  744. {
  745. if (!HasRequiredVersion()) return false;
  746. mManager = manager;
  747. return mManager.AddOption(this);
  748. }
  749. public virtual void Set(IGenericLevelOption option, bool persist)
  750. {
  751. StringOptionItem newOption = option as StringOptionItem;
  752. if (newOption == null) return;
  753. SetValue(newOption.PureValue, persist);
  754. }
  755. protected R GetValue<T, R>()
  756. where T : GenericOptionItem<R>, IGenericLevelOption, new()
  757. {
  758. return Manager.GetInternalValue<T, R>();
  759. }
  760. protected override PersistentOptionBase Store
  761. {
  762. get { return mManager; }
  763. }
  764. public void ApplyOverride(IGenericLevelOption paramOption, OverrideStyle style)
  765. {
  766. if ((style & OverrideStyle.CopyData) == OverrideStyle.CopyData)
  767. {
  768. StringOption option = paramOption as StringOption;
  769. if (option != null)
  770. {
  771. SetValue(option.Value, false);
  772. }
  773. }
  774. }
  775. public override bool ShouldDisplay()
  776. {
  777. return Manager.IsValidOption(this);
  778. }
  779. }
  780. public abstract class IntegerOption : IntegerOptionItem, IGenericLevelOption
  781. {
  782. GenericOptionBase mManager = null;
  783. public IntegerOption(int defValue)
  784. : base(defValue)
  785. { }
  786. public GenericOptionBase Manager
  787. {
  788. get { return mManager; }
  789. set { mManager = value; }
  790. }
  791. public virtual bool HasRequiredVersion()
  792. {
  793. return true;
  794. }
  795. public virtual bool Install(GenericOptionBase manager, bool initial)
  796. {
  797. if (!HasRequiredVersion()) return false;
  798. mManager = manager;
  799. return mManager.AddOption(this);
  800. }
  801. public void Set(IGenericLevelOption option, bool persist)
  802. {
  803. IntegerOptionItem newOption = option as IntegerOptionItem;
  804. if (newOption == null) return;
  805. SetValue(newOption.PureValue, persist);
  806. }
  807. protected R GetValue<T, R>()
  808. where T : GenericOptionItem<R>, IGenericLevelOption, new()
  809. {
  810. return Manager.GetInternalValue<T, R>();
  811. }
  812. protected override PersistentOptionBase Store
  813. {
  814. get { return mManager; }
  815. }
  816. public void ApplyOverride(IGenericLevelOption paramOption, OverrideStyle style)
  817. {
  818. if ((style & OverrideStyle.CopyData) == OverrideStyle.CopyData)
  819. {
  820. IntegerOption option = paramOption as IntegerOption;
  821. if (option != null)
  822. {
  823. SetValue(option.Value, false);
  824. }
  825. }
  826. }
  827. public override bool ShouldDisplay()
  828. {
  829. return Manager.IsValidOption(this);
  830. }
  831. }
  832. public abstract class GenericOption<TValue> : GenericOptionItem<TValue>, IGenericLevelOption
  833. {
  834. GenericOptionBase mManager = null;
  835. public GenericOption(TValue value, TValue defValue)
  836. : base(value, defValue)
  837. { }
  838. public GenericOptionBase Manager
  839. {
  840. get { return mManager; }
  841. set { mManager = value; }
  842. }
  843. public virtual bool HasRequiredVersion()
  844. {
  845. return true;
  846. }
  847. public virtual bool Install(GenericOptionBase manager, bool initial)
  848. {
  849. if (!HasRequiredVersion()) return false;
  850. mManager = manager;
  851. return mManager.AddOption(this);
  852. }
  853. public virtual void Set(IGenericLevelOption option, bool persist)
  854. {
  855. GenericOptionItem<TValue> newOption = option as GenericOptionItem<TValue>;
  856. if (newOption == null) return;
  857. SetValue(newOption.PureValue, persist);
  858. }
  859. protected R GetValue<T, R>()
  860. where T : GenericOptionItem<R>, IGenericLevelOption, new()
  861. {
  862. return Manager.GetInternalValue<T, R>();
  863. }
  864. protected override PersistentOptionBase Store
  865. {
  866. get { return mManager; }
  867. }
  868. public virtual void ApplyOverride(IGenericLevelOption paramOption, OverrideStyle style)
  869. {
  870. if ((style & OverrideStyle.CopyData) == OverrideStyle.CopyData)
  871. {
  872. GenericOption<TValue> option = paramOption as GenericOption<TValue>;
  873. if (option != null)
  874. {
  875. SetValue(option.Value, false);
  876. }
  877. }
  878. }
  879. public override bool ShouldDisplay()
  880. {
  881. return Manager.IsValidOption(this);
  882. }
  883. public override ICommonOptionItem Clone()
  884. {
  885. IGenericLevelOption result = base.Clone() as IGenericLevelOption;
  886. result.Set(this, false);
  887. return result;
  888. }
  889. }
  890. public abstract class ChoiceOptionItem<T> : GenericOption<T>
  891. {
  892. public ChoiceOptionItem(T value, T defValue)
  893. : base(value, defValue)
  894. { }
  895. protected abstract string GetLocalizedValue(T value, ref bool matches, ref ThumbnailKey icon);
  896. public override void Set(IGenericLevelOption option, bool persist)
  897. {
  898. ChoiceOptionItem<T> newOption = option as ChoiceOptionItem<T>;
  899. if (newOption == null) return;
  900. SetValue(newOption.PureValue, persist);
  901. }
  902. protected virtual bool Allow(T value)
  903. {
  904. return true;
  905. }
  906. protected virtual string ValuePrefix
  907. {
  908. get { return "Boolean"; }
  909. }
  910. protected abstract IEnumerable<T> GetOptions();
  911. protected override bool PrivatePerform()
  912. {
  913. List<Item> choices = new List<Item>();
  914. foreach (T choice in GetOptions())
  915. {
  916. if (!Allow(choice)) continue;
  917. bool matches = false;
  918. ThumbnailKey icon = ThumbnailKey.kInvalidThumbnailKey;
  919. string name = GetLocalizedValue(choice, ref matches, ref icon);
  920. choices.Add(new Item(choice, name, ValuePrefix, matches, icon));
  921. }
  922. Item selection = new CommonSelection<Item>(Name, choices).SelectSingle();
  923. if (selection == null) return false;
  924. SetValue(selection.Value);
  925. return true;
  926. }
  927. public class Item : ValueSettingOption<T>
  928. {
  929. string mValuePrefix;
  930. public Item(T value, string name, string valuePrefix, bool selected, ThumbnailKey thumbnail)
  931. : base(value, name, selected ? 1 : 0, thumbnail)
  932. {
  933. mValuePrefix = valuePrefix;
  934. }
  935. public override string DisplayKey
  936. {
  937. get { return mValuePrefix; }
  938. }
  939. }
  940. }
  941. public abstract class EnumOptionItem<T> : ChoiceOptionItem<T>
  942. where T : struct
  943. {
  944. public EnumOptionItem(T value, T defValue)
  945. : base(value, defValue)
  946. { }
  947. protected override IEnumerable<T> GetOptions()
  948. {
  949. List<T> results = new List<T>();
  950. foreach(T choice in Enum.GetValues(typeof(T)))
  951. {
  952. results.Add(choice);
  953. }
  954. return results;
  955. }
  956. public override object PersistValue
  957. {
  958. set
  959. {
  960. if (value is string)
  961. {
  962. T result;
  963. ParserFunctions.TryParseEnum<T>(value as string, out result, Default);
  964. SetValue(result);
  965. }
  966. else
  967. {
  968. SetValue((T)value);
  969. }
  970. }
  971. }
  972. }
  973. public abstract class ListedOptionItem<TRawType, TStorageType> : GenericOption<List<TStorageType>>, IGenericHasOption<TRawType>, IGenericAddOption<TRawType>, IGenericRemoveOption<TRawType>, ICustomClearOption
  974. {
  975. Dictionary<TStorageType, bool> mLookup = null;
  976. List<TStorageType> mSet = new List<TStorageType>();
  977. public ListedOptionItem(List<TStorageType> value, List<TStorageType> defValue)
  978. : base(value, defValue)
  979. { }
  980. public override int Count
  981. {
  982. get
  983. {
  984. return Value.Count;
  985. }
  986. }
  987. protected abstract string GetLocalizationUIKey();
  988. protected IEnumerable<TStorageType> SetList
  989. {
  990. get { return mSet; }
  991. }
  992. protected Dictionary<TStorageType, bool> Lookup
  993. {
  994. get
  995. {
  996. if (mLookup == null)
  997. {
  998. mLookup = new Dictionary<TStorageType, bool>();
  999. foreach (TStorageType item in Value)
  1000. {
  1001. mLookup[item] = true;
  1002. }
  1003. }
  1004. return mLookup;
  1005. }
  1006. }
  1007. public override string GetUIValue(bool pure)
  1008. {
  1009. string prefix = GetLocalizationUIKey();
  1010. if (string.IsNullOrEmpty(prefix))
  1011. {
  1012. string text = null;
  1013. if (Lookup.Count > 0)
  1014. {
  1015. text = EAText.GetNumberString(Lookup.Count);
  1016. }
  1017. int difference = Lookup.Count - mSet.Count;
  1018. if (difference < 0)
  1019. {
  1020. if (!string.IsNullOrEmpty(text))
  1021. {
  1022. text = ":" + text;
  1023. }
  1024. text = EAText.GetNumberString(difference) + text;
  1025. }
  1026. return text;
  1027. }
  1028. else
  1029. {
  1030. string text = ConvertToUIValue(prefix, Value);
  1031. if ((!pure) && (!string.IsNullOrEmpty(text)))
  1032. {
  1033. if (text == ConvertToUIValue(prefix, Default))
  1034. {
  1035. text = "(" + text + ")";
  1036. }
  1037. }
  1038. return text;
  1039. }
  1040. }
  1041. protected string ConvertToUIValue(string prefix, IEnumerable<TStorageType> list)
  1042. {
  1043. return new Converter(prefix, IsFemaleLocalization()).Convert(list);
  1044. }
  1045. public TRawType AddValue(TRawType value)
  1046. {
  1047. bool valid;
  1048. TStorageType type = ConvertToValue(value, out valid);
  1049. if (!valid) return value;
  1050. if (!ContainsDirect(type))
  1051. {
  1052. Value.Add(type);
  1053. AddSet(type);
  1054. Persist();
  1055. }
  1056. return value;
  1057. }
  1058. public void RemoveValue(TRawType value)
  1059. {
  1060. bool valid;
  1061. TStorageType storeValue = ConvertToValue(value, out valid);
  1062. if (!valid) return;
  1063. Value.Remove(storeValue);
  1064. Persist();
  1065. }
  1066. protected void RemoveDirectValue(TStorageType value, bool persist)
  1067. {
  1068. Value.Remove(value);
  1069. mSet.Remove(value);
  1070. mLookup = null;
  1071. if (persist)
  1072. {
  1073. Persist();
  1074. }
  1075. }
  1076. public override void SetValue(List<TStorageType> value, bool persist)
  1077. {
  1078. if (value == null)
  1079. {
  1080. value = new List<TStorageType>();
  1081. }
  1082. mLookup = null;
  1083. base.SetValue(value, persist);
  1084. }
  1085. public bool ContainsDirect(TStorageType value)
  1086. {
  1087. return Lookup.ContainsKey(value);
  1088. }
  1089. public bool Contains(TRawType value)
  1090. {
  1091. bool valid;
  1092. TStorageType storeValue = ConvertToValue(value, out valid);
  1093. if (!valid) return false;
  1094. return Lookup.ContainsKey(storeValue);
  1095. }
  1096. protected void AddUnique(TStorageType value)
  1097. {
  1098. if (ContainsDirect(value)) return;
  1099. Value.Add(value);
  1100. }
  1101. protected void AddSet(TStorageType value)
  1102. {
  1103. mSet.Add(value);
  1104. }
  1105. protected abstract string ValuePrefix
  1106. {
  1107. get;
  1108. }
  1109. protected abstract TStorageType ConvertFromString(string value);
  1110. protected abstract TStorageType ConvertToValue(TRawType value, out bool valid);
  1111. protected override string GetLocalizationValueKey()
  1112. {
  1113. return null;
  1114. }
  1115. protected abstract string GetLocalizedValue(TRawType value, ref ThumbnailKey icon);
  1116. protected virtual bool Allow(TRawType value)
  1117. {
  1118. return true;
  1119. }
  1120. protected abstract IEnumerable<TRawType> GetOptions();
  1121. public override void Set(IGenericLevelOption option, bool persist)
  1122. {
  1123. ListedOptionItem<TRawType, TStorageType> newOption = option as ListedOptionItem<TRawType, TStorageType>;
  1124. if (newOption == null) return;
  1125. mSet = new List<TStorageType>(newOption.mSet);
  1126. SetValue(new List<TStorageType>(newOption.PureValue), persist);
  1127. }
  1128. public override void ApplyOverride(IGenericLevelOption option, OverrideStyle style)
  1129. {
  1130. ListedOptionItem<TRawType, TStorageType> overrideOption = option as ListedOptionItem<TRawType, TStorageType>;
  1131. if (overrideOption != null)
  1132. {
  1133. bool mergeSet = ((style & OverrideStyle.MergeSet) == OverrideStyle.MergeSet);
  1134. if ((style & OverrideStyle.CopyData) == OverrideStyle.CopyData)
  1135. {
  1136. foreach (TStorageType type in overrideOption.mSet)
  1137. {
  1138. if (overrideOption.ContainsDirect(type))
  1139. {
  1140. AddUnique(type);
  1141. }
  1142. else
  1143. {
  1144. Value.Remove(type);
  1145. }
  1146. if (mergeSet)
  1147. {
  1148. if (!mSet.Contains(type))
  1149. {
  1150. AddSet(type);
  1151. }
  1152. }
  1153. }
  1154. }
  1155. if ((style & OverrideStyle.ClearSet) == OverrideStyle.ClearSet)
  1156. {
  1157. mSet.Clear();
  1158. }
  1159. else if (!mergeSet)
  1160. {
  1161. mSet = new List<TStorageType>(overrideOption.mSet);
  1162. }
  1163. mLookup = null;
  1164. }
  1165. }
  1166. public bool Clear(GenericOptionBase manager)
  1167. {
  1168. List<Item> choices = new List<Item>();
  1169. foreach (TRawType choice in GetOptions())
  1170. {
  1171. bool valid;
  1172. TStorageType storeValue = ConvertToValue(choice, out valid);
  1173. if (!valid) continue;
  1174. if (!mSet.Contains(storeValue)) continue;
  1175. ThumbnailKey icon = ThumbnailKey.kInvalidThumbnailKey;
  1176. string name = GetLocalizedValue(choice, ref icon);
  1177. choices.Add(new Item(this, choice, name, icon));
  1178. }
  1179. CommonSelection<Item>.Results selection = new CommonSelection<Item>(Name, choices).SelectMultiple();
  1180. foreach (Item item in selection)
  1181. {
  1182. Value.Remove(item.Value);
  1183. mSet.Remove(item.Value);
  1184. }
  1185. mLookup = null;
  1186. return (mSet.Count == 0);
  1187. }
  1188. protected override bool PrivatePerform()
  1189. {
  1190. List<Item> choices = new List<Item>();
  1191. foreach (TRawType choice in GetOptions())
  1192. {
  1193. if (!Allow(choice)) continue;
  1194. ThumbnailKey icon = ThumbnailKey.kInvalidThumbnailKey;
  1195. string name = GetLocalizedValue(choice, ref icon);
  1196. choices.Add(new Item(this, choice, name, icon));
  1197. }
  1198. CommonSelection<Item>.Results selection = new CommonSelection<Item>(Name, choices).SelectMultiple();
  1199. foreach (Item item in selection)
  1200. {
  1201. if (ContainsDirect(item.Value))
  1202. {
  1203. Value.Remove(item.Value);
  1204. }
  1205. else
  1206. {
  1207. Value.Add(item.Value);
  1208. }
  1209. if (!mSet.Contains(item.Value))
  1210. {
  1211. AddSet(item.Value);
  1212. }
  1213. }
  1214. mLookup = null;
  1215. return true;
  1216. }
  1217. public override bool Persist()
  1218. {
  1219. mLookup = null;
  1220. return base.Persist();
  1221. }
  1222. public override object PersistValue
  1223. {
  1224. get
  1225. {
  1226. string result = new ListToString<TStorageType>().Convert(Value);
  1227. result += "|" + new ListToString<TStorageType>().Convert(mSet);
  1228. return result;
  1229. }
  1230. set
  1231. {
  1232. Value.Clear();
  1233. mSet.Clear();
  1234. if (value is string)
  1235. {
  1236. string[] values = (value as string).Split('|');
  1237. if (values.Length == 2)
  1238. {
  1239. foreach (string val in values[1].Split(','))
  1240. {
  1241. if (string.IsNullOrEmpty(val)) continue;
  1242. TStorageType entry = ConvertFromString(val);
  1243. if (entry != null)
  1244. {
  1245. AddSet(entry);
  1246. }
  1247. }
  1248. }
  1249. foreach (string val in values[0].Split(','))
  1250. {
  1251. if (string.IsNullOrEmpty(val)) continue;
  1252. TStorageType entry = ConvertFromString(val);
  1253. if (entry != null)
  1254. {
  1255. AddUnique(entry);
  1256. }
  1257. }
  1258. }
  1259. mLookup = null;
  1260. }
  1261. }
  1262. public class Converter : ListToString<TStorageType>
  1263. {
  1264. string mPrefixKey;
  1265. bool mIsFemale;
  1266. public Converter(string prefixKey, bool isFemale)
  1267. {
  1268. mPrefixKey = prefixKey;
  1269. mIsFemale = isFemale;
  1270. }
  1271. protected override string PrivateConvert(TStorageType value)
  1272. {
  1273. return Common.Localize(mPrefixKey + ":" + value, mIsFemale);
  1274. }
  1275. }
  1276. public class Item : ValueSettingOption<TStorageType>
  1277. {
  1278. ListedOptionItem<TRawType, TStorageType> mOption;
  1279. public Item(ListedOptionItem<TRawType, TStorageType> option, TRawType value, string name, ThumbnailKey thumbnail)
  1280. : base(ConvertToValue(option, value), name, -1, thumbnail)
  1281. {
  1282. mOption = option;
  1283. }
  1284. protected static TStorageType ConvertToValue(ListedOptionItem<TRawType, TStorageType> option, TRawType value)
  1285. {
  1286. bool valid;
  1287. TStorageType result = option.ConvertToValue(value, out valid);
  1288. if (!valid) return default(TStorageType);
  1289. return result;
  1290. }
  1291. public override string DisplayValue
  1292. {
  1293. get
  1294. {
  1295. if (string.IsNullOrEmpty(mOption.ValuePrefix)) return null;
  1296. string result = Common.Localize(mOption.ValuePrefix + ":" + mOption.ContainsDirect(mValue)).Trim();
  1297. if (!mOption.mSet.Contains(mValue))
  1298. {
  1299. if (!string.IsNullOrEmpty(result))
  1300. {
  1301. result = "(" + result + ")";
  1302. }
  1303. }
  1304. else
  1305. {
  1306. if (string.IsNullOrEmpty(result))
  1307. {
  1308. result = "---";
  1309. }

Large files files are truncated, but you can click here to view the full file