UnitySendMessage doesn't work on iOS - ios

I was trying to send some data using UnitySendMessage in iOS plugin, but it seem like it doesn't do anything, i don't see any errors and response. I checked function where UnitySendMessage is located, and i'm 100% sure that it starts.
- (void) callback{
NSLog(#"UNITY test callback");
UnitySendMessage("PluginTest", "listener", "test callback");
}
On scene i have gameObject named PluginTest with pluginListener component which contains method:
public void listener(string parameter)
{
print(parameter);
}
I also added to UnityAppController.mm this line, which was recommended to add in some post:
extern void UnitySendMessage(const char *, const char *, const char *);
I have no idea what i'm doing wrong, unitySendMessage seems like it should work without much effort.
Any help will be appreciated

Related

Using SNMP++ method with callback in .mm file

I am using SNMP++ library in my project and everything works fine. However, there is a method where I need to get callback in my .mm file. Now when I am creating a block and passing it to that function as parameter, it throws an error "No matching member function for call to 'get_bulk'". Here is the piece of code:
void(^callbackFunc)(int,Snmp*,Pdu&,SnmpTarget&,void*);
callbackFunc = ^(int i,Snmp* s,Pdu& p,SnmpTarget& t,void* v) {
};
snmp.get_bulk(pdu, *target, l_repeaters, l_repetitions,callbackFunc);
Also, here is the function signature for "get_bulk" function:
int Snmp::get_bulk(Pdu &pdu, // pdu to use
const SnmpTarget &target, // destination target
const int non_repeaters, // number of non repeaters
const int max_reps, // maximum number of repetitions
const snmp_callback callback,// callback to use
const void * callback_data) // callback data
{
pdu.set_type( sNMP_PDU_GETBULK_ASYNC);
return snmp_engine( pdu, non_repeaters, max_reps, target,
callback, callback_data);
}
What should I pass in 'callback' type?This is the typedef for SNMP_callback:
typedef void (*snmp_callback)(int reason, Snmp *session,
Pdu &pdu, SnmpTarget &target, void *data);
I am stuck on this for the past 4-5 hours now and I can't figure out how to resolve this.
Apple's blocks are not convertible to function pointers, as they also contain data (captured variables, etc.) and a reference counting mechanism. You will need to pass a free function, static C++ class member function, or a C++ non-capturing lambda as the callback.
The lambda is the closest syntactically to a block; only non-capturing lambdas are convertible to a function pointer, however, so you will need to do the capturing "by hand" by passing a pointer to a context struct or similar through the void* callback_data argument which presumably is passed through to the callback as void* data.
The lambda will look something like this:
snmp_callback callback =
[](int reason, Snmp *session, Pdu &pdu, SnmpTarget &target, void *data)
{
// context_struct_type* context = static_cast<context_struct_type*>(data);
};

why I can't hook my private function with fishhook

These days, I found that hook in an iOS application is hard, and found that there is a tool called "fishhook", created by facebook. I import the tool in my personal project, but it doesn't work. Am I wrong? Here are the source code:
#import <dlfcn.h>
#import <Foundation/Foundation.h>
#import "fishhook.h"
static void (*orig_testABC)(void);
void testABC()
{
NSLog(#"This is main log...");
}
void my_testABC()
{
NSLog(#"This is other log, not main log...");
}
void save_original_symbols()
{
// void *handle = dlopen("/Users/bianyiji/Library/Developer/Xcode/DerivedData/HookTest-ghlgmahvsgfbqeekbrouzdyoxgdw/Build/Intermediates/HookTest.build/Debug/HookTest.build/Objects-normal/x86_64/main.o", RTLD_LAZY);
// printf("%s\n", handle);
orig_testABC = dlsym(RTLD_DEFAULT, "testABC");
}
int main(int argc, const char * argv[]) {
#autoreleasepool {
// save_original_symbols();
int rebind_int = rebind_symbols((struct rebinding[1]){"testABC", my_testABC}, 1);
printf("%d\n", rebind_int);
}
testABC();
return 0;
}
Although I called the function "testABC()", but I use "rebind_symbols" before, why I can't get my expected result...
fishhook isn't meant to hook into private functions, because fishhook works by rebinding symbols that are present in the symbol table.
for hooking private functions you need read-write access in the executable memory pages of your running app, and this is obviously not possible for security reasons.
however, in jailbroken iOS, the kernel has a patch that allows this, so you can hook private functions with frameworks such as CydiaSubstrate or substitute. But fishhook does not support this and probably never will, also if it did would never be AppStore-friendly.
Source: our open-source SSL pinning library TrustKit uses fishhook and we introduced it at BlackHat 2015 elaborating an all these topics.

iOS UnitySendMessage call parameter

I am writing the Objective-C part of a Unity project. In AppController.mm, I declared
extern void UnitySendMessage(const char *, const char *, const char *);
And I am calling this like,
- (void)callUnityObject:(const char*)object Method:(const char*)method Parameter:(const char*)parameter
{
UnitySendMessage(object, method, parameter);
}
But I have a Unity function that has to receive an int parameter.
So, if I call like this:
[self callUnityObject:"_iosManager" Method:"GiveDynamite" Parameter:"50"];
The app doesn't crash, but the call doesn't work and I am getting an output like this:
The best match for method GiveDynamite has some invalid parameter.
If I call like this:
[self callUnityObject:"_iosManager" Method:"GiveDynamite" Parameter:50];
The App is crashing.
How can I send this message from Objective-c to Unity?
I tried declaring a new method like this:
extern void UnitySendMessage(const char *, const char *, int);
But the app crashed and said that unity doesn't have a function declaration like that.
Thanks in advance.
According to:
void UnitySendMessage( const char * className, const char * methodName, const char * param )
You should pass char*:
[self callUnityObject:"_iosManager" Method:"GiveDynamite" Parameter:"50"];
On Unity class you should receive "string" param
void GiveDynamite(string dinamite) {
...
}
and then parse it to integer value, f.e:
dinamiteAmount = int.Parse(dinamite);

GNU Guile SCM to char*

I am relative new to FFI and GNU Guile, and I am writing bindings to a library that heavily uses char* variables. Here is code from function, that wraps C function:
static inline char*
scm_to_ascii_string(SCM string)
{
return SCM_UNBNDP(SCM) ? NULL
: scm_to_stringn(string, NULL, "ascii", SCM_FAILED_CONVERSION_ERROR);
}
SCM_DEFINE(func, "func", ...)
{
...
char *server_pass = scm_to_ascii_string(scm_server_pass);
char *username = scm_to_ascii_string(scm_username);
char *realname = scm_to_ascii_string(scm_realname);
}
Problem is that any call to conversion function can throw error, leaving me with memory leak.
What can I do about it?
You could make the output part an argument eg:
void scm_to_ascii_string(SCM string, char* &out);
edit:
I guess you meant what exception handler methods are there on the c side, I think there might be something on that in the manual in one of the two sections on programming stuff in C.

pthread compile error

I am trying to create a thread using pthread. So far I have this:
sample.h:
void* ReceiveLoop(void*);
pthread_t mythread;
sample.cpp:
void* ReceiveLoop(void*) {
cout<<"whatever";
}
void sample::read() {
pthread_create(&mythread, NULL, ReceiveLoop, NULL);
}
Which I think is ok having read some posts about this. I have also tried with
pthread_create(&mythread, NULL, &ReceiveLoop, NULL);
But I get this:
.cpp:532: error: no matches converting function 'ReceiveLoop' to type 'void* (*)(void*)'
.cpp:234: error: void* sample::ReceiveLoop(void*)
Anyone can help me? Thanks.
I recall a few idiosyncrasies between older versions of gcc/g++ with regards to errors like this. You didn't indicate the compiler you were using.
Go ahead and give the void* parameter passed to ReceiveLoop a name:
void ReceiveLoop(void* threadarg);
void* ReceiveLoop(void* threadarg){ cout<<"whatever"; }
For some reason, I seem to recall that's the only way I could get a particular piece of code to compile on some random compiler even though the parameter passed in wasn't actually used.
Also, if ReceiveLoop is a member function of a class, it needs to be declared static.
class sample
{
public:
void ReceiveLoopImpl()
{
cout<<"whatever";
}
static void* ReceiveLoop(void* threadargs)
{
return ((sample*)threadargs)->RecieveLoopImpl();
}
void read()
{
pthread_create(&mythread, NULL, sample::ReceiveLoop, this);
}
};

Resources