示例#1
0
    /*
     * public string EditInput(string input)
     * {
     *
     * }
     */

    public bool ObjectClick()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform != null)
                {
                    if (hit.transform.GetComponent <VirtualFunction>() != null)
                    {
                        if (vfunction != null)
                        {
                            vfunction.DettachedToEditor();
                        }

                        vfunction = hit.transform.GetComponent <VirtualFunction>();
                        vfunction.AttachedToEditor();
                        os = vfunction.vos;
                    }

                    OpenEditor();
                    return(true);
                }
            }
        }

        return(false);
    }
示例#2
0
    // Runs lines of code from a start to end index
    void RunLines(int lineIndex, int stopIndex)
    {
        if (lineIndex >= lines.Length || lineIndex == stopIndex)
        {
            return;
        }

        string line = lines[lineIndex];

        string[] sections = line.Split(' ');

        //Debug.Log ($"Line: {lineIndex}: " + line + " stop: " + stopIndex);

        // Test if line is an external function call
        if (outputFunctionNames != null)
        {
            for (int i = 0; i < outputFunctionNames.Count; i++)
            {
                //TODO: fix tolower cases etc.
                if (sections[0] == outputFunctionNames[i].ToLower()) //((sections[0].Equals(outputFunctionNames[i])))
                {
                    VirtualFunction outputFunction = new VirtualFunction()
                    {
                        name = outputFunctionNames[i]
                    };

                    string   argumentString   = Substring(line, line.IndexOf('(') + 1, line.LastIndexOf(')'));
                    string[] argumentSections = argumentString.Split(',');

                    //TODO: call these virtual functions in the execute loop
                    //TODO: provide the functions from modules in the virtualOS
                    for (int j = 0; j < argumentSections.Length; j++)
                    {
                        //true or false as parameter for a function
                        if (string.Equals(argumentSections[j].Trim(' '), "true"))
                        {
                            outputFunction.values.Add(true);
                            break;
                        }
                        if (string.Equals(argumentSections[j].Trim(' '), "false"))
                        {
                            outputFunction.values.Add(false);
                            break;
                        }

                        var   valueString = new ValueString(argumentSections[j], variables); //TODO: accept T value as func parameter
                        float value       = new NumericalExpression(valueString).Evaluate();
                        outputFunction.values.Add(value);
                    }

                    //outputFunction.FunctionWithParam = delegate (string s) { return s.ToUpper(); };

                    //outputFunction.delFunc = Delegate.CreateDelegate(typeof(VirtualCompiler), this.GetType().GetMethod(outputFunctionNames[i]));
                    //TODO: robot modules are classes that can be called => parse class attribute calls in user code

                    //Task tt = new Task(); //outputFunction.delFunc = /*class_instance_to_call_here.*/tt.GetType().GetMethod(outputFunctionNames[i]);

                    //foreach (object x in VirtualOS.Instance.availableModules) { x.GetType().GetMethod(outputFunctionNames[i]); }

                    /*Array.ForEach(VirtualOS.Instance.availableModules, x =>
                     * {
                     *  outputFunction.delFunc = x.GetType().GetMethod(outputFunctionNames[i], BindingFlags.Public); //TODO: use getmethods and compare
                     * });*/
                    Array.ForEach(vComputer.availableModules, x =>
                    {
                        if (x != null)
                        {
                            outputFunction.delFunc = x.GetType().GetMethod(outputFunctionNames[i]);
                        }
                        else
                        {
                            //Debug.Log("No available functions");
                        }
                    });

                    if (outputFunction.values.Count <= 1)
                    {
                        availableFuncsWthoutReturnV.TryGetValue(outputFunctionNames[i], out outputFunction.FunctionWithoutParam); //TODO: add function from virtualOS and invoke them from fixedturretgame
                    }
                    else
                    {
                        availableFuncsWthP.TryGetValue(outputFunctionNames[i], out outputFunction.FunctionWithParam); //TODO: either with or without parameters
                    }
                    //outputFunction.FunctionWithoutParam = PlayerCodeCallableFunction;

                    /*
                     * //bind a method from a string using reflection
                     * //MethodInfo mi = this.GetType().GetMethod(outputFunctionNames[i]);
                     * MethodInfo reflectionMethod = typeof(VirtualCompiler).GetMethod("testMethodInfo");
                     * object[] mParams = new object[outputFunction.values.Count];
                     * Array.Copy(outputFunction.values.ToArray(), mParams, outputFunction.values.Count);
                     * Debug.Log(mParams);
                     * reflectionMethod.Invoke(this, mParams);
                     */
                    //save dynamic method alloc to a del to later invoke it
                    //Delegate del = Delegate.CreateDelegate(typeof(VirtualCompiler), reflectionMethod);
                    //del.DynamicInvoke(mParams);



                    outputs.Add(outputFunction);

                    // Run the next line
                    RunLines(lineIndex + 1, stopIndex);
                    return;
                }
            }
        }

        // Test if line is a conditional statement
        if (conditionByLineIndex.ContainsKey(lineIndex))
        {
            var  condition    = conditionByLineIndex[lineIndex];
            bool runCondition = GetConditionResult(line);
            // Else statement has no condition to test, so set run to true by default
            if (condition.type == ConditionBlockInfo.Type.Else)
            {
                runCondition = true;
            }

            // elseif/else conditions only run if all previous conditions in the chain were false
            if (condition.type != ConditionBlockInfo.Type.If)
            {
                var previousInChain = condition.previousInChain;
                while (previousInChain != null)
                {
                    if (previousInChain.lastEvaluation == true)
                    {
                        runCondition = false;
                        break;
                    }
                    previousInChain = previousInChain.previousInChain;
                }
            }

            condition.lastEvaluation = runCondition;
            HandleCondition(runCondition, lineIndex, stopIndex);
            return;
        }

        if (sections[0] == "loop")
        {
            string argumentString = Substring(line, line.IndexOf('(') + 1, line.LastIndexOf(')'));
            var    e          = new NumericalExpression(new ValueString(argumentString, variables));
            int    numRepeats = (int)e.Evaluate();

            // Pass off control to the handle condition function. It will resume the running of lines.
            HandleLoop(numRepeats, lineIndex, stopIndex);
            return;
        }

        // Assignment
        if (sections.Length > 1)
        {
            if (sections[1] == "=")
            {
                ProcessAssignment(line);
            }
        }

        if (sections[0] == "print")
        {
            ProcessPrint(line);
        }

        // Run next line
        RunLines(lineIndex + 1, stopIndex);
    }
示例#3
0
    // Runs lines of code from a start to end index
    void RunLines(int lineIndex, int stopIndex)
    {
        if (lineIndex >= lines.Length || lineIndex == stopIndex)
        {
            return;
        }

        string line = lines[lineIndex];

        string[] sections = line.Split(' ');

        //Debug.Log ($"Line: {lineIndex}: " + line + " stop: " + stopIndex);

        // Test if line is an external function call
        if (outputFunctionNames != null)
        {
            for (int i = 0; i < outputFunctionNames.Count; i++)
            {
                if (sections[0] == outputFunctionNames[i].ToLower())
                {
                    string   argumentString   = Substring(line, line.IndexOf('(') + 1, line.LastIndexOf(')'));
                    string[] argumentSections = argumentString.Split(',');
                    var      outputFunction   = new VirtualFunction();
                    outputFunction.name = outputFunctionNames[i];

                    for (int j = 0; j < argumentSections.Length; j++)
                    {
                        var   valueString = new ValueString(argumentSections[j], variables);
                        float value       = new NumericalExpression(valueString).Evaluate();
                        outputFunction.values.Add(value);
                    }

                    outputs.Add(outputFunction);

                    // Run the next line
                    RunLines(lineIndex + 1, stopIndex);
                    return;
                }
            }
        }

        // Test if line is a conditional statement
        if (conditionByLineIndex.ContainsKey(lineIndex))
        {
            var  condition    = conditionByLineIndex[lineIndex];
            bool runCondition = GetConditionResult(line);
            // Else statement has no condition to test, so set run to true by default
            if (condition.type == ConditionBlockInfo.Type.Else)
            {
                runCondition = true;
            }

            // elseif/else conditions only run if all previous conditions in the chain were false
            if (condition.type != ConditionBlockInfo.Type.If)
            {
                var previousInChain = condition.previousInChain;
                while (previousInChain != null)
                {
                    if (previousInChain.lastEvaluation == true)
                    {
                        runCondition = false;
                        break;
                    }
                    previousInChain = previousInChain.previousInChain;
                }
            }

            condition.lastEvaluation = runCondition;
            HandleCondition(runCondition, lineIndex, stopIndex);
            return;
        }

        if (sections[0] == "loop")
        {
            string argumentString = Substring(line, line.IndexOf('(') + 1, line.LastIndexOf(')'));
            var    e          = new NumericalExpression(new ValueString(argumentString, variables));
            int    numRepeats = (int)e.Evaluate();

            // Pass off control to the handle condition function. It will resume the running of lines.
            HandleLoop(numRepeats, lineIndex, stopIndex);
            return;
        }

        // Assignment
        if (sections.Length > 1)
        {
            if (sections[1] == "=")
            {
                ProcessAssignment(line);
            }
        }

        if (sections[0] == "print")
        {
            ProcessPrint(line);
        }

        // Run next line
        RunLines(lineIndex + 1, stopIndex);
    }