Browse Source

Working on C#

Josh Engebretson 10 years ago
parent
commit
b4326a6a85

+ 56 - 0
Build/AtomicSharp/AtomicSharp.csproj

@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <RootNamespace>AtomicSharp</RootNamespace>
+    <AssemblyName>AtomicSharp</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug</OutputPath>
+    <DefineConstants>DEBUG;</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <ConsolePause>false</ConsolePause>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>full</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <ConsolePause>false</ConsolePause>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="MyClass.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="..\Source\Generated\MACOSX\CSharp\Packages\Atomic\Managed\CSModuleContainer.cs">
+      <Link>CSModuleContainer.cs</Link>
+    </Compile>
+    <Compile Include="..\Source\Generated\MACOSX\CSharp\Packages\Atomic\Managed\CSModuleCore.cs">
+      <Link>CSModuleCore.cs</Link>
+    </Compile>
+    <Compile Include="..\Source\Generated\MACOSX\CSharp\Packages\Atomic\Managed\CSModuleScene.cs">
+      <Link>CSModuleScene.cs</Link>
+    </Compile>
+    <Compile Include="..\Source\Generated\MACOSX\CSharp\Packages\Atomic\Managed\CSModuleResource.cs">
+      <Link>CSModuleResource.cs</Link>
+    </Compile>
+    <Compile Include="..\Source\Generated\MACOSX\CSharp\Packages\Atomic\Managed\CSModuleGraphics.cs">
+      <Link>CSModuleGraphics.cs</Link>
+    </Compile>
+    <Compile Include="..\Source\Generated\MACOSX\CSharp\Packages\Atomic\Managed\CSModuleIO.cs">
+      <Link>CSModuleIO.cs</Link>
+    </Compile>
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+</Project>

+ 17 - 0
Build/AtomicSharp/AtomicSharp.sln

@@ -0,0 +1,17 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2012
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AtomicSharp", "AtomicSharp.csproj", "{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+EndGlobal

+ 191 - 0
Build/AtomicSharp/MyClass.cs

@@ -0,0 +1,191 @@
+using System;
+using System.Collections.Generic;
+using System.Runtime.InteropServices;
+
+namespace AtomicSharp
+{
+
+	public class MyClass
+	{
+		void DoSomething()
+		{
+			var light = new Light ();
+			light.SetLightType(LightType.
+		}
+	}
+
+
+
+	
+
+
+
+
+
+	public static partial class Constants
+	{
+		public const string LIBNAME = "/Users/josh/Dev/atomic/AtomicGameEngineSharp-build/Source/AtomicSharp/AtomicSharp";
+	}
+
+	public partial class RefCounted
+	{
+		protected RefCounted (IntPtr native)
+		{
+			nativeInstance = native;
+		}
+
+		public IntPtr nativeInstance;
+
+		static public void _AddRef(IntPtr native)
+		{
+			csb_Atomic_RefCounted_AddRef(native);
+		}
+			
+		static public void _ReleaseRef(IntPtr native)
+		{
+			csb_Atomic_RefCounted_ReleaseRef(native);
+		}
+
+		[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
+		public static extern IntPtr csb_RefCounted_GetClassID (IntPtr self);	
+
+	}
+
+
+	static class NativeCore
+	{
+		// given an existing instance classID, construct the managed instance, with downcast support (ask for Component, get StaticModel for example)
+		public static Dictionary<IntPtr, Func<IntPtr, RefCounted>> nativeClassIDToManagedConstructor = new Dictionary<IntPtr, Func<IntPtr, RefCounted>>();
+
+		public static Dictionary<IntPtr, WeakReference> nativeLookup = new Dictionary<IntPtr, WeakReference> ();
+
+		public static IntPtr RegisterNative (IntPtr native, RefCounted r)
+		{
+			var w = new WeakReference (r);
+			NativeCore.nativeLookup [native] = w;
+			RefCounted._AddRef (native);
+			return native;
+		}
+
+		public static void ReleaseExpiredNativeReferences()
+		{
+			List<IntPtr> released = new List<IntPtr> ();
+
+			foreach(KeyValuePair<IntPtr, WeakReference> entry in nativeLookup)
+			{
+
+				if (entry.Value.Target == null || !entry.Value.IsAlive) {										
+					released.Add (entry.Key);
+					Console.WriteLine("Not Alive");
+				} else {
+					
+				}
+
+			}
+
+			foreach (IntPtr native in released) {
+				RefCounted._ReleaseRef(native);
+				nativeLookup.Remove (native);
+			}
+
+		}
+
+		// wraps an existing native instance, with downcast support
+		public static T WrapNative<T> (IntPtr native) where T:RefCounted
+		{
+			if (native == IntPtr.Zero)
+				return null;
+
+			WeakReference w;
+
+			// first see if we're already available
+			if (nativeLookup.TryGetValue (native, out w)) {
+
+				if (w.IsAlive) {
+
+					// we're alive!
+					return (T)w.Target; 
+
+				} else {
+
+					// we were seen before, but have since been GC'd, remove!
+					nativeLookup.Remove (native);
+				}
+			}
+
+			IntPtr classID = RefCounted.csb_RefCounted_GetClassID (native);
+
+			// and store, with downcast support for instance Component -> StaticModel
+			w = new WeakReference (nativeClassIDToManagedConstructor[classID](native));
+			NativeCore.nativeLookup [native] = w;
+
+			// store a ref, so native side will not be released while we still have a reference in managed code
+			RefCounted._AddRef (native);
+
+			return (T) w.Target;
+
+		}
+	}
+
+	[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
+	public struct Vector3
+	{
+		public Vector3 (float x, float y, float z)
+		{
+			this.x = x;
+			this.y = y;
+			this.z = z;
+		}
+
+		public override string ToString()
+		{
+			return x + ", " + y + ", " + z;
+		}
+
+		public float x;
+		public float y;
+		public float z;
+	}
+
+	[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
+	public struct Vector4
+	{
+	}
+		
+	[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
+	public struct Vector2
+	{
+	}
+
+	[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
+	public struct Quaternion
+	{
+	}
+
+	[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
+	public struct Color
+	{
+	}
+
+	[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
+	public struct IntRect
+	{
+	}
+
+	[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
+	public struct IntVector2
+	{
+	}
+
+	[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
+	public struct BoundingBox
+	{
+	}
+
+	[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
+	public struct Rect
+	{
+	}
+
+
+}

+ 27 - 0
Build/AtomicSharp/Properties/AssemblyInfo.cs

@@ -0,0 +1,27 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+// Information about this assembly is defined by the following attributes.
+// Change them to the values specific to your project.
+
+[assembly: AssemblyTitle ("AtomicSharp")]
+[assembly: AssemblyDescription ("")]
+[assembly: AssemblyConfiguration ("")]
+[assembly: AssemblyCompany ("")]
+[assembly: AssemblyProduct ("")]
+[assembly: AssemblyCopyright ("josh")]
+[assembly: AssemblyTrademark ("")]
+[assembly: AssemblyCulture ("")]
+
+// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
+// The form "{Major}.{Minor}.*" will automatically update the build and revision,
+// and "{Major}.{Minor}.{Build}.*" will update just the revision.
+
+[assembly: AssemblyVersion ("1.0.*")]
+
+// The following attributes are used to specify the signing key for the assembly,
+// if desired. See the Mono documentation for more information about signing.
+
+//[assembly: AssemblyDelaySign(false)]
+//[assembly: AssemblyKeyFile("")]
+

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

@@ -14,6 +14,7 @@
 #include "../JSBClass.h"
 #include "../JSBFunction.h"
 
+#include "CSTypeHelper.h"
 #include "CSClassWriter.h"
 #include "CSFunctionWriter.h"
 
@@ -58,6 +59,83 @@ void CSClassWriter::GenerateNativeSource(String& sourceOut)
     sourceOut += source;
 }
 
+
+void CSClassWriter::WriteManagedProperties(String& sourceOut)
+{
+    String source;
+
+    if (klass_->HasProperties())
+    {
+        Vector<String> pnames;
+        klass_->GetPropertyNames(pnames);
+
+        for (unsigned j = 0; j < pnames.Size(); j++)
+        {
+            JSBProperty* prop = klass_->GetProperty(pnames[j]);
+
+            JSBFunctionType* fType = NULL;
+            JSBFunctionType* getType = NULL;
+            JSBFunctionType* setType = NULL;
+
+            if (prop->getter_ && !prop->getter_->Skip())
+            {
+                fType = getType = prop->getter_->GetReturnType();
+            }
+            if (prop->setter_ && !prop->setter_->Skip())
+            {
+                setType = prop->setter_->GetParameters()[0];
+
+                if (!fType)
+                    fType = setType;
+            }
+
+            String type = CSTypeHelper::GetManagedTypeString(fType, false);
+            String line = ToString("public %s %s\n", type.CString(), prop->name_.CString());
+            source += IndentLine(line);
+            source += IndentLine("{\n");
+
+            Indent();
+
+            if (prop->getter_)
+            {
+                source += IndentLine("get\n");
+                source += IndentLine("{\n");
+
+                Indent();
+
+                source += IndentLine(ToString("return %s();\n", prop->getter_->GetName().CString()));
+
+                Dedent();
+
+                source += IndentLine("}\n");
+            }
+
+            if (prop->setter_)
+            {
+                source += IndentLine("set\n");
+                source += IndentLine("{\n");
+
+                Indent();
+
+                source += IndentLine(ToString("%s(value);\n", prop->setter_->GetName().CString()));
+
+                Dedent();
+
+                source += IndentLine("}\n");
+            }
+
+
+            Dedent();
+
+            source += IndentLine("}\n\n");
+        }
+
+    }
+
+    sourceOut += source;
+
+}
+
 void CSClassWriter::GenerateManagedSource(String& sourceOut)
 {
     String source = "";
@@ -81,6 +159,8 @@ void CSClassWriter::GenerateManagedSource(String& sourceOut)
 
     Indent();
 
+    WriteManagedProperties(source);
+
     // managed functions
     for (unsigned i = 0; i < klass_->functions_.Size(); i++)
     {

+ 3 - 0
Source/ToolCore/JSBind/CSharp/CSClassWriter.h

@@ -33,7 +33,10 @@ public:
 
 private:
 
+
     void WriteNativeFunctions(String& source);
+
+    void WriteManagedProperties(String& sourceOut);
     void WriteManagedFunctions(String& source);
 };
 

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

@@ -346,13 +346,16 @@ void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
 {
     source += "\n";
 
-    String line = "[DllImport (LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
+    String line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
     source += IndentLine(line);
     JSBClass* klass = function_->GetClass();
     JSBPackage* package = klass->GetPackage();
 
     String returnType = CSTypeHelper::GetNativeTypeString(function_->GetReturnType());
 
+    if (function_->IsConstructor())
+        returnType = "IntPtr";
+
     Vector<JSBFunctionType*>& parameters = function_->GetParameters();
 
     Vector<String> args;

+ 10 - 4
Source/ToolCore/JSBind/CSharp/CSModuleWriter.cpp

@@ -233,7 +233,7 @@ void CSModuleWriter::GenerateManagedEnumsAndConstants(String& source)
 
         source += "\n";
 
-        String line = "public static class Constants\n";
+        String line = "public static partial class Constants\n";
         source += IndentLine(line);
         source += IndentLine("{\n");
 
@@ -256,7 +256,7 @@ void CSModuleWriter::GenerateManagedEnumsAndConstants(String& source)
             if (value == "M_MAX_UNSIGNED")
                 value = "0xffffffff";
 
-            String line = "public static const " + managedType + " " + cname + " = " + value;
+            String line = "public const " + managedType + " " + cname + " = " + value;
 
             if (managedType == "float" && !line.EndsWith("f"))
                 line += "f";
@@ -282,9 +282,15 @@ void CSModuleWriter::GenerateManagedEnumsAndConstants(String& source)
 
 void CSModuleWriter::GenerateManagedSource()
 {
-    String source;
+    String source = "// Autogenerated";
 
-    source += "namespace " + module_->GetPackage()->GetName() + "\n";
+    source += "\nusing System;\nusing System.Collections.Generic;\nusing System.Runtime.InteropServices;\n\n\n";
+
+    String moduleName = module_->GetPackage()->GetName();
+    if (moduleName == "Atomic")
+        moduleName = "AtomicSharp";
+
+    source += "namespace " + moduleName + "\n";
     source += "{\n";
 
     GenerateManagedEnumsAndConstants(source);

+ 6 - 2
Source/ToolCore/JSBind/CSharp/CSTypeHelper.cpp

@@ -56,7 +56,7 @@ String CSTypeHelper::GetManagedTypeString(JSBType* type)
     return value;
 }
 
-String CSTypeHelper::GetManagedTypeString(JSBFunctionType* ftype)
+String CSTypeHelper::GetManagedTypeString(JSBFunctionType* ftype, bool addName)
 {
     if (!ftype)
         return "void";
@@ -65,9 +65,13 @@ String CSTypeHelper::GetManagedTypeString(JSBFunctionType* ftype)
 
     if (ftype->name_.Length())
     {
-        parameter += " " + ftype->name_;
+        if (addName)
+            parameter += " " + ftype->name_;
+
+        /*
         if (ftype->initializer_.Length())
             parameter += " = " + ftype->initializer_;
+        */
     }
 
     return parameter;

+ 1 - 1
Source/ToolCore/JSBind/CSharp/CSTypeHelper.h

@@ -31,7 +31,7 @@ public:
     static String GetNativeTypeString(JSBFunctionType* ftype);
 
     static String GetManagedTypeString(JSBType* type);
-    static String GetManagedTypeString(JSBFunctionType* ftype);
+    static String GetManagedTypeString(JSBFunctionType* ftype, bool addName = true);
 
 };