Exemplo n.º 1
0
def script(code, file_name):
    """

    Args:
        file_name:  
        code: 

    Returns:

    """
    AddReference("System")

    from System import IO
    from System import Text
    from System import CodeDom as dom
    from System.CodeDom import Compiler

    nameSpace = dom.CodeNamespace("Client")
    comment = dom.CodeCommentStatement(dom.CodeComment(_template))
    nameSpace.Comments.Add(comment)

    nameSpace.Imports.Add(dom.CodeNamespaceImport("System"))
    nameSpace.Imports.Add(dom.CodeNamespaceImport("UnityEngine"))
    nameSpace.Imports.Add(dom.CodeNamespaceImport("UnityEditor"))

    mainClass = dom.CodeTypeDeclaration("Runtime")
    # MonoBehaivor, ScriptableObject, AssetPostprocessror, ShaderGui
    # SerializeField
    # Node
    # AssetDatabase.Reflesh()
    mainClass.BaseTypes.Add(
        dom.CodeTypeReference(clr.GetClrType(UnityEngine.MonoBehaviour)))
    nameSpace.Types.Add(mainClass)

    # fields = code.GetFields(
    #     ref.BindingFlags.GetField | ref.BindingFlags.Public | ref.BindingFlags.Instance
    # )
    #
    # for field in fields:
    #     variable = dom.CodeMemberField(field.FieldType.Name, field.Name)
    #     mainClass.Members.Add(variable)
    #
    # methods = code.GetMethods(
    #     ref.BindingFlags.Public | ref.BindingFlags.NonPublic |
    #     ref.BindingFlags.Instance | ref.BindingFlags.Static | ref.BindingFlags.DeclaredOnly
    # )
    #
    # for method in methods:
    #     mainMethod = dom.CodeMemberMethod()
    #     mainMethod.ReturnType = dom.CodeTypeReference(method.ReturnType)
    #     mainMethod.Attributes = dom.MemberAttributes.Public | dom.MemberAttributes.Final
    #     mainMethod.Name = method.Name
    #
    #     mainMethod.Comments.Add(dom.CodeCommentStatement("doc comment"))
    #
    #     if (method.Attributes & dom.MethodAttributes.Static) != 0:
    #         target = dom.CodeSnippetExpression(code.FullName)
    #     else:
    #         target = dom.CodeObjectCreateExpression(code.FullName)
    #
    #     invoke = dom.CodeMethodInvokeExpression(target, method.Name)
    #
    #     for p in method.GetParameters():
    #         invoke.Parameters.Add(dom.CodeArgumentReferenceExpression(p.Name))
    #         exp = dom.CodeParameterDeclarationExpression(p.ParameterType, p.Name)
    #         mainMethod.Parameters.Add(exp)
    #
    #     if method.ReturnType.Name != "Void":
    #         mainMethod.Statements.Add(dom.CodeMethodReturnStatement(invoke))
    #     else:
    #         mainMethod.Statements.Add(invoke)
    #
    #     mainClass.Members.Add(mainMethod)

    codeText = Text.StringBuilder()
    compilerOptions = Compiler.CodeGeneratorOptions()
    compilerOptions.IndentString = "    "
    compilerOptions.BracingStyle = "C#"

    # Csharp に書き込み
    codeWriter = IO.StringWriter(codeText)
    Compiler.CodeDomProvider.CreateProvider("C#").GenerateCodeFromNamespace(
        nameSpace, codeWriter, compilerOptions)
    codeWriter.Close()

    writer = IO.StreamWriter(file_name)
    writer.Write(codeText)
    writer.Close()
Exemplo n.º 2
0
    def __init__(self,
                 command,
                 arguments=None,
                 env=None,
                 stdout=None,
                 stderr=None,
                 redirect_output=True,
                 working_dir=None):
        self._cmdline = _get_cmd(command, arguments)
        self._process = CSharpProcess()
        self._started = False
        start_info = self._process.StartInfo
        start_info.FileName = self._cmdline[0]
        start_info.Arguments = self._cmdline[1:]
        self._env = env
        start_info.CreateNoWindow = True
        if working_dir is not None:
            start_info.WorkingDirectory = working_dir

        self._stdout = os.path.abspath(stdout) if stdout else None
        self._stderr = os.path.abspath(stderr) if stderr else None
        self._redirect_output = (stdout or stderr or redirect_output)
        if self._redirect_output:
            start_info.UseShellExecute = False
            start_info.RedirectStandardInput = True
            start_info.RedirectStandardOutput = True
            start_info.RedirectStandardError = True
            self._process.OutputDataReceived += self._stdout_handler
            self._process.ErrorDataReceived += self._stderr_handler
            self._stdout_lock = threading.Lock()
            if self._stdout:
                if os.path.isfile(self._stdout):
                    shell.remove(self._stdout)
                self._stdout_ostream = IO.FileStream(self._stdout,
                                                     IO.FileMode.Append,
                                                     IO.FileAccess.Write,
                                                     IO.FileShare.Read)
                self._stdout_istream = IO.FileStream(self._stdout,
                                                     IO.FileMode.Open,
                                                     IO.FileAccess.Read,
                                                     IO.FileShare.ReadWrite)
                self._stdout_writer = IO.StreamWriter(self._stdout_ostream)
                self._stdout_reader = IO.StreamReader(self._stdout_istream)
            else:
                self._stdout_buffer = b''
            self._stderr_lock = threading.Lock()
            if self._stderr:
                if os.path.isfile(self._stderr):
                    shell.remove(self._stderr)
                self._stderr_ostream = IO.FileStream(self._stderr,
                                                     IO.FileMode.Append,
                                                     IO.FileAccess.Write,
                                                     IO.FileShare.Read)
                self._stderr_istream = IO.FileStream(self._stderr,
                                                     IO.FileMode.Open,
                                                     IO.FileAccess.Read,
                                                     IO.FileShare.ReadWrite)
                self._stderr_writer = IO.StreamWriter(self._stderr_ostream)
                self._stderr_reader = IO.StreamReader(self._stderr_istream)
            else:
                self._stderr_buffer = b''
        else:
            start_info.UseShellExecute = True
            start_info.RedirectStandardInput = False
            start_info.RedirectStandardOutput = False
            start_info.RedirectStandardError = False

        self._pid = None
        self._immutable = None