lidgren / lidgren-network-gen3

Lidgren Network Library
https://groups.google.com/forum/#!forum/lidgren-network-gen3
MIT License
1.19k stars 331 forks source link

Does not run on Unity/Android #18

Closed tr00p3r closed 9 years ago

tr00p3r commented 9 years ago

I compiled my working (Unity Windows) to Android and I receive this error:

I cannot add the define ANDROID as I need a replacement for this line of code:

Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)Android.App.Application.Context.GetSystemService(Android.App.Activity.WifiService);

04-28 02:34:30.886: D/Unity(1350): Unable to lookup library path for 'libc', native render plugin support disabled. 04-28 02:34:30.886: E/Unity(1350): Unable to find libc 04-28 02:34:30.890: D/Unity(1350): Unable to lookup library path for 'libc', native render plugin support disabled. 04-28 02:34:30.890: E/Unity(1350): Unable to find libc 04-28 02:34:30.894: D/Unity(1350): Unable to lookup library path for 'libc', native render plugin support disabled. 04-28 02:34:30.894: E/Unity(1350): Unable to find libc 04-28 02:34:30.898: D/Unity(1350): Unable to lookup library path for 'libc', native render plugin support disabled. 04-28 02:34:30.898: E/Unity(1350): Unable to find libc 04-28 02:34:30.998: I/Unity(1350): NullReferenceException: Object reference not set to an instance of an object 04-28 02:34:30.998: I/Unity(1350): at Lidgren.Network.NetPeer.InitializeNetwork () [0x000df] in C:\Users\David\Documents\LidgrenTest\Assets\Lidgren.Network\Lidgren.Network\NetPeer.Internal.cs:188 04-28 02:34:30.998: I/Unity(1350): at Lidgren.Network.NetPeer.Start () [0x0005f] in C:\Users\David\Documents\LidgrenTest\Assets\Lidgren.Network\Lidgren.Network\NetPeer.cs:151 04-28 02:34:30.998: I/Unity(1350): at Lidgren.MatchMaking.Client..ctor (System.String name, System.String masterServerKey, System.String masterServerIP, Int32 masterServerPort) [0x001df] in C:\Users\David\Documents\LidgrenTest\Assets\Lidgren.MM\MatchMaking.cs:83 04-28 02:34:30.998: I/Unity(1350): at MultiplayerTest+c__Iterator1.MoveNext () [0x00031] in C:\Users\David\Documents\LidgrenTest\Assets\MultiplayerTest.cs:17

lidgren commented 9 years ago

The unity/android defines are completely community driven - if you find the solution to this problem, please post back here and I will incorporate it.

tr00p3r commented 9 years ago

Requires a new PlatformUnity.cs file with specific unity code.

I got it working on Android, testing OSX shortly.

tr00p3r commented 9 years ago

PlatformWin32 needs something to stop it compiling on Unity...

I use this:

if !ANDROID && !CONSTRAINED && !WINDOWS_RUNTIME && !UNITY_ANDROID && !UNITY_STANDALONE_OSX && !UNITY_STANDLONE_WIN && !UNITY_STANDLONE_LINX && !UNITY_IOS

tr00p3r commented 9 years ago

Then create a PlatformUnity.cs with this:

if UNITY_ANDROID || UNITY_STANDALONE_OSX || UNITY_STANDLONE_WIN || UNITY_STANDLONE_LINX || UNITY_IOS

using System; using System.Collections.Generic; using System.Net; using System.Security.Cryptography;

namespace Lidgren.Network { public static partial class NetUtility { private static byte[] s_randomMacBytes;

    static NetUtility()
    {
        s_randomMacBytes = new byte[8];
        MWCRandom.Instance.NextBytes(s_randomMacBytes);
    }

    [CLSCompliant(false)]
    public static ulong GetPlatformSeed(int seedInc)
    {
        ulong seed = (ulong)Environment.TickCount + (ulong)seedInc;
        return seed ^ ((ulong)(new object().GetHashCode()) << 32);
    }

    /// <summary>
    /// Gets my local IPv4 address (not necessarily external) and subnet mask
    /// </summary>
    public static IPAddress GetMyAddress(out IPAddress mask)
    {
        mask = null;
        try
        {

            if (!(UnityEngine.Application.internetReachability == UnityEngine.NetworkReachability.NotReachable))
                return null;

            return IPAddress.Parse(UnityEngine.Network.player.externalIP);

        }
        catch // Catch Access Denied errors
        {
            return null;
        }
    }

    public static byte[] GetMacAddressBytes()
    {
        return s_randomMacBytes;
    }

    public static void Sleep(int milliseconds)
    {
        System.Threading.Thread.Sleep(milliseconds);
    }

    public static IPAddress GetBroadcastAddress()
    {
        return IPAddress.Broadcast;
    }

    public static IPAddress CreateAddressFromBytes(byte[] bytes)
    {
        return new IPAddress(bytes);
    }

    private static readonly SHA1 s_sha = SHA1.Create();
    public static byte[] ComputeSHAHash(byte[] bytes, int offset, int count)
    {
        return s_sha.ComputeHash(bytes, offset, count);
    }
}

public static partial class NetTime
{
    private static readonly long s_timeInitialized = Environment.TickCount;

    /// <summary>
    /// Get number of seconds since the application started
    /// </summary>
    public static double Now { get { return (double)((uint)Environment.TickCount - s_timeInitialized) / 1000.0; } }
}

}

endif

tr00p3r commented 9 years ago

I still need time to test on iOS but I'll check that out during the week,

tr00p3r commented 9 years ago

Actually... ignore that. I'll let you know when I'm happy with it :)

lidgren commented 9 years ago

Unity-specific changes to GetMyAddress incorporated in PlatformConstrained.cs