public void HierarchyAndAdditionalProperties()
        {
            var baseCfgInfo = new ConfigurationFileInfo
            {
                Base     = null,
                Name     = "BaseCfg",
                Contents = @"
public abstract string Name {get;}
public string Greeting {get {return ""hello ${name}!"";}}
"
            };

            var myCfgInfo = new ConfigurationFileInfo
            {
                Base     = baseCfgInfo,
                Name     = "MyCfg",
                Contents = @"public override string Name {get{return ""World"";}}"
            };
            var cfg = _compiler.Compile(
                myCfgInfo, new Dictionary <string, string> {
                { "Name", "Boris" }
            });

            Assert.AreEqual("hello Boris!", cfg.GetParamValue("Greeting"));
        }
示例#2
0
 protected string GetConfigurationFilePath(ConfigurationFileInfo configFileInfo)
 {
     if (configFileInfo.IsResourceFile)
     {
         return("Configuration/" + Path.GetFileNameWithoutExtension(configFileInfo.FileName));
     }
     return(Application.persistentDataPath + "/" + configFileInfo.FileName);
 }
        public ConfigurationBase Compile(ConfigurationFileInfo cfgFileInfo, Dictionary<string, string> overidenParams)
        {
            var provider = GetCodeDomProvider();
            var cp = new CompilerParameters{GenerateInMemory = true};
            var configizerLibPath = typeof(IConfigurationCompiler).Assembly.Location;
            cp.ReferencedAssemblies.Add(configizerLibPath);

            var haveOveridenParams = overidenParams != null && overidenParams.Any();

            var classesCode = new List<string>();
            var actualConfig = cfgFileInfo;
            do
            {
                var baseClassName = actualConfig.Base != null ? actualConfig.Base.Name : null;
                var @abstract = haveOveridenParams || actualConfig.Name != cfgFileInfo.Name;
                var nsImports = _standardNamespaces.Union(cfgFileInfo.NamespaceImports).Distinct();
                classesCode.Add(CreateConfigClassCode(
                    actualConfig.Contents,
                    actualConfig.Name,
                    baseClassName,
                    @abstract, // every non-leaf class is abstract
                    !@abstract,
                    nsImports));
                actualConfig = actualConfig.Base;
            } while (actualConfig != null);

            string leafConfigClassName;

            // overrides configuration with new leaf class which contains string virtual properties
            // (typically sent as commandline arguments to configizer)
            if (haveOveridenParams)
            {
                leafConfigClassName = "__configizerOveridingClass";
                classesCode.Add(CreateClassWithOverridingParams(leafConfigClassName, cfgFileInfo.Name, _standardNamespaces, overidenParams));
            }
            else
                leafConfigClassName = cfgFileInfo.Name;

            var results = provider.CompileAssemblyFromSource(cp, classesCode.ToArray());
            if (results.Errors.HasErrors)
                throw new ConfigurationCompilationException(cfgFileInfo, results.Errors);

            var configTypes = results.CompiledAssembly.GetTypes()
                .Where(t =>
                    typeof (ConfigurationBase).IsAssignableFrom(t)
                );

            var cfg = (ConfigurationBase)Activator.CreateInstance(configTypes.Single(c=>c.Name == leafConfigClassName));
            cfg.ConfigDirectory = cfgFileInfo.Directory;

            return cfg;
        }
示例#4
0
        protected void SerializeToFile(string filePath, object obj, ConfigurationFileInfo info)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            StringWriter stringWriter = new StringWriter();
            JsonWriter   jsonWriter   = new JsonWriter(stringWriter);

            jsonWriter.Validate    = false;
            jsonWriter.PrettyPrint = true;
            JsonMapper.ToJson(obj, jsonWriter);
            string text     = stringWriter.ToString();
            string contents = (info.IsEncrypted ? AesCipher.Encrypt(text, info.EncryptionKey) : text);

            File.WriteAllText(filePath, contents);
        }
示例#5
0
        protected void CreateConfigurationFile(ConfigurationFileInfo info)
        {
            string fileName = info.FileName;
            List <ConfigurableSystem> list = DiscoverAllConfigurableSystems();

            foreach (ConfigurableSystem item in list)
            {
                InsertNewSystemIntoDictionary(item.SystemName, item.Dictionary);
            }
            if (!Directory.Exists(GetConfigurationPath()))
            {
            }
            SerializeToFile(GetConfigurationPath() + fileName, mDictionary, info);
        }
        public void HierarchyAndAdditionalProperties()
        {
            var baseCfgInfo = new ConfigurationFileInfo
            {
                Base = null,
                Name = "BaseCfg",
                Contents = @"
            public abstract string Name {get;}
            public string Greeting {get {return ""hello ${name}!"";}}
            "
            };

            var myCfgInfo = new ConfigurationFileInfo
            {
                Base = baseCfgInfo,
                Name = "MyCfg",
                Contents = @"public override string Name {get{return ""World"";}}"
            };
            var cfg = _compiler.Compile(
                myCfgInfo, new Dictionary<string, string>{{"Name", "Boris"}});
            Assert.AreEqual("hello Boris!", cfg.GetParamValue("Greeting"));
        }
示例#7
0
        protected IDictionary <string, object> LoadFromFile(string filePath, ConfigurationFileInfo configFileInfo)
        {
            IDictionary <string, object> result = null;
            string text = null;

            if (configFileInfo.IsResourceFile)
            {
                TextAsset textAsset = Resources.Load(filePath, typeof(TextAsset)) as TextAsset;
                if (textAsset != null)
                {
                    text = textAsset.text;
                }
            }
            if (text == null)
            {
                text = File.ReadAllText(filePath);
            }
            string text2 = null;

            if (configFileInfo.IsEncrypted)
            {
                try
                {
                    text2 = AesCipher.Decrypt(text, configFileInfo.EncryptionKey);
                }
                catch (Exception)
                {
                }
            }
            if (text2 == null)
            {
                text2 = text;
            }
            if (text2 != null)
            {
                result = LPFJsonMapper.ToObjectSimple(text2) as IDictionary <string, object>;
            }
            return(result);
        }
示例#8
0
        protected IDictionary <string, object> LoadFromFile(string filePath, ConfigurationFileInfo configFileInfo)
        {
            IDictionary <string, object> result = null;
            string text = null;

            if (configFileInfo.IsResourceFile)
            {
                TextAsset textAsset = Resources.Load(filePath, typeof(TextAsset)) as TextAsset;
                if (textAsset != null)
                {
                    text = textAsset.text;
                }
            }
            else
            {
                text = File.ReadAllText(filePath);
            }
            if (text != null)
            {
                result = JsonMapper.ToObjectSimple(text) as IDictionary <string, object>;
            }
            return(result);
        }
示例#9
0
        public ConfigurationBase Compile(ConfigurationFileInfo cfgFileInfo, Dictionary <string, string> overidenParams)
        {
            var provider = GetCodeDomProvider();
            var cp       = new CompilerParameters {
                GenerateInMemory = true
            };
            var configizerLibPath = typeof(IConfigurationCompiler).Assembly.Location;

            cp.ReferencedAssemblies.Add(configizerLibPath);

            var haveOveridenParams = overidenParams != null && overidenParams.Any();

            var classesCode  = new List <string>();
            var actualConfig = cfgFileInfo;

            do
            {
                var baseClassName = actualConfig.Base != null ? actualConfig.Base.Name : null;
                var @abstract     = haveOveridenParams || actualConfig.Name != cfgFileInfo.Name;
                var nsImports     = _standardNamespaces.Union(cfgFileInfo.NamespaceImports).Distinct();
                classesCode.Add(CreateConfigClassCode(
                                    actualConfig.Contents,
                                    actualConfig.Name,
                                    baseClassName,
                                    @abstract, // every non-leaf class is abstract
                                    !@abstract,
                                    nsImports));
                actualConfig = actualConfig.Base;
            } while (actualConfig != null);

            string leafConfigClassName;

            // overrides configuration with new leaf class which contains string virtual properties
            // (typically sent as commandline arguments to configizer)
            if (haveOveridenParams)
            {
                leafConfigClassName = "__configizerOveridingClass";
                classesCode.Add(CreateClassWithOverridingParams(leafConfigClassName, cfgFileInfo.Name, _standardNamespaces, overidenParams));
            }
            else
            {
                leafConfigClassName = cfgFileInfo.Name;
            }

            var results = provider.CompileAssemblyFromSource(cp, classesCode.ToArray());

            if (results.Errors.HasErrors)
            {
                throw new ConfigurationCompilationException(cfgFileInfo, results.Errors);
            }

            var configTypes = results.CompiledAssembly.GetTypes()
                              .Where(t =>
                                     typeof(ConfigurationBase).IsAssignableFrom(t)
                                     );

            var cfg = (ConfigurationBase)Activator.CreateInstance(configTypes.Single(c => c.Name == leafConfigClassName));

            cfg.ConfigDirectory = cfgFileInfo.Directory;

            return(cfg);
        }
 public ConfigurationCompilationException(ConfigurationFileInfo configuration, CompilerErrorCollection compilationErrors)
     : this(configuration, compilationErrors.Cast<CompilerError>())
 {
 }
 public ConfigurationCompilationException(ConfigurationFileInfo configuration, IEnumerable<CompilerError> compilationErrors)
 {
     _configuration = configuration;
     _compilationErrors = compilationErrors;
 }
 public ConfigurationCompilationException(ConfigurationFileInfo configuration, IEnumerable <CompilerError> compilationErrors)
 {
     _configuration     = configuration;
     _compilationErrors = compilationErrors;
 }
 public ConfigurationCompilationException(ConfigurationFileInfo configuration, CompilerErrorCollection compilationErrors)
     : this(configuration, compilationErrors.Cast <CompilerError>())
 {
 }