Просмотр исходного кода

Interpreter cpp层打印日志到Unity层成功,方便调试cpp层

tanghai 4 лет назад
Родитель
Сommit
d0cde9f64c

+ 1 - 1
Libs/Interpreter/Interpreter.sln

@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio Version 16
 VisualStudioVersion = 16.0.31702.278
 MinimumVisualStudioVersion = 10.0.40219.1
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CSharpInterpreter", "CSharpInterpreter.vcxproj", "{B8BD0219-AEA8-4167-ABD7-29D7628803D9}"
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Interpreter", "Interpreter.vcxproj", "{B8BD0219-AEA8-4167-ABD7-29D7628803D9}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution

+ 2 - 1
Libs/Interpreter/CSharpInterpreter.vcxproj → Libs/Interpreter/Interpreter.vcxproj

@@ -88,6 +88,7 @@
     <LinkIncremental>true</LinkIncremental>
     <IncludePath>.\include;$(IncludePath)</IncludePath>
     <LibraryPath>.\libs;$(LibraryPath)</LibraryPath>
+    <OutDir>$(SolutionDir)..\..\Unity\Assets\Plugins\x86_64\</OutDir>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
     <LinkIncremental>false</LinkIncremental>
@@ -132,7 +133,7 @@
     <ClCompile>
       <WarningLevel>Level3</WarningLevel>
       <SDLCheck>true</SDLCheck>
-      <PreprocessorDefinitions>_DEBUG;CSHARPINTERPRETER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>_DEBUG;DLL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <ConformanceMode>true</ConformanceMode>
       <PrecompiledHeader>NotUsing</PrecompiledHeader>
       <PrecompiledHeaderFile>

+ 0 - 0
Libs/Interpreter/CSharpInterpreter.vcxproj.filters → Libs/Interpreter/Interpreter.vcxproj.filters


+ 0 - 0
Libs/Interpreter/CSharpInterpreter.vcxproj.user → Libs/Interpreter/Interpreter.vcxproj.user


BIN
Libs/Interpreter/libs/MonoPosixHelper.dll


BIN
Libs/Interpreter/libs/mono-2.0-sgen.dll


+ 33 - 2
Libs/Interpreter/main.cpp

@@ -1,8 +1,39 @@
 #include "main.h"
 #include <stdio.h>
+#include <stdarg.h>
 #include <mono\jit\jit.h>
+#include <mono/metadata/environment.h>
+#include <mono/utils/mono-publib.h>
+#include <mono/utils/mono-logger.h>
+#include <mono/metadata/assembly.h>
+#include <mono/metadata/mono-debug.h>
+#include <mono/metadata/exception.h>
 
-void InterpreterInit(char* bundleDir, const char* dllName)
+void(*log)(const char* buf, int len);
+
+void interpreter_log(const char* fmt, ...)
+{
+    if (log == 0)
+    {
+        return;
+    }
+
+    char buffer[1024];
+    va_list argptr;
+    va_start(argptr, fmt);
+    int n = vsprintf_s(buffer, fmt, argptr);
+    va_end(argptr);
+    log(buffer, n);
+}
+
+void interpreter_set_log(void(*plog)(const char* buf, int len))
+{
+    log = plog;
+}
+
+void interpreter_init(const char* bundleDir, const char* dllName)
 {
-    MonoDomain* domain = mono_jit_init(dllName);
+    mono_set_dirs(bundleDir, bundleDir);
+    interpreter_log("1111111111111111111111 %s  %s", bundleDir, dllName);
+    //MonoDomain* domain = mono_jit_init(dllName);
 }

+ 13 - 6
Libs/Interpreter/main.h

@@ -1,10 +1,17 @@
-#pragma once
-
 // Macro to put before functions that need to be exposed to C#
-#ifdef _WIN32
-#define DLLEXPORT __declspec(dllexport)
+#ifdef DLL_EXPORTS
+#define INTERPRETER_DLL _declspec(dllexport)
 #else
-#define DLLEXPORT 
+#define INTERPRETER_DLL
+#endif
+
+#ifdef __cplusplus
+extern "C" {
 #endif
+    INTERPRETER_DLL void interpreter_set_log(void(*writelog)(const char* buf, int len));
+
+    INTERPRETER_DLL void interpreter_init(const char* bundleDir, const char* dllName);
 
-DLLEXPORT void InterpreterInit(char* bundleDir, const char* dllName);
+#ifdef __cplusplus
+}
+#endif

+ 22 - 2
Unity/Assets/Mono/Interpreter/Interpreter.cs

@@ -1,4 +1,5 @@
-using System.Runtime.InteropServices;
+using System;
+using System.Runtime.InteropServices;
 
 namespace ET
 {
@@ -6,7 +7,26 @@ namespace ET
     {
         private const string InterpreterDll = "Interpreter";
         
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        public delegate void Log(IntPtr buf, int len);
+        
+        private static Log log;
+        
+        [DllImport(InterpreterDll, CallingConvention = CallingConvention.Cdecl)]
+        private static extern void interpreter_set_log(Log log);
+        
         [DllImport(InterpreterDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void InterpreterInit([In][MarshalAs(UnmanagedType.LPStr)] string reloadDir, [In][MarshalAs(UnmanagedType.LPStr)] string exeName);
+        private static extern void interpreter_init([In][MarshalAs(UnmanagedType.LPStr)] string reloadDir, [In][MarshalAs(UnmanagedType.LPStr)] string exeName);
+
+        public static void InterpreterSetLog(Log plog)
+        {
+            log = plog;
+            interpreter_set_log(log);
+        }
+
+        public static void InterpreterInit([In][MarshalAs(UnmanagedType.LPStr)]string reloadDir, [In][MarshalAs(UnmanagedType.LPStr)]string exeName)
+        {
+            interpreter_init(reloadDir, exeName);
+        }
     }
 }

+ 11 - 0
Unity/Assets/Mono/MonoBehaviour/Init.cs

@@ -1,6 +1,7 @@
 using System;
 using System.IO;
 using System.Reflection;
+using System.Runtime.InteropServices;
 using System.Threading;
 using UnityEngine;
 
@@ -43,10 +44,20 @@ namespace ET
 			}
 			else
 			{
+				byte[] log = new byte[1024];
+				Interpreter.InterpreterSetLog((buff, n) =>
+				{
+					Marshal.Copy(buff, log, 0, n);
+					UnityEngine.Debug.Log(log.Utf8ToStr(0, n));
+				});
+				Interpreter.InterpreterInit("./Temp/Bin/Debug/", "Unity.Script.dll");
+				
+				/*
 				UnityEngine.Debug.Log("unity script mode!");
 				byte[] dllBytes = File.ReadAllBytes("./Temp/Bin/Debug/Unity.Script.dll");
 				byte[] pdbBytes = File.ReadAllBytes("./Temp/Bin/Debug/Unity.Script.pdb");
 				modelAssembly = Assembly.Load(dllBytes, pdbBytes);
+				*/
 			}
 
 			Type initType = modelAssembly.GetType("ET.Entry");

+ 47 - 22
Unity/Assets/Plugins/ICSharpCode.SharpZipLib.dll.meta

@@ -1,55 +1,80 @@
 fileFormatVersion: 2
 guid: de67d260a82e87742b9c3c297379ffb3
-timeCreated: 1474948268
-licenseType: Pro
 PluginImporter:
-  serializedVersion: 1
+  externalObjects: {}
+  serializedVersion: 2
   iconMap: {}
   executionOrder: {}
+  defineConstraints: []
   isPreloaded: 0
+  isOverridable: 0
+  isExplicitlyReferenced: 0
+  validateReferences: 1
   platformData:
-    Any:
-      enabled: 1
-      settings: {}
-    Editor:
+  - first:
+      : Linux
+    second:
       enabled: 0
       settings:
-        CPU: AnyCPU
-        DefaultValueInitialized: true
-        OS: AnyOS
-    Linux:
+        CPU: x86
+  - first:
+      : LinuxUniversal
+    second:
       enabled: 0
       settings:
-        CPU: x86
-    Linux64:
+        CPU: None
+  - first:
+      : OSXIntel
+    second:
       enabled: 0
       settings:
-        CPU: x86_64
-    LinuxUniversal:
+        CPU: AnyCPU
+  - first:
+      : OSXIntel64
+    second:
       enabled: 0
       settings:
-        CPU: None
-    OSXIntel:
+        CPU: AnyCPU
+  - first:
+      Any: 
+    second:
+      enabled: 1
+      settings: {}
+  - first:
+      Editor: Editor
+    second:
       enabled: 0
       settings:
         CPU: AnyCPU
-    OSXIntel64:
+        DefaultValueInitialized: true
+        OS: AnyOS
+  - first:
+      Standalone: Linux64
+    second:
       enabled: 0
       settings:
         CPU: AnyCPU
-    OSXUniversal:
+  - first:
+      Standalone: OSXUniversal
+    second:
       enabled: 0
       settings:
         CPU: None
-    Win:
+  - first:
+      Standalone: Win
+    second:
       enabled: 1
       settings:
         CPU: AnyCPU
-    Win64:
+  - first:
+      Standalone: Win64
+    second:
       enabled: 1
       settings:
         CPU: AnyCPU
-    WindowsStoreApps:
+  - first:
+      Windows Store Apps: WindowsStoreApps
+    second:
       enabled: 0
       settings:
         CPU: AnyCPU

BIN
Unity/Assets/Plugins/x86_64/MonoPosixHelper.dll


+ 52 - 0
Unity/Assets/Plugins/x86_64/MonoPosixHelper.dll.meta

@@ -0,0 +1,52 @@
+fileFormatVersion: 2
+guid: 2c3d2309255364544bf7ce86b812c776
+PluginImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  iconMap: {}
+  executionOrder: {}
+  defineConstraints: []
+  isPreloaded: 0
+  isOverridable: 0
+  isExplicitlyReferenced: 0
+  validateReferences: 1
+  platformData:
+  - first:
+      Any: 
+    second:
+      enabled: 1
+      settings: {}
+  - first:
+      Editor: Editor
+    second:
+      enabled: 0
+      settings:
+        CPU: x86_64
+        DefaultValueInitialized: true
+  - first:
+      Standalone: Linux64
+    second:
+      enabled: 1
+      settings:
+        CPU: x86_64
+  - first:
+      Standalone: OSXUniversal
+    second:
+      enabled: 0
+      settings:
+        CPU: x86_64
+  - first:
+      Standalone: Win
+    second:
+      enabled: 0
+      settings:
+        CPU: None
+  - first:
+      Standalone: Win64
+    second:
+      enabled: 1
+      settings:
+        CPU: AnyCPU
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

BIN
Unity/Assets/Plugins/x86_64/mono-2.0-sgen.dll


+ 52 - 0
Unity/Assets/Plugins/x86_64/mono-2.0-sgen.dll.meta

@@ -0,0 +1,52 @@
+fileFormatVersion: 2
+guid: 9f9ec2c1658d44f4eb3fe4c4c53d09a5
+PluginImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  iconMap: {}
+  executionOrder: {}
+  defineConstraints: []
+  isPreloaded: 0
+  isOverridable: 0
+  isExplicitlyReferenced: 0
+  validateReferences: 1
+  platformData:
+  - first:
+      Any: 
+    second:
+      enabled: 1
+      settings: {}
+  - first:
+      Editor: Editor
+    second:
+      enabled: 0
+      settings:
+        CPU: x86_64
+        DefaultValueInitialized: true
+  - first:
+      Standalone: Linux64
+    second:
+      enabled: 1
+      settings:
+        CPU: x86_64
+  - first:
+      Standalone: OSXUniversal
+    second:
+      enabled: 0
+      settings:
+        CPU: x86_64
+  - first:
+      Standalone: Win
+    second:
+      enabled: 0
+      settings:
+        CPU: None
+  - first:
+      Standalone: Win64
+    second:
+      enabled: 1
+      settings:
+        CPU: AnyCPU
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: