Coder Social home page Coder Social logo

tuarua / fresharp Goto Github PK

View Code? Open in Web Editor NEW
48.0 10.0 7.0 4.3 MB

C# wrapper for FlashRuntimeExtensions

License: Apache License 2.0

C# 50.43% C++ 12.68% ActionScript 16.13% Batchfile 4.35% Pascal 3.96% Inno Setup 10.72% C 1.73%
adobe-air ane air-native-extensions csharp dotnet-framework actionscript

fresharp's Introduction

FreSharp

Download

Features

  • Build Adobe Air Native Extensions using C#

The package is hosted on NuGet at https://www.nuget.org/packages/TuaRua.FreSharp/


Getting Started

A basic Hello World starter project is included

How to use

Converting from FREObject args into C# types, returning FREObjects

The following table shows the primitive as3 types which can easily be converted to/from C# types

AS3 type C# type AS3 param->C# return C#->AS3
String string var str = argv[0].AsString() return str.ToFREObject()
int int var i = argv[0].AsInt() return i.ToFREObject()
Boolean bool var b = argv[0].AsBool() return b.ToFREObject()
Number double var dbl = argv[0].AsDouble() return dbl.ToFREObject()
uint ARGB Color var clr = argv[0].AsColor() return clr.ToFREObject()
Date DateTime var dt = argv[0].AsDateTime() return dt.ToFREObject()
Rectangle Rect var rect = argv[0].AsRect() return rect.ToFREObject()
Point Point var pnt = argv[0].AsPoint() return pnt.ToFREObject()
BitmapData Bitmap var bmp = argv[0].AsBitmap() return bmp.ToFREObject()
Array string[] var arr = argv[0].AsStringArray() return arr.ToFREObject()
Array int[] var arr = argv[0].AsIntArray() return arr.ToFREObject()
Array double[] var arr = argv[0].AsDoubleArray() return arr.ToFREObject()
Array bool[] var arr = argv[0].AsBoolArray() return arr.ToFREObject()
Object Dictionary var dct = argv[0].AsDictionary() N/A
null FREObject.Zero return FREObject.Zero

Basic Types

string myString = argv[0].AsString();
int myInt = argv[1].AsInt();
bool myBool = argv[2].AsBool();

const string sharpString = "I am a string from C#";
return sharpString.ToFREObject();

Creating new FREObjects

var frePerson = new FREObject().Init("com.tuarua.Person");

// create a FREObject passing args
// 
// The following param types are allowed: 
// int, uint, short, long, bool, string, double, Rect, Point, DateTime, Color, FREObject
var frePerson = new FREObject().Init("com.tuarua.Person", "Bob", "Doe", 28, myFREObject);

Calling Methods

// call a FREObject method passing args
// 
// The following param types are allowed: 
// int, uint, short, long, bool, string, double, Rect, Point, DateTime, Color, FREObject
var addition = freCalculator.Call("add", 100, 33);

Getting / Setting Properties

var oldAge = person.GetProp("age").AsInt();
var newAge = oldAge + 10;

// The following param types are allowed: 
// int, uint, short, long, bool, string, double, Rect, Point, DateTime, Color, FREObject
person.SetProp("age", newAge);

// create a FreSharpObject DynamicObject 
dynamic person = new FreObjectSharp("com.tuarua.Person", "Ben McBobster", 80);
int oldAge = person.age; // implicit conversion
var name = (string) person.name; // explicit conversion

// The following prop types are allowed: 
// int, uint, short, long, bool, string, double, Rect, Point, DateTime, Color, FREObject
person.age = oldAge + 10;

Arrays

var inFre0 = new FREArray(argv[0]);
// convert to a C# [string]
var airStringVector = inFre0.AsStringArray();

// create a Vector.<com.tuarua.Person> with fixed length of 5
var newFreArray = new FREArray("com.tuarua.Person", 5, true);
var len = newFreArray.Length;

// loop over FREArray
foreach (var fre in freIntArray) {
    Trace(fre.AsInt());
}

// set element 1 to 123
freIntArray[1] = 123.ToFREObject();

// push 2 elements to FREArray
freIntArray.Push(22, 33);

// return C# [int] to AIR
var marks = new[] {99, 98, 92, 97, 95};
return marks.ToFREObject();

Sending Events back to AIR

Trace("Hi", "There");

// with interpolation
Trace($"My name is: {name}");

DispatchEvent(name: "MY_EVENT", value: "this is a test"); 

Bitmapdata

// read AS3 bitmapData into a Bitmap
var bitmap = new FreBitmapDataSharp(argv[0]).AsBitmap();

return bitmap.ToFREObject();

ByteArrays

var ba = new FreByteArraySharp(inFre);
ba.Acquire();
var byteData = ba.Bytes;
var base64Encoded = Convert.ToBase64String(byteData);
ba.Release();

Error Handling

// Turn on logging to trace out any captured errors in FreSharp
FreSharpLogger.GetInstance().Context = Context;

person.Call("add", 100); // not passing enough args - traces captured error.

try {
    myCSharpFunc(); // call a C# method which can throw
}
catch (Exception e) {
    return new FreException(e).RawValue; // return as3 error and throw in swc
}

Advanced: Extending FreObjectSharp. Creating a C# version of flash.geom.point

using FREObject = System.IntPtr;
using Point = System.Windows.Point;

public static class FrePoint {
    public static FREObject ToFREObject(this Point value) {
        return new FREObject().Init("flash.geom.Point", value.X, value.Y);
    }

    public static Point AsPoint(this FREObject inFre) {
        dynamic fre = new FreObjectSharp(inFre);
        return new Point(fre.x, fre.y);
    }
}

Required AS3 classes

com.tuarua.fre.ANEUtils.as and com.tuarua.fre.ANEError.as are required by FreSharp and should be included in the AS3 library of your ANE

Required Dependencies

Starting from version 2.2.0 FreSharp.ane is required in your AIR project.
This ANE contains the required FreSharp dlls and also handles the searching for and loading of a project's C# dll when packaged inside the ANE.
No more copying these files into your AIRSDK!

Tech

Uses .NET 4.6

Prerequisites

You will need

  • Visual Studio 2017
  • AIR 19+ SDK

fresharp's People

Contributors

tuarua avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fresharp's Issues

System.IO.FileNotFoundException at TuaRua.FreSharp.FreSharpHelper.DispatchEvent

This error is driving my crazy... In my dev computer everything works perfect, but when I run my app in the client test computer (Windows 8.1 Pro 32-bit), I get this exception:

System.IO.FileNotFoundException: Could not load file or assembly 'FreSharpCore.dll' or one of its dependencies. The specified module could not be found.
File name: 'FreSharpCore.dll'
   at TuaRua.FreSharp.FreSharpHelper.DispatchEvent(IntPtr& freContext, String name, String value)
   at PlayerBridgeANELib.MainController.RaiseEvent(String eventType, String jsonEventInfo)
   at PlayerBridgeANELib.MainController.NotifyException(Exception ex)
   at PlayerBridgeANELib.MainController.WatchSpontania()
   at PlayerBridgeANELib.MainController.InitController(IntPtr ctx, UInt32 argc, IntPtr[] argv)

The exception line is in my code is in the SendEvent(...) method call.

  • .Net 4.6 installed ✔️
  • VC++ redist installed ✔️
  • My C# ANE lib dll, FreeSharp.dll, FreSharpCore.dll in the .exe folder ✔️

If the DispatchEvent method is reached, I guess FreSharpCore.dll is loaded but not one of it dependencies, but FreSharp.dll is in the same folder. So which is the missing file? Or maybe I have to place dlls in other location?

The Windows Event viewer and the Windows Error Reporting file don't give any useful information (or I don't interpreting it properly...).

Any clue is welcome.

Help

The content cannot be loaded because there was a problem loading an extension: Error: Extension namespace is invalid for C:\Users\pc\AppData\Local\Temp\Tmp_ANE_File_Unzipped_Packages\FreSharp.ane

AIR application crash: System.IO.FileNotFoundException

I'm not sure if this FreSharp issue or just a common one with ANE developement.

When I run my app it crashes. Looking at the windows event log I get this:

Application: CarteleriaApp.exe
Framework version: v4.0.30319
Description:  Unhandled exception.
Exception information: System.IO.FileNotFoundException
   at <Module>.FreSharpBridge.InitController()
   at <Module>.CDIPB_contextInitializer(Void*, Byte*, Void*, UInt32*, FRENamedFunction_**)

("CDIPB" is my chosen identifier)

I have successfully run the ANE launching it from Flash CC Professional (I guess because all dll's are copied at the AIR_SDK\bin from build-windows.bat), but when I install and run the application it crashes.

My app is installed with the native installer created with adt -target native. The application with the extension seems to be installed ok, as all needed files are at the folder

C:\Program Files (x86)\MyCompany\MyApp\META-INF\AIR\extensions\es.telecor.vt.cdi.bridge.PlayerBridgeExtension\Meta-Inf\ane\Windows-x86\
--- FreSharp.dll
--- FreSharpCore.dll
--- library.swc
--- PlayerBridgeANE.dll (my C++ FreSharp wrapper)
--- PlayerBridgeANELib.dll (my C# Main controller)

The error stack suggests that the main C++ DLL is loaded ok because the context initializer is invoked but not the C# library. Maybe the additional libs have to be somewhere else, but the extension dir seems the right place to me...

EDIT:
I've get the windows dump file and it confirm that de PlayerBridgeANE.dll (the C++ one) is loaded but not PlayerBridgeANELib.dll (C#). Also debugging the crash with Visual Studio, it stops at FREBRIDGE_INIT macro with

Unhandled exception type 'System.IO.FileNotFoundException' at PlayerBridgeANE.dll.
Unable to load the file or assembly 'PlayerBridgeANELib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 
or one of its dependencies.  The system can not find the specified file.

Question : FreSharp for Android

FreSharp is for windows platform only. But is there a similar solution for Android to build Air Native Extensions using C# and Xamarin ?
Thanks, Alex

Add call to ContextFinalizer in c# lib

I've implemented a call to a method in the C# lib from the C++ ContextFinalizer function in my ANE project because I need to dispose some resources when closing the app.

I think it would be a good idea to have this as a built-in feature in other to allow the C# lib to free resources (files, listeners, connections, process hooks or whatever....).

Problem with FreSharp If I upgrade to Net Frameworks 4.6.1 and Adobe Air SDK 24.0

Hello dear Tuana!

I have managed your FreSharp and FreSharpCore in Visual Studio 2017 Community and I have upgraded than I change both dlls from FreSharp and FreSharpCore FreSharpExampleAne(+lib) And I have changed sources because I wish I have tried. But no success for Adobe Air.
And I have completed successful complications than I merge Flash Builder 4.6 with Adobe Air SDK 24.0

And I add native extension to my example air application. But Adobe Air always freeze. I tried to export to native installer and no success. Why does it happen? Is it bug because AS3-reference ExtensionContext doesn't answer, right? - But I didn't mess up with FreSharp.

How is your work around with Adobe Air SDK 19.0? Does it work while ExtensionContext answers? If you open Air Application and Air Application doesn't freeze.

I'm sorry for bad English. Thanks!

Step by step guide

Is it posible to have a step by step guide for building an ANE from scratch? I'm trying to write my first Extension but just looking at the example I can't figure out how to glue all the pieces and which are needed for a new extension or are just part of the example.

Regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.