EstateLoaderFileSystem.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. foreach (string file in iniFiles)
  78. {
  79. m_log.InfoFormat("[ESTATE LOADER FILE SYSTEM]: Loading config file {0}", file);
  80. IConfigSource source = null;
  81. try
  82. {
  83. source = new IniConfigSource(file);
  84. }
  85. catch
  86. {
  87. m_log.WarnFormat("[ESTATE LOADER FILE SYSTEM]: failed to parse file {0}", file);
  88. }
  89. if(source == null)
  90. continue;
  91. foreach (IConfig config in source.Configs)
  92. {
  93. // Read Estate Config From Source File
  94. string estateName = config.Name;
  95. if (string.IsNullOrWhiteSpace(estateName))
  96. continue;
  97. if (estateName.Length > 64) // need check this and if utf8 is valid
  98. {
  99. m_log.WarnFormat("[ESTATE LOADER FILE SYSTEM]: Estate name {0} is too large, ignoring", estateName);
  100. continue;
  101. }
  102. string ownerString = config.GetString("Owner", string.Empty);
  103. if (string.IsNullOrWhiteSpace(ownerString))
  104. continue;
  105. if (!UUID.TryParse(ownerString, out UUID estateOwner) || estateOwner == UUID.Zero)
  106. continue;
  107. // Check If Estate Exists (Skip If So)
  108. existingEstates = m_application.EstateDataService.GetEstates(estateName);
  109. if (existingEstates.Count > 0)
  110. continue;
  111. //### Should check Estate Owner ID but no Scene object available at this point
  112. // Does Config Specify EstateID (0 Defaults To AutoIncrement)
  113. int EstateID = config.GetInt("EstateID", 0);
  114. if (EstateID > 0)
  115. {
  116. if (EstateID < 100)
  117. {
  118. // EstateID Cannot be less than 100
  119. m_log.WarnFormat("[ESTATE LOADER FILE SYSTEM]: Estate name {0} specified estateID that is less that 100, ignoring", estateName);
  120. continue;
  121. }
  122. else if(existingEstateIDs.Contains(EstateID))
  123. {
  124. // Specified EstateID Exists
  125. m_log.WarnFormat("[ESTATE LOADER FILE SYSTEM]: Estate name {0} specified estateID that is already in use, ignoring", estateName);
  126. continue;
  127. }
  128. }
  129. // Create a new estate with the name provided
  130. EstateSettings estateSettings = m_application.EstateDataService.CreateNewEstate(EstateID);
  131. estateSettings.EstateName = estateName;
  132. estateSettings.EstateOwner = estateOwner;
  133. // Persistence does not seem to effect the need to save a new estate
  134. m_application.EstateDataService.StoreEstateSettings(estateSettings);
  135. m_log.InfoFormat("[ESTATE LOADER FILE SYSTEM]: Loaded config for estate {0}", estateName);
  136. }
  137. }
  138. }
  139. }
  140. }