Example #1
0
        private void Init(string blockFolder, World world)
        {
            // Add the static air block type
            AddBlockType(BlockConfig.CreateAirBlockConfig(world));

            // Add all the block definitions defined in the config files
            ProcessConfigs(world, blockFolder);

            // Build block type lookup table
            BlockTypes = new Block[m_configs.Count];
            for (int i = 0; i < m_configs.Count; i++)
            {
                BlockConfig config = m_configs[i];

                Block block = (Block)Activator.CreateInstance(config.blockClass);
                block.type    = (ushort)i;
                block.config  = config;
                BlockTypes[i] = block;
            }

            // Once all blocks are set up, call OnInit on them. It is necessary to do it in a separate loop
            // in order to ensure there will be no dependency issues.
            for (int i = 0; i < BlockTypes.Length; i++)
            {
                Block block = BlockTypes[i];
                block.OnInit(this);
            }
        }
Example #2
0
        // World is only needed for setting up the textures
        private void ProcessConfigs(World world, string blockFolder)
        {
            var configFiles = Resources.LoadAll <TextAsset>(blockFolder);
            List <BlockConfig>          configs = new List <BlockConfig>(configFiles.Length);
            Dictionary <ushort, ushort> types   = new Dictionary <ushort, ushort>();

            // Add reserved block types
            AddBlockType(configs, types, BlockConfig.CreateAirBlockConfig(world));
            for (ushort i = 1; i <= LastReservedSimpleType; i++)
            {
                AddBlockType(configs, types, BlockConfig.CreateColorBlockConfig(world, i));
            }

            // Add block types from config
            foreach (var configFile in configFiles)
            {
                Hashtable configHash = JsonConvert.DeserializeObject <Hashtable>(configFile.text);

                Type configType = Type.GetType(configHash["configClass"] + ", " + typeof(BlockConfig).Assembly, false);
                if (configType == null)
                {
                    Debug.LogError("Could not create config for " + configHash["configClass"]);
                    continue;
                }

                BlockConfig config = (BlockConfig)Activator.CreateInstance(configType);
                if (!config.OnSetUp(configHash, world))
                {
                    continue;
                }

                if (!VerifyBlockConfig(types, config))
                {
                    continue;
                }

                AddBlockType(configs, types, config);
            }

            m_configs = configs.ToArray();

            // Now iterate over configs and find the one with the highest TypeInConfig
            ushort maxTypeInConfig = LastReservedType;

            for (int i = 0; i < m_configs.Length; i++)
            {
                if (m_configs[i].typeInConfig > maxTypeInConfig)
                {
                    maxTypeInConfig = m_configs[i].typeInConfig;
                }
            }

            // Allocate maxTypeInConfigs big array now and map config types to runtime types
            m_types = new ushort[maxTypeInConfig + FirstCustomType];
            for (ushort i = 0; i < m_configs.Length; i++)
            {
                m_types[m_configs[i].typeInConfig] = i;
            }
        }