Example #1
0
        public McFunction Parse(string id, string code = null)
        {
            if (code == null)
            {
                // Load file from path in build environment
                string path = Environment.GetPath(id, "mcfunction");
                code = File.ReadAllText(path);
            }

            McFunction = new McFunction(id);

            // Iterate over lines in file
            foreach (string line in code.Split('\n'))
            {
                // Break command up into arguments
                var parts = CommandParser.Parse(line);

                if (parts == null)
                {
                    continue;
                }

                // Parse command into arguments
                var cmd = new Command.Command();
                foreach (string part in parts)
                {
                    Argument argument = new Argument();
                    argument.Tokens.Add(new TextToken(part));

                    // Run argument through filters
                    foreach (IParseFilter filter in ParseFilters)
                    {
                        argument = filter.FilterArgument(argument);
                    }

                    if (argument != null)
                    {
                        cmd.Arguments.Add(argument);
                    }
                }

                // Run command through filters
                foreach (IParseFilter filter in ParseFilters)
                {
                    cmd = filter.Filter(cmd);
                }

                McFunction.Commands.Add(cmd);
            }

            return(McFunction);
        }
Example #2
0
        public ApplyResult Apply(BuildEnvironment env, Command command)
        {
            string id   = command.Arguments[1].GetAsText();
            string path = env.GetPath(id, "mcfunction");

            Logger.Debug($"Importing {id}...");

            var        parser     = new Parser.Parser(env);
            McFunction mcFunction = parser.Parse(id);

            mcFunction.Compile(env, false);

            return(new ApplyResult(true, mcFunction.Commands));
        }
Example #3
0
        public string Compile(BuildEnvironment env)
        {
            string path = env.GetPath(Id, "json");
            string file = File.ReadAllText(path);

            if (path.EndsWith(".nbt"))
            {
                // Treat as NBT JSON
                // Parse and recompile to put onto one line and ensure formatting is correct
                try
                {
                    object nbt         = NbtJsonParser.Parse(file);
                    string compiledNbt = nbt.ToString();

                    Logger.Debug($"Sucessfully parsed {Id} as NBT.");

                    return(compiledNbt);
                }
                catch (Exception e)
                {
                    Logger.Warning($"Resource {Id} isn't valid NBT! " + e.Message);
                }
            }
            else if (path.EndsWith(".json"))
            {
                // Treat as real JSON
                // Parse and recompile to put onto one line and ensure formatting is correct
                try
                {
                    object deserialized = JsonConvert.DeserializeObject(file);

                    string json = JsonConvert.SerializeObject(deserialized, Formatting.None);

                    Logger.Debug($"Sucessfully parsed {Id} as JSON.");

                    return(json);
                }
                catch (Exception e)
                {
                    Logger.Warning($"Resource {Id} isn't valid JSON! " + e.Message);
                }
            }

            return(file);
        }