Пример #1
0
    private void ParseInstructions(ScriptboundObject target, string fullString)
    {
        fullString = fullString.Replace("" + ((char)13), "");

        var methods = target.ExtractMethodReflections();
        var clone   = target.Duplicate();

        clone.scriptBoolValues.Clear();
        clone.scriptFloatValues.Clear();
        clone.scriptIntValues.Clear();
        clone.scriptObjectValues.Clear();
        clone.scriptStringValues.Clear();

        clone.scriptInstructions.Clear();

        var instructions = Regex.Split(fullString, @"\n[ \t\n]*\n");

        foreach (var instruction in instructions)
        {
            ParseInstruction(instruction, clone, methods);
        }

        target.scriptBoolValues   = clone.scriptBoolValues;
        target.scriptFloatValues  = clone.scriptFloatValues;
        target.scriptIntValues    = clone.scriptIntValues;
        target.scriptObjectValues = clone.scriptObjectValues;
        target.scriptStringValues = clone.scriptStringValues;

        target.scriptInstructions = clone.scriptInstructions;
    }
Пример #2
0
    private string ScriptboundObjectToString(ScriptboundObject target, bool highlight = false)
    {
        if (target.scriptInstructions == null)
        {
            target.scriptInstructions = new List <ScriptboundObject.Instruction>();
        }
        var results                = "";
        var instructions           = target.scriptInstructions;
        var instructionParsedCount = 0;

        foreach (var instruction in instructions)
        {
            if (instructionParsedCount > 0)
            {
                results += "\n\n";
            }
            instructionParsedCount++;

            var indent_replacement = "\n";
            for (var i = 0; i < instruction.indent; i++)
            {
                results            += '\t';
                indent_replacement += '\t';
            }

            //singular control

            if (instruction.controlElse)
            {
                results += string.Format(highlight ? "<b>{0}: </b>" : "{0}: ", "else");
                continue;
            }


            //prefix controls
            var control = "";
            if (instruction.controlIf)
            {
                control += instruction.negative? "ifnot " : "if ";
            }
            control += instruction.instructionName;

            if (methodReflections.ContainsKey(instruction.instructionName) == false)
            {
                continue;
            }
            var method = methodReflections[instruction.instructionName];
            if (method == null)
            {
                continue;
            }

            if (method.Name == this.defaultStringMethod)
            {
            }
            else
            {
                if (method.GetParameters().Length <= 0)
                {
                    results += string.Format(highlight ? "<b>{0}</b>" : "{0}", control);
                }
                else
                {
                    results += string.Format(highlight ? "<b>{0}: </b>" : "{0}: ", control);
                }
            }

            if (method.GetParameters().Length > 0)
            {
                var parameters = Target.ExtractParameters(method, instruction);

                string str_params = "";

                if (instruction.instructionName == this.defaultStringMethod)
                {
                    str_params = ParseDefaultStringInstruction(instruction, parameters, highlight);
                }
                else if (parameters.Length == 1 && instruction.parameters[0].type == ScriptboundObject.Instruction.ParamType.STRING && parameters[0] != null)
                {
                    str_params += ParseDefaultStringInstruction(instruction, parameters, highlight);
                    //str_params += parameters[0].ToString(); //TODO: CHANGE THIS TO SUPPORT INJECTIBLE AS WELL
                }
                else
                {
                    for (var i = 0; i < instruction.parameters.Count; i++)
                    {
                        var param = parameters[i];
                        if (i > 0)
                        {
                            str_params += ", ";
                        }
                        if (instruction.parameters[i].type == ScriptboundObject.Instruction.ParamType.OBJECT)
                        {
                            str_params += ObjectToString((UnityEngine.Object)param);
                        }
                        else if (param != null)
                        {
                            var param_str = param.ToString().Replace("\\,", ",");
                            param_str   = param_str.Replace(",", "\\,");
                            str_params += param_str;
                        }
                    }
                }
                str_params = str_params.Replace("\n", indent_replacement);
                results   += str_params;
            }
        }

        return(results);
    }
Пример #3
0
    private void ParseInstruction(string instruction_str, ScriptboundObject clone, Dictionary <string, MethodInfo> methods)
    {
        if (instruction_str.Length <= 0)
        {
            return;                              //if line is empty then no need to do anything
        }
        instruction_str = instruction_str.Trim(' ');
        instruction_str = instruction_str.Trim((char)13);

        //calculate tabCount
        var tabCount = 0;

        for (var i = 0; i < instruction_str.Length; i++)
        {
            if (instruction_str[i] != '\t')
            {
                tabCount = i; break;
            }
        }

        //trim instruction from tabs
        instruction_str = instruction_str.Trim('\t');

        //break the instruction into two parts
        var(controls, contents) = BreakInstruction(instruction_str);

        ScriptboundObject.Instruction instructionObj = new ScriptboundObject.Instruction();
        instructionObj.indent = tabCount;


        if (string.IsNullOrEmpty(contents) && SINGULAR_CONTROLS.Contains(controls))
        {
            if (controls == "else")
            {
                instructionObj.controlElse = true;
            }
        }
        else
        {
            try
            {
                var method = BreakInstructionControl(controls, instructionObj, clone, methods);
                if (contents != null)
                {
                    contents = TrimContentIndent(contents, tabCount);
                }
                BreakInstructionContents(contents, instructionObj, clone, method);
            }
            catch (ParsingException e)
            {
                if (defaultStringMethod != null)
                {
                    var attr_injectible = methodReflections[defaultStringMethod].GetCustomAttribute <ScriptboundObject.StringInjectible>();
                    instructionObj.instructionName = defaultStringMethod;
                    instructionObj.parameters      = new List <ScriptboundObject.Instruction.Parameter>();
                    instructionObj.injectibles     = new List <ScriptboundObject.Instruction.Injectible>();
                    ExstractSingleStringInstruction(TrimContentIndent(instruction_str, tabCount), instructionObj, clone, attr_injectible);
                }
                else
                {
                    throw e;
                }
            }
        }
        clone.scriptInstructions.Add(instructionObj);
    }