internal static void EncodeLevel(PolicyLevel level)
{
if (level.Path == null)
{
throw new PolicyException(Environment.GetResourceString("Policy_UnableToSave", (object)level.Label, (object)Environment.GetResourceString("Policy_SaveNotFileBased")));
}
SecurityElement securityElement1 = new SecurityElement("configuration");
SecurityElement child1 = new SecurityElement("mscorlib");
SecurityElement child2 = new SecurityElement("security");
SecurityElement child3 = new SecurityElement("policy");
securityElement1.AddChild(child1);
child1.AddChild(child2);
child2.AddChild(child3);
child3.AddChild(level.ToXml());
try
{
StringBuilder stringBuilder = new StringBuilder();
Encoding utF8 = Encoding.UTF8;
SecurityElement securityElement2 = new SecurityElement("xml");
securityElement2.m_type = SecurityElementType.Format;
securityElement2.AddAttribute("version", "1.0");
securityElement2.AddAttribute("encoding", utF8.WebName);
stringBuilder.Append(securityElement2.ToString());
stringBuilder.Append(securityElement1.ToString());
byte[] bytes = utF8.GetBytes(stringBuilder.ToString());
string path = level.Path;
byte[] data = bytes;
int length = data.Length;
Exception exceptionForHr = Marshal.GetExceptionForHR(Config.SaveDataByte(path, data, length));
if (exceptionForHr != null)
{
string str = exceptionForHr != null ? exceptionForHr.Message : string.Empty;
throw new PolicyException(Environment.GetResourceString("Policy_UnableToSave", (object)level.Label, (object)str), exceptionForHr);
}
}
catch (Exception ex)
{
if (ex is PolicyException)
{
throw ex;
}
throw new PolicyException(Environment.GetResourceString("Policy_UnableToSave", (object)level.Label, (object)ex.Message), ex);
}
Config.ResetCacheData(level.ConfigId);
if (!PolicyManager.CanUseQuickCache(level.RootCodeGroup))
{
return;
}
Config.SetQuickCache(level.ConfigId, PolicyManager.GenerateQuickCache(level));
}