Browse Source

Package dependencies loading

Josh Engebretson 10 years ago
parent
commit
f466a8d9c1

+ 1 - 1
Source/Atomic/Core/Object.h

@@ -106,7 +106,7 @@ public:
     /// Return object category. Categories are (optionally) registered along with the object factory. Return an empty string if the object category is not registered.
     const String& GetCategory() const;
 
-    bool IsObject() const { return true; }
+    virtual bool IsObject() const { return true; }
     
 protected:
     /// Execution context.

+ 22 - 0
Source/AtomicJS/Packages/Atomic/Graphics.json

@@ -0,0 +1,22 @@
+{
+	"name" : "Graphics",
+	"sources" : ["Source/Atomic/Graphics"],
+	"includes" : ["<Atomic/Scene/Scene.h>", "<Atomic/Graphics/OcclusionBuffer.h>", "<Atomic/Scene/ValueAnimation.h>"],
+	"classes" : ["Graphics", "Texture", "Texture2D", "Material", "Drawable",
+				 "Viewport", "Renderer", "Octree", "Zone", "Camera", "Light", "RenderPath",
+				 "DebugRenderer",
+				 "RenderSurface", "Shader", "ShaderPrecache", "ShaderVariation",
+				 "Pass", "Technique",
+				 "Texture3D", "TextureCube", "View"],
+	"overloads" : {
+		"Viewport" : {
+			"Viewport" : ["Context", "Scene", "Camera", "RenderPath"],
+			"SetRenderPath" : ["XMLFile"]
+		},
+		"Camera" : {
+			"SetOrthoSize" : ["float"]
+		}
+
+	}
+
+}

+ 5 - 0
Source/AtomicJS/Packages/Atomic/Math.json

@@ -0,0 +1,5 @@
+{
+	"name" : "Math",
+	"sources" : ["Source/Atomic/Math"],
+	"classes" : ["Color", "Vector2", "Vector3", "Vector4", "Quaternion", "BoundingBox", "Rect", "IntRect", "IntVector2"]
+}

+ 2 - 2
Source/AtomicJS/Packages/Atomic/Package.json

@@ -2,7 +2,7 @@
 {
 
   "name" : "Atomic",
-  "namespace" : "Atomic",  
-  "modules" : ["Container", "Core"]
+  "namespace" : "Atomic",
+  "modules" : ["Container", "Math", "Core", "Graphics", "Scene"]
 
 }

+ 21 - 0
Source/AtomicJS/Packages/Atomic/Scene.json

@@ -0,0 +1,21 @@
+{
+	"name" : "Scene",
+	"sources" : ["Source/Atomic/Scene"],
+	"includes" : ["<Atomic/Scene/LogicComponent.h>", "<Atomic/Network/Connection.h>"],
+	"classes" : ["Animatable", "Node", "Scene", "Component", "Serializable",
+				 "ObjectAnimation", "SmoothedTransform", "SplinePath",
+				 "ValueAnimation", "ValueAnimationInfo"],
+	"overloads" : {
+		"Node" : {
+			"CreateChild" : ["String", "CreateMode", "unsigned"],
+			"GetChild" : ["String", "bool"],
+			"SetScale" : ["Vector3"],
+			"SetPosition2D" : ["Vector2"],
+			"SetScale2D" : ["Vector2"]
+		},
+		"ValueAnimationInfo" : {
+			"ValueAnimationInfo" : ["ValueAnimation", "WrapMode", "float"]
+		}
+
+	}
+}

+ 9 - 0
Source/AtomicJS/Packages/ToolCore/Package.json

@@ -0,0 +1,9 @@
+
+{
+
+  "name" : "ToolCore",
+  "namespace" : "ToolCore",
+  "dependencies" : ["Source/AtomicJS/Packages/Atomic"],
+  "modules" : ["ToolCore"]
+
+}

+ 5 - 0
Source/AtomicJS/Packages/ToolCore/ToolCore.json

@@ -0,0 +1,5 @@
+{
+	"name" : "ToolCore",
+	"sources" : ["Source/ToolCore"],
+	"classes" : ["ToolEnvironment"]
+}

+ 1 - 1
Source/ToolCore/Command/BindCmd.cpp

@@ -44,7 +44,7 @@ void BindCmd::Run()
 
     bindPlatform_ = "MACOSX";
     sourceRootFolder_ = env->GetRootSourceDir();
-    packageFolder_ = "Source/AtomicJS/Packages/Atomic/";
+    packageFolder_ = "Source/AtomicJS/Packages/ToolCore/";
 
     SharedPtr<JSBind> jsbind(new JSBind(context_));
 

+ 105 - 1
Source/ToolCore/JSBind/JSBClass.cpp

@@ -1,3 +1,4 @@
+#include <Atomic/Core/ProcessUtils.h>
 #include <Atomic/IO/Log.h>
 #include <Atomic/IO/File.h>
 #include <Atomic/Resource/JSONFile.h>
@@ -9,11 +10,67 @@
 namespace ToolCore
 {
 
+JSBFunctionOverride::JSBFunctionOverride(const String &name, const Vector<String>& sig) : parsed_(false)
+{
+    name_ = name;
+    sig_ = sig;
+}
+
+void JSBFunctionOverride::Parse()
+{
+    if (parsed_)
+        return;
+
+    parsed_ = true;
+
+    for (unsigned i = 0; i < sig_.Size(); i++)
+    {
+        JSBType* type = JSBType::Parse(sig_.At(i));
+        if (!type)
+        {
+            ErrorExit("Unable to parse override type");
+        }
+        types_.Push(type);
+    }
+
+}
+
 JSBClass::JSBClass(Context* context, JSBModule *module, const String& name, const String& nativeName) : Object(context),
     module_(module), name_(name), nativeName_(nativeName),
-    isAbstract_(false)
+    isAbstract_(false), isObject_(false),
+    numberArrayElements_(0), arrayElementType_("float")
 {
 
+    if (nativeName == "Object")
+        SetObject(true);
+
+    // detect array type, TODO: this could go in module json
+
+    if (name_ == "Color")
+        numberArrayElements_ = 4;
+    else if (name_ == "Vector2")
+        numberArrayElements_ = 2;
+    else if (name_ == "Vector3")
+        numberArrayElements_ = 3;
+    else if (name_ == "Vector4")
+        numberArrayElements_ = 4;
+    else if (name_ == "Quaternion")
+        numberArrayElements_ = 4;
+    else if (name_ == "BoundingBox")
+        numberArrayElements_ = 6;
+    else if (name_ == "Rect")
+        numberArrayElements_ = 4;
+    else if (name_ == "IntRect")
+    {
+        numberArrayElements_ = 4;
+        arrayElementType_ = "int";
+    }
+    else if (name_ == "IntVector2")
+    {
+        numberArrayElements_ = 2;
+        arrayElementType_ = "int";
+    }
+
 }
 
 JSBClass::~JSBClass()
@@ -21,4 +78,51 @@ JSBClass::~JSBClass()
 
 }
 
+void JSBClass::AddFunction(JSBFunction* function)
+{
+    functions_.Push(function);
+}
+
+void JSBClass::AddBaseClass(JSBClass *baseClass)
+{
+    baseClasses_.Push(baseClass);
+
+    if (baseClass->GetNativeName() == "Object")
+        SetObject(true);
+}
+
+void JSBClass::Dump()
+{
+    if (name_ != nativeName_)
+    {
+        LOGINFOF("Class: %s (%s)", name_.CString(), nativeName_.CString());
+    }
+    else
+    {
+        LOGINFOF("Class: %s", name_.CString());
+    }
+
+    LOGINFOF("   IsObject: %s", IsObject() ? "true" : "false");
+
+    if (baseClasses_.Size())
+    {
+        LOGINFOF("   Bases:");
+        for (unsigned i = 0; i < baseClasses_.Size(); i++)
+        {
+
+            LOGINFOF("      %s", baseClasses_.At(i)->GetName().CString());
+        }
+    }
+
+    if (functions_.Size())
+    {
+        for (unsigned i = 0; i < functions_.Size(); i++)
+        {
+            functions_.At(i)->Dump();
+        }
+    }
+
+}
+
+
 }

+ 47 - 6
Source/ToolCore/JSBind/JSBClass.h

@@ -12,6 +12,25 @@ namespace ToolCore
 
 class JSBModule;
 class JSBFunction;
+class JSBType;
+
+// chosen function overrides
+class JSBFunctionOverride
+{
+
+public:
+
+    JSBFunctionOverride(const String& name, const Vector<String>& sig);
+
+    String name_;
+    Vector<String> sig_;
+    Vector<JSBType*> types_;
+
+    void Parse() ;
+
+    bool parsed_;
+
+};
 
 class JSBClass : public Object
 {
@@ -26,16 +45,30 @@ public:
     const String& GetName() { return name_; }
     const String& GetNativeName() { return nativeName_; }
 
+    bool IsAbstract() { return isAbstract_; }
+
+    /// Note that if we at some point want to generate bindings for JSBClass
+    /// this override will need to be addressed, as we'll need to know that JSBClass is
+    /// itself an object
+    bool IsObject() { return isObject_; }
+
+    void SetAbstract(bool value = true) { isAbstract_ = value; }
+    void SetObject(bool value = true) { isObject_ = value; }
+
+    bool IsNumberArray() { return numberArrayElements_ != 0; }
+    int  GetNumberArrayElements() { return numberArrayElements_;}
+    const String& GetArrayElementType() const { return arrayElementType_; }
+
     void SetHeader(JSBHeader* header) { header_ = header; }
 
-    void AddBaseClass(JSBClass* baseClass) { baseClasses_.Push(baseClass); }
-    void SetAbstract() { isAbstract_ = true; }
+    void AddBaseClass(JSBClass* baseClass);
 
     void AddFunction(JSBFunction* function);
+    void AddFunctionOverride(JSBFunctionOverride* override) { overrides_.Push(override); }
 
-private:
+    void Dump();
 
-    bool isAbstract_;
+private:
 
     String name_;
     String nativeName_;
@@ -43,10 +76,18 @@ private:
     SharedPtr<JSBHeader> header_;
     SharedPtr<JSBModule> module_;
 
-
-    PODVector<JSBFunction*> functions;
+    PODVector<JSBFunction*> functions_;
     PODVector<JSBClass*> baseClasses_;
 
+    PODVector<JSBFunctionOverride*> overrides_;
+
+    bool isAbstract_;
+    bool isObject_;
+
+    // Vector3, Color, etc are marshalled via arrays
+    int numberArrayElements_;
+    String arrayElementType_;
+
 };
 
 

+ 1 - 1
Source/ToolCore/JSBind/JSBEnum.h

@@ -17,7 +17,7 @@ class JSBEnum : public Object
 
 public:
 
-    JSBEnum(Context* context, JSBModule* module, const String& name);
+    JSBEnum(Context* context, JSBModule* module, const String& name);    
     virtual ~JSBEnum();
 
     const String& GetName() { return name_; }

+ 48 - 22
Source/ToolCore/JSBind/JSBFunction.h

@@ -77,19 +77,36 @@ class JSBFunction : public JSBSymbol
 {
 public:
 
-    JSBClass* class_;
-    String name_;
-    String propertyName_;
-    JSBFunctionType* returnType_;
-    Vector<JSBFunctionType*> parameters_;
-    String docString_;
+    JSBFunction(JSBClass* klass) : class_(klass), returnType_(0),
+                                   isConstructor_(false), isDestructor_(false),
+                                   isGetter_(false), isSetter_(false),
+                                   isOverride_(false), skip_(false)
+    {
 
-    bool isConstructor_;
-    bool isDestructor_;
-    bool isGetter_;
-    bool isSetter_;
-    bool isOverride_;
-    bool skip_;
+    }
+
+    const String& GetName() { return name_; }
+
+    bool IsConstructor() { return isConstructor_; }
+    bool IsDestructor() { return isDestructor_; }
+    bool IsSetter() { return isSetter_; }
+    bool IsGetter() { return isGetter_; }
+    bool IsOverride() { return isOverride_; }
+    bool Skip() { return skip_; }
+
+    JSBFunctionType* GetReturnType() { return returnType_; }
+
+    const String& GetDocString() { return docString_; }
+
+    void SetName(const String& name) { name_ = name; }
+    void SetConstructor(bool value = true) { isConstructor_ = value; }
+    void SetDestructor(bool value = true) { isDestructor_ = value; }
+    void SetSetter(bool value = true) { isSetter_ = value; }
+    void SetGetter(bool value = true) { isGetter_ = value; }
+    void SetOverride(bool value = true) { isOverride_ = value; }
+    void SetSkip(bool value) { skip_ = value; }
+    void SetReturnType(JSBFunctionType* retType) { returnType_ = retType; }
+    void SetDocString(const String& docString) { docString_ = docString; }
 
     int FirstDefaultParameter()
     {
@@ -107,16 +124,6 @@ public:
         parameters_.Push(parm);
     }
 
-    JSBFunction(JSBClass* klass) : class_(klass), returnType_(0),
-                                   isConstructor_(false), isDestructor_(false),
-                                   isGetter_(false), isSetter_(false),
-                                   isOverride_(false), skip_(false)
-    {
-
-    }
-
-    void SetSkip(bool value) { skip_ = value; }
-    bool Skip() { return skip_; }
 
     void Process();
 
@@ -150,6 +157,25 @@ public:
 
     }
 
+private:
+
+    SharedPtr<JSBClass> class_;
+
+    String name_;
+    String propertyName_;
+
+    JSBFunctionType* returnType_;
+    Vector<JSBFunctionType*> parameters_;
+
+    String docString_;
+
+    bool isConstructor_;
+    bool isDestructor_;
+    bool isGetter_;
+    bool isSetter_;
+    bool isOverride_;
+    bool skip_;
+
 };
 
 }

+ 7 - 0
Source/ToolCore/JSBind/JSBHeader.cpp

@@ -4,6 +4,7 @@
 using namespace CPlusPlus;
 
 #include "JSBPreprocessVisitor.h"
+#include "JSBHeaderVisitor.h"
 
 #include "JSBModule.h"
 #include "JSBHeader.h"
@@ -69,6 +70,12 @@ void JSBHeader::VisitPreprocess()
     JSBPreprocessVisitor(this, translationUnit_,globalNamespace_);
 }
 
+void JSBHeader::VisitHeader()
+{
+    JSBHeaderVisitor(this, translationUnit_, globalNamespace_);
+}
+
+
 
 
 }

+ 18 - 9
Source/ToolCore/JSBind/JSBHeaderVisitor.h

@@ -236,24 +236,29 @@ public:
         if (function->isVariadic())
             return NULL;
 
-        jfunction->name_ = getNameString(function->name());
+        String name = getNameString(function->name());
+
+        jfunction->SetName(name);
 
         // don't support operators atm
-        if (jfunction->name_.StartsWith("operator "))
+        if (name.StartsWith("operator "))
             return NULL;
 
-        if (jfunction->name_ == klass->GetName())
-            jfunction->isConstructor_ = true;
+        if (name == klass->GetName())
+            jfunction->SetConstructor();
 
-        if (jfunction->name_.StartsWith("~"))
-            jfunction->isDestructor_ = true;
+        if (name.StartsWith("~"))
+            jfunction->SetDestructor();
 
         // see if we support return type
         if (function->hasReturnType() && !function->returnType().type()->isVoidType())
         {
-            jfunction->returnType_ = processFunctionReturnType(function);
-            if (!jfunction->returnType_)
+            JSBFunctionType* returnType = processFunctionReturnType(function);
+
+            if (!returnType)
                 return NULL;
+
+            jfunction->SetReturnType(returnType);
         }
 
         if (function->hasArguments())
@@ -312,7 +317,11 @@ public:
             {
                 int index = 3;
                 while(comment[index] && comment[index] != '\n' && comment[index] != '\r')
-                    jfunction->docString_ += comment[index++];
+                {
+                    String docString = jfunction->GetDocString();
+                    docString += comment[index++];
+                    jfunction->SetDocString(docString);
+                }
 
             }
 

+ 102 - 3
Source/ToolCore/JSBind/JSBModule.cpp

@@ -34,6 +34,78 @@ void JSBModule::PreprocessHeaders()
     }
 }
 
+void JSBModule::VisitHeaders()
+{
+    for (unsigned i = 0; i < headers_.Size(); i++)
+    {
+        headers_[i]->VisitHeader();
+    }
+
+    // validate that all classes found
+    for (unsigned i = 0; i < classnames_.Size(); i++)
+    {
+        JSBClass* cls = GetClass(classnames_[i]);
+
+        if (!cls)
+        {
+            ErrorExit(ToString("Module class not found %s", classnames_[i].CString()));
+        }
+    }
+
+    ProcessOverloads();
+
+}
+
+void JSBModule::ProcessOverloads()
+{
+    // overloads
+
+    JSONValue root = moduleJSON_->GetRoot();
+
+    JSONValue overloads = root.GetChild("overloads");
+
+    if (overloads.IsObject())
+    {
+        Vector<String> childNames = overloads.GetChildNames();
+
+        for (unsigned j = 0; j < childNames.Size(); j++)
+        {
+            String classname = childNames.At(j);
+
+            JSBClass* klass = GetClass(classname);
+
+            if (!klass)
+            {
+                ErrorExit("Bad overload klass");
+            }
+
+            JSONValue classoverloads = overloads.GetChild(classname);
+
+            Vector<String> functionNames = classoverloads.GetChildNames();
+
+            for (unsigned k = 0; k < functionNames.Size(); k++)
+            {
+                JSONValue sig = classoverloads.GetChild(functionNames[k]);
+
+                if (!sig.IsArray())
+                {
+                    ErrorExit("Bad overload defintion");
+                }
+
+                Vector<String> values;
+                for (unsigned x = 0; x < sig.GetSize(); x++)
+                {
+                    values.Push(sig.GetString(x));
+                }
+
+                JSBFunctionOverride* fo = new JSBFunctionOverride(functionNames[k], values);
+                klass->AddFunctionOverride(fo);
+
+            }
+        }
+    }
+}
+
 void JSBModule::ScanHeaders()
 {
     JSBind* jsbind = GetSubsystem<JSBind>();
@@ -126,6 +198,12 @@ bool JSBModule::ContainsConstant(const String& constantName)
 
 void JSBModule::RegisterConstant(const String& constantName)
 {
+    // MAX_CASCADE_SPLITS is defined differently for desktop/mobile
+    if (constantName == "MAX_CASCADE_SPLITS" && JSBPackage::ContainsConstantAllPackages(constantName))
+    {
+        return;
+    }
+
     if (JSBPackage::ContainsConstantAllPackages(constantName))
     {
         ErrorExit(ToString("Constant collision: %s", constantName.CString()));
@@ -147,15 +225,15 @@ bool JSBModule::Load(const String& jsonFilename)
         return false;
     }
 
-    SharedPtr<JSONFile> moduleJSON(new JSONFile(context_));
+    moduleJSON_ = new JSONFile(context_);
 
-    if (!moduleJSON->BeginLoad(*jsonFile))
+    if (!moduleJSON_->BeginLoad(*jsonFile))
     {
         LOGERRORF("Unable to parse module json: %s", jsonFilename.CString());
         return false;
     }
 
-    JSONValue root = moduleJSON->GetRoot();
+    JSONValue root = moduleJSON_->GetRoot();
 
     name_ = root.GetString("name");
 
@@ -189,6 +267,27 @@ bool JSBModule::Load(const String& jsonFilename)
         sourceDirs_.Push(sources.GetString(i));
     }
 
+    if (name_ == "Graphics")
+    {
+#ifdef _MSC_VER
+        if (JSBind::PLATFORM == "ANDROID" || JSBind::PLATFORM == "WEB")
+        {
+            sourceDirs_.Push("Source/Atomic/Graphics/OpenGL");
+        }
+        else
+        {
+#ifdef ATOMIC_D3D11
+            sourceDirs_.Push("Source/Atomic/Graphics/Direct3D11");
+#else
+            sourceDirs_.Push("Source/Atomic/Graphics/Direct3D9");
+#endif
+        }
+#else
+        sourceDirs_.Push("Source/Atomic/Graphics/OpenGL");
+#endif
+    }
+
+
     ScanHeaders();
 
     return true;

+ 10 - 1
Source/ToolCore/JSBind/JSBModule.h

@@ -5,6 +5,11 @@
 
 using namespace Atomic;
 
+namespace Atomic
+{
+class JSONFile;
+}
+
 namespace ToolCore
 {
 
@@ -36,10 +41,12 @@ public:
 
     bool Load(const String& jsonFilename);
     void PreprocessHeaders();
-
+    void VisitHeaders();
 
 private:
 
+    void ProcessOverloads();
+
     void ScanHeaders();
 
     String name_;
@@ -57,6 +64,8 @@ private:
     HashMap<StringHash, SharedPtr<JSBEnum> > enums_;
     Vector<String> constants_;
 
+    SharedPtr<JSONFile> moduleJSON_;
+
 };
 
 

+ 36 - 2
Source/ToolCore/JSBind/JSBPackage.cpp

@@ -1,7 +1,9 @@
 #include <Atomic/IO/Log.h>
 #include <Atomic/IO/File.h>
+#include <Atomic/IO/FileSystem.h>
 #include <Atomic/Resource/JSONFile.h>
 
+#include "JSBind.h"
 #include "JSBModule.h"
 #include "JSBPackage.h"
 
@@ -28,6 +30,14 @@ void JSBPackage::PreprocessModules()
     }
 }
 
+void JSBPackage::ProcessModules()
+{
+    for (unsigned i = 0; i < modules_.Size(); i++)
+    {
+        modules_[i]->VisitHeaders();
+    }
+}
+
 JSBClass* JSBPackage::GetClass(const String& name)
 {
     for (unsigned i = 0; i < modules_.Size(); i++)
@@ -121,6 +131,29 @@ bool JSBPackage::Load(const String& packageFolder)
 
     JSONValue root = packageJSON->GetRoot();
 
+    // first load dependencies
+    JSONValue deps = root.GetChild("dependencies");
+
+    if (deps.IsArray())
+    {
+        JSBind* jsbind = GetSubsystem<JSBind>();
+
+        for (unsigned i = 0; i < deps.GetSize(); i++)
+        {
+            String dpackageFolder = AddTrailingSlash(deps.GetString(i));
+
+            SharedPtr<JSBPackage> depPackage (new JSBPackage(context_));
+
+            if (!depPackage->Load(jsbind->GetSourceRootFolder() + dpackageFolder))
+            {
+                LOGERRORF("Unable to load package dependency: %s", dpackageFolder.CString());
+                return false;
+            }
+
+        }
+
+    }
+
     name_ = root.GetString("name");
     namespace_ = root.GetString("namespace");
 
@@ -142,10 +175,11 @@ bool JSBPackage::Load(const String& packageFolder)
 
     }
 
-    PreprocessModules();
-
     allPackages_.Push(SharedPtr<JSBPackage>(this));
 
+    PreprocessModules();
+    ProcessModules();
+
     return true;
 }
 

+ 2 - 0
Source/ToolCore/JSBind/JSBPackage.h

@@ -26,6 +26,8 @@ public:
 
     void PreprocessModules();
 
+    void ProcessModules();
+
     JSBClass* GetClass(const String& name);
 
     // get a class by name across all loaded packages