Эх сурвалжийг харах

Building AtomicSharp shared library

Josh Engebretson 10 жил өмнө
parent
commit
e68854882b

+ 1 - 1
Source/AtomicJS/CMakeLists.txt

@@ -35,7 +35,7 @@ endforeach()
 
 file (GLOB JAVASCRIPT_BINDINGS_SOURCE ${CMAKE_SOURCE_DIR}/Build/Source/Generated/${JAVASCRIPT_BINDINGS_PLATFORM}/Javascript/Packages/Atomic/*.cpp)
 
-set (SOURCE_FILES ${JAVASCRIPT_SOURCE} ${JAVASCRIPT_BINDINGS_SOURCE} )
+set (SOURCE_FILES ${JAVASCRIPT_SOURCE} ${JAVASCRIPT_BINDINGS_SOURCE})
 
 add_library(AtomicJS ${SOURCE_FILES})
 

+ 1 - 1
Source/AtomicPlayer/Application/CMakeLists.txt

@@ -52,7 +52,7 @@ else()
       XCODE_ATTRIBUTE_COMBINE_HIDPI_IMAGES NO
     )
 
-	set (TARGET_PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_SOURCE_DIR}/CMake/Modules/iOSBundleInfo.plist.template)
+    set (TARGET_PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_SOURCE_DIR}/CMake/Modules/iOSBundleInfo.plist.template)
 
 endif()
 endif(APPLE)

+ 51 - 0
Source/AtomicSharp/CMakeLists.txt

@@ -0,0 +1,51 @@
+
+include_directories(${CMAKE_CURRENT_SOURCE_DIR}
+                    ${CMAKE_SOURCE_DIR}/Source/ThirdParty
+                    ${CMAKE_SOURCE_DIR}/Source/ThirdParty/rapidjson/include
+                    ${CMAKE_SOURCE_DIR}/Source/ThirdParty/kNet/include
+                    ${CMAKE_SOURCE_DIR}/Source/ThirdParty/FreeType/include
+                    ${CMAKE_SOURCE_DIR}/Source/ThirdParty/Box2D)
+
+file (GLOB CSHARP_SOURCES *.cpp *.h)
+
+if (NOT MSVC)
+    # for kNet
+    add_definitions (-DUNIX)
+endif()
+
+# Create the JSBind files ahead of time, so they are picked up with glob
+set (CSFILES CSModuleAtomic2D.cpp;CSModuleAtomic3D.cpp;CSModuleAudio.cpp;CSModuleContainer.cpp;CSModuleCore.cpp;
+             CSModuleEngine.cpp;CSModuleEnvironment.cpp;CSModuleGraphics.cpp;CSModuleInput.cpp;
+             CSModuleIO.cpp;CSModuleJavascript.cpp;CSModuleMath.cpp;CSModuleNavigation.cpp;
+             CSModuleNetwork.cpp;CSModulePhysics.cpp;CSModuleResource.cpp;CSPackageAtomic.cpp;
+             CSModuleScene.cpp;CSModuleUI.cpp)
+
+foreach(CSFILE ${CSFILES})
+
+  set (CSFILEPATH "${CMAKE_SOURCE_DIR}/Build/Source/Generated/${JAVASCRIPT_BINDINGS_PLATFORM}/CSharp/Packages/Atomic/Native/${JSFILE}")
+
+  if (NOT EXISTS ${CSFILEPATH})
+    file(WRITE "${CSFILEPATH}" "// will be created by JSBind")
+  endif()
+
+endforeach()
+
+
+file (GLOB CSHARP_BINDINGS_SOURCE ${CMAKE_SOURCE_DIR}/Build/Source/Generated/${JAVASCRIPT_BINDINGS_PLATFORM}/CSharp/Packages/Atomic/Native/*.cpp)
+
+set (SOURCE_FILES ${CSHARP_SOURCES} ${CSHARP_BINDINGS_SOURCE})
+
+add_library(AtomicSharp SHARED ${SOURCE_FILES})
+
+target_link_libraries(AtomicSharp AtomicJS AtomicPlayerLib AtomicPlayerJS ${ATOMIC_LINK_LIBRARIES})
+
+if (APPLE)
+
+target_link_libraries(AtomicSharp "-framework AudioUnit -framework Carbon -framework Cocoa -framework CoreAudio -framework ForceFeedback -framework IOKit -framework OpenGL -framework CoreServices -framework Security")
+
+endif()
+
+if (NOT IOS AND NOT ANDROID AND NOT EMSCRIPTEN)
+    add_dependencies(AtomicSharp AtomicTool)
+endif()
+

+ 1 - 0
Source/CMakeLists.txt

@@ -6,6 +6,7 @@ add_subdirectory(AtomicPlayer)
 add_subdirectory(AtomicPlayerJS)
 
 if (NOT IOS AND NOT ANDROID AND NOT EMSCRIPTEN)
+    add_subdirectory(AtomicSharp)
     add_subdirectory(ToolCore)
     add_subdirectory(ToolCoreJS)
     add_subdirectory(AtomicEditor)

+ 0 - 1
Source/ToolCore/JSBind/CSharp/CSClassWriter.cpp

@@ -25,7 +25,6 @@ CSClassWriter::CSClassWriter(JSBClass *klass) : JSBClassWriter(klass)
 
 }
 
-
 void CSClassWriter::WriteFunctions(String& source)
 {
     for (unsigned i = 0; i < klass_->functions_.Size(); i++)

+ 186 - 1
Source/ToolCore/JSBind/CSharp/CSFunctionWriter.cpp

@@ -29,14 +29,199 @@ void CSFunctionWriter::WriteParameterMarshal(String& source)
 
 }
 
-
 void CSFunctionWriter::WriteConstructor(String& source)
 {
 
 }
 
+void CSFunctionWriter::GenNativeFunctionSignature(String& sig)
+{
+    // generate args
+    Vector<JSBFunctionType*>& parameters = function_->GetParameters();
+
+    int cparam = 0;
+    if (parameters.Size())
+    {
+        for (unsigned int i = 0; i < parameters.Size(); i++, cparam++)
+        {
+            JSBFunctionType * ptype = parameters.At(i);
+
+            // ignore "Context" parameters
+            if (ptype->type_->asClassType())
+            {
+                JSBClassType* classType = ptype->type_->asClassType();
+                JSBClass* klass = classType->class_;
+                if (klass->GetName() == "Context")
+                {
+                    cparam--;
+                    continue;
+                }
+            }
+
+            String pstring = ptype->ToArgString(cparam);
+
+            if (ptype->type_->asClassType())
+            {
+                JSBClassType* classType = ptype->type_->asClassType();
+
+                JSBClass* klass = classType->class_;
+
+                if (!klass->IsNumberArray())
+                {
+                    sig.AppendWithFormat("%s", pstring.CString());
+                }
+                else
+                {
+                    sig.AppendWithFormat("%s __arg%i", klass->GetNativeName().CString(), cparam);
+                }
+            }
+            else if (ptype->type_->asStringType() || ptype->type_->asStringHashType())
+            {
+                sig.AppendWithFormat("char* __arg%i", cparam);
+            }
+            else if (ptype->type_->asHeapPtrType())
+            {
+                assert(0);
+            }
+            else if (ptype->type_->asPrimitiveType())
+            {
+                JSBPrimitiveType* prtype = ptype->type_->asPrimitiveType();
+                sig.AppendWithFormat("%s __arg%i", prtype->ToString().CString(), cparam);
+            }
+            else if (ptype->type_->asEnumType())
+            {
+                JSBEnumType* etype = ptype->type_->asEnumType();
+
+                sig.AppendWithFormat("%s __arg%i", etype->enum_->GetName().CString(), cparam);
+
+            }
+            else if (ptype->type_->asVectorType())
+            {
+                // read only vector arguments
+                if (ptype->isConst_)
+                {
+                    JSBVectorType* vtype = ptype->type_->asVectorType();
+                    sig.AppendWithFormat("%s __arg%i", vtype->ToString().CString(), cparam);
+                }
+            }
+            else
+            {
+                assert(0);
+            }
+
+            sig += ", ";
+
+        }
+    }
+
+    if (sig.EndsWith(", "))
+        sig = sig.Substring(0, sig.Length() - 2);
+
+}
+
 void CSFunctionWriter::WriteFunction(String& source)
 {
+    JSBClass* klass = function_->class_;
+
+    String sig;
+
+    GenNativeFunctionSignature(sig);
+
+    JSBFunctionType* returnType = function_->returnType_;
+    String rTypeString = "void";
+    // if the marshalling local variable type is differnt
+    // for example SharedPtr ->Object *
+    String rMarshalTypeString;
+
+    if (returnType)
+    {
+        if (returnType->type_->asStringType())
+        {
+            rTypeString = "String";
+        }
+        else if (returnType->type_->asPrimitiveType())
+        {
+            JSBPrimitiveType* prtype = returnType->type_->asPrimitiveType();
+            rTypeString = prtype->ToString();
+        }
+        else if (returnType->type_->asClassType())
+        {
+            JSBClassType* klassType = returnType->type_->asClassType();
+
+            if (returnType->isTemplate_)
+            {
+                if (klassType->class_->IsObject())
+                    rTypeString = "const Object*";
+                else
+                    rTypeString = "const RefCounted*";
+
+                rMarshalTypeString.AppendWithFormat("SharedPtr<%s>", klassType->class_->GetNativeName().CString());
+            }
+            else if (klassType->class_->IsObject())
+            {
+                rTypeString = "const Object*";
+            }
+            else if (klassType->class_->IsNumberArray())
+            {
+                rTypeString = klassType->class_->GetName().CString();
+            }
+            else
+            {
+                rTypeString = "const RefCounted*";
+            }
+        }
+        else if (returnType->type_->asEnumType())
+        {
+            JSBEnumType* enumType = returnType->type_->asEnumType();
+            rTypeString = enumType->enum_->GetName().CString();
+        }
+        else if (returnType->type_->asVectorType())
+        {
+            JSBVectorType* vtype = returnType->type_->asVectorType();
+            rTypeString = "";
+            rTypeString.AppendWithFormat("%s", vtype->ToString().CString());
+        }
+
+    }
+
+
+
+    source.AppendWithFormat("%s csb_%s_%s(%s* self%s)\n{\n", rTypeString == "String" ? "const char*" : rTypeString.CString(), klass->GetName().CString(),
+                            function_->name_.CString(), klass->GetNativeName().CString(), sig.Length() ? (", " + sig).CString() : "");
+
+    if (rTypeString != "void")
+    {
+        source.AppendWithFormat("%s retValue = ", rMarshalTypeString.Length()? rMarshalTypeString.CString() : rTypeString.CString());
+    }
+
+    // call
+
+    source.AppendWithFormat("self->%s(", function_->name_.CString());
+
+    Vector<JSBFunctionType*>& parameters = function_->GetParameters();
+
+    for (unsigned int i = 0; i < parameters.Size(); i++)
+    {
+        source.AppendWithFormat("__arg%i",  i);
+
+        if (i != parameters.Size() - 1)
+        {
+            source += ", ";
+        }
+    }
+
+    source += ");\n";
+
+
+    if (rTypeString != "void")
+    {
+        if (rTypeString == "String")
+            source.AppendWithFormat("\nreturn retValue.CString();\n");
+        else
+            source.AppendWithFormat("\nreturn retValue;\n");
+    }
+
+    source.AppendWithFormat("}\n\n");
 
 }
 

+ 2 - 0
Source/ToolCore/JSBind/CSharp/CSFunctionWriter.h

@@ -34,6 +34,8 @@ public:
 
 private:
 
+    void GenNativeFunctionSignature(String& sig);
+
     void WriteFunction(String& source);
     void WriteConstructor(String& source);
     void WriteParameterMarshal(String& source);

+ 33 - 23
Source/ToolCore/JSBind/CSharp/CSModuleWriter.cpp

@@ -32,10 +32,10 @@ void CSModuleWriter::WriteIncludes(String& source)
     Vector<String>& includes = module_->includes_;
     for (unsigned i = 0; i < includes.Size(); i++)
     {
-      if (includes[i].StartsWith("<"))
-          source.AppendWithFormat("#include %s\n", includes[i].CString());
-      else
-          source.AppendWithFormat("#include \"%s\"\n", includes[i].CString());
+        if (includes[i].StartsWith("<"))
+            source.AppendWithFormat("#include %s\n", includes[i].CString());
+        else
+            source.AppendWithFormat("#include \"%s\"\n", includes[i].CString());
     }
 
     Vector<JSBHeader*> allheaders;
@@ -81,56 +81,56 @@ void CSModuleWriter::WriteIncludes(String& source)
 
 void CSModuleWriter::GenerateNativeSource()
 {
-    source_ = "// This file was autogenerated by JSBind, changes will be lost\n";
+    String source = "// This file was autogenerated by JSBind, changes will be lost\n";
 
-    source_ += "#ifdef ATOMIC_PLATFORM_WINDOWS\n";
+    source += "#ifdef ATOMIC_PLATFORM_WINDOWS\n";
 
-    source_ += "#pragma warning(disable: 4244) // possible loss of data\n";
+    source += "#pragma warning(disable: 4244) // possible loss of data\n";
 
-    source_ += "#endif\n";
+    source += "#endif\n";
 
     if (module_->Requires("3D"))
     {
-        source_ += "#ifdef ATOMIC_3D\n";
+        source += "#ifdef ATOMIC_3D\n";
     }
 
-    source_ += "#include <Duktape/duktape.h>\n";
-    source_ += "#include <AtomicJS/Javascript/JSVM.h>\n";
-    source_ += "#include <AtomicJS/Javascript/JSAPI.h>\n";
+    source += "#include <Duktape/duktape.h>\n";
+    source += "#include <AtomicJS/Javascript/JSVM.h>\n";
+    source += "#include <AtomicJS/Javascript/JSAPI.h>\n";
 
-    WriteIncludes(source_);
+    WriteIncludes(source);
 
     String ns = module_->GetPackage()->GetNamespace();
 
     if (ns != "Atomic")
     {
-        source_ += "\n\nusing namespace " + ns + ";\n\n";
+        source += "\n\nusing namespace " + ns + ";\n\n";
     }
 
-    source_ += "\n\nnamespace Atomic\n{\n \n";
+    source += "\n\nnamespace Atomic\n{\n \n";
 
-    source_ += "// Begin Class Declarations\n";
+    source += "// Begin Class Declarations\n";
 
-    source_ += "// End Class Declarations\n\n";
+    source += "// End Class Declarations\n\n";
 
-    source_ += "// Begin Classes\n";
+    source += "// Begin Classes\n";
 
     Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
 
     for (unsigned i = 0; i < classes.Size(); i++)
     {
         CSClassWriter clsWriter(classes[i]);
-        clsWriter.GenerateSource(source_);
+        clsWriter.GenerateSource(source);
     }
 
-    source_ += "// End Classes\n\n";
+    source += "// End Classes\n\n";
 
     // end Atomic namespace
-    source_ += "\n}\n";
+    source += "\n}\n";
 
     if (module_->Requires("3D"))
     {
-        source_ += "#endif //ATOMIC_3D\n";
+        source += "#endif //ATOMIC_3D\n";
     }
 
     JSBind* jsbind = module_->GetSubsystem<JSBind>();
@@ -139,13 +139,23 @@ void CSModuleWriter::GenerateNativeSource()
 
     File file(module_->GetContext());
     file.Open(filepath, FILE_WRITE);
-    file.Write(source_.CString(), source_.Length());
+    file.Write(source.CString(), source.Length());
     file.Close();
 
 }
 
 void CSModuleWriter::GenerateManagedSource()
 {
+    String source = "// Hello C#!";
+
+    JSBind* jsbind = module_->GetSubsystem<JSBind>();
+
+    String filepath = jsbind->GetDestScriptFolder() + "/CSModule" + module_->name_ + ".cs";
+
+    File file(module_->GetContext());
+    file.Open(filepath, FILE_WRITE);
+    file.Write(source.CString(), source.Length());
+    file.Close();
 
 }
 

+ 6 - 7
Source/ToolCore/JSBind/CSharp/CSPackageWriter.cpp

@@ -37,13 +37,6 @@ void CSPackageWriter::GenerateNativeSource()
     file.Open(filepath, FILE_WRITE);
     file.Write(source.CString(), source.Length());
     file.Close();
-
-    for (unsigned i = 0; i < package_->modules_.Size(); i++)
-    {
-        CSModuleWriter writer(package_->modules_[i]);
-        writer.GenerateSource();
-    }
-
 }
 
 void CSPackageWriter::GenerateManagedSource()
@@ -57,6 +50,12 @@ void CSPackageWriter::GenerateSource()
     GenerateNativeSource();
     GenerateManagedSource();
 
+    for (unsigned i = 0; i < package_->modules_.Size(); i++)
+    {
+        CSModuleWriter writer(package_->modules_[i]);
+        writer.GenerateSource();
+    }
+
 }
 
 }

+ 0 - 1
Source/ToolCore/JSBind/JSBModuleWriter.h

@@ -28,7 +28,6 @@ protected:
     JSBModuleWriter(JSBModule* module);
 
     JSBModule* module_;
-    String source_;
 
 };
 

+ 13 - 5
Source/ToolCore/JSBind/JSBind.cpp

@@ -45,18 +45,26 @@ bool JSBind::GenerateCSharpBindings()
     String modulesFolder = "Build/Source/Generated/" + platform_ + "/CSharp/Packages/";
     modulesFolder += package_->GetName() + "/";
 
-    String outputFolder = sourceRootFolder_ + "/" + modulesFolder;
+    String nativeOutputFolder = sourceRootFolder_ + "/" + modulesFolder + "Native/";
+    String managedOutputFolder = sourceRootFolder_ + "/" + modulesFolder + "Managed/";
 
     FileSystem* fs = GetSubsystem<FileSystem>();
 
-    if (!fs->CreateDirs(sourceRootFolder_, modulesFolder) || !fs->DirExists(outputFolder))
+    if (!fs->CreateDirs(sourceRootFolder_, modulesFolder + "Native/") || !fs->DirExists(nativeOutputFolder))
     {
-        String error = "Unable to create bindings output folder: " + outputFolder;
+        String error = "Unable to create bindings native output folder: " + nativeOutputFolder;
         ErrorExit(error.CString());
     }
 
-    destScriptFolder_ = outputFolder;
-    destNativeFolder_ = outputFolder;
+    if (!fs->CreateDirs(sourceRootFolder_, modulesFolder + "Managed/") || !fs->DirExists(managedOutputFolder))
+    {
+        String error = "Unable to create bindings managed output folder: " + managedOutputFolder;
+        ErrorExit(error.CString());
+    }
+
+
+    destScriptFolder_ = managedOutputFolder;
+    destNativeFolder_ = nativeOutputFolder;
 
     CSPackageWriter writer(package_);
     package_->GenerateSource(writer);

+ 0 - 2
Source/ToolCore/JSBind/JavaScript/JSFunctionWriter.cpp

@@ -36,7 +36,6 @@ void JSFunctionWriter::WriteParameterMarshal(String& source)
         {
             JSBFunctionType * ptype = parameters.At(i);
 
-
             // ignore "Context" parameters
             if (ptype->type_->asClassType())
             {
@@ -104,7 +103,6 @@ void JSFunctionWriter::WriteParameterMarshal(String& source)
                         source.AppendWithFormat("%s __arg%i(arrayData%i);\n", klass->GetNativeName().CString(), cparam, cparam);
                     }
 
-
                 }
 
             }

+ 24 - 24
Source/ToolCore/JSBind/JavaScript/JSModuleWriter.cpp

@@ -226,66 +226,66 @@ void JSModuleWriter::WriteModuleInit(String& source)
 
 void JSModuleWriter::GenerateSource()
 {
-    source_ = "// This file was autogenerated by JSBind, changes will be lost\n";
+    String source = "// This file was autogenerated by JSBind, changes will be lost\n";
 
-    source_ += "#ifdef ATOMIC_PLATFORM_WINDOWS\n";
+    source += "#ifdef ATOMIC_PLATFORM_WINDOWS\n";
 
-    source_ += "#pragma warning(disable: 4244) // possible loss of data\n";
+    source += "#pragma warning(disable: 4244) // possible loss of data\n";
 
-    source_ += "#endif\n";
+    source += "#endif\n";
 
     if (module_->Requires("3D"))
     {
-        source_ += "#ifdef ATOMIC_3D\n";
+        source += "#ifdef ATOMIC_3D\n";
     }
 
-    source_ += "#include <Duktape/duktape.h>\n";
-    source_ += "#include <AtomicJS/Javascript/JSVM.h>\n";
-    source_ += "#include <AtomicJS/Javascript/JSAPI.h>\n";
+    source += "#include <Duktape/duktape.h>\n";
+    source += "#include <AtomicJS/Javascript/JSVM.h>\n";
+    source += "#include <AtomicJS/Javascript/JSAPI.h>\n";
 
-    WriteIncludes(source_);
+    WriteIncludes(source);
 
     String ns = module_->GetPackage()->GetNamespace();
 
     if (ns != "Atomic")
     {
-        source_ += "\n\nusing namespace " + ns + ";\n\n";
+        source += "\n\nusing namespace " + ns + ";\n\n";
     }
 
-    source_ += "\n\nnamespace Atomic\n{\n \n";
+    source += "\n\nnamespace Atomic\n{\n \n";
 
-    source_ += "// Begin Class Declarations\n";
+    source += "// Begin Class Declarations\n";
 
-    WriteForwardDeclarations(source_);
+    WriteForwardDeclarations(source);
 
-    source_ += "// End Class Declarations\n\n";
+    source += "// End Class Declarations\n\n";
 
-    source_ += "// Begin Classes\n";
+    source += "// Begin Classes\n";
 
     Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
 
     for (unsigned i = 0; i < classes.Size(); i++)
     {
         JSClassWriter clsWriter(classes[i]);
-        clsWriter.GenerateSource(source_);
+        clsWriter.GenerateSource(source);
     }
 
-    source_ += "// End Classes\n\n";
+    source += "// End Classes\n\n";
 
-    WriteClassDeclaration(source_);
+    WriteClassDeclaration(source);
 
-    WriteClassDefine(source_);
+    WriteClassDefine(source);
 
-    WriteModulePreInit(source_);
+    WriteModulePreInit(source);
 
-    WriteModuleInit(source_);
+    WriteModuleInit(source);
 
     // end Atomic namespace
-    source_ += "\n}\n";
+    source += "\n}\n";
 
     if (module_->Requires("3D"))
     {
-        source_ += "#endif //ATOMIC_3D\n";
+        source += "#endif //ATOMIC_3D\n";
     }
 
     JSBind* jsbind = module_->GetSubsystem<JSBind>();
@@ -294,7 +294,7 @@ void JSModuleWriter::GenerateSource()
 
     File file(module_->GetContext());
     file.Open(filepath, FILE_WRITE);
-    file.Write(source_.CString(), source_.Length());
+    file.Write(source.CString(), source.Length());
     file.Close();
 
 }