EstateLoaderFileSystem.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. namespace OpenSim.ApplicationPlugins.LoadRegions
  36. {
  37. public class EstateLoaderFileSystem : IEstateLoader
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. private IConfigSource m_configSource;
  41. private OpenSimBase m_application;
  42. public EstateLoaderFileSystem(OpenSimBase openSim)
  43. {
  44. m_application = openSim;
  45. }
  46. public void SetIniConfigSource(IConfigSource configSource)
  47. {
  48. m_configSource = configSource;
  49. }
  50. public void LoadEstates()
  51. {
  52. string estateConfigPath = Path.Combine(Util.configDir(), "Estates");
  53. IConfig startupConfig = m_configSource.Configs["Startup"];
  54. if(startupConfig == null)
  55. return;
  56. estateConfigPath = startupConfig.GetString("regionload_estatesdir", estateConfigPath).Trim();
  57. if(string.IsNullOrWhiteSpace(estateConfigPath))
  58. return;
  59. if (!Directory.Exists(estateConfigPath))
  60. return; // if nothing there, don't bother
  61. string[] iniFiles;
  62. try
  63. {
  64. iniFiles = Directory.GetFiles(estateConfigPath, "*.ini");
  65. }
  66. catch
  67. {
  68. m_log.Error("[ESTATE LOADER FILE SYSTEM]: could not open " + estateConfigPath);
  69. return;
  70. }
  71. // No Estate.ini Found
  72. if (iniFiles == null || iniFiles.Length == 0)
  73. return;
  74. m_log.InfoFormat("[ESTATE LOADER FILE SYSTEM]: Loading estate config files from {0}", estateConfigPath);
  75. List<int> existingEstates;
  76. List<int> existingEstateIDs = m_application.EstateDataService.GetEstatesAll();
  77. int i = 0;
  78. foreach (string file in iniFiles)
  79. {
  80. m_log.InfoFormat("[ESTATE LOADER FILE SYSTEM]: Loading config file {0}", file);
  81. IConfigSource source = null;
  82. try
  83. {
  84. source = new IniConfigSource(file);
  85. }
  86. catch
  87. {
  88. m_log.WarnFormat("[ESTATE LOADER FILE SYSTEM]: failed to parse file {0}", file);
  89. }
  90. if(source == null)
  91. continue;
  92. foreach (IConfig config in source.Configs)
  93. {
  94. // Read Estate Config From Source File
  95. string estateName = config.Name;
  96. if (string.IsNullOrWhiteSpace(estateName))
  97. continue;
  98. if (estateName.Length > 64) // need check this and if utf8 is valid
  99. {
  100. m_log.WarnFormat("[ESTATE LOADER FILE SYSTEM]: Estate name {0} is too large, ignoring", estateName);
  101. continue;
  102. }
  103. string ownerString = config.GetString("Owner", string.Empty);
  104. if (string.IsNullOrWhiteSpace(ownerString))
  105. continue;
  106. if (!UUID.TryParse(ownerString, out UUID estateOwner) || estateOwner == UUID.Zero)
  107. continue;
  108. // Check If Estate Exists (Skip If So)
  109. existingEstates = m_application.EstateDataService.GetEstates(estateName);
  110. if (existingEstates.Count > 0)
  111. continue;
  112. //### Should check Estate Owner ID but no Scene object available at this point
  113. // Does Config Specify EstateID (0 Defaults To AutoIncrement)
  114. int EstateID = config.GetInt("EstateID", 0);
  115. if (EstateID > 0)
  116. {
  117. if (EstateID < 100)
  118. {
  119. // EstateID Cannot be less than 100
  120. m_log.WarnFormat("[ESTATE LOADER FILE SYSTEM]: Estate name {0} specified estateID that is less that 100, ignoring", estateName);
  121. continue;
  122. }
  123. else if(existingEstateIDs.Contains(EstateID))
  124. {
  125. // Specified EstateID Exists
  126. m_log.WarnFormat("[ESTATE LOADER FILE SYSTEM]: Estate name {0} specified estateID that is already in use, ignoring", estateName);
  127. continue;
  128. }
  129. }
  130. // Create a new estate with the name provided
  131. EstateSettings estateSettings = m_application.EstateDataService.CreateNewEstate(EstateID);
  132. estateSettings.EstateName = estateName;
  133. estateSettings.EstateOwner = estateOwner;
  134. // Persistence does not seem to effect the need to save a new estate
  135. m_application.EstateDataService.StoreEstateSettings(estateSettings);
  136. m_log.InfoFormat("[ESTATE LOADER FILE SYSTEM]: Loaded config for estate {0}", estateName);
  137. i++;
  138. }
  139. }
  140. }
  141. }
  142. }